build: ns3 and CMake fixes

- fix scratch shortcut transformation to build target and binary
- scan scratch files for a main function to use them as the target name
- fix CMake generator parsing
- update the -- separator message
- use -- separator to forward CMake flags from ns3
- add --vis option to ns3
- embedded version fixes
    Fix NS3_VERSION_PATCH and NS3_VERSION_RELEASE_CANDIDATE default values.
    Add check-version target to CMake.
    Add --enable-build-version to ns3.
    Add --check-profile and --check-version options to ns3.
- process each scratch subdirectory as a single target
- forward ns3 arguments after -- separator to the program to run
- fix escape sequence in command-template help string
- handle modules with very long names
This commit is contained in:
Gabriel Ferreira
2022-01-10 22:36:50 -03:00
parent 2db1e3ed8d
commit 70d11fe983
7 changed files with 399 additions and 115 deletions

View File

@@ -1,36 +1,78 @@
file(GLOB_RECURSE scratches ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)
set(DONT_BUILD)
set(target_prefix scratch_)
foreach(scratch_src ${scratches})
# Get source filename without path or extension
get_filename_component(scratch_name ${scratch_src} NAME)
string(REGEX REPLACE "\\.[^.]*$" "" scratch_name ${scratch_name})
function(create_scratch source_files)
# Return early if no sources in the subdirectory
list(LENGTH source_files number_sources)
if(number_sources EQUAL 0)
return()
endif()
# If the scratch has more than a source file, we need
# to find the source with the main function
unset(scratch_src)
foreach(source_file ${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})
endif()
endforeach()
# Get parent directory name
get_filename_component(scratch_dirname ${scratch_src} DIRECTORY)
string(REPLACE "${CMAKE_CURRENT_SOURCE_DIR}/" "" scratch_dirname
${scratch_dirname}
)
# Get source name
get_filename_component(scratch_name ${scratch_src} NAME_WE)
set(target_prefix scratch_)
if(${scratch_dirname})
# Join the names together if dirname is not the scratch folder
set(target_prefix scratch_${scratch_dirname}_)
endif()
# Get source absolute path and transform into relative path
get_filename_component(scratch_absolute_directory ${scratch_src} DIRECTORY)
string(REPLACE "${PROJECT_SOURCE_DIR}" "${CMAKE_OUTPUT_DIRECTORY}"
scratch_directory ${scratch_absolute_directory}
)
# Build scratch if not listed as a DONT_BUILD
string(FIND "${DONT_BUILD}" "${scratch_name}" res)
if(res LESS 0)
add_executable(${target_prefix}${scratch_name} "${scratch_src}")
if(${NS3_STATIC})
target_link_libraries(
${target_prefix}${scratch_name} ${LIB_AS_NEEDED_PRE_STATIC}
${lib-ns3-static}
)
else()
target_link_libraries(
${target_prefix}${scratch_name} "${ns3-libs}" "${ns3-contrib-libs}"
"${ns3-external-libs}"
)
endif()
set_runtime_outputdirectory(
${scratch_name} ${scratch_directory}/ ${target_prefix}
add_executable(${target_prefix}${scratch_name} "${source_files}")
if(${NS3_STATIC})
target_link_libraries(
${target_prefix}${scratch_name} ${LIB_AS_NEEDED_PRE_STATIC}
${lib-ns3-static}
)
else()
target_link_libraries(
${target_prefix}${scratch_name} "${ns3-libs}" "${ns3-contrib-libs}"
"${ns3-external-libs}"
)
endif()
set_runtime_outputdirectory(
${scratch_name} ${scratch_directory}/ ${target_prefix}
)
endfunction()
# Scan *.cc files in ns-3-dev/scratch and build a target for each
file(GLOB single_source_file_scratches ${CMAKE_CURRENT_SOURCE_DIR}/*.cc)
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
file(GLOB_RECURSE scratch_subdirectories LIST_DIRECTORIES true
${CMAKE_CURRENT_SOURCE_DIR}/**
)
# Filter out files
foreach(entry ${scratch_subdirectories})
if(NOT (IS_DIRECTORY ${entry}))
list(REMOVE_ITEM scratch_subdirectories ${entry})
endif()
endforeach()
foreach(subdir ${scratch_subdirectories})
file(GLOB scratch_sources ${subdir}/*.cc)
create_scratch("${scratch_sources}")
endforeach()