From 6d31c16b26bb2c7feeb723dd7d3aff6d27d0ea8d Mon Sep 17 00:00:00 2001 From: Steve Walker <65963536+etherswangel@users.noreply.github.com> Date: Sat, 31 Aug 2024 15:49:45 +0800 Subject: [PATCH 1/3] support cmake find_package --- CMakeLists.txt | 4 ++- cmake/cmake_config.cmake.in | 5 +++ cmake/cmake_uninstall.cmake.in | 26 +++++++++++++++ cmake/macros.cmake | 51 +++++++++++++++++++++++++++++ deps/CMakeLists.txt | 5 +++ deps/MarchingCubeCpp/CMakeLists.txt | 7 +++- deps/glad/src/CMakeLists.txt | 7 +++- deps/imgui/CMakeLists.txt | 7 +++- deps/json/CMakeLists.txt | 7 +++- deps/stb/CMakeLists.txt | 6 +++- src/CMakeLists.txt | 9 +++-- 11 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 cmake/cmake_config.cmake.in create mode 100644 cmake/cmake_uninstall.cmake.in create mode 100644 cmake/macros.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 77dc97b9..3d039787 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/cmake/cmake_config.cmake.in b/cmake/cmake_config.cmake.in new file mode 100644 index 00000000..9989ceb1 --- /dev/null +++ b/cmake/cmake_config.cmake.in @@ -0,0 +1,5 @@ +@PACKAGE_INIT@ + +include(CMakeFindDependencyMacro) +@PACKAGE_FIND_DEPENDENCY_CALLS@ +include("${CMAKE_CURRENT_LIST_DIR}/@EXPORTED_TARGETS_FILE@") diff --git a/cmake/cmake_uninstall.cmake.in b/cmake/cmake_uninstall.cmake.in new file mode 100644 index 00000000..9d565491 --- /dev/null +++ b/cmake/cmake_uninstall.cmake.in @@ -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) + diff --git a/cmake/macros.cmake b/cmake/macros.cmake new file mode 100644 index 00000000..a64acd56 --- /dev/null +++ b/cmake/macros.cmake @@ -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 () diff --git a/deps/CMakeLists.txt b/deps/CMakeLists.txt index d7bded02..59aa1865 100644 --- a/deps/CMakeLists.txt +++ b/deps/CMakeLists.txt @@ -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() @@ -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 diff --git a/deps/MarchingCubeCpp/CMakeLists.txt b/deps/MarchingCubeCpp/CMakeLists.txt index b2610f5b..0bf40a1d 100644 --- a/deps/MarchingCubeCpp/CMakeLists.txt +++ b/deps/MarchingCubeCpp/CMakeLists.txt @@ -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 + $ + $) + +install(TARGETS MarchingCube EXPORT ${EXPORTED_TARGETS_NAME} DESTINATION "${INSTALL_LIB_DIR}") +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include DESTINATION "${INSTALL_INCLUDE_DIR}") diff --git a/deps/glad/src/CMakeLists.txt b/deps/glad/src/CMakeLists.txt index a7a08b3e..e46ab7ba 100644 --- a/deps/glad/src/CMakeLists.txt +++ b/deps/glad/src/CMakeLists.txt @@ -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 + $ + $) 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}") diff --git a/deps/imgui/CMakeLists.txt b/deps/imgui/CMakeLists.txt index ec877e5f..451d55e8 100644 --- a/deps/imgui/CMakeLists.txt +++ b/deps/imgui/CMakeLists.txt @@ -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 + $ + $) target_include_directories(imgui PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../glfw/include/") target_include_directories(imgui PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../glad/include/") @@ -74,3 +76,6 @@ 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}") diff --git a/deps/json/CMakeLists.txt b/deps/json/CMakeLists.txt index 613708fe..11c8f0d5 100644 --- a/deps/json/CMakeLists.txt +++ b/deps/json/CMakeLists.txt @@ -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 + $ + $) 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}") diff --git a/deps/stb/CMakeLists.txt b/deps/stb/CMakeLists.txt index 3d8ba438..10276a07 100644 --- a/deps/stb/CMakeLists.txt +++ b/deps/stb/CMakeLists.txt @@ -4,6 +4,10 @@ add_library( stb_impl.cpp ) -target_include_directories(stb PUBLIC ".") +target_include_directories(stb PUBLIC + $ + $) set_target_properties(stb PROPERTIES POSITION_INDEPENDENT_CODE TRUE) + +install(TARGETS stb EXPORT ${EXPORTED_TARGETS_NAME} DESTINATION "${INSTALL_LIB_DIR}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9d03d967..d283d0ab 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 @@ -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 + $ + $) # 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() From 4de6a2a738daa5669a8bfdb2d9c8c6f2ca426e8c Mon Sep 17 00:00:00 2001 From: Steve Walker <65963536+stevalkr@users.noreply.github.com> Date: Fri, 13 Sep 2024 17:16:00 +0800 Subject: [PATCH 2/3] Update CMakeLists.txt --- src/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d283d0ab..488f71e9 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -371,7 +371,7 @@ add_definitions(-DNOMINMAX) # Include settings target_include_directories(polyscope PUBLIC $ - $) + $) # Link settings target_link_libraries(polyscope PUBLIC imgui glm::glm) From b3d54fe2171e9fe763a5947b305cf14a470fd4ec Mon Sep 17 00:00:00 2001 From: Steve Walker <65963536+etherswangel@users.noreply.github.com> Date: Fri, 13 Sep 2024 17:49:21 +0800 Subject: [PATCH 3/3] fix: INSTALL_INTERFACE --- deps/glad/src/CMakeLists.txt | 2 +- deps/imgui/CMakeLists.txt | 6 ++++-- deps/stb/CMakeLists.txt | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/deps/glad/src/CMakeLists.txt b/deps/glad/src/CMakeLists.txt index e46ab7ba..9e728229 100644 --- a/deps/glad/src/CMakeLists.txt +++ b/deps/glad/src/CMakeLists.txt @@ -15,7 +15,7 @@ endif() target_include_directories(glad PUBLIC $ - $) + $) set_target_properties(glad PROPERTIES POSITION_INDEPENDENT_CODE TRUE) diff --git a/deps/imgui/CMakeLists.txt b/deps/imgui/CMakeLists.txt index 451d55e8..253cef91 100644 --- a/deps/imgui/CMakeLists.txt +++ b/deps/imgui/CMakeLists.txt @@ -28,7 +28,7 @@ if("${POLYSCOPE_BACKEND_OPENGL3_GLFW}") target_include_directories(imgui PUBLIC $ - $) + $) target_include_directories(imgui PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../glfw/include/") target_include_directories(imgui PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../glad/include/") @@ -68,7 +68,9 @@ elseif("${POLYSCOPE_BACKEND_OPENGL_MOCK}") ${SRCS} ) - target_include_directories(imgui PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/imgui/") + target_include_directories(imgui PUBLIC + $ + $) endif() diff --git a/deps/stb/CMakeLists.txt b/deps/stb/CMakeLists.txt index 10276a07..695ba2f5 100644 --- a/deps/stb/CMakeLists.txt +++ b/deps/stb/CMakeLists.txt @@ -6,7 +6,7 @@ add_library( target_include_directories(stb PUBLIC $ - $) + $) set_target_properties(stb PROPERTIES POSITION_INDEPENDENT_CODE TRUE)