authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2020-01-15 07:34:53-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-01-15 13:42:12-05:00
log0a410519559788598fcf698013c1b6389d80c67c
tree958520d6a5ce9908722d508edb7768e4e3a489fd
parent8d9d4a065820bfce0395465834cb9b7e01509a12

stage1: move local native_libc.txt to global

Automatic creation of `native_libc.txt` now occurs only in global cache. Manual creation/placement into local cache is supported. closes #3975

8 files changed, 91 insertions(+), 47 deletions(-)

src/codegen.cpp+53-9
......@@ -8616,7 +8616,7 @@ static Error define_builtin_compile_vars(CodeGen *g) {
86168616 Error err;
86178617
86188618 Buf *manifest_dir = buf_alloc();
8619 os_path_join(get_stage1_cache_path(), buf_create_from_str("builtin"), manifest_dir);
8619 os_path_join(get_global_cache_dir(), buf_create_from_str("builtin"), manifest_dir);
86208620
86218621 CacheHash cache_hash;
86228622 cache_init(&cache_hash, manifest_dir);
......@@ -8940,10 +8940,50 @@ static void detect_libc(CodeGen *g) {
89408940 if (g->zig_target->is_native) {
89418941 g->libc = allocate<ZigLibCInstallation>(1);
89428942
8943 // Look for zig-cache/native_libc.txt
8944 Buf *native_libc_txt = buf_alloc();
8945 os_path_join(g->cache_dir, buf_create_from_str("native_libc.txt"), native_libc_txt);
8946 if ((err = zig_libc_parse(g->libc, native_libc_txt, g->zig_target, false))) {
8943 // search for native_libc.txt in following dirs:
8944 // - LOCAL_CACHE_DIR
8945 // - GLOBAL_CACHE_DIR
8946 // if not found create at:
8947 // - GLOBAL_CACHE_DIR
8948 // be mindful local/global caches may be the same dir
8949
8950 Buf basename = BUF_INIT;
8951 buf_init_from_str(&basename, "native_libc.txt");
8952
8953 Buf local_libc_txt = BUF_INIT;
8954 os_path_join(g->cache_dir, &basename, &local_libc_txt);
8955
8956 Buf global_libc_txt = BUF_INIT;
8957 os_path_join(get_global_cache_dir(), &basename, &global_libc_txt);
8958
8959 Buf *pathnames[3] = { nullptr };
8960 size_t pathnames_idx = 0;
8961
8962 pathnames[pathnames_idx] = &local_libc_txt;
8963 pathnames_idx += 1;
8964
8965 if (!buf_eql_buf(pathnames[0], &global_libc_txt)) {
8966 pathnames[pathnames_idx] = &global_libc_txt;
8967 pathnames_idx += 1;
8968 }
8969
8970 Buf* libc_txt = nullptr;
8971 for (auto name : pathnames) {
8972 if (name == nullptr)
8973 break;
8974
8975 bool result;
8976 if (os_file_exists(name, &result) != ErrorNone || !result)
8977 continue;
8978
8979 libc_txt = name;
8980 break;
8981 }
8982
8983 if (libc_txt == nullptr)
8984 libc_txt = &global_libc_txt;
8985
8986 if ((err = zig_libc_parse(g->libc, libc_txt, g->zig_target, false))) {
89478987 if ((err = zig_libc_find_native(g->libc, true))) {
89488988 fprintf(stderr,
89498989 "Unable to link against libc: Unable to find libc installation: %s\n"
......@@ -8955,7 +8995,7 @@ static void detect_libc(CodeGen *g) {
89558995 buf_ptr(g->cache_dir), err_str(err));
89568996 exit(1);
89578997 }
8958 Buf *native_libc_tmp = buf_sprintf("%s.tmp", buf_ptr(native_libc_txt));
8998 Buf *native_libc_tmp = buf_sprintf("%s.tmp", buf_ptr(libc_txt));
89598999 FILE *file = fopen(buf_ptr(native_libc_tmp), "wb");
89609000 if (file == nullptr) {
89619001 fprintf(stderr, "Unable to open %s: %s\n", buf_ptr(native_libc_tmp), strerror(errno));
......@@ -8966,8 +9006,8 @@ static void detect_libc(CodeGen *g) {
89669006 fprintf(stderr, "Unable to save %s: %s\n", buf_ptr(native_libc_tmp), strerror(errno));
89679007 exit(1);
89689008 }
8969 if ((err = os_rename(native_libc_tmp, native_libc_txt))) {
8970 fprintf(stderr, "Unable to create %s: %s\n", buf_ptr(native_libc_txt), err_str(err));
9009 if ((err = os_rename(native_libc_tmp, libc_txt))) {
9010 fprintf(stderr, "Unable to create %s: %s\n", buf_ptr(libc_txt), err_str(err));
89719011 exit(1);
89729012 }
89739013 }
......@@ -8995,6 +9035,10 @@ static void detect_libc(CodeGen *g) {
89959035 g->libc_include_dir_len += 1;
89969036 }
89979037 assert(g->libc_include_dir_len == dir_count);
9038
9039 buf_deinit(&global_libc_txt);
9040 buf_deinit(&local_libc_txt);
9041 buf_deinit(&basename);
89989042 } else if ((g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) &&
89999043 !target_os_is_darwin(g->zig_target->os))
90009044 {
......@@ -10611,7 +10655,7 @@ CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType o
1061110655 name, strlen(name), 0);
1061210656
1061310657 CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type,
10614 parent_gen->build_mode, parent_gen->zig_lib_dir, libc, get_stage1_cache_path(), false, child_progress_node);
10658 parent_gen->build_mode, parent_gen->zig_lib_dir, libc, get_global_cache_dir(), false, child_progress_node);
1061510659 child_gen->root_out_name = buf_create_from_str(name);
1061610660 child_gen->disable_gen_h = true;
1061710661 child_gen->want_stack_check = WantStackCheckDisabled;
src/compiler.cpp+31-32
......@@ -1,34 +1,12 @@
11#include "cache_hash.hpp"
22#include "os.hpp"
3#include "compiler.hpp"
34
45#include <stdio.h>
56
6static Buf saved_compiler_id = BUF_INIT;
7static Buf saved_cache_dir = BUF_INIT;
8static Buf saved_stage1_path = BUF_INIT;
9static Buf saved_lib_dir = BUF_INIT;
10static Buf saved_special_dir = BUF_INIT;
11static Buf saved_std_dir = BUF_INIT;
12
137static Buf saved_dynamic_linker_path = BUF_INIT;
148static bool searched_for_dyn_linker = false;
159
16static Buf saved_libc_path = BUF_INIT;
17static bool searched_for_libc = false;
18
19Buf *get_stage1_cache_path(void) {
20 if (saved_stage1_path.list.length != 0) {
21 return &saved_stage1_path;
22 }
23 Error err;
24 if ((err = os_get_cache_dir(&saved_cache_dir, "zig"))) {
25 fprintf(stderr, "Unable to get cache dir: %s\n", err_str(err));
26 exit(1);
27 }
28 os_path_join(&saved_cache_dir, buf_create_from_str("stage1"), &saved_stage1_path);
29 return &saved_stage1_path;
30}
31
3210static void detect_dynamic_linker(Buf *lib_path) {
3311#if defined(ZIG_OS_LINUX)
3412 for (size_t i = 0; possible_ld_names[i] != NULL; i += 1) {
......@@ -40,7 +18,10 @@ static void detect_dynamic_linker(Buf *lib_path) {
4018#endif
4119}
4220
43const Buf *get_self_libc_path(void) {
21Buf *get_self_libc_path(void) {
22 static Buf saved_libc_path = BUF_INIT;
23 static bool searched_for_libc = false;
24
4425 for (;;) {
4526 if (saved_libc_path.list.length != 0) {
4627 return &saved_libc_path;
......@@ -82,15 +63,16 @@ Buf *get_self_dynamic_linker_path(void) {
8263}
8364
8465Error get_compiler_id(Buf **result) {
66 static Buf saved_compiler_id = BUF_INIT;
67
8568 if (saved_compiler_id.list.length != 0) {
8669 *result = &saved_compiler_id;
8770 return ErrorNone;
8871 }
8972
9073 Error err;
91 Buf *stage1_dir = get_stage1_cache_path();
9274 Buf *manifest_dir = buf_alloc();
93 os_path_join(stage1_dir, buf_create_from_str("exe"), manifest_dir);
75 os_path_join(get_global_cache_dir(), buf_create_from_str("exe"), manifest_dir);
9476
9577 CacheHash cache_hash;
9678 CacheHash *ch = &cache_hash;
......@@ -190,9 +172,9 @@ static int find_zig_lib_dir(Buf *out_path) {
190172}
191173
192174Buf *get_zig_lib_dir(void) {
193 if (saved_lib_dir.list.length != 0) {
175 static Buf saved_lib_dir = BUF_INIT;
176 if (saved_lib_dir.list.length != 0)
194177 return &saved_lib_dir;
195 }
196178 buf_resize(&saved_lib_dir, 0);
197179
198180 int err;
......@@ -204,9 +186,9 @@ Buf *get_zig_lib_dir(void) {
204186}
205187
206188Buf *get_zig_std_dir(Buf *zig_lib_dir) {
207 if (saved_std_dir.list.length != 0) {
189 static Buf saved_std_dir = BUF_INIT;
190 if (saved_std_dir.list.length != 0)
208191 return &saved_std_dir;
209 }
210192 buf_resize(&saved_std_dir, 0);
211193
212194 os_path_join(zig_lib_dir, buf_create_from_str("std"), &saved_std_dir);
......@@ -215,12 +197,29 @@ Buf *get_zig_std_dir(Buf *zig_lib_dir) {
215197}
216198
217199Buf *get_zig_special_dir(Buf *zig_lib_dir) {
218 if (saved_special_dir.list.length != 0) {
200 static Buf saved_special_dir = BUF_INIT;
201 if (saved_special_dir.list.length != 0)
219202 return &saved_special_dir;
220 }
221203 buf_resize(&saved_special_dir, 0);
222204
223205 os_path_join(get_zig_std_dir(zig_lib_dir), buf_sprintf("special"), &saved_special_dir);
224206
225207 return &saved_special_dir;
226208}
209
210Buf *get_global_cache_dir(void) {
211 static Buf saved_global_cache_dir = BUF_INIT;
212 if (saved_global_cache_dir.list.length != 0)
213 return &saved_global_cache_dir;
214 buf_resize(&saved_global_cache_dir, 0);
215
216 Buf app_data_dir = BUF_INIT;
217 Error err;
218 if ((err = os_get_app_data_dir(&app_data_dir, "zig"))) {
219 fprintf(stderr, "Unable to get application data dir: %s\n", err_str(err));
220 exit(1);
221 }
222 os_path_join(&app_data_dir, buf_create_from_str("stage1"), &saved_global_cache_dir);
223 buf_deinit(&app_data_dir);
224 return &saved_global_cache_dir;
225}
src/compiler.hpp+2-1
......@@ -11,7 +11,6 @@
1111#include "buffer.hpp"
1212#include "error.hpp"
1313
14Buf *get_stage1_cache_path(void);
1514Error get_compiler_id(Buf **result);
1615Buf *get_self_dynamic_linker_path(void);
1716Buf *get_self_libc_path(void);
......@@ -20,4 +19,6 @@ Buf *get_zig_lib_dir(void);
2019Buf *get_zig_special_dir(Buf *zig_lib_dir);
2120Buf *get_zig_std_dir(Buf *zig_lib_dir);
2221
22Buf *get_global_cache_dir(void);
23
2324#endif
src/glibc.cpp+1-1
......@@ -173,7 +173,7 @@ Error glibc_build_dummies_and_maps(CodeGen *g, const ZigGLibCAbi *glibc_abi, con
173173{
174174 Error err;
175175
176 Buf *cache_dir = get_stage1_cache_path();
176 Buf *cache_dir = get_global_cache_dir();
177177 CacheHash *cache_hash = allocate<CacheHash>(1);
178178 Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(cache_dir));
179179 cache_init(cache_hash, manifest_dir);
src/link.cpp+1-1
......@@ -1962,7 +1962,7 @@ static const char *get_def_lib(CodeGen *parent, const char *name, Buf *def_in_fi
19621962 exit(1);
19631963 }
19641964
1965 Buf *cache_dir = get_stage1_cache_path();
1965 Buf *cache_dir = get_global_cache_dir();
19661966 Buf *o_dir = buf_sprintf("%s" OS_SEP CACHE_OUT_SUBDIR, buf_ptr(cache_dir));
19671967 Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(cache_dir));
19681968
src/main.cpp+1-1
......@@ -1184,7 +1184,7 @@ int main(int argc, char **argv) {
11841184 Buf *cache_dir_buf;
11851185 if (cache_dir == nullptr) {
11861186 if (cmd == CmdRun) {
1187 cache_dir_buf = get_stage1_cache_path();
1187 cache_dir_buf = get_global_cache_dir();
11881188 } else {
11891189 cache_dir_buf = buf_create_from_str(default_zig_cache_name);
11901190 }
src/os.cpp+1-1
......@@ -1748,7 +1748,7 @@ static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le) {
17481748#endif
17491749
17501750// Ported from std.os.getAppDataDir
1751Error os_get_cache_dir(Buf *out_path, const char *appname) {
1751Error os_get_app_data_dir(Buf *out_path, const char *appname) {
17521752#if defined(ZIG_OS_WINDOWS)
17531753 WCHAR *dir_path_ptr;
17541754 switch (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &dir_path_ptr)) {
src/os.hpp+1-1
......@@ -150,7 +150,7 @@ bool os_is_sep(uint8_t c);
150150
151151Error ATTRIBUTE_MUST_USE os_self_exe_path(Buf *out_path);
152152
153Error ATTRIBUTE_MUST_USE os_get_cache_dir(Buf *out_path, const char *appname);
153Error ATTRIBUTE_MUST_USE os_get_app_data_dir(Buf *out_path, const char *appname);
154154
155155Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf *output_buf);
156156Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);