Skip to content

Commit 832313c

Browse files
committed
making the ci.yml file into a cmake template
To simplify the ci logic, I'm changing the ci.yml file to a template.
1 parent e927f5b commit 832313c

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

cmake/cpp-library-ci.cmake

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# SPDX-License-Identifier: BSL-1.0
2+
#
3+
# cpp-library-ci.cmake - CI/CD configuration for cpp-library projects
4+
#
5+
# This module handles GitHub Actions workflow generation with PROJECT_NAME substitution
6+
7+
# Function to configure CI workflow template
8+
function(_cpp_library_setup_ci)
9+
set(options FORCE_INIT)
10+
cmake_parse_arguments(ARG "${options}" "" "" ${ARGN})
11+
12+
set(ci_template "${CPP_LIBRARY_ROOT}/templates/.github/workflows/ci.yml.in")
13+
set(ci_dest "${CMAKE_CURRENT_SOURCE_DIR}/.github/workflows/ci.yml")
14+
15+
if(EXISTS "${ci_template}" AND (NOT EXISTS "${ci_dest}" OR ARG_FORCE_INIT))
16+
get_filename_component(ci_dir "${ci_dest}" DIRECTORY)
17+
file(MAKE_DIRECTORY "${ci_dir}")
18+
configure_file("${ci_template}" "${ci_dest}" @ONLY)
19+
message(STATUS "Configured template file: .github/workflows/ci.yml")
20+
elseif(NOT EXISTS "${ci_template}")
21+
message(WARNING "CI template file not found: ${ci_template}")
22+
endif()
23+
endfunction()

cmake/cpp-library-setup.cmake

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,27 +113,22 @@ function(_cpp_library_copy_templates)
113113
".vscode/extensions.json"
114114
"docs/index.html"
115115
"CMakePresets.json"
116-
".github/workflows/ci.yml"
117116
)
118117

119118
foreach(template_file IN LISTS TEMPLATE_FILES)
120119
set(source_file "${CPP_LIBRARY_ROOT}/templates/${template_file}")
121120
set(dest_file "${CMAKE_CURRENT_SOURCE_DIR}/${template_file}")
122121

123-
# Check if template file exists
124-
if(EXISTS "${source_file}")
125-
# Copy if file doesn't exist or FORCE_INIT is enabled
126-
if(NOT EXISTS "${dest_file}" OR ARG_FORCE_INIT)
127-
# Create directory if needed
128-
get_filename_component(dest_dir "${dest_file}" DIRECTORY)
129-
file(MAKE_DIRECTORY "${dest_dir}")
130-
131-
# Copy the file
132-
file(COPY "${source_file}" DESTINATION "${dest_dir}")
133-
message(STATUS "Copied template file: ${template_file}")
134-
endif()
135-
else()
122+
if(EXISTS "${source_file}" AND (NOT EXISTS "${dest_file}" OR ARG_FORCE_INIT))
123+
get_filename_component(dest_dir "${dest_file}" DIRECTORY)
124+
file(MAKE_DIRECTORY "${dest_dir}")
125+
file(COPY "${source_file}" DESTINATION "${dest_dir}")
126+
message(STATUS "Copied template file: ${template_file}")
127+
elseif(NOT EXISTS "${source_file}")
136128
message(WARNING "Template file not found: ${source_file}")
137129
endif()
138130
endforeach()
131+
132+
# Setup CI workflow with PROJECT_NAME substitution
133+
_cpp_library_setup_ci(${ARG_FORCE_INIT})
139134
endfunction()

cpp-library.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-setup.cmake")
1616
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-testing.cmake")
1717
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-docs.cmake")
1818
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-install.cmake")
19+
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-ci.cmake")
1920

2021
# Shared function to handle examples and tests consistently
2122
function(_cpp_library_setup_executables)

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ jobs:
7373
mkdir -p ${{ runner.temp }}/test-find-package
7474
cd ${{ runner.temp }}/test-find-package
7575

76-
# Get project name from CMakeLists.txt (handles multi-line project() declarations)
77-
PACKAGE_NAME=$(sed -n '/project(/,/)/p' "${{ github.workspace }}/CMakeLists.txt" | tr '\n' ' ' | sed 's/.*project([ \t]*\([^ \t)]*\).*/\1/')
78-
7976
# Convert paths to forward slashes for CMake (works on all platforms)
8077
INSTALL_PREFIX=$(echo "${{ runner.temp }}/install" | sed 's|\\|/|g')
8178

@@ -85,9 +82,9 @@ jobs:
8582
project(test-find-package CXX)
8683

8784
set(CMAKE_PREFIX_PATH "${INSTALL_PREFIX}")
88-
find_package(${PACKAGE_NAME} REQUIRED)
85+
find_package(@PROJECT_NAME@ REQUIRED)
8986

90-
message(STATUS "Successfully found ${PACKAGE_NAME}")
87+
message(STATUS "Successfully found @PROJECT_NAME@")
9188
EOF
9289

9390
# Test find_package

0 commit comments

Comments
 (0)