1
1
const std = @import ("std" );
2
2
3
- pub fn build (b : * std.Build ) void {
4
- const target = b .standardTargetOptions (.{});
3
+ pub fn build (b : * std.Build ) ! void {
4
+ const target_query : std.Target.Query = .{};
5
+ const target = b .resolveTargetQuery (target_query );
5
6
const optimize = b .standardOptimizeOption (.{});
6
7
7
- _ = b .addModule ("llvm" , .{
8
- .source_file = .{
9
- .path = "src/llvm.zig" ,
10
- },
8
+ const lib = b .addStaticLibrary (.{
9
+ .name = "llvm" ,
10
+ .root_source_file = .{.path = "src/llvm-zig.zig" },
11
+ .target = target ,
12
+ .optimize = optimize
11
13
});
14
+
15
+ lib .defineCMacro ("_FILE_OFFSET_BITS" , "64" );
16
+ lib .defineCMacro ("__STDC_CONSTANT_MACROS" , null );
17
+ lib .defineCMacro ("__STDC_FORMAT_MACROS" , null );
18
+ lib .defineCMacro ("__STDC_LIMIT_MACROS" , null );
19
+ lib .linkSystemLibrary ("z" );
20
+ lib .linkLibC ();
21
+ switch (target .result .os .tag ) {
22
+ .linux = > lib .linkSystemLibrary ("LLVM-17" ), // Ubuntu
23
+ .macos = > {
24
+ lib .addLibraryPath (.{ .path = "/usr/local/opt/llvm/lib" });
25
+ lib .linkSystemLibrary ("LLVM" );
26
+ },
27
+ else = > lib .linkSystemLibrary ("LLVM" ),
28
+ }
29
+
30
+ b .installArtifact (lib );
31
+
32
+ _ = try b .modules .put ("llvm" , & lib .root_module );
33
+
12
34
_ = b .addModule ("clang" , .{
13
- .source_file = .{
35
+ .root_source_file = .{
14
36
.path = "src/clang.zig" ,
15
37
},
16
38
});
17
39
18
- buildTests (b );
19
-
20
40
const examples = b .option (bool , "Examples" , "Build all examples [default: false]" ) orelse false ;
21
-
22
41
if (examples ) {
23
- buildExample (b , .{
42
+ buildExample (b , target , .{
24
43
.filepath = "examples/sum_module.zig" ,
25
- .target = target ,
44
+ .target = target . query ,
26
45
.optimize = optimize ,
27
46
});
28
- buildExample (b , .{
29
- .filepath = "examples/fatorial_module .zig" ,
30
- .target = target ,
47
+ buildExample (b , target , .{
48
+ .filepath = "examples/factorial_module .zig" ,
49
+ .target = target . query ,
31
50
.optimize = optimize ,
32
51
});
33
52
}
53
+
54
+ buildTests (b , target );
34
55
}
35
56
36
- fn buildExample (b : * std.Build , i : BuildInfo ) void {
57
+ fn buildExample (b : * std.Build , target : std.Build.ResolvedTarget , i : BuildInfo ) void {
37
58
const exe = b .addExecutable (.{
38
59
.name = i .filename (),
39
60
.root_source_file = .{ .path = i .filepath },
40
- .target = i . target ,
61
+ .target = target ,
41
62
.optimize = i .optimize ,
42
63
});
43
- exe .defineCMacro ("_FILE_OFFSET_BITS" , "64" );
44
- exe .defineCMacro ("__STDC_CONSTANT_MACROS" , null );
45
- exe .defineCMacro ("__STDC_FORMAT_MACROS" , null );
46
- exe .defineCMacro ("__STDC_LIMIT_MACROS" , null );
47
- exe .addModule ("llvm" , b .modules .get ("llvm" ).? );
48
- exe .linkSystemLibrary ("z" );
49
- switch (i .target .getOsTag ()) {
50
- .linux = > exe .linkSystemLibrary ("LLVM-17" ), // Ubuntu
51
- .macos = > {
52
- exe .addLibraryPath (.{ .path = "/usr/local/opt/llvm/lib" });
53
- exe .linkSystemLibrary ("LLVM" );
54
- },
55
- else = > exe .linkSystemLibrary ("LLVM" ),
56
- }
57
- exe .linkLibC ();
64
+ exe .root_module .addImport ("llvm" , b .modules .get ("llvm" ).? );
58
65
59
66
b .installArtifact (exe );
60
67
@@ -81,20 +88,20 @@ const BuildInfo = struct {
81
88
}
82
89
};
83
90
84
- fn buildTests (b : * std.Build ) void {
91
+ fn buildTests (b : * std.Build , target : std.Build.ResolvedTarget ) void {
85
92
const llvm_tests = b .addTest (.{
86
- .root_source_file = .{ .path = "src/llvm.zig" },
87
- .target = .{} ,
93
+ .root_source_file = .{ .path = "src/llvm-zig .zig" },
94
+ .target = target ,
88
95
.optimize = .Debug ,
89
96
.name = "llvm-tests" ,
90
97
});
91
98
const clang_tests = b .addTest (.{
92
99
.root_source_file = .{ .path = "src/clang.zig" },
93
- .target = .{} ,
100
+ .target = target ,
94
101
.optimize = .Debug ,
95
102
.name = "clang-tests" ,
96
103
});
97
- switch (clang_tests . target .getOsTag () ) {
104
+ switch (target .result . os . tag ) {
98
105
.linux = > clang_tests .linkSystemLibrary ("clang-17" ), // Ubuntu
99
106
.macos = > {
100
107
clang_tests .addLibraryPath (.{ .path = "/usr/local/opt/llvm/lib" });
@@ -104,21 +111,7 @@ fn buildTests(b: *std.Build) void {
104
111
}
105
112
clang_tests .linkLibC ();
106
113
107
- llvm_tests .defineCMacro ("_FILE_OFFSET_BITS" , "64" );
108
- llvm_tests .defineCMacro ("__STDC_CONSTANT_MACROS" , null );
109
- llvm_tests .defineCMacro ("__STDC_FORMAT_MACROS" , null );
110
- llvm_tests .defineCMacro ("__STDC_LIMIT_MACROS" , null );
111
- llvm_tests .addModule ("llvm" , b .modules .get ("llvm" ).? );
112
- llvm_tests .linkSystemLibrary ("z" );
113
- switch (llvm_tests .target .getOsTag ()) {
114
- .linux = > llvm_tests .linkSystemLibrary ("LLVM-17" ), // Ubuntu
115
- .macos = > {
116
- llvm_tests .addLibraryPath (.{ .path = "/usr/local/opt/llvm/lib" });
117
- llvm_tests .linkSystemLibrary ("LLVM" );
118
- },
119
- else = > llvm_tests .linkSystemLibrary ("LLVM" ),
120
- }
121
- llvm_tests .linkLibC ();
114
+ llvm_tests .root_module .addImport ("llvm" , b .modules .get ("llvm" ).? );
122
115
123
116
// TODO: CI build LLVM tests with clang
124
117
// llvm_tests.step.dependOn(&clang_tests.step);
0 commit comments