From 2f42a2617b55c497bc13a44f64a921e6d2620acf Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 17 Feb 2022 15:26:26 -0700 Subject: [PATCH 1/5] CI: more stage2 test coverage * Test everything on the Linux CI even if we can't run it, because it's our fastest machine. * Test stage2 using a build of stage2 (instead of using `-fno-stage1`) so that compiler-rt is also built with stage2. * Additionally test running x86_64-macos on the macOS CI, both the LLVM backend and x86_64 backend. --- ci/azure/macos_script | 7 +++++-- ci/zinc/linux_test.sh | 21 ++++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/ci/azure/macos_script b/ci/azure/macos_script index 446ad37bb5..160ce5fe03 100755 --- a/ci/azure/macos_script +++ b/ci/azure/macos_script @@ -55,8 +55,11 @@ make $JOBS install cmake .. -DZIG_EXECUTABLE="$(pwd)/release/bin/zig" make $JOBS install -# TODO figure out why this causes a segmentation fault -# release/bin/zig test ../test/behavior.zig -fno-stage1 -fLLVM -I ../test +# Build stage2 standalone so that we can test stage2 against stage2 compiler-rt. +release/bin/zig build -p stage2 -Denable-llvm + +stage2/bin/zig test ../test/behavior.zig -I../test -fLLVM +stage2/bin/zig test ../test/behavior.zig -I../test release/bin/zig build test-toolchain -Denable-macos-sdk release/bin/zig build test-std diff --git a/ci/zinc/linux_test.sh b/ci/zinc/linux_test.sh index 453269029d..775d6b78b4 100755 --- a/ci/zinc/linux_test.sh +++ b/ci/zinc/linux_test.sh @@ -4,13 +4,20 @@ ZIG=$DEBUG_STAGING/bin/zig -$ZIG test test/behavior.zig -fno-stage1 -I test -fLLVM -$ZIG test test/behavior.zig -fno-stage1 -I test -fLLVM -target aarch64-linux --test-cmd qemu-aarch64 --test-cmd-bin -$ZIG test test/behavior.zig -fno-stage1 -I test -ofmt=c -$ZIG test test/behavior.zig -fno-stage1 -I test -target wasm32-wasi --test-cmd wasmtime --test-cmd-bin -$ZIG test test/behavior.zig -fno-stage1 -I test -target arm-linux --test-cmd qemu-arm --test-cmd-bin -$ZIG test test/behavior.zig -fno-stage1 -I test -target aarch64-linux --test-cmd qemu-aarch64 --test-cmd-bin -$ZIG test test/behavior.zig -fno-stage1 -I test +# Build stage2 standalone so that we can test stage2 against stage2 compiler-rt. +$ZIG build -p stage2 -Denable-llvm + +stage2/bin/zig test test/behavior.zig -I test -fLLVM +stage2/bin/zig test test/behavior.zig -I test +stage2/bin/zig test test/behavior.zig -I test -fLLVM -target aarch64-linux --test-cmd qemu-aarch64 --test-cmd-bin +stage2/bin/zig test test/behavior.zig -I test -target aarch64-linux --test-cmd qemu-aarch64 --test-cmd-bin +stage2/bin/zig test test/behavior.zig -I test -ofmt=c +stage2/bin/zig test test/behavior.zig -I test -target wasm32-wasi --test-cmd wasmtime --test-cmd-bin +stage2/bin/zig test test/behavior.zig -I test -target arm-linux --test-cmd qemu-arm --test-cmd-bin +stage2/bin/zig test test/behavior.zig -I test -fLLVM -target aarch64-macos --test-no-exec +stage2/bin/zig test test/behavior.zig -I test -target aarch64-macos --test-no-exec +stage2/bin/zig test test/behavior.zig -I test -fLLVM -target x86_64-macos --test-no-exec +stage2/bin/zig test test/behavior.zig -I test -target x86_64-macos --test-no-exec $ZIG build test-behavior -fqemu -fwasmtime $ZIG build test-compiler-rt -fqemu -fwasmtime From 419074a81ba7d7111bae7d07af26aad9c7deeb1f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 17 Feb 2022 16:16:33 -0700 Subject: [PATCH 2/5] CI: simplify linux script * build.zig: remove detour through zig0. I'll add it back as an option later. This means you can build stage1 with a freshly built zig without detouring through zig0 again. * remove unused file windows_script.bat * drone.yml: remove unnecessary steps * ninja doesn't need a jobs parameter * build the release version of zig with zig build instead of cmake. * build stage2 with -Dstatic-llvm -Duse-zig-libcxx --- build.zig | 75 +++++-------------------------------- ci/azure/windows_script.bat | 8 ---- ci/zinc/drone.yml | 12 ------ ci/zinc/linux_base.sh | 1 - ci/zinc/linux_build.sh | 72 ----------------------------------- ci/zinc/linux_probe.sh | 10 ----- ci/zinc/linux_test.sh | 61 ++++++++++++++++++++++++++++-- 7 files changed, 67 insertions(+), 172 deletions(-) delete mode 100644 ci/azure/windows_script.bat delete mode 100755 ci/zinc/linux_build.sh delete mode 100755 ci/zinc/linux_probe.sh diff --git a/build.zig b/build.zig index fc8e6dced1..75c3ee61fa 100644 --- a/build.zig +++ b/build.zig @@ -136,7 +136,7 @@ pub fn build(b: *Builder) !void { break :blk 4; }; - const main_file: ?[]const u8 = if (is_stage1) null else "src/main.zig"; + const main_file: []const u8 = if (is_stage1) "src/stage1.zig" else "src/main.zig"; const exe = b.addExecutable("zig", main_file); exe.strip = strip; @@ -245,74 +245,17 @@ pub fn build(b: *Builder) !void { softfloat.addCSourceFiles(&softfloat_sources, &[_][]const u8{ "-std=c99", "-O3" }); softfloat.single_threaded = single_threaded; - const zig0 = b.addExecutable("zig0", null); - zig0.addCSourceFiles(&.{"src/stage1/zig0.cpp"}, &exe_cflags); - zig0.addIncludePath("zig-cache/tmp"); // for config.h - zig0.defineCMacro("ZIG_VERSION_MAJOR", b.fmt("{d}", .{zig_version.major})); - zig0.defineCMacro("ZIG_VERSION_MINOR", b.fmt("{d}", .{zig_version.minor})); - zig0.defineCMacro("ZIG_VERSION_PATCH", b.fmt("{d}", .{zig_version.patch})); - zig0.defineCMacro("ZIG_VERSION_STRING", b.fmt("\"{s}\"", .{version})); + exe.addIncludePath("src"); + exe.addIncludePath("deps/SoftFloat-3e/source/include"); + exe.addIncludePath("deps/SoftFloat-3e-prebuilt"); - for ([_]*std.build.LibExeObjStep{ zig0, exe, test_stage2 }) |artifact| { - artifact.addIncludePath("src"); - artifact.addIncludePath("deps/SoftFloat-3e/source/include"); - artifact.addIncludePath("deps/SoftFloat-3e-prebuilt"); + exe.defineCMacro("ZIG_LINK_MODE", "Static"); - artifact.defineCMacro("ZIG_LINK_MODE", "Static"); + exe.addCSourceFiles(&stage1_sources, &exe_cflags); + exe.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" }); - artifact.addCSourceFiles(&stage1_sources, &exe_cflags); - artifact.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" }); - - artifact.linkLibrary(softfloat); - artifact.linkLibCpp(); - } - - try addStaticLlvmOptionsToExe(zig0); - - const zig1_obj_ext = target.getObjectFormat().fileExt(target.getCpuArch()); - const zig1_obj_path = b.pathJoin(&.{ "zig-cache", "tmp", b.fmt("zig1{s}", .{zig1_obj_ext}) }); - const zig1_compiler_rt_path = b.pathJoin(&.{ b.pathFromRoot("lib"), "std", "special", "compiler_rt.zig" }); - - const zig1_obj = zig0.run(); - zig1_obj.addArgs(&.{ - "src/stage1.zig", - "-target", - try target.zigTriple(b.allocator), - "-mcpu=baseline", - "--name", - "zig1", - "--zig-lib-dir", - b.pathFromRoot("lib"), - b.fmt("-femit-bin={s}", .{b.pathFromRoot(zig1_obj_path)}), - "-fcompiler-rt", - "-lc", - }); - { - zig1_obj.addArgs(&.{ "--pkg-begin", "build_options" }); - zig1_obj.addFileSourceArg(exe_options.getSource()); - zig1_obj.addArgs(&.{ "--pkg-end", "--pkg-begin", "compiler_rt", zig1_compiler_rt_path, "--pkg-end" }); - } - switch (mode) { - .Debug => {}, - .ReleaseFast => { - zig1_obj.addArg("-OReleaseFast"); - zig1_obj.addArg("--strip"); - }, - .ReleaseSafe => { - zig1_obj.addArg("-OReleaseSafe"); - zig1_obj.addArg("--strip"); - }, - .ReleaseSmall => { - zig1_obj.addArg("-OReleaseSmall"); - zig1_obj.addArg("--strip"); - }, - } - if (single_threaded orelse false) { - zig1_obj.addArg("-fsingle-threaded"); - } - - exe.step.dependOn(&zig1_obj.step); - exe.addObjectFile(zig1_obj_path); + exe.linkLibrary(softfloat); + exe.linkLibCpp(); // This is intentionally a dummy path. stage1.zig tries to @import("compiler_rt") in case // of being built by cmake. But when built by zig it's gonna get a compiler_rt so that diff --git a/ci/azure/windows_script.bat b/ci/azure/windows_script.bat deleted file mode 100644 index 5002fce24b..0000000000 --- a/ci/azure/windows_script.bat +++ /dev/null @@ -1,8 +0,0 @@ -@echo on -SET "SRCROOT=%cd%" -SET "PREVPATH=%PATH%" -SET "PREVMSYSTEM=%MSYSTEM%" - -set "PATH=%CD:~0,2%\msys64\usr\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem" -SET "MSYSTEM=MINGW64" -bash -lc "cd ${SRCROOT} && ci/azure/windows_script" || exit /b diff --git a/ci/zinc/drone.yml b/ci/zinc/drone.yml index d6fac279e1..cb105381ae 100644 --- a/ci/zinc/drone.yml +++ b/ci/zinc/drone.yml @@ -9,19 +9,7 @@ workspace: path: /workspace steps: -- name: probe - image: ci/debian-amd64:11.1-3 - commands: - - ./ci/zinc/linux_probe.sh - -- name: build - image: ci/debian-amd64:11.1-3 - commands: - - ./ci/zinc/linux_build.sh - - name: test - depends_on: - - build image: ci/debian-amd64:11.1-3 commands: - ./ci/zinc/linux_test.sh diff --git a/ci/zinc/linux_base.sh b/ci/zinc/linux_base.sh index 5d33e19e67..f1e9924258 100755 --- a/ci/zinc/linux_base.sh +++ b/ci/zinc/linux_base.sh @@ -17,7 +17,6 @@ set -x set -e ARCH="$(uname -m)" -JOBS="-j$(nproc)" DEPS_LOCAL="/deps/local" WORKSPACE="$DRONE_WORKSPACE" diff --git a/ci/zinc/linux_build.sh b/ci/zinc/linux_build.sh deleted file mode 100755 index 1d468bbd25..0000000000 --- a/ci/zinc/linux_build.sh +++ /dev/null @@ -1,72 +0,0 @@ -#!/bin/sh - -. ./ci/zinc/linux_base.sh - -ZIG="$DEPS_LOCAL/bin/zig" -TARGET="${ARCH}-linux-musl" -MCPU="baseline" - -# Make the `zig version` number consistent. -# This will affect the cmake command below. -git config core.abbrev 9 - -# Build debug zig. -echo "BUILD debug zig with zig:$($ZIG version)" - -export CC="$ZIG cc -target $TARGET -mcpu=$MCPU" -export CXX="$ZIG c++ -target $TARGET -mcpu=$MCPU" - -mkdir _debug -cd _debug -cmake .. \ - -DCMAKE_INSTALL_PREFIX="$DEBUG_STAGING" \ - -DCMAKE_PREFIX_PATH="$DEPS_LOCAL" \ - -DCMAKE_BUILD_TYPE=Debug \ - -DZIG_TARGET_TRIPLE="$TARGET" \ - -DZIG_TARGET_MCPU="$MCPU" \ - -DZIG_STATIC=ON \ - -GNinja - -# Now cmake will use zig as the C/C++ compiler. We reset the environment variables -# so that installation and testing do not get affected by them. -unset CC -unset CXX - -ninja $JOBS install - -ZIG=$DEBUG_STAGING/bin/zig - -# Here we rebuild zig but this time using the Zig binary we just now produced to -# build zig1.o rather than relying on the one built with stage0. See -# https://github.com/ziglang/zig/issues/6830 for more details. -cmake .. -DZIG_EXECUTABLE="$ZIG" -ninja $JOBS install - -cd $WORKSPACE - -# Build release zig. -echo "BUILD release zig with zig:$($ZIG version)" -export CC="$ZIG cc -target $TARGET -mcpu=$MCPU" -export CXX="$ZIG c++ -target $TARGET -mcpu=$MCPU" -mkdir _release -cd _release -cmake .. \ - -DCMAKE_INSTALL_PREFIX="$RELEASE_STAGING" \ - -DCMAKE_PREFIX_PATH="$DEPS_LOCAL" \ - -DCMAKE_BUILD_TYPE=Release \ - -DZIG_TARGET_TRIPLE="$TARGET" \ - -DZIG_TARGET_MCPU="$MCPU" \ - -DZIG_STATIC=ON \ - -GNinja -unset CC -unset CXX -ninja $JOBS install - -cd $WORKSPACE - -# Look for non-conforming code formatting. -# Formatting errors can be fixed by running `zig fmt` on the files printed here. -$ZIG fmt --check . - -# Explicit exit helps show last command duration. -exit diff --git a/ci/zinc/linux_probe.sh b/ci/zinc/linux_probe.sh deleted file mode 100755 index 7c7d6c52fe..0000000000 --- a/ci/zinc/linux_probe.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/sh - -. ./ci/zinc/linux_base.sh - -# Probe CPU/brand details. -echo "lscpu:" -(lscpu | sed 's,^, : ,') 1>&2 - -# Explicit exit helps show last command duration. -exit diff --git a/ci/zinc/linux_test.sh b/ci/zinc/linux_test.sh index 775d6b78b4..79b3174f15 100755 --- a/ci/zinc/linux_test.sh +++ b/ci/zinc/linux_test.sh @@ -2,10 +2,53 @@ . ./ci/zinc/linux_base.sh -ZIG=$DEBUG_STAGING/bin/zig +ZIG="$DEPS_LOCAL/bin/zig" +TARGET="${ARCH}-linux-musl" +MCPU="baseline" + +# Make the `zig version` number consistent. +# This will affect the cmake command below. +git config core.abbrev 9 + +echo "BUILD debug zig with zig:$($ZIG version)" + +export CC="$ZIG cc -target $TARGET -mcpu=$MCPU" +export CXX="$ZIG c++ -target $TARGET -mcpu=$MCPU" + +mkdir _debug +cd _debug +cmake .. \ + -DCMAKE_INSTALL_PREFIX="$DEBUG_STAGING" \ + -DCMAKE_PREFIX_PATH="$DEPS_LOCAL" \ + -DCMAKE_BUILD_TYPE=Debug \ + -DZIG_TARGET_TRIPLE="$TARGET" \ + -DZIG_TARGET_MCPU="$MCPU" \ + -DZIG_STATIC=ON \ + -GNinja + +# Now cmake will use zig as the C/C++ compiler. We reset the environment variables +# so that installation and testing do not get affected by them. +unset CC +unset CXX + +ninja install + +ZIG="$DEBUG_STAGING/bin/zig" + +# Here we rebuild zig but this time using the Zig binary we just now produced to +# build zig1.o rather than relying on the one built with stage0. See +# https://github.com/ziglang/zig/issues/6830 for more details. +cmake .. -DZIG_EXECUTABLE="$ZIG" +ninja install + +cd $WORKSPACE + +# Look for non-conforming code formatting. +# Formatting errors can be fixed by running `zig fmt` on the files printed here. +$ZIG fmt --check . # Build stage2 standalone so that we can test stage2 against stage2 compiler-rt. -$ZIG build -p stage2 -Denable-llvm +$ZIG build -p stage2 -Denable-llvm -Dstatic-llvm -Duse-zig-libcxx stage2/bin/zig test test/behavior.zig -I test -fLLVM stage2/bin/zig test test/behavior.zig -I test @@ -38,7 +81,7 @@ $ZIG build test-fmt -fqemu -fwasmtime $ZIG build test-stage2 -fqemu -fwasmtime # Produce the experimental std lib documentation. -mkdir -p $RELEASE_STAGING/docs/std +mkdir -p "$RELEASE_STAGING/docs/std" $ZIG test lib/std/std.zig \ --zig-lib-dir lib \ -femit-docs=$RELEASE_STAGING/docs/std \ @@ -47,5 +90,17 @@ $ZIG test lib/std/std.zig \ # Look for HTML errors. tidy --drop-empty-elements no -qe zig-cache/langref.html +# Build release zig. +$ZIG build + --prefix "RELEASE_STAGING" \ + --search-prefix "$DEPS_LOCAL" \ + -Denable-llvm \ + -Dstatic-llvm \ + -Drelease \ + -Dstrip \ + -Duse-zig-libcxx \ + -Dtarget="$TARGET" \ + -Dstage1 + # Explicit exit helps show last command duration. exit From 6f42876e74831b7f9cecf5a863634df920c52b98 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 17 Feb 2022 16:55:12 -0700 Subject: [PATCH 3/5] CLI: resolve system libs into static libraries using the provided -L directories before checking if we need to integrate with system library paths. This prevents false positive of invoking system cc to find native paths when in fact all dependencies are satisfied by -L (or --search-prefix to zig build). --- build.zig | 4 +--- ci/zinc/linux_test.sh | 3 +-- src/main.zig | 42 +++++++++++++++++++++++++++++++++++------- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/build.zig b/build.zig index 75c3ee61fa..6f87d5ab8f 100644 --- a/build.zig +++ b/build.zig @@ -502,9 +502,7 @@ fn addCmakeCfgOptionsToExe( } } -fn addStaticLlvmOptionsToExe( - exe: *std.build.LibExeObjStep, -) !void { +fn addStaticLlvmOptionsToExe(exe: *std.build.LibExeObjStep) !void { // Adds the Zig C++ sources which both stage1 and stage2 need. // // We need this because otherwise zig_clang_cc1_main.cpp ends up pulling diff --git a/ci/zinc/linux_test.sh b/ci/zinc/linux_test.sh index 79b3174f15..ec3b3ad977 100755 --- a/ci/zinc/linux_test.sh +++ b/ci/zinc/linux_test.sh @@ -48,7 +48,7 @@ cd $WORKSPACE $ZIG fmt --check . # Build stage2 standalone so that we can test stage2 against stage2 compiler-rt. -$ZIG build -p stage2 -Denable-llvm -Dstatic-llvm -Duse-zig-libcxx +$ZIG build -p stage2 -Dstatic-llvm -Duse-zig-libcxx stage2/bin/zig test test/behavior.zig -I test -fLLVM stage2/bin/zig test test/behavior.zig -I test @@ -94,7 +94,6 @@ tidy --drop-empty-elements no -qe zig-cache/langref.html $ZIG build --prefix "RELEASE_STAGING" \ --search-prefix "$DEPS_LOCAL" \ - -Denable-llvm \ -Dstatic-llvm \ -Drelease \ -Dstrip \ diff --git a/src/main.zig b/src/main.zig index 05afc2b166..2abcc083a3 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2008,28 +2008,34 @@ fn buildOutputType( // are part of libc or libc++. We remove them from the list and communicate their // existence via flags instead. { + // Similarly, if any libs in this list are statically provided, we remove + // them from this list and populate the link_objects array instead. + const sep = fs.path.sep_str; + var test_path = std.ArrayList(u8).init(gpa); + defer test_path.deinit(); + var i: usize = 0; - while (i < system_libs.count()) { + syslib: while (i < system_libs.count()) { const lib_name = system_libs.keys()[i]; if (target_util.is_libc_lib_name(target_info.target, lib_name)) { link_libc = true; - _ = system_libs.orderedRemove(lib_name); + system_libs.orderedRemoveAt(i); continue; } if (target_util.is_libcpp_lib_name(target_info.target, lib_name)) { link_libcpp = true; - _ = system_libs.orderedRemove(lib_name); + system_libs.orderedRemoveAt(i); continue; } if (mem.eql(u8, lib_name, "unwind")) { link_libunwind = true; - _ = system_libs.orderedRemove(lib_name); + system_libs.orderedRemoveAt(i); continue; } if (target_util.is_compiler_rt_lib_name(target_info.target, lib_name)) { std.log.warn("ignoring superfluous library '{s}': this dependency is fulfilled instead by compiler-rt which zig unconditionally provides", .{lib_name}); - _ = system_libs.orderedRemove(lib_name); + system_libs.orderedRemoveAt(i); continue; } if (std.fs.path.isAbsolute(lib_name)) { @@ -2038,10 +2044,30 @@ fn buildOutputType( if (target_info.target.os.tag == .wasi) { if (wasi_libc.getEmulatedLibCRTFile(lib_name)) |crt_file| { try wasi_emulated_libs.append(crt_file); - _ = system_libs.orderedRemove(lib_name); + system_libs.orderedRemoveAt(i); continue; } } + + for (lib_dirs.items) |lib_dir_path| { + test_path.clearRetainingCapacity(); + try test_path.writer().print("{s}" ++ sep ++ "{s}{s}{s}", .{ + lib_dir_path, + target_info.target.libPrefix(), + lib_name, + target_info.target.staticLibSuffix(), + }); + fs.cwd().access(test_path.items, .{}) catch |err| switch (err) { + error.FileNotFound => continue, + else => |e| fatal("unable to search for static library '{s}': {s}", .{ + test_path.items, @errorName(e), + }), + }; + try link_objects.append(.{ .path = try arena.dupe(u8, test_path.items) }); + system_libs.orderedRemoveAt(i); + continue :syslib; + } + i += 1; } } @@ -2068,7 +2094,9 @@ fn buildOutputType( want_native_include_dirs = true; } - if (sysroot == null and cross_target.isNativeOs() and (system_libs.count() != 0 or want_native_include_dirs)) { + if (sysroot == null and cross_target.isNativeOs() and + (system_libs.count() != 0 or want_native_include_dirs)) + { const paths = std.zig.system.NativePaths.detect(arena, target_info) catch |err| { fatal("unable to detect native system paths: {s}", .{@errorName(err)}); }; From dd12a7b1e522c43cbbf382acedad063d419694f4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 17 Feb 2022 17:20:29 -0700 Subject: [PATCH 4/5] ci: linux: re-use cmake config for finding LLVM --- ci/zinc/linux_test.sh | 10 +++++----- src/main.zig | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/ci/zinc/linux_test.sh b/ci/zinc/linux_test.sh index ec3b3ad977..969e19312e 100755 --- a/ci/zinc/linux_test.sh +++ b/ci/zinc/linux_test.sh @@ -48,7 +48,7 @@ cd $WORKSPACE $ZIG fmt --check . # Build stage2 standalone so that we can test stage2 against stage2 compiler-rt. -$ZIG build -p stage2 -Dstatic-llvm -Duse-zig-libcxx +$ZIG build -p stage2 -Denable-llvm -Duse-zig-libcxx stage2/bin/zig test test/behavior.zig -I test -fLLVM stage2/bin/zig test test/behavior.zig -I test @@ -91,13 +91,13 @@ $ZIG test lib/std/std.zig \ tidy --drop-empty-elements no -qe zig-cache/langref.html # Build release zig. -$ZIG build - --prefix "RELEASE_STAGING" \ +$ZIG build \ + --prefix "$RELEASE_STAGING" \ --search-prefix "$DEPS_LOCAL" \ - -Dstatic-llvm \ + -Denable-llvm \ + -Duse-zig-libcxx \ -Drelease \ -Dstrip \ - -Duse-zig-libcxx \ -Dtarget="$TARGET" \ -Dstage1 diff --git a/src/main.zig b/src/main.zig index 2abcc083a3..248ae2dd76 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2068,6 +2068,8 @@ fn buildOutputType( continue :syslib; } + std.log.scoped(.cli).debug("depending on system for -l{s}", .{lib_name}); + i += 1; } } From eee500c0b59487b78294696160a976f20fe64829 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 17 Feb 2022 23:49:13 -0700 Subject: [PATCH 5/5] build.zig: restore the ability to bootstrap with zig0 via option --- build.zig | 82 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 73 insertions(+), 9 deletions(-) diff --git a/build.zig b/build.zig index 6f87d5ab8f..5896ab1a8c 100644 --- a/build.zig +++ b/build.zig @@ -129,6 +129,7 @@ pub fn build(b: *Builder) !void { const force_gpa = b.option(bool, "force-gpa", "Force the compiler to use GeneralPurposeAllocator") orelse false; const link_libc = b.option(bool, "force-link-libc", "Force self-hosted compiler to link libc") orelse enable_llvm; const strip = b.option(bool, "strip", "Omit debug information") orelse false; + const use_zig0 = b.option(bool, "zig0", "Bootstrap using zig0") orelse false; 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: { if (strip) break :blk @as(u32, 0); @@ -136,7 +137,11 @@ pub fn build(b: *Builder) !void { break :blk 4; }; - const main_file: []const u8 = if (is_stage1) "src/stage1.zig" else "src/main.zig"; + const main_file: ?[]const u8 = mf: { + if (!is_stage1) break :mf "src/main.zig"; + if (use_zig0) break :mf null; + break :mf "src/stage1.zig"; + }; const exe = b.addExecutable("zig", main_file); exe.strip = strip; @@ -245,17 +250,76 @@ pub fn build(b: *Builder) !void { softfloat.addCSourceFiles(&softfloat_sources, &[_][]const u8{ "-std=c99", "-O3" }); softfloat.single_threaded = single_threaded; - exe.addIncludePath("src"); - exe.addIncludePath("deps/SoftFloat-3e/source/include"); - exe.addIncludePath("deps/SoftFloat-3e-prebuilt"); + const zig0 = b.addExecutable("zig0", null); + zig0.addCSourceFiles(&.{"src/stage1/zig0.cpp"}, &exe_cflags); + zig0.addIncludePath("zig-cache/tmp"); // for config.h + zig0.defineCMacro("ZIG_VERSION_MAJOR", b.fmt("{d}", .{zig_version.major})); + zig0.defineCMacro("ZIG_VERSION_MINOR", b.fmt("{d}", .{zig_version.minor})); + zig0.defineCMacro("ZIG_VERSION_PATCH", b.fmt("{d}", .{zig_version.patch})); + zig0.defineCMacro("ZIG_VERSION_STRING", b.fmt("\"{s}\"", .{version})); - exe.defineCMacro("ZIG_LINK_MODE", "Static"); + for ([_]*std.build.LibExeObjStep{ zig0, exe, test_stage2 }) |artifact| { + artifact.addIncludePath("src"); + artifact.addIncludePath("deps/SoftFloat-3e/source/include"); + artifact.addIncludePath("deps/SoftFloat-3e-prebuilt"); - exe.addCSourceFiles(&stage1_sources, &exe_cflags); - exe.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" }); + artifact.defineCMacro("ZIG_LINK_MODE", "Static"); - exe.linkLibrary(softfloat); - exe.linkLibCpp(); + artifact.addCSourceFiles(&stage1_sources, &exe_cflags); + artifact.addCSourceFiles(&optimized_c_sources, &[_][]const u8{ "-std=c99", "-O3" }); + + artifact.linkLibrary(softfloat); + artifact.linkLibCpp(); + } + + try addStaticLlvmOptionsToExe(zig0); + + const zig1_obj_ext = target.getObjectFormat().fileExt(target.getCpuArch()); + const zig1_obj_path = b.pathJoin(&.{ "zig-cache", "tmp", b.fmt("zig1{s}", .{zig1_obj_ext}) }); + const zig1_compiler_rt_path = b.pathJoin(&.{ b.pathFromRoot("lib"), "std", "special", "compiler_rt.zig" }); + + const zig1_obj = zig0.run(); + zig1_obj.addArgs(&.{ + "src/stage1.zig", + "-target", + try target.zigTriple(b.allocator), + "-mcpu=baseline", + "--name", + "zig1", + "--zig-lib-dir", + b.pathFromRoot("lib"), + b.fmt("-femit-bin={s}", .{b.pathFromRoot(zig1_obj_path)}), + "-fcompiler-rt", + "-lc", + }); + { + zig1_obj.addArgs(&.{ "--pkg-begin", "build_options" }); + zig1_obj.addFileSourceArg(exe_options.getSource()); + zig1_obj.addArgs(&.{ "--pkg-end", "--pkg-begin", "compiler_rt", zig1_compiler_rt_path, "--pkg-end" }); + } + switch (mode) { + .Debug => {}, + .ReleaseFast => { + zig1_obj.addArg("-OReleaseFast"); + zig1_obj.addArg("--strip"); + }, + .ReleaseSafe => { + zig1_obj.addArg("-OReleaseSafe"); + zig1_obj.addArg("--strip"); + }, + .ReleaseSmall => { + zig1_obj.addArg("-OReleaseSmall"); + zig1_obj.addArg("--strip"); + }, + } + if (single_threaded orelse false) { + zig1_obj.addArg("-fsingle-threaded"); + } + + if (use_zig0) { + exe.step.dependOn(&zig1_obj.step); + exe.addObjectFile(zig1_obj_path); + } // This is intentionally a dummy path. stage1.zig tries to @import("compiler_rt") in case // of being built by cmake. But when built by zig it's gonna get a compiler_rt so that