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