2025-11-15 20:59:52 +03:30
function ( add_module )
2025-11-14 16:04:37 +03:30
cmake_parse_arguments (
A R G S
" "
" N A M E "
2025-11-15 20:59:52 +03:30
" I N T E R F A C E S ; R O O T _ D I R ; S O U R C E S ; D E P E N D E N C I E S ; P R I V A T E _ D E P E N D E N C I E S ; T E S T S ; E N T R Y P O I N T "
2025-11-14 16:04:37 +03:30
$ { A R G N }
)
2025-07-05 13:28:41 +03:30
2025-11-14 16:04:37 +03:30
if ( NOT ARGS_NAME )
message ( FATAL_ERROR "You must provide a name" )
endif ( )
2025-07-10 13:29:03 +03:30
2025-11-15 20:59:52 +03:30
set ( target_library_name ${ ARGS_NAME } )
set ( target_executable_name ${ ARGS_NAME } )
2025-07-05 13:28:41 +03:30
2025-11-15 20:59:52 +03:30
set ( module_directory "${CMAKE_CURRENT_SOURCE_DIR}/${target_library_name}" )
2025-11-14 16:04:37 +03:30
if ( ARGS_ROOT_DIR )
set ( module_directory "${ARGS_ROOT_DIR}" )
endif ( )
2025-07-20 04:46:15 +03:30
2025-11-30 09:46:48 +03:30
# In this case, the module is an executable, so we prepend "lib" to the target name.
# And set the "executable_target" name to ARGS_NAME.
# The rationale here is to easily be able to write tests for an executable modules's interfaces...
# by splitting it into two targets: lib"executable_name" for the interface and "executable_name" for the "int main()" defining file (the entrypoint).
# the lib"executable_name" should not be disruptive since an executable module's library will not be dependent upon (except by the tests within the same module)
2025-11-15 20:59:52 +03:30
if ( ARGS_ENTRYPOINT )
set ( target_library_name "lib_${ARGS_NAME}" )
2025-11-30 09:46:48 +03:30
add_executable ( ${ target_executable_name } ${ module_directory } / ${ ARGS_ENTRYPOINT } )
2025-11-15 20:59:52 +03:30
endif ( )
add_library ( ${ target_library_name } )
2025-11-14 16:04:37 +03:30
if ( ARGS_SOURCES )
set ( files )
foreach ( file ${ ARGS_SOURCES } )
list ( APPEND files "${module_directory}/${file}" )
endforeach ( )
2025-11-15 20:59:52 +03:30
target_sources ( ${ target_library_name } PRIVATE ${ files } )
endif ( )
if ( ARGS_PUBLIC_SOURECS )
set ( files )
foreach ( file ${ ARGS_PUBLIC_SOURECS } )
list ( APPEND files "${module_directory}/${file}" )
endforeach ( )
target_sources ( ${ target_library_name } PUBLIC ${ files } )
2025-11-14 16:04:37 +03:30
endif ( )
2025-07-20 04:46:15 +03:30
2025-11-14 16:04:37 +03:30
if ( ARGS_INTERFACES )
set ( files )
foreach ( file ${ ARGS_INTERFACES } )
list ( APPEND files "${module_directory}/${file}" )
endforeach ( )
target_sources (
2025-11-30 09:46:48 +03:30
$ { t a r g e t _ l i b r a r y _ n a m e } P U B L I C F I L E _ S E T p u b l i c _ c x x _ m o d u l e s T Y P E C X X _ M O D U L E S
F I L E S $ { f i l e s }
2025-11-14 16:04:37 +03:30
)
endif ( )
2025-07-10 13:29:03 +03:30
2025-11-15 20:59:52 +03:30
target_link_libraries ( ${ target_library_name } PUBLIC ${ ARGS_DEPENDENCIES } )
2025-11-30 09:46:48 +03:30
target_link_libraries ( ${ target_library_name } PRIVATE ${ ARGS_PRIVATE_DEPENDENCIES } )
2025-11-15 20:59:52 +03:30
if ( ARGS_TESTS )
message ( "ADDING TESTS!!!" )
set ( test_files )
foreach ( test_file ${ ARGS_TESTS } )
list ( APPEND test_files "${module_directory}/${test_file}" )
2025-11-14 16:04:37 +03:30
endforeach ( )
2025-11-15 20:59:52 +03:30
add_executable ( "${target_library_name}_tests" ${ test_files } )
target_link_libraries (
" $ { t a r g e t _ l i b r a r y _ n a m e } _ t e s t s "
P R I V A T E $ { t a r g e t _ l i b _ n a m e }
#
t e s t
2025-11-14 16:04:37 +03:30
)
2025-10-16 14:12:26 +03:30
endif ( )
2025-10-05 10:07:48 +03:30
2025-11-15 20:59:52 +03:30
if ( ARGS_ENTRYPOINT )
2025-11-30 09:46:48 +03:30
target_link_libraries ( ${ target_executable_name } PRIVATE ${ target_library_name } )
2025-11-15 20:59:52 +03:30
endif ( )
2025-10-16 14:12:26 +03:30
endfunction ( )
2025-07-05 13:28:41 +03:30
2025-10-16 14:12:26 +03:30
function ( add_executable_module exename )
2025-07-05 13:28:41 +03:30
set ( source_files )
2025-07-20 04:46:15 +03:30
set ( source_directory "${CMAKE_CURRENT_SOURCE_DIR}/private" )
2025-10-16 14:12:26 +03:30
foreach ( source_file ${ ARGN } )
2025-07-05 13:28:41 +03:30
list ( APPEND source_files "${source_directory}/${source_file}" )
2025-10-16 14:12:26 +03:30
endforeach ( )
2025-07-05 13:28:41 +03:30
message ( "Adding executable ${exename} with source files: ${source_files}" )
2025-07-20 04:46:15 +03:30
set ( PUBLIC_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/public_includes" )
file ( MAKE_DIRECTORY "${PUBLIC_INCLUDE_DIR}" )
2025-10-16 14:12:26 +03:30
file ( CREATE_LINK "${CMAKE_CURRENT_SOURCE_DIR}/public/"
2025-11-14 16:04:37 +03:30
" $ { P U B L I C _ I N C L U D E _ D I R } / $ { e x e n a m e } " S Y M B O L I C
)
2025-07-20 04:46:15 +03:30
set ( PRIVATE_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/private_includes" )
file ( MAKE_DIRECTORY "${PRIVATE_INCLUDE_DIR}" )
2025-10-16 14:12:26 +03:30
file ( CREATE_LINK "${CMAKE_CURRENT_SOURCE_DIR}/private/"
2025-11-14 16:04:37 +03:30
" $ { P R I V A T E _ I N C L U D E _ D I R } $ { e x e n a m e } " S Y M B O L I C
)
2025-07-20 04:46:15 +03:30
2025-10-16 14:12:26 +03:30
endfunction ( )
2025-07-16 10:52:49 +03:30
2025-10-16 14:12:26 +03:30
function ( add_test_module target_lib_name )
2025-11-14 16:04:37 +03:30
# if(NOT ${ENABLE_UNIT_TESTS}) return() endif()
add_executable ( ${ target_lib_name } _tests ${ ARGN } )
target_link_libraries (
$ { t a r g e t _ l i b _ n a m e } _ t e s t s
P R I V A T E $ { t a r g e t _ l i b _ n a m e }
#
t e s t
)
return ( )
2025-07-16 10:36:07 +03:30
set ( source_files )
2025-07-20 04:46:15 +03:30
set ( source_directory "${CMAKE_CURRENT_SOURCE_DIR}/private" )
2025-10-16 14:12:26 +03:30
foreach ( source_file ${ ARGN } )
2025-07-16 10:36:07 +03:30
list ( APPEND source_files "${source_directory}/${source_file}" )
2025-10-16 14:12:26 +03:30
endforeach ( )
2025-07-16 10:36:07 +03:30
2025-10-16 14:12:26 +03:30
message (
" A d d i n g t e s t e x e c u t a b l e $ { t a r g e t _ l i b _ n a m e } _ t e s t s w i t h s o u r c e f i l e s : $ { s o u r c e _ f i l e s } "
)
2025-07-20 04:46:15 +03:30
set ( PUBLIC_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/public_includes" )
file ( MAKE_DIRECTORY "${PUBLIC_INCLUDE_DIR}" )
2025-10-16 14:12:26 +03:30
file ( CREATE_LINK "${CMAKE_CURRENT_SOURCE_DIR}/public/"
2025-11-14 16:04:37 +03:30
" $ { P U B L I C _ I N C L U D E _ D I R } / $ { t a r g e t _ l i b _ n a m e } " S Y M B O L I C
)
2025-07-20 04:46:15 +03:30
set ( PRIVATE_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/private_includes" )
file ( MAKE_DIRECTORY "${PRIVATE_INCLUDE_DIR}" )
2025-10-16 14:12:26 +03:30
file ( CREATE_LINK "${CMAKE_CURRENT_SOURCE_DIR}/private/"
2025-11-14 16:04:37 +03:30
" $ { P R I V A T E _ I N C L U D E _ D I R } / $ { t a r g e t _ l i b _ n a m e } " S Y M B O L I C
)
2025-07-20 04:46:15 +03:30
add_executable ( ${ target_lib_name } _tests ${ source_files } )
2025-11-14 16:04:37 +03:30
target_link_libraries (
$ { t a r g e t _ l i b _ n a m e } _ t e s t s P R I V A T E $ { t a r g e t _ l i b _ n a m e } s t d t e s t
)
2025-10-16 14:12:26 +03:30
target_include_directories (
$ { t a r g e t _ l i b _ n a m e } _ t e s t s
2025-07-25 15:07:43 +03:30
P R I V A T E $ { P U B L I C _ I N C L U D E _ D I R }
2025-11-14 16:04:37 +03:30
P R I V A T E $ { P R I V A T E _ I N C L U D E _ D I R }
)
2025-10-16 14:12:26 +03:30
endfunction ( )
2025-07-11 14:25:09 +03:30
2025-10-16 14:12:26 +03:30
function ( add_fuzz_module target_lib_name )
if ( NOT ${ ENABLE_FUZZ_TESTS } )
2025-07-30 23:02:53 +03:30
return ( )
2025-10-16 14:12:26 +03:30
endif ( )
2025-07-30 23:02:53 +03:30
set ( source_files )
set ( source_directory "${CMAKE_CURRENT_SOURCE_DIR}/private" )
2025-10-16 14:12:26 +03:30
foreach ( source_file ${ ARGN } )
2025-07-30 23:02:53 +03:30
list ( APPEND source_files "${source_directory}/${source_file}" )
2025-10-16 14:12:26 +03:30
endforeach ( )
2025-07-30 23:02:53 +03:30
2025-10-16 14:12:26 +03:30
message (
" A d d i n g f u z z e x e c u t a b l e $ { t a r g e t _ l i b _ n a m e } _ f u z z w i t h s o u r c e f i l e s : $ { s o u r c e _ f i l e s } "
)
2025-07-30 23:02:53 +03:30
set ( PUBLIC_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/public_includes" )
file ( MAKE_DIRECTORY "${PUBLIC_INCLUDE_DIR}" )
2025-10-16 14:12:26 +03:30
file ( CREATE_LINK "${CMAKE_CURRENT_SOURCE_DIR}/public/"
2025-11-14 16:04:37 +03:30
" $ { P U B L I C _ I N C L U D E _ D I R } / $ { t a r g e t _ l i b _ n a m e } " S Y M B O L I C
)
2025-07-30 23:02:53 +03:30
set ( PRIVATE_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/private_includes" )
file ( MAKE_DIRECTORY "${PRIVATE_INCLUDE_DIR}" )
2025-10-16 14:12:26 +03:30
file ( CREATE_LINK "${CMAKE_CURRENT_SOURCE_DIR}/private/"
2025-11-14 16:04:37 +03:30
" $ { P R I V A T E _ I N C L U D E _ D I R } / $ { t a r g e t _ l i b _ n a m e } " S Y M B O L I C
)
2025-07-30 23:02:53 +03:30
add_executable ( ${ target_lib_name } _fuzz ${ source_files } )
2025-11-14 16:04:37 +03:30
target_link_libraries (
$ { t a r g e t _ l i b _ n a m e } _ f u z z P R I V A T E $ { t a r g e t _ l i b _ n a m e } s t d f u z z _ t e s t
)
2025-07-30 23:02:53 +03:30
target_link_options ( ${ target_lib_name } _fuzz PRIVATE -fsanitize=fuzzer )
target_compile_options ( ${ target_lib_name } _fuzz PRIVATE -fsanitize=fuzzer )
2025-10-16 14:12:26 +03:30
target_include_directories (
$ { t a r g e t _ l i b _ n a m e } _ f u z z
2025-07-30 23:02:53 +03:30
P R I V A T E $ { P U B L I C _ I N C L U D E _ D I R }
2025-11-14 16:04:37 +03:30
P R I V A T E $ { P R I V A T E _ I N C L U D E _ D I R }
)
2025-10-16 14:12:26 +03:30
endfunction ( )
2025-07-30 23:02:53 +03:30
2025-10-16 14:12:26 +03:30
function ( add_option option help )
2025-07-11 14:25:09 +03:30
option ( ${ option } ${ help } )
2025-10-16 14:12:26 +03:30
if ( ${ option } )
2025-07-11 14:25:09 +03:30
message ( STATUS "${option}: ON" )
add_compile_definitions ( ${ option } =1 )
2025-10-16 14:12:26 +03:30
else ( )
2025-07-11 14:25:09 +03:30
message ( STATUS "${option}: OFF" )
2025-10-16 14:12:26 +03:30
endif ( )
endfunction ( )