build: Add new build_exec macro to declare executable targets

Also adds IGNORE_PCH option to build_example macro.
This commit is contained in:
Gabriel Ferreira
2022-09-03 02:07:36 -03:00
parent 096b8d428c
commit c633a1420c
11 changed files with 239 additions and 145 deletions

View File

@@ -49,6 +49,7 @@ Changes from ns-3.36 to ns-3.37
* Replaced `./ns3 --check-config` with `./ns3 show config`.
* Replaced `./ns3 --check-profile` with `./ns3 show profile`.
* Replaced `./ns3 --check-version` with `./ns3 show version`.
* Added the `build_exec` macro to declare new executables.
### Changed behavior

View File

@@ -1,4 +1,16 @@
additional_commands:
build_exec:
flags: [IGNORE_PCH]
kwargs:
EXECNAME : '1'
EXECNAME_PREFIX : '1'
EXECUTABLE_DIRECTORY_PATH : '1'
INSTALL_DIRECTORY_PATH : '1'
SOURCE_FILES : '*'
HEADER_FILES : '*'
LIBRARIES_TO_LINK : '*'
DEFINITIONS : '*'
build_lib:
flags: [IGNORE_PCH]
kwargs:
@@ -19,6 +31,7 @@ additional_commands:
LIBRARIES_TO_LINK : '*'
build_example:
flags: [IGNORE_PCH]
kwargs:
NAME : '1'
SOURCE_FILES : '*'

View File

@@ -1,3 +1,53 @@
additional_commands:
build_exec:
flags: [IGNORE_PCH]
kwargs:
EXECNAME : '1'
EXECNAME_PREFIX : '1'
EXECUTABLE_DIRECTORY_PATH : '1'
INSTALL_DIRECTORY_PATH : '1'
SOURCE_FILES : '*'
HEADER_FILES : '*'
LIBRARIES_TO_LINK : '*'
DEFINITIONS : '*'
build_lib:
flags: [IGNORE_PCH]
kwargs:
LIBNAME : '1'
SOURCE_FILES : '*'
HEADER_FILES : '*'
LIBRARIES_TO_LINK : '*'
TEST_SOURCES : '*'
DEPRECATED_HEADER_FILES : '*'
MODULE_ENABLED_FEATURES : '*'
build_lib_example:
flags: [IGNORE_PCH]
kwargs:
NAME : '1'
SOURCE_FILES : '*'
HEADER_FILES : '*'
LIBRARIES_TO_LINK : '*'
build_example:
flags: [IGNORE_PCH]
kwargs:
NAME : '1'
SOURCE_FILES : '*'
HEADER_FILES : '*'
LIBRARIES_TO_LINK : '*'
find_external_library:
kwargs:
DEPENDENCY_NAME : '1'
HEADER_NAME : '1'
LIBRARY_NAME : '1'
HEADER_NAMES : '*'
LIBRARY_NAMES : '*'
PATH_SUFFIXES : '*'
SEARCH_PATHS : '*'
format:
tab_size: 2
line_width: 80

View File

@@ -50,10 +50,9 @@ if(${MISSING_OSTREAM_NULLPTR_OPERATOR})
endif()
# Some compilers (e.g. GCC < 9.1 and Clang < 9) do not link
# std::filesystem/std::experimental::filesystem by default.
# If the sample program can be linked, it means it is
# indeed linked by default. Otherwise, we link it manually.
# https://en.cppreference.com/w/cpp/filesystem
# std::filesystem/std::experimental::filesystem by default. If the sample
# program can be linked, it means it is indeed linked by default. Otherwise, we
# link it manually. https://en.cppreference.com/w/cpp/filesystem
check_cxx_source_compiles(
"
# ifdef __cpp_lib_filesystem

View File

@@ -315,36 +315,23 @@ function(build_lib_example)
missing_dependencies "${BLIB_EXAMPLE_LIBRARIES_TO_LINK}"
)
if(NOT missing_dependencies)
# Create shared library with sources and headers
add_executable(
"${BLIB_EXAMPLE_NAME}" ${BLIB_EXAMPLE_SOURCE_FILES}
${BLIB_EXAMPLE_HEADER_FILES}
)
if(${NS3_STATIC})
target_link_libraries(
${BLIB_EXAMPLE_NAME} ${LIB_AS_NEEDED_PRE_STATIC} ${lib-ns3-static}
)
elseif(${NS3_MONOLIB})
target_link_libraries(
${BLIB_EXAMPLE_NAME} ${LIB_AS_NEEDED_PRE} ${lib-ns3-monolib}
${LIB_AS_NEEDED_POST}
)
else()
target_link_libraries(
${BLIB_EXAMPLE_NAME} ${LIB_AS_NEEDED_PRE} ${lib${BLIB_EXAMPLE_LIBNAME}}
${BLIB_EXAMPLE_LIBRARIES_TO_LINK} ${optional_visualizer_lib}
${LIB_AS_NEEDED_POST}
)
# Convert boolean into text to forward argument
if(${BLIB_EXAMPLE_IGNORE_PCH})
set(IGNORE_PCH IGNORE_PCH)
endif()
if(${PRECOMPILE_HEADERS_ENABLED} AND (NOT ${BLIB_EXAMPLE_IGNORE_PCH}))
target_precompile_headers(${BLIB_EXAMPLE_NAME} REUSE_FROM stdlib_pch_exec)
endif()
set_runtime_outputdirectory(
${BLIB_EXAMPLE_NAME} ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${FOLDER}/ ""
# Create executable with sources and headers
# cmake-format: off
build_exec(
EXECNAME ${BLIB_EXAMPLE_NAME}
SOURCE_FILES ${BLIB_EXAMPLE_SOURCE_FILES}
HEADER_FILES ${BLIB_EXAMPLE_HEADER_FILES}
LIBRARIES_TO_LINK
${lib${BLIB_EXAMPLE_LIBNAME}} ${BLIB_EXAMPLE_LIBRARIES_TO_LINK}
${optional_visualizer_lib}
EXECUTABLE_DIRECTORY_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${FOLDER}/
${IGNORE_PCH}
)
# cmake-format: on
endif()
endfunction()

View File

@@ -942,10 +942,10 @@ macro(process_options)
# works on CMake 3.18 or newer > COMMAND ${CMAKE_COMMAND} -E cat
# ${PROJECT_SOURCE_DIR}/testpy-output/*.command-line >
# ${PROJECT_SOURCE_DIR}/doc/introspected-command-line.h
COMMAND ${cat_command}
${CMAKE_BINARY_DIR}/introspected-command-line-preamble.h
${PROJECT_SOURCE_DIR}/testpy-output/*.command-line
> ${PROJECT_SOURCE_DIR}/doc/introspected-command-line.h 2> NULL
COMMAND
${cat_command} ${CMAKE_BINARY_DIR}/introspected-command-line-preamble.h
${PROJECT_SOURCE_DIR}/testpy-output/*.command-line >
${PROJECT_SOURCE_DIR}/doc/introspected-command-line.h 2> NULL
DEPENDS run-introspected-command-line
)
@@ -1260,6 +1260,10 @@ macro(process_options)
endmacro()
function(set_runtime_outputdirectory target_name output_directory target_prefix)
# Prevent duplicate '/' in EXECUTABLE_DIRECTORY_PATH, since it gets translated
# to doubled underlines and will cause the ns3 script to fail
string(REPLACE "//" "/" output_directory "${output_directory}")
set(ns3-exec-outputname ns${NS3_VER}-${target_name}${build_profile_suffix})
set(ns3-execs "${output_directory}${ns3-exec-outputname};${ns3-execs}"
CACHE INTERNAL "list of c++ executables"
@@ -1309,6 +1313,73 @@ function(set_runtime_outputdirectory target_name output_directory target_prefix)
endif()
endfunction(set_runtime_outputdirectory)
function(build_exec)
# Argument parsing
set(options IGNORE_PCH)
set(oneValueArgs EXECNAME EXECNAME_PREFIX EXECUTABLE_DIRECTORY_PATH
INSTALL_DIRECTORY_PATH
)
set(multiValueArgs SOURCE_FILES HEADER_FILES LIBRARIES_TO_LINK DEFINITIONS)
cmake_parse_arguments(
"BEXEC" "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}
)
add_executable(
${BEXEC_EXECNAME_PREFIX}${BEXEC_EXECNAME} "${BEXEC_SOURCE_FILES}"
)
target_compile_definitions(
${BEXEC_EXECNAME_PREFIX}${BEXEC_EXECNAME} PUBLIC ${BEXEC_DEFINITIONS}
)
if(${PRECOMPILE_HEADERS_ENABLED} AND (NOT ${BEXEC_IGNORE_PCH}))
target_precompile_headers(
${BEXEC_EXECNAME_PREFIX}${BEXEC_EXECNAME} REUSE_FROM stdlib_pch_exec
)
endif()
if(${NS3_STATIC})
target_link_libraries(
${BEXEC_EXECNAME_PREFIX}${BEXEC_EXECNAME} ${LIB_AS_NEEDED_PRE_STATIC}
${lib-ns3-static}
)
elseif(${NS3_MONOLIB})
target_link_libraries(
${BEXEC_EXECNAME_PREFIX}${BEXEC_EXECNAME} ${LIB_AS_NEEDED_PRE}
${lib-ns3-monolib} ${LIB_AS_NEEDED_POST}
)
else()
target_link_libraries(
${BEXEC_EXECNAME_PREFIX}${BEXEC_EXECNAME} ${LIB_AS_NEEDED_PRE}
"${BEXEC_LIBRARIES_TO_LINK}" ${LIB_AS_NEEDED_POST}
)
endif()
set_runtime_outputdirectory(
"${BEXEC_EXECNAME}" "${BEXEC_EXECUTABLE_DIRECTORY_PATH}/"
"${BEXEC_EXECNAME_PREFIX}"
)
if(BEXEC_INSTALL_DIRECTORY_PATH)
install(TARGETS ${BEXEC_EXECNAME_PREFIX}${BEXEC_EXECNAME}
EXPORT ns3ExportTargets
RUNTIME DESTINATION ${BEXEC_INSTALL_DIRECTORY_PATH}
)
get_property(
filename TARGET ${BEXEC_EXECNAME_PREFIX}${BEXEC_EXECNAME}
PROPERTY RUNTIME_OUTPUT_NAME
)
add_custom_target(
uninstall_${BEXEC_EXECNAME_PREFIX}${BEXEC_EXECNAME}
COMMAND
rm ${CMAKE_INSTALL_PREFIX}/${BEXEC_INSTALL_DIRECTORY_PATH}/${filename}
)
add_dependencies(
uninstall uninstall_${BEXEC_EXECNAME_PREFIX}${BEXEC_EXECNAME}
)
endif()
endfunction(build_exec)
function(scan_python_examples path)
# Skip python examples search in case the bindings are disabled
if(NOT ${ENABLE_PYTHON_BINDINGS})
@@ -1428,7 +1499,7 @@ include(build-support/custom-modules/ns3-contributions.cmake)
# Macro to build examples in ns-3-dev/examples/
macro(build_example)
set(options)
set(options IGNORE_PCH)
set(oneValueArgs NAME)
set(multiValueArgs SOURCE_FILES HEADER_FILES LIBRARIES_TO_LINK)
cmake_parse_arguments(
@@ -1440,36 +1511,22 @@ macro(build_example)
)
if(NOT missing_dependencies)
# Create shared library with sources and headers
add_executable(
${EXAMPLE_NAME} "${EXAMPLE_SOURCE_FILES}" "${EXAMPLE_HEADER_FILES}"
)
if(${NS3_STATIC})
target_link_libraries(
${EXAMPLE_NAME} ${LIB_AS_NEEDED_PRE_STATIC} ${lib-ns3-static}
)
elseif(${NS3_MONOLIB})
target_link_libraries(
${EXAMPLE_NAME} ${LIB_AS_NEEDED_PRE} ${lib-ns3-monolib}
${LIB_AS_NEEDED_POST}
)
else()
# Link the shared library with the libraries passed
target_link_libraries(
${EXAMPLE_NAME} ${LIB_AS_NEEDED_PRE} ${EXAMPLE_LIBRARIES_TO_LINK}
${optional_visualizer_lib} ${LIB_AS_NEEDED_POST}
)
# Convert boolean into text to forward argument
if(${EXAMPLE_IGNORE_PCH})
set(IGNORE_PCH IGNORE_PCH)
endif()
if(${PRECOMPILE_HEADERS_ENABLED})
target_precompile_headers(${EXAMPLE_NAME} REUSE_FROM stdlib_pch_exec)
endif()
set_runtime_outputdirectory(
${EXAMPLE_NAME}
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/examples/${examplefolder}/ ""
# Create example library with sources and headers
# cmake-format: off
build_exec(
EXECNAME ${EXAMPLE_NAME}
SOURCE_FILES ${EXAMPLE_SOURCE_FILES}
HEADER_FILES ${EXAMPLE_HEADER_FILES}
LIBRARIES_TO_LINK ${EXAMPLE_LIBRARIES_TO_LINK} ${optional_visualizer_lib}
EXECUTABLE_DIRECTORY_PATH
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/examples/${examplefolder}/
${IGNORE_PCH}
)
# cmake-format: on
endif()
endmacro()

View File

@@ -44,20 +44,12 @@ function(create_scratch source_files)
string(REPLACE "${PROJECT_SOURCE_DIR}" "${CMAKE_OUTPUT_DIRECTORY}"
scratch_directory ${scratch_absolute_directory}
)
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} ${LIB_AS_NEEDED_PRE} "${ns3-libs}" "${ns3-contrib-libs}"
"${ns3-external-libs}" ${LIB_AS_NEEDED_POST}
)
endif()
set_runtime_outputdirectory(
${scratch_name} ${scratch_directory}/ ${target_prefix}
build_exec(
EXECNAME ${scratch_name}
EXECNAME_PREFIX ${target_prefix}
SOURCE_FILES "${source_files}"
LIBRARIES_TO_LINK "${ns3-libs}" "${ns3-contrib-libs}"
EXECUTABLE_DIRECTORY_PATH ${scratch_directory}/
)
endfunction()

View File

@@ -58,11 +58,8 @@ if(${NS3_STATIC})
endif()
target_link_libraries(
${lib-ns3-static}
${STATIC_LINK_FLAGS}
${LIB_AS_NEEDED_PRE_STATIC}
${ns3-external-static-libs}
${LIB_AS_NEEDED_POST_STATIC}
${lib-ns3-static} ${STATIC_LINK_FLAGS} ${LIB_AS_NEEDED_PRE_STATIC}
${ns3-external-static-libs} ${LIB_AS_NEEDED_POST_STATIC}
)
if(${NS3_CLANG_TIMETRACE})
add_dependencies(timeTraceReport ${lib-ns3-static})

View File

@@ -151,19 +151,18 @@ if(${ENABLE_FDNETDEV})
helper/emu-fd-net-device-helper.h
)
add_executable(
raw-sock-creator
helper/creator-utils.cc
helper/encode-decode.cc
helper/raw-sock-creator.cc
)
add_definitions(
-DRAW_SOCK_CREATOR="${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/fd-net-device/ns${NS3_VER}-raw-sock-creator${build_profile_suffix}"
)
set_runtime_outputdirectory(
raw-sock-creator
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/fd-net-device/
""
build_exec(
EXECNAME raw-sock-creator
SOURCE_FILES
helper/creator-utils.cc
helper/encode-decode.cc
helper/raw-sock-creator.cc
EXECUTABLE_DIRECTORY_PATH
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/fd-net-device/
INSTALL_DIRECTORY_PATH ${CMAKE_INSTALL_LIBEXECDIR}/ns3
)
list(
@@ -186,19 +185,19 @@ if(${ENABLE_FDNETDEV})
helper/tap-fd-net-device-helper.h
)
add_executable(
tap-device-creator
helper/creator-utils.cc
helper/encode-decode.cc
helper/tap-device-creator.cc
)
add_definitions(
-DTAP_DEV_CREATOR="${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/fd-net-device/ns${NS3_VER}-tap-device-creator${build_profile_suffix}"
)
set_runtime_outputdirectory(
tap-device-creator
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/fd-net-device/
""
build_exec(
EXECNAME tap-device-creator
SOURCE_FILES
helper/creator-utils.cc
helper/encode-decode.cc
helper/tap-device-creator.cc
EXECUTABLE_DIRECTORY_PATH
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/fd-net-device/
INSTALL_DIRECTORY_PATH ${CMAKE_INSTALL_LIBEXECDIR}/ns3
)
list(
@@ -223,19 +222,19 @@ if(${ENABLE_FDNETDEV})
model/netmap-net-device.h
)
add_executable(
netmap-device-creator
helper/creator-utils.cc
helper/encode-decode.cc
helper/netmap-device-creator.cc
)
add_definitions(
-DNETMAP_DEV_CREATOR="${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/fd-net-device/ns${NS3_VER}-netmap-device-creator${build_profile_suffix}"
)
set_runtime_outputdirectory(
netmap-device-creator
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/fd-net-device/
""
build_exec(
EXECNAME netmap-device-creator
SOURCE_FILES
helper/creator-utils.cc
helper/encode-decode.cc
helper/netmap-device-creator.cc
EXECUTABLE_DIRECTORY_PATH
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/fd-net-device/
INSTALL_DIRECTORY_PATH ${CMAKE_INSTALL_LIBEXECDIR}/ns3
)
list(

View File

@@ -37,13 +37,10 @@ build_lib(
add_definitions(
-DTAP_CREATOR="${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/tap-bridge/ns${NS3_VER}-tap-creator${build_profile_suffix}"
)
add_executable(
tap-creator
model/tap-creator.cc
model/tap-encode-decode.cc
)
set_runtime_outputdirectory(
tap-creator
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/tap-bridge/
""
build_exec(
EXECNAME tap-creator
SOURCE_FILES model/tap-creator.cc
model/tap-encode-decode.cc
EXECUTABLE_DIRECTORY_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/src/tap-bridge/
INSTALL_DIRECTORY_PATH ${CMAKE_INSTALL_LIBEXECDIR}/ns3
)

View File

@@ -18,32 +18,34 @@ if(${ENABLE_TESTS} AND (test IN_LIST libs_to_build))
add_dependencies(all-test-targets test-runner)
endif()
add_executable(bench-scheduler bench-scheduler.cc)
target_link_libraries(bench-scheduler ${libcore})
set_runtime_outputdirectory(
bench-scheduler ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/utils/ ""
)
build_exec(
EXECNAME bench-scheduler
SOURCE_FILES bench-scheduler.cc
LIBRARIES_TO_LINK ${libcore}
EXECUTABLE_DIRECTORY_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/utils/
)
if(network IN_LIST libs_to_build)
add_executable(bench-packets bench-packets.cc)
target_link_libraries(bench-packets ${libnetwork})
set_runtime_outputdirectory(
bench-packets ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/utils/ ""
)
build_exec(
EXECNAME bench-packets
SOURCE_FILES bench-packets.cc
LIBRARIES_TO_LINK ${libnetwork}
EXECUTABLE_DIRECTORY_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/utils/
)
add_executable(print-introspected-doxygen print-introspected-doxygen.cc)
target_link_libraries(
print-introspected-doxygen
PRIVATE ${LIB_AS_NEEDED_PRE} ${ns3-libs} ${ns3-contrib-libs}
${LIB_AS_NEEDED_POST}
)
set_runtime_outputdirectory(
print-introspected-doxygen ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/utils/ ""
)
build_exec(
EXECNAME print-introspected-doxygen
SOURCE_FILES print-introspected-doxygen.cc
LIBRARIES_TO_LINK ${ns3-libs} ${ns3-contrib-libs}
EXECUTABLE_DIRECTORY_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/utils/
)
endif()
if(core IN_LIST ns3-all-enabled-modules)
add_executable(perf-io perf/perf-io.cc)
target_link_libraries(perf-io PRIVATE ${libcore})
set_runtime_outputdirectory(perf-io ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/utils/perf/ "")
build_exec(
EXECNAME perf-io
SOURCE_FILES perf/perf-io.cc
LIBRARIES_TO_LINK ${libcore}
EXECUTABLE_DIRECTORY_PATH ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/utils/perf/
)
endif()