1
1
const std = @import ("std" );
2
2
const builtin = @import ("builtin" );
3
3
4
- const min_supported_ver = "0.14 .0" ;
4
+ const min_supported_ver = "0.15 .0" ;
5
5
6
6
comptime {
7
7
const order = std .SemanticVersion .order ;
@@ -10,7 +10,37 @@ comptime {
10
10
@compileError ("Box2d requires zig version " ++ min_supported_ver );
11
11
}
12
12
13
- fn compileBox2d (b : * std.Build , target : std.Build.ResolvedTarget , optimize : std.builtin.OptimizeMode , options : Options ) ! * std.Build.Step.Compile {
13
+ pub const Options = struct {
14
+ shared : bool ,
15
+ unit_tests : bool ,
16
+
17
+ const defaults = Options {
18
+ .shared = false ,
19
+ .unit_tests = false ,
20
+ };
21
+
22
+ pub fn getOptions (b : * std.Build ) Options {
23
+ return .{
24
+ .shared = b .option (bool , "shared" , "Compile as shared library" ) orelse defaults .shared ,
25
+ .unit_tests = b .option (bool , "unit_tests" , "Compile units tests" ) orelse defaults .unit_tests ,
26
+ };
27
+ }
28
+ };
29
+
30
+ pub fn build (b : * std.Build ) ! void {
31
+ const target = b .standardTargetOptions (.{});
32
+ const optimize = b .standardOptimizeOption (.{});
33
+ const options = Options .getOptions (b );
34
+ const lib = try compileBox2d (b , target , optimize , options .shared );
35
+
36
+ b .installArtifact (lib );
37
+
38
+ if (options .unit_tests ) {
39
+ buildTest (b , target , optimize , lib );
40
+ }
41
+ }
42
+
43
+ fn compileBox2d (b : * std.Build , target : std.Build.ResolvedTarget , optimize : std.builtin.OptimizeMode , shared : bool ) ! * std.Build.Step.Compile {
14
44
var box2d_flags_arr = std .ArrayList ([]const u8 ).empty ;
15
45
defer box2d_flags_arr .deinit (b .allocator );
16
46
@@ -19,7 +49,7 @@ fn compileBox2d(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.b
19
49
"-D_GNU_SOURCE" ,
20
50
});
21
51
22
- if (options . shared ) {
52
+ if (shared ) {
23
53
try box2d_flags_arr .appendSlice (b .allocator , &[_ ][]const u8 {
24
54
"-fPIC" ,
25
55
"-DBUILD_LIBTYPE_SHARED" ,
@@ -29,17 +59,16 @@ fn compileBox2d(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.b
29
59
const module = b .addModule ("box2d" , .{
30
60
.target = target ,
31
61
.optimize = optimize ,
62
+ .link_libc = true ,
32
63
});
33
64
34
- const linkage : std.builtin.LinkMode = if (options . shared ) .dynamic else .static ;
65
+ const linkage : std.builtin.LinkMode = if (shared ) .dynamic else .static ;
35
66
const box2d = b .addLibrary (.{
36
67
.root_module = module ,
37
68
.name = "box2d" ,
38
69
.linkage = linkage ,
39
70
});
40
71
41
- box2d .linkLibC ();
42
-
43
72
const c_source_files = &[_ ][]const u8 {
44
73
"src/aabb.c" ,
45
74
"src/aabb.h" ,
@@ -99,7 +128,8 @@ fn compileBox2d(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.b
99
128
"src/wheel_joint.c" ,
100
129
};
101
130
102
- box2d .addIncludePath (b .path ("include" ));
131
+ box2d .root_module .addIncludePath (b .path ("include" ));
132
+ box2d .installHeadersDirectory (b .path ("include/box2d" ), "box2d" , .{});
103
133
104
134
box2d .root_module .addCSourceFiles (.{
105
135
.files = c_source_files ,
@@ -109,28 +139,39 @@ fn compileBox2d(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.b
109
139
return box2d ;
110
140
}
111
141
112
- pub fn build (b : * std.Build ) ! void {
113
- const target = b .standardTargetOptions (.{});
114
- const optimize = b .standardOptimizeOption (.{});
115
- const lib = try compileBox2d (b , target , optimize , Options .getOptions (b ));
116
- lib .installHeader (b .path ("include/box2d/base.h" ), "base.h" );
117
- lib .installHeader (b .path ("include/box2d/box2d.h" ), "box2d/box2d.h" );
118
- lib .installHeader (b .path ("include/box2d/collision.h" ), "collision.h" );
119
- lib .installHeader (b .path ("include/box2d/id.h" ), "id.h" );
120
- lib .installHeader (b .path ("include/box2d/math_functions.h" ), "math_functions.h" );
121
- lib .installHeader (b .path ("include/box2d/types.h" ), "types.h" );
142
+ pub fn buildTest (b : * std.Build , target : std.Build.ResolvedTarget , optimize : std.builtin.OptimizeMode , box2d_lib : * std.Build.Step.Compile ) void {
143
+ const module = b .createModule (.{
144
+ .target = target ,
145
+ .optimize = optimize ,
146
+ .link_libc = true ,
147
+ });
122
148
123
- b .installArtifact (lib );
124
- }
149
+ module .addCSourceFiles (.{
150
+ .files = &[_ ][]const u8 {
151
+ "test/main.c" ,
152
+ "test/test_bitset.c" ,
153
+ "test/test_collision.c" ,
154
+ "test/test_determinism.c" ,
155
+ "test/test_distance.c" ,
156
+ "test/test_id.c" ,
157
+ "test/test_macros.h" ,
158
+ "test/test_math.c" ,
159
+ "test/test_shape.c" ,
160
+ "test/test_table.c" ,
161
+ "test/test_world.c" ,
162
+ },
163
+ .language = .c ,
164
+ });
125
165
126
- pub const Options = struct {
127
- shared : bool = false ,
166
+ const exe = b .addExecutable (.{
167
+ .name = "test" ,
168
+ .root_module = module ,
169
+ });
128
170
129
- const defaults = Options {};
171
+ exe .root_module .addIncludePath (b .path ("src" ));
172
+ exe .installHeadersDirectory (b .path ("src/" ), "." , .{});
130
173
131
- pub fn getOptions (b : * std.Build ) Options {
132
- return .{
133
- .shared = b .option (bool , "shared" , "Compile as shared library" ) orelse defaults .shared ,
134
- };
135
- }
136
- };
174
+ exe .root_module .linkLibrary (box2d_lib );
175
+
176
+ b .installArtifact (exe );
177
+ }
0 commit comments