build: Use cmake options -S and -B to avoid changing working directory

This commit is contained in:
Alexander Krotov
2023-11-04 18:27:48 -03:00
committed by Gabriel Ferreira
parent 3a356f4680
commit a1a6d55949
2 changed files with 20 additions and 23 deletions

33
ns3
View File

@@ -601,7 +601,7 @@ def project_configured(current_cmake_cache_folder):
def configure_cmake(cmake, args, current_cmake_cache_folder, current_cmake_generator, output, dry_run=False): def configure_cmake(cmake, args, current_cmake_cache_folder, current_cmake_generator, output, dry_run=False):
# Aggregate all flags to configure CMake # Aggregate all flags to configure CMake
cmake_args = [cmake] cmake_args = [cmake, "-S", ns3_path]
if not project_configured(current_cmake_cache_folder): if not project_configured(current_cmake_cache_folder):
# Create a new cmake_cache folder if one does not exist # Create a new cmake_cache folder if one does not exist
@@ -627,6 +627,8 @@ def configure_cmake(cmake, args, current_cmake_cache_folder, current_cmake_gener
cmake_args.append(f"-DPython3_INCLUDE_DIRS={sysconfig.get_config_var('INCLUDEPY')}") cmake_args.append(f"-DPython3_INCLUDE_DIRS={sysconfig.get_config_var('INCLUDEPY')}")
cmake_args.append(f"-DPython3_EXECUTABLE={sys.executable}") cmake_args.append(f"-DPython3_EXECUTABLE={sys.executable}")
cmake_args.extend(["-B", current_cmake_cache_folder])
# C++ standard # C++ standard
if args.cxx_standard is not None: if args.cxx_standard is not None:
cmake_args.append("-DCMAKE_CXX_STANDARD=%s" % args.cxx_standard) cmake_args.append("-DCMAKE_CXX_STANDARD=%s" % args.cxx_standard)
@@ -728,20 +730,13 @@ def configure_cmake(cmake, args, current_cmake_cache_folder, current_cmake_gener
# Append CMake flags passed using the -- separator # Append CMake flags passed using the -- separator
cmake_args.extend(args.program_args) cmake_args.extend(args.program_args)
# Configure cmake
cmake_args.append("..") # for now, assuming the cmake_cache directory is inside the ns-3-dev folder
# Echo out the configure command # Echo out the configure command
print_and_buffer("cd %s; %s ; cd %s" % (os.path.relpath(current_cmake_cache_folder, ns3_path), print_and_buffer(" ".join(cmake_args))
" ".join(cmake_args),
os.path.relpath(ns3_path, current_cmake_cache_folder)
)
)
# Run cmake # Run cmake
if not dry_run: if not dry_run:
proc_env = os.environ.copy() proc_env = os.environ.copy()
ret = subprocess.run(cmake_args, cwd=current_cmake_cache_folder, stdout=output, env=proc_env) ret = subprocess.run(cmake_args, stdout=output, env=proc_env)
if ret.returncode != 0: if ret.returncode != 0:
exit(ret.returncode) exit(ret.returncode)
@@ -877,15 +872,14 @@ def cmake_check_version():
def cmake_build(current_cmake_cache_folder, output, jobs, target=None, dry_run=False, build_verbose=False): def cmake_build(current_cmake_cache_folder, output, jobs, target=None, dry_run=False, build_verbose=False):
cmake, version = cmake_check_version() cmake, version = cmake_check_version()
jobs_part = ("-j %d" % jobs) cmake_args = [cmake, "--build", current_cmake_cache_folder]
target_part = (" --target %s" % target) if target else "" if jobs:
cmake_build_command = "%s --build . %s%s" % (cmake, jobs_part, target_part) cmake_args.extend(["-j", str(jobs)])
print_and_buffer("cd %s; %s ; cd %s" % (os.path.relpath(current_cmake_cache_folder, ns3_path), if target:
cmake_build_command, cmake_args.extend(["--target", target])
os.path.relpath(ns3_path, current_cmake_cache_folder)
) print_and_buffer(" ".join(cmake_args))
)
if not dry_run: if not dry_run:
# Assume quiet is not enabled, and print things normally # Assume quiet is not enabled, and print things normally
kwargs = {"stdout": None, kwargs = {"stdout": None,
@@ -903,8 +897,7 @@ def cmake_build(current_cmake_cache_folder, output, jobs, target=None, dry_run=F
kwargs["stdout"] = subprocess.PIPE kwargs["stdout"] = subprocess.PIPE
kwargs["stderr"] = subprocess.PIPE kwargs["stderr"] = subprocess.PIPE
ret = subprocess.run(cmake_build_command.split(), ret = subprocess.run(cmake_args,
cwd=current_cmake_cache_folder,
env=proc_env, env=proc_env,
**kwargs, **kwargs,
) )

View File

@@ -43,9 +43,13 @@ os.chdir(ns3_path)
# Cmake commands # Cmake commands
num_threads = max(1, os.cpu_count() - 1) num_threads = max(1, os.cpu_count() - 1)
cmake_build_project_command = "cmake --build . -j".format(ns3_path=ns3_path) cmake_build_project_command = "cmake --build {cmake_cache} -j".format(
cmake_build_target_command = partial("cmake --build . -j {jobs} --target {target}".format, ns3_path=ns3_path,
jobs=num_threads cmake_cache=os.path.abspath(os.path.join(ns3_path, "cmake-cache"))
)
cmake_build_target_command = partial("cmake --build {cmake_cache} -j {jobs} --target {target}".format,
jobs=num_threads,
cmake_cache=os.path.abspath(os.path.join(ns3_path, "cmake-cache"))
) )
win32 = sys.platform == "win32" win32 = sys.platform == "win32"
platform_makefiles = "MinGW Makefiles" if win32 else "Unix Makefiles" platform_makefiles = "MinGW Makefiles" if win32 else "Unix Makefiles"