Skip to content

Commit 6e4fff6

Browse files
committed
move installation logic to the build script where it belongs
* build.zig: introduce `-Dflat` option which makes the installation match what we want to ship for our download tarballs. This allows deleting a bunch of shell script logic from the CI. - for example it puts the executable directly in prefix/zig rather than prefix/bin/zig and it additionally includes prefix/LICENSE. * build.zig: by default also install std lib documentation to doc/std/ - this can be disabled by `-Dno-autodocs` similar to how there is already `-Dno-langref`. * build.zig: add `std-docs` and `langref` steps which build and install the std lib autodocs and langref to prefix/doc/std and prefix/doc/langref.html, respectively. * std.Build: implement proper handling of `-femit-docs` using the LazyPath system. This is a breaking change. - this is a partial implementation of #16351 * frontend: fixed the handling of Autodocs with regards to caching and putting the artifacts in the proper location to integrate with the build system. - closes #15864 * CI: delete the logic for autodocs since it is now handled by build.zig and is enabled by default. - in the future we should strive to have nearly all the CI shell script logic deleted in favor of `zig build` commands. * CI: pass `-DZIG_NO_LIB=ON`/`-Dno-lib` except for the one command where we want to actually generate the langref and autodocs. Generating the langref takes 14 minutes right now (why?!) so we don't want to do that more times than necessary. * Autodoc: fixed use of a global variable. It works fine as a local variable instead. - note that in the future we will want to make Autodoc run simultaneously using the job system, but for now the principle of YAGNI dictates that we don't have an init()/deinit() API and instead simply call the function that does the things. * Autodoc: only do it when there are no compile errors
1 parent 1291f4a commit 6e4fff6

16 files changed

+215
-137
lines changed

build.zig

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ pub fn build(b: *std.Build) !void {
2424

2525
const optimize = b.standardOptimizeOption(.{});
2626

27+
const flat = b.option(bool, "flat", "Put files into the installation prefix in a manner suited for upstream distribution rather than a posix file system hierarchy standard") orelse false;
2728
const single_threaded = b.option(bool, "single-threaded", "Build artifacts that run in single threaded mode");
2829
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;
2930

3031
const test_step = b.step("test", "Run all the tests");
3132
const skip_install_lib_files = b.option(bool, "no-lib", "skip copying of lib/ files and langref to installation prefix. Useful for development") orelse false;
3233
const skip_install_langref = b.option(bool, "no-langref", "skip copying of langref to the installation prefix") orelse skip_install_lib_files;
34+
const skip_install_autodocs = b.option(bool, "no-autodocs", "skip copying of standard library autodocs to the installation prefix") orelse skip_install_lib_files;
3335
const no_bin = b.option(bool, "no-bin", "skip emitting compiler binary") orelse false;
3436

3537
const docgen_exe = b.addExecutable(.{
@@ -52,8 +54,34 @@ pub fn build(b: *std.Build) !void {
5254
b.getInstallStep().dependOn(&install_langref.step);
5355
}
5456

55-
const docs_step = b.step("docs", "Build documentation");
56-
docs_step.dependOn(&docgen_cmd.step);
57+
const autodoc_test = b.addTest(.{
58+
.root_source_file = .{ .path = "lib/std/std.zig" },
59+
.target = target,
60+
});
61+
autodoc_test.overrideZigLibDir("lib");
62+
autodoc_test.emit_bin = .no_emit; // https://github.com/ziglang/zig/issues/16351
63+
const install_std_docs = b.addInstallDirectory(.{
64+
.source_dir = autodoc_test.getOutputDocs(),
65+
.install_dir = .prefix,
66+
.install_subdir = "doc/std",
67+
});
68+
if (!skip_install_autodocs) {
69+
b.getInstallStep().dependOn(&install_std_docs.step);
70+
}
71+
72+
if (flat) {
73+
b.installFile("LICENSE", "LICENSE");
74+
}
75+
76+
const langref_step = b.step("langref", "Build and install the language reference");
77+
langref_step.dependOn(&install_langref.step);
78+
79+
const std_docs_step = b.step("std-docs", "Build and install the standard library documentation");
80+
std_docs_step.dependOn(&install_std_docs.step);
81+
82+
const docs_step = b.step("docs", "Build and install documentation");
83+
docs_step.dependOn(langref_step);
84+
docs_step.dependOn(std_docs_step);
5785

5886
const check_case_exe = b.addExecutable(.{
5987
.name = "check-case",
@@ -104,10 +132,10 @@ pub fn build(b: *std.Build) !void {
104132
const config_h_path_option = b.option([]const u8, "config_h", "Path to the generated config.h");
105133

106134
if (!skip_install_lib_files) {
107-
b.installDirectory(InstallDirectoryOptions{
135+
b.installDirectory(.{
108136
.source_dir = .{ .path = "lib" },
109-
.install_dir = .lib,
110-
.install_subdir = "zig",
137+
.install_dir = if (flat) .prefix else .lib,
138+
.install_subdir = if (flat) "lib" else "zig",
111139
.exclude_extensions = &[_][]const u8{
112140
// exclude files from lib/std/compress/testdata
113141
".gz",
@@ -167,6 +195,9 @@ pub fn build(b: *std.Build) !void {
167195
exe.pie = pie;
168196
exe.sanitize_thread = sanitize_thread;
169197
exe.entitlements = entitlements;
198+
// TODO -femit-bin/-fno-emit-bin should be inferred by the build system
199+
// based on whether or not the exe is run or installed.
200+
// https://github.com/ziglang/zig/issues/16351
170201
if (no_bin) exe.emit_bin = .no_emit;
171202

172203
exe.build_id = b.option(
@@ -175,7 +206,13 @@ pub fn build(b: *std.Build) !void {
175206
"Request creation of '.note.gnu.build-id' section",
176207
);
177208

178-
b.installArtifact(exe);
209+
if (!no_bin) {
210+
const install_exe = b.addInstallArtifact(exe);
211+
if (flat) {
212+
install_exe.dest_dir = .prefix;
213+
}
214+
b.getInstallStep().dependOn(&install_exe.step);
215+
}
179216

180217
test_step.dependOn(&exe.step);
181218

ci/aarch64-linux-debug.sh

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ cmake .. \
4040
-DZIG_TARGET_TRIPLE="$TARGET" \
4141
-DZIG_TARGET_MCPU="$MCPU" \
4242
-DZIG_STATIC=ON \
43+
-DZIG_NO_LIB=ON \
4344
-GNinja
4445

4546
# Now cmake will use zig as the C/C++ compiler. We reset the environment variables
@@ -49,16 +50,18 @@ unset CXX
4950

5051
ninja install
5152

53+
# TODO: move this to a build.zig step (check-fmt)
5254
echo "Looking for non-conforming code formatting..."
5355
stage3-debug/bin/zig fmt --check .. \
5456
--exclude ../test/cases/ \
5557
--exclude ../build-debug
5658

5759
# simultaneously test building self-hosted without LLVM and with 32-bit arm
58-
stage3-debug/bin/zig build -Dtarget=arm-linux-musleabihf
60+
stage3-debug/bin/zig build \
61+
-Dtarget=arm-linux-musleabihf \
62+
-Dno-lib
5963

6064
# TODO: add -fqemu back to this line
61-
6265
stage3-debug/bin/zig build test docs \
6366
--maxrss 24696061952 \
6467
-fwasmtime \
@@ -68,10 +71,8 @@ stage3-debug/bin/zig build test docs \
6871
--zig-lib-dir "$(pwd)/../lib"
6972

7073
# Look for HTML errors.
71-
tidy --drop-empty-elements no -qe "stage3-debug/doc/langref.html"
72-
73-
# Produce the experimental std lib documentation.
74-
stage3-debug/bin/zig test ../lib/std/std.zig -femit-docs -fno-emit-bin --zig-lib-dir ../lib
74+
# TODO: move this to a build.zig flag (-Denable-tidy)
75+
tidy --drop-empty-elements no -qe "zig-out/doc/langref.html"
7576

7677
# Ensure that updating the wasm binary from this commit will result in a viable build.
7778
stage3-debug/bin/zig build update-zig1
@@ -91,6 +92,7 @@ cmake .. \
9192
-DZIG_TARGET_TRIPLE="$TARGET" \
9293
-DZIG_TARGET_MCPU="$MCPU" \
9394
-DZIG_STATIC=ON \
95+
-DZIG_NO_LIB=ON \
9496
-GNinja
9597

9698
unset CC
@@ -102,6 +104,7 @@ stage3/bin/zig test ../test/behavior.zig -I../test
102104
stage3/bin/zig build -p stage4 \
103105
-Dstatic-llvm \
104106
-Dtarget=native-native-musl \
107+
-Dno-lib \
105108
--search-prefix "$PREFIX" \
106109
--zig-lib-dir "$(pwd)/../lib"
107110
stage4/bin/zig test ../test/behavior.zig -I../test

ci/aarch64-linux-release.sh

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ cmake .. \
4040
-DZIG_TARGET_TRIPLE="$TARGET" \
4141
-DZIG_TARGET_MCPU="$MCPU" \
4242
-DZIG_STATIC=ON \
43+
-DZIG_NO_LIB=ON \
4344
-GNinja
4445

4546
# Now cmake will use zig as the C/C++ compiler. We reset the environment variables
@@ -49,16 +50,18 @@ unset CXX
4950

5051
ninja install
5152

53+
# TODO: move this to a build.zig step (check-fmt)
5254
echo "Looking for non-conforming code formatting..."
5355
stage3-release/bin/zig fmt --check .. \
5456
--exclude ../test/cases/ \
5557
--exclude ../build-release
5658

5759
# simultaneously test building self-hosted without LLVM and with 32-bit arm
58-
stage3-release/bin/zig build -Dtarget=arm-linux-musleabihf
60+
stage3-release/bin/zig build \
61+
-Dtarget=arm-linux-musleabihf \
62+
-Dno-lib
5963

6064
# TODO: add -fqemu back to this line
61-
6265
stage3-release/bin/zig build test docs \
6366
--maxrss 24696061952 \
6467
-fwasmtime \
@@ -68,10 +71,8 @@ stage3-release/bin/zig build test docs \
6871
--zig-lib-dir "$(pwd)/../lib"
6972

7073
# Look for HTML errors.
71-
tidy --drop-empty-elements no -qe "stage3-release/doc/langref.html"
72-
73-
# Produce the experimental std lib documentation.
74-
stage3-release/bin/zig test ../lib/std/std.zig -femit-docs -fno-emit-bin --zig-lib-dir ../lib
74+
# TODO: move this to a build.zig flag (-Denable-tidy)
75+
tidy --drop-empty-elements no -qe "zig-out/doc/langref.html"
7576

7677
# Ensure that updating the wasm binary from this commit will result in a viable build.
7778
stage3-release/bin/zig build update-zig1
@@ -91,6 +92,7 @@ cmake .. \
9192
-DZIG_TARGET_TRIPLE="$TARGET" \
9293
-DZIG_TARGET_MCPU="$MCPU" \
9394
-DZIG_STATIC=ON \
95+
-DZIG_NO_LIB=ON \
9496
-GNinja
9597

9698
unset CC
@@ -102,6 +104,7 @@ stage3/bin/zig test ../test/behavior.zig -I../test
102104
stage3/bin/zig build -p stage4 \
103105
-Dstatic-llvm \
104106
-Dtarget=native-native-musl \
107+
-Dno-lib \
105108
--search-prefix "$PREFIX" \
106109
--zig-lib-dir "$(pwd)/../lib"
107110
stage4/bin/zig test ../test/behavior.zig -I../test

ci/aarch64-macos-debug.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PATH="$HOME/local/bin:$PATH" cmake .. \
3939
-DZIG_TARGET_TRIPLE="$TARGET" \
4040
-DZIG_TARGET_MCPU="$MCPU" \
4141
-DZIG_STATIC=ON \
42+
-DZIG_NO_LIB=ON \
4243
-GNinja
4344

4445
$HOME/local/bin/ninja install
@@ -49,6 +50,3 @@ stage3-debug/bin/zig build test docs \
4950
-Dstatic-llvm \
5051
-Dskip-non-native \
5152
--search-prefix "$PREFIX"
52-
53-
# Produce the experimental std lib documentation.
54-
stage3-debug/bin/zig test ../lib/std/std.zig -femit-docs -fno-emit-bin --zig-lib-dir ../lib

ci/aarch64-macos-release.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ PATH="$HOME/local/bin:$PATH" cmake .. \
3939
-DZIG_TARGET_TRIPLE="$TARGET" \
4040
-DZIG_TARGET_MCPU="$MCPU" \
4141
-DZIG_STATIC=ON \
42+
-DZIG_NO_LIB=ON \
4243
-GNinja
4344

4445
$HOME/local/bin/ninja install
@@ -50,9 +51,6 @@ stage3-release/bin/zig build test docs \
5051
-Dskip-non-native \
5152
--search-prefix "$PREFIX"
5253

53-
# Produce the experimental std lib documentation.
54-
stage3-release/bin/zig test ../lib/std/std.zig -femit-docs -fno-emit-bin --zig-lib-dir ../lib
55-
5654
# Ensure that stage3 and stage4 are byte-for-byte identical.
5755
stage3-release/bin/zig build \
5856
--prefix stage4-release \

ci/aarch64-windows.ps1

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ $Env:ZIG_LOCAL_CACHE_DIR="$(Get-Location)\zig-local-cache"
5555
-DZIG_AR_WORKAROUND=ON `
5656
-DZIG_TARGET_TRIPLE="$TARGET" `
5757
-DZIG_TARGET_MCPU="$MCPU" `
58-
-DZIG_STATIC=ON
58+
-DZIG_STATIC=ON `
59+
-DZIG_NO_LIB=ON
5960
CheckLastExitCode
6061

6162
ninja install
@@ -69,11 +70,3 @@ Write-Output "Main test suite..."
6970
-Dskip-non-native `
7071
-Denable-symlinks-windows
7172
CheckLastExitCode
72-
73-
Write-Output "Testing Autodocs..."
74-
& "stage3-release\bin\zig.exe" test "..\lib\std\std.zig" `
75-
--zig-lib-dir "$ZIG_LIB_DIR" `
76-
-femit-docs `
77-
-fno-emit-bin
78-
CheckLastExitCode
79-

ci/x86_64-linux-debug.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ cmake .. \
4040
-DZIG_TARGET_TRIPLE="$TARGET" \
4141
-DZIG_TARGET_MCPU="$MCPU" \
4242
-DZIG_STATIC=ON \
43+
-DZIG_NO_LIB=ON \
4344
-GNinja
4445

4546
# Now cmake will use zig as the C/C++ compiler. We reset the environment variables
@@ -49,13 +50,16 @@ unset CXX
4950

5051
ninja install
5152

53+
# TODO: move this to a build.zig step (check-fmt)
5254
echo "Looking for non-conforming code formatting..."
5355
stage3-debug/bin/zig fmt --check .. \
5456
--exclude ../test/cases/ \
5557
--exclude ../build-debug
5658

5759
# simultaneously test building self-hosted without LLVM and with 32-bit arm
58-
stage3-debug/bin/zig build -Dtarget=arm-linux-musleabihf
60+
stage3-debug/bin/zig build \
61+
-Dtarget=arm-linux-musleabihf \
62+
-Dno-lib
5963

6064
stage3-debug/bin/zig build test docs \
6165
--maxrss 21000000000 \
@@ -67,10 +71,8 @@ stage3-debug/bin/zig build test docs \
6771
--zig-lib-dir "$(pwd)/../lib"
6872

6973
# Look for HTML errors.
70-
tidy --drop-empty-elements no -qe "stage3-debug/doc/langref.html"
71-
72-
# Produce the experimental std lib documentation.
73-
stage3-debug/bin/zig test ../lib/std/std.zig -femit-docs -fno-emit-bin --zig-lib-dir ../lib
74+
# TODO: move this to a build.zig flag (-Denable-tidy)
75+
tidy --drop-empty-elements no -qe "zig-out/doc/langref.html"
7476

7577
# Ensure that updating the wasm binary from this commit will result in a viable build.
7678
stage3-debug/bin/zig build update-zig1
@@ -90,6 +92,7 @@ cmake .. \
9092
-DZIG_TARGET_TRIPLE="$TARGET" \
9193
-DZIG_TARGET_MCPU="$MCPU" \
9294
-DZIG_STATIC=ON \
95+
-DZIG_NO_LIB=ON \
9396
-GNinja
9497

9598
unset CC
@@ -101,6 +104,7 @@ stage3/bin/zig test ../test/behavior.zig -I../test
101104
stage3/bin/zig build -p stage4 \
102105
-Dstatic-llvm \
103106
-Dtarget=native-native-musl \
107+
-Dno-lib \
104108
--search-prefix "$PREFIX" \
105109
--zig-lib-dir "$(pwd)/../lib"
106110
stage4/bin/zig test ../test/behavior.zig -I../test

ci/x86_64-linux-release.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ cmake .. \
4040
-DZIG_TARGET_TRIPLE="$TARGET" \
4141
-DZIG_TARGET_MCPU="$MCPU" \
4242
-DZIG_STATIC=ON \
43+
-DZIG_NO_LIB=ON \
4344
-GNinja
4445

4546
# Now cmake will use zig as the C/C++ compiler. We reset the environment variables
@@ -49,14 +50,17 @@ unset CXX
4950

5051
ninja install
5152

53+
# TODO: move this to a build.zig step (check-fmt)
5254
echo "Looking for non-conforming code formatting..."
5355
stage3-release/bin/zig fmt --check .. \
5456
--exclude ../test/cases/ \
5557
--exclude ../build-debug \
5658
--exclude ../build-release
5759

5860
# simultaneously test building self-hosted without LLVM and with 32-bit arm
59-
stage3-release/bin/zig build -Dtarget=arm-linux-musleabihf
61+
stage3-release/bin/zig build \
62+
-Dtarget=arm-linux-musleabihf \
63+
-Dno-lib
6064

6165
stage3-release/bin/zig build test docs \
6266
--maxrss 21000000000 \
@@ -68,10 +72,8 @@ stage3-release/bin/zig build test docs \
6872
--zig-lib-dir "$(pwd)/../lib"
6973

7074
# Look for HTML errors.
71-
tidy --drop-empty-elements no -qe "stage3-release/doc/langref.html"
72-
73-
# Produce the experimental std lib documentation.
74-
stage3-release/bin/zig test ../lib/std/std.zig -femit-docs -fno-emit-bin --zig-lib-dir ../lib
75+
# TODO: move this to a build.zig flag (-Denable-tidy)
76+
tidy --drop-empty-elements no -qe "zig-out/doc/langref.html"
7577

7678
# Ensure that stage3 and stage4 are byte-for-byte identical.
7779
stage3-release/bin/zig build \
@@ -107,6 +109,7 @@ cmake .. \
107109
-DZIG_TARGET_TRIPLE="$TARGET" \
108110
-DZIG_TARGET_MCPU="$MCPU" \
109111
-DZIG_STATIC=ON \
112+
-DZIG_NO_LIB=ON \
110113
-GNinja
111114

112115
unset CC
@@ -118,6 +121,7 @@ stage3/bin/zig test ../test/behavior.zig -I../test
118121
stage3/bin/zig build -p stage4 \
119122
-Dstatic-llvm \
120123
-Dtarget=native-native-musl \
124+
-Dno-lib \
121125
--search-prefix "$PREFIX" \
122126
--zig-lib-dir "$(pwd)/../lib"
123127
stage4/bin/zig test ../test/behavior.zig -I../test

ci/x86_64-macos-release.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ cmake .. \
4343
-DCMAKE_CXX_COMPILER="$ZIG;c++;-target;$TARGET;-mcpu=$MCPU" \
4444
-DZIG_TARGET_TRIPLE="$TARGET" \
4545
-DZIG_TARGET_MCPU="$MCPU" \
46-
-DZIG_STATIC=ON
46+
-DZIG_STATIC=ON \
47+
-DZIG_NO_LIB=ON
4748

4849
make $JOBS install
4950

@@ -54,9 +55,6 @@ stage3/bin/zig build test docs \
5455
-Dskip-non-native \
5556
--search-prefix "$PREFIX"
5657

57-
# Produce the experimental std lib documentation.
58-
stage3/bin/zig test ../lib/std/std.zig -femit-docs -fno-emit-bin --zig-lib-dir ../lib
59-
6058
# Ensure that stage3 and stage4 are byte-for-byte identical.
6159
stage3/bin/zig build \
6260
--prefix stage4 \

0 commit comments

Comments
 (0)