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) {...@@ -8616,7 +8616,7 @@ static Error define_builtin_compile_vars(CodeGen *g) {
8616 Error err;8616 Error err;
86178617
8618 Buf *manifest_dir = buf_alloc();8618 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
8621 CacheHash cache_hash;8621 CacheHash cache_hash;
8622 cache_init(&cache_hash, manifest_dir);8622 cache_init(&cache_hash, manifest_dir);
...@@ -8940,10 +8940,50 @@ static void detect_libc(CodeGen *g) {...@@ -8940,10 +8940,50 @@ static void detect_libc(CodeGen *g) {
8940 if (g->zig_target->is_native) {8940 if (g->zig_target->is_native) {
8941 g->libc = allocate<ZigLibCInstallation>(1);8941 g->libc = allocate<ZigLibCInstallation>(1);
89428942
8943 // Look for zig-cache/native_libc.txt8943 // search for native_libc.txt in following dirs:
8944 Buf *native_libc_txt = buf_alloc();8944 // - LOCAL_CACHE_DIR
8945 os_path_join(g->cache_dir, buf_create_from_str("native_libc.txt"), native_libc_txt);8945 // - GLOBAL_CACHE_DIR
8946 if ((err = zig_libc_parse(g->libc, native_libc_txt, g->zig_target, false))) {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))) {
8947 if ((err = zig_libc_find_native(g->libc, true))) {8987 if ((err = zig_libc_find_native(g->libc, true))) {
8948 fprintf(stderr,8988 fprintf(stderr,
8949 "Unable to link against libc: Unable to find libc installation: %s\n"8989 "Unable to link against libc: Unable to find libc installation: %s\n"
...@@ -8955,7 +8995,7 @@ static void detect_libc(CodeGen *g) {...@@ -8955,7 +8995,7 @@ static void detect_libc(CodeGen *g) {
8955 buf_ptr(g->cache_dir), err_str(err));8995 buf_ptr(g->cache_dir), err_str(err));
8956 exit(1);8996 exit(1);
8957 }8997 }
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));
8959 FILE *file = fopen(buf_ptr(native_libc_tmp), "wb");8999 FILE *file = fopen(buf_ptr(native_libc_tmp), "wb");
8960 if (file == nullptr) {9000 if (file == nullptr) {
8961 fprintf(stderr, "Unable to open %s: %s\n", buf_ptr(native_libc_tmp), strerror(errno));9001 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) {...@@ -8966,8 +9006,8 @@ static void detect_libc(CodeGen *g) {
8966 fprintf(stderr, "Unable to save %s: %s\n", buf_ptr(native_libc_tmp), strerror(errno));9006 fprintf(stderr, "Unable to save %s: %s\n", buf_ptr(native_libc_tmp), strerror(errno));
8967 exit(1);9007 exit(1);
8968 }9008 }
8969 if ((err = os_rename(native_libc_tmp, native_libc_txt))) {9009 if ((err = os_rename(native_libc_tmp, libc_txt))) {
8970 fprintf(stderr, "Unable to create %s: %s\n", buf_ptr(native_libc_txt), err_str(err));9010 fprintf(stderr, "Unable to create %s: %s\n", buf_ptr(libc_txt), err_str(err));
8971 exit(1);9011 exit(1);
8972 }9012 }
8973 }9013 }
...@@ -8995,6 +9035,10 @@ static void detect_libc(CodeGen *g) {...@@ -8995,6 +9035,10 @@ static void detect_libc(CodeGen *g) {
8995 g->libc_include_dir_len += 1;9035 g->libc_include_dir_len += 1;
8996 }9036 }
8997 assert(g->libc_include_dir_len == dir_count);9037 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);
8998 } else if ((g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) &&9042 } else if ((g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) &&
8999 !target_os_is_darwin(g->zig_target->os))9043 !target_os_is_darwin(g->zig_target->os))
9000 {9044 {
...@@ -10611,7 +10655,7 @@ CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType o...@@ -10611,7 +10655,7 @@ CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType o
10611 name, strlen(name), 0);10655 name, strlen(name), 0);
1061210656
10613 CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type,10657 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);
10615 child_gen->root_out_name = buf_create_from_str(name);10659 child_gen->root_out_name = buf_create_from_str(name);
10616 child_gen->disable_gen_h = true;10660 child_gen->disable_gen_h = true;
10617 child_gen->want_stack_check = WantStackCheckDisabled;10661 child_gen->want_stack_check = WantStackCheckDisabled;
src/compiler.cpp+31-32
...@@ -1,34 +1,12 @@...@@ -1,34 +1,12 @@
1#include "cache_hash.hpp"1#include "cache_hash.hpp"
2#include "os.hpp"2#include "os.hpp"
3#include "compiler.hpp"
34
4#include <stdio.h>5#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
13static Buf saved_dynamic_linker_path = BUF_INIT;7static Buf saved_dynamic_linker_path = BUF_INIT;
14static bool searched_for_dyn_linker = false;8static 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
32static void detect_dynamic_linker(Buf *lib_path) {10static void detect_dynamic_linker(Buf *lib_path) {
33#if defined(ZIG_OS_LINUX)11#if defined(ZIG_OS_LINUX)
34 for (size_t i = 0; possible_ld_names[i] != NULL; i += 1) {12 for (size_t i = 0; possible_ld_names[i] != NULL; i += 1) {
...@@ -40,7 +18,10 @@ static void detect_dynamic_linker(Buf *lib_path) {...@@ -40,7 +18,10 @@ static void detect_dynamic_linker(Buf *lib_path) {
40#endif18#endif
41}19}
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
44 for (;;) {25 for (;;) {
45 if (saved_libc_path.list.length != 0) {26 if (saved_libc_path.list.length != 0) {
46 return &saved_libc_path;27 return &saved_libc_path;
...@@ -82,15 +63,16 @@ Buf *get_self_dynamic_linker_path(void) {...@@ -82,15 +63,16 @@ Buf *get_self_dynamic_linker_path(void) {
82}63}
8364
84Error get_compiler_id(Buf **result) {65Error get_compiler_id(Buf **result) {
66 static Buf saved_compiler_id = BUF_INIT;
67
85 if (saved_compiler_id.list.length != 0) {68 if (saved_compiler_id.list.length != 0) {
86 *result = &saved_compiler_id;69 *result = &saved_compiler_id;
87 return ErrorNone;70 return ErrorNone;
88 }71 }
8972
90 Error err;73 Error err;
91 Buf *stage1_dir = get_stage1_cache_path();
92 Buf *manifest_dir = buf_alloc();74 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
95 CacheHash cache_hash;77 CacheHash cache_hash;
96 CacheHash *ch = &cache_hash;78 CacheHash *ch = &cache_hash;
...@@ -190,9 +172,9 @@ static int find_zig_lib_dir(Buf *out_path) {...@@ -190,9 +172,9 @@ static int find_zig_lib_dir(Buf *out_path) {
190}172}
191173
192Buf *get_zig_lib_dir(void) {174Buf *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)
194 return &saved_lib_dir;177 return &saved_lib_dir;
195 }
196 buf_resize(&saved_lib_dir, 0);178 buf_resize(&saved_lib_dir, 0);
197179
198 int err;180 int err;
...@@ -204,9 +186,9 @@ Buf *get_zig_lib_dir(void) {...@@ -204,9 +186,9 @@ Buf *get_zig_lib_dir(void) {
204}186}
205187
206Buf *get_zig_std_dir(Buf *zig_lib_dir) {188Buf *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)
208 return &saved_std_dir;191 return &saved_std_dir;
209 }
210 buf_resize(&saved_std_dir, 0);192 buf_resize(&saved_std_dir, 0);
211193
212 os_path_join(zig_lib_dir, buf_create_from_str("std"), &saved_std_dir);194 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) {...@@ -215,12 +197,29 @@ Buf *get_zig_std_dir(Buf *zig_lib_dir) {
215}197}
216198
217Buf *get_zig_special_dir(Buf *zig_lib_dir) {199Buf *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)
219 return &saved_special_dir;202 return &saved_special_dir;
220 }
221 buf_resize(&saved_special_dir, 0);203 buf_resize(&saved_special_dir, 0);
222204
223 os_path_join(get_zig_std_dir(zig_lib_dir), buf_sprintf("special"), &saved_special_dir);205 os_path_join(get_zig_std_dir(zig_lib_dir), buf_sprintf("special"), &saved_special_dir);
224206
225 return &saved_special_dir;207 return &saved_special_dir;
226}208}
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 @@...@@ -11,7 +11,6 @@
11#include "buffer.hpp"11#include "buffer.hpp"
12#include "error.hpp"12#include "error.hpp"
1313
14Buf *get_stage1_cache_path(void);
15Error get_compiler_id(Buf **result);14Error get_compiler_id(Buf **result);
16Buf *get_self_dynamic_linker_path(void);15Buf *get_self_dynamic_linker_path(void);
17Buf *get_self_libc_path(void);16Buf *get_self_libc_path(void);
...@@ -20,4 +19,6 @@ Buf *get_zig_lib_dir(void);...@@ -20,4 +19,6 @@ Buf *get_zig_lib_dir(void);
20Buf *get_zig_special_dir(Buf *zig_lib_dir);19Buf *get_zig_special_dir(Buf *zig_lib_dir);
21Buf *get_zig_std_dir(Buf *zig_lib_dir);20Buf *get_zig_std_dir(Buf *zig_lib_dir);
2221
22Buf *get_global_cache_dir(void);
23
23#endif24#endif
src/glibc.cpp+1-1
...@@ -173,7 +173,7 @@ Error glibc_build_dummies_and_maps(CodeGen *g, const ZigGLibCAbi *glibc_abi, con...@@ -173,7 +173,7 @@ Error glibc_build_dummies_and_maps(CodeGen *g, const ZigGLibCAbi *glibc_abi, con
173{173{
174 Error err;174 Error err;
175175
176 Buf *cache_dir = get_stage1_cache_path();176 Buf *cache_dir = get_global_cache_dir();
177 CacheHash *cache_hash = allocate<CacheHash>(1);177 CacheHash *cache_hash = allocate<CacheHash>(1);
178 Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(cache_dir));178 Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(cache_dir));
179 cache_init(cache_hash, manifest_dir);179 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...@@ -1962,7 +1962,7 @@ static const char *get_def_lib(CodeGen *parent, const char *name, Buf *def_in_fi
1962 exit(1);1962 exit(1);
1963 }1963 }
19641964
1965 Buf *cache_dir = get_stage1_cache_path();1965 Buf *cache_dir = get_global_cache_dir();
1966 Buf *o_dir = buf_sprintf("%s" OS_SEP CACHE_OUT_SUBDIR, buf_ptr(cache_dir));1966 Buf *o_dir = buf_sprintf("%s" OS_SEP CACHE_OUT_SUBDIR, buf_ptr(cache_dir));
1967 Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(cache_dir));1967 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) {...@@ -1184,7 +1184,7 @@ int main(int argc, char **argv) {
1184 Buf *cache_dir_buf;1184 Buf *cache_dir_buf;
1185 if (cache_dir == nullptr) {1185 if (cache_dir == nullptr) {
1186 if (cmd == CmdRun) {1186 if (cmd == CmdRun) {
1187 cache_dir_buf = get_stage1_cache_path();1187 cache_dir_buf = get_global_cache_dir();
1188 } else {1188 } else {
1189 cache_dir_buf = buf_create_from_str(default_zig_cache_name);1189 cache_dir_buf = buf_create_from_str(default_zig_cache_name);
1190 }1190 }
src/os.cpp+1-1
...@@ -1748,7 +1748,7 @@ static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le) {...@@ -1748,7 +1748,7 @@ static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le) {
1748#endif1748#endif
17491749
1750// Ported from std.os.getAppDataDir1750// 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) {
1752#if defined(ZIG_OS_WINDOWS)1752#if defined(ZIG_OS_WINDOWS)
1753 WCHAR *dir_path_ptr;1753 WCHAR *dir_path_ptr;
1754 switch (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &dir_path_ptr)) {1754 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);...@@ -150,7 +150,7 @@ bool os_is_sep(uint8_t c);
150150
151Error ATTRIBUTE_MUST_USE os_self_exe_path(Buf *out_path);151Error 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
155Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf *output_buf);155Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf *output_buf);
156Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);156Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);