diff --git a/src/compiler.cpp b/src/compiler.cpp index 50be7416b2..25bf124287 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -4,7 +4,7 @@ #include static Buf saved_compiler_id = BUF_INIT; -static Buf saved_app_data_dir = 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; @@ -21,11 +21,11 @@ Buf *get_stage1_cache_path(void) { return &saved_stage1_path; } Error err; - if ((err = os_get_app_data_dir(&saved_app_data_dir, "zig"))) { - fprintf(stderr, "Unable to get app data dir: %s\n", err_str(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_app_data_dir, buf_create_from_str("stage1"), &saved_stage1_path); + os_path_join(&saved_cache_dir, buf_create_from_str("stage1"), &saved_stage1_path); return &saved_stage1_path; } diff --git a/src/os.cpp b/src/os.cpp index 908c3b47d9..da1f4ecfb0 100644 --- a/src/os.cpp +++ b/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_app_data_dir(Buf *out_path, const char *appname) { +Error os_get_cache_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)) { @@ -1774,24 +1774,35 @@ Error os_get_app_data_dir(Buf *out_path, const char *appname) { buf_appendf(out_path, "%s/Library/Application Support/%s", home_dir, appname); return ErrorNone; #elif defined(ZIG_OS_POSIX) - const char *home_dir = getenv("HOME"); - if (home_dir == nullptr) { - // TODO use /etc/passwd - return ErrorFileNotFound; + const char *cache_dir = getenv("XDG_CACHE_HOME"); + if (cache_dir == nullptr) { + cache_dir = getenv("HOME"); + if (cache_dir == nullptr) { + // TODO use /etc/passwd + return ErrorFileNotFound; + } + if (cache_dir[0] == 0) { + return ErrorFileNotFound; + } + buf_init_from_str(out_path, cache_dir); + if (buf_ptr(out_path)[buf_len(out_path) - 1] != '/') { + buf_append_char(out_path, '/'); + } + buf_appendf(out_path, ".cache/%s", appname); + } else { + if (cache_dir[0] == 0) { + return ErrorFileNotFound; + } + buf_init_from_str(out_path, cache_dir); + if (buf_ptr(out_path)[buf_len(out_path) - 1] != '/') { + buf_append_char(out_path, '/'); + } + buf_appendf(out_path, "%s", appname); } - if (home_dir[0] == 0) { - return ErrorFileNotFound; - } - buf_init_from_str(out_path, home_dir); - if (buf_ptr(out_path)[buf_len(out_path) - 1] != '/') { - buf_append_char(out_path, '/'); - } - buf_appendf(out_path, ".local/share/%s", appname); return ErrorNone; #endif } - #if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) || defined(ZIG_OS_DRAGONFLY) static int self_exe_shared_libs_callback(struct dl_phdr_info *info, size_t size, void *data) { ZigList *libs = reinterpret_cast< ZigList *>(data); diff --git a/src/os.hpp b/src/os.hpp index 7da846a509..ac8c559461 100644 --- a/src/os.hpp +++ b/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_app_data_dir(Buf *out_path, const char *appname); +Error ATTRIBUTE_MUST_USE os_get_cache_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);