Files
omtoy/omt/build.zig
2025-10-27 07:38:40 +02:00

162 lines
5.8 KiB
Zig

//
// OMT module for zig
//
// Build required OMT parts and link against our code
//
// I don't know yet how to best create a conditional step to build and install
// the dependencies only if they aren't installed yet. Just putting the installs
// as dependencies runs the omt build scripts on every `zig build`.
//
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const triple = target.result;
if (triple.os.tag != .linux or triple.cpu.arch != .x86_64) {
std.log.warn("The build file only supports Linux x86-64 right now.\n", .{});
std.log.warn(
"You can extend the build.zig to call the appropriate platform scripts " ++
"in 3rd/libomtnet/build/ and 3rd/libomt/build and 3rd/libvmx/build/ " ++
"and modifying the install scripts",
.{},
);
return;
}
// This path comes from the libomt build scripts
const omt_out_path = "3rd/libomt/bin/Release/net8.0/linux-x64/publish/";
const output_dir_path: std.Build.LazyPath = .{
.src_path = .{
.owner = b,
.sub_path = omt_out_path,
},
};
b.addNamedLazyPath("output_dir", output_dir_path);
// The output lib and header directories
const lib_output_path: std.Build.LazyPath = .{
.cwd_relative = b.getInstallPath(.lib, ""),
};
const include_output_path: std.Build.LazyPath = .{
.cwd_relative = b.getInstallPath(.header, ""),
};
std.debug.print("install-path: {s}\n", .{b.getInstallPath(.prefix, "")});
std.debug.print("lib_output_path: {s}\n", .{lib_output_path.getDisplayName()});
std.debug.print("include_output_path: {s}\n", .{include_output_path.getDisplayName()});
// zomt module
const omt_mod = b.addModule("zomt", .{
.root_source_file = b.path("src/omt.zig"),
.target = target,
.optimize = optimize,
});
omt_mod.addLibraryPath(output_dir_path);
omt_mod.addIncludePath(output_dir_path);
omt_mod.linkSystemLibrary("omt", .{});
// Build libomtnet
const libomtnet_build_script = b.addSystemCommand(&.{"bash"});
libomtnet_build_script.cwd = b.path("3rd/libomtnet/build");
libomtnet_build_script.addFileArg(b.path("3rd/libomtnet/build/buildall.sh"));
// Build libomt
const libomt_build_script = b.addSystemCommand(&.{"bash"});
libomt_build_script.cwd = b.path("3rd/libomt/build");
libomt_build_script.addFileArg(b.path("3rd/libomt/build/buildlinuxx64.sh"));
libomt_build_script.step.dependOn(&libomtnet_build_script.step);
// Install libomt
const install_omt_h = b.addInstallHeaderFile(
b.path(b.pathJoin(&.{ omt_out_path, "libomt.h" })),
"libomt.h",
);
install_omt_h.step.dependOn(&libomt_build_script.step);
const install_omt_so = b.addInstallLibFile(
b.path(b.pathJoin(&.{ omt_out_path, "libomt.so" })),
"libomt.so",
);
install_omt_so.step.dependOn(&libomt_build_script.step);
// Build libvmx
const libvmx_build = b.addSystemCommand(&.{"bash"});
libvmx_build.cwd = b.path("3rd/libvmx/build");
libvmx_build.addFileArg(b.path("3rd/libvmx/build/buildlinuxx64.sh"));
// Install libvmx
const install_vmx_so = b.addInstallLibFile(
b.path("3rd/libvmx/build/libvmx.so"),
"libvmx.so",
);
install_vmx_so.step.dependOn(&libvmx_build.step);
// Add object as dependency for module
const libomtso_path = b.path(b.pathJoin(&.{ omt_out_path, "libomt.so" }));
// Has no discernable effect atm
omt_mod.addObjectFile(libomtso_path);
{
// Manual build step for libomt dependencies
const build_omt_step = b.step("omt", "Build libomtnet, libomt and libvmx");
build_omt_step.dependOn(&install_omt_h.step);
build_omt_step.dependOn(&install_omt_so.step);
build_omt_step.dependOn(&install_vmx_so.step);
}
// Test executables
{
// Define executables
const sender_exe = b.addExecutable(.{
.name = "sender",
.root_module = b.createModule(.{
.root_source_file = b.path("utils/sender.zig"),
.target = target,
.optimize = optimize,
}),
});
sender_exe.root_module.addImport("omt", omt_mod);
// These cause invocations for every zig build command, even tho it's not
// usually needed, so prefer to use a manual step
sender_exe.step.dependOn(&install_omt_h.step);
sender_exe.step.dependOn(&install_omt_so.step);
sender_exe.step.dependOn(&install_vmx_so.step);
sender_exe.root_module.addLibraryPath(lib_output_path);
sender_exe.root_module.addIncludePath(include_output_path);
sender_exe.linkSystemLibrary("omt");
sender_exe.linkLibC();
b.installArtifact(sender_exe);
}
{
const receiver_exe = b.addExecutable(.{
.name = "receiver",
.root_module = b.createModule(.{
.root_source_file = b.path("utils/receiver.zig"),
.target = target,
.optimize = optimize,
}),
});
receiver_exe.root_module.addImport("omt", omt_mod);
// These cause invocations for every zig build command, even tho it's not
// usually needed, so prefer to use a manual step
receiver_exe.step.dependOn(&install_omt_h.step);
receiver_exe.step.dependOn(&install_omt_so.step);
receiver_exe.step.dependOn(&install_vmx_so.step);
receiver_exe.root_module.addLibraryPath(lib_output_path);
receiver_exe.root_module.addIncludePath(include_output_path);
receiver_exe.linkSystemLibrary("omt");
receiver_exe.linkLibC();
b.installArtifact(receiver_exe);
}
}