11# Adds a c++20 interface library in the subdirectory NAME with the target NAME and alias
22# NAMESPACE::NAME. Libraries with multiple levels of namespace nesting are currently not supported.
33#
4+ # If `BUILD_TESTING` is ON, build the unit tests specific to the current library, and link this
5+ # library against the unified unit test target for the entire `ystdlib-cpp`.
6+ #
47# @param NAME
58# @param NAMESPACE
9+ # @parms TESTS_SOURCES
610# @param [LIB_BUILD_INTERFACE="${PROJECT_SOURCE_DIR}/src"] The list of include paths for building
711# the library and for external projects that link against it via the add_subdirectory() function.
812function (cpp_library)
@@ -11,7 +15,10 @@ function(cpp_library)
1115 NAME
1216 NAMESPACE
1317 )
14- set (multiValueArgs LIB_BUILD_INTERFACE)
18+ set (multiValueArgs
19+ TESTS_SOURCES
20+ LIB_BUILD_INTERFACE
21+ )
1522 cmake_parse_arguments (arg_cpp_lib "${options} " "${oneValueArgs} " "${multiValueArgs} " ${ARGN} )
1623
1724 # TODO: Turn this into a function for handling other optional params that have default values.
@@ -24,6 +31,7 @@ function(cpp_library)
2431 set (arg_cpp_lib_LIB_BUILD_INTERFACE "${PROJECT_SOURCE_DIR} /src" )
2532 endif ()
2633
34+ # Build interface library
2735 add_library (${arg_cpp_lib_NAME} INTERFACE )
2836 target_include_directories (
2937 ${arg_cpp_lib_NAME}
@@ -32,4 +40,32 @@ function(cpp_library)
3240 )
3341 target_compile_features (${arg_cpp_lib_NAME} INTERFACE cxx_std_20)
3442 add_library (${arg_cpp_lib_NAMESPACE} ::${arg_cpp_lib_NAME} ALIAS ${arg_cpp_lib_NAME} )
43+
44+ if (BUILD_TESTING)
45+ set (_UNIT_TEST_TARGET "unit-test-${arg_cpp_lib_NAME} " )
46+ add_executable (${_UNIT_TEST_TARGET} )
47+ target_sources (${_UNIT_TEST_TARGET} PRIVATE ${arg_cpp_lib_TESTS_SOURCES} )
48+ target_link_libraries (
49+ ${_UNIT_TEST_TARGET}
50+ PRIVATE
51+ Catch2::Catch2WithMain
52+ ${arg_cpp_lib_NAMESPACE} ::${arg_cpp_lib_NAME}
53+ )
54+ target_compile_features (${_UNIT_TEST_TARGET} PRIVATE cxx_std_20)
55+ set_property (
56+ TARGET
57+ ${_UNIT_TEST_TARGET}
58+ PROPERTY
59+ RUNTIME_OUTPUT_DIRECTORY
60+ ${CMAKE_BINARY_DIR} /testbin
61+ )
62+
63+ # Link against unified unit test
64+ target_sources (${UNIFIED_UNIT_TEST_TARGET} PRIVATE ${arg_cpp_lib_TESTS_SOURCES} )
65+ target_link_libraries (
66+ ${UNIFIED_UNIT_TEST_TARGET}
67+ PRIVATE
68+ ${arg_cpp_lib_NAMESPACE} ::${arg_cpp_lib_NAME}
69+ )
70+ endif ()
3571endfunction ()
0 commit comments