Skip to content

Commit

Permalink
Fix waf build order
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenvp committed Dec 4, 2023
1 parent 448f127 commit d63cf53
Showing 1 changed file with 78 additions and 50 deletions.
128 changes: 78 additions & 50 deletions wscript
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit d63cf53

Please sign in to comment.