build: raise error on missing or multiple main function in a scratch target

Gabriel Ferreira 2023-12-15 22:47:36 -03:00
parent 3be56ea139
commit ff566e348a
2 changed files with 17 additions and 6 deletions

View File

@ -44,6 +44,7 @@ Changes from ns-3.40 to ns-3-dev
* The `restrict` warning has been disabled in GCC versions 12.1-12.3.1.
* Raised minimum CMake version to 3.13.
* Raised minimum C++ version to C++20.
* Added guard rails for scratch targets missing or containing more than one `main` function.
### Changed behavior

View File

@ -14,14 +14,24 @@ function(create_scratch source_files)
file(READ ${source_file} source_file_contents)
string(REGEX MATCHALL "main[(| (]" main_position "${source_file_contents}")
if(CMAKE_MATCH_0)
set(scratch_src ${source_file})
list(APPEND scratch_src ${source_file})
endif()
endforeach()
if(NOT scratch_src)
return()
list(LENGTH scratch_src scratch_src_len)
# If there is no main function, raise an error
if(${scratch_src_len} EQUAL 0)
message(FATAL_ERROR "The following scratch source files do not contain a main function: ${source_files}")
endif()
# If there are multiple main functions, raise an error
if(${scratch_src_len} GREATER 1)
message(FATAL_ERROR "The following scratch source files contain ${scratch_src_len} files with a main function: ${scratch_src}")
endif()
# If there is a single main function, continue normally
# Get parent directory name
get_filename_component(scratch_dirname ${scratch_src} DIRECTORY)
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}" "" scratch_dirname
@ -69,10 +79,9 @@ foreach(scratch_src ${single_source_file_scratches})
create_scratch(${scratch_src})
endforeach()
# Scan *.cc files in ns-3-dev/scratch subdirectories and build a target for each
# subdirectory
# Scan ns-3-dev/scratch subdirectories
file(
GLOB_RECURSE scratch_subdirectories
GLOB scratch_subdirectories
CONFIGURE_DEPENDS
LIST_DIRECTORIES true
${CMAKE_CURRENT_SOURCE_DIR}/**
@ -84,6 +93,7 @@ foreach(entry ${scratch_subdirectories})
endif()
endforeach()
# Build scratches per directory or following CMakeLists.txt instructions
foreach(subdir ${scratch_subdirectories})
if(EXISTS ${subdir}/CMakeLists.txt)
# If the subdirectory contains a CMakeLists.txt file