authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-07 13:04:10-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-07 13:04:10-05:00
loge2ce00f272acdf3a0b5bdb3f9322aa1c487c483d
tree3d54be5c7ed157c65bc321d035a13e3a9d696284
parentffaa4f0a87af60718062c3a9eeab9fb354731241

fix regressions on macos


5 files changed, 44 insertions(+), 68 deletions(-)

src/codegen.cpp+3-5
...@@ -240,10 +240,6 @@ void codegen_set_emit_file_type(CodeGen *g, EmitFileType emit_file_type) {...@@ -240,10 +240,6 @@ void codegen_set_emit_file_type(CodeGen *g, EmitFileType emit_file_type) {
240 g->emit_file_type = emit_file_type;240 g->emit_file_type = emit_file_type;
241}241}
242242
243void codegen_set_is_static(CodeGen *g, bool is_static) {
244 g->is_static = is_static;
245}
246
247void codegen_set_each_lib_rpath(CodeGen *g, bool each_lib_rpath) {243void codegen_set_each_lib_rpath(CodeGen *g, bool each_lib_rpath) {
248 g->each_lib_rpath = each_lib_rpath;244 g->each_lib_rpath = each_lib_rpath;
249}245}
...@@ -9265,7 +9261,9 @@ void codegen_build_and_link(CodeGen *g) {...@@ -9265,7 +9261,9 @@ void codegen_build_and_link(CodeGen *g) {
9265 }9261 }
9266 }9262 }
92679263
9268 if (g->emit_file_type == EmitFileTypeBinary && !compilation_is_already_done(g)) {9264 if (g->emit_file_type == EmitFileTypeBinary &&
9265 (g->link_objects.length > 1 || g->out_type != OutTypeObj))
9266 {
9269 codegen_link(g);9267 codegen_link(g);
9270 }9268 }
9271 }9269 }
src/codegen.hpp-1
...@@ -25,7 +25,6 @@ void codegen_set_is_test(CodeGen *codegen, bool is_test);...@@ -25,7 +25,6 @@ void codegen_set_is_test(CodeGen *codegen, bool is_test);
25void codegen_set_each_lib_rpath(CodeGen *codegen, bool each_lib_rpath);25void codegen_set_each_lib_rpath(CodeGen *codegen, bool each_lib_rpath);
2626
27void codegen_set_emit_file_type(CodeGen *g, EmitFileType emit_file_type);27void codegen_set_emit_file_type(CodeGen *g, EmitFileType emit_file_type);
28void codegen_set_is_static(CodeGen *codegen, bool is_static);
29void codegen_set_strip(CodeGen *codegen, bool strip);28void codegen_set_strip(CodeGen *codegen, bool strip);
30void codegen_set_errmsg_color(CodeGen *codegen, ErrColor err_color);29void codegen_set_errmsg_color(CodeGen *codegen, ErrColor err_color);
31void codegen_set_out_name(CodeGen *codegen, Buf *out_name);30void codegen_set_out_name(CodeGen *codegen, Buf *out_name);
src/link.cpp+33-58
...@@ -574,7 +574,7 @@ static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path)...@@ -574,7 +574,7 @@ static Buf *build_a_raw(CodeGen *parent_gen, const char *aname, Buf *full_path)
574574
575 CodeGen *child_gen = create_child_codegen(parent_gen, full_path, child_out_type,575 CodeGen *child_gen = create_child_codegen(parent_gen, full_path, child_out_type,
576 parent_gen->libc);576 parent_gen->libc);
577 codegen_set_is_static(child_gen, true);577 child_gen->is_static = true;
578 codegen_set_out_name(child_gen, buf_create_from_str(aname));578 codegen_set_out_name(child_gen, buf_create_from_str(aname));
579579
580 // This is so that compiler_rt and builtin libraries know whether they580 // This is so that compiler_rt and builtin libraries know whether they
...@@ -699,7 +699,7 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -699,7 +699,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
699 lj->args.append(getLDMOption(g->zig_target));699 lj->args.append(getLDMOption(g->zig_target));
700700
701 bool is_lib = g->out_type == OutTypeLib;701 bool is_lib = g->out_type == OutTypeLib;
702 bool shared = !g->is_static && is_lib;702 bool is_dyn_lib = !g->is_static && is_lib;
703 Buf *soname = nullptr;703 Buf *soname = nullptr;
704 if (g->is_static) {704 if (g->is_static) {
705 if (g->zig_target->arch == ZigLLVM_arm || g->zig_target->arch == ZigLLVM_armeb ||705 if (g->zig_target->arch == ZigLLVM_arm || g->zig_target->arch == ZigLLVM_armeb ||
...@@ -709,7 +709,7 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -709,7 +709,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
709 } else {709 } else {
710 lj->args.append("-static");710 lj->args.append("-static");
711 }711 }
712 } else if (shared) {712 } else if (is_dyn_lib) {
713 lj->args.append("-shared");713 lj->args.append("-shared");
714714
715 if (buf_len(&g->output_file_path) == 0) {715 if (buf_len(&g->output_file_path) == 0) {
...@@ -780,7 +780,7 @@ static void construct_linker_job_elf(LinkJob *lj) {...@@ -780,7 +780,7 @@ static void construct_linker_job_elf(LinkJob *lj) {
780780
781 }781 }
782782
783 if (shared) {783 if (is_dyn_lib) {
784 lj->args.append("-soname");784 lj->args.append("-soname");
785 lj->args.append(buf_ptr(soname));785 lj->args.append(buf_ptr(soname));
786 }786 }
...@@ -1231,36 +1231,34 @@ static void construct_linker_job_macho(LinkJob *lj) {...@@ -1231,36 +1231,34 @@ static void construct_linker_job_macho(LinkJob *lj) {
1231 }1231 }
12321232
1233 bool is_lib = g->out_type == OutTypeLib;1233 bool is_lib = g->out_type == OutTypeLib;
1234 bool shared = !g->is_static && is_lib;1234 bool is_dyn_lib = !g->is_static && is_lib;
1235 if (g->is_static) {1235 if (g->is_static) {
1236 lj->args.append("-static");1236 lj->args.append("-static");
1237 } else {1237 } else {
1238 lj->args.append("-dynamic");1238 lj->args.append("-dynamic");
1239 }1239 }
12401240
1241 if (is_lib) {1241 if (is_dyn_lib) {
1242 if (!g->is_static) {1242 lj->args.append("-dylib");
1243 lj->args.append("-dylib");
12441243
1245 Buf *compat_vers = buf_sprintf("%" ZIG_PRI_usize ".0.0", g->version_major);1244 Buf *compat_vers = buf_sprintf("%" ZIG_PRI_usize ".0.0", g->version_major);
1246 lj->args.append("-compatibility_version");1245 lj->args.append("-compatibility_version");
1247 lj->args.append(buf_ptr(compat_vers));1246 lj->args.append(buf_ptr(compat_vers));
12481247
1249 Buf *cur_vers = buf_sprintf("%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".%" ZIG_PRI_usize,1248 Buf *cur_vers = buf_sprintf("%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".%" ZIG_PRI_usize,
1250 g->version_major, g->version_minor, g->version_patch);1249 g->version_major, g->version_minor, g->version_patch);
1251 lj->args.append("-current_version");1250 lj->args.append("-current_version");
1252 lj->args.append(buf_ptr(cur_vers));1251 lj->args.append(buf_ptr(cur_vers));
12531252
1254 // TODO getting an error when running an executable when doing this rpath thing1253 // TODO getting an error when running an executable when doing this rpath thing
1255 //Buf *dylib_install_name = buf_sprintf("@rpath/lib%s.%" ZIG_PRI_usize ".dylib",1254 //Buf *dylib_install_name = buf_sprintf("@rpath/lib%s.%" ZIG_PRI_usize ".dylib",
1256 // buf_ptr(g->root_out_name), g->version_major);1255 // buf_ptr(g->root_out_name), g->version_major);
1257 //lj->args.append("-install_name");1256 //lj->args.append("-install_name");
1258 //lj->args.append(buf_ptr(dylib_install_name));1257 //lj->args.append(buf_ptr(dylib_install_name));
12591258
1260 if (buf_len(&g->output_file_path) == 0) {1259 if (buf_len(&g->output_file_path) == 0) {
1261 buf_appendf(&g->output_file_path, "lib%s.%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".dylib",1260 buf_appendf(&g->output_file_path, "lib%s.%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".%" ZIG_PRI_usize ".dylib",
1262 buf_ptr(g->root_out_name), g->version_major, g->version_minor, g->version_patch);1261 buf_ptr(g->root_out_name), g->version_major, g->version_minor, g->version_patch);
1263 }
1264 }1262 }
1265 }1263 }
12661264
...@@ -1302,38 +1300,14 @@ static void construct_linker_job_macho(LinkJob *lj) {...@@ -1302,38 +1300,14 @@ static void construct_linker_job_macho(LinkJob *lj) {
1302 Buf *rpath = g->rpath_list.at(i);1300 Buf *rpath = g->rpath_list.at(i);
1303 add_rpath(lj, rpath);1301 add_rpath(lj, rpath);
1304 }1302 }
1305 add_rpath(lj, &g->output_file_path);1303 if (is_dyn_lib) {
1304 add_rpath(lj, &g->output_file_path);
1305 }
13061306
1307 if (shared) {1307 if (is_dyn_lib) {
1308 if (g->system_linker_hack) {1308 if (g->system_linker_hack) {
1309 lj->args.append("-headerpad_max_install_names");1309 lj->args.append("-headerpad_max_install_names");
1310 }1310 }
1311 } else if (g->is_static) {
1312 lj->args.append("-lcrt0.o");
1313 } else {
1314 switch (platform.kind) {
1315 case MacOS:
1316 if (darwin_version_lt(&platform, 10, 5)) {
1317 lj->args.append("-lcrt1.o");
1318 } else if (darwin_version_lt(&platform, 10, 6)) {
1319 lj->args.append("-lcrt1.10.5.o");
1320 } else if (darwin_version_lt(&platform, 10, 8)) {
1321 lj->args.append("-lcrt1.10.6.o");
1322 }
1323 break;
1324 case IPhoneOS:
1325 if (g->zig_target->arch == ZigLLVM_aarch64) {
1326 // iOS does not need any crt1 files for arm64
1327 } else if (darwin_version_lt(&platform, 3, 1)) {
1328 lj->args.append("-lcrt1.o");
1329 } else if (darwin_version_lt(&platform, 6, 0)) {
1330 lj->args.append("-lcrt1.3.1.o");
1331 }
1332 break;
1333 case IPhoneOSSimulator:
1334 // no crt1.o needed
1335 break;
1336 }
1337 }1311 }
13381312
1339 for (size_t i = 0; i < g->lib_dirs.length; i += 1) {1313 for (size_t i = 0; i < g->lib_dirs.length; i += 1) {
...@@ -1347,7 +1321,7 @@ static void construct_linker_job_macho(LinkJob *lj) {...@@ -1347,7 +1321,7 @@ static void construct_linker_job_macho(LinkJob *lj) {
1347 }1321 }
13481322
1349 // compiler_rt on darwin is missing some stuff, so we still build it and rely on LinkOnce1323 // compiler_rt on darwin is missing some stuff, so we still build it and rely on LinkOnce
1350 if (g->out_type == OutTypeExe || (g->out_type == OutTypeLib && !g->is_static)) {1324 if (g->out_type == OutTypeExe || is_dyn_lib) {
1351 Buf *compiler_rt_o_path = build_compiler_rt(g);1325 Buf *compiler_rt_o_path = build_compiler_rt(g);
1352 lj->args.append(buf_ptr(compiler_rt_o_path));1326 lj->args.append(buf_ptr(compiler_rt_o_path));
1353 }1327 }
...@@ -1356,11 +1330,7 @@ static void construct_linker_job_macho(LinkJob *lj) {...@@ -1356,11 +1330,7 @@ static void construct_linker_job_macho(LinkJob *lj) {
1356 for (size_t lib_i = 0; lib_i < g->link_libs_list.length; lib_i += 1) {1330 for (size_t lib_i = 0; lib_i < g->link_libs_list.length; lib_i += 1) {
1357 LinkLib *link_lib = g->link_libs_list.at(lib_i);1331 LinkLib *link_lib = g->link_libs_list.at(lib_i);
1358 if (buf_eql_str(link_lib->name, "c")) {1332 if (buf_eql_str(link_lib->name, "c")) {
1359 // on Darwin, libSystem has libc in it, but also you have to use it1333 continue;
1360 // to make syscalls because the syscall numbers are not documented
1361 // and change between versions.
1362 // so we always link against libSystem
1363 lj->args.append("-lSystem");
1364 } else {1334 } else {
1365 if (strchr(buf_ptr(link_lib->name), '/') == nullptr) {1335 if (strchr(buf_ptr(link_lib->name), '/') == nullptr) {
1366 Buf *arg = buf_sprintf("-l%s", buf_ptr(link_lib->name));1336 Buf *arg = buf_sprintf("-l%s", buf_ptr(link_lib->name));
...@@ -1370,6 +1340,11 @@ static void construct_linker_job_macho(LinkJob *lj) {...@@ -1370,6 +1340,11 @@ static void construct_linker_job_macho(LinkJob *lj) {
1370 }1340 }
1371 }1341 }
1372 }1342 }
1343 // on Darwin, libSystem has libc in it, but also you have to use it
1344 // to make syscalls because the syscall numbers are not documented
1345 // and change between versions.
1346 // so we always link against libSystem
1347 lj->args.append("-lSystem");
1373 } else {1348 } else {
1374 lj->args.append("-undefined");1349 lj->args.append("-undefined");
1375 lj->args.append("dynamic_lookup");1350 lj->args.append("dynamic_lookup");
src/main.cpp+1-1
...@@ -1037,7 +1037,7 @@ int main(int argc, char **argv) {...@@ -1037,7 +1037,7 @@ int main(int argc, char **argv) {
10371037
1038 codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2);1038 codegen_set_llvm_argv(g, llvm_argv.items + 1, llvm_argv.length - 2);
1039 codegen_set_strip(g, strip);1039 codegen_set_strip(g, strip);
1040 codegen_set_is_static(g, is_static);1040 g->is_static = is_static;
1041 if (dynamic_linker != nullptr)1041 if (dynamic_linker != nullptr)
1042 codegen_set_dynamic_linker(g, buf_create_from_str(dynamic_linker));1042 codegen_set_dynamic_linker(g, buf_create_from_str(dynamic_linker));
1043 g->verbose_tokenize = verbose_tokenize;1043 g->verbose_tokenize = verbose_tokenize;
src/os.cpp+7-3
...@@ -1414,13 +1414,17 @@ static void init_rand() {...@@ -1414,13 +1414,17 @@ static void init_rand() {
1414 if (fd == -1) {1414 if (fd == -1) {
1415 zig_panic("unable to open /dev/urandom");1415 zig_panic("unable to open /dev/urandom");
1416 }1416 }
1417 const char bytes[sizeof(unsigned)];1417 char bytes[sizeof(unsigned)];
1418 unsigned seed;1418 size_t amt_read;
1419 while (read(fd, bytes, sizeof(unsigned)) == -1) {1419 while ((amt_read = read(fd, bytes, sizeof(unsigned))) == -1) {
1420 if (errno == EINTR) continue;1420 if (errno == EINTR) continue;
1421 zig_panic("unable to read /dev/urandom");1421 zig_panic("unable to read /dev/urandom");
1422 }1422 }
1423 if (amt_read != sizeof(unsigned)) {
1424 zig_panic("unable to read enough bytes from /dev/urandom");
1425 }
1423 close(fd);1426 close(fd);
1427 unsigned seed;
1424 memcpy(&seed, bytes, sizeof(unsigned));1428 memcpy(&seed, bytes, sizeof(unsigned));
1425 srand(seed);1429 srand(seed);
1426#endif1430#endif