VSSupport.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. //
  2. // Author: Jonathan Blow
  3. // Version: 1
  4. // Date: 31 August, 2018
  5. //
  6. // This code is released under the MIT license, which you can find at
  7. //
  8. // https://opensource.org/licenses/MIT
  9. //
  10. //
  11. //
  12. // See the comments for how to use this library just below the includes.
  13. //
  14. #include <windows.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <assert.h>
  18. #include <stdio.h>
  19. #include <sys/stat.h>
  20. #include <stdint.h>
  21. #include <io.h> // For _get_osfhandle
  22. #undef max
  23. #undef min
  24. #include "BeefySysLib/Common.h"
  25. #include "Beef/BfCommon.h"
  26. //
  27. // HOW TO USE THIS CODE
  28. //
  29. // The purpose of this file is to find the folders that contain libraries
  30. // you may need to link against, on Windows, if you are linking with any
  31. // compiled C or C++ code. This will be necessary for many non-C++ programming
  32. // language environments that want to provide compatibility.
  33. //
  34. // We find the place where the Visual Studio libraries live (for example,
  35. // libvcruntime.lib), where the linker and compiler executables live
  36. // (for example, link.exe), and where the Windows SDK libraries reside
  37. // (kernel32.lib, libucrt.lib).
  38. //
  39. // We all wish you didn't have to worry about so many weird dependencies,
  40. // but we don't really have a choice about this, sadly.
  41. //
  42. // I don't claim that this is the absolute best way to solve this problem,
  43. // and so far we punt on things (if you have multiple versions of Visual Studio
  44. // installed, we return the first one, rather than the newest). But it
  45. // will solve the basic problem for you as simply as I know how to do it,
  46. // and because there isn't too much code here, it's easy to modify and expand.
  47. //
  48. //
  49. // Here is the API you need to know about:
  50. //
  51. struct Find_Result {
  52. int windows_sdk_version = 0; // Zero if no Windows SDK found.
  53. wchar_t *windows_sdk_root = NULL;
  54. wchar_t* vs_version = NULL;
  55. wchar_t *vs_exe32_path = NULL;
  56. wchar_t *vs_exe64_path = NULL;
  57. wchar_t *vs_library32_path = NULL;
  58. wchar_t *vs_library64_path = NULL;
  59. };
  60. Find_Result find_visual_studio_and_windows_sdk();
  61. void free_resources(Find_Result *result) {
  62. free(result->windows_sdk_root);
  63. free(result->vs_version);
  64. free(result->vs_exe32_path);
  65. free(result->vs_exe64_path);
  66. free(result->vs_library32_path);
  67. free(result->vs_library64_path);
  68. }
  69. //
  70. // Call find_visual_studio_and_windows_sdk, look at the resulting
  71. // paths, then call free_resources on the result.
  72. //
  73. // Everything else in this file is implementation details that you
  74. // don't need to care about.
  75. //
  76. //
  77. // This file was about 400 lines before we started adding these comments.
  78. // You might think that's way too much code to do something as simple
  79. // as finding a few library and executable paths. I agree. However,
  80. // Microsoft's own solution to this problem, called "vswhere", is a
  81. // mere EIGHT THOUSAND LINE PROGRAM, spread across 70 files,
  82. // that they posted to github *unironically*.
  83. //
  84. // I am not making this up: https://github.com/Microsoft/vswhere
  85. //
  86. // Several people have therefore found the need to solve this problem
  87. // themselves. We referred to some of these other solutions when
  88. // figuring out what to do, most prominently ziglang's version,
  89. // by Ryan Saunderson.
  90. //
  91. // I hate this kind of code. The fact that we have to do this at all
  92. // is stupid, and the actual maneuvers we need to go through
  93. // are just painful. If programming were like this all the time,
  94. // I would quit.
  95. //
  96. // Because this is such an absurd waste of time, I felt it would be
  97. // useful to package the code in an easily-reusable way, in the
  98. // style of the stb libraries. We haven't gone as all-out as some
  99. // of the stb libraries do (which compile in C with no includes, often).
  100. // For this version you need C++ and the headers at the top of the file.
  101. //
  102. // We return the strings as Windows wide character strings. Aesthetically
  103. // I don't like that (I think most sane programs are UTF-8 internally),
  104. // but apparently, not all valid Windows file paths can even be converted
  105. // correctly to UTF-8. So have fun with that. It felt safest and simplest
  106. // to stay with wchar_t since all of this code is fully ensconced in
  107. // Windows crazy-land.
  108. //
  109. // One other shortcut I took is that this is hardcoded to return the
  110. // folders for x64 libraries. If you want x86 or arm, you can make
  111. // slight edits to the code below, or, if enough people want this,
  112. // I can work it in here.
  113. //
  114. // Defer macro/thing.
  115. #undef defer
  116. #define CONCAT_INTERNAL(x,y) x##y
  117. #define CONCAT(x,y) CONCAT_INTERNAL(x,y)
  118. template<typename T>
  119. struct ExitScope {
  120. T lambda;
  121. ExitScope(T lambda) :lambda(lambda) {}
  122. ~ExitScope() { lambda(); }
  123. ExitScope(const ExitScope&);
  124. private:
  125. ExitScope& operator =(const ExitScope&);
  126. };
  127. class ExitScopeHelp {
  128. public:
  129. template<typename T>
  130. ExitScope<T> operator+(T t) { return t; }
  131. };
  132. #define defer const auto& CONCAT(defer__, __LINE__) = ExitScopeHelp() + [&]()
  133. // COM objects for the ridiculous Microsoft craziness.
  134. struct DECLSPEC_UUID("B41463C3-8866-43B5-BC33-2B0676F7F42E") DECLSPEC_NOVTABLE ISetupInstance : public IUnknown
  135. {
  136. STDMETHOD(GetInstanceId)(_Out_ BSTR* pbstrInstanceId) = 0;
  137. STDMETHOD(GetInstallDate)(_Out_ LPFILETIME pInstallDate) = 0;
  138. STDMETHOD(GetInstallationName)(_Out_ BSTR* pbstrInstallationName) = 0;
  139. STDMETHOD(GetInstallationPath)(_Out_ BSTR* pbstrInstallationPath) = 0;
  140. STDMETHOD(GetInstallationVersion)(_Out_ BSTR* pbstrInstallationVersion) = 0;
  141. STDMETHOD(GetDisplayName)(_In_ LCID lcid, _Out_ BSTR* pbstrDisplayName) = 0;
  142. STDMETHOD(GetDescription)(_In_ LCID lcid, _Out_ BSTR* pbstrDescription) = 0;
  143. STDMETHOD(ResolvePath)(_In_opt_z_ LPCOLESTR pwszRelativePath, _Out_ BSTR* pbstrAbsolutePath) = 0;
  144. };
  145. struct DECLSPEC_UUID("6380BCFF-41D3-4B2E-8B2E-BF8A6810C848") DECLSPEC_NOVTABLE IEnumSetupInstances : public IUnknown
  146. {
  147. STDMETHOD(Next)(_In_ ULONG celt, _Out_writes_to_(celt, *pceltFetched) ISetupInstance** rgelt, _Out_opt_ _Deref_out_range_(0, celt) ULONG* pceltFetched) = 0;
  148. STDMETHOD(Skip)(_In_ ULONG celt) = 0;
  149. STDMETHOD(Reset)(void) = 0;
  150. STDMETHOD(Clone)(_Deref_out_opt_ IEnumSetupInstances** ppenum) = 0;
  151. };
  152. struct DECLSPEC_UUID("42843719-DB4C-46C2-8E7C-64F1816EFD5B") DECLSPEC_NOVTABLE ISetupConfiguration : public IUnknown
  153. {
  154. STDMETHOD(EnumInstances)(_Out_ IEnumSetupInstances** ppEnumInstances) = 0;
  155. STDMETHOD(GetInstanceForCurrentProcess)(_Out_ ISetupInstance** ppInstance) = 0;
  156. STDMETHOD(GetInstanceForPath)(_In_z_ LPCWSTR wzPath, _Out_ ISetupInstance** ppInstance) = 0;
  157. };
  158. // The beginning of the actual code that does things.
  159. struct Version_Data {
  160. int32_t best_version[4]; // For Windows 8 versions, only two of these numbers are used.
  161. wchar_t *best_name;
  162. };
  163. bool os_file_exists(wchar_t *name) {
  164. // @Robustness: What flags do we really want to check here?
  165. auto attrib = GetFileAttributesW(name);
  166. if (attrib == INVALID_FILE_ATTRIBUTES) return false;
  167. if (attrib & FILE_ATTRIBUTE_DIRECTORY) return false;
  168. return true;
  169. }
  170. wchar_t *concat(wchar_t *a, wchar_t *b, wchar_t *c = nullptr, wchar_t *d = nullptr) {
  171. // Concatenate up to 4 wide strings together. Allocated with malloc.
  172. // If you don't like that, use a programming language that actually
  173. // helps you with using custom allocators. Or just edit the code.
  174. auto len_a = wcslen(a);
  175. auto len_b = wcslen(b);
  176. auto len_c = 0;
  177. if (c) len_c = (int)wcslen(c);
  178. auto len_d = 0;
  179. if (d) len_d = (int)wcslen(d);
  180. wchar_t *result = (wchar_t *)malloc((len_a + len_b + len_c + len_d + 1) * 2);
  181. memcpy(result, a, len_a * 2);
  182. memcpy(result + len_a, b, len_b * 2);
  183. if (c) memcpy(result + len_a + len_b, c, len_c * 2);
  184. if (d) memcpy(result + len_a + len_b + len_c, d, len_d * 2);
  185. result[len_a + len_b + len_c + len_d] = 0;
  186. return result;
  187. }
  188. typedef void(*Visit_Proc_W)(wchar_t *short_name, wchar_t *full_name, Version_Data *data);
  189. bool visit_files_w(wchar_t *dir_name, Version_Data *data, Visit_Proc_W proc) {
  190. // Visit everything in one folder (non-recursively). If it's a directory
  191. // that doesn't start with ".", call the visit proc on it. The visit proc
  192. // will see if the filename conforms to the expected versioning pattern.
  193. auto wildcard_name = concat(dir_name, L"\\*");
  194. defer{ free(wildcard_name); };
  195. WIN32_FIND_DATAW find_data;
  196. auto handle = FindFirstFileW(wildcard_name, &find_data);
  197. if (handle == INVALID_HANDLE_VALUE) return false;
  198. while (true) {
  199. if ((find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (find_data.cFileName[0] != '.')) {
  200. auto full_name = concat(dir_name, L"\\", find_data.cFileName);
  201. defer{ free(full_name); };
  202. proc(find_data.cFileName, full_name, data);
  203. }
  204. auto success = FindNextFileW(handle, &find_data);
  205. if (!success) break;
  206. }
  207. FindClose(handle);
  208. return true;
  209. }
  210. wchar_t *find_windows_kit_root(HKEY key, wchar_t *version) {
  211. // Given a key to an already opened registry entry,
  212. // get the value stored under the 'version' subkey.
  213. // If that's not the right terminology, hey, I never do registry stuff.
  214. DWORD required_length;
  215. auto rc = RegQueryValueExW(key, version, NULL, NULL, NULL, &required_length);
  216. if (rc != 0) return NULL;
  217. DWORD length = required_length + sizeof(wchar_t); // The extra wchar_t is for the maybe optional zero later on. Probably we are over-allocating.
  218. wchar_t *value = (wchar_t *)malloc(length);
  219. if (!value) return NULL;
  220. rc = RegQueryValueExW(key, version, NULL, NULL, (LPBYTE)value, &length); // We know that version is zero-terminated...
  221. if (rc != 0) return NULL;
  222. length /= sizeof(wchar_t);
  223. // The documentation says that if the string for some reason was not stored
  224. // with zero-termination, we need to manually terminate it. Sigh!!
  225. if (value[length - 1]) {
  226. value[length] = 0;
  227. }
  228. return value;
  229. }
  230. void win10_best(wchar_t *short_name, wchar_t *full_name, Version_Data *data) {
  231. // Find the Windows 10 subdirectory with the highest version number.
  232. int i0, i1, i2, i3;
  233. auto success = swscanf_s(short_name, L"%d.%d.%d.%d", &i0, &i1, &i2, &i3);
  234. if (success < 4) return;
  235. if (i0 < data->best_version[0]) return;
  236. else if (i0 == data->best_version[0]) {
  237. if (i1 < data->best_version[1]) return;
  238. else if (i1 == data->best_version[1]) {
  239. if (i2 < data->best_version[2]) return;
  240. else if (i2 == data->best_version[2]) {
  241. if (i3 < data->best_version[3]) return;
  242. }
  243. }
  244. }
  245. // we have to copy_string and free here because visit_files free's the full_name string
  246. // after we execute this function, so Win*_Data would contain an invalid pointer.
  247. if (data->best_name) free(data->best_name);
  248. data->best_name = _wcsdup(full_name);
  249. if (data->best_name) {
  250. data->best_version[0] = i0;
  251. data->best_version[1] = i1;
  252. data->best_version[2] = i2;
  253. data->best_version[3] = i3;
  254. }
  255. }
  256. void win8_best(wchar_t *short_name, wchar_t *full_name, Version_Data *data) {
  257. // Find the Windows 8 subdirectory with the highest version number.
  258. int i0, i1;
  259. auto success = swscanf_s(short_name, L"winv%d.%d", &i0, &i1);
  260. if (success < 2) return;
  261. if (i0 < data->best_version[0]) return;
  262. else if (i0 == data->best_version[0]) {
  263. if (i1 < data->best_version[1]) return;
  264. }
  265. // we have to copy_string and free here because visit_files free's the full_name string
  266. // after we execute this function, so Win*_Data would contain an invalid pointer.
  267. if (data->best_name) free(data->best_name);
  268. data->best_name = _wcsdup(full_name);
  269. if (data->best_name) {
  270. data->best_version[0] = i0;
  271. data->best_version[1] = i1;
  272. }
  273. }
  274. void find_windows_kit_root(Find_Result *result) {
  275. // Information about the Windows 10 and Windows 8 development kits
  276. // is stored in the same place in the registry. We open a key
  277. // to that place, first checking preferentially for a Windows 10 kit,
  278. // then, if that's not found, a Windows 8 kit.
  279. HKEY main_key;
  280. auto rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows Kits\\Installed Roots",
  281. 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY | KEY_ENUMERATE_SUB_KEYS, &main_key);
  282. if (rc != S_OK) return;
  283. defer{ RegCloseKey(main_key); };
  284. // Look for a Windows 10 entry.
  285. auto windows10_root = find_windows_kit_root(main_key, L"KitsRoot10");
  286. if (windows10_root) {
  287. defer{ free(windows10_root); };
  288. Version_Data data = { 0 };
  289. auto windows10_lib = concat(windows10_root, L"Lib");
  290. defer{ free(windows10_lib); };
  291. visit_files_w(windows10_lib, &data, win10_best);
  292. if (data.best_name) {
  293. result->windows_sdk_version = 10;
  294. result->windows_sdk_root = data.best_name;
  295. return;
  296. }
  297. }
  298. // Look for a Windows 8 entry.
  299. auto windows8_root = find_windows_kit_root(main_key, L"KitsRoot81");
  300. if (windows8_root) {
  301. defer{ free(windows8_root); };
  302. auto windows8_lib = concat(windows8_root, L"Lib");
  303. defer{ free(windows8_lib); };
  304. Version_Data data = { 0 };
  305. visit_files_w(windows8_lib, &data, win8_best);
  306. if (data.best_name) {
  307. result->windows_sdk_version = 8;
  308. result->windows_sdk_root = data.best_name;
  309. return;
  310. }
  311. }
  312. // If we get here, we failed to find anything.
  313. }
  314. void find_visual_studio_by_fighting_through_microsoft_craziness(Find_Result *result) {
  315. // The name of this procedure is kind of cryptic. Its purpose is
  316. // to fight through Microsoft craziness. The things that the fine
  317. // Visual Studio team want you to do, JUST TO FIND A SINGLE FOLDER
  318. // THAT EVERYONE NEEDS TO FIND, are ridiculous garbage.
  319. // For earlier versions of Visual Studio, you'd find this information in the registry,
  320. // similarly to the Windows Kits above. But no, now it's the future, so to ask the
  321. // question "Where is the Visual Studio folder?" you have to do a bunch of COM object
  322. // instantiation, enumeration, and querying. (For extra bonus points, try doing this in
  323. // a new, underdeveloped programming language where you don't have COM routines up
  324. // and running yet. So fun.)
  325. //
  326. // If all this COM object instantiation, enumeration, and querying doesn't give us
  327. // a useful result, we drop back to the registry-checking method.
  328. auto rc = CoInitialize(NULL);
  329. // "Subsequent valid calls return false." So ignore false.
  330. // if rc != S_OK return false;
  331. GUID my_uid = { 0x42843719, 0xDB4C, 0x46C2, {0x8E, 0x7C, 0x64, 0xF1, 0x81, 0x6E, 0xFD, 0x5B} };
  332. GUID CLSID_SetupConfiguration = { 0x177F0C4A, 0x1CD3, 0x4DE7, {0xA3, 0x2C, 0x71, 0xDB, 0xBB, 0x9F, 0xA3, 0x6D} };
  333. ISetupConfiguration *config = NULL;
  334. auto hr = CoCreateInstance(CLSID_SetupConfiguration, NULL, CLSCTX_INPROC_SERVER, my_uid, (void **)&config);
  335. if (hr != 0) return;
  336. defer{ config->Release(); };
  337. IEnumSetupInstances *instances = NULL;
  338. hr = config->EnumInstances(&instances);
  339. if (hr != 0) return;
  340. if (!instances) return;
  341. defer{ instances->Release(); };
  342. while (1) {
  343. ULONG found = 0;
  344. ISetupInstance *instance = NULL;
  345. auto hr = instances->Next(1, &instance, &found);
  346. if (hr != S_OK) break;
  347. defer{ instance->Release(); };
  348. BSTR bstr_inst_path;
  349. hr = instance->GetInstallationPath(&bstr_inst_path);
  350. if (hr != S_OK) continue;
  351. defer{ SysFreeString(bstr_inst_path); };
  352. auto tools_filename = concat(bstr_inst_path, L"\\VC\\Auxiliary\\Build\\Microsoft.VCToolsVersion.default.txt");
  353. defer{ free(tools_filename); };
  354. FILE *f = nullptr;
  355. auto open_result = _wfopen_s(&f, tools_filename, L"rt");
  356. if (open_result != 0) continue;
  357. if (!f) continue;
  358. defer{ fclose(f); };
  359. LARGE_INTEGER tools_file_size;
  360. auto file_handle = (HANDLE)_get_osfhandle(_fileno(f));
  361. BOOL success = GetFileSizeEx(file_handle, &tools_file_size);
  362. if (!success) continue;
  363. 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).
  364. wchar_t *version = (wchar_t *)malloc(version_bytes);
  365. auto read_result = fgetws(version, (int)version_bytes, f);
  366. if (!read_result) continue;
  367. auto version_tail = wcschr(version, '\n');
  368. if (version_tail) *version_tail = 0; // Stomp the data, because nobody cares about it.
  369. auto library32_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\lib\\x86");
  370. auto library64_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\lib\\x64");
  371. auto library_file = concat(library32_path, L"\\vcruntime.lib"); // @Speed: Could have library_path point to this string, with a smaller count, to save on memory flailing!
  372. auto vs_exe64_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\bin\\Hostx64\\x64");
  373. auto vs_exe64_link_path = concat(vs_exe64_path, L"\\link.exe");
  374. bool use = false;
  375. if ((os_file_exists(library_file)) && (os_file_exists(vs_exe64_link_path)))
  376. {
  377. if (result->vs_version == NULL)
  378. {
  379. use = true;
  380. }
  381. else if (wcscmp(version, result->vs_version) > 0)
  382. {
  383. use = true;
  384. }
  385. }
  386. if (use)
  387. {
  388. result->vs_version = version;
  389. result->vs_exe64_path = vs_exe64_path;
  390. result->vs_exe32_path = concat(bstr_inst_path, L"\\VC\\Tools\\MSVC\\", version, L"\\bin\\Hostx86\\x86");
  391. result->vs_library32_path = library32_path;
  392. result->vs_library64_path = library64_path;
  393. }
  394. else
  395. {
  396. free(version);
  397. free(vs_exe64_path);
  398. free(library32_path);
  399. free(library64_path);
  400. }
  401. free(library_file);
  402. free(vs_exe64_link_path);
  403. /*
  404. Ryan Saunderson said:
  405. "Clang uses the 'SetupInstance->GetInstallationVersion' / ISetupHelper->ParseVersion to find the newest version
  406. and then reads the tools file to define the tools path - which is definitely better than what i did."
  407. So... @Incomplete: Should probably pick the newest version...
  408. */
  409. }
  410. if (result->vs_exe64_path != NULL)
  411. return;
  412. // If we get here, we didn't find Visual Studio 2017. Try earlier versions.
  413. HKEY vs7_key;
  414. rc = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\VisualStudio\\SxS\\VS7", 0, KEY_QUERY_VALUE | KEY_WOW64_32KEY, &vs7_key);
  415. if (rc != S_OK) return;
  416. defer{ RegCloseKey(vs7_key); };
  417. // Hardcoded search for 4 prior Visual Studio versions. Is there something better to do here?
  418. wchar_t *versions[] = { L"14.0", L"12.0", L"11.0", L"10.0" };
  419. const int NUM_VERSIONS = sizeof(versions) / sizeof(versions[0]);
  420. for (int i = 0; i < NUM_VERSIONS; i++) {
  421. auto v = versions[i];
  422. DWORD dw_type;
  423. DWORD cb_data;
  424. auto rc = RegQueryValueExW(vs7_key, v, NULL, &dw_type, NULL, &cb_data);
  425. if ((rc == ERROR_FILE_NOT_FOUND) || (dw_type != REG_SZ)) {
  426. continue;
  427. }
  428. auto buffer = (wchar_t *)malloc(cb_data);
  429. if (!buffer) return;
  430. defer{ free(buffer); };
  431. rc = RegQueryValueExW(vs7_key, v, NULL, NULL, (LPBYTE)buffer, &cb_data);
  432. if (rc != 0) continue;
  433. // @Robustness: Do the zero-termination thing suggested in the RegQueryValue docs?
  434. auto lib32_path = concat(buffer, L"VC\\Lib\\x86");
  435. auto lib64_path = concat(buffer, L"VC\\Lib\\amd64");
  436. // Check to see whether a vcruntime.lib actually exists here.
  437. auto vcruntime_filename = concat(lib32_path, L"\\vcruntime.lib");
  438. defer{ free(vcruntime_filename); };
  439. if (os_file_exists(vcruntime_filename)) {
  440. result->vs_exe32_path = concat(buffer, L"VC\\bin");
  441. result->vs_exe64_path = concat(buffer, L"VC\\bin");
  442. result->vs_library32_path = lib32_path;
  443. result->vs_library64_path = lib64_path;
  444. return;
  445. }
  446. free(lib32_path);
  447. free(lib64_path);
  448. }
  449. // If we get here, we failed to find anything.
  450. }
  451. Find_Result find_visual_studio_and_windows_sdk() {
  452. Find_Result result;
  453. find_windows_kit_root(&result);
  454. // if (result.windows_sdk_root) {
  455. // result.windows_sdk_um_library_path = concat(result.windows_sdk_root, L"\\um\\x64");
  456. // result.windows_sdk_ucrt_library_path = concat(result.windows_sdk_root, L"\\ucrt\\x64");
  457. // }
  458. find_visual_studio_by_fighting_through_microsoft_craziness(&result);
  459. return result;
  460. }
  461. BF_EXPORT const char* BF_CALLTYPE VSSupport_Find()
  462. {
  463. Beefy::String& outString = *Beefy::gTLStrReturn.Get();
  464. outString.clear();
  465. Find_Result findResult = find_visual_studio_and_windows_sdk();
  466. auto _AddPath = [&](wchar_t* str)
  467. {
  468. if (str != NULL)
  469. {
  470. outString += "\n";
  471. outString += Beefy::UTF8Encode(str);
  472. }
  473. };
  474. if (findResult.vs_exe32_path != NULL)
  475. outString += "TOOL32\t" + Beefy::UTF8Encode(findResult.vs_exe32_path) + "\n";
  476. if (findResult.vs_exe64_path != NULL)
  477. outString += "TOOL64\t" + Beefy::UTF8Encode(findResult.vs_exe64_path) + "\n";
  478. if (findResult.windows_sdk_root != NULL)
  479. {
  480. Beefy::String path = Beefy::UTF8Encode(findResult.windows_sdk_root);
  481. outString += "LIB32\t";
  482. outString += path;
  483. outString += "\\um\\x86\n";
  484. outString += "LIB64\t";
  485. outString += path;
  486. outString += "\\um\\x64\n";
  487. outString += "LIB32\t";
  488. outString += path;
  489. outString += "\\ucrt\\x86\n";
  490. outString += "LIB64\t";
  491. outString += path;
  492. outString += "\\ucrt\\x64\n";
  493. }
  494. if (findResult.vs_library32_path != NULL)
  495. {
  496. outString += "LIB32\t";
  497. outString += Beefy::UTF8Encode(findResult.vs_library32_path);
  498. outString += "\n";
  499. }
  500. if (findResult.vs_library64_path != NULL)
  501. {
  502. outString += "LIB64\t";
  503. outString += Beefy::UTF8Encode(findResult.vs_library64_path);
  504. outString += "\n";
  505. }
  506. free_resources(&findResult);
  507. return outString.c_str();
  508. }