diff --git a/build-support/custom-modules/ns3-module-macros.cmake b/build-support/custom-modules/ns3-module-macros.cmake index ec9f00523..393041939 100644 --- a/build-support/custom-modules/ns3-module-macros.cmake +++ b/build-support/custom-modules/ns3-module-macros.cmake @@ -237,6 +237,26 @@ function(build_lib) ) endif() + # Build lib examples if requested + set(examples_before ${ns3-execs-clean}) + foreach(example_folder example;examples) + if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${example_folder}) + if(${ENABLE_EXAMPLES}) + if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${example_folder}/CMakeLists.txt) + add_subdirectory(${example_folder}) + endif() + endif() + scan_python_examples(${CMAKE_CURRENT_SOURCE_DIR}/${example_folder}) + endif() + endforeach() + set(module_examples ${ns3-execs-clean}) + + # Filter only module examples + foreach(example ${examples_before}) + list(REMOVE_ITEM module_examples ${example}) + endforeach() + unset(examples_before) + # Check if the module tests should be built set(filtered_in ON) if(NS3_FILTER_MODULE_EXAMPLES_AND_TESTS) @@ -291,21 +311,24 @@ function(build_lib) if(${PRECOMPILE_HEADERS_ENABLED} AND (NOT ${BLIB_IGNORE_PCH})) target_precompile_headers(${test${BLIB_LIBNAME}} REUSE_FROM stdlib_pch) endif() + + # Add dependency between tests and examples used as tests + if(${ENABLE_EXAMPLES}) + foreach(source_file ${BLIB_TEST_SOURCES}) + file(READ ${source_file} source_file_contents) + foreach(example_as_test ${module_examples}) + string(FIND "${source_file_contents}" "${example_as_test}" + is_sub_string + ) + if(NOT (${is_sub_string} EQUAL -1)) + add_dependencies(test-runner-examples-as-tests ${example_as_test}) + endif() + endforeach() + endforeach() + endif() endif() endif() - # Build lib examples if requested - foreach(example_folder example;examples) - if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${example_folder}) - if(${ENABLE_EXAMPLES}) - if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${example_folder}/CMakeLists.txt) - add_subdirectory(${example_folder}) - endif() - endif() - scan_python_examples(${CMAKE_CURRENT_SOURCE_DIR}/${example_folder}) - endif() - endforeach() - # Handle package export install( TARGETS ${lib${BLIB_LIBNAME}} diff --git a/build-support/macros-and-definitions.cmake b/build-support/macros-and-definitions.cmake index 9fbce1723..d92cd5515 100644 --- a/build-support/macros-and-definitions.cmake +++ b/build-support/macros-and-definitions.cmake @@ -293,6 +293,7 @@ macro(clear_global_cached_variables) unset(ns3-contrib-libs CACHE) unset(ns3-example-folders CACHE) unset(ns3-execs CACHE) + unset(ns3-execs-clean CACHE) unset(ns3-execs-py CACHE) unset(ns3-external-libs CACHE) unset(ns3-headers-to-module-map CACHE) @@ -306,6 +307,7 @@ macro(clear_global_cached_variables) ns3-contrib-libs ns3-example-folders ns3-execs + ns3-execs-clean ns3-execs-py ns3-external-libs ns3-headers-to-module-map @@ -888,6 +890,7 @@ macro(process_options) endif() if(${ENABLE_TESTS}) + add_custom_target(test-runner-examples-as-tests) add_custom_target(all-test-targets) # Create a custom target to run test.py --no-build Target is also used to @@ -1382,6 +1385,10 @@ function(set_runtime_outputdirectory target_name output_directory target_prefix) set(ns3-execs "${output_directory}${ns3-exec-outputname};${ns3-execs}" CACHE INTERNAL "list of c++ executables" ) + set(ns3-execs-clean "${target_prefix}${target_name};${ns3-execs-clean}" + CACHE INTERNAL + "list of c++ executables without version prefix and build suffix" + ) set_target_properties( ${target_prefix}${target_name} diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 7f9a5ab0c..1e32530cc 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -30,6 +30,7 @@ if(${ENABLE_TESTS} AND (test IN_LIST libs_to_build)) set_runtime_outputdirectory( test-runner ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/utils/ "" ) + add_dependencies(test-runner test-runner-examples-as-tests) add_dependencies(all-test-targets test-runner) endif()