lore-forge/desktop/vendor/uhdk/build.zig

158 lines
6.7 KiB
Zig

const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const use_llvm = b.option(bool, "use_llvm", "Use LLVM for Zig compilation") orelse null;
const shared = b.option(bool, "shared", "Build libuhdk as a shared library instead of static") orelse false;
const tracy = b.option(bool, "tracy", "Emit Tracy 0.14.1 on-demand hooks; consumer must link the client") orelse false;
if (tracy and shared) @panic("Tracy instrumentation requires static linking");
const lib = createLibraryNamed(b, target, optimize, use_llvm, shared, tracy, "uhdk");
const module = lib.root_module;
const install_lib = b.addInstallArtifact(lib, .{});
const install_headers = b.addInstallDirectory(.{
.source_dir = b.path("include"),
.install_dir = .header,
.install_subdir = "",
});
const lib_step = b.step("lib", "Build and install the Uhdk library and headers");
lib_step.dependOn(&install_lib.step);
lib_step.dependOn(&install_headers.step);
b.getInstallStep().dependOn(&install_lib.step);
b.getInstallStep().dependOn(&install_headers.step);
const unit = b.addTest(.{ .root_module = module });
const check_step = b.step("check", "Compile library and core tests without running them");
check_step.dependOn(&lib.step);
check_step.dependOn(&unit.step);
const unit_run = b.addRunArtifact(unit);
b.step("test", "Run core library tests").dependOn(&unit_run.step);
const public_c = b.addTranslateC(.{
.root_source_file = b.path("include/uhdk.h"),
.target = target,
.optimize = optimize,
});
public_c.defineCMacro("UHDK_ENABLE_OPENGL", "1");
public_c.defineCMacro("UHDK_ENABLE_NATIVE", "1");
public_c.addIncludePath(b.path("include"));
if (shared)
public_c.defineCMacro("UHDK_SHARED", "1");
const public_module = b.addModule("uhdk", .{
.root_source_file = public_c.getOutput(),
.target = target,
.optimize = optimize,
});
public_module.linkLibrary(lib);
}
// Create a library artifact without installing it. Instrumented static builds
// require the consumer to link a matching Tracy client.
pub fn createLibrary(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, use_llvm: ?bool, shared: bool, instrumented: bool) *std.Build.Step.Compile {
return createLibraryNamed(b, target, optimize, use_llvm, shared, instrumented, if (instrumented) "uhdk-instrumented" else "uhdk");
}
fn createLibraryNamed(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, use_llvm: ?bool, shared: bool, instrumented: bool, artifact_name: []const u8) *std.Build.Step.Compile {
std.debug.assert(!shared or !instrumented);
// The Windows DLL links no C runtime: MinGW-w64 headers only, and the system
// import libraries come from src/windows/imports.zig. Static libraries keep
// linking libc so consumers inherit the system libraries below.
const no_crt = shared and target.result.os.tag == .windows;
const module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.link_libc = !no_crt,
// x64 Windows requires unwind data for SEH and stack walking.
.unwind_tables = if (optimize == .small and target.result.os.tag != .windows) .none else null,
});
const native = b.addTranslateC(.{
.root_source_file = b.path("src/native.h"),
.target = target,
.optimize = optimize,
.link_libc = !no_crt,
});
native.defineCMacro("UHDK_ENABLE_OPENGL", "1");
native.addIncludePath(b.path("include"));
module.addImport("native", native.createModule());
module.addImport("profile", createProfileModule(b, target, optimize, instrumented));
module.addIncludePath(b.path("include"));
module.addIncludePath(b.path("src"));
if (instrumented)
module.addCMacro("UHDK_INTERNAL_TRACY", "1");
if (target.result.os.tag == .macos) {
module.addCSourceFile(.{
.file = b.path("src/macos/native.m"),
.flags = &.{
"-fobjc-arc", "-fblocks", "-Wno-deprecated-declarations",
"-Wall", "-Wextra", "-Werror",
},
});
module.linkFramework("Cocoa", .{});
module.linkFramework("OpenGL", .{});
module.linkFramework("CoreServices", .{});
module.linkFramework("CoreVideo", .{});
} else if (target.result.os.tag == .linux) {
@import("src/linux/build.zig").configure(b, module, target, optimize);
} else {
module.addCSourceFile(.{ .file = b.path("src/windows/native.c"), .flags = cFlags(b, no_crt, &.{ "-std=c11", "-Wall", "-Wextra", "-Werror", "-DUNICODE", "-D_UNICODE" }) });
if (no_crt) {
module.addSystemIncludePath(std.Build.LazyPath.zig_lib.path(b, "libc/include/any-windows-any"));
module.addCMacro("UHDK_NO_CRT", "1");
} else {
for ([_][]const u8{ "user32", "gdi32", "opengl32", "shell32", "ole32", "uuid", "imm32", "comctl32", "shlwapi", "advapi32", "api-ms-win-core-synch-l1-2-0" }) |name| module.linkSystemLibrary(name, .{});
}
}
module.addCSourceFile(.{ .file = b.path("src/gl_loader.c"), .flags = cFlags(b, no_crt, &.{ "-std=c99", "-Wall", "-Wextra", "-Werror", "-fvisibility=hidden" }) });
module.addCSourceFile(.{ .file = b.path("src/platform_io.c"), .flags = cFlags(b, no_crt, &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" }) });
const lib = b.addLibrary(.{
.name = artifact_name,
.linkage = if (shared) .dynamic else .static,
.use_llvm = use_llvm,
.root_module = module,
});
if (shared and target.result.os.tag == .linux)
lib.version_script = b.path("src/linux/exports.map");
// temp
// lib.root_module.strip = false;
return lib;
}
// Without a C runtime, keep LLVM from rewriting src/windows/nocrt.h's loops into
// calls to the C runtime functions they replace.
fn cFlags(b: *std.Build, no_crt: bool, flags: []const []const u8) []const []const u8 {
if (!no_crt) return flags;
return std.mem.concat(b.allocator, []const u8, &.{ flags, &.{"-fno-builtin-wcslen"} }) catch @panic("OOM");
}
pub fn createProfileModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, enabled: bool) *std.Build.Module {
const options = b.addOptions();
options.addOption(bool, "enabled", enabled);
const profile = b.createModule(.{
.root_source_file = b.path("src/Profile.zig"),
.target = target,
.optimize = optimize,
});
profile.addOptions("profile_options", options);
return profile;
}