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 {...@@ -480,12 +480,19 @@ pub const CrossTarget = struct {
480 return Target.libPrefix_cpu_arch_abi(self.getCpuArch(), self.getAbi());480 return Target.libPrefix_cpu_arch_abi(self.getCpuArch(), self.getAbi());
481 }481 }
482482
483 pub fn isNative(self: CrossTarget) bool {483 pub fn isNativeCpu(self: CrossTarget) bool {
484 return self.cpu_arch == null and484 return self.cpu_arch == null and
485 (self.cpu_model == .native or self.cpu_model == .determined_by_cpu_arch) and485 (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() and486 self.cpu_features_sub.isEmpty() and self.cpu_features_add.isEmpty();
487 self.os_tag == null and self.os_version_min == null and self.os_version_max == null and487 }
488 self.abi == null and self.dynamic_linker.get() == null and self.glibc_version == null;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;
489 }496 }
490497
491 pub fn zigTriple(self: CrossTarget, allocator: *mem.Allocator) error{OutOfMemory}![:0]u8 {498 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 {...@@ -898,7 +898,8 @@ const Stage2Target = extern struct {
898 abi: c_int,898 abi: c_int,
899 os: c_int,899 os: c_int,
900900
901 is_native: bool,901 is_native_os: bool,
902 is_native_cpu: bool,
902903
903 glibc_or_darwin_version: ?*Stage2SemVer,904 glibc_or_darwin_version: ?*Stage2SemVer,
904905
...@@ -1166,7 +1167,8 @@ const Stage2Target = extern struct {...@@ -1166,7 +1167,8 @@ const Stage2Target = extern struct {
1166 .os_builtin_str = os_builtin_str_buffer.toOwnedSlice().ptr,1167 .os_builtin_str = os_builtin_str_buffer.toOwnedSlice().ptr,
1167 .cache_hash = cache_hash_slice.ptr,1168 .cache_hash = cache_hash_slice.ptr,
1168 .cache_hash_len = cache_hash_slice.len,1169 .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(),
1170 .glibc_or_darwin_version = glibc_or_darwin_version,1172 .glibc_or_darwin_version = glibc_or_darwin_version,
1171 .dynamic_linker = dynamic_linker,1173 .dynamic_linker = dynamic_linker,
1172 .standard_dynamic_linker_path = std_dl_z,1174 .standard_dynamic_linker_path = std_dl_z,
src/codegen.cpp+9-6
...@@ -8782,7 +8782,8 @@ static Error define_builtin_compile_vars(CodeGen *g) {...@@ -8782,7 +8782,8 @@ static Error define_builtin_compile_vars(CodeGen *g) {
8782 cache_bool(&cache_hash, g->is_single_threaded);8782 cache_bool(&cache_hash, g->is_single_threaded);
8783 cache_bool(&cache_hash, g->test_is_evented);8783 cache_bool(&cache_hash, g->test_is_evented);
8784 cache_int(&cache_hash, g->code_model);8784 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);
8786 cache_int(&cache_hash, g->zig_target->arch);8787 cache_int(&cache_hash, g->zig_target->arch);
8787 cache_int(&cache_hash, g->zig_target->vendor);8788 cache_int(&cache_hash, g->zig_target->vendor);
8788 cache_int(&cache_hash, g->zig_target->os);8789 cache_int(&cache_hash, g->zig_target->os);
...@@ -8917,7 +8918,7 @@ static void init(CodeGen *g) {...@@ -8917,7 +8918,7 @@ static void init(CodeGen *g) {
8917 const char *target_specific_cpu_args = "";8918 const char *target_specific_cpu_args = "";
8918 const char *target_specific_features = "";8919 const char *target_specific_features = "";
89198920
8920 if (g->zig_target->is_native) {8921 if (g->zig_target->is_native_cpu) {
8921 target_specific_cpu_args = ZigLLVMGetHostCPUName();8922 target_specific_cpu_args = ZigLLVMGetHostCPUName();
8922 target_specific_features = ZigLLVMGetNativeFeatures();8923 target_specific_features = ZigLLVMGetNativeFeatures();
8923 }8924 }
...@@ -9034,7 +9035,7 @@ static void detect_libc(CodeGen *g) {...@@ -9034,7 +9035,7 @@ static void detect_libc(CodeGen *g) {
9034 return;9035 return;
9035 }9036 }
90369037
9037 if (g->zig_target->is_native) {9038 if (g->zig_target->is_native_os) {
9038 g->libc = heap::c_allocator.create<Stage2LibCInstallation>();9039 g->libc = heap::c_allocator.create<Stage2LibCInstallation>();
90399040
9040 // search for native_libc.txt in following dirs:9041 // 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...@@ -9672,7 +9673,8 @@ Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose
9672 cache_int(cache_hash, g->err_color);9673 cache_int(cache_hash, g->err_color);
9673 cache_buf(cache_hash, g->zig_c_headers_dir);9674 cache_buf(cache_hash, g->zig_c_headers_dir);
9674 cache_list_of_str(cache_hash, g->libc_include_dir_list, g->libc_include_dir_len);9675 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);
9676 cache_int(cache_hash, g->zig_target->arch);9678 cache_int(cache_hash, g->zig_target->arch);
9677 cache_int(cache_hash, g->zig_target->vendor);9679 cache_int(cache_hash, g->zig_target->vendor);
9678 cache_int(cache_hash, g->zig_target->os);9680 cache_int(cache_hash, g->zig_target->os);
...@@ -10474,7 +10476,8 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {...@@ -10474,7 +10476,8 @@ static Error check_cache(CodeGen *g, Buf *manifest_dir, Buf *digest) {
10474 cache_list_of_buf(ch, g->forbidden_libs.items, g->forbidden_libs.length);10476 cache_list_of_buf(ch, g->forbidden_libs.items, g->forbidden_libs.length);
10475 cache_int(ch, g->build_mode);10477 cache_int(ch, g->build_mode);
10476 cache_int(ch, g->out_type);10478 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);
10478 cache_int(ch, g->zig_target->arch);10481 cache_int(ch, g->zig_target->arch);
10479 cache_int(ch, g->zig_target->vendor);10482 cache_int(ch, g->zig_target->vendor);
10480 cache_int(ch, g->zig_target->os);10483 cache_int(ch, g->zig_target->os);
...@@ -10941,7 +10944,7 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget...@@ -10941,7 +10944,7 @@ CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget
10941 os_path_join(g->zig_std_dir, buf_sprintf("special"), g->zig_std_special_dir);10944 os_path_join(g->zig_std_dir, buf_sprintf("special"), g->zig_std_special_dir);
1094210945
10943 assert(target != nullptr);10946 assert(target != nullptr);
10944 if (!target->is_native) {10947 if (!target->is_native_os) {
10945 g->each_lib_rpath = false;10948 g->each_lib_rpath = false;
10946 } else {10949 } else {
10947 g->each_lib_rpath = true;10950 g->each_lib_rpath = true;
src/link.cpp+3-3
...@@ -635,7 +635,7 @@ static const char *build_libunwind(CodeGen *parent, Stage2ProgressNode *progress...@@ -635,7 +635,7 @@ static const char *build_libunwind(CodeGen *parent, Stage2ProgressNode *progress
635 c_file->args.append("-fPIC");635 c_file->args.append("-fPIC");
636 c_file->args.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS");636 c_file->args.append("-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS");
637 c_file->args.append("-Wa,--noexecstack");637 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) {
639 c_file->args.append("-D_LIBUNWIND_IS_NATIVE_ONLY");639 c_file->args.append("-D_LIBUNWIND_IS_NATIVE_ONLY");
640 }640 }
641 if (parent->build_mode == BuildModeDebug) {641 if (parent->build_mode == BuildModeDebug) {
...@@ -1829,7 +1829,7 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -1829,7 +1829,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
1829 }1829 }
1830 }1830 }
18311831
1832 if (!g->zig_target->is_native) {1832 if (!g->zig_target->is_native_os) {
1833 lj->args.append("--allow-shlib-undefined");1833 lj->args.append("--allow-shlib-undefined");
1834 }1834 }
1835}1835}
...@@ -2460,7 +2460,7 @@ static void construct_linker_job_macho(LinkJob *lj) {...@@ -2460,7 +2460,7 @@ static void construct_linker_job_macho(LinkJob *lj) {
2460 lj->args.append(buf_ptr(compiler_rt_o_path));2460 lj->args.append(buf_ptr(compiler_rt_o_path));
2461 }2461 }
24622462
2463 if (g->zig_target->is_native) {2463 if (g->zig_target->is_native_os) {
2464 for (size_t lib_i = 0; lib_i < g->link_libs_list.length; lib_i += 1) {2464 for (size_t lib_i = 0; lib_i < g->link_libs_list.length; lib_i += 1) {
2465 LinkLib *link_lib = g->link_libs_list.at(lib_i);2465 LinkLib *link_lib = g->link_libs_list.at(lib_i);
2466 if (target_is_libc_lib_name(g->zig_target, buf_ptr(link_lib->name))) {2466 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) {...@@ -1325,7 +1325,15 @@ static int main0(int argc, char **argv) {
1325 return print_error_usage(arg0);1325 return print_error_usage(arg0);
1326 }1326 }
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) {
1329 Error err;1337 Error err;
1330 Stage2NativePaths paths;1338 Stage2NativePaths paths;
1331 if ((err = stage2_detect_native_paths(&paths))) {1339 if ((err = stage2_detect_native_paths(&paths))) {
...@@ -1338,7 +1346,7 @@ static int main0(int argc, char **argv) {...@@ -1338,7 +1346,7 @@ static int main0(int argc, char **argv) {
1338 }1346 }
1339 for (size_t i = 0; i < paths.include_dirs_len; i += 1) {1347 for (size_t i = 0; i < paths.include_dirs_len; i += 1) {
1340 const char *include_dir = paths.include_dirs_ptr[i];1348 const char *include_dir = paths.include_dirs_ptr[i];
1341 clang_argv.append("-I");1349 clang_argv.append("-isystem");
1342 clang_argv.append(include_dir);1350 clang_argv.append(include_dir);
1343 }1351 }
1344 for (size_t i = 0; i < paths.lib_dirs_len; i += 1) {1352 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) {...@@ -181,7 +181,8 @@ static void get_native_target(ZigTarget *target) {
181 &target->abi,181 &target->abi,
182 &oformat);182 &oformat);
183 target->os = get_zig_os_type(os_type);183 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;
185 if (target->abi == ZigLLVM_UnknownEnvironment) {186 if (target->abi == ZigLLVM_UnknownEnvironment) {
186 target->abi = target_default_abi(target->arch, target->os);187 target->abi = target_default_abi(target->arch, target->os);
187 }188 }
...@@ -204,7 +205,8 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons...@@ -204,7 +205,8 @@ Error stage2_target_parse(struct ZigTarget *target, const char *zig_triple, cons
204 target->llvm_cpu_features = ZigLLVMGetNativeFeatures();205 target->llvm_cpu_features = ZigLLVMGetNativeFeatures();
205 target->cache_hash = "native\n\n";206 target->cache_hash = "native\n\n";
206 } else if (strcmp(mcpu, "baseline") == 0) {207 } else if (strcmp(mcpu, "baseline") == 0) {
207 target->is_native = false;208 target->is_native_os = false;
209 target->is_native_cpu = false;
208 target->llvm_cpu_name = "";210 target->llvm_cpu_name = "";
209 target->llvm_cpu_features = "";211 target->llvm_cpu_features = "";
210 target->cache_hash = "baseline\n\n";212 target->cache_hash = "baseline\n\n";
src/stage2.h+2-1
...@@ -280,7 +280,8 @@ struct ZigTarget {...@@ -280,7 +280,8 @@ struct ZigTarget {
280 enum ZigLLVM_EnvironmentType abi;280 enum ZigLLVM_EnvironmentType abi;
281 Os os;281 Os os;
282282
283 bool is_native;283 bool is_native_os;
284 bool is_native_cpu;
284285
285 // null means default. this is double-purposed to be darwin min version286 // null means default. this is double-purposed to be darwin min version
286 struct Stage2SemVer *glibc_or_darwin_version;287 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) {...@@ -1239,7 +1239,8 @@ void target_libc_enum(size_t index, ZigTarget *out_target) {
1239 out_target->os = libcs_available[index].os;1239 out_target->os = libcs_available[index].os;
1240 out_target->abi = libcs_available[index].abi;1240 out_target->abi = libcs_available[index].abi;
1241 out_target->vendor = ZigLLVM_UnknownVendor;1241 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;
1243}1244}
12441245
1245bool target_has_debug_info(const ZigTarget *target) {1246bool target_has_debug_info(const ZigTarget *target) {