From 7ccf7807b3f12428adc5d9ca0ede4e3f4ec6dbbc Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 7 Jul 2019 17:06:09 -0400 Subject: ability to target any glibc version --- src/main.cpp | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 2 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 179b4fddb0..17d3d627ea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,7 @@ #include "target.hpp" #include "libc_installation.hpp" #include "userland.h" +#include "glibc.hpp" #include @@ -96,6 +97,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " --forbid-library [lib] make it an error to link against lib\n" " --library-path [dir] add a directory to the library search path\n" " --linker-script [path] use a custom linker script\n" + " --version-script [path] provide a version .map file\n" " --object [obj] add object file to build\n" " -L[dir] alias for --library-path\n" " -rdynamic add all symbols to the dynamic symbol table\n" @@ -189,10 +191,33 @@ static int print_target_list(FILE *f) { for (size_t i = 0; i < libc_count; i += 1) { ZigTarget libc_target; target_libc_enum(i, &libc_target); - fprintf(f, " %s-%s-%s\n", target_arch_name(libc_target.arch), - target_os_name(libc_target.os), target_abi_name(libc_target.abi)); + bool is_native = native.arch == libc_target.arch && + native.os == libc_target.os && + native.abi == libc_target.abi; + const char *native_str = is_native ? " (native)" : ""; + fprintf(f, " %s-%s-%s%s\n", target_arch_name(libc_target.arch), + target_os_name(libc_target.os), target_abi_name(libc_target.abi), native_str); } + fprintf(f, "\nAvailable glibc versions:\n"); + ZigGLibCAbi *glibc_abi; + Error err; + if ((err = glibc_load_metadata(&glibc_abi, get_zig_lib_dir(), true))) { + return EXIT_FAILURE; + } + for (size_t i = 0; i < glibc_abi->all_versions.length; i += 1) { + ZigGLibCVersion *this_ver = &glibc_abi->all_versions.at(i); + bool is_native = native.glibc_version != nullptr && + native.glibc_version->major == this_ver->major && + native.glibc_version->minor == this_ver->minor && + native.glibc_version->patch == this_ver->patch; + const char *native_str = is_native ? " (native)" : ""; + if (this_ver->patch == 0) { + fprintf(f, " %d.%d%s\n", this_ver->major, this_ver->minor, native_str); + } else { + fprintf(f, " %d.%d.%d%s\n", this_ver->major, this_ver->minor, this_ver->patch, native_str); + } + } return EXIT_SUCCESS; } @@ -437,6 +462,8 @@ int main(int argc, char **argv) { const char *mmacosx_version_min = nullptr; const char *mios_version_min = nullptr; const char *linker_script = nullptr; + Buf *version_script = nullptr; + const char *target_glibc = nullptr; ZigList rpath_list = {0}; bool each_lib_rpath = false; ZigList objects = {0}; @@ -783,6 +810,10 @@ int main(int argc, char **argv) { frameworks.append(argv[i]); } else if (strcmp(arg, "--linker-script") == 0) { linker_script = argv[i]; + } else if (strcmp(arg, "--version-script") == 0) { + version_script = buf_create_from_str(argv[i]); + } else if (strcmp(arg, "-target-glibc") == 0) { + target_glibc = argv[i]; } else if (strcmp(arg, "-rpath") == 0) { rpath_list.append(argv[i]); } else if (strcmp(arg, "--test-filter") == 0) { @@ -904,6 +935,10 @@ int main(int argc, char **argv) { ZigTarget target; if (target_string == nullptr) { get_native_target(&target); + if (target_glibc != nullptr) { + fprintf(stderr, "-target-glibc provided but no -target parameter\n"); + return print_error_usage(arg0); + } } else { if ((err = target_parse_triple(&target, target_string))) { if (err == ErrorUnknownArchitecture && target.arch != ZigLLVM_UnknownArch) { @@ -921,6 +956,22 @@ int main(int argc, char **argv) { return print_error_usage(arg0); } } + if (target_is_glibc(&target)) { + target.glibc_version = allocate(1); + + if (target_glibc != nullptr) { + if ((err = target_parse_glibc_version(target.glibc_version, target_glibc))) { + fprintf(stderr, "invalid glibc version '%s': %s\n", target_glibc, err_str(err)); + return print_error_usage(arg0); + } + } else { + // Default cross-compiling glibc version + *target.glibc_version = {2, 17, 0}; + } + } else if (target_glibc != nullptr) { + fprintf(stderr, "'%s' is not a glibc-compatible target", target_string); + return print_error_usage(arg0); + } } if (output_dir != nullptr && enable_cache == CacheOptOn) { @@ -1074,6 +1125,7 @@ int main(int argc, char **argv) { codegen_set_is_test(g, cmd == CmdTest); g->want_single_threaded = want_single_threaded; codegen_set_linker_script(g, linker_script); + g->version_script_path = version_script; if (each_lib_rpath) codegen_set_each_lib_rpath(g, each_lib_rpath); -- cgit v1.2.3 From 336ddb80110ed66582674c73aa5b2bcf7f2aac1b Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 7 Jul 2019 17:24:49 -0400 Subject: add -target-glibc to cli help and zig build --- src/main.cpp | 1 + std/build.zig | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 17d3d627ea..43fa75178b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -75,6 +75,7 @@ static int print_full_usage(const char *arg0, FILE *file, int return_code) { " -dynamic create a shared library (.so; .dll; .dylib)\n" " --strip exclude debug symbols\n" " -target [name] -- see the targets command\n" + " -target-glibc [version] target a specific glibc version (default: 2.17)\n" " --verbose-tokenize enable compiler debug output for tokenization\n" " --verbose-ast enable compiler debug output for AST parsing\n" " --verbose-link enable compiler debug output for linking\n" diff --git a/std/build.zig b/std/build.zig index 7387bb11ae..f1cdc2f926 100644 --- a/std/build.zig +++ b/std/build.zig @@ -1053,7 +1053,8 @@ pub const LibExeObjStep = struct { installed_path: ?[]const u8, install_step: ?*InstallArtifactStep, - libc_file: ?[]const u8, + libc_file: ?[]const u8 = null, + target_glibc: ?Version = null, const LinkObject = union(enum) { StaticPath: []const u8, @@ -1148,7 +1149,6 @@ pub const LibExeObjStep = struct { .single_threaded = false, .installed_path = null, .install_step = null, - .libc_file = null, }; self.computeOutFileNames(); return self; @@ -1220,6 +1220,14 @@ pub const LibExeObjStep = struct { self.computeOutFileNames(); } + pub fn setTargetGLibC(self: *LibExeObjStep, major: u32, minor: u32, patch: u32) void { + self.target_glibc = Version{ + .major = major, + .minor = minor, + .patch = patch, + }; + } + pub fn setOutputDir(self: *LibExeObjStep, dir: []const u8) void { self.output_dir = self.builder.dupe(dir); } @@ -1581,6 +1589,11 @@ pub const LibExeObjStep = struct { }, } + if (self.target_glibc) |ver| { + try zig_args.append("-target-glibc"); + try zig_args.append(builder.fmt("{}.{}.{}", ver.major, ver.minor, ver.patch)); + } + if (self.linker_script) |linker_script| { zig_args.append("--linker-script") catch unreachable; zig_args.append(linker_script) catch unreachable; -- cgit v1.2.3 From 56d818565015b8213f6c070236658c8796c86d0a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 7 Jul 2019 17:55:25 -0400 Subject: expose glibc version in builtin --- src/codegen.cpp | 27 +++++++++++++++++++++++++++ src/main.cpp | 4 ++++ std/build.zig | 11 ++++++++--- 3 files changed, 39 insertions(+), 3 deletions(-) (limited to 'src/main.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index 57be8beed1..59d3e1f4a0 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -7918,6 +7918,14 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { //assert(EndianBig == 0); //assert(EndianLittle == 1); } + { + buf_appendf(contents, + "pub const Version = struct {\n" + " major: u32,\n" + " minor: u32,\n" + " patch: u32,\n" + "};\n\n"); + } { buf_appendf(contents, "pub const SubSystem = enum {\n" @@ -7949,6 +7957,15 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { buf_appendf(contents, "pub const os = Os.%s;\n", cur_os); buf_appendf(contents, "pub const arch = %s;\n", cur_arch); buf_appendf(contents, "pub const abi = Abi.%s;\n", cur_abi); + if (g->libc_link_lib != nullptr && g->zig_target->glibc_version != nullptr) { + buf_appendf(contents, + "pub const glibc_version: ?Version = Version{.major = %d, .minor = %d, .patch = %d};\n", + g->zig_target->glibc_version->major, + g->zig_target->glibc_version->minor, + g->zig_target->glibc_version->patch); + } else { + buf_appendf(contents, "pub const glibc_version: ?Version = null;\n"); + } buf_appendf(contents, "pub const object_format = ObjectFormat.%s;\n", cur_obj_fmt); buf_appendf(contents, "pub const mode = %s;\n", build_mode_to_str(g->build_mode)); buf_appendf(contents, "pub const link_libc = %s;\n", bool_to_str(g->libc_link_lib != nullptr)); @@ -8005,6 +8022,11 @@ static Error define_builtin_compile_vars(CodeGen *g) { cache_int(&cache_hash, g->zig_target->vendor); cache_int(&cache_hash, g->zig_target->os); cache_int(&cache_hash, g->zig_target->abi); + if (g->zig_target->glibc_version != nullptr) { + cache_int(&cache_hash, g->zig_target->glibc_version->major); + cache_int(&cache_hash, g->zig_target->glibc_version->minor); + cache_int(&cache_hash, g->zig_target->glibc_version->patch); + } cache_bool(&cache_hash, g->have_err_ret_tracing); cache_bool(&cache_hash, g->libc_link_lib != nullptr); cache_bool(&cache_hash, g->valgrind_support); @@ -9465,6 +9487,11 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) { cache_int(ch, g->zig_target->vendor); cache_int(ch, g->zig_target->os); cache_int(ch, g->zig_target->abi); + if (g->zig_target->glibc_version != nullptr) { + cache_int(ch, g->zig_target->glibc_version->major); + cache_int(ch, g->zig_target->glibc_version->minor); + cache_int(ch, g->zig_target->glibc_version->patch); + } cache_int(ch, detect_subsystem(g)); cache_bool(ch, g->strip_debug_symbols); cache_bool(ch, g->is_test_build); diff --git a/src/main.cpp b/src/main.cpp index 43fa75178b..9329229a7b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1015,6 +1015,10 @@ int main(int argc, char **argv) { CodeGen *g = codegen_create(main_pkg_path, nullptr, &target, out_type, build_mode, override_lib_dir, override_std_dir, nullptr, nullptr); codegen_set_strip(g, strip); + for (size_t i = 0; i < link_libs.length; i += 1) { + LinkLib *link_lib = codegen_add_link_lib(g, buf_create_from_str(link_libs.at(i))); + link_lib->provided_explicitly = true; + } g->subsystem = subsystem; g->valgrind_support = valgrind_support; g->want_pic = want_pic; diff --git a/std/build.zig b/std/build.zig index f1cdc2f926..87f589bc5b 100644 --- a/std/build.zig +++ b/std/build.zig @@ -1011,7 +1011,8 @@ pub const LibExeObjStep = struct { builder: *Builder, name: []const u8, target: Target, - linker_script: ?[]const u8, + linker_script: ?[]const u8 = null, + version_script: ?[]const u8 = null, out_filename: []const u8, is_dynamic: bool, version: Version, @@ -1118,7 +1119,6 @@ pub const LibExeObjStep = struct { .root_src = root_src, .name = name, .target = Target.Native, - .linker_script = null, .frameworks = BufSet.init(builder.allocator), .step = Step.init(name, builder.allocator, make), .version = ver, @@ -1596,7 +1596,12 @@ pub const LibExeObjStep = struct { if (self.linker_script) |linker_script| { zig_args.append("--linker-script") catch unreachable; - zig_args.append(linker_script) catch unreachable; + zig_args.append(builder.pathFromRoot(linker_script)) catch unreachable; + } + + if (self.version_script) |version_script| { + try zig_args.append("--version-script"); + try zig_args.append(builder.pathFromRoot(version_script)); } if (self.exec_cmd_args) |exec_cmd_args| { -- cgit v1.2.3