Skip to content

Commit

Permalink
build: raise error on missing or multiple main function in a scratch …
Browse files Browse the repository at this point in the history
…target
  • Loading branch information
Gabrielcarvfer committed Dec 16, 2023
1 parent 3be56ea commit ff566e3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 16 additions & 6 deletions scratch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}/**
Expand All @@ -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
Expand Down

0 comments on commit ff566e3

Please sign in to comment.