diff --git a/build-support/custom-modules/ns3-lock.cmake b/build-support/custom-modules/ns3-lock.cmake index 4da50f535..f8f77a3db 100644 --- a/build-support/custom-modules/ns3-lock.cmake +++ b/build-support/custom-modules/ns3-lock.cmake @@ -82,6 +82,9 @@ function(write_lock) string(APPEND lock_contents "APPNAME = 'ns'\n") string(APPEND lock_contents "BUILD_PROFILE = '${build_profile}'\n") string(APPEND lock_contents "VERSION = '${NS3_VER}' \n") + string(APPEND lock_contents + "BUILD_VERSION_STRING = '${BUILD_VERSION_STRING}' \n" + ) string(APPEND lock_contents "PYTHON = ['${Python3_EXECUTABLE}']\n") mark_as_advanced(VALGRIND) diff --git a/build-support/custom-modules/ns3-versioning.cmake b/build-support/custom-modules/ns3-versioning.cmake index 70d997c4e..5a1b6e485 100644 --- a/build-support/custom-modules/ns3-versioning.cmake +++ b/build-support/custom-modules/ns3-versioning.cmake @@ -76,6 +76,7 @@ function(configure_embedded_version) check-version COMMAND echo Build version feature disabled. Reconfigure ns-3 with NS3_ENABLE_BUILD_VERSION=ON ) + set(BUILD_VERSION_STRING PARENT_SCOPE) return() endif() @@ -195,6 +196,7 @@ function(configure_embedded_version) ) string(REPLACE "\"" "" version "${version}") add_custom_target(check-version COMMAND echo ns-3 version: ${version}) + set(BUILD_VERSION_STRING ${version} PARENT_SCOPE) # Enable embedding build version add_definitions(-DENABLE_BUILD_VERSION=1) diff --git a/ns3 b/ns3 index d1f6b7efd..cb8b4a445 100755 --- a/ns3 +++ b/ns3 @@ -263,8 +263,8 @@ def parse_args(argv): parser_show.add_argument('show', help=('Print the current ns-3 build profile type, configuration or version, ' 'or a list of buildable/runnable targets'), - choices=["profile", "version", "config", "targets"], - action="store", type=str, default=None) + choices=["profile", "version", "config", "targets", "all"], + action="store", type=str, nargs="?", default="all") add_argument_to_subparsers( [parser, parser_build, parser_configure, parser_clean, parser_docs, parser_run, parser_show], @@ -382,6 +382,7 @@ def check_lock_data(output_directory): "ENABLE_EXAMPLES": False, "ENABLE_SUDO": False, "ENABLE_TESTS": False, + "BUILD_VERSION_STRING": None } if output_directory and os.path.exists(lock_file): exec(open(lock_file).read(), globals(), build_info) @@ -1072,6 +1073,22 @@ def run_step(args, target_to_run, target_args): exit(0) +def non_ambiguous_program_target_list(programs: dict) -> list: + # Assembles a dictionary of all the possible shortcuts a program have + list_of_shortcuts = {} + for (shortcut, possible_programs) in programs.items(): + if len(possible_programs) == 1: + if possible_programs[0] not in list_of_shortcuts: + list_of_shortcuts[possible_programs[0]] = [shortcut] + else: + list_of_shortcuts[possible_programs[0]].append(shortcut) + # Select the shortest non-ambiguous shortcut for each program + final_list = [] + for shortcuts in list_of_shortcuts.values(): + final_list.append(sorted(shortcuts, key=lambda x: len(x))[0]) + return final_list + + def print_targets_list(ns3_modules: list, ns3_programs: dict) -> None: def list_to_table(l: list) -> str: # Set column width and check how much is space is left at the end @@ -1109,29 +1126,45 @@ def print_targets_list(ns3_modules: list, ns3_programs: dict) -> None: output += large_item + "\n" return output - def non_ambiguous_target_list(programs: dict) -> list: - # Assembles a dictionary of all the possible shortcuts a program have - list_of_shortcuts = {} - for (shortcut, possible_programs) in programs.items(): - if len(possible_programs) == 1: - if possible_programs[0] not in list_of_shortcuts: - list_of_shortcuts[possible_programs[0]] = [shortcut] - else: - list_of_shortcuts[possible_programs[0]].append(shortcut) - # Select the shortest non-ambiguous shortcut for each program - final_list = [] - for shortcuts in list_of_shortcuts.values(): - final_list.append(sorted(shortcuts, key=lambda x: len(x))[0]) - return final_list - print("""Buildable targets:{buildables}\n\nRunnable/Buildable targets:{runnables} """.format(buildables=list_to_table(sorted(ns3_modules)), - runnables=list_to_table(non_ambiguous_target_list(ns3_programs)) + runnables=list_to_table(non_ambiguous_program_target_list(ns3_programs)) ) ) return +def show_profile(build_profile, exit_early=True): + if build_profile: + print("Build profile: %s" % build_profile) + else: + project_not_configured() + + if exit_early: + exit(0) + + +def show_build_version(build_version_string, exit_early=True): + if build_version_string is None: + project_not_configured() + + if build_version_string == '' and shutil.which("git") and os.path.exists(os.path.join(ns3_path, ".git")): + try: + build_version_string = subprocess.check_output(["git", "describe", "--dirty"], cwd=ns3_path).decode() + build_version_string += ("Reconfigure with './ns3 configure --enable-build-version' " + "to bake the version into the libraries.") + except subprocess.CalledProcessError: + pass + + if build_version_string == '': + print("Build version feature disabled. Reconfigure ns-3 with ./ns3 configure --enable-build-version") + else: + print("ns-3 version: %s" % build_version_string) + + if exit_early: + exit(0) + + # Debugging this with PyCharm is a no no. It refuses to work hanging indefinitely def sudo_command(command: list, password: str): # Run command and feed the sudo password @@ -1254,6 +1287,7 @@ def main(): # Get build profile and other settings build_info, ns3_modules = check_lock_data(out_dir) build_profile = build_info["BUILD_PROFILE"] + build_version_string = build_info["BUILD_VERSION_STRING"] enable_sudo = build_info["ENABLE_SUDO"] ns3_version = build_info["VERSION"] @@ -1267,13 +1301,10 @@ def main(): exit(1) if args.show == "profile": - if build_profile: - print("Build profile: %s" % build_profile) - else: - project_not_configured() + show_profile(build_profile) if args.show == "version": - args.build = ["check-version"] + show_build_version(build_version_string) # Check if running something or reconfiguring ns-3 run_only = False @@ -1309,7 +1340,7 @@ def main(): if args.show == "config": check_config(current_cmake_cache_folder) # We end things earlier if only checking the current project configuration - return + exit(0) # Check for changes in scratch sources and trigger a reconfiguration if sources changed if current_cmake_cache_folder: @@ -1346,6 +1377,16 @@ def main(): print_targets_list(ns3_modules, ns3_programs) exit(0) + if args.show == "all": + show_profile(build_profile, exit_early=False) + show_build_version(build_version_string, exit_early=False) + check_config(current_cmake_cache_folder) + print("---- Summary of buildable targets:") + print("Buildable targets: %4.d" % len(ns3_modules)) + print("Runnable/Buildable targets: %4.d" % len(non_ambiguous_program_target_list(ns3_programs))) + + exit(0) + def check_ambiguous_target(target_type, target_to_check, programs): if len(programs[target_to_check]) > 1: print('%s target "%s" is ambiguous. Try one of these: "%s"'