Update microsoft_craziness.h to work correctly with the rest of the codebase (and not use WIN32_LEAN_AND_MEAN)

This commit is contained in:
gingerBill
2019-11-27 15:18:32 +00:00
parent 5ea9fc3fb0
commit 37e3e081c6
5 changed files with 158 additions and 130 deletions
+7
View File
@@ -7,6 +7,13 @@
#include <intrin.h>
#endif
#if defined(GB_SYSTEM_WINDOWS)
#define NOMINMAX 1
#include <windows.h>
#undef NOMINMAX
#endif
#define GB_WINDOWS_H_INCLUDED
#define GB_IMPLEMENTATION
#include "gb/gb.h"
+4
View File
@@ -291,14 +291,18 @@ extern "C" {
#if defined(GB_SYSTEM_WINDOWS)
#if !defined(GB_NO_WINDOWS_H)
#define NOMINMAX 1
#if !defined(GB_WINDOWS_H_INCLUDED)
#define WIN32_LEAN_AND_MEAN 1
#define WIN32_MEAN_AND_LEAN 1
#define VC_EXTRALEAN 1
#endif
#include <windows.h>
#undef NOMINMAX
#if !defined(GB_WINDOWS_H_INCLUDED)
#undef WIN32_LEAN_AND_MEAN
#undef WIN32_MEAN_AND_LEAN
#undef VC_EXTRALEAN
#endif
#endif
#include <malloc.h> // NOTE(bill): _aligned_*()
+17 -12
View File
@@ -1179,25 +1179,30 @@ int main(int arg_count, char **arg_ptr) {
}
// 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 result = find_visual_studio_and_windows_sdk_utf8();
defer(free_resources(&result));
Find_Result_Utf8 find_result = find_visual_studio_and_windows_sdk_utf8();
// defer(free_resources(&find_result));
if (result.windows_sdk_version == 0) {
if (find_result.windows_sdk_version == 0) {
gb_printf_err("Windows SDK not found.\n");
return 1;
}
// Add library search paths.
link_settings = gb_string_append_fmt(link_settings, " /LIBPATH:\"%s\"", result.windows_sdk_um_library_path);
link_settings = gb_string_append_fmt(link_settings, " /LIBPATH:\"%s\"", result.windows_sdk_ucrt_library_path);
link_settings = gb_string_append_fmt(link_settings, " /LIBPATH:\"%s\"", result.vs_library_path);
if (find_result.vs_library_path.len > 0) {
GB_ASSERT(find_result.windows_sdk_um_library_path.len > 0);
GB_ASSERT(find_result.windows_sdk_ucrt_library_path.len > 0);
link_settings = gb_string_append_fmt(link_settings, " /LIBPATH:\"%.*s\"", LIT(find_result.vs_library_path));
link_settings = gb_string_append_fmt(link_settings, " /LIBPATH:\"%.*s\"", LIT(find_result.windows_sdk_um_library_path));
link_settings = gb_string_append_fmt(link_settings, " /LIBPATH:\"%.*s\"", LIT(find_result.windows_sdk_ucrt_library_path));
}
if (!build_context.use_lld) { // msvc
if (build_context.has_resource) {
exit_code = system_exec_command_line_app("msvc-link",
"\"%s\\rc.exe\" /nologo /fo \"%.*s.res\" \"%.*s.rc\"",
result.vs_exe_path,
"\"%.*src.exe\" /nologo /fo \"%.*s.res\" \"%.*s.rc\"",
LIT(find_result.vs_exe_path),
LIT(output_base),
LIT(build_context.resource_filepath)
);
@@ -1207,23 +1212,23 @@ int main(int arg_count, char **arg_ptr) {
}
exit_code = system_exec_command_line_app("msvc-link",
"\"%s\\link.exe\" \"%.*s.obj\" \"%.*s.res\" -OUT:\"%.*s.%s\" %s "
"\"%.*slink.exe\" \"%.*s.obj\" \"%.*s.res\" -OUT:\"%.*s.%s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:CONSOLE "
" %.*s "
" %s "
"",
result.vs_exe_path, LIT(output_base), LIT(output_base), LIT(output_base), output_ext,
LIT(find_result.vs_exe_path), LIT(output_base), LIT(output_base), LIT(output_base), output_ext,
lib_str, LIT(build_context.link_flags),
link_settings
);
} else {
exit_code = system_exec_command_line_app("msvc-link",
"\"%s\\link.exe\" \"%.*s.obj\" -OUT:\"%.*s.%s\" %s "
"\"%.*slink.exe\" \"%.*s.obj\" -OUT:\"%.*s.%s\" %s "
"/nologo /incremental:no /opt:ref /subsystem:CONSOLE "
" %.*s "
" %s "
"",
result.vs_exe_path, LIT(output_base), LIT(output_base), output_ext,
LIT(find_result.vs_exe_path), LIT(output_base), LIT(output_base), output_ext,
lib_str, LIT(build_context.link_flags),
link_settings
);
+85 -90
View File
@@ -13,14 +13,7 @@
//
//#include <windows.h>
//#include <stdlib.h>
//#include <string.h>
//#include <assert.h>
//#include <stdio.h>
//#include <sys/stat.h>
//#include <stdint.h>
#include <wtypesbase.h>
#include <io.h> // For _get_osfhandle
#pragma comment(lib, "Advapi32.lib")
@@ -53,26 +46,29 @@
// Here is the API you need to know about:
//
gb_global gbAllocator mc_allocator = heap_allocator();
struct Find_Result {
int windows_sdk_version; // Zero if no Windows SDK found.
wchar_t *windows_sdk_root = NULL;
wchar_t *windows_sdk_um_library_path = NULL;
wchar_t *windows_sdk_ucrt_library_path = NULL;
wchar_t *windows_sdk_root;
wchar_t *windows_sdk_um_library_path;
wchar_t *windows_sdk_ucrt_library_path;
wchar_t *vs_exe_path = NULL;
wchar_t *vs_library_path = NULL;
wchar_t *vs_exe_path;
wchar_t *vs_library_path;
};
struct Find_Result_Utf8 {
int windows_sdk_version; // Zero if no Windows SDK found.
char *windows_sdk_root = NULL;
char *windows_sdk_um_library_path = NULL;
char *windows_sdk_ucrt_library_path = NULL;
String windows_sdk_root;
String windows_sdk_um_library_path;
String windows_sdk_ucrt_library_path;
char *vs_exe_path = NULL;
char *vs_library_path = NULL;
String vs_exe_path;
String vs_library_path;
};
@@ -80,19 +76,20 @@ Find_Result find_visual_studio_and_windows_sdk();
Find_Result_Utf8 find_visual_studio_and_windows_sdk_utf8();
void free_resources(Find_Result *result) {
free(result->windows_sdk_root);
free(result->windows_sdk_um_library_path);
free(result->windows_sdk_ucrt_library_path);
free(result->vs_exe_path);
free(result->vs_library_path);
// free(result->windows_sdk_root);
// 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) {
free(result->windows_sdk_root);
free(result->windows_sdk_um_library_path);
free(result->windows_sdk_ucrt_library_path);
free(result->vs_exe_path);
free(result->vs_library_path);
// gbAllocator a = heap_allocator();
// 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);
}
@@ -150,18 +147,6 @@ typedef WCHAR* BSTR;
typedef const WCHAR* LPCOLESTR;
struct DECLSPEC_UUID("00000000-0000-0000-C000-000000000046") IUnknown
{
virtual HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void **ppvObject) = 0;
virtual ULONG STDMETHODCALLTYPE AddRef(void) = 0;
virtual ULONG STDMETHODCALLTYPE Release(void) = 0;
};
extern "C" HRESULT STDAPICALLTYPE CoInitialize(LPVOID pvReserved);
extern "C" HRESULT STDAPICALLTYPE CoCreateInstance(REFCLSID rclsid, IUnknown * pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID * ppv);
extern "C" void STDAPICALLTYPE SysFreeString(BSTR bstrString);
struct DECLSPEC_UUID("B41463C3-8866-43B5-BC33-2B0676F7F42E") DECLSPEC_NOVTABLE ISetupInstance : public IUnknown
{
virtual HRESULT STDMETHODCALLTYPE GetInstanceId(BSTR* pbstrInstanceId) = 0;
@@ -193,7 +178,7 @@ struct DECLSPEC_UUID("42843719-DB4C-46C2-8E7C-64F1816EFD5B") DECLSPEC_NOVTABLE I
// The beginning of the actual code that does things.
struct Version_Data {
int32_t 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 *best_name;
};
@@ -212,21 +197,17 @@ wchar_t *concat(wchar_t *a, wchar_t *b, wchar_t *c = nullptr, wchar_t *d = nullp
// If you don't like that, use a programming language that actually
// helps you with using custom allocators. Or just edit the code.
auto len_a = wcslen(a);
auto len_b = wcslen(b);
size_t len_c = 0;
if (c) len_c = wcslen(c);
size_t len_d = 0;
if (d) len_d = wcslen(d);
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 *)malloc((len_a + len_b + len_c + len_d + 1) * 2);
memcpy(result, a, len_a*2);
memcpy(result + len_a, b, len_b*2);
gb_memmove(result, a, len_a*2);
gb_memmove(result + len_a, b, len_b*2);
if (c) memcpy(result + len_a + len_b, c, len_c * 2);
if (d) memcpy(result + len_a + len_b + len_c, d, len_d * 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;
@@ -361,16 +342,20 @@ void find_windows_kit_root(Find_Result *result) {
// Look for a Windows 10 entry.
auto windows10_root = find_windows_kit_root(main_key, L"KitsRoot10");
if (windows10_root) {
defer (free(windows10_root));
Version_Data data = {0};
auto windows10_lib = concat(windows10_root, L"Lib");
defer (free(windows10_lib));
visit_files_w(windows10_lib, &data, win10_best);
if (data.best_name) {
result->windows_sdk_version = 10;
result->windows_sdk_root = data.best_name;
result->windows_sdk_root = concat(data.best_name, L"\\");
return;
}
}
@@ -388,7 +373,7 @@ void find_windows_kit_root(Find_Result *result) {
visit_files_w(windows8_lib, &data, win8_best);
if (data.best_name) {
result->windows_sdk_version = 8;
result->windows_sdk_root = data.best_name;
result->windows_sdk_root = concat(data.best_name, L"\\");
return;
}
}
@@ -397,7 +382,7 @@ void find_windows_kit_root(Find_Result *result) {
}
void find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *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
// to fight through Microsoft craziness. The things that the fine
// Visual Studio team want you to do, JUST TO FIND A SINGLE FOLDER
@@ -415,24 +400,25 @@ void find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *res
auto rc = CoInitialize(NULL);
// "Subsequent valid calls return false." So ignore false.
// if rc != S_OK return false;
if (rc != S_OK) return false;
GUID my_uid = {0x42843719, 0xDB4C, 0x46C2, {0x8E, 0x7C, 0x64, 0xF1, 0x81, 0x6E, 0xFD, 0x5B}};
GUID CLSID_SetupConfiguration = {0x177F0C4A, 0x1CD3, 0x4DE7, {0xA3, 0x2C, 0x71, 0xDB, 0xBB, 0x9F, 0xA3, 0x6D}};
DWORD CLSCTX_INPROC_SERVER = 0x1;
ISetupConfiguration *config = NULL;
auto hr = CoCreateInstance(CLSID_SetupConfiguration, NULL, CLSCTX_INPROC_SERVER, my_uid, (void **)&config);
if (hr != 0) return;
HRESULT hr = 0;
hr = CoCreateInstance(CLSID_SetupConfiguration, NULL, CLSCTX_INPROC_SERVER, my_uid, (void **)&config);
if (hr != 0) return false;
defer (config->Release());
IEnumSetupInstances *instances = NULL;
hr = config->EnumInstances(&instances);
if (hr != 0) return;
if (!instances) return;
if (hr != 0) return false;
if (!instances) return false;
defer (instances->Release());
while (1) {
for (;;) {
ULONG found = 0;
ISetupInstance *instance = NULL;
auto hr = instances->Next(1, &instance, &found);
@@ -471,14 +457,14 @@ void find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *res
auto version_tail = wcschr(version, '\n');
if (version_tail) *version_tail = 0; // Stomp the data, because nobody cares about it.
auto library_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\lib\\x64");
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!
auto library_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\lib\\x64\\");
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!
if (os_file_exists(library_file)) {
auto link_exe_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\bin\\Hostx64\\x64");
auto link_exe_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\bin\\Hostx64\\x64\\");
result->vs_exe_path = link_exe_path;
result->vs_library_path = library_path;
return;
return true;
}
/*
@@ -495,7 +481,7 @@ void find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *res
HKEY vs7_key;
rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7", 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &vs7_key);
if (rc != S_OK) return;
if (rc != S_OK) return false;
defer (RegCloseKey(vs7_key));
// Hardcoded search for 4 prior Visual Studio versions. Is there something better to do here?
@@ -514,7 +500,7 @@ void find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *res
}
auto buffer = (wchar_t *)malloc(cb_data);
if (!buffer) return;
if (!buffer) return false;
defer (free(buffer));
rc = RegQueryValueExW(vs7_key, v, NULL, NULL, (LPBYTE)buffer, &cb_data);
@@ -531,55 +517,64 @@ void find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *res
if (os_file_exists(vcruntime_filename)) {
result->vs_exe_path = concat(buffer, L"VC\\bin");
result->vs_library_path = lib_path;
return;
return true;
}
free(lib_path);
}
// If we get here, we failed to find anything.
return false;
}
Find_Result find_visual_studio_and_windows_sdk() {
Find_Result result;
Find_Result result = {};
find_windows_kit_root(&result);
if (result.windows_sdk_root) {
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");
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\\");
}
find_visual_studio_by_fighting_through_microsoft_craziness(&result);
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;
}
static char * ucs2_to_utf8(wchar_t * wstr) {
int len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL) + 1;
char * str = (char*)malloc(len);
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
return str;
String mc_wstring_to_string(wchar_t *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;
r.windows_sdk_root = ucs2_to_utf8(result.windows_sdk_root);
r.windows_sdk_um_library_path = ucs2_to_utf8(result.windows_sdk_um_library_path);
r.windows_sdk_ucrt_library_path = ucs2_to_utf8(result.windows_sdk_ucrt_library_path);
r.vs_exe_path = ucs2_to_utf8(result.vs_exe_path);
r.vs_library_path = ucs2_to_utf8(result.vs_library_path);
r.windows_sdk_root = mc_wstring_to_string(result.windows_sdk_root);
r.windows_sdk_um_library_path = mc_wstring_to_string(result.windows_sdk_um_library_path);
r.windows_sdk_ucrt_library_path = mc_wstring_to_string(result.windows_sdk_ucrt_library_path);
r.vs_exe_path = mc_wstring_to_string(result.vs_exe_path);
r.vs_library_path = mc_wstring_to_string(result.vs_library_path);
// gb_printf("%.*s\n", LIT(r.windows_sdk_root));
// gb_printf("%.*s\n", LIT(r.windows_sdk_um_library_path));
// gb_printf("%.*s\n", LIT(r.windows_sdk_ucrt_library_path));
// gb_printf("%.*s\n", LIT(r.vs_exe_path));
// gb_printf("%.*s\n", LIT(r.vs_library_path));
// gb_exit(1);
return r;
}
+17
View File
@@ -207,6 +207,23 @@ gb_inline bool string_ends_with(String const &s, String const &suffix) {
return substring(s, s.len-suffix.len, s.len) == suffix;
}
gb_inline bool string_starts_with(String const &s, u8 prefix) {
if (1 > s.len) {
return false;
}
return s[0] == prefix;
}
gb_inline bool string_ends_with(String const &s, u8 suffix) {
if (1 > s.len) {
return false;
}
return s[s.len-1] == suffix;
}
gb_inline isize string_extension_position(String const &str) {
isize dot_pos = -1;
isize i = str.len;