From ff566e348ae35817f9293ffe80494a382a31e563 Mon Sep 17 00:00:00 2001 From: Gabriel Ferreira Date: Fri, 15 Dec 2023 22:47:36 -0300 Subject: [PATCH] build: raise error on missing or multiple main function in a scratch target --- CHANGES.md | 1 + scratch/CMakeLists.txt | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index d0e62f3e7..32a40a76c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/scratch/CMakeLists.txt b/scratch/CMakeLists.txt index 093e79d7b..adeeb2cf8 100644 --- a/scratch/CMakeLists.txt +++ b/scratch/CMakeLists.txt @@ -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