VSSupport.cpp 20 KB

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