authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-16 13:50:47-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-16 14:12:15-07:00
logcc9eb9e90fec961a74e90e1ab3d66a7b8ec14f47
treea6901d6182774785c24697d1232afb5696800955
parent2b3df5c81d8455856c2b355694ce818385218f36

build.zig: bootstrap stage1 with zig0

Instead of assuming that the zig version used with `zig build` is appropriate for building the self-hosted compiler component, we follow the same path as the cmake build script and build zig0, using that to produce zig1.o, re-linking to produce stage1. This allows an arbitrarily old Zig version and corresponding build.zig script to build all future versions of Zig from source via the bootstrap compiler. In other words, it allows us to use `zig build` as an alternative to cmake when bootstrapping.

1 files changed, 141 insertions(+), 57 deletions(-)

build.zig+141-57
......@@ -19,7 +19,7 @@ pub fn build(b: *Builder) !void {
1919 const single_threaded = b.option(bool, "single-threaded", "Build artifacts that run in single threaded mode");
2020 const use_zig_libcxx = b.option(bool, "use-zig-libcxx", "If libc++ is needed, use zig's bundled version, don't try to integrate with the system") orelse false;
2121
22 var docgen_exe = b.addExecutable("docgen", "doc/docgen.zig");
22 const docgen_exe = b.addExecutable("docgen", "doc/docgen.zig");
2323 docgen_exe.single_threaded = single_threaded;
2424
2525 const rel_zig_exe = try fs.path.relative(b.allocator, b.build_root, b.zig_exe);
......@@ -27,7 +27,7 @@ pub fn build(b: *Builder) !void {
2727 b.allocator,
2828 &[_][]const u8{ b.cache_root, "langref.html" },
2929 ) catch unreachable;
30 var docgen_cmd = docgen_exe.run();
30 const docgen_cmd = docgen_exe.run();
3131 docgen_cmd.addArgs(&[_][]const u8{
3232 rel_zig_exe,
3333 "doc" ++ fs.path.sep_str ++ "langref.html.in",
......@@ -60,6 +60,7 @@ pub fn build(b: *Builder) !void {
6060 const skip_install_lib_files = b.option(bool, "skip-install-lib-files", "Do not copy lib/ files to installation prefix") orelse false;
6161
6262 const only_install_lib_files = b.option(bool, "lib-files-only", "Only install library files") orelse false;
63
6364 const is_stage1 = b.option(bool, "stage1", "Build the stage1 compiler, put stage2 behind a feature flag") orelse false;
6465 const omit_stage2 = b.option(bool, "omit-stage2", "Do not include stage2 behind a feature flag inside stage1") orelse false;
6566 const static_llvm = b.option(bool, "static-llvm", "Disable integration with system-installed LLVM, Clang, LLD, and libc++") orelse false;
......@@ -135,9 +136,9 @@ pub fn build(b: *Builder) !void {
135136 break :blk 4;
136137 };
137138
138 const main_file = if (is_stage1) "src/stage1.zig" else "src/main.zig";
139 const main_file: ?[]const u8 = if (is_stage1) null else "src/main.zig";
139140
140 var exe = b.addExecutable("zig", main_file);
141 const exe = b.addExecutable("zig", main_file);
141142 exe.strip = strip;
142143 exe.install();
143144 exe.setBuildMode(mode);
......@@ -145,6 +146,7 @@ pub fn build(b: *Builder) !void {
145146 if (!skip_stage2_tests) {
146147 toolchain_step.dependOn(&exe.step);
147148 }
149
148150 b.default_step.dependOn(&exe.step);
149151 exe.single_threaded = single_threaded;
150152
......@@ -166,56 +168,6 @@ pub fn build(b: *Builder) !void {
166168 exe_options.addOption(bool, "llvm_has_arc", llvm_has_arc);
167169 exe_options.addOption(bool, "force_gpa", force_gpa);
168170
169 if (enable_llvm) {
170 const cmake_cfg = if (static_llvm) null else findAndParseConfigH(b, config_h_path_option);
171
172 if (is_stage1) {
173 exe.addIncludePath("src");
174 exe.addIncludePath("deps/SoftFloat-3e/source/include");
175
176 test_stage2.addIncludePath("src");
177 test_stage2.addIncludePath("deps/SoftFloat-3e/source/include");
178 // This is intentionally a dummy path. stage1.zig tries to @import("compiler_rt") in case
179 // of being built by cmake. But when built by zig it's gonna get a compiler_rt so that
180 // is pointless.
181 exe.addPackagePath("compiler_rt", "src/empty.zig");
182 exe.defineCMacro("ZIG_LINK_MODE", "Static");
183 test_stage2.defineCMacro("ZIG_LINK_MODE", "Static");
184
185 const softfloat = b.addStaticLibrary("softfloat", null);
186 softfloat.setBuildMode(.ReleaseFast);
187 softfloat.setTarget(target);
188 softfloat.addIncludePath("deps/SoftFloat-3e-prebuilt");
189 softfloat.addIncludePath("deps/SoftFloat-3e/source/8086");
190 softfloat.addIncludePath("deps/SoftFloat-3e/source/include");
191 softfloat.addCSourceFiles(&softfloat_sources, &[_][]const u8{ "-std=c99", "-O3" });
192 softfloat.single_threaded = single_threaded;
193
194 exe.linkLibrary(softfloat);
195 test_stage2.linkLibrary(softfloat);
196
197 exe.addCSourceFiles(&stage1_sources, &exe_cflags);
198 exe.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" });
199
200 test_stage2.addCSourceFiles(&stage1_sources, &exe_cflags);
201 test_stage2.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" });
202 }
203 if (cmake_cfg) |cfg| {
204 // Inside this code path, we have to coordinate with system packaged LLVM, Clang, and LLD.
205 // That means we also have to rely on stage1 compiled c++ files. We parse config.h to find
206 // the information passed on to us from cmake.
207 if (cfg.cmake_prefix_path.len > 0) {
208 b.addSearchPrefix(cfg.cmake_prefix_path);
209 }
210
211 try addCmakeCfgOptionsToExe(b, cfg, exe, use_zig_libcxx);
212 try addCmakeCfgOptionsToExe(b, cfg, test_stage2, use_zig_libcxx);
213 } else {
214 // Here we are -Denable-llvm but no cmake integration.
215 try addStaticLlvmOptionsToExe(exe);
216 try addStaticLlvmOptionsToExe(test_stage2);
217 }
218 }
219171 if (link_libc) {
220172 exe.linkLibC();
221173 test_stage2.linkLibC();
......@@ -227,12 +179,12 @@ pub fn build(b: *Builder) !void {
227179
228180 const opt_version_string = b.option([]const u8, "version-string", "Override Zig version string. Default is to find out with git.");
229181 const version = if (opt_version_string) |version| version else v: {
230 const version_string = b.fmt("{d}.{d}.{d}", .{ zig_version.major, zig_version.minor, zig_version.patch });
231
232182 if (!std.process.can_spawn) {
233183 std.debug.print("error: version info cannot be retrieved from git. Zig version must be provided using -Dversion-string\n", .{});
234184 std.process.exit(1);
235185 }
186 const version_string = b.fmt("{d}.{d}.{d}", .{ zig_version.major, zig_version.minor, zig_version.patch });
187
236188 var code: u8 = undefined;
237189 const git_describe_untrimmed = b.execAllowFail(&[_][]const u8{
238190 "git", "-C", b.build_root, "describe", "--match", "*.*.*", "--tags",
......@@ -280,6 +232,108 @@ pub fn build(b: *Builder) !void {
280232 };
281233 exe_options.addOption([:0]const u8, "version", try b.allocator.dupeZ(u8, version));
282234
235 if (enable_llvm) {
236 const cmake_cfg = if (static_llvm) null else findAndParseConfigH(b, config_h_path_option);
237
238 if (is_stage1) {
239 const softfloat = b.addStaticLibrary("softfloat", null);
240 softfloat.setBuildMode(.ReleaseFast);
241 softfloat.setTarget(target);
242 softfloat.addIncludePath("deps/SoftFloat-3e-prebuilt");
243 softfloat.addIncludePath("deps/SoftFloat-3e/source/8086");
244 softfloat.addIncludePath("deps/SoftFloat-3e/source/include");
245 softfloat.addCSourceFiles(&softfloat_sources, &[_][]const u8{ "-std=c99", "-O3" });
246 softfloat.single_threaded = single_threaded;
247
248 const zig0 = b.addExecutable("zig0", null);
249 zig0.addCSourceFiles(&.{"src/stage1/zig0.cpp"}, &exe_cflags);
250 zig0.addIncludePath("zig-cache/tmp"); // for config.h
251 zig0.defineCMacro("ZIG_VERSION_MAJOR", b.fmt("{d}", .{zig_version.major}));
252 zig0.defineCMacro("ZIG_VERSION_MINOR", b.fmt("{d}", .{zig_version.minor}));
253 zig0.defineCMacro("ZIG_VERSION_PATCH", b.fmt("{d}", .{zig_version.patch}));
254 zig0.defineCMacro("ZIG_VERSION_STRING", b.fmt("\"{s}\"", .{version}));
255
256 for ([_]*std.build.LibExeObjStep{ zig0, exe }) |artifact| {
257 artifact.addIncludePath("src");
258 artifact.addIncludePath("deps/SoftFloat-3e/source/include");
259 artifact.addIncludePath("deps/SoftFloat-3e-prebuilt");
260
261 artifact.defineCMacro("ZIG_LINK_MODE", "Static");
262
263 artifact.addCSourceFiles(&stage1_sources, &exe_cflags);
264 artifact.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" });
265
266 artifact.linkLibrary(softfloat);
267 artifact.linkLibCpp();
268 }
269
270 try addStaticLlvmOptionsToExe(zig0);
271
272 const zig1_obj_ext = target.getObjectFormat().fileExt(target.getCpuArch());
273 const zig1_obj_path = b.pathJoin(&.{ "zig-cache", "tmp", b.fmt("zig1{s}", .{zig1_obj_ext}) });
274 const zig1_compiler_rt_path = b.pathJoin(&.{ b.pathFromRoot("lib"), "std", "special", "compiler_rt.zig" });
275
276 const zig1_obj = zig0.run();
277 zig1_obj.addArgs(&.{
278 "src/stage1.zig",
279 "-target",
280 try target.zigTriple(b.allocator),
281 "-mcpu=baseline",
282 "--name",
283 "zig1",
284 "--zig-lib-dir",
285 b.pathFromRoot("lib"),
286 b.fmt("-femit-bin={s}", .{b.pathFromRoot(zig1_obj_path)}),
287 "-fcompiler-rt",
288 "-lc",
289 });
290 {
291 zig1_obj.addArgs(&.{ "--pkg-begin", "build_options" });
292 zig1_obj.addFileSourceArg(exe_options.getSource());
293 zig1_obj.addArgs(&.{ "--pkg-end", "--pkg-begin", "compiler_rt", zig1_compiler_rt_path, "--pkg-end" });
294 }
295 switch (mode) {
296 .Debug => {},
297 .ReleaseFast => {
298 zig1_obj.addArg("-OReleaseFast");
299 zig1_obj.addArg("--strip");
300 },
301 .ReleaseSafe => {
302 zig1_obj.addArg("-OReleaseSafe");
303 zig1_obj.addArg("--strip");
304 },
305 .ReleaseSmall => {
306 zig1_obj.addArg("-OReleaseSmall");
307 zig1_obj.addArg("--strip");
308 },
309 }
310 if (single_threaded orelse false) {
311 zig1_obj.addArg("-fsingle-threaded");
312 }
313
314 exe.step.dependOn(&zig1_obj.step);
315 exe.addObjectFile(zig1_obj_path);
316
317 // This is intentionally a dummy path. stage1.zig tries to @import("compiler_rt") in case
318 // of being built by cmake. But when built by zig it's gonna get a compiler_rt so that
319 // is pointless.
320 exe.addPackagePath("compiler_rt", "src/empty.zig");
321 }
322 if (cmake_cfg) |cfg| {
323 // Inside this code path, we have to coordinate with system packaged LLVM, Clang, and LLD.
324 // That means we also have to rely on stage1 compiled c++ files. We parse config.h to find
325 // the information passed on to us from cmake.
326 if (cfg.cmake_prefix_path.len > 0) {
327 b.addSearchPrefix(cfg.cmake_prefix_path);
328 }
329
330 try addCmakeCfgOptionsToExe(b, cfg, exe, use_zig_libcxx);
331 } else {
332 // Here we are -Denable-llvm but no cmake integration.
333 try addStaticLlvmOptionsToExe(exe);
334 }
335 }
336
283337 const semver = try std.SemanticVersion.parse(version);
284338 exe_options.addOption(std.SemanticVersion, "semver", semver);
285339
......@@ -548,7 +602,6 @@ fn addCxxKnownPath(
548602) !void {
549603 if (!std.process.can_spawn)
550604 return error.RequiredLibraryNotFound;
551
552605 const path_padded = try b.exec(&[_][]const u8{
553606 ctx.cxx_compiler,
554607 b.fmt("-print-file-name={s}", .{objname}),
......@@ -695,15 +748,19 @@ fn toNativePathSep(b: *Builder, s: []const u8) []u8 {
695748
696749const softfloat_sources = [_][]const u8{
697750 "deps/SoftFloat-3e/source/8086/f128M_isSignalingNaN.c",
751 "deps/SoftFloat-3e/source/8086/extF80M_isSignalingNaN.c",
698752 "deps/SoftFloat-3e/source/8086/s_commonNaNToF128M.c",
753 "deps/SoftFloat-3e/source/8086/s_commonNaNToExtF80M.c",
699754 "deps/SoftFloat-3e/source/8086/s_commonNaNToF16UI.c",
700755 "deps/SoftFloat-3e/source/8086/s_commonNaNToF32UI.c",
701756 "deps/SoftFloat-3e/source/8086/s_commonNaNToF64UI.c",
702757 "deps/SoftFloat-3e/source/8086/s_f128MToCommonNaN.c",
758 "deps/SoftFloat-3e/source/8086/s_extF80MToCommonNaN.c",
703759 "deps/SoftFloat-3e/source/8086/s_f16UIToCommonNaN.c",
704760 "deps/SoftFloat-3e/source/8086/s_f32UIToCommonNaN.c",
705761 "deps/SoftFloat-3e/source/8086/s_f64UIToCommonNaN.c",
706762 "deps/SoftFloat-3e/source/8086/s_propagateNaNF128M.c",
763 "deps/SoftFloat-3e/source/8086/s_propagateNaNExtF80M.c",
707764 "deps/SoftFloat-3e/source/8086/s_propagateNaNF16UI.c",
708765 "deps/SoftFloat-3e/source/8086/softfloat_raiseFlags.c",
709766 "deps/SoftFloat-3e/source/f128M_add.c",
......@@ -723,6 +780,7 @@ const softfloat_sources = [_][]const u8{
723780 "deps/SoftFloat-3e/source/f128M_to_f16.c",
724781 "deps/SoftFloat-3e/source/f128M_to_f32.c",
725782 "deps/SoftFloat-3e/source/f128M_to_f64.c",
783 "deps/SoftFloat-3e/source/f128M_to_extF80M.c",
726784 "deps/SoftFloat-3e/source/f128M_to_i32.c",
727785 "deps/SoftFloat-3e/source/f128M_to_i32_r_minMag.c",
728786 "deps/SoftFloat-3e/source/f128M_to_i64.c",
......@@ -731,6 +789,20 @@ const softfloat_sources = [_][]const u8{
731789 "deps/SoftFloat-3e/source/f128M_to_ui32_r_minMag.c",
732790 "deps/SoftFloat-3e/source/f128M_to_ui64.c",
733791 "deps/SoftFloat-3e/source/f128M_to_ui64_r_minMag.c",
792 "deps/SoftFloat-3e/source/extF80M_add.c",
793 "deps/SoftFloat-3e/source/extF80M_div.c",
794 "deps/SoftFloat-3e/source/extF80M_eq.c",
795 "deps/SoftFloat-3e/source/extF80M_le.c",
796 "deps/SoftFloat-3e/source/extF80M_lt.c",
797 "deps/SoftFloat-3e/source/extF80M_mul.c",
798 "deps/SoftFloat-3e/source/extF80M_rem.c",
799 "deps/SoftFloat-3e/source/extF80M_roundToInt.c",
800 "deps/SoftFloat-3e/source/extF80M_sqrt.c",
801 "deps/SoftFloat-3e/source/extF80M_sub.c",
802 "deps/SoftFloat-3e/source/extF80M_to_f16.c",
803 "deps/SoftFloat-3e/source/extF80M_to_f32.c",
804 "deps/SoftFloat-3e/source/extF80M_to_f64.c",
805 "deps/SoftFloat-3e/source/extF80M_to_f128M.c",
734806 "deps/SoftFloat-3e/source/f16_add.c",
735807 "deps/SoftFloat-3e/source/f16_div.c",
736808 "deps/SoftFloat-3e/source/f16_eq.c",
......@@ -742,9 +814,12 @@ const softfloat_sources = [_][]const u8{
742814 "deps/SoftFloat-3e/source/f16_roundToInt.c",
743815 "deps/SoftFloat-3e/source/f16_sqrt.c",
744816 "deps/SoftFloat-3e/source/f16_sub.c",
817 "deps/SoftFloat-3e/source/f16_to_extF80M.c",
745818 "deps/SoftFloat-3e/source/f16_to_f128M.c",
746819 "deps/SoftFloat-3e/source/f16_to_f64.c",
820 "deps/SoftFloat-3e/source/f32_to_extF80M.c",
747821 "deps/SoftFloat-3e/source/f32_to_f128M.c",
822 "deps/SoftFloat-3e/source/f64_to_extF80M.c",
748823 "deps/SoftFloat-3e/source/f64_to_f128M.c",
749824 "deps/SoftFloat-3e/source/f64_to_f16.c",
750825 "deps/SoftFloat-3e/source/i32_to_f128M.c",
......@@ -752,6 +827,7 @@ const softfloat_sources = [_][]const u8{
752827 "deps/SoftFloat-3e/source/s_addCarryM.c",
753828 "deps/SoftFloat-3e/source/s_addComplCarryM.c",
754829 "deps/SoftFloat-3e/source/s_addF128M.c",
830 "deps/SoftFloat-3e/source/s_addExtF80M.c",
755831 "deps/SoftFloat-3e/source/s_addM.c",
756832 "deps/SoftFloat-3e/source/s_addMagsF16.c",
757833 "deps/SoftFloat-3e/source/s_addMagsF32.c",
......@@ -762,12 +838,14 @@ const softfloat_sources = [_][]const u8{
762838 "deps/SoftFloat-3e/source/s_approxRecip_1Ks.c",
763839 "deps/SoftFloat-3e/source/s_compare128M.c",
764840 "deps/SoftFloat-3e/source/s_compare96M.c",
841 "deps/SoftFloat-3e/source/s_compareNonnormExtF80M.c",
765842 "deps/SoftFloat-3e/source/s_countLeadingZeros16.c",
766843 "deps/SoftFloat-3e/source/s_countLeadingZeros32.c",
767844 "deps/SoftFloat-3e/source/s_countLeadingZeros64.c",
768845 "deps/SoftFloat-3e/source/s_countLeadingZeros8.c",
769846 "deps/SoftFloat-3e/source/s_eq128.c",
770847 "deps/SoftFloat-3e/source/s_invalidF128M.c",
848 "deps/SoftFloat-3e/source/s_invalidExtF80M.c",
771849 "deps/SoftFloat-3e/source/s_isNaNF128M.c",
772850 "deps/SoftFloat-3e/source/s_le128.c",
773851 "deps/SoftFloat-3e/source/s_lt128.c",
......@@ -778,7 +856,9 @@ const softfloat_sources = [_][]const u8{
778856 "deps/SoftFloat-3e/source/s_mulAddF32.c",
779857 "deps/SoftFloat-3e/source/s_mulAddF64.c",
780858 "deps/SoftFloat-3e/source/s_negXM.c",
859 "deps/SoftFloat-3e/source/s_normExtF80SigM.c",
781860 "deps/SoftFloat-3e/source/s_normRoundPackMToF128M.c",
861 "deps/SoftFloat-3e/source/s_normRoundPackMToExtF80M.c",
782862 "deps/SoftFloat-3e/source/s_normRoundPackToF16.c",
783863 "deps/SoftFloat-3e/source/s_normRoundPackToF32.c",
784864 "deps/SoftFloat-3e/source/s_normRoundPackToF64.c",
......@@ -789,6 +869,7 @@ const softfloat_sources = [_][]const u8{
789869 "deps/SoftFloat-3e/source/s_remStepMBy32.c",
790870 "deps/SoftFloat-3e/source/s_roundMToI64.c",
791871 "deps/SoftFloat-3e/source/s_roundMToUI64.c",
872 "deps/SoftFloat-3e/source/s_roundPackMToExtF80M.c",
792873 "deps/SoftFloat-3e/source/s_roundPackMToF128M.c",
793874 "deps/SoftFloat-3e/source/s_roundPackToF16.c",
794875 "deps/SoftFloat-3e/source/s_roundPackToF32.c",
......@@ -817,9 +898,12 @@ const softfloat_sources = [_][]const u8{
817898 "deps/SoftFloat-3e/source/s_subMagsF32.c",
818899 "deps/SoftFloat-3e/source/s_subMagsF64.c",
819900 "deps/SoftFloat-3e/source/s_tryPropagateNaNF128M.c",
901 "deps/SoftFloat-3e/source/s_tryPropagateNaNExtF80M.c",
820902 "deps/SoftFloat-3e/source/softfloat_state.c",
821903 "deps/SoftFloat-3e/source/ui32_to_f128M.c",
822904 "deps/SoftFloat-3e/source/ui64_to_f128M.c",
905 "deps/SoftFloat-3e/source/ui32_to_extF80M.c",
906 "deps/SoftFloat-3e/source/ui64_to_extF80M.c",
823907};
824908
825909const stage1_sources = [_][]const u8{