aboutsummaryrefslogtreecommitdiff
path: root/src/os.cpp
diff options
context:
space:
mode:
authorAndrea Orru <andrea@orru.io>2018-08-06 01:43:19 -0400
committerAndrea Orru <andrea@orru.io>2018-08-06 01:43:19 -0400
commitd2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d (patch)
treee9fa3caec533a0d1e2b434868b2fde1f9240e5c8 /src/os.cpp
parent06614b3fa09954464c2e2f32756cacedc178a282 (diff)
parent63a23e848a62d5f167f8d5478de9766cb24aa6eb (diff)
downloadzig-d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d.tar.gz
zig-d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d.zip
Merge branch 'master' into zen_stdlib
Diffstat (limited to 'src/os.cpp')
-rw-r--r--src/os.cpp275
1 files changed, 29 insertions, 246 deletions
diff --git a/src/os.cpp b/src/os.cpp
index e0491b21de..91a591a7b6 100644
--- a/src/os.cpp
+++ b/src/os.cpp
@@ -26,7 +26,6 @@
#include <windows.h>
#include <io.h>
#include <fcntl.h>
-#include "windows_com.hpp"
typedef SSIZE_T ssize_t;
#else
@@ -225,6 +224,11 @@ void os_path_extname(Buf *full_path, Buf *out_basename, Buf *out_extname) {
}
void os_path_join(Buf *dirname, Buf *basename, Buf *out_full_path) {
+ if (buf_len(dirname) == 0) {
+ buf_init_from_buf(out_full_path, basename);
+ return;
+ }
+
buf_init_from_buf(out_full_path, dirname);
uint8_t c = *(buf_ptr(out_full_path) + buf_len(out_full_path) - 1);
if (!os_is_sep(c))
@@ -989,12 +993,29 @@ int os_self_exe_path(Buf *out_path) {
}
#elif defined(ZIG_OS_DARWIN)
+ // How long is the executable's path?
uint32_t u32_len = 0;
int ret1 = _NSGetExecutablePath(nullptr, &u32_len);
assert(ret1 != 0);
- buf_resize(out_path, u32_len);
- int ret2 = _NSGetExecutablePath(buf_ptr(out_path), &u32_len);
+
+ Buf *tmp = buf_alloc_fixed(u32_len);
+
+ // Fill the executable path.
+ int ret2 = _NSGetExecutablePath(buf_ptr(tmp), &u32_len);
assert(ret2 == 0);
+
+ // According to libuv project, PATH_MAX*2 works around a libc bug where
+ // the resolved path is sometimes bigger than PATH_MAX.
+ buf_resize(out_path, PATH_MAX*2);
+ char *real_path = realpath(buf_ptr(tmp), buf_ptr(out_path));
+ if (!real_path) {
+ buf_init_from_buf(out_path, tmp);
+ return 0;
+ }
+
+ // Resize out_path for the correct length.
+ buf_resize(out_path, strlen(buf_ptr(out_path)));
+
return 0;
#elif defined(ZIG_OS_LINUX)
buf_resize(out_path, 256);
@@ -1007,6 +1028,7 @@ int os_self_exe_path(Buf *out_path) {
buf_resize(out_path, buf_len(out_path) * 2);
continue;
}
+ buf_resize(out_path, amt);
return 0;
}
#endif
@@ -1092,249 +1114,10 @@ void os_stderr_set_color(TermColor color) {
#endif
}
-int os_find_windows_sdk(ZigWindowsSDK **out_sdk) {
-#if defined(ZIG_OS_WINDOWS)
- ZigWindowsSDK *result_sdk = allocate<ZigWindowsSDK>(1);
- buf_resize(&result_sdk->path10, 0);
- buf_resize(&result_sdk->path81, 0);
-
- HKEY key;
- HRESULT rc;
- rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots", 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY | KEY_ENUMERATE_SUB_KEYS, &key);
- if (rc != ERROR_SUCCESS) {
- return ErrorFileNotFound;
- }
-
- {
- DWORD tmp_buf_len = MAX_PATH;
- buf_resize(&result_sdk->path10, tmp_buf_len);
- rc = RegQueryValueEx(key, "KitsRoot10", NULL, NULL, (LPBYTE)buf_ptr(&result_sdk->path10), &tmp_buf_len);
- if (rc == ERROR_FILE_NOT_FOUND) {
- buf_resize(&result_sdk->path10, 0);
- } else {
- buf_resize(&result_sdk->path10, tmp_buf_len);
- }
- }
- {
- DWORD tmp_buf_len = MAX_PATH;
- buf_resize(&result_sdk->path81, tmp_buf_len);
- rc = RegQueryValueEx(key, "KitsRoot81", NULL, NULL, (LPBYTE)buf_ptr(&result_sdk->path81), &tmp_buf_len);
- if (rc == ERROR_FILE_NOT_FOUND) {
- buf_resize(&result_sdk->path81, 0);
- } else {
- buf_resize(&result_sdk->path81, tmp_buf_len);
- }
- }
-
- if (buf_len(&result_sdk->path10) != 0) {
- Buf *sdk_lib_dir = buf_sprintf("%s\\Lib\\*", buf_ptr(&result_sdk->path10));
-
- // enumerate files in sdk path looking for latest version
- WIN32_FIND_DATA ffd;
- HANDLE hFind = FindFirstFileA(buf_ptr(sdk_lib_dir), &ffd);
- if (hFind == INVALID_HANDLE_VALUE) {
- return ErrorFileNotFound;
- }
- int v0 = 0, v1 = 0, v2 = 0, v3 = 0;
- bool found_version_dir = false;
- for (;;) {
- if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
- int c0 = 0, c1 = 0, c2 = 0, c3 = 0;
- sscanf(ffd.cFileName, "%d.%d.%d.%d", &c0, &c1, &c2, &c3);
- if (c0 == 10 && c1 == 0 && c2 == 10240 && c3 == 0) {
- // Microsoft released 26624 as 10240 accidentally.
- // https://developer.microsoft.com/en-us/windows/downloads/sdk-archive
- c2 = 26624;
- }
- if ((c0 > v0) || (c1 > v1) || (c2 > v2) || (c3 > v3)) {
- v0 = c0, v1 = c1, v2 = c2, v3 = c3;
- buf_init_from_str(&result_sdk->version10, ffd.cFileName);
- found_version_dir = true;
- }
- }
- if (FindNextFile(hFind, &ffd) == 0) {
- FindClose(hFind);
- break;
- }
- }
- if (!found_version_dir) {
- buf_resize(&result_sdk->path10, 0);
- }
- }
-
- if (buf_len(&result_sdk->path81) != 0) {
- Buf *sdk_lib_dir = buf_sprintf("%s\\Lib\\winv*", buf_ptr(&result_sdk->path81));
-
- // enumerate files in sdk path looking for latest version
- WIN32_FIND_DATA ffd;
- HANDLE hFind = FindFirstFileA(buf_ptr(sdk_lib_dir), &ffd);
- if (hFind == INVALID_HANDLE_VALUE) {
- return ErrorFileNotFound;
- }
- int v0 = 0, v1 = 0;
- bool found_version_dir = false;
- for (;;) {
- if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
- int c0 = 0, c1 = 0;
- sscanf(ffd.cFileName, "winv%d.%d", &c0, &c1);
- if ((c0 > v0) || (c1 > v1)) {
- v0 = c0, v1 = c1;
- buf_init_from_str(&result_sdk->version81, ffd.cFileName);
- found_version_dir = true;
- }
- }
- if (FindNextFile(hFind, &ffd) == 0) {
- FindClose(hFind);
- break;
- }
- }
- if (!found_version_dir) {
- buf_resize(&result_sdk->path81, 0);
- }
- }
-
- *out_sdk = result_sdk;
- return 0;
-#else
- return ErrorFileNotFound;
-#endif
-}
-
-int os_get_win32_vcruntime_path(Buf* output_buf, ZigLLVM_ArchType platform_type) {
-#if defined(ZIG_OS_WINDOWS)
- buf_resize(output_buf, 0);
- //COM Smart Pointerse requires explicit scope
- {
- HRESULT rc;
- rc = CoInitializeEx(NULL, COINIT_MULTITHREADED);
- if (rc != S_OK) {
- goto com_done;
- }
-
- //This COM class is installed when a VS2017
- ISetupConfigurationPtr setup_config;
- rc = setup_config.CreateInstance(__uuidof(SetupConfiguration));
- if (rc != S_OK) {
- goto com_done;
- }
-
- IEnumSetupInstancesPtr all_instances;
- rc = setup_config->EnumInstances(&all_instances);
- if (rc != S_OK) {
- goto com_done;
- }
-
- ISetupInstance* curr_instance;
- ULONG found_inst;
- while ((rc = all_instances->Next(1, &curr_instance, &found_inst) == S_OK)) {
- BSTR bstr_inst_path;
- rc = curr_instance->GetInstallationPath(&bstr_inst_path);
- if (rc != S_OK) {
- goto com_done;
- }
- //BSTRs are UTF-16 encoded, so we need to convert the string & adjust the length
- UINT bstr_path_len = *((UINT*)bstr_inst_path - 1);
- ULONG tmp_path_len = bstr_path_len / 2 + 1;
- char* conv_path = (char*)bstr_inst_path;
- char *tmp_path = (char*)alloca(tmp_path_len);
- memset(tmp_path, 0, tmp_path_len);
- uint32_t c = 0;
- for (uint32_t i = 0; i < bstr_path_len; i += 2) {
- tmp_path[c] = conv_path[i];
- ++c;
- assert(c != tmp_path_len);
- }
-
- buf_append_str(output_buf, tmp_path);
- buf_append_char(output_buf, '\\');
-
- Buf* tmp_buf = buf_alloc();
- buf_append_buf(tmp_buf, output_buf);
- buf_append_str(tmp_buf, "VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt");
- FILE* tools_file = fopen(buf_ptr(tmp_buf), "r");
- if (!tools_file) {
- goto com_done;
- }
- memset(tmp_path, 0, tmp_path_len);
- fgets(tmp_path, tmp_path_len, tools_file);
- strtok(tmp_path, " \r\n");
- fclose(tools_file);
- buf_appendf(output_buf, "VC\\Tools\\MSVC\\%s\\lib\\", tmp_path);
- switch (platform_type) {
- case ZigLLVM_x86:
- buf_append_str(output_buf, "x86\\");
- break;
- case ZigLLVM_x86_64:
- buf_append_str(output_buf, "x64\\");
- break;
- case ZigLLVM_arm:
- buf_append_str(output_buf, "arm\\");
- break;
- default:
- zig_panic("Attemped to use vcruntime for non-supported platform.");
- }
- buf_resize(tmp_buf, 0);
- buf_append_buf(tmp_buf, output_buf);
- buf_append_str(tmp_buf, "vcruntime.lib");
-
- if (GetFileAttributesA(buf_ptr(tmp_buf)) != INVALID_FILE_ATTRIBUTES) {
- return 0;
- }
- }
- }
-
-com_done:;
- HKEY key;
- HRESULT rc;
- rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7", 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &key);
- if (rc != ERROR_SUCCESS) {
- return ErrorFileNotFound;
- }
-
- DWORD dw_type = 0;
- DWORD cb_data = 0;
- rc = RegQueryValueEx(key, "14.0", NULL, &dw_type, NULL, &cb_data);
- if ((rc == ERROR_FILE_NOT_FOUND) || (REG_SZ != dw_type)) {
- return ErrorFileNotFound;
- }
-
- Buf* tmp_buf = buf_alloc_fixed(cb_data);
- RegQueryValueExA(key, "14.0", NULL, NULL, (LPBYTE)buf_ptr(tmp_buf), &cb_data);
- //RegQueryValueExA returns the length of the string INCLUDING the null terminator
- buf_resize(tmp_buf, cb_data-1);
- buf_append_str(tmp_buf, "VC\\Lib\\");
- switch (platform_type) {
- case ZigLLVM_x86:
- //x86 is in the root of the Lib folder
- break;
- case ZigLLVM_x86_64:
- buf_append_str(tmp_buf, "amd64\\");
- break;
- case ZigLLVM_arm:
- buf_append_str(tmp_buf, "arm\\");
- break;
- default:
- zig_panic("Attemped to use vcruntime for non-supported platform.");
- }
-
- buf_append_buf(output_buf, tmp_buf);
- buf_append_str(tmp_buf, "vcruntime.lib");
-
- if (GetFileAttributesA(buf_ptr(tmp_buf)) != INVALID_FILE_ATTRIBUTES) {
- return 0;
- } else {
- buf_resize(output_buf, 0);
- return ErrorFileNotFound;
- }
-#else
- return ErrorFileNotFound;
-#endif
-}
-
int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchType platform_type) {
#if defined(ZIG_OS_WINDOWS)
buf_resize(output_buf, 0);
- buf_appendf(output_buf, "%s\\Lib\\%s\\ucrt\\", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
+ buf_appendf(output_buf, "%s\\Lib\\%s\\ucrt\\", sdk->path10_ptr, sdk->version10_ptr);
switch (platform_type) {
case ZigLLVM_x86:
buf_append_str(output_buf, "x86\\");
@@ -1366,7 +1149,7 @@ int os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_Arch
int os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf* output_buf) {
#if defined(ZIG_OS_WINDOWS)
buf_resize(output_buf, 0);
- buf_appendf(output_buf, "%s\\Include\\%s\\ucrt", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
+ buf_appendf(output_buf, "%s\\Include\\%s\\ucrt", sdk->path10_ptr, sdk->version10_ptr);
if (GetFileAttributesA(buf_ptr(output_buf)) != INVALID_FILE_ATTRIBUTES) {
return 0;
}
@@ -1383,7 +1166,7 @@ int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchTy
#if defined(ZIG_OS_WINDOWS)
{
buf_resize(output_buf, 0);
- buf_appendf(output_buf, "%s\\Lib\\%s\\um\\", buf_ptr(&sdk->path10), buf_ptr(&sdk->version10));
+ buf_appendf(output_buf, "%s\\Lib\\%s\\um\\", sdk->path10_ptr, sdk->version10_ptr);
switch (platform_type) {
case ZigLLVM_x86:
buf_append_str(output_buf, "x86\\");
@@ -1406,7 +1189,7 @@ int os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf* output_buf, ZigLLVM_ArchTy
}
{
buf_resize(output_buf, 0);
- buf_appendf(output_buf, "%s\\Lib\\%s\\um\\", buf_ptr(&sdk->path81), buf_ptr(&sdk->version81));
+ buf_appendf(output_buf, "%s\\Lib\\%s\\um\\", sdk->path81_ptr, sdk->version81_ptr);
switch (platform_type) {
case ZigLLVM_x86:
buf_append_str(output_buf, "x86\\");