authorgravatar for michael.larouche@gmail.comMichaël Larouche <michael.larouche@gmail.com> 2020-04-04 18:41:16-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-04-05 11:01:35-04:00
logc4a5f519f22511ce90bfe0aca0690b24cc592f88
treec5b74949cb47f84d3357bbe4088d05f4b07911b2
parentcd20e0cc672249555709e1b58a415ddd50b03ad9

Do not parse native_libc.txt anymore when linking on native target, always run detection of libc.

Fixes #4772

1 files changed, 5 insertions(+), 76 deletions(-)

src/codegen.cpp+5-76
......@@ -9057,80 +9057,13 @@ static void detect_libc(CodeGen *g) {
90579057 if (g->zig_target->is_native_os) {
90589058 g->libc = heap::c_allocator.create<Stage2LibCInstallation>();
90599059
9060 // search for native_libc.txt in following dirs:
9061 // - LOCAL_CACHE_DIR
9062 // - GLOBAL_CACHE_DIR
9063 // if not found create at:
9064 // - GLOBAL_CACHE_DIR
9065 // be mindful local/global caches may be the same dir
9066
9067 Buf basename = BUF_INIT;
9068 buf_init_from_str(&basename, "native_libc.txt");
9069
9070 Buf local_libc_txt = BUF_INIT;
9071 os_path_join(g->cache_dir, &basename, &local_libc_txt);
9072
9073 Buf global_libc_txt = BUF_INIT;
9074 os_path_join(get_global_cache_dir(), &basename, &global_libc_txt);
9075
9076 Buf *pathnames[3] = { nullptr };
9077 size_t pathnames_idx = 0;
9078
9079 pathnames[pathnames_idx] = &local_libc_txt;
9080 pathnames_idx += 1;
9081
9082 if (!buf_eql_buf(pathnames[0], &global_libc_txt)) {
9083 pathnames[pathnames_idx] = &global_libc_txt;
9084 pathnames_idx += 1;
9085 }
9086
9087 Buf* libc_txt = nullptr;
9088 for (auto name : pathnames) {
9089 if (name == nullptr)
9090 break;
9091
9092 bool result;
9093 if (os_file_exists(name, &result) != ErrorNone || !result)
9094 continue;
9095
9096 libc_txt = name;
9097 break;
9060 if ((err = stage2_libc_find_native(g->libc))) {
9061 fprintf(stderr,
9062 "Unable to link against libc: Unable to find libc installation: %s\n"
9063 "See `zig libc --help` for more details.\n", err_str(err));
9064 exit(1);
90989065 }
90999066
9100 if (libc_txt == nullptr)
9101 libc_txt = &global_libc_txt;
9102
9103 if ((err = stage2_libc_parse(g->libc, buf_ptr(libc_txt)))) {
9104 if ((err = stage2_libc_find_native(g->libc))) {
9105 fprintf(stderr,
9106 "Unable to link against libc: Unable to find libc installation: %s\n"
9107 "See `zig libc --help` for more details.\n", err_str(err));
9108 exit(1);
9109 }
9110 Buf libc_txt_dir = BUF_INIT;
9111 os_path_dirname(libc_txt, &libc_txt_dir);
9112 buf_deinit(&libc_txt_dir);
9113 if ((err = os_make_path(&libc_txt_dir))) {
9114 fprintf(stderr, "Unable to create %s directory: %s\n",
9115 buf_ptr(g->cache_dir), err_str(err));
9116 exit(1);
9117 }
9118 Buf *native_libc_tmp = buf_sprintf("%s.tmp", buf_ptr(libc_txt));
9119 FILE *file = fopen(buf_ptr(native_libc_tmp), "wb");
9120 if (file == nullptr) {
9121 fprintf(stderr, "Unable to open %s: %s\n", buf_ptr(native_libc_tmp), strerror(errno));
9122 exit(1);
9123 }
9124 stage2_libc_render(g->libc, file);
9125 if (fclose(file) != 0) {
9126 fprintf(stderr, "Unable to save %s: %s\n", buf_ptr(native_libc_tmp), strerror(errno));
9127 exit(1);
9128 }
9129 if ((err = os_rename(native_libc_tmp, libc_txt))) {
9130 fprintf(stderr, "Unable to create %s: %s\n", buf_ptr(libc_txt), err_str(err));
9131 exit(1);
9132 }
9133 }
91349067 bool want_sys_dir = !mem_eql_mem(g->libc->include_dir, g->libc->include_dir_len,
91359068 g->libc->sys_include_dir, g->libc->sys_include_dir_len);
91369069 size_t want_um_and_shared_dirs = (g->zig_target->os == OsWindows) ? 2 : 0;
......@@ -9164,10 +9097,6 @@ static void detect_libc(CodeGen *g) {
91649097 g->libc_include_dir_len += 1;
91659098 }
91669099 assert(g->libc_include_dir_len == dir_count);
9167
9168 buf_deinit(&global_libc_txt);
9169 buf_deinit(&local_libc_txt);
9170 buf_deinit(&basename);
91719100 } else if ((g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) &&
91729101 !target_os_is_darwin(g->zig_target->os))
91739102 {