cmCoreTryCompile.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2011 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCoreTryCompile.h"
  11. #include "cmake.h"
  12. #include "cmCacheManager.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmExportTryCompileFileGenerator.h"
  16. #include <cmsys/Directory.hxx>
  17. #include <assert.h>
  18. int cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv)
  19. {
  20. this->BinaryDirectory = argv[1].c_str();
  21. this->OutputFile = "";
  22. // which signature were we called with ?
  23. this->SrcFileSignature = true;
  24. const char* sourceDirectory = argv[2].c_str();
  25. const char* projectName = 0;
  26. std::string targetName;
  27. std::vector<std::string> cmakeFlags;
  28. std::vector<std::string> compileDefs;
  29. std::string outputVariable;
  30. std::string copyFile;
  31. std::string copyFileError;
  32. std::vector<cmTarget const*> targets;
  33. std::string libsToLink = " ";
  34. bool useOldLinkLibs = true;
  35. char targetNameBuf[64];
  36. bool didOutputVariable = false;
  37. bool didCopyFile = false;
  38. bool didCopyFileError = false;
  39. bool useSources = argv[2] == "SOURCES";
  40. std::vector<std::string> sources;
  41. enum Doing { DoingNone, DoingCMakeFlags, DoingCompileDefinitions,
  42. DoingLinkLibraries, DoingOutputVariable, DoingCopyFile,
  43. DoingCopyFileError, DoingSources };
  44. Doing doing = useSources? DoingSources : DoingNone;
  45. for(size_t i=3; i < argv.size(); ++i)
  46. {
  47. if(argv[i] == "CMAKE_FLAGS")
  48. {
  49. doing = DoingCMakeFlags;
  50. // CMAKE_FLAGS is the first argument because we need an argv[0] that
  51. // is not used, so it matches regular command line parsing which has
  52. // the program name as arg 0
  53. cmakeFlags.push_back(argv[i]);
  54. }
  55. else if(argv[i] == "COMPILE_DEFINITIONS")
  56. {
  57. doing = DoingCompileDefinitions;
  58. }
  59. else if(argv[i] == "LINK_LIBRARIES")
  60. {
  61. doing = DoingLinkLibraries;
  62. useOldLinkLibs = false;
  63. }
  64. else if(argv[i] == "OUTPUT_VARIABLE")
  65. {
  66. doing = DoingOutputVariable;
  67. didOutputVariable = true;
  68. }
  69. else if(argv[i] == "COPY_FILE")
  70. {
  71. doing = DoingCopyFile;
  72. didCopyFile = true;
  73. }
  74. else if(argv[i] == "COPY_FILE_ERROR")
  75. {
  76. doing = DoingCopyFileError;
  77. didCopyFileError = true;
  78. }
  79. else if(doing == DoingCMakeFlags)
  80. {
  81. cmakeFlags.push_back(argv[i]);
  82. }
  83. else if(doing == DoingCompileDefinitions)
  84. {
  85. compileDefs.push_back(argv[i]);
  86. }
  87. else if(doing == DoingLinkLibraries)
  88. {
  89. libsToLink += "\"" + cmSystemTools::TrimWhitespace(argv[i]) + "\" ";
  90. if(cmTarget *tgt = this->Makefile->FindTargetToUse(argv[i]))
  91. {
  92. switch(tgt->GetType())
  93. {
  94. case cmTarget::SHARED_LIBRARY:
  95. case cmTarget::STATIC_LIBRARY:
  96. case cmTarget::INTERFACE_LIBRARY:
  97. case cmTarget::UNKNOWN_LIBRARY:
  98. break;
  99. case cmTarget::EXECUTABLE:
  100. if (tgt->IsExecutableWithExports())
  101. {
  102. break;
  103. }
  104. default:
  105. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  106. "Only libraries may be used as try_compile IMPORTED "
  107. "LINK_LIBRARIES. Got " + std::string(tgt->GetName()) + " of "
  108. "type " + tgt->GetTargetTypeName(tgt->GetType()) + ".");
  109. return -1;
  110. }
  111. if (tgt->IsImported())
  112. {
  113. targets.push_back(tgt);
  114. }
  115. }
  116. }
  117. else if(doing == DoingOutputVariable)
  118. {
  119. outputVariable = argv[i].c_str();
  120. doing = DoingNone;
  121. }
  122. else if(doing == DoingCopyFile)
  123. {
  124. copyFile = argv[i].c_str();
  125. doing = DoingNone;
  126. }
  127. else if(doing == DoingCopyFileError)
  128. {
  129. copyFileError = argv[i].c_str();
  130. doing = DoingNone;
  131. }
  132. else if(doing == DoingSources)
  133. {
  134. sources.push_back(argv[i]);
  135. }
  136. else if(i == 3)
  137. {
  138. this->SrcFileSignature = false;
  139. projectName = argv[i].c_str();
  140. }
  141. else if(i == 4 && !this->SrcFileSignature)
  142. {
  143. targetName = argv[i].c_str();
  144. }
  145. else
  146. {
  147. cmOStringStream m;
  148. m << "try_compile given unknown argument \"" << argv[i] << "\".";
  149. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, m.str());
  150. }
  151. }
  152. if(didCopyFile && copyFile.empty())
  153. {
  154. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  155. "COPY_FILE must be followed by a file path");
  156. return -1;
  157. }
  158. if(didCopyFileError && copyFileError.empty())
  159. {
  160. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  161. "COPY_FILE_ERROR must be followed by a variable name");
  162. return -1;
  163. }
  164. if(didCopyFileError && !didCopyFile)
  165. {
  166. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  167. "COPY_FILE_ERROR may be used only with COPY_FILE");
  168. return -1;
  169. }
  170. if(didOutputVariable && outputVariable.empty())
  171. {
  172. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  173. "OUTPUT_VARIABLE must be followed by a variable name");
  174. return -1;
  175. }
  176. if(useSources && sources.empty())
  177. {
  178. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  179. "SOURCES must be followed by at least one source file");
  180. return -1;
  181. }
  182. // compute the binary dir when TRY_COMPILE is called with a src file
  183. // signature
  184. if (this->SrcFileSignature)
  185. {
  186. this->BinaryDirectory += cmake::GetCMakeFilesDirectory();
  187. this->BinaryDirectory += "/CMakeTmp";
  188. }
  189. else
  190. {
  191. // only valid for srcfile signatures
  192. if (compileDefs.size())
  193. {
  194. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  195. "COMPILE_DEFINITIONS specified on a srcdir type TRY_COMPILE");
  196. return -1;
  197. }
  198. if (copyFile.size())
  199. {
  200. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  201. "COPY_FILE specified on a srcdir type TRY_COMPILE");
  202. return -1;
  203. }
  204. }
  205. // make sure the binary directory exists
  206. cmSystemTools::MakeDirectory(this->BinaryDirectory.c_str());
  207. // do not allow recursive try Compiles
  208. if (this->BinaryDirectory == this->Makefile->GetHomeOutputDirectory())
  209. {
  210. cmOStringStream e;
  211. e << "Attempt at a recursive or nested TRY_COMPILE in directory\n"
  212. << " " << this->BinaryDirectory << "\n";
  213. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  214. return -1;
  215. }
  216. std::string outFileName = this->BinaryDirectory + "/CMakeLists.txt";
  217. // which signature are we using? If we are using var srcfile bindir
  218. if (this->SrcFileSignature)
  219. {
  220. // remove any CMakeCache.txt files so we will have a clean test
  221. std::string ccFile = this->BinaryDirectory + "/CMakeCache.txt";
  222. cmSystemTools::RemoveFile(ccFile);
  223. // Choose sources.
  224. if(!useSources)
  225. {
  226. sources.push_back(argv[2]);
  227. }
  228. // Detect languages to enable.
  229. cmLocalGenerator* lg = this->Makefile->GetLocalGenerator();
  230. cmGlobalGenerator* gg = lg->GetGlobalGenerator();
  231. std::set<std::string> testLangs;
  232. for(std::vector<std::string>::iterator si = sources.begin();
  233. si != sources.end(); ++si)
  234. {
  235. std::string ext = cmSystemTools::GetFilenameLastExtension(*si);
  236. std::string lang = gg->GetLanguageFromExtension(ext.c_str());
  237. if(!lang.empty())
  238. {
  239. testLangs.insert(lang);
  240. }
  241. else
  242. {
  243. cmOStringStream err;
  244. err << "Unknown extension \"" << ext << "\" for file\n"
  245. << " " << *si << "\n"
  246. << "try_compile() works only for enabled languages. "
  247. << "Currently these are:\n ";
  248. std::vector<std::string> langs;
  249. gg->GetEnabledLanguages(langs);
  250. for(std::vector<std::string>::iterator l = langs.begin();
  251. l != langs.end(); ++l)
  252. {
  253. err << " " << *l;
  254. }
  255. err << "\nSee project() command to enable other languages.";
  256. this->Makefile->IssueMessage(cmake::FATAL_ERROR, err.str());
  257. return -1;
  258. }
  259. }
  260. // we need to create a directory and CMakeLists file etc...
  261. // first create the directories
  262. sourceDirectory = this->BinaryDirectory.c_str();
  263. // now create a CMakeLists.txt file in that directory
  264. FILE *fout = cmsys::SystemTools::Fopen(outFileName,"w");
  265. if (!fout)
  266. {
  267. cmOStringStream e;
  268. e << "Failed to open\n"
  269. << " " << outFileName << "\n"
  270. << cmSystemTools::GetLastSystemError();
  271. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  272. return -1;
  273. }
  274. const char* def = this->Makefile->GetDefinition("CMAKE_MODULE_PATH");
  275. fprintf(fout, "cmake_minimum_required(VERSION %u.%u.%u.%u)\n",
  276. cmVersion::GetMajorVersion(), cmVersion::GetMinorVersion(),
  277. cmVersion::GetPatchVersion(), cmVersion::GetTweakVersion());
  278. if(def)
  279. {
  280. fprintf(fout, "set(CMAKE_MODULE_PATH %s)\n", def);
  281. }
  282. std::string projectLangs;
  283. for(std::set<std::string>::iterator li = testLangs.begin();
  284. li != testLangs.end(); ++li)
  285. {
  286. projectLangs += " " + *li;
  287. std::string rulesOverrideBase = "CMAKE_USER_MAKE_RULES_OVERRIDE";
  288. std::string rulesOverrideLang = rulesOverrideBase + "_" + *li;
  289. if(const char* rulesOverridePath =
  290. this->Makefile->GetDefinition(rulesOverrideLang))
  291. {
  292. fprintf(fout, "set(%s \"%s\")\n",
  293. rulesOverrideLang.c_str(), rulesOverridePath);
  294. }
  295. else if(const char* rulesOverridePath2 =
  296. this->Makefile->GetDefinition(rulesOverrideBase))
  297. {
  298. fprintf(fout, "set(%s \"%s\")\n",
  299. rulesOverrideBase.c_str(), rulesOverridePath2);
  300. }
  301. }
  302. fprintf(fout, "project(CMAKE_TRY_COMPILE%s)\n", projectLangs.c_str());
  303. fprintf(fout, "set(CMAKE_VERBOSE_MAKEFILE 1)\n");
  304. for(std::set<std::string>::iterator li = testLangs.begin();
  305. li != testLangs.end(); ++li)
  306. {
  307. std::string langFlags = "CMAKE_" + *li + "_FLAGS";
  308. const char* flags = this->Makefile->GetDefinition(langFlags);
  309. fprintf(fout, "set(CMAKE_%s_FLAGS %s)\n", li->c_str(),
  310. lg->EscapeForCMake(flags?flags:"").c_str());
  311. fprintf(fout, "set(CMAKE_%s_FLAGS \"${CMAKE_%s_FLAGS}"
  312. " ${COMPILE_DEFINITIONS}\")\n", li->c_str(), li->c_str());
  313. }
  314. fprintf(fout, "include_directories(${INCLUDE_DIRECTORIES})\n");
  315. fprintf(fout, "set(CMAKE_SUPPRESS_REGENERATION 1)\n");
  316. fprintf(fout, "link_directories(${LINK_DIRECTORIES})\n");
  317. // handle any compile flags we need to pass on
  318. if (compileDefs.size())
  319. {
  320. fprintf(fout, "add_definitions( ");
  321. for (size_t i = 0; i < compileDefs.size(); ++i)
  322. {
  323. fprintf(fout,"%s ",compileDefs[i].c_str());
  324. }
  325. fprintf(fout, ")\n");
  326. }
  327. /* Use a random file name to avoid rapid creation and deletion
  328. of the same executable name (some filesystems fail on that). */
  329. sprintf(targetNameBuf, "cmTryCompileExec%u",
  330. cmSystemTools::RandomSeed());
  331. targetName = targetNameBuf;
  332. if (!targets.empty())
  333. {
  334. std::string fname = "/" + std::string(targetName) + "Targets.cmake";
  335. cmExportTryCompileFileGenerator tcfg;
  336. tcfg.SetExportFile((this->BinaryDirectory + fname).c_str());
  337. tcfg.SetExports(targets);
  338. tcfg.SetConfig(this->Makefile->GetSafeDefinition(
  339. "CMAKE_TRY_COMPILE_CONFIGURATION"));
  340. if(!tcfg.GenerateImportFile())
  341. {
  342. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  343. "could not write export file.");
  344. fclose(fout);
  345. return -1;
  346. }
  347. fprintf(fout,
  348. "\ninclude(\"${CMAKE_CURRENT_LIST_DIR}/%s\")\n\n",
  349. fname.c_str());
  350. }
  351. /* for the TRY_COMPILEs we want to be able to specify the architecture.
  352. So the user can set CMAKE_OSX_ARCHITECTURES to i386;ppc and then set
  353. CMAKE_TRY_COMPILE_OSX_ARCHITECTURES first to i386 and then to ppc to
  354. have the tests run for each specific architecture. Since
  355. cmLocalGenerator doesn't allow building for "the other"
  356. architecture only via CMAKE_OSX_ARCHITECTURES.
  357. */
  358. if(this->Makefile->GetDefinition("CMAKE_TRY_COMPILE_OSX_ARCHITECTURES")!=0)
  359. {
  360. std::string flag="-DCMAKE_OSX_ARCHITECTURES=";
  361. flag += this->Makefile->GetSafeDefinition(
  362. "CMAKE_TRY_COMPILE_OSX_ARCHITECTURES");
  363. cmakeFlags.push_back(flag);
  364. }
  365. else if (this->Makefile->GetDefinition("CMAKE_OSX_ARCHITECTURES")!=0)
  366. {
  367. std::string flag="-DCMAKE_OSX_ARCHITECTURES=";
  368. flag += this->Makefile->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES");
  369. cmakeFlags.push_back(flag);
  370. }
  371. /* on APPLE also pass CMAKE_OSX_SYSROOT to the try_compile */
  372. if(this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT")!=0)
  373. {
  374. std::string flag="-DCMAKE_OSX_SYSROOT=";
  375. flag += this->Makefile->GetSafeDefinition("CMAKE_OSX_SYSROOT");
  376. cmakeFlags.push_back(flag);
  377. }
  378. /* on APPLE also pass CMAKE_OSX_DEPLOYMENT_TARGET to the try_compile */
  379. if(this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET")!=0)
  380. {
  381. std::string flag="-DCMAKE_OSX_DEPLOYMENT_TARGET=";
  382. flag += this->Makefile->GetSafeDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  383. cmakeFlags.push_back(flag);
  384. }
  385. if (const char *cxxDef
  386. = this->Makefile->GetDefinition("CMAKE_CXX_COMPILER_TARGET"))
  387. {
  388. std::string flag="-DCMAKE_CXX_COMPILER_TARGET=";
  389. flag += cxxDef;
  390. cmakeFlags.push_back(flag);
  391. }
  392. if (const char *cDef
  393. = this->Makefile->GetDefinition("CMAKE_C_COMPILER_TARGET"))
  394. {
  395. std::string flag="-DCMAKE_C_COMPILER_TARGET=";
  396. flag += cDef;
  397. cmakeFlags.push_back(flag);
  398. }
  399. if (const char *tcxxDef = this->Makefile->GetDefinition(
  400. "CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN"))
  401. {
  402. std::string flag="-DCMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN=";
  403. flag += tcxxDef;
  404. cmakeFlags.push_back(flag);
  405. }
  406. if (const char *tcDef = this->Makefile->GetDefinition(
  407. "CMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN"))
  408. {
  409. std::string flag="-DCMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN=";
  410. flag += tcDef;
  411. cmakeFlags.push_back(flag);
  412. }
  413. if (const char *rootDef
  414. = this->Makefile->GetDefinition("CMAKE_SYSROOT"))
  415. {
  416. std::string flag="-DCMAKE_SYSROOT=";
  417. flag += rootDef;
  418. cmakeFlags.push_back(flag);
  419. }
  420. if(this->Makefile->GetDefinition("CMAKE_POSITION_INDEPENDENT_CODE")!=0)
  421. {
  422. fprintf(fout, "set(CMAKE_POSITION_INDEPENDENT_CODE \"ON\")\n");
  423. }
  424. /* Put the executable at a known location (for COPY_FILE). */
  425. fprintf(fout, "set(CMAKE_RUNTIME_OUTPUT_DIRECTORY \"%s\")\n",
  426. this->BinaryDirectory.c_str());
  427. /* Create the actual executable. */
  428. fprintf(fout, "add_executable(%s", targetName.c_str());
  429. for(std::vector<std::string>::iterator si = sources.begin();
  430. si != sources.end(); ++si)
  431. {
  432. fprintf(fout, " \"%s\"", si->c_str());
  433. // Add dependencies on any non-temporary sources.
  434. if(si->find("CMakeTmp") == si->npos)
  435. {
  436. this->Makefile->AddCMakeDependFile(*si);
  437. }
  438. }
  439. fprintf(fout, ")\n");
  440. if (useOldLinkLibs)
  441. {
  442. fprintf(fout,
  443. "target_link_libraries(%s ${LINK_LIBRARIES})\n",
  444. targetName.c_str());
  445. }
  446. else
  447. {
  448. fprintf(fout, "target_link_libraries(%s %s)\n",
  449. targetName.c_str(),
  450. libsToLink.c_str());
  451. }
  452. fclose(fout);
  453. projectName = "CMAKE_TRY_COMPILE";
  454. }
  455. bool erroroc = cmSystemTools::GetErrorOccuredFlag();
  456. cmSystemTools::ResetErrorOccuredFlag();
  457. std::string output;
  458. // actually do the try compile now that everything is setup
  459. int res = this->Makefile->TryCompile(sourceDirectory,
  460. this->BinaryDirectory,
  461. projectName,
  462. targetName,
  463. this->SrcFileSignature,
  464. &cmakeFlags,
  465. output);
  466. if ( erroroc )
  467. {
  468. cmSystemTools::SetErrorOccured();
  469. }
  470. // set the result var to the return value to indicate success or failure
  471. this->Makefile->AddCacheDefinition(argv[0],
  472. (res == 0 ? "TRUE" : "FALSE"),
  473. "Result of TRY_COMPILE",
  474. cmCacheManager::INTERNAL);
  475. if ( outputVariable.size() > 0 )
  476. {
  477. this->Makefile->AddDefinition(outputVariable, output.c_str());
  478. }
  479. if (this->SrcFileSignature)
  480. {
  481. std::string copyFileErrorMessage;
  482. this->FindOutputFile(targetName);
  483. if ((res==0) && (copyFile.size()))
  484. {
  485. if(this->OutputFile.empty() ||
  486. !cmSystemTools::CopyFileAlways(this->OutputFile,
  487. copyFile))
  488. {
  489. cmOStringStream emsg;
  490. emsg << "Cannot copy output executable\n"
  491. << " '" << this->OutputFile << "'\n"
  492. << "to destination specified by COPY_FILE:\n"
  493. << " '" << copyFile << "'\n";
  494. if(!this->FindErrorMessage.empty())
  495. {
  496. emsg << this->FindErrorMessage.c_str();
  497. }
  498. if(copyFileError.empty())
  499. {
  500. this->Makefile->IssueMessage(cmake::FATAL_ERROR, emsg.str());
  501. return -1;
  502. }
  503. else
  504. {
  505. copyFileErrorMessage = emsg.str();
  506. }
  507. }
  508. }
  509. if(!copyFileError.empty())
  510. {
  511. this->Makefile->AddDefinition(copyFileError,
  512. copyFileErrorMessage.c_str());
  513. }
  514. }
  515. return res;
  516. }
  517. void cmCoreTryCompile::CleanupFiles(const char* binDir)
  518. {
  519. if ( !binDir )
  520. {
  521. return;
  522. }
  523. std::string bdir = binDir;
  524. if(bdir.find("CMakeTmp") == std::string::npos)
  525. {
  526. cmSystemTools::Error(
  527. "TRY_COMPILE attempt to remove -rf directory that does not contain "
  528. "CMakeTmp:", binDir);
  529. return;
  530. }
  531. cmsys::Directory dir;
  532. dir.Load(binDir);
  533. size_t fileNum;
  534. std::set<std::string> deletedFiles;
  535. for (fileNum = 0; fileNum < dir.GetNumberOfFiles(); ++fileNum)
  536. {
  537. if (strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".") &&
  538. strcmp(dir.GetFile(static_cast<unsigned long>(fileNum)),".."))
  539. {
  540. if(deletedFiles.find( dir.GetFile(static_cast<unsigned long>(fileNum)))
  541. == deletedFiles.end())
  542. {
  543. deletedFiles.insert(dir.GetFile(static_cast<unsigned long>(fileNum)));
  544. std::string fullPath = binDir;
  545. fullPath += "/";
  546. fullPath += dir.GetFile(static_cast<unsigned long>(fileNum));
  547. if(cmSystemTools::FileIsDirectory(fullPath))
  548. {
  549. this->CleanupFiles(fullPath.c_str());
  550. cmSystemTools::RemoveADirectory(fullPath);
  551. }
  552. else
  553. {
  554. #ifdef _WIN32
  555. // Sometimes anti-virus software hangs on to new files so we
  556. // cannot delete them immediately. Try a few times.
  557. cmSystemTools::WindowsFileRetry retry =
  558. cmSystemTools::GetWindowsFileRetry();
  559. while(!cmSystemTools::RemoveFile(fullPath.c_str()) &&
  560. --retry.Count && cmSystemTools::FileExists(fullPath.c_str()))
  561. {
  562. cmSystemTools::Delay(retry.Delay);
  563. }
  564. if(retry.Count == 0)
  565. #else
  566. if(!cmSystemTools::RemoveFile(fullPath))
  567. #endif
  568. {
  569. std::string m = "Remove failed on file: " + fullPath;
  570. cmSystemTools::ReportLastSystemError(m.c_str());
  571. }
  572. }
  573. }
  574. }
  575. }
  576. }
  577. void cmCoreTryCompile::FindOutputFile(const std::string& targetName)
  578. {
  579. this->FindErrorMessage = "";
  580. this->OutputFile = "";
  581. std::string tmpOutputFile = "/";
  582. tmpOutputFile += targetName;
  583. tmpOutputFile +=this->Makefile->GetSafeDefinition("CMAKE_EXECUTABLE_SUFFIX");
  584. // a list of directories where to search for the compilation result
  585. // at first directly in the binary dir
  586. std::vector<std::string> searchDirs;
  587. searchDirs.push_back("");
  588. const char* config = this->Makefile->GetDefinition(
  589. "CMAKE_TRY_COMPILE_CONFIGURATION");
  590. // if a config was specified try that first
  591. if (config && config[0])
  592. {
  593. std::string tmp = "/";
  594. tmp += config;
  595. searchDirs.push_back(tmp);
  596. }
  597. searchDirs.push_back("/Debug");
  598. #if defined(__APPLE__)
  599. std::string app = "/Debug/" + targetName + ".app";
  600. searchDirs.push_back(app);
  601. #endif
  602. searchDirs.push_back("/Development");
  603. for(std::vector<std::string>::const_iterator it = searchDirs.begin();
  604. it != searchDirs.end();
  605. ++it)
  606. {
  607. std::string command = this->BinaryDirectory;
  608. command += *it;
  609. command += tmpOutputFile;
  610. if(cmSystemTools::FileExists(command.c_str()))
  611. {
  612. tmpOutputFile = cmSystemTools::CollapseFullPath(command);
  613. this->OutputFile = tmpOutputFile;
  614. return;
  615. }
  616. }
  617. cmOStringStream emsg;
  618. emsg << "Unable to find the executable at any of:\n";
  619. for (unsigned int i = 0; i < searchDirs.size(); ++i)
  620. {
  621. emsg << " " << this->BinaryDirectory << searchDirs[i]
  622. << tmpOutputFile << "\n";
  623. }
  624. this->FindErrorMessage = emsg.str();
  625. return;
  626. }