Optimize cmake targets

This commit is contained in:
xiaozhuai 2023-12-22 20:01:49 +08:00 committed by Christophe
parent e4dd44d584
commit 46b796dd79
2 changed files with 75 additions and 59 deletions

View File

@ -16,30 +16,58 @@ set(GLM_VERSION ${GLM_VERSION_MAJOR}.${GLM_VERSION_MINOR}.${GLM_VERSION_PATCH}.$
project(glm VERSION ${GLM_VERSION} LANGUAGES CXX) project(glm VERSION ${GLM_VERSION} LANGUAGES CXX)
message(STATUS "GLM: Version " ${GLM_VERSION}) message(STATUS "GLM: Version " ${GLM_VERSION})
add_subdirectory(glm) set(GLM_IS_MASTER_PROJECT OFF)
add_library(glm::glm ALIAS glm) if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
set(GLM_IS_MASTER_PROJECT ON)
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR}) endif()
include(CPack) option(GLM_BUILD_LIBRARY "Build dynamic/static library" ON)
install(DIRECTORY glm DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} PATTERN "CMakeLists.txt" EXCLUDE) option(GLM_BUILD_TESTS "Build the test programs" ${GLM_IS_MASTER_PROJECT})
install(EXPORT glm FILE glmConfig.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/glm NAMESPACE glm::) option(GLM_BUILD_INSTALL "Generate the install target" ${GLM_IS_MASTER_PROJECT})
include(CMakePackageConfigHelpers)
write_basic_package_version_file("glmConfigVersion.cmake" COMPATIBILITY AnyNewerVersion) include(GNUInstallDirs)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/glmConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/glm)
add_subdirectory(glm)
include(CTest)
if(BUILD_TESTING) if (GLM_BUILD_TESTS)
add_subdirectory(test) include(CTest)
endif() add_subdirectory(test)
endif()
endif(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
if (GLM_BUILD_INSTALL)
if (NOT TARGET uninstall) include(CPack)
configure_file(cmake/cmake_uninstall.cmake.in
cmake_uninstall.cmake IMMEDIATE @ONLY) install(TARGETS glm-header-only glm EXPORT glm)
install(
add_custom_target(uninstall DIRECTORY glm
"${CMAKE_COMMAND}" -P DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
"${CMAKE_BINARY_DIR}/cmake_uninstall.cmake") PATTERN "CMakeLists.txt" EXCLUDE
)
install(
EXPORT glm
NAMESPACE glm::
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/glm"
FILE glmConfig.cmake
)
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/glmConfigVersion.cmake"
COMPATIBILITY AnyNewerVersion
)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/glmConfigVersion.cmake"
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/glm"
)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
IMMEDIATE @ONLY
)
add_custom_target(
uninstall
"${CMAKE_COMMAND}" -P
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
)
endif() endif()

View File

@ -1,6 +1,3 @@
option(BUILD_SHARED_LIBS "Build shared library" ON)
option(BUILD_STATIC_LIBS "Build static library" ON)
file(GLOB ROOT_SOURCE *.cpp) file(GLOB ROOT_SOURCE *.cpp)
file(GLOB ROOT_INLINE *.inl) file(GLOB ROOT_INLINE *.inl)
file(GLOB ROOT_HEADER *.hpp) file(GLOB ROOT_HEADER *.hpp)
@ -45,37 +42,28 @@ source_group("SIMD Files" FILES ${SIMD_SOURCE})
source_group("SIMD Files" FILES ${SIMD_INLINE}) source_group("SIMD Files" FILES ${SIMD_INLINE})
source_group("SIMD Files" FILES ${SIMD_HEADER}) source_group("SIMD Files" FILES ${SIMD_HEADER})
add_library(glm INTERFACE) add_library(glm-header-only INTERFACE)
add_library(glm::glm-header-only ALIAS glm-header-only)
include(GNUInstallDirs) target_include_directories(glm-header-only INTERFACE
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>"
target_include_directories(glm INTERFACE "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
) )
install(TARGETS glm EXPORT glm) if (GLM_BUILD_LIBRARY)
add_library(glm
if(BUILD_STATIC_LIBS) ${ROOT_TEXT} ${ROOT_MD} ${ROOT_NAT}
add_library(glm_static STATIC ${ROOT_TEXT} ${ROOT_MD} ${ROOT_NAT}
${ROOT_SOURCE} ${ROOT_INLINE} ${ROOT_HEADER} ${ROOT_SOURCE} ${ROOT_INLINE} ${ROOT_HEADER}
${CORE_SOURCE} ${CORE_INLINE} ${CORE_HEADER} ${CORE_SOURCE} ${CORE_INLINE} ${CORE_HEADER}
${EXT_SOURCE} ${EXT_INLINE} ${EXT_HEADER} ${EXT_SOURCE} ${EXT_INLINE} ${EXT_HEADER}
${GTC_SOURCE} ${GTC_INLINE} ${GTC_HEADER} ${GTC_SOURCE} ${GTC_INLINE} ${GTC_HEADER}
${GTX_SOURCE} ${GTX_INLINE} ${GTX_HEADER} ${GTX_SOURCE} ${GTX_INLINE} ${GTX_HEADER}
${SIMD_SOURCE} ${SIMD_INLINE} ${SIMD_HEADER}) ${SIMD_SOURCE} ${SIMD_INLINE} ${SIMD_HEADER}
target_link_libraries(glm_static PUBLIC glm) )
add_library(glm::glm_static ALIAS glm_static) add_library(glm::glm ALIAS glm)
endif() target_link_libraries(glm PUBLIC glm-header-only)
else()
if(BUILD_SHARED_LIBS) add_library(glm INTERFACE)
add_library(glm_shared SHARED ${ROOT_TEXT} ${ROOT_MD} ${ROOT_NAT} add_library(glm::glm ALIAS glm)
${ROOT_SOURCE} ${ROOT_INLINE} ${ROOT_HEADER} target_link_libraries(glm INTERFACE glm-header-only)
${CORE_SOURCE} ${CORE_INLINE} ${CORE_HEADER}
${EXT_SOURCE} ${EXT_INLINE} ${EXT_HEADER}
${GTC_SOURCE} ${GTC_INLINE} ${GTC_HEADER}
${GTX_SOURCE} ${GTX_INLINE} ${GTX_HEADER}
${SIMD_SOURCE} ${SIMD_INLINE} ${SIMD_HEADER})
target_link_libraries(glm_shared PUBLIC glm)
add_library(glm::glm_shared ALIAS glm_shared)
endif() endif()