|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +pub fn build(b: *std.Build) void { |
| 4 | + const optimize = b.standardOptimizeOption(.{}); |
| 5 | + const target = b.standardTargetOptions(.{}); |
| 6 | + |
| 7 | + const options = .{ |
| 8 | + .shape_use_32bit_indices = b.option( |
| 9 | + bool, |
| 10 | + "shape_use_32bit_indices", |
| 11 | + "Enable par shapes 32-bit indices", |
| 12 | + ) orelse true, |
| 13 | + .shared = b.option( |
| 14 | + bool, |
| 15 | + "shared", |
| 16 | + "Build as shared library", |
| 17 | + ) orelse false, |
| 18 | + }; |
| 19 | + |
| 20 | + const options_step = b.addOptions(); |
| 21 | + inline for (std.meta.fields(@TypeOf(options))) |field| { |
| 22 | + options_step.addOption(field.type, field.name, @field(options, field.name)); |
| 23 | + } |
| 24 | + |
| 25 | + const options_module = options_step.createModule(); |
| 26 | + |
| 27 | + _ = b.addModule("root", .{ |
| 28 | + .root_source_file = b.path("src/root.zig"), |
| 29 | + .imports = &.{ |
| 30 | + .{ .name = "zmesh_options", .module = options_module }, |
| 31 | + }, |
| 32 | + }); |
| 33 | + |
| 34 | + const zmesh_lib = if (options.shared) blk: { |
| 35 | + const lib = b.addSharedLibrary(.{ |
| 36 | + .name = "zmesh", |
| 37 | + .target = target, |
| 38 | + .optimize = optimize, |
| 39 | + }); |
| 40 | + |
| 41 | + if (target.result.os.tag == .windows) { |
| 42 | + lib.defineCMacro("PAR_SHAPES_API", "__declspec(dllexport)"); |
| 43 | + lib.defineCMacro("CGLTF_API", "__declspec(dllexport)"); |
| 44 | + lib.defineCMacro("MESHOPTIMIZER_API", "__declspec(dllexport)"); |
| 45 | + lib.defineCMacro("ZMESH_API", "__declspec(dllexport)"); |
| 46 | + } |
| 47 | + |
| 48 | + break :blk lib; |
| 49 | + } else b.addStaticLibrary(.{ |
| 50 | + .name = "zmesh", |
| 51 | + .target = target, |
| 52 | + .optimize = optimize, |
| 53 | + }); |
| 54 | + b.installArtifact(zmesh_lib); |
| 55 | + |
| 56 | + zmesh_lib.linkLibC(); |
| 57 | + if (target.result.abi != .msvc) |
| 58 | + zmesh_lib.linkLibCpp(); |
| 59 | + |
| 60 | + const par_shapes_t = if (options.shape_use_32bit_indices) |
| 61 | + "-DPAR_SHAPES_T=uint32_t" |
| 62 | + else |
| 63 | + "-DPAR_SHAPES_T=uint16_t"; |
| 64 | + |
| 65 | + zmesh_lib.addIncludePath(b.path("libs/par_shapes")); |
| 66 | + zmesh_lib.addCSourceFile(.{ |
| 67 | + .file = b.path("libs/par_shapes/par_shapes.c"), |
| 68 | + .flags = &.{ "-std=c99", "-fno-sanitize=undefined", par_shapes_t }, |
| 69 | + }); |
| 70 | + |
| 71 | + zmesh_lib.addCSourceFiles(.{ |
| 72 | + .files = &.{ |
| 73 | + "libs/meshoptimizer/clusterizer.cpp", |
| 74 | + "libs/meshoptimizer/indexgenerator.cpp", |
| 75 | + "libs/meshoptimizer/vcacheoptimizer.cpp", |
| 76 | + "libs/meshoptimizer/vcacheanalyzer.cpp", |
| 77 | + "libs/meshoptimizer/vfetchoptimizer.cpp", |
| 78 | + "libs/meshoptimizer/vfetchanalyzer.cpp", |
| 79 | + "libs/meshoptimizer/overdrawoptimizer.cpp", |
| 80 | + "libs/meshoptimizer/overdrawanalyzer.cpp", |
| 81 | + "libs/meshoptimizer/simplifier.cpp", |
| 82 | + "libs/meshoptimizer/allocator.cpp", |
| 83 | + }, |
| 84 | + .flags = &.{""}, |
| 85 | + }); |
| 86 | + zmesh_lib.addIncludePath(b.path("libs/cgltf")); |
| 87 | + zmesh_lib.addCSourceFile(.{ |
| 88 | + .file = b.path("libs/cgltf/cgltf.c"), |
| 89 | + .flags = &.{"-std=c99"}, |
| 90 | + }); |
| 91 | + |
| 92 | + const test_step = b.step("test", "Run zmesh tests"); |
| 93 | + |
| 94 | + const tests = b.addTest(.{ |
| 95 | + .name = "zmesh-tests", |
| 96 | + .root_source_file = b.path("src/root.zig"), |
| 97 | + .target = target, |
| 98 | + .optimize = optimize, |
| 99 | + }); |
| 100 | + b.installArtifact(tests); |
| 101 | + |
| 102 | + tests.linkLibrary(zmesh_lib); |
| 103 | + tests.addIncludePath(b.path("libs/cgltf")); |
| 104 | + |
| 105 | + test_step.dependOn(&b.addRunArtifact(tests).step); |
| 106 | +} |
0 commit comments