| author | |
| committer | |
| log | 0a410519559788598fcf698013c1b6389d80c67c |
| tree | 958520d6a5ce9908722d508edb7768e4e3a489fd |
| parent | 8d9d4a065820bfce0395465834cb9b7e01509a12 |
Automatic creation of `native_libc.txt` now occurs only in global
cache. Manual creation/placement into local cache is supported.
closes #39758 files changed, 91 insertions(+), 47 deletions(-)
src/codegen.cpp+53-9| ... | ... | @@ -8616,7 +8616,7 @@ static Error define_builtin_compile_vars(CodeGen *g) { |
| 8616 | 8616 | Error err; |
| 8617 | 8617 | |
| 8618 | 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); | |
| 8620 | 8620 | |
| 8621 | 8621 | CacheHash cache_hash; |
| 8622 | 8622 | cache_init(&cache_hash, manifest_dir); |
| ... | ... | @@ -8940,10 +8940,50 @@ static void detect_libc(CodeGen *g) { |
| 8940 | 8940 | if (g->zig_target->is_native) { |
| 8941 | 8941 | g->libc = allocate<ZigLibCInstallation>(1); |
| 8942 | 8942 | |
| 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))) { | |
| 8947 | 8987 | if ((err = zig_libc_find_native(g->libc, true))) { |
| 8948 | 8988 | fprintf(stderr, |
| 8949 | 8989 | "Unable to link against libc: Unable to find libc installation: %s\n" |
| ... | ... | @@ -8955,7 +8995,7 @@ static void detect_libc(CodeGen *g) { |
| 8955 | 8995 | buf_ptr(g->cache_dir), err_str(err)); |
| 8956 | 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 | 8999 | FILE *file = fopen(buf_ptr(native_libc_tmp), "wb"); |
| 8960 | 9000 | if (file == nullptr) { |
| 8961 | 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 | 9006 | fprintf(stderr, "Unable to save %s: %s\n", buf_ptr(native_libc_tmp), strerror(errno)); |
| 8967 | 9007 | exit(1); |
| 8968 | 9008 | } |
| 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)); | |
| 8971 | 9011 | exit(1); |
| 8972 | 9012 | } |
| 8973 | 9013 | } |
| ... | ... | @@ -8995,6 +9035,10 @@ static void detect_libc(CodeGen *g) { |
| 8995 | 9035 | g->libc_include_dir_len += 1; |
| 8996 | 9036 | } |
| 8997 | 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 | 9042 | } else if ((g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) && |
| 8999 | 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 | 10655 | name, strlen(name), 0); |
| 10612 | 10656 | |
| 10613 | 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 | 10659 | child_gen->root_out_name = buf_create_from_str(name); |
| 10616 | 10660 | child_gen->disable_gen_h = true; |
| 10617 | 10661 | child_gen->want_stack_check = WantStackCheckDisabled; |
src/compiler.cpp+31-32| ... | ... | @@ -1,34 +1,12 @@ |
| 1 | 1 | #include "cache_hash.hpp" |
| 2 | 2 | #include "os.hpp" |
| 3 | #include "compiler.hpp" | |
| 3 | 4 | |
| 4 | 5 | #include <stdio.h> |
| 5 | 6 | |
| 6 | static Buf saved_compiler_id = BUF_INIT; | |
| 7 | static Buf saved_cache_dir = BUF_INIT; | |
| 8 | static Buf saved_stage1_path = BUF_INIT; | |
| 9 | static Buf saved_lib_dir = BUF_INIT; | |
| 10 | static Buf saved_special_dir = BUF_INIT; | |
| 11 | static Buf saved_std_dir = BUF_INIT; | |
| 12 | ||
| 13 | 7 | static Buf saved_dynamic_linker_path = BUF_INIT; |
| 14 | 8 | static bool searched_for_dyn_linker = false; |
| 15 | 9 | |
| 16 | static Buf saved_libc_path = BUF_INIT; | |
| 17 | static bool searched_for_libc = false; | |
| 18 | ||
| 19 | Buf *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 | ||
| 32 | 10 | static void detect_dynamic_linker(Buf *lib_path) { |
| 33 | 11 | #if defined(ZIG_OS_LINUX) |
| 34 | 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 | 18 | #endif |
| 41 | 19 | } |
| 42 | 20 | |
| 43 | const Buf *get_self_libc_path(void) { | |
| 21 | Buf *get_self_libc_path(void) { | |
| 22 | static Buf saved_libc_path = BUF_INIT; | |
| 23 | static bool searched_for_libc = false; | |
| 24 | ||
| 44 | 25 | for (;;) { |
| 45 | 26 | if (saved_libc_path.list.length != 0) { |
| 46 | 27 | return &saved_libc_path; |
| ... | ... | @@ -82,15 +63,16 @@ Buf *get_self_dynamic_linker_path(void) { |
| 82 | 63 | } |
| 83 | 64 | |
| 84 | 65 | Error get_compiler_id(Buf **result) { |
| 66 | static Buf saved_compiler_id = BUF_INIT; | |
| 67 | ||
| 85 | 68 | if (saved_compiler_id.list.length != 0) { |
| 86 | 69 | *result = &saved_compiler_id; |
| 87 | 70 | return ErrorNone; |
| 88 | 71 | } |
| 89 | 72 | |
| 90 | 73 | Error err; |
| 91 | Buf *stage1_dir = get_stage1_cache_path(); | |
| 92 | 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); | |
| 94 | 76 | |
| 95 | 77 | CacheHash cache_hash; |
| 96 | 78 | CacheHash *ch = &cache_hash; |
| ... | ... | @@ -190,9 +172,9 @@ static int find_zig_lib_dir(Buf *out_path) { |
| 190 | 172 | } |
| 191 | 173 | |
| 192 | 174 | Buf *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 | 177 | return &saved_lib_dir; |
| 195 | } | |
| 196 | 178 | buf_resize(&saved_lib_dir, 0); |
| 197 | 179 | |
| 198 | 180 | int err; |
| ... | ... | @@ -204,9 +186,9 @@ Buf *get_zig_lib_dir(void) { |
| 204 | 186 | } |
| 205 | 187 | |
| 206 | 188 | Buf *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 | 191 | return &saved_std_dir; |
| 209 | } | |
| 210 | 192 | buf_resize(&saved_std_dir, 0); |
| 211 | 193 | |
| 212 | 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 | 197 | } |
| 216 | 198 | |
| 217 | 199 | Buf *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 | 202 | return &saved_special_dir; |
| 220 | } | |
| 221 | 203 | buf_resize(&saved_special_dir, 0); |
| 222 | 204 | |
| 223 | 205 | os_path_join(get_zig_std_dir(zig_lib_dir), buf_sprintf("special"), &saved_special_dir); |
| 224 | 206 | |
| 225 | 207 | return &saved_special_dir; |
| 226 | 208 | } |
| 209 | ||
| 210 | Buf *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 | 11 | #include "buffer.hpp" |
| 12 | 12 | #include "error.hpp" |
| 13 | 13 | |
| 14 | Buf *get_stage1_cache_path(void); | |
| 15 | 14 | Error get_compiler_id(Buf **result); |
| 16 | 15 | Buf *get_self_dynamic_linker_path(void); |
| 17 | 16 | Buf *get_self_libc_path(void); |
| ... | ... | @@ -20,4 +19,6 @@ Buf *get_zig_lib_dir(void); |
| 20 | 19 | Buf *get_zig_special_dir(Buf *zig_lib_dir); |
| 21 | 20 | Buf *get_zig_std_dir(Buf *zig_lib_dir); |
| 22 | 21 | |
| 22 | Buf *get_global_cache_dir(void); | |
| 23 | ||
| 23 | 24 | #endif |
src/glibc.cpp+1-1| ... | ... | @@ -173,7 +173,7 @@ Error glibc_build_dummies_and_maps(CodeGen *g, const ZigGLibCAbi *glibc_abi, con |
| 173 | 173 | { |
| 174 | 174 | Error err; |
| 175 | 175 | |
| 176 | Buf *cache_dir = get_stage1_cache_path(); | |
| 176 | Buf *cache_dir = get_global_cache_dir(); | |
| 177 | 177 | CacheHash *cache_hash = allocate<CacheHash>(1); |
| 178 | 178 | Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(cache_dir)); |
| 179 | 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 | 1962 | exit(1); |
| 1963 | 1963 | } |
| 1964 | 1964 | |
| 1965 | Buf *cache_dir = get_stage1_cache_path(); | |
| 1965 | Buf *cache_dir = get_global_cache_dir(); | |
| 1966 | 1966 | Buf *o_dir = buf_sprintf("%s" OS_SEP CACHE_OUT_SUBDIR, buf_ptr(cache_dir)); |
| 1967 | 1967 | Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(cache_dir)); |
| 1968 | 1968 |
src/main.cpp+1-1| ... | ... | @@ -1184,7 +1184,7 @@ int main(int argc, char **argv) { |
| 1184 | 1184 | Buf *cache_dir_buf; |
| 1185 | 1185 | if (cache_dir == nullptr) { |
| 1186 | 1186 | if (cmd == CmdRun) { |
| 1187 | cache_dir_buf = get_stage1_cache_path(); | |
| 1187 | cache_dir_buf = get_global_cache_dir(); | |
| 1188 | 1188 | } else { |
| 1189 | 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 | 1748 | #endif |
| 1749 | 1749 | |
| 1750 | 1750 | // Ported from std.os.getAppDataDir |
| 1751 | Error os_get_cache_dir(Buf *out_path, const char *appname) { | |
| 1751 | Error os_get_app_data_dir(Buf *out_path, const char *appname) { | |
| 1752 | 1752 | #if defined(ZIG_OS_WINDOWS) |
| 1753 | 1753 | WCHAR *dir_path_ptr; |
| 1754 | 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 | 150 | |
| 151 | 151 | Error ATTRIBUTE_MUST_USE os_self_exe_path(Buf *out_path); |
| 152 | 152 | |
| 153 | Error ATTRIBUTE_MUST_USE os_get_cache_dir(Buf *out_path, const char *appname); | |
| 153 | Error ATTRIBUTE_MUST_USE os_get_app_data_dir(Buf *out_path, const char *appname); | |
| 154 | 154 | |
| 155 | 155 | Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf *output_buf); |
| 156 | 156 | Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type); |