From ca3d30d8e9899a4023ebbfeb0b0e0c9f5eaaf007 Mon Sep 17 00:00:00 2001 From: Violin Yanev Date: Sun, 1 Nov 2020 17:10:36 +0100 Subject: [PATCH] Add CMake option to disable installation Rationale: since sol2 is a header-only library, some projects might use it as a private dependency instead of exposing it through their API. Thus, it is useful to have an option to not install sol2, in order to reduce the installation size as well as to avoid accidentally exposing internal dependencies in the top-level project. The project I work on is a demonstration of how the feature might be used: https://github.com/GENIVI/ramses-logic/blob/f4a20ec39ea2a29e9d26a82564d15c4d6e2405a7/external/CMakeLists.txt#L84 (mind the different name of the CMake variable, I renamed it to fit the rest of the sol options convention) We have an install-check script script which verifies that only expected headers, libs, targets and docs are exported (the one which are interesting from users PoV). I wouldn't mind contributing the script as github workflows if you want to use it as well - please provide feedback if that's desired. Here is a link to our script for preview: https://github.com/GENIVI/ramses-logic/blob/master/ci/scripts/installation-check/check-installation.py While modifying the CMake code, I noticed a 4-space instead of TAB in one place and fixed it to be consistent with the rest of the CMake code. --- CMakeLists.txt | 43 +++++++++++-------- docs/CMakeLists.txt | 7 ++- examples/CMakeLists.txt | 4 +- single/CMakeLists.txt | 6 ++- tests/compile_tests/CMakeLists.txt | 4 +- .../function_pointers/CMakeLists.txt | 4 +- tests/regression_tests/1011/CMakeLists.txt | 4 +- tests/regression_tests/simple/CMakeLists.txt | 4 +- tests/runtime_tests/CMakeLists.txt | 4 +- 9 files changed, 51 insertions(+), 29 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dd0a81bf..21f3fb6c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,6 +53,7 @@ option(SOL2_DYNAMIC_LOADING_EXAMPLES "Enable build of interop examples" OFF) option(SOL2_GENERATE_SINGLE "Enable generation and build of single header files" OFF) option(SOL2_SINGLE "Enable use of prepackaged single header files" OFF) option(SOL2_DOCS "Enable build of documentation" OFF) +option(SOL2_ENABLE_INSTALL "Enable installation of Sol2" ON) # Single tests and examples tests will only be turned on if both SINGLE and TESTS are defined CMAKE_DEPENDENT_OPTION(SOL2_TESTS_SINGLE "Enable build of tests using the premade single headers" ON "SOL2_SINGLE;SOL2_TESTS" OFF) @@ -132,20 +133,22 @@ write_basic_package_version_file( export(TARGETS sol2 FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/sol2-targets.cmake") -install(TARGETS sol2 - EXPORT sol2) +if(SOL2_ENABLE_INSTALL) + install(TARGETS sol2 + EXPORT sol2) -install(EXPORT sol2 - FILE sol2-targets.cmake - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/sol2") + install(EXPORT sol2 + FILE sol2-targets.cmake + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/sol2") -install(DIRECTORY include/sol - DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") + install(DIRECTORY include/sol + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}") -install(FILES - "${CMAKE_CURRENT_BINARY_DIR}/cmake/sol2-config.cmake" - "${CMAKE_CURRENT_BINARY_DIR}/cmake/sol2-config-version.cmake" - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/sol2") + install(FILES + "${CMAKE_CURRENT_BINARY_DIR}/cmake/sol2-config.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/cmake/sol2-config-version.cmake" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/sol2") +endif() # # # Single header target # Find Python3 for single header / forward header generation @@ -161,14 +164,16 @@ if (SOL2_DOCS) add_subdirectory(docs) endif() -# pkg-config support, except on Windows -if(NOT WIN32 OR NOT CMAKE_HOST_SYSTEM_NAME MATCHES Windows) - set(PKGCONFIG_INSTALL_DIR - "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig" - CACHE PATH "Path where sol2.pc is installed") - - configure_file("${CMAKE_CURRENT_SOURCE_DIR}/sol2.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/sol2.pc" @ONLY) - install(FILES "${CMAKE_CURRENT_BINARY_DIR}/sol2.pc" DESTINATION "${PKGCONFIG_INSTALL_DIR}") +if(SOL2_ENABLE_INSTALL) + # pkg-config support, except on Windows + if(NOT WIN32 OR NOT CMAKE_HOST_SYSTEM_NAME MATCHES Windows) + set(PKGCONFIG_INSTALL_DIR + "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig" + CACHE PATH "Path where sol2.pc is installed") + + configure_file("${CMAKE_CURRENT_SOURCE_DIR}/sol2.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/sol2.pc" @ONLY) + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/sol2.pc" DESTINATION "${PKGCONFIG_INSTALL_DIR}") + endif() endif() if (SOL2_CI) diff --git a/docs/CMakeLists.txt b/docs/CMakeLists.txt index d21a2dba..2ebb85cf 100644 --- a/docs/CMakeLists.txt +++ b/docs/CMakeLists.txt @@ -32,7 +32,7 @@ endif() find_program(sol2_make_executable make make.exe mingw32-make mingw32-make.exe) if(NOT sol2_make_executable) - message(FATAL_ERROR "could not find a suitable make executable to build Sphinx documentation") + message(FATAL_ERROR "could not find a suitable make executable to build Sphinx documentation") endif() add_custom_command(OUTPUT docs_invisible_file_always_generate @@ -40,4 +40,7 @@ add_custom_command(OUTPUT docs_invisible_file_always_generate COMMAND "${sol2_make_executable}" -C "${CMAKE_CURRENT_SOURCE_DIR}" html "BUILDDIR=${CMAKE_CURRENT_BINARY_DIR}") add_custom_target(docs DEPENDS docs_invisible_file_always_generate) -install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/docs/html" DESTINATION "${CMAKE_INSTALL_DOCDIR}") + +if(SOL2_ENABLE_INSTALL) + install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/docs/html" DESTINATION "${CMAKE_INSTALL_DOCDIR}") +endif() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 400b67c2..4036361a 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -109,7 +109,9 @@ function (MAKE_EXAMPLE example_source_file example_suffix target_sol) if (SOL2_TESTS_EXAMPLES) add_test(NAME ${example_output_name} COMMAND ${example_name}) endif() - install(TARGETS ${example_name} RUNTIME DESTINATION bin) + if(SOL2_ENABLE_INSTALL) + install(TARGETS ${example_name} RUNTIME DESTINATION bin) + endif() endfunction(MAKE_EXAMPLE) if (SOL2_EXAMPLES) diff --git a/single/CMakeLists.txt b/single/CMakeLists.txt index c5bd0bfb..8cce15e5 100644 --- a/single/CMakeLists.txt +++ b/single/CMakeLists.txt @@ -70,6 +70,8 @@ if (SOL2_GENERATE_SINGLE) EXPORT_NAME sol2::sol2_single_generated INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}/include") add_dependencies(sol2_single_generated sol2_single_header_generator) - install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/sol/sol.hpp" "${CMAKE_CURRENT_BINARY_DIR}/include/sol/forward.hpp" - DESTINATION include/sol/single/sol) + if(SOL2_ENABLE_INSTALL) + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/include/sol/sol.hpp" "${CMAKE_CURRENT_BINARY_DIR}/include/sol/forward.hpp" + DESTINATION include/sol/single/sol) + endif() endif() diff --git a/tests/compile_tests/CMakeLists.txt b/tests/compile_tests/CMakeLists.txt index 68830470..01cfb136 100644 --- a/tests/compile_tests/CMakeLists.txt +++ b/tests/compile_tests/CMakeLists.txt @@ -87,7 +87,9 @@ function(CREATE_TEST test_target_name test_name target_sol) endif() add_test(NAME ${test_name} COMMAND ${test_target_name}) - install(TARGETS ${test_target_name} RUNTIME DESTINATION bin) + if(SOL2_ENABLE_INSTALL) + install(TARGETS ${test_target_name} RUNTIME DESTINATION bin) + endif() endfunction(CREATE_TEST) if (SOL2_TESTS) diff --git a/tests/config_tests/function_pointers/CMakeLists.txt b/tests/config_tests/function_pointers/CMakeLists.txt index ff6186f5..7e0afa17 100644 --- a/tests/config_tests/function_pointers/CMakeLists.txt +++ b/tests/config_tests/function_pointers/CMakeLists.txt @@ -89,7 +89,9 @@ function(CREATE_TEST test_target_name test_name target_sol) endif() add_test(NAME ${test_name} COMMAND ${test_target_name}) - install(TARGETS ${test_target_name} RUNTIME DESTINATION bin) + if(SOL2_ENABLE_INSTALL) + install(TARGETS ${test_target_name} RUNTIME DESTINATION bin) + endif() endfunction(CREATE_TEST) if (SOL2_TESTS) diff --git a/tests/regression_tests/1011/CMakeLists.txt b/tests/regression_tests/1011/CMakeLists.txt index 49f89642..7c43b760 100644 --- a/tests/regression_tests/1011/CMakeLists.txt +++ b/tests/regression_tests/1011/CMakeLists.txt @@ -85,7 +85,9 @@ function(CREATE_TEST test_target_name test_name target_sol) endif() add_test(NAME ${test_name} COMMAND ${test_target_name}) - install(TARGETS ${test_target_name} RUNTIME DESTINATION bin) + if(SOL2_ENABLE_INSTALL) + install(TARGETS ${test_target_name} RUNTIME DESTINATION bin) + endif() endfunction(CREATE_TEST) if (SOL2_TESTS) diff --git a/tests/regression_tests/simple/CMakeLists.txt b/tests/regression_tests/simple/CMakeLists.txt index 9e12b6cc..ca2e39e6 100644 --- a/tests/regression_tests/simple/CMakeLists.txt +++ b/tests/regression_tests/simple/CMakeLists.txt @@ -57,7 +57,9 @@ function(CREATE_TEST test_target_name test_name target_sol) endif() add_test(NAME ${test_name} COMMAND ${test_target_name}) - install(TARGETS ${test_target_name} RUNTIME DESTINATION bin) + if(SOL2_ENABLE_INSTALL) + install(TARGETS ${test_target_name} RUNTIME DESTINATION bin) + endif() endfunction(CREATE_TEST) if (SOL2_TESTS) diff --git a/tests/runtime_tests/CMakeLists.txt b/tests/runtime_tests/CMakeLists.txt index 60666dfa..039adadb 100644 --- a/tests/runtime_tests/CMakeLists.txt +++ b/tests/runtime_tests/CMakeLists.txt @@ -91,7 +91,9 @@ function(CREATE_TEST test_target_name test_name target_sol) endif() add_test(NAME ${test_name} COMMAND ${test_target_name}) - install(TARGETS ${test_target_name} RUNTIME DESTINATION bin) + if(SOL2_ENABLE_INSTALL) + install(TARGETS ${test_target_name} RUNTIME DESTINATION bin) + endif() endfunction(CREATE_TEST) if (SOL2_TESTS)