Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support cmake find_package #293

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.5...3.22)

project(polyscope)
project(polyscope VERSION 2.3.1)

include(cmake/macros.cmake)

### Policy settings
cmake_policy(SET CMP0054 NEW) # don't implicitly dereference inside if()
Expand Down
5 changes: 5 additions & 0 deletions cmake/cmake_config.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@PACKAGE_INIT@

include(CMakeFindDependencyMacro)
@PACKAGE_FIND_DEPENDENCY_CALLS@
include("${CMAKE_CURRENT_LIST_DIR}/@EXPORTED_TARGETS_FILE@")
26 changes: 26 additions & 0 deletions cmake/cmake_uninstall.cmake.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -----------------------------------------------
# File that provides "make uninstall" target
# We use the file 'install_manifest.txt'
# -----------------------------------------------
if(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")
MESSAGE(FATAL_ERROR "Cannot find install manifest: \"@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt\"")
endif(NOT EXISTS "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt")

file(READ "@CMAKE_CURRENT_BINARY_DIR@/install_manifest.txt" files)
string(REGEX REPLACE "\n" ";" files "${files}")
foreach(file ${files})
message(STATUS "Uninstalling \"$ENV{DESTDIR}${file}\"")
if(EXISTS "$ENV{DESTDIR}${file}")
exec_program(
"@CMAKE_COMMAND@" ARGS "-E remove \"$ENV{DESTDIR}${file}\""
OUTPUT_VARIABLE rm_out
RETURN_VALUE rm_retval
)
if(NOT "${rm_retval}" STREQUAL 0)
message(FATAL_ERROR "Problem when removing \"$ENV{DESTDIR}${file}\"")
endif(NOT "${rm_retval}" STREQUAL 0)
else(EXISTS "$ENV{DESTDIR}${file}")
message(STATUS "File \"$ENV{DESTDIR}${file}\" does not exist.")
endif(EXISTS "$ENV{DESTDIR}${file}")
endforeach(file)

51 changes: 51 additions & 0 deletions cmake/macros.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
option(CMAKE_EXPORT_COMPILE_COMMANDS "Export compile_commands.json" ON)

set(CONFIG_BASENAME "${PROJECT_NAME}Config")
set(CONFIG_FILENAME "${CONFIG_BASENAME}.cmake")
set(VERSION_FILENAME "${CONFIG_BASENAME}Version.cmake")
set(EXPORTED_TARGETS_NAME "${PROJECT_NAME}Targets")
set(EXPORTED_TARGETS_FILE "${EXPORTED_TARGETS_NAME}.cmake")

include(GNUInstallDirs)
set(INSTALL_BIN_DIR "${CMAKE_INSTALL_BINDIR}")
set(INSTALL_LIB_DIR "${CMAKE_INSTALL_LIBDIR}/${PROJECT_NAME}")
set(INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}")
set(INSTALL_CMAKE_DIR "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}")
set(INSTALL_PKGCONFIG_DIR "${CMAKE_INSTALL_DATADIR}/pkgconfig")

macro (package)
include(CMakePackageConfigHelpers)

write_basic_package_version_file(
"${PROJECT_BINARY_DIR}/${VERSION_FILENAME}"
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion)

configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/cmake_config.cmake.in"
"${PROJECT_BINARY_DIR}/${CONFIG_FILENAME}"
INSTALL_DESTINATION ${INSTALL_CMAKE_DIR})

install(
EXPORT ${EXPORTED_TARGETS_NAME}
FILE ${EXPORTED_TARGETS_FILE}
NAMESPACE ${PROJECT_NAME}::
DESTINATION ${INSTALL_CMAKE_DIR})

install(FILES "${PROJECT_BINARY_DIR}/${CONFIG_FILENAME}"
"${PROJECT_BINARY_DIR}/${VERSION_FILENAME}"
DESTINATION ${INSTALL_CMAKE_DIR})

configure_file("${PROJECT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
"${PROJECT_BINARY_DIR}/cmake_uninstall.cmake" @ONLY IMMEDIATE)

if (NOT TARGET uninstall)
add_custom_target(uninstall "${CMAKE_COMMAND}" -P
"${PROJECT_BINARY_DIR}/cmake_uninstall.cmake")
else ()
add_custom_target(
${PROJECT_NAME}_uninstall "${CMAKE_COMMAND}" -P
"${PROJECT_BINARY_DIR}/cmake_uninstall.cmake")
add_dependencies(uninstall ${PROJECT_NAME}_uninstall)
endif ()
endmacro ()
5 changes: 5 additions & 0 deletions deps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ if("${POLYSCOPE_BACKEND_OPENGL3_GLFW}")
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
add_subdirectory(glfw)
install(TARGETS glfw EXPORT ${EXPORTED_TARGETS_NAME} DESTINATION "${INSTALL_LIB_DIR}")
endif()
endif()

Expand All @@ -20,6 +21,10 @@ if(NOT TARGET glm::glm)
add_subdirectory(glm)
target_compile_definitions(glm INTERFACE GLM_ENABLE_EXPERIMENTAL)
set_target_properties(glm PROPERTIES LINKER_LANGUAGE CXX)

install(TARGETS glm EXPORT ${EXPORTED_TARGETS_NAME} DESTINATION "${INSTALL_LIB_DIR}")
install(TARGETS glm-header-only EXPORT ${EXPORTED_TARGETS_NAME} DESTINATION "${INSTALL_LIB_DIR}")
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/glm/glm DESTINATION "${INSTALL_INCLUDE_DIR}")
endif()

## Imgui
Expand Down
7 changes: 6 additions & 1 deletion deps/MarchingCubeCpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
add_library(MarchingCube INTERFACE)
add_library(MarchingCube::MarchingCube ALIAS MarchingCube)

target_include_directories(MarchingCube INTERFACE include)
target_include_directories(MarchingCube INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

install(TARGETS MarchingCube EXPORT ${EXPORTED_TARGETS_NAME} DESTINATION "${INSTALL_LIB_DIR}")
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include DESTINATION "${INSTALL_INCLUDE_DIR}")
7 changes: 6 additions & 1 deletion deps/glad/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ if (library_type STREQUAL SHARED_LIBRARY)
target_compile_definitions(glad PUBLIC GLAD_GLAPI_EXPORT PRIVATE GLAD_GLAPI_EXPORT_BUILD)
endif()

target_include_directories(glad PUBLIC "../include")
target_include_directories(glad PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
$<INSTALL_INTERFACE:include>)

set_target_properties(glad PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

install(TARGETS glad EXPORT ${EXPORTED_TARGETS_NAME} DESTINATION "${INSTALL_LIB_DIR}")
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../include DESTINATION "${INSTALL_INCLUDE_DIR}")
11 changes: 9 additions & 2 deletions deps/imgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ if("${POLYSCOPE_BACKEND_OPENGL3_GLFW}")
${SRCS}
)

target_include_directories(imgui PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/imgui/")
target_include_directories(imgui PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/imgui>
$<INSTALL_INTERFACE:include>)
target_include_directories(imgui PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../glfw/include/")
target_include_directories(imgui PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../glad/include/")

Expand Down Expand Up @@ -66,11 +68,16 @@ elseif("${POLYSCOPE_BACKEND_OPENGL_MOCK}")
${SRCS}
)

target_include_directories(imgui PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/imgui/")
target_include_directories(imgui PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/imgui>
$<INSTALL_INTERFACE:include>)

endif()


target_compile_features(imgui PUBLIC cxx_std_11)

set_target_properties(imgui PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

install(TARGETS imgui EXPORT ${EXPORTED_TARGETS_NAME} DESTINATION "${INSTALL_LIB_DIR}")
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/imgui/ DESTINATION "${INSTALL_INCLUDE_DIR}")
7 changes: 6 additions & 1 deletion deps/json/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
add_library(nlohmann_json INTERFACE)
add_library(nlohmann_json::nlohmann_json ALIAS nlohmann_json)

target_include_directories(nlohmann_json INTERFACE include)
target_include_directories(nlohmann_json INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_compile_features(nlohmann_json INTERFACE cxx_std_11)

install(TARGETS nlohmann_json EXPORT ${EXPORTED_TARGETS_NAME} DESTINATION "${INSTALL_LIB_DIR}")
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include DESTINATION "${INSTALL_INCLUDE_DIR}")
6 changes: 5 additions & 1 deletion deps/stb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ add_library(
stb_impl.cpp
)

target_include_directories(stb PUBLIC ".")
target_include_directories(stb PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>)

set_target_properties(stb PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

install(TARGETS stb EXPORT ${EXPORTED_TARGETS_NAME} DESTINATION "${INSTALL_LIB_DIR}")
9 changes: 7 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,10 @@ SET(HEADERS

# Create a single library for the project
add_library(polyscope ${SRCS} ${BACKEND_SRCS} ${HEADERS} ${BACKEND_HEADERS})
add_library(polyscope::polyscope ALIAS polyscope)

# Mark the library target and its headers as installable
install(TARGETS polyscope)
install(TARGETS polyscope EXPORT ${EXPORTED_TARGETS_NAME} DESTINATION "${INSTALL_LIB_DIR}")
install(DIRECTORY ${INCLUDE_ROOT}/ DESTINATION "${CMAKE_INSTALL_PREFIX}/include/polyscope")

# Required compiler settings
Expand All @@ -368,8 +369,12 @@ set_target_properties(polyscope PROPERTIES POSITION_INDEPENDENT_CODE TRUE)
add_definitions(-DNOMINMAX)

# Include settings
target_include_directories(polyscope PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../include")
target_include_directories(polyscope PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
$<INSTALL_INTERFACE:include>)

# Link settings
target_link_libraries(polyscope PUBLIC imgui glm::glm)
target_link_libraries(polyscope PRIVATE "${BACKEND_LIBS}" stb nlohmann_json::nlohmann_json MarchingCube::MarchingCube)

package()