Skip to content

Commit 13cf43f

Browse files
committed
Adding contracts for CMake functions.
1 parent 77c30bc commit 13cf43f

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

cmake/cpp-library-ci.cmake

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
#
55
# This module handles GitHub Actions workflow generation with PROJECT_NAME substitution
66

7-
# Function to configure CI workflow template
8-
# Arguments: force_init (boolean) - whether to overwrite existing file
7+
# Generates GitHub Actions CI workflow from template with PROJECT_NAME substitution.
8+
# - Postcondition: .github/workflows/ci.yml created from template if not present
9+
# - With force_init: overwrites existing workflow file
910
function(_cpp_library_setup_ci force_init)
1011
set(ci_template "${CPP_LIBRARY_ROOT}/templates/.github/workflows/ci.yml.in")
1112
set(ci_dest "${CMAKE_CURRENT_SOURCE_DIR}/.github/workflows/ci.yml")

cmake/cpp-library-docs.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#
33
# cpp-library-docs.cmake - Documentation setup with Doxygen
44

5+
# Creates 'docs' target for generating API documentation with Doxygen and doxygen-awesome-css theme.
6+
# - Precondition: NAME, VERSION, and DESCRIPTION specified; Doxygen available
7+
# - Postcondition: 'docs' custom target created, Doxyfile configured, theme downloaded via CPM
58
function(_cpp_library_setup_docs)
69
set(oneValueArgs
710
NAME

cmake/cpp-library-install.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
include(GNUInstallDirs)
1313
include(CMakePackageConfigHelpers)
1414

15-
# Function to setup installation for a library target
16-
# This should be called from _cpp_library_setup_core when PROJECT_IS_TOP_LEVEL
15+
# Configures CMake install rules for library target and package config files.
16+
# - Precondition: NAME, VERSION, and NAMESPACE specified; target NAME exists
17+
# - Postcondition: install rules created for target, config files, and export with NAMESPACE:: prefix
18+
# - Supports header-only (INTERFACE) and compiled libraries, uses SameMajorVersion compatibility
1719
function(_cpp_library_setup_install)
1820
set(oneValueArgs
1921
NAME # Target name (e.g., "stlab-enum-ops")

cmake/cpp-library-setup.cmake

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

5-
# Function to get version from git tags
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
67
function(_cpp_library_get_git_version OUTPUT_VAR)
78
# Try to get version from git tags
89
execute_process(
@@ -24,6 +25,9 @@ function(_cpp_library_get_git_version OUTPUT_VAR)
2425
endif()
2526
endfunction()
2627

28+
# Creates library target (INTERFACE or compiled) with headers and proper configuration.
29+
# - Precondition: NAME, NAMESPACE, and REQUIRES_CPP_VERSION specified
30+
# - Postcondition: library target created with alias NAMESPACE::CLEAN_NAME, install configured if TOP_LEVEL
2731
function(_cpp_library_setup_core)
2832
set(oneValueArgs
2933
NAME
@@ -100,7 +104,9 @@ function(_cpp_library_setup_core)
100104

101105
endfunction()
102106

103-
# Function to copy static template files
107+
# Copies template files (.clang-format, .gitignore, etc.) to project root if not present.
108+
# - Postcondition: missing template files copied to project, CI workflow configured with PROJECT_NAME substitution
109+
# - With FORCE_INIT: overwrites existing files
104110
function(_cpp_library_copy_templates)
105111
set(options FORCE_INIT)
106112
cmake_parse_arguments(ARG "${options}" "" "" ${ARGN})

cmake/cpp-library-testing.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
# This file is kept for backward compatibility but the actual implementation
77
# is now in the _cpp_library_setup_executables function.
88

9-
# Legacy function - now delegates to the consolidated implementation
9+
# Delegates to _cpp_library_setup_executables for backward compatibility.
10+
# - Postcondition: test executables configured via _cpp_library_setup_executables
1011
function(_cpp_library_setup_testing)
1112
set(oneValueArgs
1213
NAME

cpp-library.cmake

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-docs.cmake")
1818
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-install.cmake")
1919
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-ci.cmake")
2020

21-
# Shared function to handle examples and tests consistently
21+
# Creates test or example executables and registers them with CTest.
22+
# - Precondition: doctest target available via CPM, source files exist in TYPE directory
23+
# - Postcondition: executables created and added as tests (unless in clang-tidy mode)
24+
# - Executables with "_fail" suffix are added as negative compilation tests
2225
function(_cpp_library_setup_executables)
2326
set(oneValueArgs
2427
NAME
@@ -99,7 +102,10 @@ function(_cpp_library_setup_executables)
99102

100103
endfunction()
101104

102-
# Main entry point function - users call this to set up their library
105+
# Sets up a C++ header-only or compiled library with testing, docs, and install support.
106+
# - Precondition: PROJECT_NAME defined via project(), at least one HEADERS specified
107+
# - Postcondition: library target created, version set from git tags, optional tests/docs/examples configured
108+
# - When PROJECT_IS_TOP_LEVEL: also configures templates, testing, docs, and installation
103109
function(cpp_library_setup)
104110
# Parse arguments
105111
set(oneValueArgs

0 commit comments

Comments
 (0)