Skip to content

Commit d91a7b6

Browse files
committed
Prepare use of beman-install-library-config.cmake
1 parent 30a3246 commit d91a7b6

File tree

8 files changed

+505
-91
lines changed

8 files changed

+505
-91
lines changed

CMakeLists.txt

Lines changed: 14 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,30 @@
33
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
# gersemi: on
55

6-
cmake_minimum_required(VERSION 3.25...4.2)
7-
8-
#========================== pre project settings ===============================
9-
# gersemi: off
10-
if(CMAKE_VERSION VERSION_EQUAL 4.2)
11-
set(CMAKE_EXPERIMENTAL_CXX_IMPORT_STD "d0edc3af-4c50-42ea-a356-e2862fe7a444")
12-
13-
if(CMAKE_CXX_STDLIB_MODULES_JSON)
14-
message(
15-
STATUS
16-
"CMAKE_CXX_STDLIB_MODULES_JSON=${CMAKE_CXX_STDLIB_MODULES_JSON}"
17-
)
18-
endif()
19-
endif()
20-
# gersemi: on
21-
#===============================================================================
6+
cmake_minimum_required(VERSION 3.30...4.2)
227

8+
include(cmake/prelude.cmake)
239
#===================================================
24-
project(beman_execution VERSION 0.0.1 LANGUAGES CXX)
10+
project(beman.execution VERSION 0.0.1 LANGUAGES CXX)
2511
#===================================================
2612

27-
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
28-
message(FATAL_ERROR "In-source builds are not allowed!")
29-
endif()
13+
# Modules opt in only on compilers that support it: msvc, g++-15 and clang-20+
14+
include(cxx-modules-rules)
3015

3116
set(TARGET_NAME execution)
3217
set(TARGET_NAMESPACE beman)
3318
set(TARGET_PREFIX ${TARGET_NAMESPACE}.${TARGET_NAME})
34-
set(TARGET_LIBRARY ${PROJECT_NAME})
19+
set(TARGET_LIBRARY ${TARGET_NAME})
3520
set(TARGET_ALIAS ${TARGET_NAMESPACE}::${TARGET_NAME})
3621
set(TARGET_PACKAGE_NAME ${PROJECT_NAME}-config)
3722
set(TARGETS_EXPORT_NAME ${PROJECT_NAME}-targets)
3823

39-
#========================== post project settings ==============================
40-
# Tell CMake that we explicitly want `import std`.
41-
# This will initialize the property on all targets declared after this to 1
42-
message(STATUS "CMAKE_CXX_COMPILER_IMPORT_STD=${CMAKE_CXX_COMPILER_IMPORT_STD}")
43-
if(${CMAKE_CXX_STANDARD} IN_LIST CMAKE_CXX_COMPILER_IMPORT_STD)
44-
set(CMAKE_CXX_MODULE_STD ON)
45-
message(STATUS "CMAKE_CXX_MODULE_STD=${CMAKE_CXX_MODULE_STD}")
46-
endif()
47-
48-
add_library(${TARGET_NAME} STATIC)
24+
add_library(${TARGET_LIBRARY} STATIC)
25+
add_library(${TARGET_ALIAS} ALIAS ${TARGET_LIBRARY})
4926

5027
# CMake requires the language standard to be specified as compile feature
5128
# when a target provides C++23 modules and the target will be installed
52-
target_compile_features(${TARGET_NAME} PUBLIC cxx_std_23)
53-
54-
if(CMAKE_CXX_SCAN_FOR_MODULES AND ${CMAKE_GENERATOR} STREQUAL Ninja)
55-
set(BEMAN_USE_MODULES ON)
56-
message(STATUS "BEMAN_USE_MODULES=${BEMAN_USE_MODULES}")
57-
target_compile_definitions(${TARGET_NAME} PUBLIC BEMAN_USE_MODULES)
58-
else()
59-
message(WARNING "Missing support for CMAKE_CXX_SCAN_FOR_MODULES!")
60-
endif()
61-
62-
if(BEMAN_USE_MODULES AND CMAKE_CXX_MODULE_STD)
63-
set(BEMAN_HAS_IMPORT_STD ON)
64-
message(STATUS "BEMAN_HAS_IMPORT_STD=${BEMAN_HAS_IMPORT_STD}")
65-
target_compile_definitions(${TARGET_NAME} PUBLIC BEMAN_HAS_IMPORT_STD)
66-
else()
67-
set(CMAKE_CXX_MODULE_STD OFF)
68-
message(WARNING "Missing support for CMAKE_CXX_MODULE_STD!")
69-
endif()
70-
71-
# gersemi: off
72-
if(CMAKE_EXPORT_COMPILE_COMMANDS)
73-
set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
74-
message(
75-
STATUS
76-
"CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES=${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}"
77-
)
78-
endif()
79-
# gersemi: on
80-
#===============================================================================
29+
target_compile_features(${TARGET_LIBRARY} PUBLIC cxx_std_23)
8130

8231
option(
8332
BEMAN_EXECUTION_ENABLE_TESTING
@@ -102,9 +51,9 @@ set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
10251

10352
add_subdirectory(src/beman/execution)
10453

105-
if(BEMAN_EXECUTION_ENABLE_TESTING)
106-
enable_testing()
54+
enable_testing()
10755

56+
if(BEMAN_EXECUTION_ENABLE_TESTING)
10857
add_subdirectory(tests/beman/execution)
10958
endif()
11059

@@ -117,6 +66,9 @@ if(NOT BEMAN_EXECUTION_ENABLE_INSTALL OR CMAKE_SKIP_INSTALL_RULES)
11766
return()
11867
endif()
11968

69+
include(beman-install-library-config)
70+
# TODO(CK): beman_install_library(${TARGET_LIBRARY})
71+
12072
include(CMakePackageConfigHelpers)
12173

12274
write_basic_package_version_file(
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2+
include_guard(GLOBAL)
3+
4+
# This file defines the function `beman_install_library` which is used to
5+
# install a library target and its headers, along with optional CMake
6+
# configuration files.
7+
#
8+
# The function is designed to be reusable across different Beman libraries.
9+
10+
function(beman_install_library name)
11+
# Usage
12+
# -----
13+
#
14+
# beman_install_library(NAME)
15+
#
16+
# Brief
17+
# -----
18+
#
19+
# This function installs the specified library target and its headers.
20+
# It also handles the installation of the CMake configuration files if needed.
21+
#
22+
# CMake variables
23+
# ---------------
24+
#
25+
# Note that configuration of the installation is generally controlled by CMake
26+
# cache variables so that they can be controlled by the user or tool running the
27+
# `cmake` command. Neither `CMakeLists.txt` nor `*.cmake` files should set these
28+
# variables directly.
29+
#
30+
# - BEMAN_INSTALL_CONFIG_FILE_PACKAGES:
31+
# List of packages that require config file installation.
32+
# If the package name is in this list, it will install the config file.
33+
#
34+
# - <PREFIX>_INSTALL_CONFIG_FILE_PACKAGE:
35+
# Boolean to control config file installation for the specific library.
36+
# The prefix `<PREFIX>` is the uppercased name of the library with dots
37+
# replaced by underscores.
38+
#
39+
if(NOT TARGET "${name}")
40+
message(FATAL_ERROR "Target '${name}' does not exist.")
41+
endif()
42+
43+
if(NOT ARGN STREQUAL "")
44+
message(
45+
FATAL_ERROR
46+
"beman_install_library does not accept extra arguments: ${ARGN}"
47+
)
48+
endif()
49+
50+
# Given foo.bar, the component name is bar
51+
string(REPLACE "." ";" name_parts "${name}")
52+
# fail if the name doesn't look like foo.bar
53+
list(LENGTH name_parts name_parts_length)
54+
if(NOT name_parts_length EQUAL 2)
55+
message(
56+
FATAL_ERROR
57+
"beman_install_library expects a name of the form 'beman.<name>', got '${name}'"
58+
)
59+
endif()
60+
61+
set(target_name "${name}")
62+
set(install_component_name "${name}")
63+
set(export_name "${name}")
64+
set(package_name "${name}")
65+
list(GET name_parts -1 component_name)
66+
67+
include(GNUInstallDirs)
68+
69+
install(
70+
TARGETS "${target_name}"
71+
COMPONENT "${install_component_name}"
72+
EXPORT "${export_name}"
73+
FILE_SET HEADERS
74+
FILE_SET CXX_MODULES
75+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${package_name}/modules
76+
)
77+
78+
set_target_properties(
79+
"${target_name}"
80+
PROPERTIES EXPORT_NAME "${component_name}"
81+
)
82+
83+
# Determine the prefix for project-specific variables
84+
string(TOUPPER "${name}" project_prefix)
85+
string(REPLACE "." "_" project_prefix "${project_prefix}")
86+
87+
option(
88+
${project_prefix}_INSTALL_CONFIG_FILE_PACKAGE
89+
"Enable creating and installing a CMake config-file package. Default: ON. Values: { ON, OFF }."
90+
ON
91+
)
92+
93+
# By default, install the config package
94+
set(install_config_package ON)
95+
96+
# Turn OFF installation of config package by default if,
97+
# in order of precedence:
98+
# 1. The specific package variable is set to OFF
99+
# 2. The package name is not in the list of packages to install config files
100+
if(DEFINED BEMAN_INSTALL_CONFIG_FILE_PACKAGES)
101+
if(
102+
NOT "${install_component_name}"
103+
IN_LIST
104+
BEMAN_INSTALL_CONFIG_FILE_PACKAGES
105+
)
106+
set(install_config_package OFF)
107+
endif()
108+
endif()
109+
if(DEFINED ${project_prefix}_INSTALL_CONFIG_FILE_PACKAGE)
110+
set(install_config_package
111+
${${project_prefix}_INSTALL_CONFIG_FILE_PACKAGE}
112+
)
113+
endif()
114+
115+
if(install_config_package)
116+
message(
117+
DEBUG
118+
"beman-install-library: Installing a config package for '${name}'"
119+
)
120+
121+
include(CMakePackageConfigHelpers)
122+
123+
find_file(
124+
config_file_template
125+
NAMES "${package_name}-config.cmake.in"
126+
PATHS "${PROJECT_SOURCE_DIR}/cmake"
127+
NO_DEFAULT_PATH
128+
NO_CACHE
129+
REQUIRED
130+
)
131+
set(config_package_file
132+
"${CMAKE_CURRENT_BINARY_DIR}/${package_name}-config.cmake"
133+
)
134+
set(package_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${package_name}")
135+
configure_package_config_file(
136+
"${config_file_template}"
137+
"${config_package_file}"
138+
INSTALL_DESTINATION "${package_install_dir}"
139+
PATH_VARS PROJECT_NAME PROJECT_VERSION
140+
)
141+
142+
set(config_version_file
143+
"${CMAKE_CURRENT_BINARY_DIR}/${package_name}-config-version.cmake"
144+
)
145+
write_basic_package_version_file(
146+
"${config_version_file}"
147+
VERSION "${PROJECT_VERSION}"
148+
COMPATIBILITY ExactVersion
149+
)
150+
151+
install(
152+
FILES "${config_package_file}" "${config_version_file}"
153+
DESTINATION "${package_install_dir}"
154+
COMPONENT "${install_component_name}"
155+
)
156+
157+
set(config_targets_file "${package_name}-targets.cmake")
158+
install(
159+
EXPORT "${export_name}"
160+
DESTINATION "${package_install_dir}"
161+
NAMESPACE beman::
162+
FILE "${config_targets_file}"
163+
CXX_MODULES_DIRECTORY
164+
cxx-modules
165+
COMPONENT "${install_component_name}"
166+
)
167+
else()
168+
message(
169+
DEBUG
170+
"beman-install-library: Not installing a config package for '${name}'"
171+
)
172+
endif()
173+
endfunction()

0 commit comments

Comments
 (0)