@@ -18,48 +18,28 @@ const src_flags = [_][]const u8{
1818const include_paths = [_ ][]const u8 {
1919 "../native/box2d/src" ,
2020 "../native/box2d/include" ,
21- "../native/box2d/extern/simde" ,
2221};
2322
24- const src_files = [_ ][]const u8 {
25- "../native/box2d/src/aabb.c" ,
26- "../native/box2d/src/allocate.c" ,
27- "../native/box2d/src/array.c" ,
28- "../native/box2d/src/bitset.c" ,
29- "../native/box2d/src/block_array.c" ,
30- "../native/box2d/src/body.c" ,
31- "../native/box2d/src/broad_phase.c" ,
32- "../native/box2d/src/constraint_graph.c" ,
33- "../native/box2d/src/contact.c" ,
34- "../native/box2d/src/contact_solver.c" ,
35- "../native/box2d/src/core.c" ,
36- "../native/box2d/src/distance.c" ,
37- "../native/box2d/src/distance_joint.c" ,
38- "../native/box2d/src/dynamic_tree.c" ,
39- "../native/box2d/src/geometry.c" ,
40- "../native/box2d/src/hull.c" ,
41- "../native/box2d/src/id_pool.c" ,
42- "../native/box2d/src/island.c" ,
43- "../native/box2d/src/joint.c" ,
44- "../native/box2d/src/manifold.c" ,
45- "../native/box2d/src/math_functions.c" ,
46- "../native/box2d/src/motor_joint.c" ,
47- "../native/box2d/src/mouse_joint.c" ,
48- "../native/box2d/src/prismatic_joint.c" ,
49- "../native/box2d/src/revolute_joint.c" ,
50- "../native/box2d/src/shape.c" ,
51- "../native/box2d/src/solver.c" ,
52- "../native/box2d/src/solver_set.c" ,
53- "../native/box2d/src/stack_allocator.c" ,
54- "../native/box2d/src/table.c" ,
55- "../native/box2d/src/timer.c" ,
56- "../native/box2d/src/types.c" ,
57- "../native/box2d/src/weld_joint.c" ,
58- "../native/box2d/src/wheel_joint.c" ,
59- "../native/box2d/src/world.c" ,
60- };
23+ // Collect all C source files in the given directory and its subdirectories.
24+ fn getSourceFiles (b : * std.Build , dir_path : []const u8 ) ! std. ArrayList ([]const u8 ) {
25+ var list = std .ArrayList ([]const u8 ).init (b .allocator );
26+
27+ var dir = try std .fs .cwd ().openDir (dir_path , .{ .iterate = true });
28+ defer dir .close ();
29+
30+ var walker = try dir .walk (b .allocator );
31+ defer walker .deinit ();
32+
33+ while (try walker .next ()) | entry | {
34+ if (entry .kind == .file and std .mem .endsWith (u8 , entry .path , ".c" )) {
35+ try list .append (b .pathJoin (&.{ dir_path , entry .path }));
36+ }
37+ }
38+
39+ return list ;
40+ }
6141
62- pub fn compile (b : * Build , options : BuildOptions ) void {
42+ pub fn compile (b : * Build , options : BuildOptions ) ! void {
6343 const lib = switch (options .library_type ) {
6444 .Shared = > b .addSharedLibrary (.{
6545 .name = "box2d" ,
@@ -79,22 +59,25 @@ pub fn compile(b: *Build, options: BuildOptions) void {
7959 };
8060
8161 if (options .optimize != .Debug ) {
82- lib .defineCMacro ("NDEBUG" , null );
62+ lib .root_module . addCMacro ("NDEBUG" , "" );
8363 }
8464
85- for (include_paths ) | path | {
86- lib .addIncludePath (b .path (path ));
87- }
65+ const source_files = try getSourceFiles (b , "../native/box2d/src" );
66+ defer source_files .deinit ();
8867
89- for (src_files ) | file | {
68+ for (source_files . items ) | file | {
9069 lib .addCSourceFile (.{ .file = b .path (file ), .flags = & src_flags });
9170 }
9271
72+ for (include_paths ) | path | {
73+ lib .addIncludePath (b .path (path ));
74+ }
75+
9376 switch (options .target .result .os .tag ) {
9477 .windows = > {
9578 // Temporary fix to get rid of undefined symbol errors when statically linking in Native AOT.
9679 if (options .library_type == LibraryType .Static ) {
97- // lib.addCSourceFile(.{ .file = b.path("../native/windows.c"), .flags = &src_flags });
80+ lib .addCSourceFile (.{ .file = b .path ("../native/windows.c" ), .flags = & src_flags });
9881 }
9982 },
10083 .ios = > {
@@ -166,8 +149,8 @@ pub fn compile(b: *Build, options: BuildOptions) void {
166149 b .installArtifact (lib );
167150}
168151
169- pub fn build (b : * Build ) void {
170- compile (b , .{
152+ pub fn build (b : * Build ) ! void {
153+ try compile (b , .{
171154 .optimize = b .standardOptimizeOption (.{}),
172155 .target = b .standardTargetOptions (.{}),
173156 .library_type = b .option (LibraryType , "library-type" , "Compile as a static or shared library." ) orelse LibraryType .Shared ,
0 commit comments