Skip to content

Commit

Permalink
Minor fixes (#41)
Browse files Browse the repository at this point in the history
* cleanup `build.zig`
* add tsan and ubsan support
  • Loading branch information
kassane authored Dec 8, 2024
1 parent 4df29ab commit de11612
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 41 deletions.
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,28 @@ zig build run-droptest -Doptimize=ReleaseSafe -Dimgui

zig build --help
# Project-Specific Options:
# -Dgl=[bool] Force OpenGL (default: false)
# -Dwgpu=[bool] Force WebGPU (default: false, web only)
# -Dx11=[bool] Force X11 (default: true, Linux only)
# -Dwayland=[bool] Force Wayland (default: false, Linux only, not supported in main-line headers)
# -Degl=[bool] Force EGL (default: false, Linux only)
# -Dimgui=[bool] Add support for sokol_imgui.h bindings
# -Dartifact=[bool] Build artifacts (default: false)
# -DbetterC=[bool] Omit generating some runtime information and helper functions #(default: false)
# -DzigCC=[bool] Use zig cc as compiler and linker (default: false)
# -Dtarget=[string] The CPU architecture, OS, and ABI to build for
# -Dcpu=[string] Target CPU features to add or subtract
# -Ddynamic-linker=[string] Path to interpreter on the target system
# -Doptimize=[enum] Prioritize performance, safety, or binary size
# Supported Values:
# Debug
# ReleaseSafe
# ReleaseFast
# ReleaseSmall
# -Dshared=[bool] Build sokol dynamic library (default: static)
# -Dgl=[bool] Force OpenGL (default: false)
# -Dgles3=[bool] Force OpenGL ES3 (default: false)
# -Dwgpu=[bool] Force WebGPU (default: false, web only)
# -Dx11=[bool] Force X11 (default: true, Linux only)
# -Dwayland=[bool] Force Wayland (default: false, Linux only, not supported in main-line headers)
# -Degl=[bool] Force EGL (default: false, Linux only)
# -Dimgui=[bool] Add support for sokol_imgui.h bindings
# -Dartifact=[bool] Build artifacts (default: false)
# -DbetterC=[bool] Omit generating some runtime information and helper functions (default: false)
# -DzigCC=[bool] Use zig cc as compiler and linker (default: false)
# -Dtarget=[string] The CPU architecture, OS, and ABI to build for
# -Dcpu=[string] Target CPU features to add or subtract
# -Ddynamic-linker=[string] Path to interpreter on the target system
# -Doptimize=[enum] Prioritize performance, safety, or binary size
# Supported Values:
# Debug
# ReleaseSafe
# ReleaseFast
# ReleaseSmall
# -Dshared=[bool] Build sokol dynamic library (default: static)
# -Dubsan=[bool] Enable undefined behavior sanitizer
# -Dtsan=[bool] Enable thread sanitizer
```
(also run `zig build -l` to get a list of build targets)

Expand Down
40 changes: 18 additions & 22 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
.optimize = options.optimize,
.link_libc = true,
});
lib.root_module.sanitize_c = false;

lib.root_module.sanitize_c = b.option(bool, "ubsan", "Enable undefined behavior sanitizer") orelse false;
lib.root_module.sanitize_thread = b.option(bool, "tsan", "Enable thread sanitizer") orelse false;

switch (options.optimize) {
.Debug, .ReleaseSafe => lib.bundle_compiler_rt = true,
Expand Down Expand Up @@ -192,6 +194,8 @@ pub fn buildLibSokol(b: *Build, options: LibSokolOptions) !*CompileStep {
.target = options.target,
.optimize = options.optimize,
.emsdk = options.emsdk,
.use_tsan = lib.root_module.sanitize_thread orelse false,
.use_ubsan = lib.root_module.sanitize_c orelse false,
});
for (cimgui.root_module.include_dirs.items) |dir| {
try lib.root_module.include_dirs.append(b.allocator, dir);
Expand Down Expand Up @@ -498,11 +502,9 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*std.Build.Step.InstallDi
if (tsan)
ldc_exec.addArg("--fsanitize=thread");
}

// zig enable sanitize=undefined by default
if (lib_sokol.root_module.sanitize_c) |ubsan| {
if (ubsan)
ldc_exec.addArg("--fsanitize=address");
if (lib_sokol.root_module.fuzz) |fuzz| {
if (fuzz)
ldc_exec.addArg("--fsanitize=fuzzer");
}

if (lib_sokol.root_module.omit_frame_pointer) |enabled| {
Expand Down Expand Up @@ -586,6 +588,7 @@ pub fn ldcBuildStep(b: *Build, options: DCompileStep) !*std.Build.Step.InstallDi
.use_webgl2 = backend != .wgpu,
.use_emmalloc = true,
.use_filesystem = false,
.use_ubsan = options.artifact.?.root_module.sanitize_c orelse false,
.release_use_lto = options.artifact.?.want_lto orelse false,
.shell_file_path = b.path("src/sokol/web/shell.html"),
.extra_args = &.{"-sSTACK_SIZE=512KB"},
Expand Down Expand Up @@ -747,16 +750,15 @@ fn buildShaders(b: *Build, target: Build.ResolvedTarget) void {
pub const EmLinkOptions = struct {
target: Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
// name: []const u8,
lib_main: *Build.Step.Compile,
// lib_sokol: *Build.Step.Compile,
emsdk: *Build.Dependency,
release_use_closure: bool = true,
release_use_lto: bool = false,
use_webgpu: bool = false,
use_webgl2: bool = false,
use_emmalloc: bool = false,
use_filesystem: bool = true,
use_ubsan: bool = false,
shell_file_path: ?Build.LazyPath,
extra_args: []const []const u8 = &.{},
};
Expand Down Expand Up @@ -797,6 +799,9 @@ pub fn emLinkStep(b: *Build, options: EmLinkOptions) !*Build.Step.InstallDir {
if (options.use_emmalloc) {
emcc.addArg("-sMALLOC='emmalloc'");
}
if (options.use_ubsan) {
emcc.addArg("-fsanitize=undefined");
}
if (options.shell_file_path) |shell_file_path| {
emcc.addPrefixedFileArg("--shell-file=", shell_file_path);
}
Expand Down Expand Up @@ -891,7 +896,10 @@ const libImGuiOptions = struct {
target: Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
emsdk: ?*Build.Dependency,
use_ubsan: bool = false,
use_tsan: bool = false,
};

fn buildImgui(b: *Build, options: libImGuiOptions) !*CompileStep {
const cimgui = b.dependency("imgui", .{}).path("src");

Expand All @@ -900,11 +908,9 @@ fn buildImgui(b: *Build, options: libImGuiOptions) !*CompileStep {
.target = options.target,
.optimize = options.optimize,
});
libimgui.root_module.sanitize_c = options.use_ubsan;
libimgui.root_module.sanitize_thread = options.use_tsan;

if (libimgui.linkage == .static)
libimgui.pie = true
else if (libimgui.linkage == .static)
libimgui.root_module.pic = true;
libimgui.addIncludePath(cimgui);

if (libimgui.rootModuleTarget().isWasm()) {
Expand All @@ -925,15 +931,6 @@ fn buildImgui(b: *Build, options: libImGuiOptions) !*CompileStep {
.files = &.{
"cimgui.cpp",
},
.flags = &.{
"-Wall",
"-Wextra",
"-fno-rtti",
"-fno-exceptions",
"-Wno-unused-parameter",
"-Wno-missing-field-initializers",
"-fno-threadsafe-statics",
},
});
libimgui.addCSourceFiles(.{
.root = cimgui,
Expand All @@ -947,7 +944,6 @@ fn buildImgui(b: *Build, options: libImGuiOptions) !*CompileStep {
.flags = &.{
"-Wall",
"-Wextra",
"-fno-rtti",
"-fno-exceptions",
"-Wno-unused-parameter",
"-Wno-missing-field-initializers",
Expand Down

0 comments on commit de11612

Please sign in to comment.