From 836c86c4353a8c863a64bb457ee6c106c2161198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccolo=CC=80=20Iardella?= Date: Fri, 27 May 2022 15:50:54 +0200 Subject: [PATCH 1/2] Add CMake option for disabling installation --- CMakeLists.txt | 98 ++++++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 46 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6ba19ab..b8736b5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,56 +15,62 @@ add_library(eventpp::eventpp ALIAS eventpp) # Installation # ------------ include(GNUInstallDirs) +if (POLICY CMP0077) + cmake_policy(SET CMP0077 NEW) +endif (POLICY CMP0077) +option(EVENTPP_INSTALL "Enable installation (Projects embedding eventpp may want to turn this OFF)." ON) -# Install the library -install( - TARGETS eventpp - EXPORT eventppTargets - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} -) +if (EVENTPP_INSTALL) + # Install the library + install( + TARGETS eventpp + EXPORT eventppTargets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + ) -# Install the headers -install( - DIRECTORY include/ - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} -) + # Install the headers + install( + DIRECTORY include/ + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) -# (Generate and) install the target import file -install( - EXPORT eventppTargets - NAMESPACE eventpp:: - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/eventpp -) + # (Generate and) install the target import file + install( + EXPORT eventppTargets + NAMESPACE eventpp:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/eventpp + ) -# Generate the package version file -include(CMakePackageConfigHelpers) -write_basic_package_version_file( - ${CMAKE_CURRENT_BINARY_DIR}/eventppConfigVersion.cmake - VERSION ${PROJECT_VERSION} - COMPATIBILITY AnyNewerVersion -) + # Generate the package version file + include(CMakePackageConfigHelpers) + write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/eventppConfigVersion.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion + ) -# Generate the package configuration file, that allows other -# CMake projects to find the library with find_package() -configure_package_config_file( - cmake/eventppConfig.cmake.in - ${CMAKE_CURRENT_BINARY_DIR}/eventppConfig.cmake - INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/eventpp -) + # Generate the package configuration file, that allows other + # CMake projects to find the library with find_package() + configure_package_config_file( + cmake/eventppConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/eventppConfig.cmake + INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/eventpp + ) -# Install the package version and configuration files -install( - FILES - ${CMAKE_CURRENT_BINARY_DIR}/eventppConfig.cmake - ${CMAKE_CURRENT_BINARY_DIR}/eventppConfigVersion.cmake - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/eventpp -) + # Install the package version and configuration files + install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/eventppConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/eventppConfigVersion.cmake + DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/eventpp + ) -# Install readme and license -install( - FILES - readme.md - license - DESTINATION ${CMAKE_INSTALL_DATADIR}/eventpp -) + # Install readme and license + install( + FILES + readme.md + license + DESTINATION ${CMAKE_INSTALL_DATADIR}/eventpp + ) +endif() \ No newline at end of file From a4b2f9104582914ece297d457d722dcdf2d2050b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccolo=CC=80=20Iardella?= Date: Fri, 27 May 2022 16:36:31 +0200 Subject: [PATCH 2/2] Add automatic check for main project --- CMakeLists.txt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b8736b5..e62cdeb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,13 +12,22 @@ target_include_directories( add_library(eventpp::eventpp ALIAS eventpp) +# Checks if eventpp is the main project or if it is +# being built as a subproject (using add_subdirectory/FetchContent). +set(MAIN_PROJECT OFF) +if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) + set(MAIN_PROJECT ON) +endif() + # Installation # ------------ include(GNUInstallDirs) if (POLICY CMP0077) + # Allow CMake 3.13+ to override options when using add_subdirectory/FetchContent. cmake_policy(SET CMP0077 NEW) endif (POLICY CMP0077) -option(EVENTPP_INSTALL "Enable installation (Projects embedding eventpp may want to turn this OFF)." ON) + +option(EVENTPP_INSTALL "Enable installation" ${MAIN_PROJECT}) if (EVENTPP_INSTALL) # Install the library