Skip to content

Commit 2ac1757

Browse files
feat(cmake): Enhance cpp_library() with argument validation, test-only linking, and improved docstrings. (#44)
Co-authored-by: davidlion <davidlion2@protonmail.com>
1 parent 8090ca6 commit 2ac1757

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

CMake/ystdlib-cpp-helpers.cmake

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
# @param SOURCE_LIST The list of source files to check.
2-
# @param IS_HEADER_ONLY Returns whether the list only contains header files.
3-
# @param NON_HEADER_FILE Returns the name of the first, if any, non-header file.
1+
# @param {string[]} REQUIRED_ARGS The list of arguments to check.
2+
# @param {string[]} ARG_KEYWORDS_MISSING_VALUES The list of arguments with missing values.
3+
# @param {string} TARGET_NAME The target on which to perform the check.
4+
function(require_argument_values REQUIRED_ARGS ARG_KEYWORDS_MISSING_VALUES TARGET_NAME)
5+
foreach(arg IN LISTS REQUIRED_ARGS)
6+
if("${arg}" IN_LIST ARG_KEYWORDS_MISSING_VALUES)
7+
message(FATAL_ERROR "Missing values for argument '${arg}' in target '${TARGET_NAME}'.")
8+
endif()
9+
endforeach()
10+
endfunction()
11+
12+
# @param {string[]} SOURCE_LIST The list of source files to check.
13+
# @param {bool} IS_HEADER_ONLY Returns TRUE if list only contains header files, FALSE otherwise.
14+
# @param {string} NON_HEADER_FILE Returns the name of the first, if any, non-header file.
415
function(check_if_header_only SOURCE_LIST IS_HEADER_ONLY NON_HEADER_FILE)
516
set(_LOCAL_SOURCE_LIST "${${SOURCE_LIST}}")
617
foreach(src_file IN LISTS _LOCAL_SOURCE_LIST)
@@ -20,16 +31,17 @@ endfunction()
2031
# If `YSTDLIB_CPP_ENABLE_TESTS` is ON, builds the unit tests specific to the current library, and
2132
# links this library against the unified unit test target for the entire `ystdlib-cpp` project.
2233
#
23-
# @param NAME
24-
# @param NAMESPACE
25-
# @param PUBLIC_HEADERS
26-
# @param PRIVATE_SOURCES
27-
# @param PUBLIC_LINK_LIBRARIES
28-
# @param PRIVATE_LINK_LIBRARIES
29-
# @parms TESTS_SOURCES
30-
# @param [BUILD_INCLUDE_DIR="${PROJECT_SOURCE_DIR}/src"] The list of include paths for building the
31-
# library and for external projects that builds `ystdlib-cpp` as a CMAKE subproject via the
32-
# add_subdirectory() function.
34+
# @param {string} NAME
35+
# @param {string} NAMESPACE
36+
# @param {string[]} PUBLIC_HEADERS
37+
# @param {string[]} TESTS_SOURCES
38+
# @param {string[]} [PRIVATE_SOURCES]
39+
# @param {string[]} [PUBLIC_LINK_LIBRARIES]
40+
# @param {string[]} [PRIVATE_LINK_LIBRARIES]
41+
# @param {string[]} [TESTS_LINK_LIBRARIES]
42+
# @param {string[]} [BUILD_INCLUDE_DIR="${PROJECT_SOURCE_DIR}/src"] The list of include paths for
43+
# building the library and for external projects that builds `ystdlib-cpp` as a CMAKE subproject via
44+
# the add_subdirectory() function.
3345
function(cpp_library)
3446
set(options "")
3547
set(oneValueArgs
@@ -42,16 +54,27 @@ function(cpp_library)
4254
PUBLIC_LINK_LIBRARIES
4355
PRIVATE_LINK_LIBRARIES
4456
TESTS_SOURCES
57+
TESTS_LINK_LIBRARIES
58+
BUILD_INCLUDE_DIR
59+
)
60+
set(requireValueArgs
61+
NAME
62+
NAMESPACE
63+
PUBLIC_HEADERS
64+
TESTS_SOURCES
4565
BUILD_INCLUDE_DIR
4666
)
4767
cmake_parse_arguments(arg_cpp_lib "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
4868

4969
set(_ALIAS_TARGET_NAME "${arg_cpp_lib_NAMESPACE}::${arg_cpp_lib_NAME}")
5070

51-
# TODO: Turn this into a function for handling other optional params that have default values.
52-
if("BUILD_INCLUDE_DIR" IN_LIST arg_cpp_lib_KEYWORDS_MISSING_VALUES)
53-
message(FATAL_ERROR "Missing build interface list for ${_ALIAS_TARGET_NAME}.")
54-
elseif(NOT DEFINED arg_cpp_lib_BUILD_INCLUDE_DIR)
71+
require_argument_values(
72+
"${requireValueArgs}"
73+
"${arg_cpp_lib_KEYWORDS_MISSING_VALUES}"
74+
"${_ALIAS_TARGET_NAME}"
75+
)
76+
77+
if(NOT DEFINED arg_cpp_lib_BUILD_INCLUDE_DIR)
5578
set(arg_cpp_lib_BUILD_INCLUDE_DIR "${PROJECT_SOURCE_DIR}/src")
5679
endif()
5780

@@ -116,6 +139,7 @@ function(cpp_library)
116139
PRIVATE
117140
Catch2::Catch2WithMain
118141
${_ALIAS_TARGET_NAME}
142+
${arg_cpp_lib_TESTS_LINK_LIBRARIES}
119143
)
120144
target_compile_features(${_UNIT_TEST_TARGET} PRIVATE cxx_std_20)
121145
set_property(
@@ -128,6 +152,11 @@ function(cpp_library)
128152

129153
# Link against unified unit test
130154
target_sources(${UNIFIED_UNIT_TEST_TARGET} PRIVATE ${arg_cpp_lib_TESTS_SOURCES})
131-
target_link_libraries(${UNIFIED_UNIT_TEST_TARGET} PRIVATE ${_ALIAS_TARGET_NAME})
155+
target_link_libraries(
156+
${UNIFIED_UNIT_TEST_TARGET}
157+
PRIVATE
158+
${_ALIAS_TARGET_NAME}
159+
${arg_cpp_lib_TESTS_LINK_LIBRARIES}
160+
)
132161
endif()
133162
endfunction()

0 commit comments

Comments
 (0)