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 @@...@@ -4,7 +4,7 @@
4#include <stdio.h>4#include <stdio.h>
55
6static Buf saved_compiler_id = BUF_INIT;6static Buf saved_compiler_id = BUF_INIT;
7static Buf saved_app_data_dir = BUF_INIT;7static Buf saved_cache_dir = BUF_INIT;
8static Buf saved_stage1_path = BUF_INIT;8static Buf saved_stage1_path = BUF_INIT;
9static Buf saved_lib_dir = BUF_INIT;9static Buf saved_lib_dir = BUF_INIT;
10static Buf saved_special_dir = BUF_INIT;10static Buf saved_special_dir = BUF_INIT;
...@@ -21,11 +21,11 @@ Buf *get_stage1_cache_path(void) {...@@ -21,11 +21,11 @@ Buf *get_stage1_cache_path(void) {
21 return &saved_stage1_path;21 return &saved_stage1_path;
22 }22 }
23 Error err;23 Error err;
24 if ((err = os_get_app_data_dir(&saved_app_data_dir, "zig"))) {24 if ((err = os_get_cache_dir(&saved_cache_dir, "zig"))) {
25 fprintf(stderr, "Unable to get app data dir: %s\n", err_str(err));25 fprintf(stderr, "Unable to get cache dir: %s\n", err_str(err));
26 exit(1);26 exit(1);
27 }27 }
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);
29 return &saved_stage1_path;29 return &saved_stage1_path;
30}30}
3131
src/os.cpp+25-14
...@@ -1748,7 +1748,7 @@ static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le) {...@@ -1748,7 +1748,7 @@ static void utf16le_ptr_to_utf8(Buf *out, WCHAR *utf16le) {
1748#endif1748#endif
17491749
1750// Ported from std.os.getAppDataDir1750// 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) {
1752#if defined(ZIG_OS_WINDOWS)1752#if defined(ZIG_OS_WINDOWS)
1753 WCHAR *dir_path_ptr;1753 WCHAR *dir_path_ptr;
1754 switch (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_CREATE, nullptr, &dir_path_ptr)) {1754 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) {...@@ -1774,24 +1774,35 @@ Error os_get_app_data_dir(Buf *out_path, const char *appname) {
1774 buf_appendf(out_path, "%s/Library/Application Support/%s", home_dir, appname);1774 buf_appendf(out_path, "%s/Library/Application Support/%s", home_dir, appname);
1775 return ErrorNone;1775 return ErrorNone;
1776#elif defined(ZIG_OS_POSIX)1776#elif defined(ZIG_OS_POSIX)
1777 const char *home_dir = getenv("HOME");1777 const char *cache_dir = getenv("XDG_CACHE_HOME");
1778 if (home_dir == nullptr) {1778 if (cache_dir == nullptr) {
1779 // TODO use /etc/passwd1779 cache_dir = getenv("HOME");
1780 return ErrorFileNotFound;1780 if (cache_dir == nullptr) {
1781 }1781 // TODO use /etc/passwd
1782 if (home_dir[0] == 0) {1782 return ErrorFileNotFound;
1783 return ErrorFileNotFound;1783 }
1784 }1784 if (cache_dir[0] == 0) {
1785 buf_init_from_str(out_path, home_dir);1785 return ErrorFileNotFound;
1786 if (buf_ptr(out_path)[buf_len(out_path) - 1] != '/') {1786 }
1787 buf_append_char(out_path, '/');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);
1788 }1801 }
1789 buf_appendf(out_path, ".local/share/%s", appname);
1790 return ErrorNone;1802 return ErrorNone;
1791#endif1803#endif
1792}1804}
17931805
1794
1795#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) || defined(ZIG_OS_DRAGONFLY)1806#if defined(ZIG_OS_LINUX) || defined(ZIG_OS_FREEBSD) || defined(ZIG_OS_NETBSD) || defined(ZIG_OS_DRAGONFLY)
1796static int self_exe_shared_libs_callback(struct dl_phdr_info *info, size_t size, void *data) {1807static int self_exe_shared_libs_callback(struct dl_phdr_info *info, size_t size, void *data) {
1797 ZigList<Buf *> *libs = reinterpret_cast< ZigList<Buf *> *>(data);1808 ZigList<Buf *> *libs = reinterpret_cast< ZigList<Buf *> *>(data);
src/os.hpp+1-1
...@@ -150,7 +150,7 @@ bool os_is_sep(uint8_t c);...@@ -150,7 +150,7 @@ bool os_is_sep(uint8_t c);
150150
151Error ATTRIBUTE_MUST_USE os_self_exe_path(Buf *out_path);151Error 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
155Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf *output_buf);155Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf *output_buf);
156Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);156Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type);