From d63cf5313bc8e81bb21a8d7ab22e97dbb3b733c6 Mon Sep 17 00:00:00 2001 From: "Morten V. Pedersen" Date: Mon, 4 Dec 2023 15:46:12 +0100 Subject: [PATCH] Fix waf build order --- wscript | 128 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 78 insertions(+), 50 deletions(-) diff --git a/wscript b/wscript index 62032bb..764f1e6 100644 --- a/wscript +++ b/wscript @@ -12,86 +12,114 @@ VERSION = "1.1.0" CMAKE_BUILD_TYPE = "Debug" lib_name = None + def configure(conf): if conf.has_tool_option("cxx_debug"): CMAKE_BUILD_TYPE = "Debug" +def build(bld): + bld.post_mode = Build.POST_LAZY + # Find the source directory for the external library + src_dir = bld.dependency_node("srt-source") -def pre(bld): - bld.find_program("cmake", mandatory=True, quiet=1, output=0) - bld.post_mode = Build.POST_LAZY - root_dir = bld.dependency_node("srt-source") - if platform.system() == "Windows": - lib_name = "srt_static.lib" - elif platform.system() == "Darwin": - lib_name = "libsrt.a" - elif platform.system() == "Linux": - lib_name = "libsrt.a" - else: - Logs.error("Unsupported platform") - return -1 - target = bld.bldnode.make_node("srt") - bld(name="srt_lib",rule=CMakeBuildTask, target=target.make_node('flag.lock'), source=root_dir ) + # Declare the temporary build directory for the external library + # it is best to keep it under the project build directory + build_dir = bld.bldnode.make_node("cmake_build") + # Declare the install directory for the external library + install_dir = build_dir.make_node("install") + # Declare the include directory for the external library + include_dir = install_dir.make_node("include") -def post(bld): - lib_path = bld.path.find_node("srt/lib") - include_path = bld.path.find_node("srt/include") - bld(name="srt_includes", export_includes=[include_path]) + # Declare the lib directory for the external library + lib_dir = install_dir.make_node("lib") - bld.read_stlib(lib_name, paths=[lib_path], export_includes=[include_path]) + # build the external library through an external process + bld( + rule=CMakeBuildTask, + target=build_dir.make_node("flag.lock"), + install_dir=install_dir, + source=src_dir, + ) + # once it is done create a second build group + bld.add_group() + + if platform.system() == "Windows": + lib_name = "srt_static" + else: + lib_name = "srt" + + bld.read_stlib(lib_name, paths=[lib_dir], export_includes=[include_dir]) if bld.is_toplevel(): bld.program( features="cxx test", - source= bld.path.ant_glob( - "test/**/*.cpp"), + source=bld.path.ant_glob("test/**/*.cpp"), target="srt_test", - use=["srt_static", "srt_includes", "gtest", "platform_includes" ], + use=[lib_name, "srt_includes", "gtest", "platform_includes"], ) -def build(bld): - bld.add_pre_fun(pre) - bld.add_post_fun(post) +def CMakeBuildTask(task): + # This is the directory where the external library will be installed the + # task.outputs[0] is the flag file that will be created once the external + # library is installed + output_dir = task.outputs[0].parent + # This is the directory where the external library source code is located + source_dir = task.inputs[0] + # The install dir is passed as a parameter to the task + install_dir = task.generator.install_dir + # remove the output directory if it exists + shutil.rmtree(output_dir.abspath()) + # create the output directory + os.makedirs(output_dir.abspath()) -def CMakeBuildTask(task): - output = task.outputs[0].parent - source_dir = task.inputs[0] - shutil.rmtree(output.abspath()) - if not os.path.isdir(output.abspath()): - os.makedirs(output.abspath()) - #check if cmake_build folder exists - if os.path.isdir(f"{source_dir}/cmake_build"): - shutil.rmtree(f"{source_dir}/cmake_build") - os.makedirs (f"{source_dir}/cmake_build") - print(CMAKE_BUILD_TYPE) - # SRT cmake flags - flags = " ".join([ + # SRT cmake flags + flags = " ".join( + [ "-DENABLE_SHARED=ON", "-DENABLE_STATIC=ON", "-DENABLE_APPS=OFF", "-DENABLE_ENCRYPTION=OFF", "-DENABLE_BONDING=ON", f"-DCMAKE_BUILD_TYPE={CMAKE_BUILD_TYPE}", - ]) - try: - task.generator.bld.cmd_and_log(f"cmake {flags} -S {source_dir} -B {source_dir}/cmake_build", quiet=0, output=0) - task.generator.bld.cmd_and_log(f"cmake --build {source_dir}/cmake_build --parallel --config {CMAKE_BUILD_TYPE}", quiet=0, output=0) - task.generator.bld.cmd_and_log(f"cmake --install {source_dir}/cmake_build --prefix {output} --config {CMAKE_BUILD_TYPE}", quiet=0, output=0) - except Errors.WafError as e: - Logs.error(e.stderr) - return -1 - Logs.info(f"Installed srt lib to {output}") - else: - Logs.info(f"Found srt lib in {output}") + ] + ) + + # Run all commands in the output directory + cwd = output_dir.abspath() + + try: + task.generator.bld.cmd_and_log( + f"cmake {flags} -S {source_dir}", cwd=cwd, quiet=0, output=0 + ) + task.generator.bld.cmd_and_log( + f"cmake --build . --parallel --config {CMAKE_BUILD_TYPE}", + cwd=cwd, + quiet=0, + output=0, + ) + + task.generator.bld.cmd_and_log( + f"cmake --install . --prefix {install_dir} --config {CMAKE_BUILD_TYPE}", + cwd=cwd, + quiet=0, + output=0, + ) + + except Errors.WafError as e: + Logs.error(e.stderr) + return -1 + + Logs.info(f"Installed srt lib to {output_dir}") + # write a lock file so that a rebuild occurs if files are removed manually task.outputs[0].write("ok")