| ... | @@ -129,6 +129,7 @@ pub fn build(b: *Builder) !void { | ... | @@ -129,6 +129,7 @@ pub fn build(b: *Builder) !void { |
| 129 | const force_gpa = b.option(bool, "force-gpa", "Force the compiler to use GeneralPurposeAllocator") orelse false; | 129 | const force_gpa = b.option(bool, "force-gpa", "Force the compiler to use GeneralPurposeAllocator") orelse false; |
| 130 | const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm; | 130 | const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm; |
| 131 | const strip = b.option(bool, "strip", "Omit debug information") orelse false; | 131 | const strip = b.option(bool, "strip", "Omit debug information") orelse false; |
| | 132 | const use_zig0 = b.option(bool, "zig0", "Bootstrap using zig0") orelse false; |
| 132 | | 133 | |
| 133 | const mem_leak_frames: u32 = b.option(u32, "mem-leak-frames", "How many stack frames to print when a memory leak occurs. Tests get 2x this amount.") orelse blk: { | 134 | const mem_leak_frames: u32 = b.option(u32, "mem-leak-frames", "How many stack frames to print when a memory leak occurs. Tests get 2x this amount.") orelse blk: { |
| 134 | if (strip) break :blk @as(u32, 0); | 135 | if (strip) break :blk @as(u32, 0); |
| ... | @@ -136,7 +137,11 @@ pub fn build(b: *Builder) !void { | ... | @@ -136,7 +137,11 @@ pub fn build(b: *Builder) !void { |
| 136 | break :blk 4; | 137 | break :blk 4; |
| 137 | }; | 138 | }; |
| 138 | | 139 | |
| 139 | const main_file: []const u8 = if (is_stage1) "src/stage1.zig" else "src/main.zig"; | 140 | const main_file: ?[]const u8 = mf: { |
| | 141 | if (!is_stage1) break :mf "src/main.zig"; |
| | 142 | if (use_zig0) break :mf null; |
| | 143 | break :mf "src/stage1.zig"; |
| | 144 | }; |
| 140 | | 145 | |
| 141 | const exe = b.addExecutable("zig", main_file); | 146 | const exe = b.addExecutable("zig", main_file); |
| 142 | exe.strip = strip; | 147 | exe.strip = strip; |
| ... | @@ -245,17 +250,76 @@ pub fn build(b: *Builder) !void { | ... | @@ -245,17 +250,76 @@ pub fn build(b: *Builder) !void { |
| 245 | softfloat.addCSourceFiles(&softfloat_sources, &[_][]const u8{ "-std=c99", "-O3" }); | 250 | softfloat.addCSourceFiles(&softfloat_sources, &[_][]const u8{ "-std=c99", "-O3" }); |
| 246 | softfloat.single_threaded = single_threaded; | 251 | softfloat.single_threaded = single_threaded; |
| 247 | | 252 | |
| 248 | exe.addIncludePath("src"); | 253 | const zig0 = b.addExecutable("zig0", null); |
| 249 | exe.addIncludePath("deps/SoftFloat-3e/source/include"); | 254 | zig0.addCSourceFiles(&.{"src/stage1/zig0.cpp"}, &exe_cflags); |
| 250 | exe.addIncludePath("deps/SoftFloat-3e-prebuilt"); | 255 | zig0.addIncludePath("zig-cache/tmp"); // for config.h |
| | 256 | zig0.defineCMacro("ZIG_VERSION_MAJOR", b.fmt("{d}", .{zig_version.major})); |
| | 257 | zig0.defineCMacro("ZIG_VERSION_MINOR", b.fmt("{d}", .{zig_version.minor})); |
| | 258 | zig0.defineCMacro("ZIG_VERSION_PATCH", b.fmt("{d}", .{zig_version.patch})); |
| | 259 | zig0.defineCMacro("ZIG_VERSION_STRING", b.fmt("\"{s}\"", .{version})); |
| | 260 | |
| | 261 | for ([_]*std.build.LibExeObjStep{ zig0, exe, test_stage2 }) |artifact| { |
| | 262 | artifact.addIncludePath("src"); |
| | 263 | artifact.addIncludePath("deps/SoftFloat-3e/source/include"); |
| | 264 | artifact.addIncludePath("deps/SoftFloat-3e-prebuilt"); |
| | 265 | |
| | 266 | artifact.defineCMacro("ZIG_LINK_MODE", "Static"); |
| 251 | | 267 | |
| 252 | exe.defineCMacro("ZIG_LINK_MODE", "Static"); | 268 | artifact.addCSourceFiles(&stage1_sources, &exe_cflags); |
| | 269 | artifact.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" }); |
| 253 | | 270 | |
| 254 | exe.addCSourceFiles(&stage1_sources, &exe_cflags); | 271 | artifact.linkLibrary(softfloat); |
| 255 | exe.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" }); | 272 | artifact.linkLibCpp(); |
| | 273 | } |
| | 274 | |
| | 275 | try addStaticLlvmOptionsToExe(zig0); |
| | 276 | |
| | 277 | const zig1_obj_ext = target.getObjectFormat().fileExt(target.getCpuArch()); |
| | 278 | const zig1_obj_path = b.pathJoin(&.{ "zig-cache", "tmp", b.fmt("zig1{s}", .{zig1_obj_ext}) }); |
| | 279 | const zig1_compiler_rt_path = b.pathJoin(&.{ b.pathFromRoot("lib"), "std", "special", "compiler_rt.zig" }); |
| | 280 | |
| | 281 | const zig1_obj = zig0.run(); |
| | 282 | zig1_obj.addArgs(&.{ |
| | 283 | "src/stage1.zig", |
| | 284 | "-target", |
| | 285 | try target.zigTriple(b.allocator), |
| | 286 | "-mcpu=baseline", |
| | 287 | "--name", |
| | 288 | "zig1", |
| | 289 | "--zig-lib-dir", |
| | 290 | b.pathFromRoot("lib"), |
| | 291 | b.fmt("-femit-bin={s}", .{b.pathFromRoot(zig1_obj_path)}), |
| | 292 | "-fcompiler-rt", |
| | 293 | "-lc", |
| | 294 | }); |
| | 295 | { |
| | 296 | zig1_obj.addArgs(&.{ "--pkg-begin", "build_options" }); |
| | 297 | zig1_obj.addFileSourceArg(exe_options.getSource()); |
| | 298 | zig1_obj.addArgs(&.{ "--pkg-end", "--pkg-begin", "compiler_rt", zig1_compiler_rt_path, "--pkg-end" }); |
| | 299 | } |
| | 300 | switch (mode) { |
| | 301 | .Debug => {}, |
| | 302 | .ReleaseFast => { |
| | 303 | zig1_obj.addArg("-OReleaseFast"); |
| | 304 | zig1_obj.addArg("--strip"); |
| | 305 | }, |
| | 306 | .ReleaseSafe => { |
| | 307 | zig1_obj.addArg("-OReleaseSafe"); |
| | 308 | zig1_obj.addArg("--strip"); |
| | 309 | }, |
| | 310 | .ReleaseSmall => { |
| | 311 | zig1_obj.addArg("-OReleaseSmall"); |
| | 312 | zig1_obj.addArg("--strip"); |
| | 313 | }, |
| | 314 | } |
| | 315 | if (single_threaded orelse false) { |
| | 316 | zig1_obj.addArg("-fsingle-threaded"); |
| | 317 | } |
| 256 | | 318 | |
| 257 | exe.linkLibrary(softfloat); | 319 | if (use_zig0) { |
| 258 | exe.linkLibCpp(); | 320 | exe.step.dependOn(&zig1_obj.step); |
| | 321 | exe.addObjectFile(zig1_obj_path); |
| | 322 | } |
| 259 | | 323 | |
| 260 | // This is intentionally a dummy path. stage1.zig tries to @import("compiler_rt") in case | 324 | // This is intentionally a dummy path. stage1.zig tries to @import("compiler_rt") in case |
| 261 | // of being built by cmake. But when built by zig it's gonna get a compiler_rt so that | 325 | // of being built by cmake. But when built by zig it's gonna get a compiler_rt so that |