Skip to content

Commit 0ae886c

Browse files
PacificVikingdavidlionkirkrodrigues
authored
build(cmake)!: Add support for installation and usage as a library in other CMake projects; Add example project to test this use case. (#65)
Co-authored-by: davidlion <[email protected]> Co-authored-by: kirkrodrigues <[email protected]>
1 parent d80cf86 commit 0ae886c

20 files changed

+676
-233
lines changed

.gersemirc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# yaml-language-server: $schema=https://raw.githubusercontent.com/BlankSpruce/gersemi/master/gersemi/configuration.schema.json
33

44
definitions:
5-
- "CMake/ystdlib-cpp-helpers.cmake"
5+
- "cmake/ystdlib-helpers.cmake"
66
- "build/deps/Catch2/Catch2-src/extras/Catch.cmake"
77
line_length: 100
88
list_expansion: "favour-expansion"

CMake/ystdlib-cpp-helpers.cmake

Lines changed: 0 additions & 162 deletions
This file was deleted.

CMakeLists.txt

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,76 @@
1-
cmake_minimum_required(VERSION 3.22.1)
1+
cmake_minimum_required(VERSION 3.23)
22

3-
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
4-
include(ystdlib-cpp-helpers)
3+
project(ystdlib VERSION "0.1.0" LANGUAGES CXX)
54

6-
project(YSTDLIB_CPP LANGUAGES CXX)
7-
8-
set(YSTDLIB_CPP_VERSION "0.0.1" CACHE STRING "Project version.")
5+
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
6+
include(CMakePackageConfigHelpers)
7+
include(GNUInstallDirs)
8+
include(ystdlib-helpers)
99

1010
option(BUILD_SHARED_LIBS "Build using shared libraries." OFF)
11-
option(YSTDLIB_CPP_BUILD_TESTING "Build the testing tree for ystdlib-cpp." ON)
11+
option(ystdlib_BUILD_TESTING "Build the testing tree for ystdlib." ON)
1212

13-
# Require compiler versions that support the C++20 features necessary for compiling ystdlib-cpp
13+
# Require compiler versions that support the C++20 features necessary for compiling ystdlib
1414
if("AppleClang" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
15-
set(YSTDLIB_CPP_CMAKE_CXX_COMPILER_MIN_VERSION "16")
15+
set(ystdlib_CMAKE_CXX_COMPILER_MIN_VERSION "16")
1616
elseif("Clang" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
17-
set(YSTDLIB_CPP_CMAKE_CXX_COMPILER_MIN_VERSION "16")
17+
set(ystdlib_CMAKE_CXX_COMPILER_MIN_VERSION "16")
1818
elseif("GNU" STREQUAL "${CMAKE_CXX_COMPILER_ID}")
19-
set(YSTDLIB_CPP_CMAKE_CXX_COMPILER_MIN_VERSION "11")
19+
set(ystdlib_CMAKE_CXX_COMPILER_MIN_VERSION "11")
2020
else()
2121
message(
2222
FATAL_ERROR
2323
"Unsupported compiler: ${CMAKE_CXX_COMPILER_ID}. Please use AppleClang, Clang, or GNU."
2424
)
2525
endif()
26-
if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "${YSTDLIB_CPP_CMAKE_CXX_COMPILER_MIN_VERSION}")
26+
if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "${ystdlib_CMAKE_CXX_COMPILER_MIN_VERSION}")
2727
message(
2828
FATAL_ERROR
2929
"${CMAKE_CXX_COMPILER_ID} version ${CMAKE_CXX_COMPILER_VERSION} is too low. Must be at \
30-
least ${YSTDLIB_CPP_CMAKE_CXX_COMPILER_MIN_VERSION}."
30+
least ${ystdlib_CMAKE_CXX_COMPILER_MIN_VERSION}."
3131
)
3232
endif()
3333

34-
# Enable exporting compile commands
3534
set(CMAKE_EXPORT_COMPILE_COMMANDS
3635
ON
3736
CACHE BOOL
3837
"Enable/Disable output of compile commands during generation."
3938
FORCE
4039
)
4140

42-
if(YSTDLIB_CPP_IS_TOP_LEVEL)
41+
set(ystdlib_LIBRARIES
42+
"containers"
43+
"error_handling"
44+
"io_interface"
45+
"wrapped_facade_headers"
46+
CACHE STRING
47+
"Semicolon-separated list of libraries to be built."
48+
)
49+
50+
message(STATUS "Building the following libraries:")
51+
foreach(LIB IN LISTS ystdlib_LIBRARIES)
52+
message(STATUS " - ${LIB}")
53+
endforeach()
54+
55+
if(ystdlib_IS_TOP_LEVEL)
4356
# Include dependency settings if the project isn't being included as a subproject.
4457
# NOTE: We mark the file optional because if the user happens to have the dependencies
4558
# installed, this file is not necessary.
46-
include("build/deps/cmake-settings/settings.cmake" OPTIONAL)
59+
include("build/deps/cmake-settings/all.cmake" OPTIONAL)
4760

4861
# If previously undefined, `BUILD_TESTING` will be set to ON.
4962
include(CTest)
5063
endif()
5164

52-
if(BUILD_TESTING AND YSTDLIB_CPP_BUILD_TESTING)
53-
set(YSTDLIB_CPP_ENABLE_TESTS ON)
65+
if(BUILD_TESTING AND ystdlib_BUILD_TESTING)
66+
set(ystdlib_ENABLE_TESTS ON)
5467
endif()
5568

56-
find_package(Boost REQUIRED)
57-
if(Boost_FOUND)
58-
message(STATUS "Found Boost ${Boost_VERSION}.")
59-
endif()
60-
61-
if(YSTDLIB_CPP_ENABLE_TESTS)
69+
if(ystdlib_ENABLE_TESTS)
6270
find_package(Catch2 3.8.0 REQUIRED)
63-
if(Catch2_FOUND)
64-
message(STATUS "Found Catch2 ${Catch2_VERSION}.")
65-
else()
66-
message(FATAL_ERROR "Could not find libraries for Catch2.")
67-
endif()
71+
message(STATUS "Found Catch2 ${Catch2_VERSION}.")
6872
include(Catch)
6973

70-
# Set up the unified unit test target
7174
set(UNIFIED_UNIT_TEST_TARGET "unit-test-all")
7275
add_executable(${UNIFIED_UNIT_TEST_TARGET})
7376
target_link_libraries(${UNIFIED_UNIT_TEST_TARGET} PRIVATE Catch2::Catch2WithMain)
@@ -82,4 +85,32 @@ if(YSTDLIB_CPP_ENABLE_TESTS)
8285
catch_discover_tests(${UNIFIED_UNIT_TEST_TARGET} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/testbin)
8386
endif()
8487

88+
# We require all libraries use the same minimum version of dependencies to avoid issues.
89+
set(BOOST_MIN_VERSION "1.81.0")
90+
91+
set(CONFIG_PATH_SUFFIX "cmake/ystdlib")
92+
set(CONFIG_LIBS_PATH_SUFFIX "${CONFIG_PATH_SUFFIX}/libs")
93+
94+
# Used by libraries and must come before add_subdirectory.
95+
set(CONFIG_LIBS_DEST_DIR "${CMAKE_INSTALL_LIBDIR}/${CONFIG_LIBS_PATH_SUFFIX}")
96+
set(CONFIG_LIBS_INPUT_DIR "${PROJECT_SOURCE_DIR}/${CONFIG_LIBS_PATH_SUFFIX}")
97+
8598
add_subdirectory(src/ystdlib)
99+
100+
set(CONFIG_FILE_PREFIX "ystdlib-config")
101+
set(CONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/${CONFIG_PATH_SUFFIX}")
102+
set(CONFIG_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_FILE_PREFIX}.cmake")
103+
set(CONFIG_VERSION_OUTPUT_PATH "${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_FILE_PREFIX}-version.cmake")
104+
105+
configure_package_config_file(
106+
"${CMAKE_CURRENT_LIST_DIR}/${CONFIG_PATH_SUFFIX}/${CONFIG_FILE_PREFIX}.cmake.in"
107+
"${CONFIG_OUTPUT_PATH}"
108+
INSTALL_DESTINATION "${CONFIG_INSTALL_DIR}"
109+
)
110+
write_basic_package_version_file("${CONFIG_VERSION_OUTPUT_PATH}" COMPATIBILITY SameMajorVersion)
111+
install(
112+
FILES
113+
"${CONFIG_OUTPUT_PATH}"
114+
"${CONFIG_VERSION_OUTPUT_PATH}"
115+
DESTINATION "${CONFIG_INSTALL_DIR}"
116+
)

0 commit comments

Comments
 (0)