From df5fa6bc3bcdd53d7c359dad1f265e2b17ae830a Mon Sep 17 00:00:00 2001 From: Gabriel Ferreira Date: Mon, 30 Jan 2023 15:54:35 -0300 Subject: [PATCH] build,scratch: add a nested scratch directory example Signed-off-by: Gabriel Ferreira --- build-support/macros-and-definitions.cmake | 19 +++++++ scratch/nested-subdir/CMakeLists.txt | 16 ++++++ .../scratch-nested-subdir-library-header.h | 32 ++++++++++++ .../scratch-nested-subdir-library-source.cc | 30 +++++++++++ .../scratch-nested-subdir-executable.cc | 51 +++++++++++++++++++ 5 files changed, 148 insertions(+) create mode 100644 scratch/nested-subdir/CMakeLists.txt create mode 100644 scratch/nested-subdir/lib/scratch-nested-subdir-library-header.h create mode 100644 scratch/nested-subdir/lib/scratch-nested-subdir-library-source.cc create mode 100644 scratch/nested-subdir/scratch-nested-subdir-executable.cc diff --git a/build-support/macros-and-definitions.cmake b/build-support/macros-and-definitions.cmake index 44c2c2af4..c63667056 100644 --- a/build-support/macros-and-definitions.cmake +++ b/build-support/macros-and-definitions.cmake @@ -537,6 +537,7 @@ macro(process_options) else() file(GLOB_RECURSE MODULES_CMAKE_FILES src/**/CMakeLists.txt contrib/**/CMakeLists.txt examples/**/CMakeLists.txt + scratch/**/CMakeLists.txt ) file( GLOB @@ -1517,6 +1518,17 @@ function(set_runtime_outputdirectory target_name output_directory target_prefix) endif() endfunction(set_runtime_outputdirectory) +function(get_scratch_prefix prefix) + # /path/to/ns-3-dev/scratch/nested-subdir + set(temp ${CMAKE_CURRENT_SOURCE_DIR}) + # remove /path/to/ns-3-dev/ to get scratch/nested-subdir + string(REPLACE "${PROJECT_SOURCE_DIR}/" "" temp "${temp}") + # replace path separators with underlines + string(REPLACE "/" "_" temp "${temp}") + # save the prefix value to the passed variable + set(${prefix} ${temp}_ PARENT_SCOPE) +endfunction() + function(build_exec) # Argument parsing set(options IGNORE_PCH STANDALONE) @@ -1528,6 +1540,13 @@ function(build_exec) "BEXEC" "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) + # Resolve nested scratch prefixes without user intervention + if("${CMAKE_CURRENT_SOURCE_DIR}" MATCHES "scratch" + AND "${BEXEC_EXECNAME_PREFIX}" STREQUAL "" + ) + get_scratch_prefix(BEXEC_EXECNAME_PREFIX) + endif() + add_executable( ${BEXEC_EXECNAME_PREFIX}${BEXEC_EXECNAME} "${BEXEC_SOURCE_FILES}" ) diff --git a/scratch/nested-subdir/CMakeLists.txt b/scratch/nested-subdir/CMakeLists.txt new file mode 100644 index 000000000..c3fe137b0 --- /dev/null +++ b/scratch/nested-subdir/CMakeLists.txt @@ -0,0 +1,16 @@ +# Create a custom library +add_library( + scratch-nested-subdir-lib + lib/scratch-nested-subdir-library-source.cc +) + +# Link the custom library to the scratch executable +build_exec( + EXECNAME scratch-nested-subdir-executable + SOURCE_FILES scratch-nested-subdir-executable.cc + LIBRARIES_TO_LINK scratch-nested-subdir-lib + ${libcore} + # use "${ns3-libs}" "${ns3-contrib-libs}" in case you want to link to all + # enabled modules + EXECUTABLE_DIRECTORY_PATH ${CMAKE_OUTPUT_DIRECTORY}/scratch/nested-subdir +) diff --git a/scratch/nested-subdir/lib/scratch-nested-subdir-library-header.h b/scratch/nested-subdir/lib/scratch-nested-subdir-library-header.h new file mode 100644 index 000000000..55a765910 --- /dev/null +++ b/scratch/nested-subdir/lib/scratch-nested-subdir-library-header.h @@ -0,0 +1,32 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +// This header does not provide much functionality but is meant to demonstrate how, +// in a scratch subdirectory, one can create new programs that are implemented +// in multiple files and headers. + +#include + +namespace ns3 +{ + +/** + * Get a message from the subdir. + * + * \return The message from the subdir + */ +std::string ScratchNestedSubdirGetMessage(); + +} // namespace ns3 diff --git a/scratch/nested-subdir/lib/scratch-nested-subdir-library-source.cc b/scratch/nested-subdir/lib/scratch-nested-subdir-library-source.cc new file mode 100644 index 000000000..186fb2cd2 --- /dev/null +++ b/scratch/nested-subdir/lib/scratch-nested-subdir-library-source.cc @@ -0,0 +1,30 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +// This file contains the implementation of the functions declared in the +// corresponding header file. + +#include "scratch-nested-subdir-library-header.h" + +namespace ns3 +{ + +std::string +ScratchNestedSubdirGetMessage() +{ + return "Scratch Nested Subdir"; +} + +} // namespace ns3 diff --git a/scratch/nested-subdir/scratch-nested-subdir-executable.cc b/scratch/nested-subdir/scratch-nested-subdir-executable.cc new file mode 100644 index 000000000..1e258edb3 --- /dev/null +++ b/scratch/nested-subdir/scratch-nested-subdir-executable.cc @@ -0,0 +1,51 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +// This example shows how to create new simulations that are implemented in +// multiple files and headers. The structure of this simulation project +// is as follows: +// +// scratch/ +// | nested-subdir/ +// | | - scratch-nested-subdir-executable.cc // Main simulation file +// | | lib +// | | | - scratch-nested-subdir-library-header.h // Additional header +// | | | - scratch-nested-subdir-library-source.cc // Additional header implementation +// +// This file contains the main() function, which calls an external function +// defined in the "scratch-nested-subdir-library-header.h" header file and +// implemented in "scratch-nested-subdir-library-source.cc". + +#include "lib/scratch-nested-subdir-library-header.h" + +#include "ns3/core-module.h" + +#include + +using namespace ns3; + +NS_LOG_COMPONENT_DEFINE("ScratchNestedSubdir"); + +int +main(int argc, char* argv[]) +{ + std::string message = ScratchNestedSubdirGetMessage(); + NS_LOG_UNCOND(message); + + Simulator::Run(); + Simulator::Destroy(); + + return 0; +}