WindowsToolChain.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. //===--- ToolChains.cpp - ToolChain Implementations -----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "ToolChains.h"
  10. #include "clang/Basic/CharInfo.h"
  11. #include "clang/Basic/Version.h"
  12. #include "clang/Driver/Compilation.h"
  13. #include "clang/Driver/Driver.h"
  14. #include "clang/Driver/DriverDiagnostic.h"
  15. #include "clang/Driver/Options.h"
  16. #include "llvm/Option/Arg.h"
  17. #include "llvm/Option/ArgList.h"
  18. #include "llvm/Support/ErrorHandling.h"
  19. #include "llvm/Support/Path.h"
  20. // Include the necessary headers to interface with the Windows registry and
  21. // environment.
  22. #ifdef _MSC_VER
  23. #define WIN32_LEAN_AND_MEAN
  24. #define NOGDI
  25. #define NOMINMAX
  26. #include <Windows.h>
  27. #endif
  28. using namespace clang::driver;
  29. using namespace clang::driver::toolchains;
  30. using namespace clang;
  31. using namespace llvm::opt;
  32. Windows::Windows(const Driver &D, const llvm::Triple& Triple,
  33. const ArgList &Args)
  34. : ToolChain(D, Triple, Args) {
  35. }
  36. Tool *Windows::buildLinker() const {
  37. return new tools::visualstudio::Link(*this);
  38. }
  39. Tool *Windows::buildAssembler() const {
  40. if (getTriple().isOSBinFormatMachO())
  41. return new tools::darwin::Assemble(*this);
  42. getDriver().Diag(clang::diag::err_no_external_assembler);
  43. return nullptr;
  44. }
  45. bool Windows::IsIntegratedAssemblerDefault() const {
  46. return true;
  47. }
  48. bool Windows::IsUnwindTablesDefault() const {
  49. return getArch() == llvm::Triple::x86_64;
  50. }
  51. bool Windows::isPICDefault() const {
  52. return getArch() == llvm::Triple::x86_64;
  53. }
  54. bool Windows::isPIEDefault() const {
  55. return false;
  56. }
  57. bool Windows::isPICDefaultForced() const {
  58. return getArch() == llvm::Triple::x86_64;
  59. }
  60. // FIXME: This probably should goto to some platform utils place.
  61. #ifdef _MSC_VER
  62. /// \brief Read registry string.
  63. /// This also supports a means to look for high-versioned keys by use
  64. /// of a $VERSION placeholder in the key path.
  65. /// $VERSION in the key path is a placeholder for the version number,
  66. /// causing the highest value path to be searched for and used.
  67. /// I.e. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION".
  68. /// There can be additional characters in the component. Only the numberic
  69. /// characters are compared.
  70. static bool getSystemRegistryString(const char *keyPath, const char *valueName,
  71. char *value, size_t maxLength) {
  72. HKEY hRootKey = NULL;
  73. HKEY hKey = NULL;
  74. const char* subKey = NULL;
  75. DWORD valueType;
  76. DWORD valueSize = maxLength - 1;
  77. long lResult;
  78. bool returnValue = false;
  79. if (strncmp(keyPath, "HKEY_CLASSES_ROOT\\", 18) == 0) {
  80. hRootKey = HKEY_CLASSES_ROOT;
  81. subKey = keyPath + 18;
  82. } else if (strncmp(keyPath, "HKEY_USERS\\", 11) == 0) {
  83. hRootKey = HKEY_USERS;
  84. subKey = keyPath + 11;
  85. } else if (strncmp(keyPath, "HKEY_LOCAL_MACHINE\\", 19) == 0) {
  86. hRootKey = HKEY_LOCAL_MACHINE;
  87. subKey = keyPath + 19;
  88. } else if (strncmp(keyPath, "HKEY_CURRENT_USER\\", 18) == 0) {
  89. hRootKey = HKEY_CURRENT_USER;
  90. subKey = keyPath + 18;
  91. } else {
  92. return false;
  93. }
  94. const char *placeHolder = strstr(subKey, "$VERSION");
  95. char bestName[256];
  96. bestName[0] = '\0';
  97. // If we have a $VERSION placeholder, do the highest-version search.
  98. if (placeHolder) {
  99. const char *keyEnd = placeHolder - 1;
  100. const char *nextKey = placeHolder;
  101. // Find end of previous key.
  102. while ((keyEnd > subKey) && (*keyEnd != '\\'))
  103. keyEnd--;
  104. // Find end of key containing $VERSION.
  105. while (*nextKey && (*nextKey != '\\'))
  106. nextKey++;
  107. size_t partialKeyLength = keyEnd - subKey;
  108. char partialKey[256];
  109. if (partialKeyLength > sizeof(partialKey))
  110. partialKeyLength = sizeof(partialKey);
  111. strncpy(partialKey, subKey, partialKeyLength);
  112. partialKey[partialKeyLength] = '\0';
  113. HKEY hTopKey = NULL;
  114. lResult = RegOpenKeyEx(hRootKey, partialKey, 0, KEY_READ | KEY_WOW64_32KEY,
  115. &hTopKey);
  116. if (lResult == ERROR_SUCCESS) {
  117. char keyName[256];
  118. int bestIndex = -1;
  119. double bestValue = 0.0;
  120. DWORD index, size = sizeof(keyName) - 1;
  121. for (index = 0; RegEnumKeyEx(hTopKey, index, keyName, &size, NULL,
  122. NULL, NULL, NULL) == ERROR_SUCCESS; index++) {
  123. const char *sp = keyName;
  124. while (*sp && !isDigit(*sp))
  125. sp++;
  126. if (!*sp)
  127. continue;
  128. const char *ep = sp + 1;
  129. while (*ep && (isDigit(*ep) || (*ep == '.')))
  130. ep++;
  131. char numBuf[32];
  132. strncpy(numBuf, sp, sizeof(numBuf) - 1);
  133. numBuf[sizeof(numBuf) - 1] = '\0';
  134. double dvalue = strtod(numBuf, NULL);
  135. if (dvalue > bestValue) {
  136. // Test that InstallDir is indeed there before keeping this index.
  137. // Open the chosen key path remainder.
  138. strcpy(bestName, keyName);
  139. // Append rest of key.
  140. strncat(bestName, nextKey, sizeof(bestName) - 1);
  141. bestName[sizeof(bestName) - 1] = '\0';
  142. lResult = RegOpenKeyEx(hTopKey, bestName, 0,
  143. KEY_READ | KEY_WOW64_32KEY, &hKey);
  144. if (lResult == ERROR_SUCCESS) {
  145. lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
  146. (LPBYTE)value, &valueSize);
  147. if (lResult == ERROR_SUCCESS) {
  148. bestIndex = (int)index;
  149. bestValue = dvalue;
  150. returnValue = true;
  151. }
  152. RegCloseKey(hKey);
  153. }
  154. }
  155. size = sizeof(keyName) - 1;
  156. }
  157. RegCloseKey(hTopKey);
  158. }
  159. } else {
  160. lResult = RegOpenKeyEx(hRootKey, subKey, 0, KEY_READ | KEY_WOW64_32KEY,
  161. &hKey);
  162. if (lResult == ERROR_SUCCESS) {
  163. lResult = RegQueryValueEx(hKey, valueName, NULL, &valueType,
  164. (LPBYTE)value, &valueSize);
  165. if (lResult == ERROR_SUCCESS)
  166. returnValue = true;
  167. RegCloseKey(hKey);
  168. }
  169. }
  170. return returnValue;
  171. }
  172. /// \brief Get Windows SDK installation directory.
  173. static bool getWindowsSDKDir(std::string &path) {
  174. char windowsSDKInstallDir[256];
  175. // Try the Windows registry.
  176. bool hasSDKDir = getSystemRegistryString(
  177. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Microsoft SDKs\\Windows\\$VERSION",
  178. "InstallationFolder",
  179. windowsSDKInstallDir,
  180. sizeof(windowsSDKInstallDir) - 1);
  181. // If we have both vc80 and vc90, pick version we were compiled with.
  182. if (hasSDKDir && windowsSDKInstallDir[0]) {
  183. path = windowsSDKInstallDir;
  184. return true;
  185. }
  186. return false;
  187. }
  188. // Get Visual Studio installation directory.
  189. static bool getVisualStudioDir(std::string &path) {
  190. // First check the environment variables that vsvars32.bat sets.
  191. const char* vcinstalldir = getenv("VCINSTALLDIR");
  192. if (vcinstalldir) {
  193. char *p = const_cast<char *>(strstr(vcinstalldir, "\\VC"));
  194. if (p)
  195. *p = '\0';
  196. path = vcinstalldir;
  197. return true;
  198. }
  199. char vsIDEInstallDir[256];
  200. char vsExpressIDEInstallDir[256];
  201. // Then try the windows registry.
  202. bool hasVCDir = getSystemRegistryString(
  203. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\$VERSION",
  204. "InstallDir", vsIDEInstallDir, sizeof(vsIDEInstallDir) - 1);
  205. bool hasVCExpressDir = getSystemRegistryString(
  206. "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\$VERSION",
  207. "InstallDir", vsExpressIDEInstallDir, sizeof(vsExpressIDEInstallDir) - 1);
  208. // If we have both vc80 and vc90, pick version we were compiled with.
  209. if (hasVCDir && vsIDEInstallDir[0]) {
  210. char *p = (char*)strstr(vsIDEInstallDir, "\\Common7\\IDE");
  211. if (p)
  212. *p = '\0';
  213. path = vsIDEInstallDir;
  214. return true;
  215. }
  216. if (hasVCExpressDir && vsExpressIDEInstallDir[0]) {
  217. char *p = (char*)strstr(vsExpressIDEInstallDir, "\\Common7\\IDE");
  218. if (p)
  219. *p = '\0';
  220. path = vsExpressIDEInstallDir;
  221. return true;
  222. }
  223. // Try the environment.
  224. const char *vs100comntools = getenv("VS100COMNTOOLS");
  225. const char *vs90comntools = getenv("VS90COMNTOOLS");
  226. const char *vs80comntools = getenv("VS80COMNTOOLS");
  227. const char *vscomntools = NULL;
  228. // Try to find the version that we were compiled with
  229. if(false) {}
  230. #if (_MSC_VER >= 1600) // VC100
  231. else if(vs100comntools) {
  232. vscomntools = vs100comntools;
  233. }
  234. #elif (_MSC_VER == 1500) // VC80
  235. else if(vs90comntools) {
  236. vscomntools = vs90comntools;
  237. }
  238. #elif (_MSC_VER == 1400) // VC80
  239. else if(vs80comntools) {
  240. vscomntools = vs80comntools;
  241. }
  242. #endif
  243. // Otherwise find any version we can
  244. else if (vs100comntools)
  245. vscomntools = vs100comntools;
  246. else if (vs90comntools)
  247. vscomntools = vs90comntools;
  248. else if (vs80comntools)
  249. vscomntools = vs80comntools;
  250. if (vscomntools && *vscomntools) {
  251. const char *p = strstr(vscomntools, "\\Common7\\Tools");
  252. path = p ? std::string(vscomntools, p) : vscomntools;
  253. return true;
  254. }
  255. return false;
  256. }
  257. #endif // _MSC_VER
  258. void Windows::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
  259. ArgStringList &CC1Args) const {
  260. if (DriverArgs.hasArg(options::OPT_nostdinc))
  261. return;
  262. if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
  263. SmallString<128> P(getDriver().ResourceDir);
  264. llvm::sys::path::append(P, "include");
  265. addSystemInclude(DriverArgs, CC1Args, P.str());
  266. }
  267. if (DriverArgs.hasArg(options::OPT_nostdlibinc))
  268. return;
  269. #ifdef _MSC_VER
  270. // Honor %INCLUDE%. It should know essential search paths with vcvarsall.bat.
  271. if (const char *cl_include_dir = getenv("INCLUDE")) {
  272. SmallVector<StringRef, 8> Dirs;
  273. StringRef(cl_include_dir)
  274. .split(Dirs, ";", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
  275. for (StringRef Dir : Dirs)
  276. addSystemInclude(DriverArgs, CC1Args, Dir);
  277. if (!Dirs.empty())
  278. return;
  279. }
  280. std::string VSDir;
  281. std::string WindowsSDKDir;
  282. // When built with access to the proper Windows APIs, try to actually find
  283. // the correct include paths first.
  284. if (getVisualStudioDir(VSDir)) {
  285. SmallString<128> P;
  286. P = VSDir;
  287. llvm::sys::path::append(P, "VC\\include");
  288. addSystemInclude(DriverArgs, CC1Args, P.str());
  289. if (getWindowsSDKDir(WindowsSDKDir)) {
  290. P = WindowsSDKDir;
  291. llvm::sys::path::append(P, "include");
  292. addSystemInclude(DriverArgs, CC1Args, P.str());
  293. } else {
  294. P = VSDir;
  295. llvm::sys::path::append(P, "VC\\PlatformSDK\\Include");
  296. addSystemInclude(DriverArgs, CC1Args, P.str());
  297. }
  298. return;
  299. }
  300. // As a fallback, select default install paths.
  301. const StringRef Paths[] = {
  302. "C:/Program Files/Microsoft Visual Studio 10.0/VC/include",
  303. "C:/Program Files/Microsoft Visual Studio 9.0/VC/include",
  304. "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include",
  305. "C:/Program Files/Microsoft Visual Studio 8/VC/include",
  306. "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
  307. };
  308. addSystemIncludes(DriverArgs, CC1Args, Paths);
  309. #endif // _MSC_VER
  310. }
  311. void Windows::AddClangCXXStdlibIncludeArgs(const ArgList &DriverArgs,
  312. ArgStringList &CC1Args) const {
  313. // FIXME: There should probably be logic here to find libc++ on Windows.
  314. }