From e10ba181f1883ea46321afd6836725aa05b8517b Mon Sep 17 00:00:00 2001 From: Anes Belfodil Date: Sun, 21 Apr 2019 18:26:21 -0400 Subject: [PATCH] CMake enhancements * Add magic_enum as an interface library * Enable tests and examples only if is top project * Use more modern approach to include directories * Add install target --- CMakeLists.txt | 25 +++++++++++++++++++------ example/CMakeLists.txt | 3 +-- install.cmake | 25 +++++++++++++++++++++++++ test/CMakeLists.txt | 5 ++--- 4 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 install.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e244a4..220ff3a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,14 +2,27 @@ project(magic_enum VERSION "0.4.2" LANGUAGES CXX) -option(MAGIC_ENUM_OPT_BUILD_EXAMPLES "Build magic_enum examples" ON) -option(MAGIC_ENUM_OPT_BUILD_TESTS "Build and perform magic_enum tests" ON) +add_library(${PROJECT_NAME} INTERFACE) +target_include_directories(${PROJECT_NAME} INTERFACE + $ + $) -if(MAGIC_ENUM_OPT_BUILD_EXAMPLES) +if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + set(IS_TOPLEVEL_PROJECT TRUE) +else () + set(IS_TOPLEVEL_PROJECT FALSE) +endif () + +option(MAGIC_ENUM_OPT_BUILD_EXAMPLES "Build magic_enum examples" ${IS_TOPLEVEL_PROJECT}) +option(MAGIC_ENUM_OPT_BUILD_TESTS "Build and perform magic_enum tests" ${IS_TOPLEVEL_PROJECT}) + +if (MAGIC_ENUM_OPT_BUILD_EXAMPLES) add_subdirectory(example) -endif() +endif () -if(MAGIC_ENUM_OPT_BUILD_TESTS) +if (MAGIC_ENUM_OPT_BUILD_TESTS) enable_testing() add_subdirectory(test) -endif() +endif () + +include(install.cmake) diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 6cd8b54..56f03df 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,7 +1,5 @@ include(CheckCXXCompilerFlag) -include_directories(${CMAKE_SOURCE_DIR}/include) - set(OPTIONS "") if((CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR (CMAKE_CXX_COMPILER_ID MATCHES "Clang")) @@ -21,3 +19,4 @@ add_executable(example example.cpp ${CMAKE_SOURCE_DIR}/include/magic_enum.hpp) target_compile_options(example PRIVATE ${OPTIONS}) +target_link_libraries(example PRIVATE magic_enum) diff --git a/install.cmake b/install.cmake new file mode 100644 index 0000000..8b614e4 --- /dev/null +++ b/install.cmake @@ -0,0 +1,25 @@ +include(CMakePackageConfigHelpers) +write_basic_package_version_file( + ${PROJECT_NAME}ConfigVersion.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion +) + +install(TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME}Targets + PUBLIC_HEADER DESTINATION include + INCLUDES DESTINATION include + ) + +install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake + DESTINATION lib/cmake/${PROJECT_NAME} + ) + +install(EXPORT ${PROJECT_NAME}Targets + NAMESPACE ${PROJECT_NAME}:: + DESTINATION lib/cmake/${PROJECT_NAME} + ) + +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include + DESTINATION include + ) \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 51840d9..6f0686f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,8 +1,5 @@ include(CheckCXXCompilerFlag) -include_directories(3rdparty/Catch2) -include_directories(${CMAKE_SOURCE_DIR}/include) - set(SOURCES test.cpp) set(OPTIONS "") @@ -28,6 +25,8 @@ endif() function(make_target target std) add_executable(${target} ${SOURCES}) target_compile_options(${target} PRIVATE ${OPTIONS}) + target_include_directories(${target} PRIVATE 3rdparty/Catch2) + target_link_libraries(${target} PRIVATE magic_enum) if(std) if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") target_compile_options(${target} PRIVATE /std:${std})