authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-16 18:57:34-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-02-16 18:57:34-05:00
log7eb0a3edcef2ab752ff1e45e6f1316b633b29606
tree152cce9cc48d92b8a8b319fb71c0aa83e44e8eec
parent39ee46a6c1b5a56129950953d304a1169b7ffa67
signaturelock-open Commit is signed but in an unrecognized format.

remove libc dependency of zig0 building libstage2

Rather than `zig0 build ...` the build now does `zig0 build-lib ...`, avoiding the requirement of linking the build script, and thus avoiding the requirement of finding native libc, for systems where libc is the system ABI.

6 files changed, 90 insertions(+), 80 deletions(-)

CMakeLists.txt+41-22
......@@ -604,28 +604,29 @@ else()
604604 set(LIBUSERLAND "${CMAKE_BINARY_DIR}/libuserland.a")
605605endif()
606606if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
607 set(LIBUSERLAND_RELEASE_MODE "false")
607 set(LIBUSERLAND_RELEASE_ARG "")
608608else()
609 set(LIBUSERLAND_RELEASE_MODE "true")
609 set(LIBUSERLAND_RELEASE_ARG "--release-fast --strip")
610endif()
611if(WIN32)
612 set(LIBUSERLAND_WINDOWS_ARGS "-lntdll")
613else()
614 set(LIBUSERLAND_WINDOWS_ARGS "")
610615endif()
611616
612set(BUILD_LIBUSERLAND_ARGS "build"
617set(BUILD_LIBUSERLAND_ARGS "build-lib"
613618 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
614 "-Doutput-dir=${CMAKE_BINARY_DIR}"
615 "-Drelease=${LIBUSERLAND_RELEASE_MODE}"
616 "-Dlib-files-only"
617 --prefix "${CMAKE_INSTALL_PREFIX}"
618 libuserland
619 --cache on
620 --output-dir "${CMAKE_BINARY_DIR}"
621 ${LIBUSERLAND_RELEASE_ARG}
622 "src-self-hosted/stage1.zig"
623 --disable-gen-h
624 --bundle-compiler-rt
625 -fPIC
626 -lc
627 ${LIBUSERLAND_WINDOWS_ARGS}
619628)
620629
621# When using Visual Studio build system generator we default to libuserland install.
622if(MSVC)
623 set(ZIG_SKIP_INSTALL_LIB_FILES off CACHE BOOL "Disable copying lib/ files to install prefix")
624 if(NOT ZIG_SKIP_INSTALL_LIB_FILES)
625 set(BUILD_LIBUSERLAND_ARGS ${BUILD_LIBUSERLAND_ARGS} install)
626 endif()
627endif()
628
629630add_custom_target(zig_build_libuserland ALL
630631 COMMAND zig0 ${BUILD_LIBUSERLAND_ARGS}
631632 DEPENDS zig0
......@@ -648,12 +649,30 @@ add_dependencies(zig zig_build_libuserland)
648649
649650install(TARGETS zig DESTINATION bin)
650651
651# CODE has no effect with Visual Studio build system generator.
652if(NOT MSVC)
653 get_target_property(zig0_BINARY_DIR zig0 BINARY_DIR)
654 install(CODE "set(zig0_EXE \"${zig0_BINARY_DIR}/zig0\")")
655 install(CODE "set(INSTALL_LIBUSERLAND_ARGS \"${BUILD_LIBUSERLAND_ARGS}\" install)")
656 install(CODE "set(BUILD_LIBUSERLAND_ARGS \"${BUILD_LIBUSERLAND_ARGS}\")")
652set(ZIG_INSTALL_ARGS "build"
653 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
654 "-Dlib-files-only"
655 --prefix "${CMAKE_INSTALL_PREFIX}"
656 install
657)
658
659# CODE has no effect with Visual Studio build system generator, therefore
660# when using Visual Studio build system generator we resort to running
661# `zig build install` during the build phase.
662if(MSVC)
663 set(ZIG_SKIP_INSTALL_LIB_FILES off CACHE BOOL
664 "Windows-only: Disable copying lib/ files to install prefix during the build phase")
665 if(NOT ZIG_SKIP_INSTALL_LIB_FILES)
666 add_custom_target(zig_install_lib_files ALL
667 COMMAND zig ${ZIG_INSTALL_ARGS}
668 DEPENDS zig
669 WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
670 )
671 endif()
672else()
673 get_target_property(zig_BINARY_DIR zig BINARY_DIR)
674 install(CODE "set(zig_EXE \"${zig_BINARY_DIR}/zig\")")
675 install(CODE "set(ZIG_INSTALL_ARGS \"${ZIG_INSTALL_ARGS}\")")
657676 install(CODE "set(CMAKE_SOURCE_DIR \"${CMAKE_SOURCE_DIR}\")")
658677 install(SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/cmake/install.cmake)
659678endif()
build.zig-27
......@@ -64,8 +64,6 @@ pub fn build(b: *Builder) !void {
6464 try configureStage2(b, test_stage2, ctx);
6565 try configureStage2(b, exe, ctx);
6666
67 addLibUserlandStep(b, mode);
68
6967 const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;
7068 const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release;
7169 const skip_release_fast = b.option(bool, "skip-release-fast", "Main test suite skips release-fast builds") orelse skip_release;
......@@ -366,28 +364,3 @@ const Context = struct {
366364 dia_guids_lib: []const u8,
367365 llvm: LibraryDep,
368366};
369
370fn addLibUserlandStep(b: *Builder, mode: builtin.Mode) void {
371 const artifact = b.addStaticLibrary("userland", "src-self-hosted/stage1.zig");
372 artifact.disable_gen_h = true;
373 artifact.bundle_compiler_rt = true;
374 artifact.setTarget(builtin.arch, builtin.os, builtin.abi);
375 artifact.setBuildMode(mode);
376 artifact.force_pic = true;
377 if (mode != .Debug) {
378 artifact.strip = true;
379 }
380 artifact.linkSystemLibrary("c");
381 if (builtin.os == .windows) {
382 artifact.linkSystemLibrary("ntdll");
383 }
384 const libuserland_step = b.step("libuserland", "Build the userland compiler library for use in stage1");
385 libuserland_step.dependOn(&artifact.step);
386
387 const output_dir = b.option(
388 []const u8,
389 "output-dir",
390 "For libuserland step, where to put the output",
391 ) orelse return;
392 artifact.setOutputDir(output_dir);
393}
cmake/install.cmake+6-6
......@@ -1,16 +1,16 @@
11message("-- Installing: ${CMAKE_INSTALL_PREFIX}/lib")
22
3if(NOT EXISTS ${zig0_EXE})
3if(NOT EXISTS ${zig_EXE})
44 message("::")
55 message(":: ERROR: Executable not found")
66 message(":: (execute_process)")
77 message("::")
8 message(":: executable: ${zig0_EXE}")
8 message(":: executable: ${zig_EXE}")
99 message("::")
1010 message(FATAL_ERROR)
1111endif()
1212
13execute_process(COMMAND ${zig0_EXE} ${INSTALL_LIBUSERLAND_ARGS}
13execute_process(COMMAND ${zig_EXE} ${ZIG_INSTALL_ARGS}
1414 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
1515 RESULT_VARIABLE _result
1616)
......@@ -19,11 +19,11 @@ if(_result)
1919 message(":: ERROR: ${_result}")
2020 message(":: (execute_process)")
2121
22 string(REPLACE ";" " " s_INSTALL_LIBUSERLAND_ARGS "${INSTALL_LIBUSERLAND_ARGS}")
22 string(REPLACE ";" " " s_INSTALL_LIBUSERLAND_ARGS "${ZIG_INSTALL_ARGS}")
2323 message("::")
24 message(":: argv: ${zig0_EXE} ${s_INSTALL_LIBUSERLAND_ARGS} install")
24 message(":: argv: ${zig_EXE} ${s_INSTALL_LIBUSERLAND_ARGS}")
2525
26 set(_args ${zig0_EXE} ${INSTALL_LIBUSERLAND_ARGS})
26 set(_args ${zig_EXE} ${ZIG_INSTALL_ARGS})
2727 list(LENGTH _args _len)
2828 math(EXPR _len "${_len} - 1")
2929 message("::")
src-self-hosted/libc_installation.zig+11-8
......@@ -204,14 +204,17 @@ pub const LibCInstallation = struct {
204204 }
205205 }
206206 } else {
207 var batch = Batch(FindError!void, 2, .auto_async).init();
208 batch.add(&async self.findNativeIncludeDirPosix(allocator));
209 if (is_freebsd or is_netbsd) {
210 self.crt_dir = try std.mem.dupeZ(allocator, u8, "/usr/lib");
211 } else if (is_linux or is_dragonfly) {
212 batch.add(&async self.findNativeCrtDirPosix(allocator));
213 }
214 try batch.wait();
207 try blk: {
208 var batch = Batch(FindError!void, 2, .auto_async).init();
209 errdefer batch.wait() catch {};
210 batch.add(&async self.findNativeIncludeDirPosix(allocator));
211 if (is_freebsd or is_netbsd) {
212 self.crt_dir = try std.mem.dupeZ(allocator, u8, "/usr/lib");
213 } else if (is_linux or is_dragonfly) {
214 batch.add(&async self.findNativeCrtDirPosix(allocator));
215 }
216 break :blk batch.wait();
217 };
215218 }
216219 return self;
217220 }
src/main.cpp+30-4
......@@ -1003,9 +1003,22 @@ static int main0(int argc, char **argv) {
10031003 return main_exit(root_progress_node, EXIT_FAILURE);
10041004 }
10051005
1006 // If both output_dir and enable_cache are provided, and doing build-lib, we
1007 // will just do a file copy at the end. This helps when bootstrapping zig from zig0
1008 // because we want to pass something like this:
1009 // zig0 build-lib --cache on --output-dir ${CMAKE_BINARY_DIR}
1010 // And we don't have access to `zig0 build` because that would require detecting native libc
1011 // on systems where we are not able to build a libc from source for them.
1012 // But that's the only reason this works, so otherwise we give an error here.
1013 Buf *final_output_dir_step = nullptr;
10061014 if (output_dir != nullptr && enable_cache == CacheOptOn) {
1007 fprintf(stderr, "`--output-dir` is incompatible with --cache on.\n");
1008 return print_error_usage(arg0);
1015 if (cmd == CmdBuild && out_type == OutTypeLib) {
1016 final_output_dir_step = output_dir;
1017 output_dir = nullptr;
1018 } else {
1019 fprintf(stderr, "`--output-dir` is incompatible with --cache on.\n");
1020 return print_error_usage(arg0);
1021 }
10091022 }
10101023
10111024 if (target_requires_pic(&target, have_libc) && want_pic == WantPICDisabled) {
......@@ -1290,8 +1303,21 @@ static int main0(int argc, char **argv) {
12901303#if defined(ZIG_OS_WINDOWS)
12911304 buf_replace(&g->output_file_path, '/', '\\');
12921305#endif
1293 if (printf("%s\n", buf_ptr(&g->output_file_path)) < 0)
1294 return main_exit(root_progress_node, EXIT_FAILURE);
1306 if (final_output_dir_step != nullptr) {
1307 Buf *dest_basename = buf_alloc();
1308 os_path_split(&g->output_file_path, nullptr, dest_basename);
1309 Buf *dest_path = buf_alloc();
1310 os_path_join(final_output_dir_step, dest_basename, dest_path);
1311
1312 if ((err = os_copy_file(&g->output_file_path, dest_path))) {
1313 fprintf(stderr, "unable to copy %s to %s: %s\n", buf_ptr(&g->output_file_path),
1314 buf_ptr(dest_path), err_str(err));
1315 return main_exit(root_progress_node, EXIT_FAILURE);
1316 }
1317 } else {
1318 if (printf("%s\n", buf_ptr(&g->output_file_path)) < 0)
1319 return main_exit(root_progress_node, EXIT_FAILURE);
1320 }
12951321 }
12961322 return main_exit(root_progress_node, EXIT_SUCCESS);
12971323 } else {
src/userland.cpp+2-13
......@@ -146,19 +146,8 @@ int stage2_cmd_targets(const char *zig_triple) {
146146}
147147
148148enum Error stage2_libc_parse(struct Stage2LibCInstallation *libc, const char *libc_file) {
149 libc->include_dir = "/dummy/include";
150 libc->include_dir_len = strlen(libc->include_dir);
151 libc->sys_include_dir = "/dummy/sys/include";
152 libc->sys_include_dir_len = strlen(libc->sys_include_dir);
153 libc->crt_dir = "";
154 libc->crt_dir_len = strlen(libc->crt_dir);
155 libc->static_crt_dir = "";
156 libc->static_crt_dir_len = strlen(libc->static_crt_dir);
157 libc->msvc_lib_dir = "";
158 libc->msvc_lib_dir_len = strlen(libc->msvc_lib_dir);
159 libc->kernel32_lib_dir = "";
160 libc->kernel32_lib_dir_len = strlen(libc->kernel32_lib_dir);
161 return ErrorNone;
149 const char *msg = "stage0 called stage2_libc_parse";
150 stage2_panic(msg, strlen(msg));
162151}
163152
164153enum Error stage2_libc_render(struct Stage2LibCInstallation *self, FILE *file) {