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:
f4a20ec39e/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.
This commit is contained in:
Violin Yanev 2020-11-01 17:10:36 +01:00 committed by The Phantom Derpstorm
parent 4adc453419
commit ca3d30d8e9
9 changed files with 51 additions and 29 deletions

View File

@ -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")
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}")
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)

View File

@ -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()

View File

@ -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)

View File

@ -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()

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)