Commit 0a41051955

Michael Dusan <michael.dusan@gmail.com>
2020-01-15 13:34:53
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
1 parent 8d9d4a0
src/codegen.cpp
@@ -8616,7 +8616,7 @@ static Error define_builtin_compile_vars(CodeGen *g) {
     Error err;
 
     Buf *manifest_dir = buf_alloc();
-    os_path_join(get_stage1_cache_path(), buf_create_from_str("builtin"), manifest_dir);
+    os_path_join(get_global_cache_dir(), buf_create_from_str("builtin"), manifest_dir);
 
     CacheHash cache_hash;
     cache_init(&cache_hash, manifest_dir);
@@ -8940,10 +8940,50 @@ static void detect_libc(CodeGen *g) {
     if (g->zig_target->is_native) {
         g->libc = allocate<ZigLibCInstallation>(1);
 
-        // Look for zig-cache/native_libc.txt
-        Buf *native_libc_txt = buf_alloc();
-        os_path_join(g->cache_dir, buf_create_from_str("native_libc.txt"), native_libc_txt);
-        if ((err = zig_libc_parse(g->libc, native_libc_txt, g->zig_target, false))) {
+        // search for native_libc.txt in following dirs:
+        //   - LOCAL_CACHE_DIR
+        //   - GLOBAL_CACHE_DIR
+        // if not found create at:
+        //   - GLOBAL_CACHE_DIR
+        // be mindful local/global caches may be the same dir
+
+        Buf basename = BUF_INIT;
+        buf_init_from_str(&basename, "native_libc.txt");
+
+        Buf local_libc_txt = BUF_INIT;
+        os_path_join(g->cache_dir, &basename, &local_libc_txt);
+
+        Buf global_libc_txt = BUF_INIT;
+        os_path_join(get_global_cache_dir(), &basename, &global_libc_txt);
+
+        Buf *pathnames[3] = { nullptr };
+        size_t pathnames_idx = 0;
+
+        pathnames[pathnames_idx] = &local_libc_txt;
+        pathnames_idx += 1;
+
+        if (!buf_eql_buf(pathnames[0], &global_libc_txt)) {
+            pathnames[pathnames_idx] = &global_libc_txt;
+            pathnames_idx += 1;
+        }
+
+        Buf* libc_txt = nullptr;
+        for (auto name : pathnames) {
+            if (name == nullptr)
+                break;
+
+            bool result;
+            if (os_file_exists(name, &result) != ErrorNone || !result)
+                continue;
+
+            libc_txt = name;
+            break;
+        }
+
+        if (libc_txt == nullptr)
+            libc_txt = &global_libc_txt;
+
+        if ((err = zig_libc_parse(g->libc, libc_txt, g->zig_target, false))) {
             if ((err = zig_libc_find_native(g->libc, true))) {
                 fprintf(stderr,
                     "Unable to link against libc: Unable to find libc installation: %s\n"
@@ -8955,7 +8995,7 @@ static void detect_libc(CodeGen *g) {
                     buf_ptr(g->cache_dir), err_str(err));
                 exit(1);
             }
-            Buf *native_libc_tmp = buf_sprintf("%s.tmp", buf_ptr(native_libc_txt));
+            Buf *native_libc_tmp = buf_sprintf("%s.tmp", buf_ptr(libc_txt));
             FILE *file = fopen(buf_ptr(native_libc_tmp), "wb");
             if (file == nullptr) {
                 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) {
                 fprintf(stderr, "Unable to save %s: %s\n", buf_ptr(native_libc_tmp), strerror(errno));
                 exit(1);
             }
-            if ((err = os_rename(native_libc_tmp, native_libc_txt))) {
-                fprintf(stderr, "Unable to create %s: %s\n", buf_ptr(native_libc_txt), err_str(err));
+            if ((err = os_rename(native_libc_tmp, libc_txt))) {
+                fprintf(stderr, "Unable to create %s: %s\n", buf_ptr(libc_txt), err_str(err));
                 exit(1);
             }
         }
@@ -8995,6 +9035,10 @@ static void detect_libc(CodeGen *g) {
             g->libc_include_dir_len += 1;
         }
         assert(g->libc_include_dir_len == dir_count);
+
+        buf_deinit(&global_libc_txt);
+        buf_deinit(&local_libc_txt);
+        buf_deinit(&basename);
     } else if ((g->out_type == OutTypeExe || (g->out_type == OutTypeLib && g->is_dynamic)) &&
         !target_os_is_darwin(g->zig_target->os))
     {
@@ -10611,7 +10655,7 @@ CodeGen *create_child_codegen(CodeGen *parent_gen, Buf *root_src_path, OutType o
             name, strlen(name), 0);
 
     CodeGen *child_gen = codegen_create(nullptr, root_src_path, parent_gen->zig_target, out_type,
-        parent_gen->build_mode, parent_gen->zig_lib_dir, libc, get_stage1_cache_path(), false, child_progress_node);
+        parent_gen->build_mode, parent_gen->zig_lib_dir, libc, get_global_cache_dir(), false, child_progress_node);
     child_gen->root_out_name = buf_create_from_str(name);
     child_gen->disable_gen_h = true;
     child_gen->want_stack_check = WantStackCheckDisabled;
src/compiler.cpp
@@ -1,34 +1,12 @@
 #include "cache_hash.hpp"
 #include "os.hpp"
+#include "compiler.hpp"
 
 #include <stdio.h>
 
-static Buf saved_compiler_id = BUF_INIT;
-static Buf saved_cache_dir = BUF_INIT;
-static Buf saved_stage1_path = BUF_INIT;
-static Buf saved_lib_dir = BUF_INIT;
-static Buf saved_special_dir = BUF_INIT;
-static Buf saved_std_dir = BUF_INIT;
-
 static Buf saved_dynamic_linker_path = BUF_INIT;
 static bool searched_for_dyn_linker = false;
 
-static Buf saved_libc_path = BUF_INIT;
-static bool searched_for_libc = false;
-
-Buf *get_stage1_cache_path(void) {
-    if (saved_stage1_path.list.length != 0) {
-        return &saved_stage1_path;
-    }
-    Error err;
-    if ((err = os_get_cache_dir(&saved_cache_dir, "zig"))) {
-        fprintf(stderr, "Unable to get cache dir: %s\n", err_str(err));
-        exit(1);
-    }
-    os_path_join(&saved_cache_dir, buf_create_from_str("stage1"), &saved_stage1_path);
-    return &saved_stage1_path;
-}
-
 static void detect_dynamic_linker(Buf *lib_path) {
 #if defined(ZIG_OS_LINUX)
     for (size_t i = 0; possible_ld_names[i] != NULL; i += 1) {
@@ -40,7 +18,10 @@ static void detect_dynamic_linker(Buf *lib_path) {
 #endif
 }
 
-const Buf *get_self_libc_path(void) {
+Buf *get_self_libc_path(void) {
+    static Buf saved_libc_path = BUF_INIT;
+    static bool searched_for_libc = false;
+
     for (;;) {
         if (saved_libc_path.list.length != 0) {
             return &saved_libc_path;
@@ -82,15 +63,16 @@ Buf *get_self_dynamic_linker_path(void) {
 }
 
 Error get_compiler_id(Buf **result) {
+    static Buf saved_compiler_id = BUF_INIT;
+
     if (saved_compiler_id.list.length != 0) {
         *result = &saved_compiler_id;
         return ErrorNone;
     }
 
     Error err;
-    Buf *stage1_dir = get_stage1_cache_path();
     Buf *manifest_dir = buf_alloc();
-    os_path_join(stage1_dir, buf_create_from_str("exe"), manifest_dir);
+    os_path_join(get_global_cache_dir(), buf_create_from_str("exe"), manifest_dir);
 
     CacheHash cache_hash;
     CacheHash *ch = &cache_hash;
@@ -190,9 +172,9 @@ static int find_zig_lib_dir(Buf *out_path) {
 }
 
 Buf *get_zig_lib_dir(void) {
-    if (saved_lib_dir.list.length != 0) {
+    static Buf saved_lib_dir = BUF_INIT;
+    if (saved_lib_dir.list.length != 0)
         return &saved_lib_dir;
-    }
     buf_resize(&saved_lib_dir, 0);
 
     int err;
@@ -204,9 +186,9 @@ Buf *get_zig_lib_dir(void) {
 }
 
 Buf *get_zig_std_dir(Buf *zig_lib_dir) {
-    if (saved_std_dir.list.length != 0) {
+    static Buf saved_std_dir = BUF_INIT;
+    if (saved_std_dir.list.length != 0)
         return &saved_std_dir;
-    }
     buf_resize(&saved_std_dir, 0);
 
     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) {
 }
 
 Buf *get_zig_special_dir(Buf *zig_lib_dir) {
-    if (saved_special_dir.list.length != 0) {
+    static Buf saved_special_dir = BUF_INIT;
+    if (saved_special_dir.list.length != 0)
         return &saved_special_dir;
-    }
     buf_resize(&saved_special_dir, 0);
 
     os_path_join(get_zig_std_dir(zig_lib_dir), buf_sprintf("special"), &saved_special_dir);
 
     return &saved_special_dir;
 }
+
+Buf *get_global_cache_dir(void) {
+    static Buf saved_global_cache_dir = BUF_INIT;
+    if (saved_global_cache_dir.list.length != 0)
+        return &saved_global_cache_dir;
+    buf_resize(&saved_global_cache_dir, 0);
+
+    Buf app_data_dir = BUF_INIT;
+    Error err;
+    if ((err = os_get_app_data_dir(&app_data_dir, "zig"))) {
+        fprintf(stderr, "Unable to get application data dir: %s\n", err_str(err));
+        exit(1);
+    }
+    os_path_join(&app_data_dir, buf_create_from_str("stage1"), &saved_global_cache_dir);
+    buf_deinit(&app_data_dir);
+    return &saved_global_cache_dir;
+}
src/compiler.hpp
@@ -11,7 +11,6 @@
 #include "buffer.hpp"
 #include "error.hpp"
 
-Buf *get_stage1_cache_path(void);
 Error get_compiler_id(Buf **result);
 Buf *get_self_dynamic_linker_path(void);
 Buf *get_self_libc_path(void);
@@ -20,4 +19,6 @@ Buf *get_zig_lib_dir(void);
 Buf *get_zig_special_dir(Buf *zig_lib_dir);
 Buf *get_zig_std_dir(Buf *zig_lib_dir);
 
+Buf *get_global_cache_dir(void);
+
 #endif
src/glibc.cpp
@@ -173,7 +173,7 @@ Error glibc_build_dummies_and_maps(CodeGen *g, const ZigGLibCAbi *glibc_abi, con
 {
     Error err;
 
-    Buf *cache_dir = get_stage1_cache_path();
+    Buf *cache_dir = get_global_cache_dir();
     CacheHash *cache_hash = allocate<CacheHash>(1);
     Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(cache_dir));
     cache_init(cache_hash, manifest_dir);
src/link.cpp
@@ -1962,7 +1962,7 @@ static const char *get_def_lib(CodeGen *parent, const char *name, Buf *def_in_fi
         exit(1);
     }
 
-    Buf *cache_dir = get_stage1_cache_path();
+    Buf *cache_dir = get_global_cache_dir();
     Buf *o_dir = buf_sprintf("%s" OS_SEP CACHE_OUT_SUBDIR, buf_ptr(cache_dir));
     Buf *manifest_dir = buf_sprintf("%s" OS_SEP CACHE_HASH_SUBDIR, buf_ptr(cache_dir));
 
src/main.cpp
@@ -1184,7 +1184,7 @@ int main(int argc, char **argv) {
             Buf *cache_dir_buf;
             if (cache_dir == nullptr) {
                 if (cmd == CmdRun) {
-                    cache_dir_buf = get_stage1_cache_path();
+                    cache_dir_buf = get_global_cache_dir();
                 } else {
                     cache_dir_buf = buf_create_from_str(default_zig_cache_name);
                 }
src/os.cpp
@@ -1748,7 +1748,7 @@ static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le) {
 #endif
 
 // Ported from std.os.getAppDataDir
-Error os_get_cache_dir(Buf *out_path, const char *appname) {
+Error os_get_app_data_dir(Buf *out_path, const char *appname) {
 #if defined(ZIG_OS_WINDOWS)
     WCHAR *dir_path_ptr;
     switch (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &dir_path_ptr)) {
src/os.hpp
@@ -150,7 +150,7 @@ bool os_is_sep(uint8_t c);
 
 Error ATTRIBUTE_MUST_USE os_self_exe_path(Buf *out_path);
 
-Error ATTRIBUTE_MUST_USE os_get_cache_dir(Buf *out_path, const char *appname);
+Error ATTRIBUTE_MUST_USE os_get_app_data_dir(Buf *out_path, const char *appname);
 
 Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf *output_buf);
 Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);