Skip to content

Commit 49eaf58

Browse files
committed
Add PACKAGE_NAME to install setup and improve version detection
The _cpp_library_setup_install function now requires a PACKAGE_NAME argument, ensuring correct package naming in generated CMake config files and install locations. Version detection in _cpp_library_get_git_version now prefers PROJECT_VERSION if set, improving compatibility with package managers and source archives.
1 parent 69e1aff commit 49eaf58

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

cmake/cpp-library-install.cmake

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ include(GNUInstallDirs)
1313
include(CMakePackageConfigHelpers)
1414

1515
# Configures CMake install rules for library target and package config files.
16-
# - Precondition: NAME, VERSION, and NAMESPACE specified; target NAME exists
16+
# - Precondition: NAME, PACKAGE_NAME, VERSION, and NAMESPACE specified; target NAME exists
1717
# - Postcondition: install rules created for target, config files, and export with NAMESPACE:: prefix
1818
# - Supports header-only (INTERFACE) and compiled libraries, uses SameMajorVersion compatibility
1919
function(_cpp_library_setup_install)
2020
set(oneValueArgs
21-
NAME # Target name (e.g., "stlab-enum-ops")
22-
VERSION # Version string (e.g., "1.2.3")
23-
NAMESPACE # Namespace for alias (e.g., "stlab")
21+
NAME # Target name (e.g., "stlab-enum-ops")
22+
PACKAGE_NAME # Package name for find_package() (e.g., "enum-ops")
23+
VERSION # Version string (e.g., "1.2.3")
24+
NAMESPACE # Namespace for alias (e.g., "stlab")
2425
)
2526
set(multiValueArgs
2627
HEADERS # List of header file paths (for FILE_SET support check)
@@ -32,6 +33,9 @@ function(_cpp_library_setup_install)
3233
if(NOT ARG_NAME)
3334
message(FATAL_ERROR "_cpp_library_setup_install: NAME is required")
3435
endif()
36+
if(NOT ARG_PACKAGE_NAME)
37+
message(FATAL_ERROR "_cpp_library_setup_install: PACKAGE_NAME is required")
38+
endif()
3539
if(NOT ARG_VERSION)
3640
message(FATAL_ERROR "_cpp_library_setup_install: VERSION is required")
3741
endif()
@@ -64,32 +68,32 @@ function(_cpp_library_setup_install)
6468
# Generate package version file
6569
# Uses SameMajorVersion compatibility (e.g., 2.1.0 is compatible with 2.0.0)
6670
write_basic_package_version_file(
67-
"${CMAKE_CURRENT_BINARY_DIR}/${ARG_NAME}ConfigVersion.cmake"
71+
"${CMAKE_CURRENT_BINARY_DIR}/${ARG_PACKAGE_NAME}ConfigVersion.cmake"
6872
VERSION ${ARG_VERSION}
6973
COMPATIBILITY SameMajorVersion
7074
)
7175

7276
# Generate package config file from template
7377
configure_file(
7478
"${CPP_LIBRARY_ROOT}/templates/Config.cmake.in"
75-
"${CMAKE_CURRENT_BINARY_DIR}/${ARG_NAME}Config.cmake"
79+
"${CMAKE_CURRENT_BINARY_DIR}/${ARG_PACKAGE_NAME}Config.cmake"
7680
@ONLY
7781
)
7882

7983
# Install export targets with namespace
8084
# This allows downstream projects to use find_package(package-name)
8185
# and link against namespace::target
8286
install(EXPORT ${ARG_NAME}Targets
83-
FILE ${ARG_NAME}Targets.cmake
87+
FILE ${ARG_PACKAGE_NAME}Targets.cmake
8488
NAMESPACE ${ARG_NAMESPACE}::
85-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${ARG_NAME}
89+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${ARG_PACKAGE_NAME}
8690
)
8791

8892
# Install package config and version files
8993
install(FILES
90-
"${CMAKE_CURRENT_BINARY_DIR}/${ARG_NAME}Config.cmake"
91-
"${CMAKE_CURRENT_BINARY_DIR}/${ARG_NAME}ConfigVersion.cmake"
92-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${ARG_NAME}
94+
"${CMAKE_CURRENT_BINARY_DIR}/${ARG_PACKAGE_NAME}Config.cmake"
95+
"${CMAKE_CURRENT_BINARY_DIR}/${ARG_PACKAGE_NAME}ConfigVersion.cmake"
96+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${ARG_PACKAGE_NAME}
9397
)
9498

9599
endfunction()

cmake/cpp-library-setup.cmake

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,16 @@
22
#
33
# cpp-library-setup.cmake - Core library setup functionality
44

5-
# Returns version string from latest git tag, falling back to "0.0.0".
6-
# - Postcondition: OUTPUT_VAR set to version string with 'v' prefix removed
5+
# Returns version string from PROJECT_VERSION (if set), git tag (with 'v' prefix removed), or
6+
# "0.0.0" fallback
77
function(_cpp_library_get_git_version OUTPUT_VAR)
8+
# If PROJECT_VERSION is already set (e.g., by vcpkg or other package manager),
9+
# use it instead of trying to query git (which may not be available in source archives)
10+
if(DEFINED PROJECT_VERSION AND NOT PROJECT_VERSION STREQUAL "")
11+
set(${OUTPUT_VAR} "${PROJECT_VERSION}" PARENT_SCOPE)
12+
return()
13+
endif()
14+
815
# Try to get version from git tags
916
execute_process(
1017
COMMAND git describe --tags --abbrev=0
@@ -96,6 +103,7 @@ function(_cpp_library_setup_core)
96103
if(ARG_TOP_LEVEL)
97104
_cpp_library_setup_install(
98105
NAME "${ARG_NAME}"
106+
PACKAGE_NAME "${CLEAN_NAME}"
99107
VERSION "${ARG_VERSION}"
100108
NAMESPACE "${ARG_NAMESPACE}"
101109
HEADERS "${ARG_HEADERS}"

0 commit comments

Comments
 (0)