From cf52f3f99a371fd4cb897afb2ed515ea00927808 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 3 Apr 2020 13:44:56 -0400 Subject: zig cc: add -allow-shlib-undefined alias --- src/main.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index cd45d40e5f..4d2775d75a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -833,9 +833,13 @@ static int main0(int argc, char **argv) { linker_gc_sections = OptionalBoolTrue; } else if (buf_eql_str(arg, "--no-gc-sections")) { linker_gc_sections = OptionalBoolFalse; - } else if (buf_eql_str(arg, "--allow-shlib-undefined")) { + } else if (buf_eql_str(arg, "--allow-shlib-undefined") || + buf_eql_str(arg, "-allow-shlib-undefined")) + { linker_allow_shlib_undefined = OptionalBoolTrue; - } else if (buf_eql_str(arg, "--no-allow-shlib-undefined")) { + } else if (buf_eql_str(arg, "--no-allow-shlib-undefined") || + buf_eql_str(arg, "-no-allow-shlib-undefined")) + { linker_allow_shlib_undefined = OptionalBoolFalse; } else if (buf_eql_str(arg, "-z")) { i += 1; -- cgit v1.2.3 From d73808f3ff7d36b32d53cc38cf9a5b29b1ea57d4 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 4 Apr 2020 11:57:28 -0400 Subject: remove `zig BUILD_INFO` hack Rather than stuffing configuration information into the Zig binary, the build script reads it from config.h. This solves a problem for package maintainers and improves the use case of deterministic builds. closes #3758 --- build.zig | 112 ++++++++++++++++++++++++++++++++++++++++------------------- src/main.cpp | 12 ------- 2 files changed, 77 insertions(+), 47 deletions(-) (limited to 'src/main.cpp') diff --git a/build.zig b/build.zig index d31d8bbe3c..16f279ac90 100644 --- a/build.zig +++ b/build.zig @@ -34,22 +34,7 @@ pub fn build(b: *Builder) !void { const test_step = b.step("test", "Run all the tests"); - // find the stage0 build artifacts because we're going to re-use config.h and zig_cpp library - const build_info = try b.exec(&[_][]const u8{ - b.zig_exe, - "BUILD_INFO", - }); - var index: usize = 0; - var ctx = Context{ - .cmake_binary_dir = nextValue(&index, build_info), - .cxx_compiler = nextValue(&index, build_info), - .llvm_config_exe = nextValue(&index, build_info), - .lld_include_dir = nextValue(&index, build_info), - .lld_libraries = nextValue(&index, build_info), - .clang_libraries = nextValue(&index, build_info), - .dia_guids_lib = nextValue(&index, build_info), - .llvm = undefined, - }; + var ctx = try findAndParseConfigH(b); ctx.llvm = try findLLVM(b, ctx.llvm_config_exe); var test_stage2 = b.addTest("src-self-hosted/test.zig"); @@ -262,25 +247,6 @@ fn findLLVM(b: *Builder, llvm_config_exe: []const u8) !LibraryDep { return result; } -fn nextValue(index: *usize, build_info: []const u8) []const u8 { - const start = index.*; - while (true) : (index.* += 1) { - switch (build_info[index.*]) { - '\n' => { - const result = build_info[start..index.*]; - index.* += 1; - return result; - }, - '\r' => { - const result = build_info[start..index.*]; - index.* += 2; - return result; - }, - else => continue, - } - } -} - fn configureStage2(b: *Builder, exe: var, ctx: Context) !void { exe.addIncludeDir("src"); exe.addIncludeDir(ctx.cmake_binary_dir); @@ -376,3 +342,79 @@ const Context = struct { dia_guids_lib: []const u8, llvm: LibraryDep, }; + +fn findAndParseConfigH(b: *Builder) !Context { + var check_dir = fs.path.dirname(b.zig_exe).?; + const config_h_text = while (true) { + var dir = try fs.cwd().openDir(check_dir, .{}); + defer dir.close(); + + const max_bytes = 1 * 1024 * 1024; + const config_h_text = dir.readFileAlloc(b.allocator, "config.h", max_bytes) catch |err| switch (err) { + error.FileNotFound => { + check_dir = fs.path.dirname(check_dir) orelse { + std.debug.warn("Unable to find config.h file relative to Zig executable.\n", .{}); + std.debug.warn("`zig build` must be run using a Zig executable within the source tree.\n", .{}); + std.process.exit(1); + }; + continue; + }, + else => |e| return e, + }; + break config_h_text; + } else unreachable; // TODO should not need `else unreachable`. + + var ctx: Context = .{ + .cmake_binary_dir = undefined, + .cxx_compiler = undefined, + .llvm_config_exe = undefined, + .lld_include_dir = undefined, + .lld_libraries = undefined, + .clang_libraries = undefined, + .dia_guids_lib = undefined, + .llvm = undefined, + }; + + const mappings = [_]struct { prefix: []const u8, field: []const u8 }{ + .{ + .prefix = "#define ZIG_CMAKE_BINARY_DIR ", + .field = "cmake_binary_dir", + }, + .{ + .prefix = "#define ZIG_CXX_COMPILER ", + .field = "cxx_compiler", + }, + .{ + .prefix = "#define ZIG_LLD_INCLUDE_PATH ", + .field = "lld_include_dir", + }, + .{ + .prefix = "#define ZIG_LLD_LIBRARIES ", + .field = "lld_libraries", + }, + .{ + .prefix = "#define ZIG_CLANG_LIBRARIES ", + .field = "clang_libraries", + }, + .{ + .prefix = "#define ZIG_LLVM_CONFIG_EXE ", + .field = "llvm_config_exe", + }, + .{ + .prefix = "#define ZIG_DIA_GUIDS_LIB ", + .field = "dia_guids_lib", + }, + }; + + var lines_it = mem.tokenize(config_h_text, "\r\n"); + while (lines_it.next()) |line| { + inline for (mappings) |mapping| { + if (mem.startsWith(u8, line, mapping.prefix)) { + var it = mem.separate(line, "\""); + _ = it.next().?; // skip the stuff before the quote + @field(ctx, mapping.field) = it.next().?; // the stuff inside the quote + } + } + } + return ctx; +} diff --git a/src/main.cpp b/src/main.cpp index 4d2775d75a..17a1c2bc53 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -260,18 +260,6 @@ static int main0(int argc, char **argv) { char *arg0 = argv[0]; Error err; - if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) { - printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n", - ZIG_CMAKE_BINARY_DIR, - ZIG_CXX_COMPILER, - ZIG_LLVM_CONFIG_EXE, - ZIG_LLD_INCLUDE_PATH, - ZIG_LLD_LIBRARIES, - ZIG_CLANG_LIBRARIES, - ZIG_DIA_GUIDS_LIB); - return 0; - } - if (argc >= 2 && (strcmp(argv[1], "clang") == 0 || strcmp(argv[1], "-cc1") == 0 || strcmp(argv[1], "-cc1as") == 0)) { -- cgit v1.2.3 From 52db13738b3fca0ad5d83476e584be1d61a1428f Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 4 Apr 2020 14:58:24 -0400 Subject: zig cc looks for native include directories unless -nostdinc closes #4938 --- src-self-hosted/clang_options_data.zig | 38 +++++++++++++++++++++++++++++----- src-self-hosted/stage2.zig | 1 + src/main.cpp | 7 ++++++- src/stage2.h | 1 + tools/update_clang_options.zig | 20 ++++++++++++++++++ 5 files changed, 61 insertions(+), 6 deletions(-) (limited to 'src/main.cpp') diff --git a/src-self-hosted/clang_options_data.zig b/src-self-hosted/clang_options_data.zig index 2afe7f5681..889737bdac 100644 --- a/src-self-hosted/clang_options_data.zig +++ b/src-self-hosted/clang_options_data.zig @@ -1725,7 +1725,7 @@ flagpsl("MT"), .{ .name = "no-standard-includes", .syntax = .flag, - .zig_equivalent = .other, + .zig_equivalent = .nostdlibinc, .pd1 = false, .pd2 = true, .psl = false, @@ -3781,7 +3781,14 @@ flagpd1("nocudainc"), flagpd1("nodefaultlibs"), flagpd1("nofixprebinding"), flagpd1("nogpulib"), -flagpd1("nolibc"), +.{ + .name = "nolibc", + .syntax = .flag, + .zig_equivalent = .nostdlib, + .pd1 = true, + .pd2 = false, + .psl = false, +}, flagpd1("nomultidefs"), flagpd1("fnon-call-exceptions"), flagpd1("fno-non-call-exceptions"), @@ -3790,8 +3797,22 @@ flagpd1("noprebind"), flagpd1("noprofilelib"), flagpd1("noseglinkedit"), flagpd1("nostartfiles"), -flagpd1("nostdinc"), -flagpd1("nostdinc++"), +.{ + .name = "nostdinc", + .syntax = .flag, + .zig_equivalent = .nostdlibinc, + .pd1 = true, + .pd2 = false, + .psl = false, +}, +.{ + .name = "nostdinc++", + .syntax = .flag, + .zig_equivalent = .nostdlib_cpp, + .pd1 = true, + .pd2 = false, + .psl = false, +}, .{ .name = "nostdlib", .syntax = .flag, @@ -3800,7 +3821,14 @@ flagpd1("nostdinc++"), .pd2 = false, .psl = false, }, -flagpd1("nostdlibinc"), +.{ + .name = "nostdlibinc", + .syntax = .flag, + .zig_equivalent = .nostdlibinc, + .pd1 = true, + .pd2 = false, + .psl = false, +}, .{ .name = "nostdlib++", .syntax = .flag, diff --git a/src-self-hosted/stage2.zig b/src-self-hosted/stage2.zig index b63b6e8ca8..bfc49a876b 100644 --- a/src-self-hosted/stage2.zig +++ b/src-self-hosted/stage2.zig @@ -1293,6 +1293,7 @@ pub const ClangArgIterator = extern struct { dep_file, framework_dir, framework, + nostdlibinc, }; const Args = struct { diff --git a/src/main.cpp b/src/main.cpp index 17a1c2bc53..a2d7577a74 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -447,6 +447,7 @@ static int main0(int argc, char **argv) { bool ensure_libc_on_non_freestanding = false; bool ensure_libcpp_on_non_freestanding = false; bool disable_c_depfile = false; + bool want_native_include_dirs = false; Buf *linker_optimization = nullptr; OptionalBool linker_gc_sections = OptionalBoolNull; OptionalBool linker_allow_shlib_undefined = OptionalBoolNull; @@ -581,6 +582,7 @@ static int main0(int argc, char **argv) { strip = true; ensure_libc_on_non_freestanding = true; ensure_libcpp_on_non_freestanding = (strcmp(argv[1], "c++") == 0); + want_native_include_dirs = true; bool c_arg = false; Stage2ClangArgIterator it; @@ -747,6 +749,9 @@ static int main0(int argc, char **argv) { case Stage2ClangArgFramework: frameworks.append(it.only_arg); break; + case Stage2ClangArgNoStdLibInc: + want_native_include_dirs = false; + break; } } // Parse linker args @@ -1418,7 +1423,7 @@ static int main0(int argc, char **argv) { } } - if (target.is_native_os && any_system_lib_dependencies) { + if (target.is_native_os && (any_system_lib_dependencies || want_native_include_dirs)) { Error err; Stage2NativePaths paths; if ((err = stage2_detect_native_paths(&paths))) { diff --git a/src/stage2.h b/src/stage2.h index 02b33419f6..e397291f52 100644 --- a/src/stage2.h +++ b/src/stage2.h @@ -352,6 +352,7 @@ enum Stage2ClangArg { Stage2ClangArgDepFile, Stage2ClangArgFrameworkDir, Stage2ClangArgFramework, + Stage2ClangArgNoStdLibInc, }; // ABI warning diff --git a/tools/update_clang_options.zig b/tools/update_clang_options.zig index fd6edbdf2c..f1029cb067 100644 --- a/tools/update_clang_options.zig +++ b/tools/update_clang_options.zig @@ -54,6 +54,10 @@ const known_options = [_]KnownOpt{ .name = "fno-PIC", .ident = "no_pic", }, + .{ + .name = "nolibc", + .ident = "nostdlib", + }, .{ .name = "nostdlib", .ident = "nostdlib", @@ -66,6 +70,22 @@ const known_options = [_]KnownOpt{ .name = "nostdlib++", .ident = "nostdlib_cpp", }, + .{ + .name = "nostdinc++", + .ident = "nostdlib_cpp", + }, + .{ + .name = "nostdlibinc", + .ident = "nostdlibinc", + }, + .{ + .name = "nostdinc", + .ident = "nostdlibinc", + }, + .{ + .name = "no-standard-includes", + .ident = "nostdlibinc", + }, .{ .name = "shared", .ident = "shared", -- cgit v1.2.3 From 64d0960244a219526fc100b17f4ecd26223df496 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 6 Apr 2020 19:13:36 -0400 Subject: zig cc: recognize a few more linker options * `--major-image-version` * `--minor-image-version` * `--stack` --- src/all_types.hpp | 1 + src/codegen.cpp | 1 + src/link.cpp | 6 ++++-- src/main.cpp | 23 +++++++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) (limited to 'src/main.cpp') diff --git a/src/all_types.hpp b/src/all_types.hpp index 9b22279b91..650dcfd0c7 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -2259,6 +2259,7 @@ struct CodeGen { size_t version_minor; size_t version_patch; const char *linker_script; + size_t stack_size_override; BuildMode build_mode; OutType out_type; diff --git a/src/codegen.cpp b/src/codegen.cpp index a2cd5fafc0..7de26e0b6d 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -10597,6 +10597,7 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { cache_int(ch, g->linker_allow_shlib_undefined); cache_bool(ch, g->linker_z_nodelete); cache_bool(ch, g->linker_z_defs); + cache_usize(ch, g->stack_size_override); // gen_c_objects appends objects to g->link_objects which we want to include in the hash gen_c_objects(g); diff --git a/src/link.cpp b/src/link.cpp index 4ee8915e55..439b76a756 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1840,7 +1840,8 @@ static void construct_linker_job_elf(LinkJob *lj) { if (g->out_type == OutTypeExe) { lj->args.append("-z"); - lj->args.append("stack-size=16777216"); // default to 16 MiB + size_t stack_size = (g->stack_size_override == 0) ? 16777216 : g->stack_size_override; + lj->args.append(buf_ptr(buf_sprintf("stack-size=%" ZIG_PRI_usize, stack_size))); } if (g->linker_script) { @@ -2479,7 +2480,8 @@ static void construct_linker_job_coff(LinkJob *lj) { if (g->out_type == OutTypeExe) { // TODO compile time stack upper bound detection - lj->args.append("-STACK:16777216"); + size_t stack_size = (g->stack_size_override == 0) ? 16777216 : g->stack_size_override; + lj->args.append(buf_ptr(buf_sprintf("-STACK:%" ZIG_PRI_usize, stack_size))); } coff_append_machine_arg(g, &lj->args); diff --git a/src/main.cpp b/src/main.cpp index a2d7577a74..8805382f37 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -453,6 +453,7 @@ static int main0(int argc, char **argv) { OptionalBool linker_allow_shlib_undefined = OptionalBoolNull; bool linker_z_nodelete = false; bool linker_z_defs = false; + size_t stack_size_override = 0; ZigList llvm_argv = {0}; llvm_argv.append("zig (LLVM option parsing)"); @@ -848,6 +849,27 @@ static int main0(int argc, char **argv) { } else { fprintf(stderr, "warning: unsupported linker arg: -z %s\n", buf_ptr(z_arg)); } + } else if (buf_eql_str(arg, "--major-image-version")) { + i += 1; + if (i >= linker_args.length) { + fprintf(stderr, "expected linker arg after '%s'\n", buf_ptr(arg)); + return EXIT_FAILURE; + } + ver_major = atoi(buf_ptr(linker_args.at(i))); + } else if (buf_eql_str(arg, "--minor-image-version")) { + i += 1; + if (i >= linker_args.length) { + fprintf(stderr, "expected linker arg after '%s'\n", buf_ptr(arg)); + return EXIT_FAILURE; + } + ver_minor = atoi(buf_ptr(linker_args.at(i))); + } else if (buf_eql_str(arg, "--stack")) { + i += 1; + if (i >= linker_args.length) { + fprintf(stderr, "expected linker arg after '%s'\n", buf_ptr(arg)); + return EXIT_FAILURE; + } + stack_size_override = atoi(buf_ptr(linker_args.at(i))); } else { fprintf(stderr, "warning: unsupported linker arg: %s\n", buf_ptr(arg)); } @@ -1573,6 +1595,7 @@ static int main0(int argc, char **argv) { g->linker_allow_shlib_undefined = linker_allow_shlib_undefined; g->linker_z_nodelete = linker_z_nodelete; g->linker_z_defs = linker_z_defs; + g->stack_size_override = stack_size_override; if (override_soname) { g->override_soname = buf_create_from_str(override_soname); -- cgit v1.2.3