Skip to content

Commit

Permalink
add config template
Browse files Browse the repository at this point in the history
  • Loading branch information
phlptp committed Aug 13, 2021
1 parent e50ce8d commit a841903
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
29 changes: 13 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,15 @@ set(UNITS_NAMESPACE
CACHE STRING "Top-level namespace name. Default is `units`."
)

option(UNITS_INSTALL "Enable installation of the units library" ON)
mark_as_advanced(UNITS_INSTALL)

cmake_dependent_option(
UNITS_WITH_CMAKE_PACKAGE
UNITS_INSTALL
"Generate and install cmake package files"
ON
"CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME;UNITS_INSTALL;NOT UNITS_BINARY_ONLY_INSTALL"
"CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME;NOT UNITS_BINARY_ONLY_INSTALL"
OFF
)

mark_as_advanced(UNITS_WITH_CMAKE_PACKAGE)
mark_as_advanced(UNITS_INSTALL)

cmake_dependent_option(
UNITS_BUILD_FUZZ_TARGETS "Build the targets for a fuzzing system" OFF
Expand All @@ -113,12 +110,9 @@ set(UNITS_CLANG_TIDY_OPTIONS
mark_as_advanced(UNITS_CLANG_TIDY_OPTIONS)
mark_as_advanced(UNITS_CLANG_TIDY)

# Install instructions for this target
if(UNITS_WITH_CMAKE_PACKAGE)
set(UNITS_LIBRARY_EXPORT_COMMAND EXPORT unitsConfig)
else(UNITS_WITH_CMAKE_PACKAGE)
set(UNITS_LIBRARY_EXPORT_COMMAND)
endif(UNITS_WITH_CMAKE_PACKAGE)

set(UNITS_LIBRARY_EXPORT_COMMAND EXPORT unitsTargets)
mark_as_advanced(UNITS_LIBRARY_EXPORT_COMMAND)

option(UNITS_HEADER_ONLY "Expose the units library as header-only" OFF)

Expand Down Expand Up @@ -193,11 +187,14 @@ if(UNITS_INSTALL)
if(UNITS_BUILD_STATIC_LIBRARY)
install(TARGETS compile_flags_target ${UNITS_LIBRARY_EXPORT_COMMAND})
endif()
if(UNITS_WITH_CMAKE_PACKAGE AND NOT UNITS_BINARY_ONLY_INSTALL)
install(EXPORT unitsTargets DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
export(EXPORT unitsTargets NAMESPACE units::)
if(NOT UNITS_BINARY_ONLY_INSTALL)
configure_file(config/unitsConfig.cmake.in
"${PROJECT_BINARY_DIR}/unitsConfig.cmake" @ONLY)

install(${UNITS_LIBRARY_EXPORT_COMMAND} DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
export(${UNITS_LIBRARY_EXPORT_COMMAND} NAMESPACE units::)
write_basic_package_version_file(${PROJECT_BINARY_DIR}/unitsConfigVersion.cmake COMPATIBILITY AnyNewerVersion)
install(FILES ${PROJECT_BINARY_DIR}/unitsConfigVersion.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/units)
install(FILES ${PROJECT_BINARY_DIR}/unitsConfigVersion.cmake ${PROJECT_BINARY_DIR}/unitsConfig.cmake DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/units)
endif()
endif()

Expand Down
4 changes: 2 additions & 2 deletions config/unitConfig.cmake.in → config/unitsConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ endif()

# These are IMPORTED targets created by FooBarTargets.cmake
set(UNITS_LIBRARIES units::units)
set(UNITS_WEBSERVER units::webserver)
set(UNITS_CONVERT units::convert
set(UNITS_WEBSERVER units::units_webserver)
set(UNITS_CONVERT units::units_convert
16 changes: 8 additions & 8 deletions converter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ cmake_dependent_option(
)

if(UNITS_BUILD_CONVERTER_APP)
add_executable(unit_convert converter.cpp)
target_link_libraries(unit_convert PUBLIC units::units compile_flags_target)
target_include_directories(unit_convert PRIVATE ${PROJECT_SOURCE_DIR}/ThirdParty)
add_executable(units_convert converter.cpp)
target_link_libraries(units_convert PUBLIC units::units compile_flags_target)
target_include_directories(units_convert PRIVATE ${PROJECT_SOURCE_DIR}/ThirdParty)
target_compile_definitions(
unit_convert PUBLIC UNITS_VERSION_STRING="${UNITS_VERSION}"
units_convert PUBLIC UNITS_VERSION_STRING="${UNITS_VERSION}"
)
if(CMAKE_CXX_STANDARD GREATER 16)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT CMAKE_CXX_COMPILER_VERSION
VERSION_EQUAL 8
)
target_link_libraries(unit_convert PRIVATE -lstdc++fs)
target_link_libraries(unit_sconvert PRIVATE -lstdc++fs)
endif()
endif()

if (UNITS_INSTALL)
install(TARGETS unit_convert
EXPORT unitsTargets
install(TARGETS units_convert
${UNITS_LIBRARY_EXPORT_COMMAND}
RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT units_convert)
endif()

add_executable(units::units_convert ALIAS units_convert)
endif()
4 changes: 2 additions & 2 deletions units/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ elseif(UNITS_HEADER_ONLY)
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_definitions(header_only INTERFACE UNITS_HEADER_ONLY)
if(UNITS_INSTALL)
if(UNITS_INSTALL AND NOT UNITS_BINARY_ONLY_INSTALL)
install(TARGETS header_only ${UNITS_LIBRARY_EXPORT_COMMAND})
endif()
if(UNITS_NAMESPACE)
Expand Down Expand Up @@ -93,7 +93,7 @@ else()
if(UNITS_BASE_TYPE)
target_compile_definitions(units PUBLIC -DUNITS_BASE_TYPE=${UNITS_BASE_TYPE})
endif()
if(UNITS_INSTALL)
if(UNITS_INSTALL AND NOT UNITS_BINARY_ONLY_INSTALL)
install(TARGETS units ${UNITS_LIBRARY_EXPORT_COMMAND}
DESTINATION ${CMAKE_INSTALL_LIBDIR} # INCLUDES DESTINATION include/units
)
Expand Down
2 changes: 1 addition & 1 deletion webserver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ if(CMAKE_VERSION VERSION_GREATER 3.12)

if (UNITS_INSTALL)
install(TARGETS units_webserver
EXPORT unitsTargets
${UNITS_LIBRARY_EXPORT_COMMAND}
RUNTIME DESTINATION "${INSTALL_BIN_DIR}" COMPONENT units_webserver)
endif()
add_executable(units::units_webserver ALIAS units_webserver)
Expand Down

0 comments on commit a841903

Please sign in to comment.