-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
1311 lines (1217 loc) · 57 KB
/
Copy pathbuild.zig
File metadata and controls
1311 lines (1217 loc) · 57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const DawnBuild = struct {
step: *std.Build.Step,
include_dir: []const u8,
gen_include_dir: []const u8,
out_dir: []const u8,
};
const WindowsSdkInfo = struct {
path: []const u8,
version: []const u8,
};
const WindowsHostToolchain = struct {
clang: []const u8,
clangxx: []const u8,
llvm_ar: []const u8,
zig_lib_dir: []const u8,
};
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const is_android = target.result.os.tag == .linux and target.result.abi == .android;
const use_lld = target.result.os.tag == .windows;
const strip_for_windows: ?bool = if (target.result.os.tag == .windows) true else null;
const android_ndk_root = if (is_android)
std.process.getEnvVarOwned(b.allocator, "ANDROID_NDK_HOME") catch
@as([]const u8, "/home/autark/android/android-ndk-r27c")
else
"";
// --- quickjs-ng (vendored) ---
const qjs_dep = b.dependency("zig-quickjs-ng", .{
.target = target,
.optimize = optimize,
});
const qjs_mod = qjs_dep.module("quickjs");
const qjs_lib = qjs_dep.artifact("quickjs-ng");
// --- Library module (for test aggregation) ---
const lib_mod = b.addModule("threez", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.strip = strip_for_windows,
});
lib_mod.addImport("quickjs", qjs_mod);
// --- Main artifact: executable on desktop, shared library on Android ---
// QuickJS Value is an extern struct; the default backend cannot
// lower its return type yet (Zig compiler TODO), so use LLVM.
const exe = if (is_android) b.addLibrary(.{
.name = "threez",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
.linkage = .dynamic,
.use_llvm = true,
.use_lld = use_lld,
}) else b.addExecutable(.{
.name = "threez",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.strip = strip_for_windows,
}),
.use_llvm = true,
.use_lld = use_lld,
});
// Android NDK sysroot — Zig doesn't ship bionic headers, so we provide them.
// Also add NDK library search path for system libs (libandroid, liblog, etc.).
if (is_android) {
const libc_conf = if (target.result.cpu.arch.isAARCH64())
b.path("deps/android-sysroot/aarch64-libc.conf")
else
b.path("deps/android-sysroot/x86_64-libc.conf");
qjs_lib.setLibCFile(libc_conf);
exe.setLibCFile(libc_conf);
const arch_dir = if (target.result.cpu.arch.isAARCH64())
"aarch64-linux-android"
else
"x86_64-linux-android";
const ndk_sysroot_lib = b.fmt(
"{s}/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/{s}",
.{ android_ndk_root, arch_dir },
);
exe.addLibraryPath(.{ .cwd_relative = b.fmt("{s}/26", .{ndk_sysroot_lib}) });
// Also add the base sysroot lib dir for the NDK C++ runtime.
exe.addLibraryPath(.{ .cwd_relative = ndk_sysroot_lib });
exe.linkSystemLibrary("c++_static");
// Vendor native_app_glue: compile C source and add include path
exe.addIncludePath(b.path("deps/android-ndk"));
exe.addCSourceFile(.{
.file = b.path("deps/android-ndk/android_native_app_glue.c"),
.flags = &.{"-fno-sanitize=undefined"},
});
// Force ANativeActivity_onCreate into .dynsym so the Android runtime
// can dlsym() it when launching the NativeActivity.
exe.forceUndefinedSymbol("ANativeActivity_onCreate");
}
// --- miniaudio (cross-platform audio) ---
const miniaudio = b.dependency("miniaudio", .{});
exe.addIncludePath(miniaudio.path(""));
exe.addCSourceFile(.{
.file = miniaudio.path("miniaudio.c"),
.flags = &.{"-std=c99"},
});
// Platform-specific linking for miniaudio
if (target.result.os.tag == .linux and !is_android) {
exe.linkSystemLibrary("pthread");
exe.linkSystemLibrary("m");
exe.linkSystemLibrary("dl");
} else if (target.result.os.tag == .windows) {
// Windows: no extra linking needed
} else if (target.result.os.tag == .macos) {
// macOS: needs to be compiled as Objective-C
exe.linkFramework("CoreAudio");
exe.linkFramework("AudioToolbox");
exe.linkFramework("CoreFoundation");
} else if (is_android) {
// Android: OpenSL|ES is built-in, no pthread linking needed
exe.linkSystemLibrary("OpenSLES");
}
// --- Dependencies ---
// zglfw — GLFW windowing library (not available on Android)
const zglfw_dep = if (!is_android) b.dependency("zglfw", .{
.target = target,
.optimize = optimize,
}) else null;
if (zglfw_dep) |dep| {
const zglfw_mod = dep.module("root");
lib_mod.addImport("zglfw", zglfw_mod);
exe.root_module.addImport("zglfw", zglfw_mod);
exe.linkLibrary(dep.artifact("glfw"));
}
const dawn_mri_tool = addDawnMriTool(b);
const dawn_prep_tool = addDawnPrepTool(b);
const dawn = addNativeDawnBuild(b, target, dawn_mri_tool, dawn_prep_tool, android_ndk_root);
// quickjs — JavaScript engine (needed by main.zig for JS runtime)
exe.root_module.addImport("quickjs", qjs_mod);
exe.linkLibrary(qjs_lib);
// --- Library artifact (static, for embedding — desktop only) ---
const lib = if (!is_android) blk: {
// QuickJS Value is an extern struct; use LLVM backend.
const l = b.addLibrary(.{
.name = "threez",
.root_module = lib_mod,
.linkage = .static,
.use_llvm = true,
.use_lld = use_lld,
});
l.linkLibrary(qjs_lib);
l.linkLibrary(zglfw_dep.?.artifact("glfw"));
addDawnHeaders(l, dawn);
break :blk l;
} else null;
// Dawn/WebGPU native linking: all native targets use the source-built Dawn C API.
if (is_android) {
exe.addCSourceFile(.{
.file = b.path("src/android_wgpu_shim.c"),
.flags = &.{"-fno-sanitize=undefined"},
});
}
linkDawnConsumer(b, exe, target, dawn);
// zignal
if (b.lazyDependency("zignal", .{})) |zignal_dep| {
const zignal_mod = zignal_dep.module("zignal");
lib_mod.addImport("zignal", zignal_mod);
exe.root_module.addImport("zignal", zignal_mod);
}
// zig-clap (desktop only — CLI argument parsing)
if (!is_android) {
if (b.lazyDependency("clap", .{})) |clap_dep| {
const clap_mod = clap_dep.module("clap");
lib_mod.addImport("clap", clap_mod);
exe.root_module.addImport("clap", clap_mod);
}
}
// --- Install ---
if (lib) |l| b.installArtifact(l);
b.installArtifact(exe);
// --- APK packaging (Android only) ---
if (is_android) {
const android_sdk_root = std.process.getEnvVarOwned(b.allocator, "ANDROID_HOME") catch
@as([]const u8, "/home/autark/android/sdk");
const build_tools = b.fmt("{s}/build-tools/35.0.0", .{android_sdk_root});
const platform_jar = b.fmt("{s}/platforms/android-35/android.jar", .{android_sdk_root});
const abi_dir = if (target.result.cpu.arch.isAARCH64()) "arm64-v8a" else "x86_64";
// Step 1: aapt2 link — compile manifest into base APK
const aapt2_link = b.addSystemCommand(&.{
b.fmt("{s}/aapt2", .{build_tools}),
"link",
"-o",
});
const base_apk = aapt2_link.addOutputFileArg("base.apk");
aapt2_link.addArgs(&.{ "-I", platform_jar });
aapt2_link.addArgs(&.{ "--manifest" });
aapt2_link.addFileArg(b.path("AndroidManifest.xml"));
aapt2_link.addArgs(&.{ "--min-sdk-version", "26", "--target-sdk-version", "33" });
// Optional assets directory to bundle into the APK
// If not provided, use default example assets
const user_assets_dir = b.option([]const u8, "assets", "Directory of assets to bundle in APK");
// Asset staging step (created conditionally)
var stage_step: ?*std.Build.Step = null;
// Create default asset staging directory if no custom assets provided
const assets_dir = if (user_assets_dir) |dir| dir else blk: {
const default_assets_dir = b.path("android-assets");
const stage_assets = b.addRunArtifact(b.addExecutable(.{
.name = "stage-android-assets",
.root_module = b.createModule(.{
.root_source_file = b.path("build/stage_android_assets.zig"),
.target = b.graph.host,
.optimize = .ReleaseSafe,
}),
}));
stage_assets.addArg(default_assets_dir.getPath(b));
stage_assets.addArg(b.path("examples/gltf_viewer/dist/gltf-bundle.js").getPath(b));
stage_assets.addArg(b.path("examples/gltf_viewer/assets").getPath(b));
stage_step = b.step("stage-android-assets", "Stage default Android assets");
stage_step.?.dependOn(&stage_assets.step);
break :blk default_assets_dir.getPath(b);
};
// Step 2: assemble APK (inject .so, assets, zipalign, sign)
const assemble = b.addSystemCommand(&.{"/bin/sh", "-c"});
const so_path = exe.getEmittedBin();
const apk_script = b.fmt(
\\set -e
\\WORK=$(mktemp -d)
\\cp "$1" "$WORK/base.apk"
\\mkdir -p "$WORK/lib/{s}"
\\cp "$2" "$WORK/lib/{s}/libthreez.so"
\\cd "$WORK" && zip -0 base.apk "lib/{s}/libthreez.so"
\\if [ -n "$5" -a -d "$5" ]; then mkdir -p "$WORK/assets" && cp -r "$5"/. "$WORK/assets/" && cd "$WORK" && find assets -type f -exec zip -0 base.apk {{}} \; ; fi
\\"{s}/zipalign" -f 4 "$WORK/base.apk" "$WORK/aligned.apk"
\\"{s}/apksigner" sign --ks "$3" --ks-pass pass:android --key-pass pass:android --out "$4" "$WORK/aligned.apk"
\\rm -rf "$WORK"
, .{ abi_dir, abi_dir, abi_dir, build_tools, build_tools });
assemble.addArg(apk_script);
assemble.addArg("--");
assemble.addFileArg(base_apk);
assemble.addFileArg(so_path);
assemble.addFileArg(b.path("debug.keystore"));
const signed_apk = assemble.addOutputFileArg("threez.apk");
assemble.addArg(assets_dir);
assemble.step.dependOn(&aapt2_link.step);
const install_apk = b.addInstallFile(signed_apk, "threez.apk");
const apk_step = b.step("apk", "Build signed Android APK");
apk_step.dependOn(&install_apk.step);
// Make APK step depend on asset staging when using default assets
if (stage_step) |step| {
apk_step.dependOn(step);
}
const adb_install = b.addSystemCommand(&.{ "adb", "install", "-r" });
adb_install.addFileArg(signed_apk);
adb_install.step.dependOn(&assemble.step);
const adb_step = b.step("adb-install", "Install APK on connected device");
adb_step.dependOn(&adb_install.step);
}
// --- Run step (desktop only) ---
if (!is_android) {
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the threez CLI");
run_step.dependOn(&run_cmd.step);
}
// --- Embed API check (desktop only) ---
if (!is_android) {
const embed_check_mod = b.createModule(.{
.root_source_file = b.path("examples/embed_api_check.zig"),
.target = target,
.optimize = optimize,
.strip = strip_for_windows,
});
embed_check_mod.addImport("threez", lib_mod);
const embed_check_exe = b.addExecutable(.{
.name = "threez-embed-check",
.root_module = embed_check_mod,
.use_llvm = true,
.use_lld = use_lld,
});
embed_check_exe.linkLibrary(qjs_lib);
embed_check_exe.linkLibrary(zglfw_dep.?.artifact("glfw"));
linkDawnConsumer(b, embed_check_exe, target, dawn);
const run_embed_check = b.addRunArtifact(embed_check_exe);
const embed_check_step = b.step("embed-check", "Run @embedFile library API check");
embed_check_step.dependOn(&run_embed_check.step);
}
// --- Tests (desktop only — can't run Android tests on host) ---
if (!is_android) {
const zglfw_mod = zglfw_dep.?.module("root");
const lib_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.strip = strip_for_windows,
.imports = &.{
.{ .name = "quickjs", .module = qjs_mod },
.{ .name = "zglfw", .module = zglfw_mod },
},
}),
.use_llvm = true,
.use_lld = use_lld,
});
lib_unit_tests.linkLibrary(qjs_lib);
linkDawnConsumer(b, lib_unit_tests, target, dawn);
lib_unit_tests.linkLibrary(zglfw_dep.?.artifact("glfw"));
// Add miniaudio for audio bridge tests
const miniaudio_dep = b.dependency("miniaudio", .{});
lib_unit_tests.addIncludePath(miniaudio_dep.path(""));
lib_unit_tests.addCSourceFile(.{
.file = miniaudio_dep.path("miniaudio.c"),
.flags = &.{"-std=c99"},
});
// Platform-specific linking for miniaudio
if (target.result.os.tag == .linux and !is_android) {
lib_unit_tests.linkSystemLibrary("pthread");
lib_unit_tests.linkSystemLibrary("m");
lib_unit_tests.linkSystemLibrary("dl");
} else if (target.result.os.tag == .macos) {
lib_unit_tests.linkFramework("CoreAudio");
lib_unit_tests.linkFramework("AudioToolbox");
}
// Add lazy dependencies to the test module so tests can use them
if (b.lazyDependency("zignal", .{})) |zignal_dep| {
lib_unit_tests.root_module.addImport("zignal", zignal_dep.module("zignal"));
}
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
// main.zig has no unit tests but must still compile during `zig build test`.
// It imports quickjs, zglfw, zgpu, so we give the test module the same deps.
const exe_unit_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.strip = strip_for_windows,
.imports = &.{
.{ .name = "quickjs", .module = qjs_mod },
.{ .name = "zglfw", .module = zglfw_mod },
},
}),
.use_llvm = true,
.use_lld = use_lld,
});
exe_unit_tests.linkLibrary(qjs_lib);
// Add lazy dependencies to exe test module
if (b.lazyDependency("clap", .{})) |clap_dep| {
exe_unit_tests.root_module.addImport("clap", clap_dep.module("clap"));
}
linkDawnConsumer(b, exe_unit_tests, target, dawn);
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_exe_unit_tests.step);
}
}
fn addDawnMriTool(b: *std.Build) *std.Build.Step.Compile {
const wf = b.addWriteFiles();
const src = wf.add("dawn-mri-tool.zig",
\\const std = @import("std");
\\
\\fn collectArchives(
\\ allocator: std.mem.Allocator,
\\ build_dir: []const u8,
\\ libs: *std.array_list.Managed([]const u8),
\\) !void {
\\ var dir = try std.fs.cwd().openDir(build_dir, .{ .iterate = true });
\\ defer dir.close();
\\ var walker = try dir.walk(allocator);
\\ defer walker.deinit();
\\ while (try walker.next()) |entry| {
\\ if (entry.kind != .file) continue;
\\ if (!std.mem.endsWith(u8, entry.path, ".a")) continue;
\\ if (std.mem.endsWith(u8, entry.path, "libdawn_proc.a")) continue;
\\ const full_path = try std.fs.path.join(allocator, &.{ build_dir, entry.path });
\\ try libs.append(full_path);
\\ }
\\}
\\
\\pub fn main() !void {
\\ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
\\ defer _ = gpa.deinit();
\\ const allocator = gpa.allocator();
\\
\\ var args = try std.process.argsWithAllocator(allocator);
\\ defer args.deinit();
\\ _ = args.next();
\\ const build_dir = args.next() orelse return error.InvalidArgs;
\\ const out_lib = args.next() orelse return error.InvalidArgs;
\\ const out_script = args.next() orelse return error.InvalidArgs;
\\ if (args.next() != null) return error.InvalidArgs;
\\
\\ const cwd_real = try std.fs.cwd().realpathAlloc(allocator, ".");
\\ defer allocator.free(cwd_real);
\\ const build_dir_abs = try std.fs.path.resolve(allocator, &.{ cwd_real, build_dir });
\\ defer allocator.free(build_dir_abs);
\\ const out_lib_abs = try std.fs.path.resolve(allocator, &.{ cwd_real, out_lib });
\\ defer allocator.free(out_lib_abs);
\\
\\ var libs = std.array_list.Managed([]const u8).init(allocator);
\\ defer {
\\ for (libs.items) |lib| allocator.free(lib);
\\ libs.deinit();
\\ }
\\ try collectArchives(allocator, build_dir_abs, &libs);
\\
\\ var script = std.array_list.Managed(u8).init(allocator);
\\ defer script.deinit();
\\ try script.writer().print("create {s}\n", .{out_lib_abs});
\\ for (libs.items) |lib| {
\\ try script.writer().print("addlib {s}\n", .{lib});
\\ }
\\ try script.writer().writeAll("save\nend\n");
\\
\\ const script_dir = std.fs.path.dirname(out_script) orelse ".";
\\ const lib_dir = std.fs.path.dirname(out_lib) orelse ".";
\\ try std.fs.cwd().makePath(script_dir);
\\ try std.fs.cwd().makePath(lib_dir);
\\ var file = try std.fs.cwd().createFile(out_script, .{ .truncate = true });
\\ defer file.close();
\\ try file.writeAll(script.items);
\\}
);
return b.addExecutable(.{
.name = "dawn-mri-tool",
.root_module = b.createModule(.{
.root_source_file = src,
.target = b.graph.host,
.optimize = .ReleaseSafe,
}),
});
}
fn addDawnPrepTool(b: *std.Build) *std.Build.Step.Compile {
const wf = b.addWriteFiles();
const src = wf.add("dawn-prep-tool.zig",
\\const std = @import("std");
\\
\\fn pathExists(path: []const u8) bool {
\\ std.fs.cwd().access(path, .{}) catch return false;
\\ return true;
\\}
\\
\\fn runChecked(allocator: std.mem.Allocator, cwd: ?[]const u8, argv: []const []const u8) !void {
\\ const result = try std.process.Child.run(.{
\\ .allocator = allocator,
\\ .argv = argv,
\\ .cwd = cwd,
\\ .max_output_bytes = 1024 * 1024,
\\ });
\\ defer allocator.free(result.stdout);
\\ defer allocator.free(result.stderr);
\\
\\ switch (result.term) {
\\ .Exited => |code| if (code == 0) return,
\\ else => {},
\\ }
\\
\\ if (result.stdout.len != 0) std.debug.print("{s}", .{result.stdout});
\\ if (result.stderr.len != 0) std.debug.print("{s}", .{result.stderr});
\\ return error.CommandFailed;
\\}
\\
\\fn captureStdout(allocator: std.mem.Allocator, cwd: ?[]const u8, argv: []const []const u8) ![]u8 {
\\ const result = try std.process.Child.run(.{
\\ .allocator = allocator,
\\ .argv = argv,
\\ .cwd = cwd,
\\ .max_output_bytes = 64 * 1024,
\\ });
\\ defer allocator.free(result.stderr);
\\
\\ switch (result.term) {
\\ .Exited => |code| if (code == 0) return result.stdout,
\\ else => {},
\\ }
\\
\\ allocator.free(result.stdout);
\\ return error.CommandFailed;
\\}
\\
\\fn findPython(allocator: std.mem.Allocator) ![]const u8 {
\\ const candidates = [_][]const u8{ "python3", "python" };
\\ for (candidates) |candidate| {
\\ const result = std.process.Child.run(.{
\\ .allocator = allocator,
\\ .argv = &.{ candidate, "--version" },
\\ .max_output_bytes = 256,
\\ }) catch continue;
\\ defer allocator.free(result.stdout);
\\ defer allocator.free(result.stderr);
\\
\\ switch (result.term) {
\\ .Exited => |code| if (code == 0) return candidate,
\\ else => {},
\\ }
\\ }
\\ return error.PythonNotFound;
\\}
\\
\\fn patchFile(
\\ allocator: std.mem.Allocator,
\\ path: []const u8,
\\ needle: []const u8,
\\ replacement: []const u8,
\\) !void {
\\ const cwd = std.fs.cwd();
\\ const original = try cwd.readFileAlloc(allocator, path, 1024 * 1024);
\\ defer allocator.free(original);
\\
\\ if (std.mem.indexOf(u8, original, replacement) != null) return;
\\ if (std.mem.indexOf(u8, original, needle) == null) return error.ExpectedTextNotFound;
\\
\\ const patched = try std.mem.replaceOwned(u8, allocator, original, needle, replacement);
\\ defer allocator.free(patched);
\\
\\ var file = try cwd.createFile(path, .{ .truncate = true });
\\ defer file.close();
\\ try file.writeAll(patched);
\\}
\\
\\pub fn main() !void {
\\ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
\\ defer _ = gpa.deinit();
\\ const allocator = gpa.allocator();
\\
\\ const args = try std.process.argsAlloc(allocator);
\\ defer std.process.argsFree(allocator, args);
\\ if (args.len != 7) return error.InvalidArgs;
\\
\\ const cache_root = args[1];
\\ const src_dir = args[2];
\\ const stamp_dir = args[3];
\\ const dawn_commit = args[4];
\\ const prep_stamp = args[5];
\\ const zig_lib_dir = args[6];
\\ const cwd = std.fs.cwd();
\\
\\ try cwd.makePath(cache_root);
\\ try cwd.makePath(stamp_dir);
\\
\\ const git_dir = try std.fmt.allocPrint(allocator, "{s}/.git", .{src_dir});
\\ defer allocator.free(git_dir);
\\ if (!pathExists(git_dir)) {
\\ cwd.deleteTree(src_dir) catch {};
\\ try runChecked(allocator, null, &.{
\\ "git",
\\ "clone",
\\ "--shallow-since=2023-06-28",
\\ "https://dawn.googlesource.com/dawn",
\\ src_dir,
\\ });
\\ }
\\
\\ const current_commit = captureStdout(allocator, null, &.{
\\ "git",
\\ "-C",
\\ src_dir,
\\ "rev-parse",
\\ "--short=12",
\\ "HEAD",
\\ }) catch "";
\\ defer if (current_commit.len != 0) allocator.free(current_commit);
\\ if (!std.mem.eql(u8, std.mem.trim(u8, current_commit, "\r\n"), dawn_commit)) {
\\ try runChecked(allocator, null, &.{
\\ "git",
\\ "-C",
\\ src_dir,
\\ "fetch",
\\ "--shallow-since=2023-06-28",
\\ "origin",
\\ });
\\ try runChecked(allocator, null, &.{
\\ "git",
\\ "-C",
\\ src_dir,
\\ "checkout",
\\ "--force",
\\ dawn_commit,
\\ });
\\ }
\\
\\ const d3d_error_h = try std.fmt.allocPrint(allocator, "{s}/src/dawn/native/d3d/D3DError.h", .{src_dir});
\\ defer allocator.free(d3d_error_h);
\\ try patchFile(
\\ allocator,
\\ d3d_error_h,
\\ "#include <winerror.h>",
\\ "#include <wtypesbase.h>\n#include <winerror.h>",
\\ );
\\
\\ const backend_d3d12_cpp = try std.fmt.allocPrint(allocator, "{s}/src/dawn/native/d3d12/BackendD3D12.cpp", .{src_dir});
\\ defer allocator.free(backend_d3d12_cpp);
\\ try patchFile(
\\ allocator,
\\ backend_d3d12_cpp,
\\ " if (instance->IsBeginCaptureOnStartupEnabled()) {\n ComPtr<IDXGraphicsAnalysis> graphicsAnalysis;\n if (GetFunctions()->dxgiGetDebugInterface1 != nullptr &&\n SUCCEEDED(GetFunctions()->dxgiGetDebugInterface1(0, IID_PPV_ARGS(&graphicsAnalysis)))) {\n graphicsAnalysis->BeginCapture();\n }\n }\n",
\\ " if (instance->IsBeginCaptureOnStartupEnabled()) {\n#if !defined(__MINGW32__)\n ComPtr<IDXGraphicsAnalysis> graphicsAnalysis;\n if (GetFunctions()->dxgiGetDebugInterface1 != nullptr &&\n SUCCEEDED(GetFunctions()->dxgiGetDebugInterface1(0, IID_PPV_ARGS(&graphicsAnalysis)))) {\n graphicsAnalysis->BeginCapture();\n }\n#endif\n }\n",
\\ );
\\
\\ const shared_buffer_memory_d3d12_cpp = try std.fmt.allocPrint(allocator, "{s}/src/dawn/native/d3d12/SharedBufferMemoryD3D12.cpp", .{src_dir});
\\ defer allocator.free(shared_buffer_memory_d3d12_cpp);
\\ try patchFile(
\\ allocator,
\\ shared_buffer_memory_d3d12_cpp,
\\ " ComPtr<ID3D12Device> resourceDevice;\n d3d12Resource->GetDevice(__uuidof(resourceDevice), &resourceDevice);\n",
\\ " ComPtr<ID3D12Device> resourceDevice;\n d3d12Resource->GetDevice(IID_PPV_ARGS(resourceDevice.GetAddressOf()));\n",
\\ );
\\
\\ const shared_fence_d3d12_cpp = try std.fmt.allocPrint(allocator, "{s}/src/dawn/native/d3d12/SharedFenceD3D12.cpp", .{src_dir});
\\ defer allocator.free(shared_fence_d3d12_cpp);
\\ try patchFile(
\\ allocator,
\\ shared_fence_d3d12_cpp,
\\ " const auto& queueFence = ToBackend(device->GetQueue())->GetSharedFence();\n if (queueFence &&\n ::CompareObjectHandles(queueFence->GetSystemHandle().Get(), descriptor->handle)) {\n return queueFence;\n }\n",
\\ " const auto& queueFence = ToBackend(device->GetQueue())->GetSharedFence();\n#if defined(__MINGW32__)\n if (queueFence && queueFence->GetSystemHandle().Get() == descriptor->handle) {\n return queueFence;\n }\n#else\n if (queueFence &&\n ::CompareObjectHandles(queueFence->GetSystemHandle().Get(), descriptor->handle)) {\n return queueFence;\n }\n#endif\n",
\\ );
\\
\\ if (zig_lib_dir.len != 0) {
\\ const compat_dir = try std.fmt.allocPrint(allocator, "{s}/build_overrides/windows-gnu", .{src_dir});
\\ defer allocator.free(compat_dir);
\\ try cwd.makePath(compat_dir);
\\ const corecrt_h = try std.fmt.allocPrint(allocator, "{s}/corecrt.h", .{compat_dir});
\\ defer allocator.free(corecrt_h);
\\ const mingw_corecrt = try std.fmt.allocPrint(allocator, "{s}/libc/include/any-windows-any/corecrt.h", .{zig_lib_dir});
\\ defer allocator.free(mingw_corecrt);
\\ var corecrt_file = try cwd.createFile(corecrt_h, .{ .truncate = true });
\\ defer corecrt_file.close();
\\ const corecrt_prefix = try std.fmt.allocPrint(allocator, "#pragma once\n#include \"{s}\"\n", .{mingw_corecrt});
\\ defer allocator.free(corecrt_prefix);
\\ try corecrt_file.writeAll(corecrt_prefix);
\\ try corecrt_file.writeAll(
\\ "#ifndef _UCRT_DISABLED_WARNINGS\n#define _UCRT_DISABLED_WARNINGS\n#endif\n"
\\ ++ "#ifndef _UCRT_DISABLE_CLANG_WARNINGS\n"
\\ ++ "#ifdef __clang__\n"
\\ ++ "#define _UCRT_DISABLE_CLANG_WARNINGS _Pragma(\"clang diagnostic push\") _Pragma(\"clang diagnostic ignored \\\"-Wdeprecated-declarations\\\"\") _Pragma(\"clang diagnostic ignored \\\"-Wignored-attributes\\\"\") _Pragma(\"clang diagnostic ignored \\\"-Wignored-pragma-optimize\\\"\") _Pragma(\"clang diagnostic ignored \\\"-Wunknown-pragmas\\\"\")\n"
\\ ++ "#else\n#define _UCRT_DISABLE_CLANG_WARNINGS\n#endif\n#endif\n"
\\ ++ "#ifndef _UCRT_RESTORE_CLANG_WARNINGS\n#ifdef __clang__\n#define _UCRT_RESTORE_CLANG_WARNINGS _Pragma(\"clang diagnostic pop\")\n#else\n#define _UCRT_RESTORE_CLANG_WARNINGS\n#endif\n#endif\n"
\\ ++ "#ifndef _CRT_BEGIN_C_HEADER\n#ifdef __cplusplus\n#define _CRT_BEGIN_C_HEADER extern \"C\" {\n#define _CRT_END_C_HEADER }\n#else\n#define _CRT_BEGIN_C_HEADER\n#define _CRT_END_C_HEADER\n#endif\n#endif\n"
\\ ++ "#ifndef _ACRTIMP\n#define _ACRTIMP\n#endif\n"
\\ ++ "#ifndef _VCRTIMP\n#define _VCRTIMP\n#endif\n"
\\ ++ "#ifndef _CRTIMP\n#define _CRTIMP\n#endif\n"
\\ ++ "#ifndef _CRT_INSECURE_DEPRECATE\n#define _CRT_INSECURE_DEPRECATE(_Replacement)\n#endif\n"
\\ ++ "#ifndef _CRT_INSECURE_DEPRECATE_MEMORY\n#define _CRT_INSECURE_DEPRECATE_MEMORY(_Replacement)\n#endif\n"
\\ ++ "#ifndef _CRT_DEPRECATE_TEXT\n#define _CRT_DEPRECATE_TEXT(_Text)\n#endif\n"
\\ ++ "#ifndef _CRT_MANAGED_FP_DEPRECATE\n#define _CRT_MANAGED_FP_DEPRECATE\n#endif\n"
\\ ++ "#ifndef _NODISCARD\n#define _NODISCARD [[nodiscard]]\n#endif\n"
\\ ++ "#ifndef _CONST_RETURN\n#define _CONST_RETURN\n#endif\n"
\\ ++ "#ifndef _Check_return_\n#define _Check_return_\n#endif\n"
\\ ++ "#ifndef _Check_return_opt_\n#define _Check_return_opt_\n#endif\n"
\\ ++ "#ifndef _Check_return_wat_\n#define _Check_return_wat_\n#endif\n"
\\ ++ "#ifndef _Success_\n#define _Success_(_Expr)\n#endif\n"
\\ ++ "#ifndef _In_\n#define _In_\n#endif\n"
\\ ++ "#ifndef _In_opt_\n#define _In_opt_\n#endif\n"
\\ ++ "#ifndef _In_z_\n#define _In_z_\n#endif\n"
\\ ++ "#ifndef _In_opt_z_\n#define _In_opt_z_\n#endif\n"
\\ ++ "#ifndef _In_reads_\n#define _In_reads_(_Size)\n#endif\n"
\\ ++ "#ifndef _In_reads_opt_\n#define _In_reads_opt_(_Size)\n#endif\n"
\\ ++ "#ifndef _In_reads_bytes_\n#define _In_reads_bytes_(_Size)\n#endif\n"
\\ ++ "#ifndef _In_reads_bytes_opt_\n#define _In_reads_bytes_opt_(_Size)\n#endif\n"
\\ ++ "#ifndef _Out_\n#define _Out_\n#endif\n"
\\ ++ "#ifndef _Out_opt_\n#define _Out_opt_\n#endif\n"
\\ ++ "#ifndef _Out_writes_\n#define _Out_writes_(_Size)\n#endif\n"
\\ ++ "#ifndef _Out_writes_opt_\n#define _Out_writes_opt_(_Size)\n#endif\n"
\\ ++ "#ifndef _Out_writes_bytes_\n#define _Out_writes_bytes_(_Size)\n#endif\n"
\\ ++ "#ifndef _Out_writes_bytes_opt_\n#define _Out_writes_bytes_opt_(_Size)\n#endif\n"
\\ ++ "#ifndef _Out_writes_bytes_all_\n#define _Out_writes_bytes_all_(_Size)\n#endif\n"
\\ ++ "#ifndef _Out_writes_bytes_all_opt_\n#define _Out_writes_bytes_all_opt_(_Size)\n#endif\n"
\\ ++ "#ifndef _Out_writes_bytes_to_opt_\n#define _Out_writes_bytes_to_opt_(_A,_B)\n#endif\n"
\\ ++ "#ifndef _Ret_maybenull_\n#define _Ret_maybenull_\n#endif\n"
\\ ++ "#ifndef _Ret_notnull_\n#define _Ret_notnull_\n#endif\n"
\\ ++ "#ifndef _Ret_range_\n#define _Ret_range_(_A,_B)\n#endif\n"
\\ ++ "#ifndef _Post_equal_to_\n#define _Post_equal_to_(_Value)\n#endif\n"
\\ ++ "#ifndef _When_\n#define _When_(_Cond,_Ann)\n#endif\n"
\\ ++ "#ifndef _At_buffer_\n#define _At_buffer_(_Buf,_Iter,_Size,_Ann)\n#endif\n"
\\ ++ "#ifndef _Post_satisfies_\n#define _Post_satisfies_(_Expr)\n#endif\n"
\\ ++ "#ifndef _String_length_\n#define _String_length_(_Str)\n#endif\n"
\\ ++ "#ifndef _Iter_\n#define _Iter_\n#endif\n",
\\ );
\\ const float_h = try std.fmt.allocPrint(allocator, "{s}/float.h", .{compat_dir});
\\ defer allocator.free(float_h);
\\ const mingw_float = try std.fmt.allocPrint(allocator, "{s}/libc/include/any-windows-any/float.h", .{zig_lib_dir});
\\ defer allocator.free(mingw_float);
\\ var float_file = try cwd.createFile(float_h, .{ .truncate = true });
\\ defer float_file.close();
\\ const float_prefix = try std.fmt.allocPrint(allocator, "#pragma once\n#include \"corecrt.h\"\n#include \"{s}\"\n", .{mingw_float});
\\ defer allocator.free(float_prefix);
\\ try float_file.writeAll(float_prefix);
\\ const vcruntime_string_h = try std.fmt.allocPrint(allocator, "{s}/vcruntime_string.h", .{compat_dir});
\\ defer allocator.free(vcruntime_string_h);
\\ var compat_file = try cwd.createFile(vcruntime_string_h, .{ .truncate = true });
\\ defer compat_file.close();
\\ try compat_file.writeAll("#pragma once\n#include <string.h>\n#include <wchar.h>\n");
\\ }
\\
\\ if (!pathExists(prep_stamp)) {
\\ const python = try findPython(allocator);
\\ try runChecked(allocator, src_dir, &.{ python, "tools/fetch_dawn_dependencies.py", "--shallow" });
\\ if (std.fs.path.dirname(prep_stamp)) |prep_dir| {
\\ try cwd.makePath(prep_dir);
\\ }
\\ var stamp = try cwd.createFile(prep_stamp, .{ .truncate = true });
\\ defer stamp.close();
\\ try stamp.writeAll(dawn_commit);
\\ }
\\}
);
return b.addExecutable(.{
.name = "dawn-prep-tool",
.root_module = b.createModule(.{
.root_source_file = src,
.target = b.graph.host,
.optimize = .ReleaseSafe,
}),
});
}
fn addNativeDawnBuild(
b: *std.Build,
target: std.Build.ResolvedTarget,
dawn_mri_tool: *std.Build.Step.Compile,
dawn_prep_tool: *std.Build.Step.Compile,
android_ndk_root: []const u8,
) DawnBuild {
const dawn_commit = "03e999815027";
const use_host_toolchain = shouldUseHostDawnToolchain(b, target);
const dawn_build_flavor = if (target.result.os.tag == .linux and target.result.abi == .android)
"android-ndk"
else if (use_host_toolchain)
"hostcc"
else
"zigcc";
const cache_root = ".zig-cache/dawn";
const project_root = std.fs.cwd().realpathAlloc(b.allocator, ".") catch @panic("failed to resolve project root");
const target_key = dawnTargetKey(b, target);
const src_dir = b.fmt("{s}/src-{s}", .{ cache_root, dawn_commit });
const include_dir = b.fmt("{s}/include", .{src_dir});
const stamp_dir = b.fmt("{s}/stamps", .{cache_root});
const build_dir = b.fmt("{s}/build-{s}-{s}-{s}", .{ cache_root, dawn_commit, dawn_build_flavor, target_key });
const out_dir = b.fmt("{s}/out/{s}-{s}", .{ cache_root, dawn_build_flavor, target_key });
const out_lib = b.fmt("{s}/libdawn.a", .{out_dir});
const out_lib_abs = b.fmt("{s}/{s}", .{ project_root, out_lib });
const gen_include_dir = b.fmt("{s}/gen/include", .{build_dir});
const prep_stamp = b.fmt("{s}/fetched-{s}", .{ stamp_dir, dawn_commit });
const windows_host_toolchain = if (target.result.os.tag == .windows and use_host_toolchain)
detectWindowsHostToolchain(b) orelse @panic("missing Windows host Dawn toolchain: install VS Build Tools LLVM")
else
null;
const prepare = b.addRunArtifact(dawn_prep_tool);
prepare.addArgs(&.{ cache_root, src_dir, stamp_dir, dawn_commit, prep_stamp, if (windows_host_toolchain) |toolchain| windowsPathForCMake(b, toolchain.zig_lib_dir) else "" });
prepare.has_side_effects = true;
const jobs = std.Thread.getCpuCount() catch 8;
var cmake_args = std.array_list.Managed([]const u8).init(b.allocator);
const windows_sdk = if (target.result.os.tag == .windows and b.graph.host.result.os.tag == .windows)
detectWindowsSdkInfo(b)
else
null;
const cmake_build_type = if (target.result.os.tag == .windows)
"-DCMAKE_BUILD_TYPE=RelWithDebInfo"
else
"-DCMAKE_BUILD_TYPE=Release";
cmake_args.appendSlice(&.{
"cmake",
"-S",
src_dir,
"-B",
build_dir,
cmake_build_type,
"-DCMAKE_CXX_FLAGS=-Wno-switch-default -Wno-unsafe-buffer-usage",
"-DDAWN_SUPPORTS_GLFW_FOR_WINDOWING=OFF",
"-DDAWN_USE_GLFW=OFF",
"-DDAWN_BUILD_SAMPLES=OFF",
"-DDAWN_BUILD_TESTS=OFF",
"-DTINT_BUILD_TESTS=OFF",
"-DTINT_BUILD_CMD_TOOLS=OFF",
"-DTINT_BUILD_GLSL_WRITER=OFF",
"-DTINT_BUILD_HLSL_WRITER=OFF",
"-DTINT_BUILD_MSL_WRITER=OFF",
"-DTINT_BUILD_BENCHMARKS=OFF",
"-DDAWN_BUILD_BENCHMARKS=OFF",
"-DDAWN_ENABLE_NULL=OFF",
"-DDAWN_ENABLE_DESKTOP_GL=OFF",
"-DDAWN_ENABLE_OPENGLES=OFF",
"-G",
"Ninja",
}) catch @panic("OOM");
if (target.result.os.tag == .linux and target.result.abi == .android) {
cmake_args.appendSlice(&.{
b.fmt("-DCMAKE_TOOLCHAIN_FILE={s}/build/cmake/android.toolchain.cmake", .{android_ndk_root}),
b.fmt("-DANDROID_ABI={s}", .{if (target.result.cpu.arch.isAARCH64()) "arm64-v8a" else "x86_64"}),
"-DANDROID_PLATFORM=android-26",
}) catch @panic("OOM");
} else if (windows_host_toolchain) |toolchain| {
cmake_args.appendSlice(&.{
b.fmt("-DCMAKE_C_COMPILER={s}", .{windowsPathForCMake(b, toolchain.clang)}),
b.fmt("-DCMAKE_CXX_COMPILER={s}", .{windowsPathForCMake(b, toolchain.clangxx)}),
b.fmt("-DCMAKE_AR={s}", .{windowsPathForCMake(b, toolchain.llvm_ar)}),
"-DCMAKE_TRY_COMPILE_TARGET_TYPE=STATIC_LIBRARY",
b.fmt("-DCMAKE_C_COMPILER_TARGET={s}", .{windowsClangTarget(target)}),
b.fmt("-DCMAKE_CXX_COMPILER_TARGET={s}", .{windowsClangTarget(target)}),
}) catch @panic("OOM");
} else if (use_host_toolchain) {
cmake_args.appendSlice(&.{
"-DCMAKE_C_COMPILER=cc",
"-DCMAKE_CXX_COMPILER=c++",
}) catch @panic("OOM");
} else {
cmake_args.appendSlice(&.{
b.fmt("-DCMAKE_C_COMPILER={s}", .{b.graph.zig_exe}),
b.fmt("-DCMAKE_CXX_COMPILER={s}", .{b.graph.zig_exe}),
"-DCMAKE_C_COMPILER_ARG1=cc",
"-DCMAKE_CXX_COMPILER_ARG1=c++",
b.fmt("-DCMAKE_C_COMPILER_TARGET={s}", .{target_key}),
b.fmt("-DCMAKE_CXX_COMPILER_TARGET={s}", .{target_key}),
}) catch @panic("OOM");
}
if (target.result.os.tag == .linux and target.result.abi == .android) {
cmake_args.appendSlice(&.{
"-DDAWN_ENABLE_VULKAN=ON",
"-DDAWN_ENABLE_D3D11=OFF",
"-DDAWN_ENABLE_D3D12=OFF",
"-DDAWN_ENABLE_METAL=OFF",
"-DDAWN_USE_X11=OFF",
"-DDAWN_USE_WAYLAND=OFF",
}) catch @panic("OOM");
} else switch (target.result.os.tag) {
.linux => blk: {
requireLinuxDawnDeps();
cmake_args.appendSlice(&.{
"-DDAWN_ENABLE_VULKAN=ON",
"-DDAWN_ENABLE_D3D11=OFF",
"-DDAWN_ENABLE_D3D12=OFF",
"-DDAWN_ENABLE_METAL=OFF",
"-DDAWN_USE_X11=ON",
"-DDAWN_USE_WAYLAND=OFF",
"-DCMAKE_INCLUDE_PATH=/usr/include",
b.fmt("-DCMAKE_LIBRARY_PATH={s};/lib/{s}", .{
linuxSystemLibDir(target),
linuxSystemLibTriplet(target),
}),
}) catch @panic("OOM");
break :blk;
},
.windows => cmake_args.appendSlice(&.{
"-DCMAKE_SYSTEM_NAME=Windows",
b.fmt("-DCMAKE_SYSTEM_VERSION={s}", .{if (windows_sdk) |sdk| sdk.version else "10"}),
b.fmt("-DCMAKE_VS_WINDOWS_TARGET_PLATFORM_VERSION={s}", .{if (windows_sdk) |sdk| sdk.version else "10"}),
b.fmt("-DCMAKE_SYSTEM_PROCESSOR={s}", .{if (target.result.cpu.arch.isX86()) "x86_64" else "aarch64"}),
"-DWIN32=TRUE",
"-DUNIX=FALSE",
"-DAPPLE=FALSE",
"-DDAWN_ENABLE_VULKAN=OFF",
"-DDAWN_ENABLE_D3D11=OFF",
"-DDAWN_ENABLE_D3D12=ON",
"-DTINT_BUILD_HLSL_WRITER=ON",
"-DDAWN_ENABLE_METAL=OFF",
"-DDAWN_FORCE_SYSTEM_COMPONENT_LOAD=ON",
"-DDAWN_USE_WINDOWS_UI=OFF",
"-DDAWN_USE_X11=OFF",
"-DDAWN_USE_WAYLAND=OFF",
}) catch @panic("OOM"),
.macos => cmake_args.appendSlice(&.{
"-DDAWN_ENABLE_VULKAN=OFF",
"-DDAWN_ENABLE_D3D11=OFF",
"-DDAWN_ENABLE_D3D12=OFF",
"-DDAWN_ENABLE_METAL=ON",
"-DDAWN_USE_X11=OFF",
"-DDAWN_USE_WAYLAND=OFF",
}) catch @panic("OOM"),
else => @panic("unsupported Dawn target"),
}
if (windows_sdk) |sdk| {
const sdk_root = windowsPathForCMake(b, sdk.path);
const compat_include_flags = if (windows_host_toolchain != null)
b.fmt("-isystem \"{s}\"", .{windowsPathForCMake(b, b.fmt("{s}/{s}/build_overrides/windows-gnu", .{ project_root, src_dir }))})
else
"";
const after_include_flags = b.fmt("-isystem \"{s}/Include/{s}/ucrt\" -isystem \"{s}/Include/{s}/shared\" -isystem \"{s}/Include/{s}/um\"", .{
sdk_root, sdk.version,
sdk_root, sdk.version,
sdk_root, sdk.version,
});
const lib_arch = if (target.result.cpu.arch.isX86()) "x64" else "arm64";
const library_dirs = b.fmt("{s}/Lib/{s}/ucrt/{s};{s}/Lib/{s}/um/{s}", .{
sdk_root, sdk.version, lib_arch,
sdk_root, sdk.version, lib_arch,
});
const c_flags = if (windows_host_toolchain) |toolchain|
b.fmt("{s} {s} {s}", .{ windowsZigCFlags(b, target, toolchain), compat_include_flags, after_include_flags })
else
after_include_flags;
const cxx_flags = if (windows_host_toolchain) |toolchain|
b.fmt("-Wno-switch-default -Wno-unsafe-buffer-usage {s} {s} {s}", .{ windowsZigCxxFlags(b, target, toolchain), compat_include_flags, after_include_flags })
else
b.fmt("-Wno-switch-default -Wno-unsafe-buffer-usage {s}", .{after_include_flags});
cmake_args.appendSlice(&.{
b.fmt("-DCMAKE_C_FLAGS={s}", .{c_flags}),
b.fmt("-DCMAKE_CXX_FLAGS={s}", .{cxx_flags}),
b.fmt("-DCMAKE_LIBRARY_PATH={s}", .{library_dirs}),
}) catch @panic("OOM");
}
const configure_stamp = b.fmt("{s}/configured-{s}-{s}", .{ stamp_dir, dawn_commit, target_key });
// Create a configure check tool that runs cmake when needed
const configure_check_exe = b.addExecutable(.{
.name = "dawn-configure-check",
.root_module = b.createModule(.{
.root_source_file = b.path("build/dawn_configure_check.zig"),
.target = b.graph.host,
.optimize = .ReleaseSafe,
}),
});
// Create a configure step that checks if reconfiguration is needed and runs cmake if so
const configure_check = b.addRunArtifact(configure_check_exe);
configure_check.addArgs(&.{ src_dir, build_dir, configure_stamp });
// Pass all the cmake args to the configure tool so it can run cmake when needed
for (cmake_args.items) |arg| {
configure_check.addArg(arg);
}
// Add file dependencies for key CMake files
const cmake_files = [_][]const u8{
"CMakeLists.txt",
"cmake/Config.cmake",
"cmake/DawnDependencies.cmake",
"cmake/DawnUtils.cmake",
"cmake/TintDependencies.cmake",
};
for (cmake_files) |cmake_file| {
const cmake_path = b.fmt("{s}/{s}", .{ src_dir, cmake_file });
configure_check.addFileInput(.{ .cwd_relative = cmake_path });
}
configure_check.has_side_effects = true;
configure_check.step.dependOn(&prepare.step);
const build_dawn = b.addSystemCommand(&.{
"cmake",
"--build",
build_dir,