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()...@@ -604,28 +604,29 @@ else()
604 set(LIBUSERLAND "${CMAKE_BINARY_DIR}/libuserland.a")604 set(LIBUSERLAND "${CMAKE_BINARY_DIR}/libuserland.a")
605endif()605endif()
606if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")606if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
607 set(LIBUSERLAND_RELEASE_MODE "false")607 set(LIBUSERLAND_RELEASE_ARG "")
608else()608else()
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 "")
610endif()615endif()
611616
612set(BUILD_LIBUSERLAND_ARGS "build"617set(BUILD_LIBUSERLAND_ARGS "build-lib"
613 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"618 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
614 "-Doutput-dir=${CMAKE_BINARY_DIR}"619 --cache on
615 "-Drelease=${LIBUSERLAND_RELEASE_MODE}"620 --output-dir "${CMAKE_BINARY_DIR}"
616 "-Dlib-files-only"621 ${LIBUSERLAND_RELEASE_ARG}
617 --prefix "${CMAKE_INSTALL_PREFIX}"622 "src-self-hosted/stage1.zig"
618 libuserland623 --disable-gen-h
624 --bundle-compiler-rt
625 -fPIC
626 -lc
627 ${LIBUSERLAND_WINDOWS_ARGS}
619)628)
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
629add_custom_target(zig_build_libuserland ALL630add_custom_target(zig_build_libuserland ALL
630 COMMAND zig0 ${BUILD_LIBUSERLAND_ARGS}631 COMMAND zig0 ${BUILD_LIBUSERLAND_ARGS}
631 DEPENDS zig0632 DEPENDS zig0
...@@ -648,12 +649,30 @@ add_dependencies(zig zig_build_libuserland)...@@ -648,12 +649,30 @@ add_dependencies(zig zig_build_libuserland)
648649
649install(TARGETS zig DESTINATION bin)650install(TARGETS zig DESTINATION bin)
650651
651# CODE has no effect with Visual Studio build system generator.652set(ZIG_INSTALL_ARGS "build"
652if(NOT MSVC)653 --override-lib-dir "${CMAKE_SOURCE_DIR}/lib"
653 get_target_property(zig0_BINARY_DIR zig0 BINARY_DIR)654 "-Dlib-files-only"
654 install(CODE "set(zig0_EXE \"${zig0_BINARY_DIR}/zig0\")")655 --prefix "${CMAKE_INSTALL_PREFIX}"
655 install(CODE "set(INSTALL_LIBUSERLAND_ARGS \"${BUILD_LIBUSERLAND_ARGS}\" install)")656 install
656 install(CODE "set(BUILD_LIBUSERLAND_ARGS \"${BUILD_LIBUSERLAND_ARGS}\")")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}\")")
657 install(CODE "set(CMAKE_SOURCE_DIR \"${CMAKE_SOURCE_DIR}\")")676 install(CODE "set(CMAKE_SOURCE_DIR \"${CMAKE_SOURCE_DIR}\")")
658 install(SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/cmake/install.cmake)677 install(SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/cmake/install.cmake)
659endif()678endif()
build.zig-27
...@@ -64,8 +64,6 @@ pub fn build(b: *Builder) !void {...@@ -64,8 +64,6 @@ pub fn build(b: *Builder) !void {
64 try configureStage2(b, test_stage2, ctx);64 try configureStage2(b, test_stage2, ctx);
65 try configureStage2(b, exe, ctx);65 try configureStage2(b, exe, ctx);
6666
67 addLibUserlandStep(b, mode);
68
69 const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;67 const skip_release = b.option(bool, "skip-release", "Main test suite skips release builds") orelse false;
70 const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release;68 const skip_release_small = b.option(bool, "skip-release-small", "Main test suite skips release-small builds") orelse skip_release;
71 const skip_release_fast = b.option(bool, "skip-release-fast", "Main test suite skips release-fast builds") orelse skip_release;69 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 {...@@ -366,28 +364,3 @@ const Context = struct {
366 dia_guids_lib: []const u8,364 dia_guids_lib: []const u8,
367 llvm: LibraryDep,365 llvm: LibraryDep,
368};366};
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 @@...@@ -1,16 +1,16 @@
1message("-- Installing: ${CMAKE_INSTALL_PREFIX}/lib")1message("-- Installing: ${CMAKE_INSTALL_PREFIX}/lib")
22
3if(NOT EXISTS ${zig0_EXE})3if(NOT EXISTS ${zig_EXE})
4 message("::")4 message("::")
5 message(":: ERROR: Executable not found")5 message(":: ERROR: Executable not found")
6 message(":: (execute_process)")6 message(":: (execute_process)")
7 message("::")7 message("::")
8 message(":: executable: ${zig0_EXE}")8 message(":: executable: ${zig_EXE}")
9 message("::")9 message("::")
10 message(FATAL_ERROR)10 message(FATAL_ERROR)
11endif()11endif()
1212
13execute_process(COMMAND ${zig0_EXE} ${INSTALL_LIBUSERLAND_ARGS}13execute_process(COMMAND ${zig_EXE} ${ZIG_INSTALL_ARGS}
14 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}14 WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
15 RESULT_VARIABLE _result15 RESULT_VARIABLE _result
16)16)
...@@ -19,11 +19,11 @@ if(_result)...@@ -19,11 +19,11 @@ if(_result)
19 message(":: ERROR: ${_result}")19 message(":: ERROR: ${_result}")
20 message(":: (execute_process)")20 message(":: (execute_process)")
2121
22 string(REPLACE ";" " " s_INSTALL_LIBUSERLAND_ARGS "${INSTALL_LIBUSERLAND_ARGS}")22 string(REPLACE ";" " " s_INSTALL_LIBUSERLAND_ARGS "${ZIG_INSTALL_ARGS}")
23 message("::")23 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})
27 list(LENGTH _args _len)27 list(LENGTH _args _len)
28 math(EXPR _len "${_len} - 1")28 math(EXPR _len "${_len} - 1")
29 message("::")29 message("::")
src-self-hosted/libc_installation.zig+11-8
...@@ -204,14 +204,17 @@ pub const LibCInstallation = struct {...@@ -204,14 +204,17 @@ pub const LibCInstallation = struct {
204 }204 }
205 }205 }
206 } else {206 } else {
207 var batch = Batch(FindError!void, 2, .auto_async).init();207 try blk: {
208 batch.add(&async self.findNativeIncludeDirPosix(allocator));208 var batch = Batch(FindError!void, 2, .auto_async).init();
209 if (is_freebsd or is_netbsd) {209 errdefer batch.wait() catch {};
210 self.crt_dir = try std.mem.dupeZ(allocator, u8, "/usr/lib");210 batch.add(&async self.findNativeIncludeDirPosix(allocator));
211 } else if (is_linux or is_dragonfly) {211 if (is_freebsd or is_netbsd) {
212 batch.add(&async self.findNativeCrtDirPosix(allocator));212 self.crt_dir = try std.mem.dupeZ(allocator, u8, "/usr/lib");
213 }213 } else if (is_linux or is_dragonfly) {
214 try batch.wait();214 batch.add(&async self.findNativeCrtDirPosix(allocator));
215 }
216 break :blk batch.wait();
217 };
215 }218 }
216 return self;219 return self;
217 }220 }
src/main.cpp+30-4
...@@ -1003,9 +1003,22 @@ static int main0(int argc, char **argv) {...@@ -1003,9 +1003,22 @@ static int main0(int argc, char **argv) {
1003 return main_exit(root_progress_node, EXIT_FAILURE);1003 return main_exit(root_progress_node, EXIT_FAILURE);
1004 }1004 }
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;
1006 if (output_dir != nullptr && enable_cache == CacheOptOn) {1014 if (output_dir != nullptr && enable_cache == CacheOptOn) {
1007 fprintf(stderr, "`--output-dir` is incompatible with --cache on.\n");1015 if (cmd == CmdBuild && out_type == OutTypeLib) {
1008 return print_error_usage(arg0);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 }
1009 }1022 }
10101023
1011 if (target_requires_pic(&target, have_libc) && want_pic == WantPICDisabled) {1024 if (target_requires_pic(&target, have_libc) && want_pic == WantPICDisabled) {
...@@ -1290,8 +1303,21 @@ static int main0(int argc, char **argv) {...@@ -1290,8 +1303,21 @@ static int main0(int argc, char **argv) {
1290#if defined(ZIG_OS_WINDOWS)1303#if defined(ZIG_OS_WINDOWS)
1291 buf_replace(&g->output_file_path, '/', '\\');1304 buf_replace(&g->output_file_path, '/', '\\');
1292#endif1305#endif
1293 if (printf("%s\n", buf_ptr(&g->output_file_path)) < 0)1306 if (final_output_dir_step != nullptr) {
1294 return main_exit(root_progress_node, EXIT_FAILURE);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 }
1295 }1321 }
1296 return main_exit(root_progress_node, EXIT_SUCCESS);1322 return main_exit(root_progress_node, EXIT_SUCCESS);
1297 } else {1323 } else {
src/userland.cpp+2-13
...@@ -146,19 +146,8 @@ int stage2_cmd_targets(const char *zig_triple) {...@@ -146,19 +146,8 @@ int stage2_cmd_targets(const char *zig_triple) {
146}146}
147147
148enum Error stage2_libc_parse(struct Stage2LibCInstallation *libc, const char *libc_file) {148enum Error stage2_libc_parse(struct Stage2LibCInstallation *libc, const char *libc_file) {
149 libc->include_dir = "/dummy/include";149 const char *msg = "stage0 called stage2_libc_parse";
150 libc->include_dir_len = strlen(libc->include_dir);150 stage2_panic(msg, strlen(msg));
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;
162}151}
163152
164enum Error stage2_libc_render(struct Stage2LibCInstallation *self, FILE *file) {153enum Error stage2_libc_render(struct Stage2LibCInstallation *self, FILE *file) {