authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2019-11-02 17:01:43-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-02 18:30:07-04:00
log10046f9a52c089c656bfd4578e4ef17671ff1f75
tree4b63ae0994061631a051a18895da03aa28a80ae7
parent2e0dd5733f9aec168f72167ca0a0e306b2810ae2

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

3 files changed, 30 insertions(+), 19 deletions(-)

src/compiler.cpp+4-4
......@@ -4,7 +4,7 @@
44#include <stdio.h>
55
66static Buf saved_compiler_id = BUF_INIT;
7static Buf saved_app_data_dir = BUF_INIT;
7static Buf saved_cache_dir = BUF_INIT;
88static Buf saved_stage1_path = BUF_INIT;
99static Buf saved_lib_dir = BUF_INIT;
1010static Buf saved_special_dir = BUF_INIT;
......@@ -21,11 +21,11 @@ Buf *get_stage1_cache_path(void) {
2121 return &saved_stage1_path;
2222 }
2323 Error err;
24 if ((err = os_get_app_data_dir(&saved_app_data_dir, "zig"))) {
25 fprintf(stderr, "Unable to get app data dir: %s\n", err_str(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));
2626 exit(1);
2727 }
28 os_path_join(&saved_app_data_dir, buf_create_from_str("stage1"), &saved_stage1_path);
28 os_path_join(&saved_cache_dir, buf_create_from_str("stage1"), &saved_stage1_path);
2929 return &saved_stage1_path;
3030}
3131
src/os.cpp+25-14
......@@ -1748,7 +1748,7 @@ static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le) {
17481748#endif
17491749
17501750// Ported from std.os.getAppDataDir
1751Error os_get_app_data_dir(Buf *out_path, const char *appname) {
1751Error os_get_cache_dir(Buf *out_path, const char *appname) {
17521752#if defined(ZIG_OS_WINDOWS)
17531753 WCHAR *dir_path_ptr;
17541754 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) {
17741774 buf_appendf(out_path, "%s/Library/Application Support/%s", home_dir, appname);
17751775 return ErrorNone;
17761776#elif defined(ZIG_OS_POSIX)
1777 const char *home_dir = getenv("HOME");
1778 if (home_dir == nullptr) {
1779 // TODO use /etc/passwd
1780 return ErrorFileNotFound;
1781 }
1782 if (home_dir[0] == 0) {
1783 return ErrorFileNotFound;
1784 }
1785 buf_init_from_str(out_path, home_dir);
1786 if (buf_ptr(out_path)[buf_len(out_path) - 1] != '/') {
1787 buf_append_char(out_path, '/');
1777 const char *cache_dir = getenv("XDG_CACHE_HOME");
1778 if (cache_dir == nullptr) {
1779 cache_dir = getenv("HOME");
1780 if (cache_dir == nullptr) {
1781 // TODO use /etc/passwd
1782 return ErrorFileNotFound;
1783 }
1784 if (cache_dir[0] == 0) {
1785 return ErrorFileNotFound;
1786 }
1787 buf_init_from_str(out_path, cache_dir);
1788 if (buf_ptr(out_path)[buf_len(out_path) - 1] != '/') {
1789 buf_append_char(out_path, '/');
1790 }
1791 buf_appendf(out_path, ".cache/%s", appname);
1792 } else {
1793 if (cache_dir[0] == 0) {
1794 return ErrorFileNotFound;
1795 }
1796 buf_init_from_str(out_path, cache_dir);
1797 if (buf_ptr(out_path)[buf_len(out_path) - 1] != '/') {
1798 buf_append_char(out_path, '/');
1799 }
1800 buf_appendf(out_path, "%s", appname);
17881801 }
1789 buf_appendf(out_path, ".local/share/%s", appname);
17901802 return ErrorNone;
17911803#endif
17921804}
17931805
1794
17951806#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) || defined(ZIG_OS_DRAGONFLY)
17961807static int self_exe_shared_libs_callback(struct dl_phdr_info *info, size_t size, void *data) {
17971808 ZigList<Buf *> *libs = reinterpret_cast< ZigList<Buf *> *>(data);
src/os.hpp+1-1
......@@ -150,7 +150,7 @@ bool os_is_sep(uint8_t c);
150150
151151Error ATTRIBUTE_MUST_USE os_self_exe_path(Buf *out_path);
152152
153Error ATTRIBUTE_MUST_USE os_get_app_data_dir(Buf *out_path, const char *appname);
153Error ATTRIBUTE_MUST_USE os_get_cache_dir(Buf *out_path, const char *appname);
154154
155155Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf *output_buf);
156156Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);