Skip to content

Commit c1bce1c

Browse files
committed
Refactor to use PACKAGE_NAME for target and CI consistency
Replaces usage of a 'clean' name with PACKAGE_NAME for library target aliases and CI workflow templates. Ensures consistent naming between CMake targets and package discovery, improving clarity and reducing potential mismatches.
1 parent fc7d4e4 commit c1bce1c

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

cmake/cpp-library-setup.cmake

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ function(_cpp_library_get_git_version OUTPUT_VAR)
3333
endfunction()
3434

3535
# Creates library target (INTERFACE or compiled) with headers and proper configuration.
36-
# - Precondition: NAME, NAMESPACE, and REQUIRES_CPP_VERSION specified
37-
# - Postcondition: library target created with alias NAMESPACE::CLEAN_NAME, install configured if TOP_LEVEL
36+
# - Precondition: NAME, NAMESPACE, PACKAGE_NAME, and REQUIRES_CPP_VERSION specified
37+
# - Postcondition: library target created with alias NAMESPACE::PACKAGE_NAME, install configured if TOP_LEVEL
3838
function(_cpp_library_setup_core)
3939
set(oneValueArgs
4040
NAME
4141
VERSION
4242
DESCRIPTION
4343
NAMESPACE
44+
PACKAGE_NAME
4445
REQUIRES_CPP_VERSION
4546
TOP_LEVEL
4647
)
@@ -60,13 +61,10 @@ function(_cpp_library_setup_core)
6061
# Note: Project declaration is now handled in the main cpp_library_setup function
6162
# No need to check ARG_TOP_LEVEL here for project declaration
6263

63-
# Extract the library name without namespace prefix for target naming
64-
string(REPLACE "${ARG_NAMESPACE}-" "" CLEAN_NAME "${ARG_NAME}")
65-
6664
if(ARG_SOURCES)
6765
# Create a library with sources (respects BUILD_SHARED_LIBS variable)
6866
add_library(${ARG_NAME} ${ARG_SOURCES})
69-
add_library(${ARG_NAMESPACE}::${CLEAN_NAME} ALIAS ${ARG_NAME})
67+
add_library(${ARG_NAMESPACE}::${ARG_PACKAGE_NAME} ALIAS ${ARG_NAME})
7068
target_include_directories(${ARG_NAME} PUBLIC
7169
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
7270
$<INSTALL_INTERFACE:include>
@@ -83,7 +81,7 @@ function(_cpp_library_setup_core)
8381
else()
8482
# Header-only INTERFACE target
8583
add_library(${ARG_NAME} INTERFACE)
86-
add_library(${ARG_NAMESPACE}::${CLEAN_NAME} ALIAS ${ARG_NAME})
84+
add_library(${ARG_NAMESPACE}::${ARG_PACKAGE_NAME} ALIAS ${ARG_NAME})
8785
target_include_directories(${ARG_NAME} INTERFACE
8886
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
8987
$<INSTALL_INTERFACE:include>
@@ -103,7 +101,7 @@ function(_cpp_library_setup_core)
103101
if(ARG_TOP_LEVEL)
104102
_cpp_library_setup_install(
105103
NAME "${ARG_NAME}"
106-
PACKAGE_NAME "${CLEAN_NAME}"
104+
PACKAGE_NAME "${ARG_PACKAGE_NAME}"
107105
VERSION "${ARG_VERSION}"
108106
NAMESPACE "${ARG_NAMESPACE}"
109107
HEADERS "${ARG_HEADERS}"

cpp-library.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ function(cpp_library_setup)
140140
endif()
141141
set(ARG_NAME "${PROJECT_NAME}")
142142

143+
# Calculate PACKAGE_NAME (clean name without namespace prefix) for template substitution
144+
string(REPLACE "${ARG_NAMESPACE}-" "" PACKAGE_NAME "${ARG_NAME}")
145+
143146
# Set defaults
144147
if(NOT ARG_REQUIRES_CPP_VERSION)
145148
set(ARG_REQUIRES_CPP_VERSION 17)
@@ -184,6 +187,7 @@ function(cpp_library_setup)
184187
VERSION "${ARG_VERSION}"
185188
DESCRIPTION "${ARG_DESCRIPTION}"
186189
NAMESPACE "${ARG_NAMESPACE}"
190+
PACKAGE_NAME "${PACKAGE_NAME}"
187191
HEADERS "${GENERATED_HEADERS}"
188192
SOURCES "${GENERATED_SOURCES}"
189193
REQUIRES_CPP_VERSION "${ARG_REQUIRES_CPP_VERSION}"

templates/.github/workflows/ci.yml.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,9 @@ jobs:
8383
cmake_minimum_required(VERSION 3.20)
8484
project(test-find-package CXX)
8585

86-
find_package(@PROJECT_NAME@ REQUIRED)
86+
find_package(@PACKAGE_NAME@ REQUIRED)
8787

88-
message(STATUS "Successfully found @PROJECT_NAME@")
88+
message(STATUS "Successfully found @PACKAGE_NAME@")
8989
EOF
9090

9191
# Convert paths to forward slashes for CMake (works on all platforms)

0 commit comments

Comments
 (0)