stage1: add linux XDG Base Directory support

- define zig global cache based on XDG spec:

    if env XDG_CACHE_HOME {
        "$XDG_CACHE_HOME/zig"
    } else {
        "$HOME/.cache/zig"
    }

- old definition "$HOME/.local/share/zig" is retired
- closes #3573
This commit is contained in:
Michael Dusan 2019-11-02 17:01:43 -04:00 committed by Andrew Kelley
parent 2e0dd5733f
commit 10046f9a52
3 changed files with 30 additions and 19 deletions

View File

@ -4,7 +4,7 @@
#include <stdio.h>
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;
}

View File

@ -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<Buf *> *libs = reinterpret_cast< ZigList<Buf *> *>(data);

View File

@ -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);