|
| 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}" |
| 69 | + COMPONENT "${install_component_name}" |
| 70 | + EXPORT "${export_name}" |
| 71 | + FILE_SET HEADERS |
| 72 | + ) |
| 73 | + |
| 74 | + set_target_properties( |
| 75 | + "${target_name}" |
| 76 | + PROPERTIES EXPORT_NAME "${component_name}" |
| 77 | + ) |
| 78 | + |
| 79 | + include(GNUInstallDirs) |
| 80 | + |
| 81 | + # Determine the prefix for project-specific variables |
| 82 | + string(TOUPPER "${name}" project_prefix) |
| 83 | + string(REPLACE "." "_" project_prefix "${project_prefix}") |
| 84 | + |
| 85 | + option( |
| 86 | + ${project_prefix}_INSTALL_CONFIG_FILE_PACKAGE |
| 87 | + "Enable building examples. Default: ${PROJECT_IS_TOP_LEVEL}. Values: { ON, OFF }." |
| 88 | + ${PROJECT_IS_TOP_LEVEL} |
| 89 | + ) |
| 90 | + |
| 91 | + # By default, install the config package |
| 92 | + set(install_config_package ON) |
| 93 | + |
| 94 | + # Turn OFF installation of config package by default if, |
| 95 | + # in order of precedence: |
| 96 | + # 1. The specific package variable is set to OFF |
| 97 | + # 2. The package name is not in the list of packages to install config files |
| 98 | + if(DEFINED BEMAN_INSTALL_CONFIG_FILE_PACKAGES) |
| 99 | + if( |
| 100 | + NOT "${install_component_name}" |
| 101 | + IN_LIST |
| 102 | + BEMAN_INSTALL_CONFIG_FILE_PACKAGES |
| 103 | + ) |
| 104 | + set(install_config_package OFF) |
| 105 | + endif() |
| 106 | + endif() |
| 107 | + if(DEFINED ${project_prefix}_INSTALL_CONFIG_FILE_PACKAGE) |
| 108 | + set(install_config_package |
| 109 | + ${${project_prefix}_INSTALL_CONFIG_FILE_PACKAGE} |
| 110 | + ) |
| 111 | + endif() |
| 112 | + |
| 113 | + if(install_config_package) |
| 114 | + message( |
| 115 | + DEBUG |
| 116 | + "beman-install-library: Installing a config package for '${name}'" |
| 117 | + ) |
| 118 | + |
| 119 | + include(CMakePackageConfigHelpers) |
| 120 | + |
| 121 | + find_file( |
| 122 | + config_file_template |
| 123 | + NAMES "${package_name}-config.cmake.in" |
| 124 | + PATHS "${CMAKE_CURRENT_SOURCE_DIR}" |
| 125 | + NO_DEFAULT_PATH |
| 126 | + NO_CACHE |
| 127 | + REQUIRED |
| 128 | + ) |
| 129 | + set(config_package_file |
| 130 | + "${CMAKE_CURRENT_BINARY_DIR}/${package_name}-config.cmake" |
| 131 | + ) |
| 132 | + set(package_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${package_name}") |
| 133 | + configure_package_config_file( |
| 134 | + "${config_file_template}" |
| 135 | + "${config_package_file}" |
| 136 | + INSTALL_DESTINATION "${package_install_dir}" |
| 137 | + PATH_VARS PROJECT_NAME PROJECT_VERSION |
| 138 | + ) |
| 139 | + |
| 140 | + set(config_version_file |
| 141 | + "${CMAKE_CURRENT_BINARY_DIR}/${package_name}-config-version.cmake" |
| 142 | + ) |
| 143 | + write_basic_package_version_file( |
| 144 | + "${config_version_file}" |
| 145 | + VERSION "${PROJECT_VERSION}" |
| 146 | + COMPATIBILITY ExactVersion |
| 147 | + ) |
| 148 | + |
| 149 | + install( |
| 150 | + FILES "${config_package_file}" "${config_version_file}" |
| 151 | + DESTINATION "${package_install_dir}" |
| 152 | + COMPONENT "${install_component_name}" |
| 153 | + ) |
| 154 | + |
| 155 | + set(config_targets_file "${package_name}-targets.cmake") |
| 156 | + install( |
| 157 | + EXPORT "${export_name}" |
| 158 | + DESTINATION "${package_install_dir}" |
| 159 | + NAMESPACE beman:: |
| 160 | + FILE "${config_targets_file}" |
| 161 | + COMPONENT "${install_component_name}" |
| 162 | + ) |
| 163 | + else() |
| 164 | + message( |
| 165 | + DEBUG |
| 166 | + "beman-install-library: Not installing a config package for '${name}'" |
| 167 | + ) |
| 168 | + endif() |
| 169 | +endfunction() |
0 commit comments