-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.zig
1394 lines (1321 loc) · 43.7 KB
/
build.zig
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");
fn addTool(b: *std.Build, dep: *std.Build.Dependency, tool_name: []const u8, files: []const []const u8) *std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = tool_name,
.target = b.resolveTargetQuery(.{}), // native
.optimize = .ReleaseSafe,
});
exe.linkLibC();
exe.linkLibCpp();
exe.addCSourceFiles(.{
.root = dep.path(""),
.files = files,
.flags = &.{
b.fmt("-DPACKAGE_STRING=\"{s}\"", .{tool_name}),
"-D__DATE__=\"disabled\"",
"-D__TIME__=\"disabled\"",
// "-fno-sanitize=undefined",
},
});
exe.addIncludePath(dep.path(""));
const insa = b.addInstallArtifact(exe, .{
.dest_dir = .{ .override = .{ .custom = "3dstools" } },
});
b.getInstallStep().dependOn(&insa.step);
return exe;
}
const AnyPtr = struct {
id: [*]const u8,
val: *const anyopaque,
};
fn exposeArbitrary(b: *std.Build, name: []const u8, comptime ty: type, val: *const ty) void {
const valv = b.allocator.create(AnyPtr) catch @panic("oom");
valv.* = .{
.id = @typeName(ty),
.val = val,
};
const name_fmt = b.fmt("__exposearbitrary_{s}", .{name});
const mod = b.addModule(name_fmt, .{});
// HACKHACKHACK
mod.* = undefined;
mod.owner = @ptrCast(@alignCast(@constCast(valv)));
}
fn findArbitrary(dep: *std.Build.Dependency, comptime ty: type, name: []const u8) *const ty {
const name_fmt = dep.builder.fmt("__exposearbitrary_{s}", .{name});
const modv = dep.module(name_fmt);
// HACKHACKHACK
const anyptr: *const AnyPtr = @ptrCast(@alignCast(modv.owner));
std.debug.assert(anyptr.id == @typeName(ty));
return @ptrCast(@alignCast(anyptr.val));
}
pub const CIncluder = struct {
//! addStaticLibrary + linkLibrary() has this functionality already
//! however, it doesn't support define_macros, which we need for libc.
//! also, it probably doesn't support merging headers from a few
//! different folders, which we also need for libc.
//!
//! zig also limits it so all headers in one build.zig need to output
//! to the same directory, but we're exporting multiple seperate
//! packages with seperate headers, and they need to stay seperate.
const DefineMacro = struct { []const u8, ?[]const u8 };
owner: *std.Build,
define_macros: []const DefineMacro,
add_include_paths: []const std.Build.LazyPath,
pub const Options = struct {
define_macros: []const DefineMacro = &.{},
add_include_paths: []const std.Build.LazyPath = &.{},
};
pub fn createCIncluder(b: *std.Build, options: Options) *CIncluder {
const ci = b.allocator.create(CIncluder) catch @panic("oom");
ci.* = .{
.owner = b,
.define_macros = b.allocator.dupe(DefineMacro, options.define_macros) catch @panic("oom"),
.add_include_paths = b.allocator.dupe(std.Build.LazyPath, options.add_include_paths) catch @panic("oom"),
};
return ci;
}
pub fn expose(self: *const CIncluder, name: []const u8) void {
exposeArbitrary(self.owner, name, CIncluder, self);
}
pub fn find(dep: *std.Build.Dependency, name: []const u8) *const CIncluder {
return findArbitrary(dep, CIncluder, name);
}
pub fn applyTo(self: *const CIncluder, mod: *std.Build.Module) void {
for (self.define_macros) |m| mod.addCMacro(m[0], m[1] orelse "1");
for (self.add_include_paths) |ip| mod.addIncludePath(ip);
}
};
pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{});
const target_3ds = b.resolveTargetQuery(std.Target.Query.parse(.{
.arch_os_abi = "arm-freestanding-gnueabihf",
.cpu_features = "mpcore",
}) catch @panic("bad target"));
// 1. we need 3dstools
const @"3dstools_dep" = b.dependency("3dstools", .{});
const @"3dstools_cxitool_dep" = b.dependency("3dstools-cxitool", .{});
const general_tools_dep = b.dependency("general-tools", .{});
const picasso_dep = b.dependency("picasso", .{});
// elf -> .3dsx
const tool_3dsxtool = addTool(b, @"3dstools_dep", "3dsxtool", &[_][]const u8{
"src/3dsxtool.cpp",
"src/romfs.cpp",
});
// .3dsx -> .cia
_ = addTool(b, @"3dstools_cxitool_dep", "cxitool", &[_][]const u8{
"src/cxitool.cpp",
"src/CxiBuilder.cpp",
"src/CxiSettings.cpp",
"src/CxiExeFS.cpp",
"src/CxiRomFS.cpp",
"src/blz.c",
"src/3dsx_loader.cpp",
"src/crypto.cpp",
"src/polarssl/aes.c",
"src/polarssl/rsa.c",
"src/polarssl/sha1.c",
"src/polarssl/sha2.c",
"src/polarssl/base64.c",
"src/polarssl/bignum.c",
"src/YamlReader.cpp",
"src/libyaml/api.c",
"src/libyaml/dumper.c",
"src/libyaml/emitter.c",
"src/libyaml/loader.c",
"src/libyaml/parser.c",
"src/libyaml/reader.c",
"src/libyaml/scanner.c",
"src/libyaml/writer.c",
});
// .bin -> .s & .h
const bin2s_tool = addTool(b, general_tools_dep, "bin2s", &[_][]const u8{
"bin2s.c",
});
// .pica -> .shbin
const tool_picasso = addTool(b, picasso_dep, "picasso", &[_][]const u8{
"source/picasso_assembler.cpp",
"source/picasso_frontend.cpp",
});
const libctru_dep = b.dependency("libctru", .{});
const crtls_dep = b.dependency("devkitarm-crtls", .{});
const newlib_dep = b.dependency("newlib", .{});
const examples_dep = b.dependency("3ds-examples", .{});
// build_helper
const build_helper = try b.allocator.create(T3dsBuildHelper);
exposeArbitrary(b, "build_helper", T3dsBuildHelper, build_helper);
build_helper.* = .{
.owner = b,
.target = target_3ds,
.tool_3dsxtool = tool_3dsxtool,
.tool_bin2s = bin2s_tool,
.tool_picasso = tool_picasso,
.crtls_dep = crtls_dep,
.cflags = b.dupeStrings(&.{
"-mtp=soft",
"-fno-sanitize=undefined", // :/
"-Wno-return-type", // :/ :/ `newlib/libc/sys/arm/sys/lock.h:51:1`
}),
};
// newlib
const libc_includer = CIncluder.createCIncluder(b, .{
.define_macros = &.{
.{ "_LIBC", null },
.{ "__DYNAMIC_REENT__", null },
.{ "GETREENT_PROVIDED", null },
.{ "REENTRANT_SYSCALLS_PROVIDED", null },
.{ "__DEFAULT_UTF8__", null },
.{ "_LDBL_EQ_DBL", null },
.{ "_HAVE_INITFINI_ARRAY", null },
.{ "_MB_CAPABLE", null },
.{ "__3DS__", null },
},
.add_include_paths = &.{
newlib_dep.path("newlib/libc/sys/arm"),
newlib_dep.path("newlib/libc/machine/arm"),
newlib_dep.path("newlib/libc/include"),
},
});
libc_includer.expose("c");
// libgloss_libsysbase
const libgloss_libsysbase = b.addStaticLibrary(.{
.name = "sysbase",
.target = target_3ds,
.optimize = optimize,
});
libc_includer.applyTo(&libgloss_libsysbase.root_module);
libgloss_libsysbase.addIncludePath(b.path("src/config_fix"));
libgloss_libsysbase.defineCMacro("_BUILDING_LIBSYSBASE", null);
libgloss_libsysbase.addCSourceFiles(.{
.root = newlib_dep.path("libgloss/libsysbase"),
.files = libgloss_libsysbase_files,
.flags = build_helper.cflags,
});
// newlib (libc)
const libc = b.addStaticLibrary(.{
.name = "c",
.target = target_3ds,
.optimize = optimize,
});
libc_includer.applyTo(&libc.root_module);
libc.addCSourceFiles(.{
.root = newlib_dep.path("newlib/libc"),
.files = newlib_libc_files,
.flags = build_helper.cflags,
});
libc.addAssemblyFile(crtls_dep.path("3dsx_crt0.s"));
libc.linkLibrary(libgloss_libsysbase);
b.installArtifact(libc);
// libm
const libm = b.addStaticLibrary(.{
.name = "m",
.target = target_3ds,
.optimize = optimize,
});
libc_includer.applyTo(&libm.root_module);
libm.addCSourceFiles(.{
.root = newlib_dep.path("newlib/libm"),
.files = newlib_libm_files,
.flags = build_helper.cflags,
});
b.installArtifact(libm);
// libctru
const libctru_includer = CIncluder.createCIncluder(b, .{
.add_include_paths = &.{
b.path("src/asm_fix"),
libctru_dep.path("libctru/include"),
},
});
libctru_includer.expose("ctru");
const libctru = b.addStaticLibrary(.{
.name = "ctru",
.target = target_3ds,
.optimize = optimize,
});
b.installArtifact(libctru);
{
libc_includer.applyTo(&libctru.root_module);
libctru_includer.applyTo(&libctru.root_module);
build_helper.addDir(&libctru.root_module, libctru_dep.path("libctru"));
}
// citro3d
const citro3d_dep = b.dependency("citro3d", .{});
const citro3d_includer = CIncluder.createCIncluder(b, .{
.add_include_paths = &.{
citro3d_dep.path("include"),
},
});
citro3d_includer.expose("citro3d");
const citro3d = b.addStaticLibrary(.{
.name = "citro3d",
.target = target_3ds,
.optimize = optimize,
});
b.installArtifact(citro3d);
libc_includer.applyTo(&citro3d.root_module);
libctru_includer.applyTo(&citro3d.root_module);
citro3d_includer.applyTo(&citro3d.root_module);
build_helper.addDir(&citro3d.root_module, citro3d_dep.path(""));
// citro2d
const citro2d_dep = b.dependency("citro2d", .{});
const citro2d_includer = CIncluder.createCIncluder(b, .{
.add_include_paths = &.{
citro2d_dep.path("include"),
},
});
citro2d_includer.expose("citro2d");
const citro2d = b.addStaticLibrary(.{
.name = "citro2d",
.target = target_3ds,
.optimize = optimize,
});
b.installArtifact(citro2d);
libc_includer.applyTo(&citro2d.root_module);
libctru_includer.applyTo(&citro2d.root_module);
citro3d_includer.applyTo(&citro2d.root_module);
citro2d_includer.applyTo(&citro2d.root_module);
build_helper.addDir(&citro2d.root_module, citro2d_dep.path(""));
// 4. build the game
for (t3ds_examples) |example_t| {
const example = T3dsExample.from(example_t);
const example_name = example.name(b.allocator);
const elf = b.addExecutable(.{
.name = example_name,
.target = target_3ds,
.optimize = optimize,
});
build_helper.link(elf);
build_helper.addDir(&elf.root_module, examples_dep.path(example.root_dir));
if (example.dependencies.c) {
libc_includer.applyTo(&elf.root_module);
elf.linkLibrary(libc);
}
if (example.dependencies.m) {
elf.linkLibrary(libm);
}
if (example.dependencies.ctru) {
libctru_includer.applyTo(&elf.root_module);
elf.linkLibrary(libctru);
}
if (example.dependencies.citro3d) {
citro3d_includer.applyTo(&elf.root_module);
elf.linkLibrary(citro3d);
}
if (example.dependencies.citro2d) {
citro2d_includer.applyTo(&elf.root_module);
elf.linkLibrary(citro2d);
}
// elf -> 3dsx
const output_3dsx = build_helper.to3dsx(elf);
const output_3dsx_install = b.addInstallFileWithDir(output_3dsx, .bin, b.fmt("{s}.3dsx", .{example_name}));
const output_3dsx_path = b.getInstallPath(.bin, b.fmt("{s}.3dsx", .{example_name}));
b.getInstallStep().dependOn(&output_3dsx_install.step);
const run_step = std.Build.Step.Run.create(b, b.fmt("citra run:{s}", .{example_name}));
run_step.addArg("citra");
run_step.addArg(output_3dsx_path);
run_step.step.dependOn(b.getInstallStep());
const run_step_cmdl = b.step(b.fmt("run:{s}", .{example.root_dir}), b.fmt("Run {s}", .{example.root_dir}));
run_step_cmdl.dependOn(&run_step.step);
}
}
pub const T3dsBuildHelper = struct {
owner: *std.Build,
target: std.Build.ResolvedTarget,
tool_3dsxtool: *std.Build.Step.Compile,
tool_bin2s: *std.Build.Step.Compile,
tool_picasso: *std.Build.Step.Compile,
crtls_dep: *std.Build.Dependency,
cflags: []const []const u8,
pub fn find(dep: *std.Build.Dependency, name: []const u8) *const T3dsBuildHelper {
return findArbitrary(dep, T3dsBuildHelper, name);
}
pub fn link(bh: *const T3dsBuildHelper, elf: *std.Build.Step.Compile) void {
elf.linker_script = bh.crtls_dep.path("3dsx.ld"); // -T 3dsx.ld%s
elf.link_emit_relocs = true; // --emit-relocs
elf.root_module.strip = false; // can't combine 'strip-all' with 'emit-relocs'
// TODO: -d: They assign space to common symbols even if a relocatable output file is specified
// TODO: --use-blx: The ‘--use-blx’ switch enables the linker to use ARM/Thumb BLX instructions (available on ARMv5t and above) in various situations.
// skipped gc-sections because it seems to have no effect on ReleaseSmall builds
}
pub fn to3dsx(bh: *const T3dsBuildHelper, elf: *std.Build.Step.Compile) std.Build.LazyPath {
const b = elf.root_module.owner;
const output_3dsx_name = b.fmt("{s}.3dsx", .{elf.name});
const run_3dsxtool = b.addRunArtifact(bh.tool_3dsxtool);
run_3dsxtool.addFileArg(elf.getEmittedBin());
const output_3dsx = run_3dsxtool.addOutputFileArg(output_3dsx_name);
return output_3dsx;
}
pub const Bin2sCfg = struct {
sym_name: []const u8,
file_name: []const u8,
};
pub fn addBin2s(bh: *const T3dsBuildHelper, mod: *std.Build.Module, file: std.Build.LazyPath, cfg: Bin2sCfg) void {
const bin2s_run_cmd = bh.owner.addRunArtifact(bh.tool_bin2s);
bin2s_run_cmd.addArg("-H");
const h_file = bin2s_run_cmd.addOutputFileArg(bh.owner.fmt("{s}.h", .{cfg.file_name}));
bin2s_run_cmd.addArg("-n");
bin2s_run_cmd.addArg(cfg.sym_name);
bin2s_run_cmd.addFileArg(file);
const s_file = captureStdoutNamed(bin2s_run_cmd, bh.owner.fmt("{s}.s", .{cfg.file_name}));
mod.addIncludePath(h_file.dirname());
mod.addAssemblyFile(s_file);
}
pub fn addPica(bh: *const T3dsBuildHelper, mod: *std.Build.Module, v_pica: std.Build.LazyPath, g_pica: ?std.Build.LazyPath, name: []const u8) void {
const picasso_run_cmd = bh.owner.addRunArtifact(bh.tool_picasso);
picasso_run_cmd.addArg("-o");
const out_file = picasso_run_cmd.addOutputFileArg(bh.owner.fmt("{s}.shbin", .{name}));
picasso_run_cmd.addFileArg(v_pica);
if (g_pica) |gp| picasso_run_cmd.addFileArg(gp);
bh.addBin2s(mod, out_file, .{
.sym_name = bh.owner.fmt("{s}_shbin", .{name}),
.file_name = bh.owner.fmt("{s}_shbin", .{name}),
});
}
fn dotToUnderscore(str: []const u8, alloc: std.mem.Allocator) []const u8 {
var res = std.ArrayList(u8).init(alloc);
defer res.deinit();
for (str) |char| {
switch (char) {
'.' => res.append('_') catch @panic("oom"),
else => res.append(char) catch @panic("oom"),
}
}
return res.toOwnedSlice() catch @panic("oom");
}
pub fn addDir(bh: *const T3dsBuildHelper, mod: *std.Build.Module, dir: std.Build.LazyPath) void {
std.debug.assert(dir == .dependency);
const source_dir = dir.path(bh.owner, "source");
const data_dir = dir.path(bh.owner, "data");
const include_dir = dir.path(bh.owner, "include");
const gfx_dir = dir.path(bh.owner, "gfx");
const romfs_dir = dir.path(bh.owner, "romfs");
const source_files = readAll(bh.owner.allocator, source_dir.getPath(bh.owner));
const data_files = readAll(bh.owner.allocator, data_dir.getPath(bh.owner));
const gfx_files = readAll(bh.owner.allocator, gfx_dir.getPath(bh.owner));
const romfs_files = readAll(bh.owner.allocator, romfs_dir.getPath(bh.owner));
if (gfx_files.len > 0) std.debug.panic("TODO gfx_files for: {s} (requires tex3ds & has options)", .{dir.getPath(bh.owner)});
if (romfs_files.len > 0) std.debug.panic("TODO romfs files for: {s}", .{dir.getPath(bh.owner)});
bh.addFiles(mod, source_dir, source_files);
bh.addDataFiles(mod, data_dir, data_files);
mod.addIncludePath(include_dir);
}
fn readAll(alloc: std.mem.Allocator, path: []const u8) []const []const u8 {
var dir_target = std.fs.cwd().openDir(path, .{ .iterate = true }) catch return &.{};
defer dir_target.close();
var res_paths = std.ArrayList([]const u8).init(alloc);
defer res_paths.deinit();
var dir_walker = dir_target.walk(alloc) catch @panic("walk error");
defer dir_walker.deinit();
while (dir_walker.next() catch @panic("next error")) |entry| {
if (entry.kind != .file) continue;
if (std.mem.startsWith(u8, entry.basename, ".")) continue;
res_paths.append(alloc.dupe(u8, entry.path) catch @panic("oom")) catch @panic("oom");
}
// TODO sort
return res_paths.toOwnedSlice() catch @panic("oom");
}
// https://github.com/devkitPro/devkitarm-rules/blob/master/3ds_rules
pub fn addDataFiles(bh: *const T3dsBuildHelper, mod: *std.Build.Module, files_root: std.Build.LazyPath, files: []const []const u8) void {
for (files) |file| {
bh.addBin2s(mod, files_root.path(bh.owner, file), .{
.sym_name = dotToUnderscore(file, bh.owner.allocator),
.file_name = dotToUnderscore(file, bh.owner.allocator),
});
}
}
/// in the future, this could add a custom step that does a directory scan to discover all the files
pub fn addFiles(bh: *const T3dsBuildHelper, mod: *std.Build.Module, files_root: std.Build.LazyPath, files: []const []const u8) void {
const b = bh.owner;
var c_source_files = std.ArrayList([]const u8).init(bh.owner.allocator);
defer c_source_files.deinit();
var s_source_files = std.ArrayList([]const u8).init(bh.owner.allocator);
defer s_source_files.deinit();
for (files) |file| {
if (std.mem.endsWith(u8, file, ".c") or std.mem.endsWith(u8, file, ".cpp")) {
c_source_files.append(file) catch @panic("oom");
} else if (std.mem.endsWith(u8, file, ".v.pica")) {
// unfortunately, we have to check if there is an adjacent `.g.pica` file to
// call picasso with both
const v_pica = files_root.path(bh.owner, file);
const filebasename = bh.owner.dupe(std.fs.path.basename(file));
const fileflatname = filebasename[0 .. filebasename.len - ".v.pica".len];
const g_pica_lazypath = v_pica.dirname().path(bh.owner, bh.owner.fmt("{s}.g.pica", .{fileflatname}));
std.debug.assert(files_root == .dependency);
const g_pica_file_path = g_pica_lazypath.getPath(bh.owner);
const g_pica: ?std.Build.LazyPath = if (std.fs.accessAbsolute(g_pica_file_path, .{})) |_| blk: {
// @panic("has .g.pica file");
break :blk g_pica_lazypath;
} else |_| null;
bh.addPica(mod, v_pica, g_pica, fileflatname);
} else if (std.mem.endsWith(u8, file, ".h")) {
// nothing to do
} else if (std.mem.endsWith(u8, file, ".s")) {
s_source_files.append(file) catch @panic("oom");
} else if (std.mem.endsWith(u8, file, ".g.pica")) {
// handled in .v.pica
} else {
std.debug.panic("TODO ext: `{s}`", .{file});
}
}
if (s_source_files.items.len > 0) {
// '.s' files are renamed to '.S' before adding to the build
const asm_files_dir = b.addWriteFiles();
for (s_source_files.items) |file| {
const src_path = files_root.path(bh.owner, file);
const dst_name = b.fmt("{s}.S", .{file[0 .. file.len - ".s".len]});
const asm_file_lazypath = asm_files_dir.addCopyFile(src_path, dst_name);
mod.addAssemblyFile(asm_file_lazypath);
}
}
if (c_source_files.items.len > 0) mod.addCSourceFiles(.{
.root = files_root,
.files = c_source_files.items,
.flags = bh.cflags,
});
}
};
fn captureStdoutNamed(run: *std.Build.Step.Run, name: []const u8) std.Build.LazyPath {
std.debug.assert(run.stdio != .inherit);
std.debug.assert(run.captured_stdout == null);
const output = run.step.owner.allocator.create(std.Build.Step.Run.Output) catch @panic("OOM");
output.* = .{
.prefix = "",
.basename = name,
.generated_file = .{ .step = &run.step },
};
run.captured_stdout = output;
return .{ .generated = .{ .file = &output.generated_file } };
}
pub const T3dsDep = struct {
c: bool = false,
m: bool = false,
ctru: bool = false,
citro3d: bool = false,
citro2d: bool = false,
};
pub const T3dsExample = struct {
root_dir: []const u8,
dependencies: T3dsDep,
pub fn name(self: *const T3dsExample, alloc: std.mem.Allocator) []const u8 {
var res_al = std.ArrayList(u8).init(alloc);
defer res_al.deinit();
for (self.root_dir) |char| {
switch (char) {
'A'...'Z', 'a'...'z', '0'...'9', '-', '_' => res_al.append(char) catch @panic("oom"),
'/' => res_al.appendSlice("__") catch @panic("oom"),
else => res_al.append('_') catch @panic("oom"),
}
}
return res_al.toOwnedSlice() catch @panic("oom");
}
pub fn from(v: ExampleT) T3dsExample {
var res_dependencies = T3dsDep{};
for (v[1]) |dep| {
inline for (std.meta.fields(T3dsDep)) |field| {
if (dep == @field(T3dsDepEnum, field.name)) {
@field(res_dependencies, field.name) = true;
}
}
}
return .{
.root_dir = v[0],
.dependencies = res_dependencies,
};
}
};
const T3dsDepEnum = std.meta.FieldEnum(T3dsDep);
const ExampleTOpts = struct { gfx_mode: enum { not_set, embed, romfs } = .not_set };
const ExampleT = struct { []const u8, []const T3dsDepEnum, ExampleTOpts };
const t3ds_examples = &[_]ExampleT{
.{ "app_launch", &.{ .c, .m, .ctru }, .{} },
.{ "audio/filters", &.{ .c, .m, .ctru }, .{} },
.{ "audio/mic", &.{ .c, .m, .ctru }, .{} },
// .{ "audio/modplug-decoding", &.{ .c, .m, .ctru }, .{} }, // romfs
// .{ "audio/ogg-vorbis-decoding", &.{ .c, .m, .ctru }, .{} }, // romfs & -lvorbisidec
// .{ "audio/opus-decoding", &.{ .c, .m, .ctru }, .{} }, // romfs & -lopusfile
.{ "audio/streaming", &.{ .c, .m, .ctru }, .{} },
// .{ "camera/image", &.{ .c, .m, .ctru }, .{} }, // have xml files?
// .{ "camera/video", &.{ .c, .m, .ctru }, .{} }, // also missing setjmp/longjmp & 'clearScreen'
.{ "get_system_language", &.{ .c, .m, .ctru }, .{} },
// .{ "graphics/bitmap/24bit-color", &.{ .c, .m, .ctru, .citro3d, .citro2d }, .{ .gfx_include_png = true } },
.{ "graphics/gpu/2d_shapes", &.{ .c, .m, .ctru, .citro3d, .citro2d }, .{} },
.{ "graphics/gpu/both_screens", &.{ .c, .m, .ctru, .citro3d }, .{} },
.{ "graphics/gpu/composite_scene", &.{ .c, .m, .ctru, .citro3d, .citro2d }, .{} },
// .{ "graphics/gpu/cubemap", &.{ .c, .m, .ctru, .citro3d }, .{} }, // requires tex3ds
.{ "graphics/gpu/fragment_light", &.{ .c, .m, .ctru, .citro3d }, .{} },
.{ "graphics/gpu/geoshader", &.{ .c, .m, .ctru, .citro3d }, .{} },
// .{ "graphics/gpu/gpusprites", &.{ .c, .m, .ctru, .citro3d }, .{} }, // requires tex3ds
.{ "graphics/gpu/immediate", &.{ .c, .m, .ctru, .citro3d }, .{} },
.{ "graphics/gpu/lenny", &.{ .c, .m, .ctru, .citro3d }, .{} },
.{ "graphics/gpu/loop_subdivision", &.{ .c, .m, .ctru, .citro3d }, .{} },
// .{ "graphics/gpu/mipmap_fog", &.{ .c, .m, .ctru, .citro3d }, .{} }, // requires tex3ds
.{ "graphics/gpu/multiple_buf", &.{ .c, .m, .ctru, .citro3d }, .{} },
// .{ "graphics/gpu/normal_mapping", &.{ .c, .m, .ctru, .citro3d }, .{} }, // requires tex3ds
.{ "graphics/gpu/particles", &.{ .c, .m, .ctru, .citro3d }, .{} },
.{ "graphics/gpu/proctex", &.{ .c, .m, .ctru, .citro3d }, .{} },
.{ "graphics/gpu/simple_tri", &.{ .c, .m, .ctru, .citro3d }, .{} },
// .{ "graphics/gpu/stereoscopic_2d", &.{ .c, .m, .ctru, .citro3d, .citro2d }, .{} }, // requires tex3ds
// .{ "graphics/gpu/textured_cube", &.{ .c, .m, .ctru, .citro3d }, .{} }, // requires tex3ds
.{ "graphics/gpu/toon_shading", &.{ .c, .m, .ctru, .citro3d }, .{} },
.{ "graphics/gpu/wide_mode_3d", &.{ .c, .m, .ctru, .citro3d }, .{} },
.{ "graphics/printing/both-screen-text", &.{ .c, .m, .ctru }, .{} },
.{ "graphics/printing/colored-text", &.{ .c, .m, .ctru }, .{} },
// .{ "graphics/printing/custom-font", &.{ .c, .m, .ctru, .citro3d, .citro2d }, .{ .gfx_mode = .romfs } }, // requires tex3ds
.{ "graphics/printing/hello-world", &.{ .c, .m, .ctru }, .{} },
.{ "graphics/printing/multiple-windows-text", &.{ .c, .m, .ctru }, .{} },
.{ "graphics/printing/system-font", &.{ .c, .m, .ctru, .citro3d, .citro2d }, .{} },
.{ "graphics/printing/wide-console", &.{ .c, .m, .ctru }, .{} },
.{ "input/read-controls", &.{ .c, .m, .ctru }, .{} },
.{ "input/software-keyboard", &.{ .c, .m, .ctru }, .{} },
.{ "input/touch-screen", &.{ .c, .m, .ctru }, .{} },
.{ "libapplet_launch", &.{ .c, .m, .ctru }, .{} },
.{ "mii_selector", &.{ .c, .m, .ctru }, .{} },
// .{ "mvd", &.{ .c, .m, .ctru }, .{} }, // new 3ds only & needs special build stuff
.{ "network/3dslink-demo", &.{ .c, .m, .ctru }, .{} },
.{ "network/boss", &.{ .c, .m, .ctru }, .{} },
.{ "network/http", &.{ .c, .m, .ctru }, .{} },
.{ "network/http_post", &.{ .c, .m, .ctru }, .{} },
.{ "network/sockets", &.{ .c, .m, .ctru }, .{} },
.{ "network/sslc", &.{ .c, .m, .ctru }, .{} },
.{ "network/uds", &.{ .c, .m, .ctru }, .{} },
.{ "nfc", &.{ .c, .m, .ctru }, .{} },
// .{ "physics/box2d", &.{ .c, .m, .ctru, .box2d }, .{} }, // needs box2d
.{ "qtm", &.{ .c, .m, .ctru }, .{} },
// .{ "romfs", &.{ .c, .m, .ctru }, .{} }, // needs build romfs support
.{ "sdmc", &.{ .c, .m, .ctru }, .{} },
.{ "templates/application", &.{ .c, .m, .ctru }, .{} },
// .{ "templates/library", &.{ .c, .m, .ctru }, .{} }, // this is a template for building a library
.{ "threads/event", &.{ .c, .m, .ctru }, .{} },
.{ "threads/thread-basic", &.{ .c, .m, .ctru }, .{} },
.{ "time/rtc", &.{ .c, .m, .ctru }, .{} },
};
const libgloss_libsysbase_files = &[_][]const u8{
"_exit.c",
// "abort.c", // prefer newlib abort
// "assert.c", // prefer newlib assert
"build_argv.c",
"chdir.c",
"chmod.c",
"clocks.c",
"concatenate.c",
"dirent.c",
// "environ.c", // prefer newlib environ
"execve.c",
"fchmod.c",
"flock.c",
"fnmatch.c",
"fork.c",
"fpathconf.c",
"fstat.c",
"fsync.c",
"ftruncate.c",
"getpid.c",
"getreent.c",
"gettod.c",
"handle_manager.c",
"iosupport.c",
"isatty.c",
"kill.c",
"link.c",
"lseek.c",
"lstat.c",
"malloc_vars.c",
"mkdir.c",
"pthread.c",
"nanosleep.c",
"open.c",
"pathconf.c",
"read.c",
"readlink.c",
"realpath.c",
"rename.c",
"rmdir.c",
"sbrk.c",
"scandir.c",
"sleep.c",
"stat.c",
"statvfs.c",
"symlink.c",
"syscall_support.c",
"times.c",
"truncate.c",
"unlink.c",
"usleep.c",
"utime.c",
"wait.c",
"write.c",
};
const newlib_libm_files = &[_][]const u8{
"common/acoshl.c",
"common/acosl.c",
"common/asinhl.c",
"common/asinl.c",
"common/atan2l.c",
"common/atanhl.c",
"common/atanl.c",
"common/cbrtl.c",
"common/ceill.c",
"common/copysignl.c",
"common/cosf.c",
"common/coshl.c",
"common/cosl.c",
"common/erfcl.c",
"common/erfl.c",
"common/exp.c",
"common/exp2.c",
"common/exp2l.c",
"common/exp_data.c",
"common/expl.c",
"common/expm1l.c",
"common/fabsl.c",
"common/fdiml.c",
"common/floorl.c",
"common/fmal.c",
"common/fmaxl.c",
"common/fminl.c",
"common/fmodl.c",
"common/frexpl.c",
"common/hypotl.c",
"common/ilogbl.c",
"common/isgreater.c",
"common/ldexpl.c",
"common/lgammal.c",
"common/llrintl.c",
"common/llroundl.c",
"common/log.c",
"common/log10l.c",
"common/log1pl.c",
"common/log2.c",
"common/log2_data.c",
"common/log2l.c",
"common/log_data.c",
"common/logbl.c",
"common/logl.c",
"common/lrintl.c",
"common/lroundl.c",
"common/math_err.c",
"common/math_errf.c",
"common/modfl.c",
"common/nanl.c",
"common/nearbyintl.c",
"common/nextafterl.c",
"common/nexttoward.c",
"common/nexttowardf.c",
"common/nexttowardl.c",
"common/pow.c",
"common/pow_log_data.c",
"common/powl.c",
"common/remainderl.c",
"common/remquol.c",
"common/rintl.c",
"common/roundl.c",
"common/s_cbrt.c",
"common/s_copysign.c",
"common/s_exp10.c",
"common/s_expm1.c",
"common/s_fdim.c",
"common/s_finite.c",
"common/s_fma.c",
"common/s_fmax.c",
"common/s_fmin.c",
"common/s_fpclassify.c",
"common/s_ilogb.c",
"common/s_infinity.c",
"common/s_isinf.c",
"common/s_isinfd.c",
"common/s_isnan.c",
"common/s_isnand.c",
"common/s_llrint.c",
"common/s_llround.c",
"common/s_log1p.c",
"common/s_log2.c",
"common/s_logb.c",
"common/s_lrint.c",
"common/s_lround.c",
"common/s_modf.c",
"common/s_nan.c",
"common/s_nearbyint.c",
"common/s_nextafter.c",
"common/s_pow10.c",
"common/s_remquo.c",
"common/s_rint.c",
"common/s_round.c",
"common/s_scalbln.c",
"common/s_scalbn.c",
"common/s_signbit.c",
"common/s_trunc.c",
"common/scalblnl.c",
"common/scalbnl.c",
"common/sf_cbrt.c",
"common/sf_copysign.c",
"common/sf_exp.c",
"common/sf_exp10.c",
"common/sf_exp2.c",
"common/sf_exp2_data.c",
"common/sf_expm1.c",
"common/sf_fdim.c",
"common/sf_finite.c",
"common/sf_fma.c",
"common/sf_fmax.c",
"common/sf_fmin.c",
"common/sf_fpclassify.c",
"common/sf_ilogb.c",
"common/sf_infinity.c",
"common/sf_isinf.c",
"common/sf_isinff.c",
"common/sf_isnan.c",
"common/sf_isnanf.c",
"common/sf_llrint.c",
"common/sf_llround.c",
"common/sf_log.c",
"common/sf_log1p.c",
"common/sf_log2.c",
"common/sf_log2_data.c",
"common/sf_log_data.c",
"common/sf_logb.c",
"common/sf_lrint.c",
"common/sf_lround.c",
"common/sf_modf.c",
"common/sf_nan.c",
"common/sf_nearbyint.c",
"common/sf_nextafter.c",
"common/sf_pow.c",
"common/sf_pow10.c",
"common/sf_pow_log2_data.c",
"common/sf_remquo.c",
"common/sf_rint.c",
"common/sf_round.c",
"common/sf_scalbln.c",
"common/sf_scalbn.c",
"common/sf_trunc.c",
"common/sincosf.c",
"common/sincosf_data.c",
"common/sinf.c",
"common/sinhl.c",
"common/sinl.c",
"common/sl_finite.c",
"common/sqrtl.c",
"common/tanhl.c",
"common/tanl.c",
"common/tgammal.c",
"common/truncl.c",
};
const newlib_libc_files = &[_][]const u8{
"reent/closer.c",
"reent/execr.c",
"reent/fcntlr.c",
// "reent/fstat64r.c",
"reent/fstatr.c",
"reent/getentropyr.c",
"reent/getreent.c",
"reent/gettimeofdayr.c",
"reent/impure.c",
"reent/isattyr.c",
// "reent/linkr.c", // defines _dummy_link_syscalls when REENTRANT_SYSCALLS_PROVIDED
// "reent/lseek64r.c",
"reent/lseekr.c",
"reent/mkdirr.c",
// "reent/open64r.c",
"reent/openr.c",
"reent/readr.c",
"reent/reent.c",
"reent/renamer.c",
"reent/sbrkr.c",
// "reent/signalr.c", // defines _dummy_link_syscalls when REENTRANT_SYSCALLS_PROVIDED
// "reent/stat64r.c",
"reent/statr.c",
"reent/timesr.c",
"reent/unlinkr.c",
"reent/writer.c",
"string/bcmp.c",
"string/bcopy.c",
"string/bzero.c",
"string/explicit_bzero.c",
"string/ffsl.c",
"string/ffsll.c",
"string/fls.c",
"string/flsl.c",
"string/flsll.c",
"string/gnu_basename.c",
"string/index.c",
"string/memccpy.c",
"string/memchr.c",
"string/memcmp.c",
"string/memcpy.c",
"string/memmem.c",
"string/memmove.c",
"string/mempcpy.c",
"string/memrchr.c",
"string/memset.c",
"string/rawmemchr.c",
"string/rindex.c",
"string/stpcpy.c",
"string/stpncpy.c",
"string/strcasecmp.c",
"string/strcasecmp_l.c",
"string/strcasestr.c",
"string/strcat.c",
"string/strchr.c",
"string/strchrnul.c",
"string/strcmp.c",
"string/strcoll.c",
"string/strcoll_l.c",
"string/strcpy.c",
"string/strcspn.c",
"string/strdup.c",
"string/strdup_r.c",
"string/strerror.c",
"string/strerror_r.c",
"string/strlcat.c",
"string/strlcpy.c",
"string/strlen.c",
"string/strlwr.c",
"string/strncasecmp.c",
"string/strncasecmp_l.c",
"string/strncat.c",
"string/strncmp.c",
"string/strncpy.c",
"string/strndup.c",
"string/strndup_r.c",
"string/strnlen.c",
"string/strnstr.c",
"string/strpbrk.c",
"string/strrchr.c",
"string/strsep.c",
"string/strsignal.c",
"string/strspn.c",
"string/strstr.c",
"string/strtok.c",
"string/strtok_r.c",
"string/strupr.c",
"string/strverscmp.c",
"string/strxfrm.c",
"string/strxfrm_l.c",
"string/swab.c",
"string/timingsafe_bcmp.c",
"string/timingsafe_memcmp.c",
"string/u_strerr.c",
"string/wcpcpy.c",
"string/wcpncpy.c",
"string/wcscasecmp.c",
"string/wcscasecmp_l.c",
"string/wcscat.c",
"string/wcschr.c",
"string/wcscmp.c",
"string/wcscoll.c",
"string/wcscoll_l.c",
"string/wcscpy.c",
"string/wcscspn.c",
"string/wcsdup.c",
"string/wcslcat.c",
"string/wcslcpy.c",
"string/wcslen.c",
"string/wcsncasecmp.c",
"string/wcsncasecmp_l.c",
"string/wcsncat.c",
"string/wcsncmp.c",
"string/wcsncpy.c",
"string/wcsnlen.c",
"string/wcspbrk.c",
"string/wcsrchr.c",
"string/wcsspn.c",
"string/wcsstr.c",
"string/wcstok.c",
"string/wcswidth.c",
"string/wcsxfrm.c",
"string/wcsxfrm_l.c",
"string/wcwidth.c",
"string/wmemchr.c",