From 69442e97c322e29e23cf3a107a01a4d915e0475c Mon Sep 17 00:00:00 2001 From: Gabriel Ferreira Date: Wed, 14 Feb 2024 23:56:51 +0100 Subject: [PATCH] build: prevent usage of uninitialized variables set inside src modules --- src/CMakeLists.txt | 32 ++++++++++++++++++++++++++++++++ utils/tests/test-ns3.py | 5 ++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d72e049a3..52f65e9f8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,6 +21,38 @@ foreach(libname ${libs}) endif() endforeach() +# Scan for disabled modules that define settings prefixed with NS3_ or ENABLE_ +# so that we can initialize them as turned off (setting as temporary variables +# not to affect the cached variables that are set when normally processed) +subdirlist(modules ${CMAKE_CURRENT_SOURCE_DIR}) +foreach(libname ${modules}) + if(NOT (${libname} IN_LIST libs)) + # Skip module directories without CMakeLists.txt files + if(NOT (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${libname}/CMakeLists.txt)) + continue() + endif() + + file(READ ${CMAKE_CURRENT_SOURCE_DIR}/${libname}/CMakeLists.txt + lib_cmakelists_contents + ) + string(REGEX MATCHALL "(NS3_[a-zA-Z0-9_]*)|(ENABLE_[a-zA-Z0-9_]*)" flags + "${lib_cmakelists_contents}" + ) + foreach(flag ${flags}) + # Skip reason flags + if(${flag} MATCHES "REASON") + continue() + endif() + # Skip flags already defined + if(DEFINED ${flag}) + continue() + endif() + # Set flag to off + set(${flag} OFF PARENT_SCOPE) + endforeach() + endif() +endforeach() + # Prevents link errors due to symbol collisions if the same library is linked # multiple times list(REMOVE_DUPLICATES ns3-external-libs) diff --git a/utils/tests/test-ns3.py b/utils/tests/test-ns3.py index f8692d303..ceaf3e7e8 100755 --- a/utils/tests/test-ns3.py +++ b/utils/tests/test-ns3.py @@ -1064,7 +1064,10 @@ class NS3ConfigureTestCase(NS3BaseTestCase): f.write(ns3rc_template.format(modules="'lte'", examples="False", tests="True")) # Reconfigure. - return_code, stdout, stderr = run_ns3('configure -G "{generator}"') + run_ns3("clean") + return_code, stdout, stderr = run_ns3( + 'configure -G "{generator}" -d release --enable-verbose' + ) self.config_ok(return_code, stdout, stderr) # Check.