authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-25 20:32:40-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-25 20:34:15-04:00
logfae6cf09619e7a8e64f61b84cca7d9fcd471262f
tree373d125e6c7eb97f563af264de62eb48fb6b5baa
parentdd66fbb96a2e1f85e7dacdce18ebc5f85d26d0cc
signaturelock-open Commit is signed but in an unrecognized format.

improved handling of native system directories

* `-isystem` instead of `-I` for system include directories fixes a problem with native system directories interfering with zig's bundled libc. * separate Stage2Target.is_native into Stage2Target.is_native_os and Stage2Target.is_native_cpu.

8 files changed, 45 insertions(+), 21 deletions(-)

lib/std/zig/cross_target.zig+11-4
......@@ -480,12 +480,19 @@ pub const CrossTarget = struct {
480480 return Target.libPrefix_cpu_arch_abi(self.getCpuArch(), self.getAbi());
481481 }
482482
483 pub fn isNative(self: CrossTarget) bool {
483 pub fn isNativeCpu(self: CrossTarget) bool {
484484 return self.cpu_arch == null and
485485 (self.cpu_model == .native or self.cpu_model == .determined_by_cpu_arch) and
486 self.cpu_features_sub.isEmpty() and self.cpu_features_add.isEmpty() and
487 self.os_tag == null and self.os_version_min == null and self.os_version_max == null and
488 self.abi == null and self.dynamic_linker.get() == null and self.glibc_version == null;
486 self.cpu_features_sub.isEmpty() and self.cpu_features_add.isEmpty();
487 }
488
489 pub fn isNativeOs(self: CrossTarget) bool {
490 return self.os_tag == null and self.os_version_min == null and self.os_version_max == null and
491 self.dynamic_linker.get() == null and self.glibc_version == null;
492 }
493
494 pub fn isNative(self: CrossTarget) bool {
495 return self.isNativeCpu() and self.isNativeOs() and self.abi == null;
489496 }
490497
491498 pub fn zigTriple(self: CrossTarget, allocator: *mem.Allocator) error{OutOfMemory}![:0]u8 {
src-self-hosted/stage2.zig+4-2
......@@ -898,7 +898,8 @@ const Stage2Target = extern struct {
898898 abi: c_int,
899899 os: c_int,
900900
901 is_native: bool,
901 is_native_os: bool,
902 is_native_cpu: bool,
902903
903904 glibc_or_darwin_version: ?*Stage2SemVer,
904905
......@@ -1166,7 +1167,8 @@ const Stage2Target = extern struct {
11661167 .os_builtin_str = os_builtin_str_buffer.toOwnedSlice().ptr,
11671168 .cache_hash = cache_hash_slice.ptr,
11681169 .cache_hash_len = cache_hash_slice.len,
1169 .is_native = cross_target.isNative(),
1170 .is_native_os = cross_target.isNativeOs(),
1171 .is_native_cpu = cross_target.isNativeCpu(),
11701172 .glibc_or_darwin_version = glibc_or_darwin_version,
11711173 .dynamic_linker = dynamic_linker,
11721174 .standard_dynamic_linker_path = std_dl_z,
src/codegen.cpp+9-6
......@@ -8782,7 +8782,8 @@ static Error define_builtin_compile_vars(CodeGen *g) {
87828782 cache_bool(&cache_hash, g->is_single_threaded);
87838783 cache_bool(&cache_hash, g->test_is_evented);
87848784 cache_int(&cache_hash, g->code_model);
8785 cache_int(&cache_hash, g->zig_target->is_native);
8785 cache_int(&cache_hash, g->zig_target->is_native_os);
8786 cache_int(&cache_hash, g->zig_target->is_native_cpu);
87868787 cache_int(&cache_hash, g->zig_target->arch);
87878788 cache_int(&cache_hash, g->zig_target->vendor);
87888789 cache_int(&cache_hash, g->zig_target->os);
......@@ -8917,7 +8918,7 @@ static void init(CodeGen *g) {
89178918 const char *target_specific_cpu_args = "";
89188919 const char *target_specific_features = "";
89198920
8920 if (g->zig_target->is_native) {
8921 if (g->zig_target->is_native_cpu) {
89218922 target_specific_cpu_args = ZigLLVMGetHostCPUName();
89228923 target_specific_features = ZigLLVMGetNativeFeatures();
89238924 }
......@@ -9034,7 +9035,7 @@ static void detect_libc(CodeGen *g) {
90349035 return;
90359036 }
90369037
9037 if (g->zig_target->is_native) {
9038 if (g->zig_target->is_native_os) {
90389039 g->libc = heap::c_allocator.create<Stage2LibCInstallation>();
90399040
90409041 // search for native_libc.txt in following dirs:
......@@ -9672,7 +9673,8 @@ Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose
96729673 cache_int(cache_hash, g->err_color);
96739674 cache_buf(cache_hash, g->zig_c_headers_dir);
96749675 cache_list_of_str(cache_hash, g->libc_include_dir_list, g->libc_include_dir_len);
9675 cache_int(cache_hash, g->zig_target->is_native);
9676 cache_int(cache_hash, g->zig_target->is_native_os);
9677 cache_int(cache_hash, g->zig_target->is_native_cpu);
96769678 cache_int(cache_hash, g->zig_target->arch);
96779679 cache_int(cache_hash, g->zig_target->vendor);
96789680 cache_int(cache_hash, g->zig_target->os);
......@@ -10474,7 +10476,8 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
1047410476 cache_list_of_buf(ch, g->forbidden_libs.items, g->forbidden_libs.length);
1047510477 cache_int(ch, g->build_mode);
1047610478 cache_int(ch, g->out_type);
10477 cache_bool(ch, g->zig_target->is_native);
10479 cache_bool(ch, g->zig_target->is_native_os);
10480 cache_bool(ch, g->zig_target->is_native_cpu);
1047810481 cache_int(ch, g->zig_target->arch);
1047910482 cache_int(ch, g->zig_target->vendor);
1048010483 cache_int(ch, g->zig_target->os);
......@@ -10941,7 +10944,7 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget
1094110944 os_path_join(g->zig_std_dir, buf_sprintf("special"), g->zig_std_special_dir);
1094210945
1094310946 assert(target != nullptr);
10944 if (!target->is_native) {
10947 if (!target->is_native_os) {
1094510948 g->each_lib_rpath = false;
1094610949 } else {
1094710950 g->each_lib_rpath = true;
src/link.cpp+3-3
......@@ -635,7 +635,7 @@ static const char *build_libunwind(CodeGen *parent, Stage2ProgressNode *progress
635635 c_file->args.append("-fPIC");
636636 c_file->args.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS");
637637 c_file->args.append("-Wa,--noexecstack");
638 if (parent->zig_target->is_native) {
638 if (parent->zig_target->is_native_os && parent->zig_target->is_native_cpu) {
639639 c_file->args.append("-D_LIBUNWIND_IS_NATIVE_ONLY");
640640 }
641641 if (parent->build_mode == BuildModeDebug) {
......@@ -1829,7 +1829,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
18291829 }
18301830 }
18311831
1832 if (!g->zig_target->is_native) {
1832 if (!g->zig_target->is_native_os) {
18331833 lj->args.append("--allow-shlib-undefined");
18341834 }
18351835}
......@@ -2460,7 +2460,7 @@ static void construct_linker_job_macho(LinkJob *lj) {
24602460 lj->args.append(buf_ptr(compiler_rt_o_path));
24612461 }
24622462
2463 if (g->zig_target->is_native) {
2463 if (g->zig_target->is_native_os) {
24642464 for (size_t lib_i = 0; lib_i < g->link_libs_list.length; lib_i += 1) {
24652465 LinkLib *link_lib = g->link_libs_list.at(lib_i);
24662466 if (target_is_libc_lib_name(g->zig_target, buf_ptr(link_lib->name))) {
src/main.cpp+10-2
......@@ -1325,7 +1325,15 @@ static int main0(int argc, char **argv) {
13251325 return print_error_usage(arg0);
13261326 }
13271327
1328 if (target.is_native && link_libs.length != 0) {
1328 bool any_non_c_link_libs = false;
1329 for (size_t i = 0; i < link_libs.length; i += 1) {
1330 if (!target_is_libc_lib_name(&target, link_libs.at(i))) {
1331 any_non_c_link_libs = true;
1332 break;
1333 }
1334 }
1335
1336 if (target.is_native_os && any_non_c_link_libs) {
13291337 Error err;
13301338 Stage2NativePaths paths;
13311339 if ((err = stage2_detect_native_paths(&paths))) {
......@@ -1338,7 +1346,7 @@ static int main0(int argc, char **argv) {
13381346 }
13391347 for (size_t i = 0; i < paths.include_dirs_len; i += 1) {
13401348 const char *include_dir = paths.include_dirs_ptr[i];
1341 clang_argv.append("-I");
1349 clang_argv.append("-isystem");
13421350 clang_argv.append(include_dir);
13431351 }
13441352 for (size_t i = 0; i < paths.lib_dirs_len; i += 1) {
src/stage2.cpp+4-2
......@@ -181,7 +181,8 @@ static void get_native_target(ZigTarget *target) {
181181 &target->abi,
182182 &oformat);
183183 target->os = get_zig_os_type(os_type);
184 target->is_native = true;
184 target->is_native_os = true;
185 target->is_native_cpu = true;
185186 if (target->abi == ZigLLVM_UnknownEnvironment) {
186187 target->abi = target_default_abi(target->arch, target->os);
187188 }
......@@ -204,7 +205,8 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons
204205 target->llvm_cpu_features = ZigLLVMGetNativeFeatures();
205206 target->cache_hash = "native\n\n";
206207 } else if (strcmp(mcpu, "baseline") == 0) {
207 target->is_native = false;
208 target->is_native_os = false;
209 target->is_native_cpu = false;
208210 target->llvm_cpu_name = "";
209211 target->llvm_cpu_features = "";
210212 target->cache_hash = "baseline\n\n";
src/stage2.h+2-1
......@@ -280,7 +280,8 @@ struct ZigTarget {
280280 enum ZigLLVM_EnvironmentType abi;
281281 Os os;
282282
283 bool is_native;
283 bool is_native_os;
284 bool is_native_cpu;
284285
285286 // null means default. this is double-purposed to be darwin min version
286287 struct Stage2SemVer *glibc_or_darwin_version;
src/target.cpp+2-1
......@@ -1239,7 +1239,8 @@ void target_libc_enum(size_t index, ZigTarget *out_target) {
12391239 out_target->os = libcs_available[index].os;
12401240 out_target->abi = libcs_available[index].abi;
12411241 out_target->vendor = ZigLLVM_UnknownVendor;
1242 out_target->is_native = false;
1242 out_target->is_native_os = false;
1243 out_target->is_native_cpu = false;
12431244}
12441245
12451246bool target_has_debug_info(const ZigTarget *target) {