Skip to content

Commit 8e45a74

Browse files
authored
Merge pull request bemanproject#171 from bretbrownjr/beman-install-library
Add a beman_install_library CMake function
2 parents 41d3ed8 + 1d2453a commit 8e45a74

6 files changed

Lines changed: 167 additions & 1 deletion

cmake/appleclang-toolchain.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
3939

4040
set(CMAKE_C_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
4141
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
42+
43+
# Add this dir to the module path so that `find_package(beman-install-library)` works
44+
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_LIST_DIR}")
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
install(
68+
TARGETS "${target_name}" COMPONENT "${install_component_name}"
69+
EXPORT "${export_name}"
70+
FILE_SET HEADERS
71+
)
72+
73+
set_target_properties(
74+
"${target_name}"
75+
PROPERTIES EXPORT_NAME "${component_name}"
76+
)
77+
78+
include(GNUInstallDirs)
79+
80+
# Determine the prefix for project-specific variables
81+
string(TOUPPER "${name}" project_prefix)
82+
string(REPLACE "." "_" project_prefix "${project_prefix}")
83+
84+
if(
85+
"${name}" IN_LIST BEMAN_INSTALL_CONFIG_FILE_PACKAGES
86+
OR "${project_prefix}_INSTALL_CONFIG_FILE_PACKAGE"
87+
)
88+
set(install_config_package ON)
89+
endif()
90+
91+
if(install_config_package)
92+
message(
93+
DEBUG
94+
"beman-install-library: Installing a config package for '${name}'"
95+
)
96+
97+
include(CMakePackageConfigHelpers)
98+
99+
find_file(
100+
config_file_template
101+
NAMES "${package_name}-config.cmake.in"
102+
PATHS "${CMAKE_CURRENT_SOURCE_DIR}"
103+
NO_DEFAULT_PATH
104+
NO_CACHE
105+
REQUIRED
106+
)
107+
set(config_package_file
108+
"${CMAKE_CURRENT_BINARY_DIR}/${package_name}-config.cmake"
109+
)
110+
set(package_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${package_name}")
111+
configure_package_config_file(
112+
"${config_file_template}"
113+
"${config_package_file}"
114+
INSTALL_DESTINATION "${package_install_dir}"
115+
PATH_VARS PROJECT_NAME PROJECT_VERSION
116+
)
117+
118+
set(config_version_file
119+
"${CMAKE_CURRENT_BINARY_DIR}/${package_name}-config-version.cmake"
120+
)
121+
write_basic_package_version_file(
122+
"${config_version_file}"
123+
VERSION "${PROJECT_VERSION}"
124+
COMPATIBILITY ExactVersion
125+
)
126+
127+
install(
128+
FILES "${config_package_file}" "${config_version_file}"
129+
DESTINATION "${package_install_dir}"
130+
COMPONENT "${install_component_name}"
131+
)
132+
133+
set(config_targets_file "${package_name}-targets.cmake")
134+
install(
135+
EXPORT "${export_name}"
136+
DESTINATION "${package_install_dir}"
137+
NAMESPACE beman::
138+
FILE "${config_targets_file}"
139+
COMPONENT "${install_component_name}"
140+
)
141+
else()
142+
message(
143+
DEBUG
144+
"beman-install-library: Not installing a config package for '${name}'"
145+
)
146+
endif()
147+
endfunction()

cmake/gnu-toolchain.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
3636

3737
set(CMAKE_C_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
3838
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
39+
40+
# Add this dir to the module path so that `find_package(beman-install-library)` works
41+
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_LIST_DIR}")

cmake/llvm-toolchain.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
3636

3737
set(CMAKE_C_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
3838
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
39+
40+
# Add this dir to the module path so that `find_package(beman-install-library)` works
41+
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_LIST_DIR}")

cmake/msvc-toolchain.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "${RELEASE_FLAGS}")
3636

3737
set(CMAKE_C_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
3838
set(CMAKE_CXX_FLAGS_RELEASE_INIT "${RELEASE_FLAGS}")
39+
40+
# Add this dir to the module path so that `find_package(beman-install-library)` works
41+
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_LIST_DIR}")

cmake/use-fetch-content.cmake

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ function(BemanExemplar_provideDependency method package_name)
152152
APPEND
153153
BemanExemplar_debug
154154
"Redirecting find_package calls for ${BemanExemplar_pkgName} "
155-
"to FetchContent logic fetching ${BemanExemplar_repo} at "
155+
"to FetchContent logic.\n"
156+
string
157+
APPEND
158+
BemanExemplar_debug
159+
"Fetching ${BemanExemplar_repo} at "
156160
"${BemanExemplar_tag} according to ${BemanExemplar_lockfile}."
157161
)
158162
message(DEBUG "${BemanExemplar_debug}")
@@ -177,3 +181,6 @@ cmake_language(
177181
SET_DEPENDENCY_PROVIDER BemanExemplar_provideDependency
178182
SUPPORTED_METHODS FIND_PACKAGE
179183
)
184+
185+
# Add this dir to the module path so that `find_package(beman-install-library)` works
186+
list(APPEND CMAKE_PREFIX_PATH "${CMAKE_CURRENT_LIST_DIR}")

0 commit comments

Comments
 (0)