11const std = @import ("std" );
22
33pub 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 (.{});
65 const optimize = b .standardOptimizeOption (.{});
76
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" ) ,
1110 .target = target ,
12- .optimize = optimize
11+ .optimize = optimize ,
1312 });
1413
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+
2125 switch (target .result .os .tag ) {
22- .linux = > lib .linkSystemLibrary ("LLVM-17" ), // Ubuntu
26+ .linux = > llvm_module .linkSystemLibrary ("LLVM-18" , .{} ), // Ubuntu
2327 .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+ });
2634 },
27- else = > lib .linkSystemLibrary ("LLVM" ),
35+ else = > llvm_module .linkSystemLibrary ("LLVM" , .{
36+ .use_pkg_config = .no ,
37+ }),
2838 }
2939
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 ,
3845 });
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 ;
3964
4065 const examples = b .option (bool , "Examples" , "Build all examples [default: false]" ) orelse false ;
4166 if (examples ) {
42- buildExample (b , target , .{
67+ buildExample (b , .{
4368 .filepath = "examples/sum_module.zig" ,
44- .target = target . query ,
69+ .target = target ,
4570 .optimize = optimize ,
4671 });
47- buildExample (b , target , .{
72+ buildExample (b , .{
4873 .filepath = "examples/factorial_module.zig" ,
49- .target = target . query ,
74+ .target = target ,
5075 .optimize = optimize ,
5176 });
5277 }
5378
5479 buildTests (b , target );
5580}
5681
57- fn buildExample (b : * std.Build , target : std.Build.ResolvedTarget , i : BuildInfo ) void {
82+ fn buildExample (b : * std.Build , i : BuildInfo ) void {
5883 const exe = b .addExecutable (.{
5984 .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 ,
6287 .optimize = i .optimize ,
6388 });
6489 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
79104
80105const BuildInfo = struct {
81106 filepath : []const u8 ,
82- target : std.zig.CrossTarget ,
107+ target : std.Build.ResolvedTarget ,
83108 optimize : std.builtin.OptimizeMode ,
84109
85110 fn filename (self : BuildInfo ) []const u8 {
@@ -90,28 +115,19 @@ const BuildInfo = struct {
90115
91116fn buildTests (b : * std.Build , target : std.Build.ResolvedTarget ) void {
92117 const llvm_tests = b .addTest (.{
93- .root_source_file = .{ . path = "src/llvm-zig .zig" } ,
118+ .root_source_file = b . path ( "src/llvm.zig" ) ,
94119 .target = target ,
95120 .optimize = .Debug ,
96121 .name = "llvm-tests" ,
97122 });
98123 const clang_tests = b .addTest (.{
99- .root_source_file = .{ . path = "src/clang.zig" } ,
124+ .root_source_file = b . path ( "src/clang.zig" ) ,
100125 .target = target ,
101126 .optimize = .Debug ,
102127 .name = "clang-tests" ,
103128 });
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-
114129 llvm_tests .root_module .addImport ("llvm" , b .modules .get ("llvm" ).? );
130+ clang_tests .root_module .addImport ("clang" , b .modules .get ("clang" ).? );
115131
116132 // TODO: CI build LLVM tests with clang
117133 // llvm_tests.step.dependOn(&clang_tests.step);
0 commit comments