authorgravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2021-01-03 16:48:52+01:00
committergravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2021-01-03 17:39:43+01:00
loga926c91814bbfa29be7f75c7cfeeda0e2d0669d2
tree8562a26a435f3bffc2029115048b77d21fca117d
parent7e5aacab69b74de23ce57d2c7f3af0061c3343cf

stage2: enable building test-stage2 with LLVM backend enabled

We can now run `zig build test-stage2 -Denable-llvm`.

3 files changed, 113 insertions(+), 93 deletions(-)

build.zig+110-91
......@@ -92,19 +92,6 @@ pub fn build(b: *Builder) !void {
9292 if (enable_llvm) {
9393 const cmake_cfg = if (static_llvm) null else findAndParseConfigH(b, config_h_path_option);
9494
95 const exe_cflags = [_][]const u8{
96 "-std=c++14",
97 "-D__STDC_CONSTANT_MACROS",
98 "-D__STDC_FORMAT_MACROS",
99 "-D__STDC_LIMIT_MACROS",
100 "-D_GNU_SOURCE",
101 "-fvisibility-inlines-hidden",
102 "-fno-exceptions",
103 "-fno-rtti",
104 "-Werror=type-limits",
105 "-Wno-missing-braces",
106 "-Wno-comment",
107 };
10895 if (is_stage1) {
10996 exe.addIncludeDir("src");
11097 exe.addIncludeDir("deps/SoftFloat-3e/source/include");
......@@ -133,88 +120,14 @@ pub fn build(b: *Builder) !void {
133120 if (cfg.cmake_prefix_path.len > 0) {
134121 b.addSearchPrefix(cfg.cmake_prefix_path);
135122 }
136 exe.addObjectFile(fs.path.join(b.allocator, &[_][]const u8{
137 cfg.cmake_binary_dir,
138 "zigcpp",
139 b.fmt("{s}{s}{s}", .{ exe.target.libPrefix(), "zigcpp", exe.target.staticLibSuffix() }),
140 }) catch unreachable);
141 assert(cfg.lld_include_dir.len != 0);
142 exe.addIncludeDir(cfg.lld_include_dir);
143 addCMakeLibraryList(exe, cfg.clang_libraries);
144 addCMakeLibraryList(exe, cfg.lld_libraries);
145 addCMakeLibraryList(exe, cfg.llvm_libraries);
146
147 const need_cpp_includes = tracy != null;
148
149 // System -lc++ must be used because in this code path we are attempting to link
150 // against system-provided LLVM, Clang, LLD.
151 if (exe.target.getOsTag() == .linux) {
152 // First we try to static link against gcc libstdc++. If that doesn't work,
153 // we fall back to -lc++ and cross our fingers.
154 addCxxKnownPath(b, cfg, exe, "libstdc++.a", "", need_cpp_includes) catch |err| switch (err) {
155 error.RequiredLibraryNotFound => {
156 exe.linkSystemLibrary("c++");
157 },
158 else => |e| return e,
159 };
160
161 exe.linkSystemLibrary("pthread");
162 } else if (exe.target.isFreeBSD()) {
163 try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes);
164 exe.linkSystemLibrary("pthread");
165 } else if (exe.target.getOsTag() == .openbsd) {
166 try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes);
167 try addCxxKnownPath(b, cfg, exe, "libc++abi.a", null, need_cpp_includes);
168 } else if (exe.target.isDarwin()) {
169 if (addCxxKnownPath(b, cfg, exe, "libgcc_eh.a", "", need_cpp_includes)) {
170 // Compiler is GCC.
171 try addCxxKnownPath(b, cfg, exe, "libstdc++.a", null, need_cpp_includes);
172 exe.linkSystemLibrary("pthread");
173 // TODO LLD cannot perform this link.
174 // Set ZIG_SYSTEM_LINKER_HACK env var to use system linker ld instead.
175 // See https://github.com/ziglang/zig/issues/1535
176 } else |err| switch (err) {
177 error.RequiredLibraryNotFound => {
178 // System compiler, not gcc.
179 exe.linkSystemLibrary("c++");
180 },
181 else => |e| return e,
182 }
183 }
184123
185 if (cfg.dia_guids_lib.len != 0) {
186 exe.addObjectFile(cfg.dia_guids_lib);
187 }
124 try addCmakeCfgOptionsToExe(b, cfg, tracy, exe);
125 try addCmakeCfgOptionsToExe(b, cfg, tracy, test_stage2);
188126 } else {
189127 // Here we are -Denable-llvm but no cmake integration.
190128
191 // Adds the Zig C++ sources which both stage1 and stage2 need.
192 //
193 // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling
194 // in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is
195 // unavailable when LLVM is compiled in Release mode.
196 const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"};
197 exe.addCSourceFiles(&zig_cpp_sources, &zig_cpp_cflags);
198
199 for (clang_libs) |lib_name| {
200 exe.linkSystemLibrary(lib_name);
201 }
202
203 for (lld_libs) |lib_name| {
204 exe.linkSystemLibrary(lib_name);
205 }
206
207 for (llvm_libs) |lib_name| {
208 exe.linkSystemLibrary(lib_name);
209 }
210
211 // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
212 exe.linkSystemLibrary("c++");
213
214 if (target.getOs().tag == .windows) {
215 exe.linkSystemLibrary("version");
216 exe.linkSystemLibrary("uuid");
217 }
129 try addStaticLlvmOptionsToExe(exe);
130 try addStaticLlvmOptionsToExe(test_stage2);
218131 }
219132 }
220133 if (link_libc) {
......@@ -360,6 +273,112 @@ pub fn build(b: *Builder) !void {
360273 test_step.dependOn(docs_step);
361274}
362275
276const exe_cflags = [_][]const u8{
277 "-std=c++14",
278 "-D__STDC_CONSTANT_MACROS",
279 "-D__STDC_FORMAT_MACROS",
280 "-D__STDC_LIMIT_MACROS",
281 "-D_GNU_SOURCE",
282 "-fvisibility-inlines-hidden",
283 "-fno-exceptions",
284 "-fno-rtti",
285 "-Werror=type-limits",
286 "-Wno-missing-braces",
287 "-Wno-comment",
288};
289
290fn addCmakeCfgOptionsToExe(
291 b: *Builder,
292 cfg: CMakeConfig,
293 tracy: ?[]const u8,
294 exe: *std.build.LibExeObjStep,
295) !void {
296 exe.addObjectFile(fs.path.join(b.allocator, &[_][]const u8{
297 cfg.cmake_binary_dir,
298 "zigcpp",
299 b.fmt("{s}{s}{s}", .{ exe.target.libPrefix(), "zigcpp", exe.target.staticLibSuffix() }),
300 }) catch unreachable);
301 assert(cfg.lld_include_dir.len != 0);
302 exe.addIncludeDir(cfg.lld_include_dir);
303 addCMakeLibraryList(exe, cfg.clang_libraries);
304 addCMakeLibraryList(exe, cfg.lld_libraries);
305 addCMakeLibraryList(exe, cfg.llvm_libraries);
306
307 const need_cpp_includes = tracy != null;
308
309 // System -lc++ must be used because in this code path we are attempting to link
310 // against system-provided LLVM, Clang, LLD.
311 if (exe.target.getOsTag() == .linux) {
312 // First we try to static link against gcc libstdc++. If that doesn't work,
313 // we fall back to -lc++ and cross our fingers.
314 addCxxKnownPath(b, cfg, exe, "libstdc++.a", "", need_cpp_includes) catch |err| switch (err) {
315 error.RequiredLibraryNotFound => {
316 exe.linkSystemLibrary("c++");
317 },
318 else => |e| return e,
319 };
320
321 exe.linkSystemLibrary("pthread");
322 } else if (exe.target.isFreeBSD()) {
323 try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes);
324 exe.linkSystemLibrary("pthread");
325 } else if (exe.target.getOsTag() == .openbsd) {
326 try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes);
327 try addCxxKnownPath(b, cfg, exe, "libc++abi.a", null, need_cpp_includes);
328 } else if (exe.target.isDarwin()) {
329 if (addCxxKnownPath(b, cfg, exe, "libgcc_eh.a", "", need_cpp_includes)) {
330 // Compiler is GCC.
331 try addCxxKnownPath(b, cfg, exe, "libstdc++.a", null, need_cpp_includes);
332 exe.linkSystemLibrary("pthread");
333 // TODO LLD cannot perform this link.
334 // Set ZIG_SYSTEM_LINKER_HACK env var to use system linker ld instead.
335 // See https://github.com/ziglang/zig/issues/1535
336 } else |err| switch (err) {
337 error.RequiredLibraryNotFound => {
338 // System compiler, not gcc.
339 exe.linkSystemLibrary("c++");
340 },
341 else => |e| return e,
342 }
343 }
344
345 if (cfg.dia_guids_lib.len != 0) {
346 exe.addObjectFile(cfg.dia_guids_lib);
347 }
348}
349
350fn addStaticLlvmOptionsToExe(
351 exe: *std.build.LibExeObjStep,
352) !void {
353 // Adds the Zig C++ sources which both stage1 and stage2 need.
354 //
355 // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling
356 // in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is
357 // unavailable when LLVM is compiled in Release mode.
358 const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"};
359 exe.addCSourceFiles(&zig_cpp_sources, &zig_cpp_cflags);
360
361 for (clang_libs) |lib_name| {
362 exe.linkSystemLibrary(lib_name);
363 }
364
365 for (lld_libs) |lib_name| {
366 exe.linkSystemLibrary(lib_name);
367 }
368
369 for (llvm_libs) |lib_name| {
370 exe.linkSystemLibrary(lib_name);
371 }
372
373 // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries.
374 exe.linkSystemLibrary("c++");
375
376 if (exe.target.getOs().tag == .windows) {
377 exe.linkSystemLibrary("version");
378 exe.linkSystemLibrary("uuid");
379 }
380}
381
363382fn addCxxKnownPath(
364383 b: *Builder,
365384 ctx: CMakeConfig,
src/Cache.zig+2-1
......@@ -16,7 +16,8 @@ const Allocator = std.mem.Allocator;
1616/// This protection is conditionally compiled depending on `want_debug_deadlock`.
1717var all_cache_digest_set: std.AutoHashMapUnmanaged(BinDigest, void) = .{};
1818var all_cache_digest_lock: std.Mutex = .{};
19const want_debug_deadlock = std.debug.runtime_safety;
19// TODO: Figure out how to make sure that `all_cache_digest_set` does not leak memory!
20pub const want_debug_deadlock = false;
2021const DebugBinDigest = if (want_debug_deadlock) BinDigest else void;
2122const null_debug_bin_digest = if (want_debug_deadlock) ([1]u8{0} ** bin_digest_len) else {};
2223
src/DepTokenizer.zig+1-1
......@@ -927,7 +927,7 @@ fn depTokenizer(input: []const u8, expect: []const u8) !void {
927927 try out.writeAll("\n");
928928 try printSection(out, "<<<< input", input);
929929 try printSection(out, "==== expect", expect);
930 try printSection(out, ">>>> got", got);
930 try printSection(out, ">>>> got", buffer.items);
931931 try printRuler(out);
932932
933933 testing.expect(false);