| ... | @@ -54,7 +54,8 @@ pub fn build(b: *Builder) !void { | ... | @@ -54,7 +54,8 @@ pub fn build(b: *Builder) !void { |
| 54 | | 54 | |
| 55 | const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false; | 55 | const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false; |
| 56 | const is_stage1 = b.option(bool, "stage1", "Build the stage1 compiler, put stage2 behind a feature flag") orelse false; | 56 | const is_stage1 = b.option(bool, "stage1", "Build the stage1 compiler, put stage2 behind a feature flag") orelse false; |
| 57 | const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse is_stage1; | 57 | const static_llvm = b.option(bool, "static-llvm", "Disable integration with system-installed LLVM, Clang, LLD, and libc++") orelse false; |
| | 58 | const enable_llvm = b.option(bool, "enable-llvm", "Build self-hosted compiler with LLVM backend enabled") orelse (is_stage1 or static_llvm); |
| 58 | const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h"); | 59 | const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h"); |
| 59 | | 60 | |
| 60 | b.installDirectory(InstallDirectoryOptions{ | 61 | b.installDirectory(InstallDirectoryOptions{ |
| ... | @@ -89,6 +90,7 @@ pub fn build(b: *Builder) !void { | ... | @@ -89,6 +90,7 @@ pub fn build(b: *Builder) !void { |
| 89 | exe.addBuildOption(bool, "skip_non_native", skip_non_native); | 90 | exe.addBuildOption(bool, "skip_non_native", skip_non_native); |
| 90 | exe.addBuildOption(bool, "have_llvm", enable_llvm); | 91 | exe.addBuildOption(bool, "have_llvm", enable_llvm); |
| 91 | if (enable_llvm) { | 92 | if (enable_llvm) { |
| | 93 | const cmake_cfg = if (static_llvm) null else findAndParseConfigH(b, config_h_path_option); |
| 92 | if (is_stage1) { | 94 | if (is_stage1) { |
| 93 | exe.addIncludeDir("src"); | 95 | exe.addIncludeDir("src"); |
| 94 | exe.addIncludeDir("deps/SoftFloat-3e/source/include"); | 96 | exe.addIncludeDir("deps/SoftFloat-3e/source/include"); |
| ... | @@ -96,6 +98,7 @@ pub fn build(b: *Builder) !void { | ... | @@ -96,6 +98,7 @@ pub fn build(b: *Builder) !void { |
| 96 | // of being built by cmake. But when built by zig it's gonna get a compiler_rt so that | 98 | // of being built by cmake. But when built by zig it's gonna get a compiler_rt so that |
| 97 | // is pointless. | 99 | // is pointless. |
| 98 | exe.addPackagePath("compiler_rt", "src/empty.zig"); | 100 | exe.addPackagePath("compiler_rt", "src/empty.zig"); |
| | 101 | exe.defineCMacro("ZIG_LINK_MODE=Static"); |
| 99 | | 102 | |
| 100 | const softfloat = b.addStaticLibrary("softfloat", null); | 103 | const softfloat = b.addStaticLibrary("softfloat", null); |
| 101 | softfloat.setBuildMode(.ReleaseFast); | 104 | softfloat.setBuildMode(.ReleaseFast); |
| ... | @@ -121,31 +124,91 @@ pub fn build(b: *Builder) !void { | ... | @@ -121,31 +124,91 @@ pub fn build(b: *Builder) !void { |
| 121 | }; | 124 | }; |
| 122 | exe.addCSourceFiles(&stage1_sources, &exe_cflags); | 125 | exe.addCSourceFiles(&stage1_sources, &exe_cflags); |
| 123 | exe.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" }); | 126 | exe.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" }); |
| 124 | // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling | 127 | if (cmake_cfg == null) { |
| 125 | // in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is | 128 | // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling |
| 126 | // unavailable when LLVM is compiled in Release mode. | 129 | // in a dependency on llvm::cfg::Update<llvm::BasicBlock*>::dump() which is |
| 127 | const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"}; | 130 | // unavailable when LLVM is compiled in Release mode. |
| 128 | exe.addCSourceFiles(&zig_cpp_sources, &zig_cpp_cflags); | 131 | const zig_cpp_cflags = exe_cflags ++ [_][]const u8{"-DNDEBUG=1"}; |
| | 132 | exe.addCSourceFiles(&zig_cpp_sources, &zig_cpp_cflags); |
| | 133 | } |
| 129 | } | 134 | } |
| | 135 | if (cmake_cfg) |cfg| { |
| | 136 | // Inside this code path, we have to coordinate with system packaged LLVM, Clang, and LLD. |
| | 137 | // That means we also have to rely on stage1 compiled c++ files. We parse config.h to find |
| | 138 | // the information passed on to us from cmake. |
| | 139 | if (cfg.cmake_prefix_path.len > 0) { |
| | 140 | b.addSearchPrefix(cfg.cmake_prefix_path); |
| | 141 | } |
| | 142 | exe.addObjectFile(fs.path.join(b.allocator, &[_][]const u8{ |
| | 143 | cfg.cmake_binary_dir, |
| | 144 | "zigcpp", |
| | 145 | b.fmt("{s}{s}{s}", .{ exe.target.libPrefix(), "zigcpp", exe.target.staticLibSuffix() }), |
| | 146 | }) catch unreachable); |
| | 147 | assert(cfg.lld_include_dir.len != 0); |
| | 148 | exe.addIncludeDir(cfg.lld_include_dir); |
| | 149 | addCMakeLibraryList(exe, cfg.clang_libraries); |
| | 150 | addCMakeLibraryList(exe, cfg.lld_libraries); |
| | 151 | addCMakeLibraryList(exe, cfg.llvm_libraries); |
| | 152 | |
| | 153 | const need_cpp_includes = tracy != null; |
| | 154 | |
| | 155 | // System -lc++ must be used because in this code path we are attempting to link |
| | 156 | // against system-provided LLVM, Clang, LLD. |
| | 157 | if (exe.target.getOsTag() == .linux) { |
| | 158 | // First we try to static link against gcc libstdc++. If that doesn't work, |
| | 159 | // we fall back to -lc++ and cross our fingers. |
| | 160 | addCxxKnownPath(b, cfg, exe, "libstdc++.a", "", need_cpp_includes) catch |err| switch (err) { |
| | 161 | error.RequiredLibraryNotFound => { |
| | 162 | exe.linkSystemLibrary("c++"); |
| | 163 | }, |
| | 164 | else => |e| return e, |
| | 165 | }; |
| | 166 | |
| | 167 | exe.linkSystemLibrary("pthread"); |
| | 168 | } else if (exe.target.isFreeBSD()) { |
| | 169 | try addCxxKnownPath(b, cfg, exe, "libc++.a", null, need_cpp_includes); |
| | 170 | exe.linkSystemLibrary("pthread"); |
| | 171 | } else if (exe.target.isDarwin()) { |
| | 172 | if (addCxxKnownPath(b, cfg, exe, "libgcc_eh.a", "", need_cpp_includes)) { |
| | 173 | // Compiler is GCC. |
| | 174 | try addCxxKnownPath(b, cfg, exe, "libstdc++.a", null, need_cpp_includes); |
| | 175 | exe.linkSystemLibrary("pthread"); |
| | 176 | // TODO LLD cannot perform this link. |
| | 177 | // Set ZIG_SYSTEM_LINKER_HACK env var to use system linker ld instead. |
| | 178 | // See https://github.com/ziglang/zig/issues/1535 |
| | 179 | } else |err| switch (err) { |
| | 180 | error.RequiredLibraryNotFound => { |
| | 181 | // System compiler, not gcc. |
| | 182 | exe.linkSystemLibrary("c++"); |
| | 183 | }, |
| | 184 | else => |e| return e, |
| | 185 | } |
| | 186 | } |
| 130 | | 187 | |
| 131 | for (clang_libs) |lib_name| { | 188 | if (cfg.dia_guids_lib.len != 0) { |
| 132 | exe.linkSystemLibrary(lib_name); | 189 | exe.addObjectFile(cfg.dia_guids_lib); |
| 133 | } | 190 | } |
| | 191 | } else { |
| | 192 | // Here we are -Denable-llvm but no cmake integration. |
| | 193 | for (clang_libs) |lib_name| { |
| | 194 | exe.linkSystemLibrary(lib_name); |
| | 195 | } |
| 134 | | 196 | |
| 135 | for (lld_libs) |lib_name| { | 197 | for (lld_libs) |lib_name| { |
| 136 | exe.linkSystemLibrary(lib_name); | 198 | exe.linkSystemLibrary(lib_name); |
| 137 | } | 199 | } |
| 138 | | 200 | |
| 139 | for (llvm_libs) |lib_name| { | 201 | for (llvm_libs) |lib_name| { |
| 140 | exe.linkSystemLibrary(lib_name); | 202 | exe.linkSystemLibrary(lib_name); |
| 141 | } | 203 | } |
| 142 | | 204 | |
| 143 | // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries. | 205 | // This means we rely on clang-or-zig-built LLVM, Clang, LLD libraries. |
| 144 | exe.linkSystemLibrary("c++"); | 206 | exe.linkSystemLibrary("c++"); |
| 145 | | 207 | |
| 146 | if (target.getOs().tag == .windows) { | 208 | if (target.getOs().tag == .windows) { |
| 147 | exe.linkSystemLibrary("version"); | 209 | exe.linkSystemLibrary("version"); |
| 148 | exe.linkSystemLibrary("uuid"); | 210 | exe.linkSystemLibrary("uuid"); |
| | 211 | } |
| 149 | } | 212 | } |
| 150 | } | 213 | } |
| 151 | if (link_libc) { | 214 | if (link_libc) { |
| ... | @@ -281,6 +344,158 @@ pub fn build(b: *Builder) !void { | ... | @@ -281,6 +344,158 @@ pub fn build(b: *Builder) !void { |
| 281 | test_step.dependOn(docs_step); | 344 | test_step.dependOn(docs_step); |
| 282 | } | 345 | } |
| 283 | | 346 | |
| | 347 | fn addCxxKnownPath( |
| | 348 | b: *Builder, |
| | 349 | ctx: CMakeConfig, |
| | 350 | exe: *std.build.LibExeObjStep, |
| | 351 | objname: []const u8, |
| | 352 | errtxt: ?[]const u8, |
| | 353 | need_cpp_includes: bool, |
| | 354 | ) !void { |
| | 355 | const path_padded = try b.exec(&[_][]const u8{ |
| | 356 | ctx.cxx_compiler, |
| | 357 | b.fmt("-print-file-name={}", .{objname}), |
| | 358 | }); |
| | 359 | const path_unpadded = mem.tokenize(path_padded, "\r\n").next().?; |
| | 360 | if (mem.eql(u8, path_unpadded, objname)) { |
| | 361 | if (errtxt) |msg| { |
| | 362 | warn("{}", .{msg}); |
| | 363 | } else { |
| | 364 | warn("Unable to determine path to {}\n", .{objname}); |
| | 365 | } |
| | 366 | return error.RequiredLibraryNotFound; |
| | 367 | } |
| | 368 | exe.addObjectFile(path_unpadded); |
| | 369 | |
| | 370 | // TODO a way to integrate with system c++ include files here |
| | 371 | // cc -E -Wp,-v -xc++ /dev/null |
| | 372 | if (need_cpp_includes) { |
| | 373 | // I used these temporarily for testing something but we obviously need a |
| | 374 | // more general purpose solution here. |
| | 375 | //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0"); |
| | 376 | //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0/x86_64-unknown-linux-gnu"); |
| | 377 | //exe.addIncludeDir("/nix/store/b3zsk4ihlpiimv3vff86bb5bxghgdzb9-gcc-9.2.0/lib/gcc/x86_64-unknown-linux-gnu/9.2.0/../../../../include/c++/9.2.0/backward"); |
| | 378 | } |
| | 379 | } |
| | 380 | |
| | 381 | fn addCMakeLibraryList(exe: *std.build.LibExeObjStep, list: []const u8) void { |
| | 382 | var it = mem.tokenize(list, ";"); |
| | 383 | while (it.next()) |lib| { |
| | 384 | if (mem.startsWith(u8, lib, "-l")) { |
| | 385 | exe.linkSystemLibrary(lib["-l".len..]); |
| | 386 | } else { |
| | 387 | exe.addObjectFile(lib); |
| | 388 | } |
| | 389 | } |
| | 390 | } |
| | 391 | |
| | 392 | const CMakeConfig = struct { |
| | 393 | cmake_binary_dir: []const u8, |
| | 394 | cmake_prefix_path: []const u8, |
| | 395 | cxx_compiler: []const u8, |
| | 396 | lld_include_dir: []const u8, |
| | 397 | lld_libraries: []const u8, |
| | 398 | clang_libraries: []const u8, |
| | 399 | llvm_libraries: []const u8, |
| | 400 | dia_guids_lib: []const u8, |
| | 401 | }; |
| | 402 | |
| | 403 | const max_config_h_bytes = 1 * 1024 * 1024; |
| | 404 | |
| | 405 | fn findAndParseConfigH(b: *Builder, config_h_path_option: ?[]const u8) ?CMakeConfig { |
| | 406 | const config_h_text: []const u8 = if (config_h_path_option) |config_h_path| blk: { |
| | 407 | break :blk fs.cwd().readFileAlloc(b.allocator, config_h_path, max_config_h_bytes) catch unreachable; |
| | 408 | } else blk: { |
| | 409 | // TODO this should stop looking for config.h once it detects we hit the |
| | 410 | // zig source root directory. |
| | 411 | var check_dir = fs.path.dirname(b.zig_exe).?; |
| | 412 | while (true) { |
| | 413 | var dir = fs.cwd().openDir(check_dir, .{}) catch unreachable; |
| | 414 | defer dir.close(); |
| | 415 | |
| | 416 | break :blk dir.readFileAlloc(b.allocator, "config.h", max_config_h_bytes) catch |err| switch (err) { |
| | 417 | error.FileNotFound => { |
| | 418 | const new_check_dir = fs.path.dirname(check_dir); |
| | 419 | if (new_check_dir == null or mem.eql(u8, new_check_dir.?, check_dir)) { |
| | 420 | return null; |
| | 421 | } |
| | 422 | check_dir = new_check_dir.?; |
| | 423 | continue; |
| | 424 | }, |
| | 425 | else => unreachable, |
| | 426 | }; |
| | 427 | } else unreachable; // TODO should not need `else unreachable`. |
| | 428 | }; |
| | 429 | |
| | 430 | var ctx: CMakeConfig = .{ |
| | 431 | .cmake_binary_dir = undefined, |
| | 432 | .cmake_prefix_path = undefined, |
| | 433 | .cxx_compiler = undefined, |
| | 434 | .lld_include_dir = undefined, |
| | 435 | .lld_libraries = undefined, |
| | 436 | .clang_libraries = undefined, |
| | 437 | .llvm_libraries = undefined, |
| | 438 | .dia_guids_lib = undefined, |
| | 439 | }; |
| | 440 | |
| | 441 | const mappings = [_]struct { prefix: []const u8, field: []const u8 }{ |
| | 442 | .{ |
| | 443 | .prefix = "#define ZIG_CMAKE_BINARY_DIR ", |
| | 444 | .field = "cmake_binary_dir", |
| | 445 | }, |
| | 446 | .{ |
| | 447 | .prefix = "#define ZIG_CMAKE_PREFIX_PATH ", |
| | 448 | .field = "cmake_prefix_path", |
| | 449 | }, |
| | 450 | .{ |
| | 451 | .prefix = "#define ZIG_CXX_COMPILER ", |
| | 452 | .field = "cxx_compiler", |
| | 453 | }, |
| | 454 | .{ |
| | 455 | .prefix = "#define ZIG_LLD_INCLUDE_PATH ", |
| | 456 | .field = "lld_include_dir", |
| | 457 | }, |
| | 458 | .{ |
| | 459 | .prefix = "#define ZIG_LLD_LIBRARIES ", |
| | 460 | .field = "lld_libraries", |
| | 461 | }, |
| | 462 | .{ |
| | 463 | .prefix = "#define ZIG_CLANG_LIBRARIES ", |
| | 464 | .field = "clang_libraries", |
| | 465 | }, |
| | 466 | .{ |
| | 467 | .prefix = "#define ZIG_LLVM_LIBRARIES ", |
| | 468 | .field = "llvm_libraries", |
| | 469 | }, |
| | 470 | .{ |
| | 471 | .prefix = "#define ZIG_DIA_GUIDS_LIB ", |
| | 472 | .field = "dia_guids_lib", |
| | 473 | }, |
| | 474 | }; |
| | 475 | |
| | 476 | var lines_it = mem.tokenize(config_h_text, "\r\n"); |
| | 477 | while (lines_it.next()) |line| { |
| | 478 | inline for (mappings) |mapping| { |
| | 479 | if (mem.startsWith(u8, line, mapping.prefix)) { |
| | 480 | var it = mem.split(line, "\""); |
| | 481 | _ = it.next().?; // skip the stuff before the quote |
| | 482 | const quoted = it.next().?; // the stuff inside the quote |
| | 483 | @field(ctx, mapping.field) = toNativePathSep(b, quoted); |
| | 484 | } |
| | 485 | } |
| | 486 | } |
| | 487 | return ctx; |
| | 488 | } |
| | 489 | |
| | 490 | fn toNativePathSep(b: *Builder, s: []const u8) []u8 { |
| | 491 | const duplicated = mem.dupe(b.allocator, u8, s) catch unreachable; |
| | 492 | for (duplicated) |*byte| switch (byte.*) { |
| | 493 | '/' => byte.* = fs.path.sep, |
| | 494 | else => {}, |
| | 495 | }; |
| | 496 | return duplicated; |
| | 497 | } |
| | 498 | |
| 284 | const softfloat_sources = [_][]const u8{ | 499 | const softfloat_sources = [_][]const u8{ |
| 285 | "deps/SoftFloat-3e/source/8086/f128M_isSignalingNaN.c", | 500 | "deps/SoftFloat-3e/source/8086/f128M_isSignalingNaN.c", |
| 286 | "deps/SoftFloat-3e/source/8086/s_commonNaNToF128M.c", | 501 | "deps/SoftFloat-3e/source/8086/s_commonNaNToF128M.c", |