Merge branch 'master' into find-vs-winsdk-env

This commit is contained in:
Jeroen van Rijn
2022-05-27 21:37:36 +02:00
committed by GitHub
8 changed files with 454 additions and 479 deletions
+12
View File
@@ -1,3 +1,15 @@
package dynlib package dynlib
Library :: distinct rawptr Library :: distinct rawptr
load_library :: proc(path: string, global_symbols := false) -> (Library, bool) {
return _load_library(path, global_symbols)
}
unload_library :: proc(library: Library) -> bool {
return _unload_library(library)
}
symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) #optional_ok {
return _symbol_address(library, symbol)
}
+5 -4
View File
@@ -1,9 +1,10 @@
//+build linux, darwin, freebsd, openbsd //+build linux, darwin, freebsd, openbsd
//+private
package dynlib package dynlib
import "core:os" import "core:os"
load_library :: proc(path: string, global_symbols := false) -> (Library, bool) { _load_library :: proc(path: string, global_symbols := false) -> (Library, bool) {
flags := os.RTLD_NOW flags := os.RTLD_NOW
if global_symbols { if global_symbols {
flags |= os.RTLD_GLOBAL flags |= os.RTLD_GLOBAL
@@ -12,11 +13,11 @@ load_library :: proc(path: string, global_symbols := false) -> (Library, bool) {
return Library(lib), lib != nil return Library(lib), lib != nil
} }
unload_library :: proc(library: Library) { _unload_library :: proc(library: Library) -> bool {
os.dlclose(rawptr(library)) return os.dlclose(rawptr(library))
} }
symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) { _symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) {
ptr = os.dlsym(rawptr(library), symbol) ptr = os.dlsym(rawptr(library), symbol)
found = ptr != nil found = ptr != nil
return return
+4 -3
View File
@@ -1,10 +1,11 @@
//+build windows //+build windows
//+private
package dynlib package dynlib
import win32 "core:sys/windows" import win32 "core:sys/windows"
import "core:strings" import "core:strings"
load_library :: proc(path: string, global_symbols := false) -> (Library, bool) { _load_library :: proc(path: string, global_symbols := false) -> (Library, bool) {
// NOTE(bill): 'global_symbols' is here only for consistency with POSIX which has RTLD_GLOBAL // NOTE(bill): 'global_symbols' is here only for consistency with POSIX which has RTLD_GLOBAL
wide_path := win32.utf8_to_wstring(path, context.temp_allocator) wide_path := win32.utf8_to_wstring(path, context.temp_allocator)
@@ -12,12 +13,12 @@ load_library :: proc(path: string, global_symbols := false) -> (Library, bool) {
return handle, handle != nil return handle, handle != nil
} }
unload_library :: proc(library: Library) -> bool { _unload_library :: proc(library: Library) -> bool {
ok := win32.FreeLibrary(cast(win32.HMODULE)library) ok := win32.FreeLibrary(cast(win32.HMODULE)library)
return bool(ok) return bool(ok)
} }
symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) { _symbol_address :: proc(library: Library, symbol: string) -> (ptr: rawptr, found: bool) {
c_str := strings.clone_to_cstring(symbol, context.temp_allocator) c_str := strings.clone_to_cstring(symbol, context.temp_allocator)
ptr = win32.GetProcAddress(cast(win32.HMODULE)library, c_str) ptr = win32.GetProcAddress(cast(win32.HMODULE)library, c_str)
found = ptr != nil found = ptr != nil
+4 -5
View File
@@ -37,9 +37,9 @@ MADV_WIPEONFORK :: 18
MADV_KEEPONFORK :: 19 MADV_KEEPONFORK :: 19
MADV_HWPOISON :: 100 MADV_HWPOISON :: 100
mmap :: proc "contextless" (addr: rawptr, length: uint, prot: c.int, flags: c.int, fd: c.int, offset: uintptr) -> rawptr { mmap :: proc "contextless" (addr: rawptr, length: uint, prot: c.int, flags: c.int, fd: c.int, offset: uintptr) -> int {
res := intrinsics.syscall(unix.SYS_mmap, uintptr(addr), uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), offset) res := intrinsics.syscall(unix.SYS_mmap, uintptr(addr), uintptr(length), uintptr(prot), uintptr(flags), uintptr(fd), offset)
return rawptr(res) return int(res)
} }
munmap :: proc "contextless" (addr: rawptr, length: uint) -> c.int { munmap :: proc "contextless" (addr: rawptr, length: uint) -> c.int {
@@ -59,12 +59,11 @@ madvise :: proc "contextless" (addr: rawptr, length: uint, advice: c.int) -> c.i
_reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) { _reserve :: proc "contextless" (size: uint) -> (data: []byte, err: Allocator_Error) {
MAP_FAILED := rawptr(~uintptr(0))
result := mmap(nil, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) result := mmap(nil, size, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)
if result == MAP_FAILED { if result < 0 && result > -4096 {
return nil, .Out_Of_Memory return nil, .Out_Of_Memory
} }
return ([^]byte)(result)[:size], nil return ([^]byte)(uintptr(result))[:size], nil
} }
_commit :: proc "contextless" (data: rawptr, size: uint) -> Allocator_Error { _commit :: proc "contextless" (data: rawptr, size: uint) -> Allocator_Error {
+12 -7
View File
@@ -1424,6 +1424,8 @@ bool init_build_paths(String init_filename) {
if ((bc->command_kind & Command__does_build) && (!bc->ignore_microsoft_magic)) { if ((bc->command_kind & Command__does_build) && (!bc->ignore_microsoft_magic)) {
// NOTE(ic): It would be nice to extend this so that we could specify the Visual Studio version that we want instead of defaulting to the latest. // NOTE(ic): It would be nice to extend this so that we could specify the Visual Studio version that we want instead of defaulting to the latest.
Find_Result_Utf8 find_result = find_visual_studio_and_windows_sdk_utf8(); Find_Result_Utf8 find_result = find_visual_studio_and_windows_sdk_utf8();
defer (mc_free_all());
bool all_found = bool all_found =
find_result.windows_sdk_root.len > 0 && find_result.windows_sdk_root.len > 0 &&
find_result.windows_sdk_um_library_path.len > 0 && find_result.windows_sdk_um_library_path.len > 0 &&
@@ -1438,6 +1440,16 @@ bool init_build_paths(String init_filename) {
} }
} }
if (!build_context.use_lld && find_result.vs_exe_path.len == 0) {
gb_printf_err("link.exe not found.\n");
return false;
}
if (find_result.vs_library_path.len == 0) {
gb_printf_err("VS library path not found.\n");
return false;
}
if (find_result.windows_sdk_um_library_path.len > 0) { if (find_result.windows_sdk_um_library_path.len > 0) {
GB_ASSERT(find_result.windows_sdk_ucrt_library_path.len > 0); GB_ASSERT(find_result.windows_sdk_ucrt_library_path.len > 0);
@@ -1461,13 +1473,6 @@ bool init_build_paths(String init_filename) {
bc->build_paths[BuildPath_VS_LIB] = path_from_string(ha, find_result.vs_library_path); bc->build_paths[BuildPath_VS_LIB] = path_from_string(ha, find_result.vs_library_path);
} }
} }
gb_free(ha, find_result.windows_sdk_root.text);
gb_free(ha, find_result.windows_sdk_um_library_path.text);
gb_free(ha, find_result.windows_sdk_ucrt_library_path.text);
gb_free(ha, find_result.vs_exe_path.text);
gb_free(ha, find_result.vs_library_path.text);
} }
#endif #endif
+1 -1
View File
@@ -1680,7 +1680,7 @@ GB_DEF gbFileContents gb_file_read_contents(gbAllocator a, b32 zero_terminate, c
GB_DEF void gb_file_free_contents(gbFileContents *fc); GB_DEF void gb_file_free_contents(gbFileContents *fc);
// TODO(bill): Should these have different na,es as they do not take in a gbFile * ??? // TODO(bill): Should these have different names as they do not take in a gbFile * ???
GB_DEF b32 gb_file_exists (char const *filepath); GB_DEF b32 gb_file_exists (char const *filepath);
GB_DEF gbFileTime gb_file_last_write_time(char const *filepath); GB_DEF gbFileTime gb_file_last_write_time(char const *filepath);
GB_DEF b32 gb_file_copy (char const *existing_filename, char const *new_filename, b32 fail_if_exists); GB_DEF b32 gb_file_copy (char const *existing_filename, char const *new_filename, b32 fail_if_exists);
+170 -213
View File
@@ -45,8 +45,6 @@
// //
// Here is the API you need to know about: // Here is the API you need to know about:
// //
gb_global gbAllocator mc_allocator = heap_allocator(); gb_global gbAllocator mc_allocator = heap_allocator();
struct Find_Result { struct Find_Result {
@@ -71,27 +69,68 @@ struct Find_Result_Utf8 {
String vs_library_path; String vs_library_path;
}; };
Find_Result find_visual_studio_and_windows_sdk();
Find_Result_Utf8 find_visual_studio_and_windows_sdk_utf8(); Find_Result_Utf8 find_visual_studio_and_windows_sdk_utf8();
void free_resources(Find_Result *result) { String mc_wstring_to_string(wchar_t const *str) {
// free(result->windows_sdk_root); return string16_to_string(mc_allocator, make_string16_c(str));
// free(result->windows_sdk_um_library_path);
// free(result->windows_sdk_ucrt_library_path);
// free(result->vs_exe_path);
// free(result->vs_library_path);
} }
void free_resources(Find_Result_Utf8 *result) { String16 mc_string_to_wstring(String str) {
// gbAllocator a = heap_allocator(); return string_to_string16(mc_allocator, str);
// gb_free(a, result->windows_sdk_root.text);
// gb_free(a, result->windows_sdk_um_library_path.text);
// gb_free(a, result->windows_sdk_ucrt_library_path.text);
// gb_free(a, result->vs_exe_path.text);
// gb_free(a, result->vs_library_path.text);
} }
String mc_concat(String a, String b) {
return concatenate_strings(mc_allocator, a, b);
}
String mc_concat(String a, String b, String c) {
return concatenate3_strings(mc_allocator, a, b, c);
}
void mc_free(String str) {
gb_free(mc_allocator, str.text);
}
void mc_free(String16 str) {
gb_free(mc_allocator, str.text);
}
void mc_free_all() {
gb_free_all(mc_allocator);
}
typedef struct _MC_Find_Data {
DWORD file_attributes;
String filename;
} MC_Find_Data;
HANDLE mc_find_first(String wildcard, MC_Find_Data *find_data) {
WIN32_FIND_DATAW _find_data;
String16 wildcard_wide = mc_string_to_wstring(wildcard);
defer (mc_free(wildcard_wide));
HANDLE handle = FindFirstFileW(wildcard_wide.text, &_find_data);
if (handle == INVALID_HANDLE_VALUE) return false;
find_data->file_attributes = _find_data.dwFileAttributes;
find_data->filename = mc_wstring_to_string(_find_data.cFileName);
return handle;
}
bool mc_find_next(HANDLE handle, MC_Find_Data *find_data) {
WIN32_FIND_DATAW _find_data;
bool success = FindNextFileW(handle, &_find_data);
find_data->file_attributes = _find_data.dwFileAttributes;
find_data->filename = mc_wstring_to_string(_find_data.cFileName);
return success;
}
void mc_find_close(HANDLE handle) {
FindClose(handle);
}
// //
// Call find_visual_studio_and_windows_sdk, look at the resulting // Call find_visual_studio_and_windows_sdk, look at the resulting
@@ -142,7 +181,6 @@ void free_resources(Find_Result_Utf8 *result) {
// COM objects for the ridiculous Microsoft craziness. // COM objects for the ridiculous Microsoft craziness.
typedef WCHAR* BSTR; typedef WCHAR* BSTR;
typedef const WCHAR* LPCOLESTR; typedef const WCHAR* LPCOLESTR;
@@ -176,107 +214,76 @@ struct DECLSPEC_UUID("42843719-DB4C-46C2-8E7C-64F1816EFD5B") DECLSPEC_NOVTABLE I
// The beginning of the actual code that does things. // The beginning of the actual code that does things.
struct Version_Data_Utf8 {
struct Version_Data {
i32 best_version[4]; // For Windows 8 versions, only two of these numbers are used. i32 best_version[4]; // For Windows 8 versions, only two of these numbers are used.
wchar_t const *best_name; String best_name;
}; };
bool os_file_exists(wchar_t const *name) { typedef void (*MC_Visit_Proc)(String short_name, String full_name, Version_Data_Utf8 *data);
// @Robustness: What flags do we really want to check here? bool mc_visit_files(String dir_name, Version_Data_Utf8 *data, MC_Visit_Proc proc) {
auto attrib = GetFileAttributesW(name);
if (attrib == INVALID_FILE_ATTRIBUTES) return false;
if (attrib & FILE_ATTRIBUTE_DIRECTORY) return false;
return true;
}
wchar_t *concat(wchar_t const *a, wchar_t const *b, wchar_t const *c = nullptr, wchar_t const *d = nullptr) {
// Concatenate up to 4 wide strings together. Allocated with malloc.
// If you don't like that, use a programming language that actually
// helps you with using custom allocators. Or just edit the code.
isize len_a = string16_len(a);
isize len_b = string16_len(b);
isize len_c = string16_len(c);
isize len_d = string16_len(d);
wchar_t *result = (wchar_t *)calloc(2, (len_a + len_b + len_c + len_d + 1));
gb_memmove(result, a, len_a*2);
gb_memmove(result + len_a, b, len_b*2);
if (c) gb_memmove(result + len_a + len_b, c, len_c * 2);
if (d) gb_memmove(result + len_a + len_b + len_c, d, len_d * 2);
result[len_a + len_b + len_c + len_d] = 0;
return result;
}
typedef void (*Visit_Proc_W)(wchar_t const *short_name, wchar_t const *full_name, Version_Data *data);
bool visit_files_w(wchar_t const *dir_name, Version_Data *data, Visit_Proc_W proc) {
// Visit everything in one folder (non-recursively). If it's a directory // Visit everything in one folder (non-recursively). If it's a directory
// that doesn't start with ".", call the visit proc on it. The visit proc // that doesn't start with ".", call the visit proc on it. The visit proc
// will see if the filename conforms to the expected versioning pattern. // will see if the filename conforms to the expected versioning pattern.
auto wildcard_name = concat(dir_name, L"\\*"); String wildcard_name = mc_concat(dir_name, str_lit("\\*"));
defer (free(wildcard_name)); defer (mc_free(wildcard_name));
WIN32_FIND_DATAW find_data; MC_Find_Data find_data;
auto handle = FindFirstFileW(wildcard_name, &find_data);
HANDLE handle = mc_find_first(wildcard_name, &find_data);
if (handle == INVALID_HANDLE_VALUE) return false; if (handle == INVALID_HANDLE_VALUE) return false;
while (true) { bool success = true;
if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (find_data.cFileName[0] != '.')) { while (success) {
auto full_name = concat(dir_name, L"\\", find_data.cFileName); if ((find_data.file_attributes & FILE_ATTRIBUTE_DIRECTORY) && (find_data.filename[0] != '.')) {
defer (free(full_name)); String full_name = mc_concat(dir_name, str_lit("\\"), find_data.filename);
defer (mc_free(full_name));
proc(find_data.cFileName, full_name, data); proc(find_data.filename, full_name, data);
} }
auto success = FindNextFileW(handle, &find_data); success = mc_find_next(handle, &find_data);
if (!success) break; if (!success) break;
} }
mc_find_close(handle);
FindClose(handle);
return true; return true;
} }
String find_windows_kit_root(HKEY key, String const version) {
wchar_t *find_windows_kit_root(HKEY key, wchar_t const *version) {
// Given a key to an already opened registry entry, // Given a key to an already opened registry entry,
// get the value stored under the 'version' subkey. // get the value stored under the 'version' subkey.
// If that's not the right terminology, hey, I never do registry stuff. // If that's not the right terminology, hey, I never do registry stuff.
char *version_str = (char*)version.text;
DWORD required_length; DWORD required_length;
auto rc = RegQueryValueExW(key, version, NULL, NULL, NULL, &required_length); auto rc = RegQueryValueExA(key, version_str, NULL, NULL, NULL, &required_length);
if (rc != 0) return NULL; if (rc != 0) return {};
DWORD length = required_length + 2; // The +2 is for the maybe optional zero later on. Probably we are over-allocating. DWORD length = required_length + 2; // The +2 is for the maybe optional zero later on. Probably we are over-allocating.
wchar_t *value = (wchar_t *)calloc(1, length); char *c_str = gb_alloc_array(mc_allocator, char, length);
if (!value) return NULL;
rc = RegQueryValueExW(key, version, NULL, NULL, (LPBYTE)value, &length); // We know that version is zero-terminated... rc = RegQueryValueExA(key, version_str, NULL, NULL, (LPBYTE)c_str, &length); // We know that version is zero-terminated...
if (rc != 0) return NULL; if (rc != 0) return {};
// The documentation says that if the string for some reason was not stored // The documentation says that if the string for some reason was not stored
// with zero-termination, we need to manually terminate it. Sigh!! // with zero-termination, we need to manually terminate it. Sigh!!
if (value[length]) { if (c_str[required_length]) {
value[length+1] = 0; c_str[required_length+1] = 0;
} }
String value = make_string_c(c_str);
return value; return value;
} }
void win10_best(wchar_t const *short_name, wchar_t const *full_name, Version_Data *data) { void win10_best(String short_name, String full_name, Version_Data_Utf8 *data) {
// Find the Windows 10 subdirectory with the highest version number. // Find the Windows 10 subdirectory with the highest version number.
int i0, i1, i2, i3; int i0, i1, i2, i3;
auto success = swscanf_s(short_name, L"%d.%d.%d.%d", &i0, &i1, &i2, &i3); auto success = sscanf_s((const char *const)short_name.text, "%d.%d.%d.%d", &i0, &i1, &i2, &i3);
if (success < 4) return; if (success < 4) return;
if (i0 < data->best_version[0]) return; if (i0 < data->best_version[0]) return;
@@ -292,10 +299,11 @@ void win10_best(wchar_t const *short_name, wchar_t const *full_name, Version_Dat
// we have to copy_string and free here because visit_files free's the full_name string // we have to copy_string and free here because visit_files free's the full_name string
// after we execute this function, so Win*_Data would contain an invalid pointer. // after we execute this function, so Win*_Data would contain an invalid pointer.
if (data->best_name) free((void *)data->best_name); if (data->best_name.len > 0) mc_free(data->best_name);
data->best_name = _wcsdup(full_name);
if (data->best_name) { data->best_name = copy_string(mc_allocator, full_name);
if (data->best_name.len > 0) {
data->best_version[0] = i0; data->best_version[0] = i0;
data->best_version[1] = i1; data->best_version[1] = i1;
data->best_version[2] = i2; data->best_version[2] = i2;
@@ -303,11 +311,11 @@ void win10_best(wchar_t const *short_name, wchar_t const *full_name, Version_Dat
} }
} }
void win8_best(wchar_t const *short_name, wchar_t const *full_name, Version_Data *data) { void win8_best(String short_name, String full_name, Version_Data_Utf8 *data) {
// Find the Windows 8 subdirectory with the highest version number. // Find the Windows 8 subdirectory with the highest version number.
int i0, i1; int i0, i1;
auto success = swscanf_s(short_name, L"winv%d.%d", &i0, &i1); auto success = sscanf_s((const char *const)short_name.text, "winv%d.%d", &i0, &i1);
if (success < 2) return; if (success < 2) return;
if (i0 < data->best_version[0]) return; if (i0 < data->best_version[0]) return;
@@ -317,16 +325,16 @@ void win8_best(wchar_t const *short_name, wchar_t const *full_name, Version_Data
// we have to copy_string and free here because visit_files free's the full_name string // we have to copy_string and free here because visit_files free's the full_name string
// after we execute this function, so Win*_Data would contain an invalid pointer. // after we execute this function, so Win*_Data would contain an invalid pointer.
if (data->best_name) free((void *)data->best_name); if (data->best_name.len > 0) mc_free(data->best_name);
data->best_name = _wcsdup(full_name); data->best_name = copy_string(mc_allocator, full_name);
if (data->best_name) { if (data->best_name.len > 0) {
data->best_version[0] = i0; data->best_version[0] = i0;
data->best_version[1] = i1; data->best_version[1] = i1;
} }
} }
void find_windows_kit_root(Find_Result *result) { void find_windows_kit_root(Find_Result_Utf8 *result) {
// Information about the Windows 10 and Windows 8 development kits // Information about the Windows 10 and Windows 8 development kits
// is stored in the same place in the registry. We open a key // is stored in the same place in the registry. We open a key
// to that place, first checking preferntially for a Windows 10 kit, // to that place, first checking preferntially for a Windows 10 kit,
@@ -340,49 +348,46 @@ void find_windows_kit_root(Find_Result *result) {
defer (RegCloseKey(main_key)); defer (RegCloseKey(main_key));
// Look for a Windows 10 entry. // Look for a Windows 10 entry.
auto windows10_root = find_windows_kit_root(main_key, L"KitsRoot10"); String windows10_root = find_windows_kit_root(main_key, str_lit("KitsRoot10"));
if (windows10_root.len > 0) {
defer (mc_free(windows10_root));
if (windows10_root) { String windows10_lib = mc_concat(windows10_root, str_lit("Lib"));
defer (free(windows10_root)); defer (mc_free(windows10_lib));
Version_Data_Utf8 data = {0};
Version_Data data = {0}; mc_visit_files(windows10_lib, &data, win10_best);
auto windows10_lib = concat(windows10_root, L"Lib"); if (data.best_name.len > 0) {
defer (free(windows10_lib));
visit_files_w(windows10_lib, &data, win10_best);
if (data.best_name) {
result->windows_sdk_version = 10; result->windows_sdk_version = 10;
result->windows_sdk_root = concat(data.best_name, L"\\"); result->windows_sdk_root = mc_concat(data.best_name, str_lit("\\"));
return; return;
} }
mc_free(data.best_name);
} }
// Look for a Windows 8 entry. // Look for a Windows 8 entry.
auto windows8_root = find_windows_kit_root(main_key, L"KitsRoot81"); String windows8_root = find_windows_kit_root(main_key, str_lit("KitsRoot81"));
if (windows8_root) { if (windows8_root.len > 0) {
defer (free(windows8_root)); defer (mc_free(windows8_root));
auto windows8_lib = concat(windows8_root, L"Lib"); String windows8_lib = mc_concat(windows8_root, str_lit("Lib"));
defer (free(windows8_lib)); defer (mc_free(windows8_lib));
Version_Data data = {0}; Version_Data_Utf8 data = {0};
visit_files_w(windows8_lib, &data, win8_best); mc_visit_files(windows8_lib, &data, win8_best);
if (data.best_name) { if (data.best_name.len > 0) {
result->windows_sdk_version = 8; result->windows_sdk_version = 8;
result->windows_sdk_root = concat(data.best_name, L"\\"); result->windows_sdk_root = mc_concat(data.best_name, str_lit("\\"));
return; return;
} }
mc_free(data.best_name);
} }
// If we get here, we failed to find anything. // If we get here, we failed to find anything.
} }
bool find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result_Utf8 *result) {
bool find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *result) {
// The name of this procedure is kind of cryptic. Its purpose is // The name of this procedure is kind of cryptic. Its purpose is
// to fight through Microsoft craziness. The things that the fine // to fight through Microsoft craziness. The things that the fine
// Visual Studio team want you to do, JUST TO FIND A SINGLE FOLDER // Visual Studio team want you to do, JUST TO FIND A SINGLE FOLDER
@@ -426,64 +431,49 @@ bool find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *res
defer (instance->Release()); defer (instance->Release());
BSTR bstr_inst_path; wchar_t* inst_path_wide;
hr = instance->GetInstallationPath(&bstr_inst_path); hr = instance->GetInstallationPath(&inst_path_wide);
if (hr != S_OK) continue; if (hr != S_OK) continue;
defer (SysFreeString(bstr_inst_path)); defer (SysFreeString(inst_path_wide));
auto tools_filename = concat(bstr_inst_path, L"\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt"); String inst_path = mc_wstring_to_string(inst_path_wide);
defer (free(tools_filename)); defer (mc_free(inst_path));
FILE *f = nullptr; String tools_filename = mc_concat(inst_path, str_lit("\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt"));
auto open_result = _wfopen_s(&f, tools_filename, L"rt"); defer (mc_free(tools_filename));
if (open_result != 0) continue;
if (!f) continue;
defer (fclose(f));
LARGE_INTEGER tools_file_size; gbFileContents tool_version = gb_file_read_contents(mc_allocator, true, (const char*)tools_filename.text);
auto file_handle = (HANDLE)_get_osfhandle(_fileno(f)); defer (gb_file_free_contents(&tool_version));
BOOL success = GetFileSizeEx(file_handle, &tools_file_size);
if (!success) continue;
auto version_bytes = (tools_file_size.QuadPart + 1) * 2; // Warning: This multiplication by 2 presumes there is no variable-length encoding in the wchars (wacky characters in the file could betray this expectation). String version_string = make_string((const u8*)tool_version.data, tool_version.size);
if (version_bytes > 0x7FFFFFFF) continue; // Avoid overflow. version_string = string_trim_whitespace(version_string);
wchar_t *version = (wchar_t *)calloc(1, (usize)version_bytes); String base_path = mc_concat(inst_path, str_lit("\\VC\\Tools\\MSVC\\"), version_string);
defer (free(version)); defer (mc_free(base_path));
auto read_result = fgetws(version, (int)version_bytes, f); String library_path = {};
if (!read_result) continue;
auto version_tail = wcschr(version, '\n');
if (version_tail) *version_tail = 0; // Stomp the data, because nobody cares about it.
wchar_t *library_path = nullptr;
if (build_context.metrics.arch == TargetArch_amd64) { if (build_context.metrics.arch == TargetArch_amd64) {
library_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\lib\\x64\\"); library_path = mc_concat(base_path, str_lit("\\lib\\x64\\"));
} else if (build_context.metrics.arch == TargetArch_i386) { } else if (build_context.metrics.arch == TargetArch_i386) {
library_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\lib\\x86\\"); library_path = mc_concat(base_path, str_lit("\\lib\\x86\\"));
} else { } else {
continue; continue;
} }
auto library_file = concat(library_path, L"vcruntime.lib"); // @Speed: Could have library_path point to this string, with a smaller count, to save on memory flailing! String library_file = mc_concat(library_path, str_lit("vcruntime.lib"));
if (os_file_exists(library_file)) { if (gb_file_exists((const char*)library_file.text)) {
wchar_t *link_exe_path = nullptr;
if (build_context.metrics.arch == TargetArch_amd64) { if (build_context.metrics.arch == TargetArch_amd64) {
link_exe_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\bin\\Hostx64\\x64\\"); result->vs_exe_path = mc_concat(base_path, str_lit("\\bin\\Hostx64\\x64\\"));
} else if (build_context.metrics.arch == TargetArch_i386) { } else if (build_context.metrics.arch == TargetArch_i386) {
link_exe_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\bin\\Hostx86\\x86\\"); result->vs_exe_path = mc_concat(base_path, str_lit("\\bin\\Hostx86\\x86\\"));
} else { } else {
continue; continue;
} }
result->vs_exe_path = link_exe_path;
result->vs_library_path = library_path; result->vs_library_path = library_path;
return true; return true;
} }
/* /*
Ryan Saunderson said: Ryan Saunderson said:
"Clang uses the 'SetupInstance->GetInstallationVersion' / ISetupHelper->ParseVersion to find the newest version "Clang uses the 'SetupInstance->GetInstallationVersion' / ISetupHelper->ParseVersion to find the newest version
@@ -502,110 +492,78 @@ bool find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *res
defer (RegCloseKey(vs7_key)); defer (RegCloseKey(vs7_key));
// Hardcoded search for 4 prior Visual Studio versions. Is there something better to do here? // Hardcoded search for 4 prior Visual Studio versions. Is there something better to do here?
wchar_t const *versions[] = { L"14.0", L"13.0", L"12.0", L"11.0", L"10.0", L"9.0", }; char const *versions[] = { "14.0", "13.0", "12.0", "11.0", "10.0", "9.0", };
const int NUM_VERSIONS = sizeof(versions) / sizeof(versions[0]); const int NUM_VERSIONS = sizeof(versions) / sizeof(versions[0]);
for (int i = 0; i < NUM_VERSIONS; i++) { for (int i = 0; i < NUM_VERSIONS; i++) {
wchar_t const *v = versions[i]; char const *v = versions[i];
DWORD dw_type; DWORD dw_type;
DWORD cb_data; DWORD required_length;
auto rc = RegQueryValueExW(vs7_key, v, NULL, &dw_type, NULL, &cb_data); auto rc = RegQueryValueExA(vs7_key, v, NULL, &dw_type, NULL, &required_length);
if ((rc == ERROR_FILE_NOT_FOUND) || (dw_type != REG_SZ)) { if ((rc == ERROR_FILE_NOT_FOUND) || (dw_type != REG_SZ)) {
continue; continue;
} }
auto buffer = (wchar_t *)calloc(1, cb_data); DWORD length = required_length + 2; // The +2 is for the maybe optional zero later on. Probably we are over-allocating.
if (!buffer) return false; char *c_str = gb_alloc_array(mc_allocator, char, length);
defer (free(buffer));
rc = RegQueryValueExW(vs7_key, v, NULL, NULL, (LPBYTE)buffer, &cb_data); rc = RegQueryValueExA(vs7_key, v, NULL, NULL, (LPBYTE)c_str, &length);
if (rc != 0) continue; if (rc != 0) continue;
// @Robustness: Do the zero-termination thing suggested in the RegQueryValue docs? if (c_str[required_length]) {
c_str[required_length+1] = 0;
}
String base_path = make_string_c(c_str);
wchar_t *lib_path = nullptr; String lib_path = {};
if (build_context.metrics.arch == TargetArch_amd64) { if (build_context.metrics.arch == TargetArch_amd64) {
lib_path = concat(buffer, L"VC\\Lib\\amd64\\"); lib_path = mc_concat(base_path, str_lit("VC\\Lib\\amd64\\"));
} else if (build_context.metrics.arch == TargetArch_i386) { } else if (build_context.metrics.arch == TargetArch_i386) {
lib_path = concat(buffer, L"VC\\Lib\\"); lib_path = mc_concat(base_path, str_lit("VC\\Lib\\"));
} else { } else {
continue; continue;
} }
// Check to see whether a vcruntime.lib actually exists here. // Check to see whether a vcruntime.lib actually exists here.
auto vcruntime_filename = concat(lib_path, L"vcruntime.lib"); String vcruntime_filename = mc_concat(lib_path, str_lit("vcruntime.lib"));
defer (free(vcruntime_filename)); defer (mc_free(vcruntime_filename));
if (os_file_exists(vcruntime_filename)) { if (gb_file_exists((const char*)vcruntime_filename.text)) {
if (build_context.metrics.arch == TargetArch_amd64) { if (build_context.metrics.arch == TargetArch_amd64) {
result->vs_exe_path = concat(buffer, L"VC\\bin\\"); result->vs_exe_path = mc_concat(base_path, str_lit("VC\\bin\\"));
} else if (build_context.metrics.arch == TargetArch_i386) { } else if (build_context.metrics.arch == TargetArch_i386) {
// result->vs_exe_path = concat(buffer, L"VC\\bin\\amd64_x86\\"); result->vs_exe_path = mc_concat(base_path, str_lit("VC\\bin\\x86_amd64\\"));
result->vs_exe_path = concat(buffer, L"VC\\bin\\x86_amd64\\");
} else { } else {
continue; continue;
} }
result->vs_library_path = lib_path; result->vs_library_path = lib_path;
return true; return true;
} }
mc_free(lib_path);
free(lib_path);
} }
// If we get here, we failed to find anything. // If we get here, we failed to find anything.
} }
return false; return false;
} }
Find_Result find_visual_studio_and_windows_sdk() {
Find_Result result = {};
find_windows_kit_root(&result);
if (result.windows_sdk_root) {
if (build_context.metrics.arch == TargetArch_amd64) {
result.windows_sdk_um_library_path = concat(result.windows_sdk_root, L"um\\x64\\");
result.windows_sdk_ucrt_library_path = concat(result.windows_sdk_root, L"ucrt\\x64\\");
} else if (build_context.metrics.arch == TargetArch_i386) {
result.windows_sdk_um_library_path = concat(result.windows_sdk_root, L"um\\x86\\");
result.windows_sdk_ucrt_library_path = concat(result.windows_sdk_root, L"ucrt\\x86\\");
}
}
bool ok = find_visual_studio_by_fighting_through_microsoft_craziness(&result);
if (!ok) {
result.vs_exe_path = concat(L"", L"");
result.vs_library_path = concat(L"", L"");
}
return result;
}
String mc_wstring_to_string(wchar_t const *str) {
return string16_to_string(mc_allocator, make_string16_c(str));
}
Find_Result_Utf8 find_visual_studio_and_windows_sdk_utf8() { Find_Result_Utf8 find_visual_studio_and_windows_sdk_utf8() {
Find_Result result = find_visual_studio_and_windows_sdk();
defer (free_resources(&result));
Find_Result_Utf8 r = {}; Find_Result_Utf8 r = {};
r.windows_sdk_version = result.windows_sdk_version; find_windows_kit_root(&r);
r.windows_sdk_root = mc_wstring_to_string(result.windows_sdk_root); if (r.windows_sdk_root.len > 0) {
r.windows_sdk_um_library_path = mc_wstring_to_string(result.windows_sdk_um_library_path); if (build_context.metrics.arch == TargetArch_amd64) {
r.windows_sdk_ucrt_library_path = mc_wstring_to_string(result.windows_sdk_ucrt_library_path); r.windows_sdk_um_library_path = mc_concat(r.windows_sdk_root, str_lit("um\\x64\\"));
r.vs_exe_path = mc_wstring_to_string(result.vs_exe_path); r.windows_sdk_ucrt_library_path = mc_concat(r.windows_sdk_root, str_lit("ucrt\\x64\\"));
r.vs_library_path = mc_wstring_to_string(result.vs_library_path); } else if (build_context.metrics.arch == TargetArch_i386) {
r.windows_sdk_um_library_path = mc_concat(r.windows_sdk_root, str_lit("um\\x86\\"));
r.windows_sdk_ucrt_library_path = mc_concat(r.windows_sdk_root, str_lit("ucrt\\x86\\"));
}
}
find_visual_studio_by_fighting_through_microsoft_craziness(&r);
#if 0 #if 0
printf("windows_sdk_root: %.*s\n", LIT(r.windows_sdk_root)); printf("windows_sdk_root: %.*s\n", LIT(r.windows_sdk_root));
@@ -619,4 +577,3 @@ Find_Result_Utf8 find_visual_studio_and_windows_sdk_utf8() {
return r; return r;
} }
+1 -1
View File
@@ -226,7 +226,7 @@ foreign stbtt {
// (.ttf) files only contain one font. The number of fonts can be used for // (.ttf) files only contain one font. The number of fonts can be used for
// indexing with the previous function where the index is between zero and one // indexing with the previous function where the index is between zero and one
// less than the total fonts. If an error occurs, -1 is returned. // less than the total fonts. If an error occurs, -1 is returned.
GetNumberOfFonts :: proc(data: [^]byte) -> b32 --- GetNumberOfFonts :: proc(data: [^]byte) -> c.int ---
// Each .ttf/.ttc file may have more than one font. Each font has a sequential // Each .ttf/.ttc file may have more than one font. Each font has a sequential
// index number starting from 0. Call this function to get the font offset for // index number starting from 0. Call this function to get the font offset for