cmake.cxx 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 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 "cmake.h"
  11. #include "cmCacheManager.h"
  12. #include "cmMakefile.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmExternalMakefileProjectGenerator.h"
  15. #include "cmCommands.h"
  16. #include "cmCommand.h"
  17. #include "cmFileTimeComparison.h"
  18. #include "cmSourceFile.h"
  19. #include "cmTest.h"
  20. #include "cmDocumentationFormatter.h"
  21. #include "cmAlgorithms.h"
  22. #include "cmState.h"
  23. #if defined(CMAKE_BUILD_WITH_CMAKE)
  24. # include "cmGraphVizWriter.h"
  25. # include "cmVariableWatch.h"
  26. # include <cmsys/SystemInformation.hxx>
  27. #endif
  28. #include <cmsys/Glob.hxx>
  29. #include <cmsys/RegularExpression.hxx>
  30. #include <cmsys/FStream.hxx>
  31. // only build kdevelop generator on non-windows platforms
  32. // when not bootstrapping cmake
  33. #if !defined(_WIN32)
  34. # if defined(CMAKE_BUILD_WITH_CMAKE)
  35. # define CMAKE_USE_KDEVELOP
  36. # endif
  37. #endif
  38. #if defined(CMAKE_BUILD_WITH_CMAKE)
  39. # define CMAKE_USE_ECLIPSE
  40. #endif
  41. #if defined(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
  42. # define CMAKE_BOOT_MINGW
  43. #endif
  44. // include the generator
  45. #if defined(_WIN32) && !defined(__CYGWIN__)
  46. # if !defined(CMAKE_BOOT_MINGW)
  47. # include "cmGlobalVisualStudio6Generator.h"
  48. # include "cmGlobalVisualStudio7Generator.h"
  49. # include "cmGlobalVisualStudio71Generator.h"
  50. # include "cmGlobalVisualStudio8Generator.h"
  51. # include "cmGlobalVisualStudio9Generator.h"
  52. # include "cmGlobalVisualStudio10Generator.h"
  53. # include "cmGlobalVisualStudio11Generator.h"
  54. # include "cmGlobalVisualStudio12Generator.h"
  55. # include "cmGlobalVisualStudio14Generator.h"
  56. # include "cmGlobalBorlandMakefileGenerator.h"
  57. # include "cmGlobalNMakeMakefileGenerator.h"
  58. # include "cmGlobalJOMMakefileGenerator.h"
  59. # include "cmGlobalGhsMultiGenerator.h"
  60. # define CMAKE_HAVE_VS_GENERATORS
  61. # endif
  62. # include "cmGlobalMSYSMakefileGenerator.h"
  63. # include "cmGlobalMinGWMakefileGenerator.h"
  64. #else
  65. #endif
  66. #if defined(CMAKE_USE_WMAKE)
  67. # include "cmGlobalWatcomWMakeGenerator.h"
  68. #endif
  69. #include "cmGlobalUnixMakefileGenerator3.h"
  70. #include "cmGlobalNinjaGenerator.h"
  71. #include "cmExtraCodeLiteGenerator.h"
  72. #if !defined(CMAKE_BOOT_MINGW)
  73. # include "cmExtraCodeBlocksGenerator.h"
  74. #endif
  75. #include "cmExtraSublimeTextGenerator.h"
  76. #include "cmExtraKateGenerator.h"
  77. #ifdef CMAKE_USE_KDEVELOP
  78. # include "cmGlobalKdevelopGenerator.h"
  79. #endif
  80. #ifdef CMAKE_USE_ECLIPSE
  81. # include "cmExtraEclipseCDT4Generator.h"
  82. #endif
  83. #include <stdlib.h> // required for atoi
  84. #if defined( __APPLE__ )
  85. # if defined(CMAKE_BUILD_WITH_CMAKE)
  86. # include "cmGlobalXCodeGenerator.h"
  87. # define CMAKE_USE_XCODE 1
  88. # endif
  89. # include <sys/types.h>
  90. # include <sys/time.h>
  91. # include <sys/resource.h>
  92. #endif
  93. #include <sys/stat.h> // struct stat
  94. #include <list>
  95. static bool cmakeCheckStampFile(const char* stampName);
  96. static bool cmakeCheckStampList(const char* stampName);
  97. void cmWarnUnusedCliWarning(const std::string& variable,
  98. int, void* ctx, const char*, const cmMakefile*)
  99. {
  100. cmake* cm = reinterpret_cast<cmake*>(ctx);
  101. cm->MarkCliAsUsed(variable);
  102. }
  103. cmake::cmake()
  104. {
  105. this->Trace = false;
  106. this->TraceExpand = false;
  107. this->WarnUninitialized = false;
  108. this->WarnUnused = false;
  109. this->WarnUnusedCli = true;
  110. this->CheckSystemVars = false;
  111. this->SuppressDevWarnings = false;
  112. this->DoSuppressDevWarnings = false;
  113. this->DebugOutput = false;
  114. this->DebugTryCompile = false;
  115. this->ClearBuildSystem = false;
  116. this->FileComparison = new cmFileTimeComparison;
  117. this->Policies = new cmPolicies();
  118. this->State = new cmState(this);
  119. this->CurrentSnapshot = this->State->CreateBaseSnapshot();
  120. #ifdef __APPLE__
  121. struct rlimit rlp;
  122. if(!getrlimit(RLIMIT_STACK, &rlp))
  123. {
  124. if(rlp.rlim_cur != rlp.rlim_max)
  125. {
  126. rlp.rlim_cur = rlp.rlim_max;
  127. setrlimit(RLIMIT_STACK, &rlp);
  128. }
  129. }
  130. #endif
  131. this->Verbose = false;
  132. this->CacheManager = new cmCacheManager(this);
  133. this->GlobalGenerator = 0;
  134. this->ProgressCallback = 0;
  135. this->ProgressCallbackClientData = 0;
  136. this->CurrentWorkingMode = NORMAL_MODE;
  137. #ifdef CMAKE_BUILD_WITH_CMAKE
  138. this->VariableWatch = new cmVariableWatch;
  139. #endif
  140. this->AddDefaultGenerators();
  141. this->AddDefaultExtraGenerators();
  142. this->AddDefaultCommands();
  143. // Make sure we can capture the build tool output.
  144. cmSystemTools::EnableVSConsoleOutput();
  145. }
  146. cmake::~cmake()
  147. {
  148. delete this->CacheManager;
  149. delete this->Policies;
  150. delete this->State;
  151. if (this->GlobalGenerator)
  152. {
  153. delete this->GlobalGenerator;
  154. this->GlobalGenerator = 0;
  155. }
  156. cmDeleteAll(this->Generators);
  157. #ifdef CMAKE_BUILD_WITH_CMAKE
  158. delete this->VariableWatch;
  159. #endif
  160. delete this->FileComparison;
  161. }
  162. void cmake::CleanupCommandsAndMacros()
  163. {
  164. this->CurrentSnapshot = this->State->Reset();
  165. this->State->RemoveUserDefinedCommands();
  166. }
  167. // Parse the args
  168. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  169. {
  170. bool findPackageMode = false;
  171. for(unsigned int i=1; i < args.size(); ++i)
  172. {
  173. std::string arg = args[i];
  174. if(arg.find("-D",0) == 0)
  175. {
  176. std::string entry = arg.substr(2);
  177. if(entry.empty())
  178. {
  179. ++i;
  180. if(i < args.size())
  181. {
  182. entry = args[i];
  183. }
  184. else
  185. {
  186. cmSystemTools::Error("-D must be followed with VAR=VALUE.");
  187. return false;
  188. }
  189. }
  190. std::string var, value;
  191. cmState::CacheEntryType type = cmState::UNINITIALIZED;
  192. if(cmCacheManager::ParseEntry(entry, var, value, type))
  193. {
  194. // The value is transformed if it is a filepath for example, so
  195. // we can't compare whether the value is already in the cache until
  196. // after we call AddCacheEntry.
  197. bool haveValue = false;
  198. std::string cachedValue;
  199. if(this->WarnUnusedCli)
  200. {
  201. if(const char *v = this->State->GetInitializedCacheValue(var))
  202. {
  203. haveValue = true;
  204. cachedValue = v;
  205. }
  206. }
  207. this->State->AddCacheEntry(var, value.c_str(),
  208. "No help, variable specified on the command line.", type);
  209. if(this->WarnUnusedCli)
  210. {
  211. if (!haveValue ||
  212. cachedValue != this->State->GetInitializedCacheValue(var))
  213. {
  214. this->WatchUnusedCli(var);
  215. }
  216. }
  217. }
  218. else
  219. {
  220. std::cerr << "Parse error in command line argument: " << arg << "\n"
  221. << "Should be: VAR:type=value\n";
  222. cmSystemTools::Error("No cmake script provided.");
  223. return false;
  224. }
  225. }
  226. else if(arg.find("-Wno-dev",0) == 0)
  227. {
  228. this->SuppressDevWarnings = true;
  229. this->DoSuppressDevWarnings = true;
  230. }
  231. else if(arg.find("-Wdev",0) == 0)
  232. {
  233. this->SuppressDevWarnings = false;
  234. this->DoSuppressDevWarnings = true;
  235. }
  236. else if(arg.find("-U",0) == 0)
  237. {
  238. std::string entryPattern = arg.substr(2);
  239. if(entryPattern.empty())
  240. {
  241. ++i;
  242. if(i < args.size())
  243. {
  244. entryPattern = args[i];
  245. }
  246. else
  247. {
  248. cmSystemTools::Error("-U must be followed with VAR.");
  249. return false;
  250. }
  251. }
  252. cmsys::RegularExpression regex(
  253. cmsys::Glob::PatternToRegex(entryPattern, true, true).c_str());
  254. //go through all cache entries and collect the vars which will be removed
  255. std::vector<std::string> entriesToDelete;
  256. std::vector<std::string> cacheKeys = this->State->GetCacheEntryKeys();
  257. for (std::vector<std::string>::const_iterator it = cacheKeys.begin();
  258. it != cacheKeys.end(); ++it)
  259. {
  260. cmState::CacheEntryType t = this->State->GetCacheEntryType(*it);
  261. if(t != cmState::STATIC)
  262. {
  263. if (regex.find(it->c_str()))
  264. {
  265. entriesToDelete.push_back(*it);
  266. }
  267. }
  268. }
  269. // now remove them from the cache
  270. for(std::vector<std::string>::const_iterator currentEntry =
  271. entriesToDelete.begin();
  272. currentEntry != entriesToDelete.end();
  273. ++currentEntry)
  274. {
  275. this->State->RemoveCacheEntry(*currentEntry);
  276. }
  277. }
  278. else if(arg.find("-C",0) == 0)
  279. {
  280. std::string path = arg.substr(2);
  281. if (path.empty())
  282. {
  283. ++i;
  284. if(i < args.size())
  285. {
  286. path = args[i];
  287. }
  288. else
  289. {
  290. cmSystemTools::Error("-C must be followed by a file name.");
  291. return false;
  292. }
  293. }
  294. std::cout << "loading initial cache file " << path << "\n";
  295. this->ReadListFile(args, path.c_str());
  296. }
  297. else if(arg.find("-P",0) == 0)
  298. {
  299. i++;
  300. if(i >= args.size())
  301. {
  302. cmSystemTools::Error("-P must be followed by a file name.");
  303. return false;
  304. }
  305. std::string path = args[i];
  306. if (path.empty())
  307. {
  308. cmSystemTools::Error("No cmake script provided.");
  309. return false;
  310. }
  311. this->ReadListFile(args, path.c_str());
  312. }
  313. else if (arg.find("--find-package",0) == 0)
  314. {
  315. findPackageMode = true;
  316. }
  317. }
  318. if (findPackageMode)
  319. {
  320. return this->FindPackage(args);
  321. }
  322. return true;
  323. }
  324. void cmake::ReadListFile(const std::vector<std::string>& args,
  325. const char *path)
  326. {
  327. // if a generator was not yet created, temporarily create one
  328. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  329. bool created = false;
  330. // if a generator was not specified use a generic one
  331. if (!gg)
  332. {
  333. gg = new cmGlobalGenerator(this);
  334. created = true;
  335. }
  336. // read in the list file to fill the cache
  337. if(path)
  338. {
  339. this->CurrentSnapshot = this->State->Reset();
  340. std::string homeDir = this->GetHomeDirectory();
  341. std::string homeOutputDir = this->GetHomeOutputDirectory();
  342. this->SetHomeDirectory(cmSystemTools::GetCurrentWorkingDirectory());
  343. this->SetHomeOutputDirectory(cmSystemTools::GetCurrentWorkingDirectory());
  344. cmsys::auto_ptr<cmLocalGenerator> lg(gg->MakeLocalGenerator());
  345. lg->GetMakefile()->SetCurrentBinaryDirectory
  346. (cmSystemTools::GetCurrentWorkingDirectory());
  347. lg->GetMakefile()->SetCurrentSourceDirectory
  348. (cmSystemTools::GetCurrentWorkingDirectory());
  349. if (this->GetWorkingMode() != NORMAL_MODE)
  350. {
  351. std::string file(cmSystemTools::CollapseFullPath(path));
  352. cmSystemTools::ConvertToUnixSlashes(file);
  353. lg->GetMakefile()->SetScriptModeFile(file.c_str());
  354. lg->GetMakefile()->SetArgcArgv(args);
  355. }
  356. if (!lg->GetMakefile()->ReadListFile(path))
  357. {
  358. cmSystemTools::Error("Error processing file: ", path);
  359. }
  360. this->SetHomeDirectory(homeDir);
  361. this->SetHomeOutputDirectory(homeOutputDir);
  362. }
  363. // free generic one if generated
  364. if (created)
  365. {
  366. delete gg;
  367. }
  368. }
  369. bool cmake::FindPackage(const std::vector<std::string>& args)
  370. {
  371. this->SetHomeDirectory
  372. (cmSystemTools::GetCurrentWorkingDirectory());
  373. this->SetHomeOutputDirectory
  374. (cmSystemTools::GetCurrentWorkingDirectory());
  375. // if a generator was not yet created, temporarily create one
  376. cmGlobalGenerator *gg = new cmGlobalGenerator(this);
  377. this->SetGlobalGenerator(gg);
  378. // read in the list file to fill the cache
  379. cmsys::auto_ptr<cmLocalGenerator> lg(gg->MakeLocalGenerator());
  380. cmMakefile* mf = lg->GetMakefile();
  381. mf->SetCurrentBinaryDirectory
  382. (cmSystemTools::GetCurrentWorkingDirectory());
  383. mf->SetCurrentSourceDirectory
  384. (cmSystemTools::GetCurrentWorkingDirectory());
  385. mf->SetArgcArgv(args);
  386. std::string systemFile = mf->GetModulesFile("CMakeFindPackageMode.cmake");
  387. mf->ReadListFile(systemFile.c_str());
  388. std::string language = mf->GetSafeDefinition("LANGUAGE");
  389. std::string mode = mf->GetSafeDefinition("MODE");
  390. std::string packageName = mf->GetSafeDefinition("NAME");
  391. bool packageFound = mf->IsOn("PACKAGE_FOUND");
  392. bool quiet = mf->IsOn("PACKAGE_QUIET");
  393. if (!packageFound)
  394. {
  395. if (!quiet)
  396. {
  397. printf("%s not found.\n", packageName.c_str());
  398. }
  399. }
  400. else if (mode == "EXIST")
  401. {
  402. if (!quiet)
  403. {
  404. printf("%s found.\n", packageName.c_str());
  405. }
  406. }
  407. else if (mode == "COMPILE")
  408. {
  409. std::string includes = mf->GetSafeDefinition("PACKAGE_INCLUDE_DIRS");
  410. std::vector<std::string> includeDirs;
  411. cmSystemTools::ExpandListArgument(includes, includeDirs);
  412. std::string includeFlags = lg->GetIncludeFlags(includeDirs, 0, language);
  413. std::string definitions = mf->GetSafeDefinition("PACKAGE_DEFINITIONS");
  414. printf("%s %s\n", includeFlags.c_str(), definitions.c_str());
  415. }
  416. else if (mode == "LINK")
  417. {
  418. const char* targetName = "dummy";
  419. std::vector<std::string> srcs;
  420. cmTarget* tgt = mf->AddExecutable(targetName, srcs, true);
  421. tgt->SetProperty("LINKER_LANGUAGE", language.c_str());
  422. std::string libs = mf->GetSafeDefinition("PACKAGE_LIBRARIES");
  423. std::vector<std::string> libList;
  424. cmSystemTools::ExpandListArgument(libs, libList);
  425. for(std::vector<std::string>::const_iterator libIt=libList.begin();
  426. libIt != libList.end();
  427. ++libIt)
  428. {
  429. mf->AddLinkLibraryForTarget(targetName, *libIt,
  430. cmTarget::GENERAL);
  431. }
  432. std::string linkLibs;
  433. std::string frameworkPath;
  434. std::string linkPath;
  435. std::string flags;
  436. std::string linkFlags;
  437. gg->CreateGeneratorTargets(cmGlobalGenerator::AllTargets, lg.get());
  438. cmGeneratorTarget *gtgt = gg->GetGeneratorTarget(tgt);
  439. lg->GetTargetFlags(linkLibs, frameworkPath, linkPath, flags, linkFlags,
  440. gtgt, false);
  441. linkLibs = frameworkPath + linkPath + linkLibs;
  442. printf("%s\n", linkLibs.c_str() );
  443. /* if ( use_win32 )
  444. {
  445. tgt->SetProperty("WIN32_EXECUTABLE", "ON");
  446. }
  447. if ( use_macbundle)
  448. {
  449. tgt->SetProperty("MACOSX_BUNDLE", "ON");
  450. }*/
  451. }
  452. // free generic one if generated
  453. // this->SetGlobalGenerator(0); // setting 0-pointer is not possible
  454. // delete gg; // this crashes inside the cmake instance
  455. return packageFound;
  456. }
  457. // Parse the args
  458. void cmake::SetArgs(const std::vector<std::string>& args,
  459. bool directoriesSetBefore)
  460. {
  461. bool directoriesSet = directoriesSetBefore;
  462. bool haveToolset = false;
  463. bool havePlatform = false;
  464. for(unsigned int i=1; i < args.size(); ++i)
  465. {
  466. std::string arg = args[i];
  467. if(arg.find("-H",0) == 0)
  468. {
  469. directoriesSet = true;
  470. std::string path = arg.substr(2);
  471. path = cmSystemTools::CollapseFullPath(path);
  472. cmSystemTools::ConvertToUnixSlashes(path);
  473. this->SetHomeDirectory(path);
  474. }
  475. else if(arg.find("-S",0) == 0)
  476. {
  477. // There is no local generate anymore. Ignore -S option.
  478. }
  479. else if(arg.find("-O",0) == 0)
  480. {
  481. // There is no local generate anymore. Ignore -O option.
  482. }
  483. else if(arg.find("-B",0) == 0)
  484. {
  485. directoriesSet = true;
  486. std::string path = arg.substr(2);
  487. path = cmSystemTools::CollapseFullPath(path);
  488. cmSystemTools::ConvertToUnixSlashes(path);
  489. this->SetHomeOutputDirectory(path);
  490. }
  491. else if((i < args.size()-2) && (arg.find("--check-build-system",0) == 0))
  492. {
  493. this->CheckBuildSystemArgument = args[++i];
  494. this->ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  495. }
  496. else if((i < args.size()-1) && (arg.find("--check-stamp-file",0) == 0))
  497. {
  498. this->CheckStampFile = args[++i];
  499. }
  500. else if((i < args.size()-1) && (arg.find("--check-stamp-list",0) == 0))
  501. {
  502. this->CheckStampList = args[++i];
  503. }
  504. #if defined(CMAKE_HAVE_VS_GENERATORS)
  505. else if((i < args.size()-1) && (arg.find("--vs-solution-file",0) == 0))
  506. {
  507. this->VSSolutionFile = args[++i];
  508. }
  509. #endif
  510. else if(arg.find("-V",0) == 0)
  511. {
  512. this->Verbose = true;
  513. }
  514. else if(arg.find("-D",0) == 0)
  515. {
  516. // skip for now
  517. }
  518. else if(arg.find("-U",0) == 0)
  519. {
  520. // skip for now
  521. }
  522. else if(arg.find("-C",0) == 0)
  523. {
  524. // skip for now
  525. }
  526. else if(arg.find("-P",0) == 0)
  527. {
  528. // skip for now
  529. i++;
  530. }
  531. else if(arg.find("--find-package",0) == 0)
  532. {
  533. // skip for now
  534. i++;
  535. }
  536. else if(arg.find("-Wno-dev",0) == 0)
  537. {
  538. // skip for now
  539. }
  540. else if(arg.find("-Wdev",0) == 0)
  541. {
  542. // skip for now
  543. }
  544. else if(arg.find("--graphviz=",0) == 0)
  545. {
  546. std::string path = arg.substr(strlen("--graphviz="));
  547. path = cmSystemTools::CollapseFullPath(path);
  548. cmSystemTools::ConvertToUnixSlashes(path);
  549. this->GraphVizFile = path;
  550. if ( this->GraphVizFile.empty() )
  551. {
  552. cmSystemTools::Error("No file specified for --graphviz");
  553. }
  554. }
  555. else if(arg.find("--debug-trycompile",0) == 0)
  556. {
  557. std::cout << "debug trycompile on\n";
  558. this->DebugTryCompileOn();
  559. }
  560. else if(arg.find("--debug-output",0) == 0)
  561. {
  562. std::cout << "Running with debug output on.\n";
  563. this->SetDebugOutputOn(true);
  564. }
  565. else if(arg.find("--trace-expand",0) == 0)
  566. {
  567. std::cout << "Running with expanded trace output on.\n";
  568. this->SetTrace(true);
  569. this->SetTraceExpand(true);
  570. }
  571. else if(arg.find("--trace",0) == 0)
  572. {
  573. std::cout << "Running with trace output on.\n";
  574. this->SetTrace(true);
  575. this->SetTraceExpand(false);
  576. }
  577. else if(arg.find("--warn-uninitialized",0) == 0)
  578. {
  579. std::cout << "Warn about uninitialized values.\n";
  580. this->SetWarnUninitialized(true);
  581. }
  582. else if(arg.find("--warn-unused-vars",0) == 0)
  583. {
  584. std::cout << "Finding unused variables.\n";
  585. this->SetWarnUnused(true);
  586. }
  587. else if(arg.find("--no-warn-unused-cli",0) == 0)
  588. {
  589. std::cout << "Not searching for unused variables given on the " <<
  590. "command line.\n";
  591. this->SetWarnUnusedCli(false);
  592. }
  593. else if(arg.find("--check-system-vars",0) == 0)
  594. {
  595. std::cout << "Also check system files when warning about unused and " <<
  596. "uninitialized variables.\n";
  597. this->SetCheckSystemVars(true);
  598. }
  599. else if(arg.find("-A",0) == 0)
  600. {
  601. std::string value = arg.substr(2);
  602. if(value.empty())
  603. {
  604. ++i;
  605. if(i >= args.size())
  606. {
  607. cmSystemTools::Error("No platform specified for -A");
  608. return;
  609. }
  610. value = args[i];
  611. }
  612. if(havePlatform)
  613. {
  614. cmSystemTools::Error("Multiple -A options not allowed");
  615. return;
  616. }
  617. this->GeneratorPlatform = value;
  618. havePlatform = true;
  619. }
  620. else if(arg.find("-T",0) == 0)
  621. {
  622. std::string value = arg.substr(2);
  623. if(value.empty())
  624. {
  625. ++i;
  626. if(i >= args.size())
  627. {
  628. cmSystemTools::Error("No toolset specified for -T");
  629. return;
  630. }
  631. value = args[i];
  632. }
  633. if(haveToolset)
  634. {
  635. cmSystemTools::Error("Multiple -T options not allowed");
  636. return;
  637. }
  638. this->GeneratorToolset = value;
  639. haveToolset = true;
  640. }
  641. else if(arg.find("-G",0) == 0)
  642. {
  643. std::string value = arg.substr(2);
  644. if(value.empty())
  645. {
  646. ++i;
  647. if(i >= args.size())
  648. {
  649. cmSystemTools::Error("No generator specified for -G");
  650. this->PrintGeneratorList();
  651. return;
  652. }
  653. value = args[i];
  654. }
  655. cmGlobalGenerator* gen =
  656. this->CreateGlobalGenerator(value);
  657. if(!gen)
  658. {
  659. cmSystemTools::Error("Could not create named generator ",
  660. value.c_str());
  661. this->PrintGeneratorList();
  662. }
  663. else
  664. {
  665. this->SetGlobalGenerator(gen);
  666. }
  667. }
  668. // no option assume it is the path to the source
  669. else
  670. {
  671. directoriesSet = true;
  672. this->SetDirectoriesFromFile(arg.c_str());
  673. }
  674. }
  675. if(!directoriesSet)
  676. {
  677. this->SetHomeOutputDirectory
  678. (cmSystemTools::GetCurrentWorkingDirectory());
  679. this->SetHomeDirectory
  680. (cmSystemTools::GetCurrentWorkingDirectory());
  681. }
  682. }
  683. //----------------------------------------------------------------------------
  684. void cmake::SetDirectoriesFromFile(const char* arg)
  685. {
  686. // Check if the argument refers to a CMakeCache.txt or
  687. // CMakeLists.txt file.
  688. std::string listPath;
  689. std::string cachePath;
  690. bool argIsFile = false;
  691. if(cmSystemTools::FileIsDirectory(arg))
  692. {
  693. std::string path = cmSystemTools::CollapseFullPath(arg);
  694. cmSystemTools::ConvertToUnixSlashes(path);
  695. std::string cacheFile = path;
  696. cacheFile += "/CMakeCache.txt";
  697. std::string listFile = path;
  698. listFile += "/CMakeLists.txt";
  699. if(cmSystemTools::FileExists(cacheFile.c_str()))
  700. {
  701. cachePath = path;
  702. }
  703. if(cmSystemTools::FileExists(listFile.c_str()))
  704. {
  705. listPath = path;
  706. }
  707. }
  708. else if(cmSystemTools::FileExists(arg))
  709. {
  710. argIsFile = true;
  711. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  712. std::string name = cmSystemTools::GetFilenameName(fullPath);
  713. name = cmSystemTools::LowerCase(name);
  714. if(name == "cmakecache.txt")
  715. {
  716. cachePath = cmSystemTools::GetFilenamePath(fullPath);
  717. }
  718. else if(name == "cmakelists.txt")
  719. {
  720. listPath = cmSystemTools::GetFilenamePath(fullPath);
  721. }
  722. }
  723. else
  724. {
  725. // Specified file or directory does not exist. Try to set things
  726. // up to produce a meaningful error message.
  727. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  728. std::string name = cmSystemTools::GetFilenameName(fullPath);
  729. name = cmSystemTools::LowerCase(name);
  730. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  731. {
  732. argIsFile = true;
  733. listPath = cmSystemTools::GetFilenamePath(fullPath);
  734. }
  735. else
  736. {
  737. listPath = fullPath;
  738. }
  739. }
  740. // If there is a CMakeCache.txt file, use its settings.
  741. if(!cachePath.empty())
  742. {
  743. if(this->LoadCache(cachePath))
  744. {
  745. const char* existingValue =
  746. this->State->GetCacheEntryValue("CMAKE_HOME_DIRECTORY");
  747. if (existingValue)
  748. {
  749. this->SetHomeOutputDirectory(cachePath);
  750. this->SetHomeDirectory(existingValue);
  751. return;
  752. }
  753. }
  754. }
  755. // If there is a CMakeLists.txt file, use it as the source tree.
  756. if(!listPath.empty())
  757. {
  758. this->SetHomeDirectory(listPath);
  759. if(argIsFile)
  760. {
  761. // Source CMakeLists.txt file given. It was probably dropped
  762. // onto the executable in a GUI. Default to an in-source build.
  763. this->SetHomeOutputDirectory(listPath);
  764. }
  765. else
  766. {
  767. // Source directory given on command line. Use current working
  768. // directory as build tree.
  769. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  770. this->SetHomeOutputDirectory(cwd);
  771. }
  772. return;
  773. }
  774. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  775. // argument. Assume it is the path to the source tree, and use the
  776. // current working directory as the build tree.
  777. std::string full = cmSystemTools::CollapseFullPath(arg);
  778. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  779. this->SetHomeDirectory(full);
  780. this->SetHomeOutputDirectory(cwd);
  781. }
  782. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
  783. // cache
  784. int cmake::AddCMakePaths()
  785. {
  786. // Save the value in the cache
  787. this->CacheManager->AddCacheEntry
  788. ("CMAKE_COMMAND", cmSystemTools::GetCMakeCommand().c_str(),
  789. "Path to CMake executable.", cmState::INTERNAL);
  790. #ifdef CMAKE_BUILD_WITH_CMAKE
  791. this->CacheManager->AddCacheEntry
  792. ("CMAKE_CTEST_COMMAND", cmSystemTools::GetCTestCommand().c_str(),
  793. "Path to ctest program executable.", cmState::INTERNAL);
  794. this->CacheManager->AddCacheEntry
  795. ("CMAKE_CPACK_COMMAND", cmSystemTools::GetCPackCommand().c_str(),
  796. "Path to cpack program executable.", cmState::INTERNAL);
  797. #endif
  798. if(!cmSystemTools::FileExists(
  799. (cmSystemTools::GetCMakeRoot()+"/Modules/CMake.cmake").c_str()))
  800. {
  801. // couldn't find modules
  802. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  803. "CMake has most likely not been installed correctly.\n"
  804. "Modules directory not found in\n",
  805. cmSystemTools::GetCMakeRoot().c_str());
  806. return 0;
  807. }
  808. this->CacheManager->AddCacheEntry
  809. ("CMAKE_ROOT", cmSystemTools::GetCMakeRoot().c_str(),
  810. "Path to CMake installation.", cmState::INTERNAL);
  811. return 1;
  812. }
  813. void cmake::AddExtraGenerator(const std::string& name,
  814. CreateExtraGeneratorFunctionType newFunction)
  815. {
  816. cmExternalMakefileProjectGenerator* extraGenerator = newFunction();
  817. const std::vector<std::string>& supportedGlobalGenerators =
  818. extraGenerator->GetSupportedGlobalGenerators();
  819. for(std::vector<std::string>::const_iterator
  820. it = supportedGlobalGenerators.begin();
  821. it != supportedGlobalGenerators.end();
  822. ++it )
  823. {
  824. std::string fullName = cmExternalMakefileProjectGenerator::
  825. CreateFullGeneratorName(*it, name);
  826. this->ExtraGenerators[fullName] = newFunction;
  827. }
  828. delete extraGenerator;
  829. }
  830. void cmake::AddDefaultExtraGenerators()
  831. {
  832. #if defined(CMAKE_BUILD_WITH_CMAKE)
  833. #if defined(_WIN32) && !defined(__CYGWIN__)
  834. // e.g. kdevelop4 ?
  835. #endif
  836. this->AddExtraGenerator(cmExtraCodeBlocksGenerator::GetActualName(),
  837. &cmExtraCodeBlocksGenerator::New);
  838. this->AddExtraGenerator(cmExtraCodeLiteGenerator::GetActualName(),
  839. &cmExtraCodeLiteGenerator::New);
  840. this->AddExtraGenerator(cmExtraSublimeTextGenerator::GetActualName(),
  841. &cmExtraSublimeTextGenerator::New);
  842. this->AddExtraGenerator(cmExtraKateGenerator::GetActualName(),
  843. &cmExtraKateGenerator::New);
  844. #ifdef CMAKE_USE_ECLIPSE
  845. this->AddExtraGenerator(cmExtraEclipseCDT4Generator::GetActualName(),
  846. &cmExtraEclipseCDT4Generator::New);
  847. #endif
  848. #ifdef CMAKE_USE_KDEVELOP
  849. this->AddExtraGenerator(cmGlobalKdevelopGenerator::GetActualName(),
  850. &cmGlobalKdevelopGenerator::New);
  851. // for kdevelop also add the generator with just the name of the
  852. // extra generator, since it was this way since cmake 2.2
  853. this->ExtraGenerators[cmGlobalKdevelopGenerator::GetActualName()]
  854. = &cmGlobalKdevelopGenerator::New;
  855. #endif
  856. #endif
  857. }
  858. //----------------------------------------------------------------------------
  859. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  860. {
  861. for(RegisteredGeneratorsVector::const_iterator i = this->Generators.begin();
  862. i != this->Generators.end(); ++i)
  863. {
  864. (*i)->GetGenerators(names);
  865. }
  866. for(RegisteredExtraGeneratorsMap::const_iterator
  867. i = this->ExtraGenerators.begin();
  868. i != this->ExtraGenerators.end(); ++i)
  869. {
  870. names.push_back(i->first);
  871. }
  872. }
  873. cmGlobalGenerator* cmake::CreateGlobalGenerator(const std::string& gname)
  874. {
  875. cmExternalMakefileProjectGenerator* extraGenerator = 0;
  876. std::string name = gname;
  877. RegisteredExtraGeneratorsMap::const_iterator extraGenIt =
  878. this->ExtraGenerators.find(name);
  879. if (extraGenIt != this->ExtraGenerators.end())
  880. {
  881. extraGenerator = (extraGenIt->second)();
  882. name = extraGenerator->GetGlobalGeneratorName(name);
  883. }
  884. cmGlobalGenerator* generator = 0;
  885. for (RegisteredGeneratorsVector::const_iterator i =
  886. this->Generators.begin(); i != this->Generators.end(); ++i)
  887. {
  888. generator = (*i)->CreateGlobalGenerator(name, this);
  889. if (generator)
  890. {
  891. break;
  892. }
  893. }
  894. if (generator)
  895. {
  896. generator->SetExternalMakefileProjectGenerator(extraGenerator);
  897. }
  898. else
  899. {
  900. delete extraGenerator;
  901. }
  902. return generator;
  903. }
  904. void cmake::SetHomeDirectory(const std::string& dir)
  905. {
  906. this->State->SetSourceDirectory(dir);
  907. }
  908. const char* cmake::GetHomeDirectory() const
  909. {
  910. return this->State->GetSourceDirectory();
  911. }
  912. void cmake::SetHomeOutputDirectory(const std::string& dir)
  913. {
  914. this->State->SetBinaryDirectory(dir);
  915. }
  916. const char* cmake::GetHomeOutputDirectory() const
  917. {
  918. return this->State->GetBinaryDirectory();
  919. }
  920. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  921. {
  922. if(!gg)
  923. {
  924. cmSystemTools::Error("Error SetGlobalGenerator called with null");
  925. return;
  926. }
  927. // delete the old generator
  928. if (this->GlobalGenerator)
  929. {
  930. delete this->GlobalGenerator;
  931. // restore the original environment variables CXX and CC
  932. // Restore CC
  933. std::string env = "CC=";
  934. if(!this->CCEnvironment.empty())
  935. {
  936. env += this->CCEnvironment;
  937. }
  938. cmSystemTools::PutEnv(env);
  939. env = "CXX=";
  940. if(!this->CXXEnvironment.empty())
  941. {
  942. env += this->CXXEnvironment;
  943. }
  944. cmSystemTools::PutEnv(env);
  945. }
  946. // set the new
  947. this->GlobalGenerator = gg;
  948. // set the global flag for unix style paths on cmSystemTools as soon as
  949. // the generator is set. This allows gmake to be used on windows.
  950. cmSystemTools::SetForceUnixPaths
  951. (this->GlobalGenerator->GetForceUnixPaths());
  952. // Save the environment variables CXX and CC
  953. const char* cxx = getenv("CXX");
  954. const char* cc = getenv("CC");
  955. if(cxx)
  956. {
  957. this->CXXEnvironment = cxx;
  958. }
  959. else
  960. {
  961. this->CXXEnvironment = "";
  962. }
  963. if(cc)
  964. {
  965. this->CCEnvironment = cc;
  966. }
  967. else
  968. {
  969. this->CCEnvironment = "";
  970. }
  971. }
  972. int cmake::DoPreConfigureChecks()
  973. {
  974. // Make sure the Source directory contains a CMakeLists.txt file.
  975. std::string srcList = this->GetHomeDirectory();
  976. srcList += "/CMakeLists.txt";
  977. if(!cmSystemTools::FileExists(srcList.c_str()))
  978. {
  979. std::ostringstream err;
  980. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  981. {
  982. err << "The source directory \"" << this->GetHomeDirectory()
  983. << "\" does not appear to contain CMakeLists.txt.\n";
  984. }
  985. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  986. {
  987. err << "The source directory \"" << this->GetHomeDirectory()
  988. << "\" is a file, not a directory.\n";
  989. }
  990. else
  991. {
  992. err << "The source directory \"" << this->GetHomeDirectory()
  993. << "\" does not exist.\n";
  994. }
  995. err << "Specify --help for usage, or press the help button on the CMake "
  996. "GUI.";
  997. cmSystemTools::Error(err.str().c_str());
  998. return -2;
  999. }
  1000. // do a sanity check on some values
  1001. if(this->CacheManager->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY"))
  1002. {
  1003. std::string cacheStart =
  1004. this->CacheManager->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY");
  1005. cacheStart += "/CMakeLists.txt";
  1006. std::string currentStart = this->GetHomeDirectory();
  1007. currentStart += "/CMakeLists.txt";
  1008. if(!cmSystemTools::SameFile(cacheStart, currentStart))
  1009. {
  1010. std::string message = "The source \"";
  1011. message += currentStart;
  1012. message += "\" does not match the source \"";
  1013. message += cacheStart;
  1014. message += "\" used to generate cache. ";
  1015. message += "Re-run cmake with a different source directory.";
  1016. cmSystemTools::Error(message.c_str());
  1017. return -2;
  1018. }
  1019. }
  1020. else
  1021. {
  1022. return 0;
  1023. }
  1024. return 1;
  1025. }
  1026. struct SaveCacheEntry
  1027. {
  1028. std::string key;
  1029. std::string value;
  1030. std::string help;
  1031. cmState::CacheEntryType type;
  1032. };
  1033. int cmake::HandleDeleteCacheVariables(const std::string& var)
  1034. {
  1035. std::vector<std::string> argsSplit;
  1036. cmSystemTools::ExpandListArgument(std::string(var), argsSplit, true);
  1037. // erase the property to avoid infinite recursion
  1038. this->State
  1039. ->SetGlobalProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_", "");
  1040. if(this->State->GetIsInTryCompile())
  1041. {
  1042. return 0;
  1043. }
  1044. std::vector<SaveCacheEntry> saved;
  1045. std::ostringstream warning;
  1046. warning
  1047. << "You have changed variables that require your cache to be deleted.\n"
  1048. << "Configure will be re-run and you may have to reset some variables.\n"
  1049. << "The following variables have changed:\n";
  1050. for(std::vector<std::string>::iterator i = argsSplit.begin();
  1051. i != argsSplit.end(); ++i)
  1052. {
  1053. SaveCacheEntry save;
  1054. save.key = *i;
  1055. warning << *i << "= ";
  1056. i++;
  1057. save.value = *i;
  1058. warning << *i << "\n";
  1059. const char* existingValue =
  1060. this->CacheManager->GetCacheEntryValue(save.key);
  1061. if(existingValue)
  1062. {
  1063. save.type = this->CacheManager->GetCacheEntryType(save.key);
  1064. if(const char* help =
  1065. this->CacheManager->GetCacheEntryProperty(save.key, "HELPSTRING"))
  1066. {
  1067. save.help = help;
  1068. }
  1069. }
  1070. saved.push_back(save);
  1071. }
  1072. // remove the cache
  1073. this->CacheManager->DeleteCache(this->GetHomeOutputDirectory());
  1074. // load the empty cache
  1075. this->LoadCache();
  1076. // restore the changed compilers
  1077. for(std::vector<SaveCacheEntry>::iterator i = saved.begin();
  1078. i != saved.end(); ++i)
  1079. {
  1080. this->AddCacheEntry(i->key, i->value.c_str(),
  1081. i->help.c_str(), i->type);
  1082. }
  1083. cmSystemTools::Message(warning.str().c_str());
  1084. // avoid reconfigure if there were errors
  1085. if(!cmSystemTools::GetErrorOccuredFlag())
  1086. {
  1087. // re-run configure
  1088. return this->Configure();
  1089. }
  1090. return 0;
  1091. }
  1092. int cmake::Configure()
  1093. {
  1094. if(this->DoSuppressDevWarnings)
  1095. {
  1096. if(this->SuppressDevWarnings)
  1097. {
  1098. this->CacheManager->
  1099. AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "TRUE",
  1100. "Suppress Warnings that are meant for"
  1101. " the author of the CMakeLists.txt files.",
  1102. cmState::INTERNAL);
  1103. }
  1104. else
  1105. {
  1106. this->CacheManager->
  1107. AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "FALSE",
  1108. "Suppress Warnings that are meant for"
  1109. " the author of the CMakeLists.txt files.",
  1110. cmState::INTERNAL);
  1111. }
  1112. }
  1113. int ret = this->ActualConfigure();
  1114. const char* delCacheVars = this->State
  1115. ->GetGlobalProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_");
  1116. if(delCacheVars && delCacheVars[0] != 0)
  1117. {
  1118. return this->HandleDeleteCacheVariables(delCacheVars);
  1119. }
  1120. return ret;
  1121. }
  1122. int cmake::ActualConfigure()
  1123. {
  1124. // Construct right now our path conversion table before it's too late:
  1125. this->UpdateConversionPathTable();
  1126. this->CleanupCommandsAndMacros();
  1127. int res = 0;
  1128. if ( this->GetWorkingMode() == NORMAL_MODE )
  1129. {
  1130. res = this->DoPreConfigureChecks();
  1131. }
  1132. if ( res < 0 )
  1133. {
  1134. return -2;
  1135. }
  1136. if ( !res )
  1137. {
  1138. this->CacheManager->AddCacheEntry
  1139. ("CMAKE_HOME_DIRECTORY",
  1140. this->GetHomeDirectory(),
  1141. "Source directory with the top level CMakeLists.txt file for this "
  1142. "project",
  1143. cmState::INTERNAL);
  1144. }
  1145. // no generator specified on the command line
  1146. if(!this->GlobalGenerator)
  1147. {
  1148. const char* genName =
  1149. this->CacheManager->GetInitializedCacheValue("CMAKE_GENERATOR");
  1150. const char* extraGenName =
  1151. this->CacheManager->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
  1152. if(genName)
  1153. {
  1154. std::string fullName = cmExternalMakefileProjectGenerator::
  1155. CreateFullGeneratorName(genName,
  1156. extraGenName ? extraGenName : "");
  1157. this->GlobalGenerator = this->CreateGlobalGenerator(fullName);
  1158. }
  1159. if(this->GlobalGenerator)
  1160. {
  1161. // set the global flag for unix style paths on cmSystemTools as
  1162. // soon as the generator is set. This allows gmake to be used
  1163. // on windows.
  1164. cmSystemTools::SetForceUnixPaths
  1165. (this->GlobalGenerator->GetForceUnixPaths());
  1166. }
  1167. else
  1168. {
  1169. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1170. std::string installedCompiler;
  1171. // Try to find the newest VS installed on the computer and
  1172. // use that as a default if -G is not specified
  1173. const std::string vsregBase =
  1174. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\";
  1175. std::vector<std::string> vsVerions;
  1176. vsVerions.push_back("VisualStudio\\");
  1177. vsVerions.push_back("VCExpress\\");
  1178. vsVerions.push_back("WDExpress\\");
  1179. struct VSRegistryEntryName
  1180. {
  1181. const char* MSVersion;
  1182. const char* GeneratorName;
  1183. };
  1184. VSRegistryEntryName version[] = {
  1185. {"6.0", "Visual Studio 6"},
  1186. {"7.0", "Visual Studio 7"},
  1187. {"7.1", "Visual Studio 7 .NET 2003"},
  1188. {"8.0", "Visual Studio 8 2005"},
  1189. {"9.0", "Visual Studio 9 2008"},
  1190. {"10.0", "Visual Studio 10 2010"},
  1191. {"11.0", "Visual Studio 11 2012"},
  1192. {"12.0", "Visual Studio 12 2013"},
  1193. {"14.0", "Visual Studio 14 2015"},
  1194. {0, 0}};
  1195. for(int i=0; version[i].MSVersion != 0; i++)
  1196. {
  1197. for(size_t b=0; b < vsVerions.size(); b++)
  1198. {
  1199. std::string reg = vsregBase + vsVerions[b] + version[i].MSVersion;
  1200. reg += ";InstallDir]";
  1201. cmSystemTools::ExpandRegistryValues(reg,
  1202. cmSystemTools::KeyWOW64_32);
  1203. if (!(reg == "/registry"))
  1204. {
  1205. installedCompiler = version[i].GeneratorName;
  1206. break;
  1207. }
  1208. }
  1209. }
  1210. cmGlobalGenerator* gen
  1211. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1212. if(!gen)
  1213. {
  1214. gen = new cmGlobalNMakeMakefileGenerator(this);
  1215. }
  1216. this->SetGlobalGenerator(gen);
  1217. std::cout << "-- Building for: " << gen->GetName() << "\n";
  1218. #else
  1219. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3(this));
  1220. #endif
  1221. }
  1222. if(!this->GlobalGenerator)
  1223. {
  1224. cmSystemTools::Error("Could not create generator");
  1225. return -1;
  1226. }
  1227. }
  1228. const char* genName = this->CacheManager
  1229. ->GetInitializedCacheValue("CMAKE_GENERATOR");
  1230. if(genName)
  1231. {
  1232. if(!this->GlobalGenerator->MatchesGeneratorName(genName))
  1233. {
  1234. std::string message = "Error: generator : ";
  1235. message += this->GlobalGenerator->GetName();
  1236. message += "\nDoes not match the generator used previously: ";
  1237. message += genName;
  1238. message +=
  1239. "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1240. "directory or choose a different binary directory.";
  1241. cmSystemTools::Error(message.c_str());
  1242. return -2;
  1243. }
  1244. }
  1245. if(!this->CacheManager->GetInitializedCacheValue("CMAKE_GENERATOR"))
  1246. {
  1247. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1248. this->GlobalGenerator->GetName().c_str(),
  1249. "Name of generator.",
  1250. cmState::INTERNAL);
  1251. this->CacheManager->AddCacheEntry("CMAKE_EXTRA_GENERATOR",
  1252. this->GlobalGenerator->GetExtraGeneratorName().c_str(),
  1253. "Name of external makefile project generator.",
  1254. cmState::INTERNAL);
  1255. }
  1256. if(const char* platformName =
  1257. this->CacheManager->GetInitializedCacheValue("CMAKE_GENERATOR_PLATFORM"))
  1258. {
  1259. if(this->GeneratorPlatform.empty())
  1260. {
  1261. this->GeneratorPlatform = platformName;
  1262. }
  1263. else if(this->GeneratorPlatform != platformName)
  1264. {
  1265. std::string message = "Error: generator platform: ";
  1266. message += this->GeneratorPlatform;
  1267. message += "\nDoes not match the platform used previously: ";
  1268. message += platformName;
  1269. message +=
  1270. "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1271. "directory or choose a different binary directory.";
  1272. cmSystemTools::Error(message.c_str());
  1273. return -2;
  1274. }
  1275. }
  1276. else
  1277. {
  1278. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR_PLATFORM",
  1279. this->GeneratorPlatform.c_str(),
  1280. "Name of generator platform.",
  1281. cmState::INTERNAL);
  1282. }
  1283. if(const char* tsName =
  1284. this->CacheManager->GetInitializedCacheValue("CMAKE_GENERATOR_TOOLSET"))
  1285. {
  1286. if(this->GeneratorToolset.empty())
  1287. {
  1288. this->GeneratorToolset = tsName;
  1289. }
  1290. else if(this->GeneratorToolset != tsName)
  1291. {
  1292. std::string message = "Error: generator toolset: ";
  1293. message += this->GeneratorToolset;
  1294. message += "\nDoes not match the toolset used previously: ";
  1295. message += tsName;
  1296. message +=
  1297. "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1298. "directory or choose a different binary directory.";
  1299. cmSystemTools::Error(message.c_str());
  1300. return -2;
  1301. }
  1302. }
  1303. else
  1304. {
  1305. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR_TOOLSET",
  1306. this->GeneratorToolset.c_str(),
  1307. "Name of generator toolset.",
  1308. cmState::INTERNAL);
  1309. }
  1310. // reset any system configuration information, except for when we are
  1311. // InTryCompile. With TryCompile the system info is taken from the parent's
  1312. // info to save time
  1313. if (!this->State->GetIsInTryCompile())
  1314. {
  1315. this->GlobalGenerator->ClearEnabledLanguages();
  1316. this->TruncateOutputLog("CMakeOutput.log");
  1317. this->TruncateOutputLog("CMakeError.log");
  1318. }
  1319. // actually do the configure
  1320. this->GlobalGenerator->Configure();
  1321. // Before saving the cache
  1322. // if the project did not define one of the entries below, add them now
  1323. // so users can edit the values in the cache:
  1324. // We used to always present LIBRARY_OUTPUT_PATH and
  1325. // EXECUTABLE_OUTPUT_PATH. They are now documented as old-style and
  1326. // should no longer be used. Therefore we present them only if the
  1327. // project requires compatibility with CMake 2.4. We detect this
  1328. // here by looking for the old CMAKE_BACKWARDS_COMPATIBILITY
  1329. // variable created when CMP0001 is not set to NEW.
  1330. if(this->State
  1331. ->GetInitializedCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1332. {
  1333. if(!this->State->GetInitializedCacheValue("LIBRARY_OUTPUT_PATH"))
  1334. {
  1335. this->State->AddCacheEntry
  1336. ("LIBRARY_OUTPUT_PATH", "",
  1337. "Single output directory for building all libraries.",
  1338. cmState::PATH);
  1339. }
  1340. if(!this->State
  1341. ->GetInitializedCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1342. {
  1343. this->State->AddCacheEntry
  1344. ("EXECUTABLE_OUTPUT_PATH", "",
  1345. "Single output directory for building all executables.",
  1346. cmState::PATH);
  1347. }
  1348. }
  1349. cmMakefile* mf=this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  1350. if (mf->IsOn("CTEST_USE_LAUNCHERS")
  1351. && !this->State->GetGlobalProperty("RULE_LAUNCH_COMPILE"))
  1352. {
  1353. cmSystemTools::Error("CTEST_USE_LAUNCHERS is enabled, but the "
  1354. "RULE_LAUNCH_COMPILE global property is not defined.\n"
  1355. "Did you forget to include(CTest) in the toplevel "
  1356. "CMakeLists.txt ?");
  1357. }
  1358. // only save the cache if there were no fatal errors
  1359. if ( this->GetWorkingMode() == NORMAL_MODE )
  1360. {
  1361. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1362. }
  1363. if(cmSystemTools::GetErrorOccuredFlag())
  1364. {
  1365. return -1;
  1366. }
  1367. return 0;
  1368. }
  1369. void cmake::PreLoadCMakeFiles()
  1370. {
  1371. std::vector<std::string> args;
  1372. std::string pre_load = this->GetHomeDirectory();
  1373. if (!pre_load.empty())
  1374. {
  1375. pre_load += "/PreLoad.cmake";
  1376. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1377. {
  1378. this->ReadListFile(args, pre_load.c_str());
  1379. }
  1380. }
  1381. pre_load = this->GetHomeOutputDirectory();
  1382. if (!pre_load.empty())
  1383. {
  1384. pre_load += "/PreLoad.cmake";
  1385. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1386. {
  1387. this->ReadListFile(args, pre_load.c_str());
  1388. }
  1389. }
  1390. }
  1391. // handle a command line invocation
  1392. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1393. {
  1394. // Process the arguments
  1395. this->SetArgs(args);
  1396. if(cmSystemTools::GetErrorOccuredFlag())
  1397. {
  1398. return -1;
  1399. }
  1400. // If we are given a stamp list file check if it is really out of date.
  1401. if(!this->CheckStampList.empty() &&
  1402. cmakeCheckStampList(this->CheckStampList.c_str()))
  1403. {
  1404. return 0;
  1405. }
  1406. // If we are given a stamp file check if it is really out of date.
  1407. if(!this->CheckStampFile.empty() &&
  1408. cmakeCheckStampFile(this->CheckStampFile.c_str()))
  1409. {
  1410. return 0;
  1411. }
  1412. if ( this->GetWorkingMode() == NORMAL_MODE )
  1413. {
  1414. // load the cache
  1415. if(this->LoadCache() < 0)
  1416. {
  1417. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1418. return -1;
  1419. }
  1420. }
  1421. else
  1422. {
  1423. this->AddCMakePaths();
  1424. }
  1425. // Add any cache args
  1426. if ( !this->SetCacheArgs(args) )
  1427. {
  1428. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1429. return -1;
  1430. }
  1431. // In script mode we terminate after running the script.
  1432. if(this->GetWorkingMode() != NORMAL_MODE)
  1433. {
  1434. if(cmSystemTools::GetErrorOccuredFlag())
  1435. {
  1436. return -1;
  1437. }
  1438. else
  1439. {
  1440. return 0;
  1441. }
  1442. }
  1443. // If MAKEFLAGS are given in the environment, remove the environment
  1444. // variable. This will prevent try-compile from succeeding when it
  1445. // should fail (if "-i" is an option). We cannot simply test
  1446. // whether "-i" is given and remove it because some make programs
  1447. // encode the MAKEFLAGS variable in a strange way.
  1448. if(getenv("MAKEFLAGS"))
  1449. {
  1450. cmSystemTools::PutEnv("MAKEFLAGS=");
  1451. }
  1452. this->PreLoadCMakeFiles();
  1453. if ( noconfigure )
  1454. {
  1455. return 0;
  1456. }
  1457. // now run the global generate
  1458. // Check the state of the build system to see if we need to regenerate.
  1459. if(!this->CheckBuildSystem())
  1460. {
  1461. return 0;
  1462. }
  1463. int ret = this->Configure();
  1464. if (ret || this->GetWorkingMode() != NORMAL_MODE)
  1465. {
  1466. #if defined(CMAKE_HAVE_VS_GENERATORS)
  1467. if(!this->VSSolutionFile.empty() && this->GlobalGenerator)
  1468. {
  1469. // CMake is running to regenerate a Visual Studio build tree
  1470. // during a build from the VS IDE. The build files cannot be
  1471. // regenerated, so we should stop the build.
  1472. cmSystemTools::Message(
  1473. "CMake Configure step failed. "
  1474. "Build files cannot be regenerated correctly. "
  1475. "Attempting to stop IDE build.");
  1476. cmGlobalVisualStudioGenerator* gg =
  1477. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  1478. gg->CallVisualStudioMacro(cmGlobalVisualStudioGenerator::MacroStop,
  1479. this->VSSolutionFile.c_str());
  1480. }
  1481. #endif
  1482. return ret;
  1483. }
  1484. ret = this->Generate();
  1485. std::string message = "Build files have been written to: ";
  1486. message += this->GetHomeOutputDirectory();
  1487. this->UpdateProgress(message.c_str(), -1);
  1488. return ret;
  1489. }
  1490. int cmake::Generate()
  1491. {
  1492. if(!this->GlobalGenerator)
  1493. {
  1494. return -1;
  1495. }
  1496. if (!this->GlobalGenerator->Compute())
  1497. {
  1498. return -1;
  1499. }
  1500. this->GlobalGenerator->Generate();
  1501. if ( !this->GraphVizFile.empty() )
  1502. {
  1503. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1504. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1505. }
  1506. if(this->WarnUnusedCli)
  1507. {
  1508. this->RunCheckForUnusedVariables();
  1509. }
  1510. if(cmSystemTools::GetErrorOccuredFlag())
  1511. {
  1512. return -1;
  1513. }
  1514. // Save the cache again after a successful Generate so that any internal
  1515. // variables created during Generate are saved. (Specifically target GUIDs
  1516. // for the Visual Studio and Xcode generators.)
  1517. if ( this->GetWorkingMode() == NORMAL_MODE )
  1518. {
  1519. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1520. }
  1521. return 0;
  1522. }
  1523. void cmake::AddCacheEntry(const std::string& key, const char* value,
  1524. const char* helpString,
  1525. int type)
  1526. {
  1527. this->CacheManager->AddCacheEntry(key, value,
  1528. helpString,
  1529. cmState::CacheEntryType(type));
  1530. }
  1531. const char* cmake::GetCacheDefinition(const std::string& name) const
  1532. {
  1533. return this->CacheManager->GetInitializedCacheValue(name);
  1534. }
  1535. void cmake::AddDefaultCommands()
  1536. {
  1537. std::vector<cmCommand*> commands;
  1538. GetBootstrapCommands1(commands);
  1539. GetBootstrapCommands2(commands);
  1540. GetPredefinedCommands(commands);
  1541. for(std::vector<cmCommand*>::iterator i = commands.begin();
  1542. i != commands.end(); ++i)
  1543. {
  1544. this->State->AddCommand(*i);
  1545. }
  1546. }
  1547. void cmake::AddDefaultGenerators()
  1548. {
  1549. #if defined(_WIN32) && !defined(__CYGWIN__)
  1550. # if !defined(CMAKE_BOOT_MINGW)
  1551. this->Generators.push_back(
  1552. cmGlobalVisualStudio14Generator::NewFactory());
  1553. this->Generators.push_back(
  1554. cmGlobalVisualStudio12Generator::NewFactory());
  1555. this->Generators.push_back(
  1556. cmGlobalVisualStudio11Generator::NewFactory());
  1557. this->Generators.push_back(
  1558. cmGlobalVisualStudio10Generator::NewFactory());
  1559. this->Generators.push_back(
  1560. cmGlobalVisualStudio9Generator::NewFactory());
  1561. this->Generators.push_back(
  1562. cmGlobalVisualStudio8Generator::NewFactory());
  1563. this->Generators.push_back(
  1564. cmGlobalVisualStudio71Generator::NewFactory());
  1565. this->Generators.push_back(
  1566. cmGlobalVisualStudio7Generator::NewFactory());
  1567. this->Generators.push_back(
  1568. cmGlobalVisualStudio6Generator::NewFactory());
  1569. this->Generators.push_back(
  1570. cmGlobalBorlandMakefileGenerator::NewFactory());
  1571. this->Generators.push_back(
  1572. cmGlobalNMakeMakefileGenerator::NewFactory());
  1573. this->Generators.push_back(
  1574. cmGlobalJOMMakefileGenerator::NewFactory());
  1575. this->Generators.push_back(
  1576. cmGlobalGhsMultiGenerator::NewFactory());
  1577. # endif
  1578. this->Generators.push_back(
  1579. cmGlobalMSYSMakefileGenerator::NewFactory());
  1580. this->Generators.push_back(
  1581. cmGlobalMinGWMakefileGenerator::NewFactory());
  1582. #endif
  1583. this->Generators.push_back(
  1584. cmGlobalUnixMakefileGenerator3::NewFactory());
  1585. this->Generators.push_back(
  1586. cmGlobalNinjaGenerator::NewFactory());
  1587. #if defined(CMAKE_USE_WMAKE)
  1588. this->Generators.push_back(
  1589. cmGlobalWatcomWMakeGenerator::NewFactory());
  1590. #endif
  1591. #ifdef CMAKE_USE_XCODE
  1592. this->Generators.push_back(
  1593. cmGlobalXCodeGenerator::NewFactory());
  1594. #endif
  1595. }
  1596. bool cmake::ParseCacheEntry(const std::string& entry,
  1597. std::string& var,
  1598. std::string& value,
  1599. cmState::CacheEntryType& type)
  1600. {
  1601. return cmCacheManager::ParseEntry(entry, var, value, type);
  1602. }
  1603. int cmake::LoadCache()
  1604. {
  1605. // could we not read the cache
  1606. if (!this->LoadCache(this->GetHomeOutputDirectory()))
  1607. {
  1608. // if it does exist, but isn't readable then warn the user
  1609. std::string cacheFile = this->GetHomeOutputDirectory();
  1610. cacheFile += "/CMakeCache.txt";
  1611. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1612. {
  1613. cmSystemTools::Error(
  1614. "There is a CMakeCache.txt file for the current binary tree but "
  1615. "cmake does not have permission to read it. Please check the "
  1616. "permissions of the directory you are trying to run CMake on.");
  1617. return -1;
  1618. }
  1619. }
  1620. // setup CMAKE_ROOT and CMAKE_COMMAND
  1621. if(!this->AddCMakePaths())
  1622. {
  1623. return -3;
  1624. }
  1625. return 0;
  1626. }
  1627. bool cmake::LoadCache(const std::string& path)
  1628. {
  1629. return this->CacheManager->LoadCache(path);
  1630. }
  1631. bool cmake::LoadCache(const std::string& path, bool internal,
  1632. std::set<std::string>& excludes,
  1633. std::set<std::string>& includes)
  1634. {
  1635. return this->CacheManager->LoadCache(path, internal, excludes, includes);
  1636. }
  1637. bool cmake::SaveCache(const std::string& path)
  1638. {
  1639. return this->CacheManager->SaveCache(path);
  1640. }
  1641. bool cmake::DeleteCache(const std::string& path)
  1642. {
  1643. return this->CacheManager->DeleteCache(path);
  1644. }
  1645. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  1646. {
  1647. this->ProgressCallback = f;
  1648. this->ProgressCallbackClientData = cd;
  1649. }
  1650. void cmake::UpdateProgress(const char *msg, float prog)
  1651. {
  1652. if(this->ProgressCallback && !this->State->GetIsInTryCompile())
  1653. {
  1654. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  1655. return;
  1656. }
  1657. }
  1658. bool cmake::GetIsInTryCompile() const
  1659. {
  1660. return this->State->GetIsInTryCompile();
  1661. }
  1662. void cmake::SetIsInTryCompile(bool b)
  1663. {
  1664. this->State->SetIsInTryCompile(b);
  1665. }
  1666. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1667. {
  1668. for(RegisteredGeneratorsVector::const_iterator i =
  1669. this->Generators.begin(); i != this->Generators.end(); ++i)
  1670. {
  1671. cmDocumentationEntry e;
  1672. (*i)->GetDocumentation(e);
  1673. v.push_back(e);
  1674. }
  1675. for(RegisteredExtraGeneratorsMap::const_iterator i =
  1676. this->ExtraGenerators.begin(); i != this->ExtraGenerators.end(); ++i)
  1677. {
  1678. cmDocumentationEntry e;
  1679. cmExternalMakefileProjectGenerator* generator = (i->second)();
  1680. generator->GetDocumentation(e, i->first);
  1681. e.Name = i->first;
  1682. delete generator;
  1683. v.push_back(e);
  1684. }
  1685. }
  1686. void cmake::PrintGeneratorList()
  1687. {
  1688. #ifdef CMAKE_BUILD_WITH_CMAKE
  1689. cmDocumentation doc;
  1690. std::vector<cmDocumentationEntry> generators;
  1691. this->GetGeneratorDocumentation(generators);
  1692. doc.AppendSection("Generators",generators);
  1693. std::cerr << "\n";
  1694. doc.PrintDocumentation(cmDocumentation::ListGenerators, std::cerr);
  1695. #endif
  1696. }
  1697. void cmake::UpdateConversionPathTable()
  1698. {
  1699. // Update the path conversion table with any specified file:
  1700. const char* tablepath =
  1701. this->CacheManager
  1702. ->GetInitializedCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1703. if(tablepath)
  1704. {
  1705. cmsys::ifstream table( tablepath );
  1706. if(!table)
  1707. {
  1708. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  1709. ". CMake can not open file.");
  1710. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1711. }
  1712. else
  1713. {
  1714. std::string a, b;
  1715. while(!table.eof())
  1716. {
  1717. // two entries per line
  1718. table >> a; table >> b;
  1719. cmSystemTools::AddTranslationPath( a, b);
  1720. }
  1721. }
  1722. }
  1723. }
  1724. //----------------------------------------------------------------------------
  1725. int cmake::CheckBuildSystem()
  1726. {
  1727. // We do not need to rerun CMake. Check dependency integrity. Use
  1728. // the make system's VERBOSE environment variable to enable verbose
  1729. // output. This can be skipped by setting CMAKE_NO_VERBOSE (which is set
  1730. // by the Eclipse and KDevelop generators).
  1731. bool verbose = ((cmSystemTools::GetEnv("VERBOSE") != 0)
  1732. && (cmSystemTools::GetEnv("CMAKE_NO_VERBOSE") == 0));
  1733. // This method will check the integrity of the build system if the
  1734. // option was given on the command line. It reads the given file to
  1735. // determine whether CMake should rerun.
  1736. // If no file is provided for the check, we have to rerun.
  1737. if(this->CheckBuildSystemArgument.empty())
  1738. {
  1739. if(verbose)
  1740. {
  1741. std::ostringstream msg;
  1742. msg << "Re-run cmake no build system arguments\n";
  1743. cmSystemTools::Stdout(msg.str().c_str());
  1744. }
  1745. return 1;
  1746. }
  1747. // If the file provided does not exist, we have to rerun.
  1748. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  1749. {
  1750. if(verbose)
  1751. {
  1752. std::ostringstream msg;
  1753. msg << "Re-run cmake missing file: "
  1754. << this->CheckBuildSystemArgument << "\n";
  1755. cmSystemTools::Stdout(msg.str().c_str());
  1756. }
  1757. return 1;
  1758. }
  1759. // Read the rerun check file and use it to decide whether to do the
  1760. // global generate.
  1761. cmake cm;
  1762. cm.SetHomeDirectory("");
  1763. cm.SetHomeOutputDirectory("");
  1764. cmGlobalGenerator gg(&cm);
  1765. cmsys::auto_ptr<cmLocalGenerator> lg(gg.MakeLocalGenerator());
  1766. cmMakefile* mf = lg->GetMakefile();
  1767. if(!mf->ReadListFile(this->CheckBuildSystemArgument.c_str()) ||
  1768. cmSystemTools::GetErrorOccuredFlag())
  1769. {
  1770. if(verbose)
  1771. {
  1772. std::ostringstream msg;
  1773. msg << "Re-run cmake error reading : "
  1774. << this->CheckBuildSystemArgument << "\n";
  1775. cmSystemTools::Stdout(msg.str().c_str());
  1776. }
  1777. // There was an error reading the file. Just rerun.
  1778. return 1;
  1779. }
  1780. if(this->ClearBuildSystem)
  1781. {
  1782. // Get the generator used for this build system.
  1783. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1784. if(!genName || genName[0] == '\0')
  1785. {
  1786. genName = "Unix Makefiles";
  1787. }
  1788. // Create the generator and use it to clear the dependencies.
  1789. cmsys::auto_ptr<cmGlobalGenerator>
  1790. ggd(this->CreateGlobalGenerator(genName));
  1791. if(ggd.get())
  1792. {
  1793. cmsys::auto_ptr<cmLocalGenerator> lgd(ggd->MakeLocalGenerator());
  1794. lgd->ClearDependencies(mf, verbose);
  1795. }
  1796. }
  1797. // If any byproduct of makefile generation is missing we must re-run.
  1798. std::vector<std::string> products;
  1799. if(const char* productStr = mf->GetDefinition("CMAKE_MAKEFILE_PRODUCTS"))
  1800. {
  1801. cmSystemTools::ExpandListArgument(productStr, products);
  1802. }
  1803. for(std::vector<std::string>::const_iterator pi = products.begin();
  1804. pi != products.end(); ++pi)
  1805. {
  1806. if(!(cmSystemTools::FileExists(pi->c_str()) ||
  1807. cmSystemTools::FileIsSymlink(*pi)))
  1808. {
  1809. if(verbose)
  1810. {
  1811. std::ostringstream msg;
  1812. msg << "Re-run cmake, missing byproduct: " << *pi << "\n";
  1813. cmSystemTools::Stdout(msg.str().c_str());
  1814. }
  1815. return 1;
  1816. }
  1817. }
  1818. // Get the set of dependencies and outputs.
  1819. std::vector<std::string> depends;
  1820. std::vector<std::string> outputs;
  1821. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1822. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1823. if(dependsStr && outputsStr)
  1824. {
  1825. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1826. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1827. }
  1828. if(depends.empty() || outputs.empty())
  1829. {
  1830. // Not enough information was provided to do the test. Just rerun.
  1831. if(verbose)
  1832. {
  1833. std::ostringstream msg;
  1834. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  1835. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  1836. cmSystemTools::Stdout(msg.str().c_str());
  1837. }
  1838. return 1;
  1839. }
  1840. // Find the newest dependency.
  1841. std::vector<std::string>::iterator dep = depends.begin();
  1842. std::string dep_newest = *dep++;
  1843. for(;dep != depends.end(); ++dep)
  1844. {
  1845. int result = 0;
  1846. if(this->FileComparison->FileTimeCompare(dep_newest.c_str(),
  1847. dep->c_str(), &result))
  1848. {
  1849. if(result < 0)
  1850. {
  1851. dep_newest = *dep;
  1852. }
  1853. }
  1854. else
  1855. {
  1856. if(verbose)
  1857. {
  1858. std::ostringstream msg;
  1859. msg << "Re-run cmake: build system dependency is missing\n";
  1860. cmSystemTools::Stdout(msg.str().c_str());
  1861. }
  1862. return 1;
  1863. }
  1864. }
  1865. // Find the oldest output.
  1866. std::vector<std::string>::iterator out = outputs.begin();
  1867. std::string out_oldest = *out++;
  1868. for(;out != outputs.end(); ++out)
  1869. {
  1870. int result = 0;
  1871. if(this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  1872. out->c_str(), &result))
  1873. {
  1874. if(result > 0)
  1875. {
  1876. out_oldest = *out;
  1877. }
  1878. }
  1879. else
  1880. {
  1881. if(verbose)
  1882. {
  1883. std::ostringstream msg;
  1884. msg << "Re-run cmake: build system output is missing\n";
  1885. cmSystemTools::Stdout(msg.str().c_str());
  1886. }
  1887. return 1;
  1888. }
  1889. }
  1890. // If any output is older than any dependency then rerun.
  1891. {
  1892. int result = 0;
  1893. if(!this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  1894. dep_newest.c_str(),
  1895. &result) ||
  1896. result < 0)
  1897. {
  1898. if(verbose)
  1899. {
  1900. std::ostringstream msg;
  1901. msg << "Re-run cmake file: " << out_oldest
  1902. << " older than: " << dep_newest << "\n";
  1903. cmSystemTools::Stdout(msg.str().c_str());
  1904. }
  1905. return 1;
  1906. }
  1907. }
  1908. // No need to rerun.
  1909. return 0;
  1910. }
  1911. //----------------------------------------------------------------------------
  1912. void cmake::TruncateOutputLog(const char* fname)
  1913. {
  1914. std::string fullPath = this->GetHomeOutputDirectory();
  1915. fullPath += "/";
  1916. fullPath += fname;
  1917. struct stat st;
  1918. if ( ::stat(fullPath.c_str(), &st) )
  1919. {
  1920. return;
  1921. }
  1922. if (!this->State->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR"))
  1923. {
  1924. cmSystemTools::RemoveFile(fullPath);
  1925. return;
  1926. }
  1927. off_t fsize = st.st_size;
  1928. const off_t maxFileSize = 50 * 1024;
  1929. if ( fsize < maxFileSize )
  1930. {
  1931. //TODO: truncate file
  1932. return;
  1933. }
  1934. }
  1935. inline std::string removeQuotes(const std::string& s)
  1936. {
  1937. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1938. {
  1939. return s.substr(1, s.size()-2);
  1940. }
  1941. return s;
  1942. }
  1943. void cmake::MarkCliAsUsed(const std::string& variable)
  1944. {
  1945. this->UsedCliVariables[variable] = true;
  1946. }
  1947. void cmake::GenerateGraphViz(const char* fileName) const
  1948. {
  1949. #ifdef CMAKE_BUILD_WITH_CMAKE
  1950. cmsys::auto_ptr<cmGraphVizWriter> gvWriter(
  1951. new cmGraphVizWriter(this->GetGlobalGenerator()->GetLocalGenerators()));
  1952. std::string settingsFile = this->GetHomeOutputDirectory();
  1953. settingsFile += "/CMakeGraphVizOptions.cmake";
  1954. std::string fallbackSettingsFile = this->GetHomeDirectory();
  1955. fallbackSettingsFile += "/CMakeGraphVizOptions.cmake";
  1956. gvWriter->ReadSettings(settingsFile.c_str(), fallbackSettingsFile.c_str());
  1957. gvWriter->WritePerTargetFiles(fileName);
  1958. gvWriter->WriteTargetDependersFiles(fileName);
  1959. gvWriter->WriteGlobalFile(fileName);
  1960. #endif
  1961. }
  1962. void cmake::SetProperty(const std::string& prop, const char* value)
  1963. {
  1964. this->State->SetGlobalProperty(prop, value);
  1965. }
  1966. void cmake::AppendProperty(const std::string& prop,
  1967. const char* value, bool asString)
  1968. {
  1969. this->State->AppendGlobalProperty(prop, value, asString);
  1970. }
  1971. const char *cmake::GetProperty(const std::string& prop)
  1972. {
  1973. return this->State->GetGlobalProperty(prop);
  1974. }
  1975. bool cmake::GetPropertyAsBool(const std::string& prop)
  1976. {
  1977. return this->State->GetGlobalPropertyAsBool(prop);
  1978. }
  1979. cmInstalledFile *cmake::GetOrCreateInstalledFile(
  1980. cmMakefile* mf, const std::string& name)
  1981. {
  1982. std::map<std::string, cmInstalledFile>::iterator i =
  1983. this->InstalledFiles.find(name);
  1984. if(i != this->InstalledFiles.end())
  1985. {
  1986. cmInstalledFile &file = i->second;
  1987. return &file;
  1988. }
  1989. else
  1990. {
  1991. cmInstalledFile &file = this->InstalledFiles[name];
  1992. file.SetName(mf, name);
  1993. return &file;
  1994. }
  1995. }
  1996. cmInstalledFile const* cmake::GetInstalledFile(const std::string& name) const
  1997. {
  1998. std::map<std::string, cmInstalledFile>::const_iterator i =
  1999. this->InstalledFiles.find(name);
  2000. if(i != this->InstalledFiles.end())
  2001. {
  2002. cmInstalledFile const& file = i->second;
  2003. return &file;
  2004. }
  2005. else
  2006. {
  2007. return 0;
  2008. }
  2009. }
  2010. int cmake::GetSystemInformation(std::vector<std::string>& args)
  2011. {
  2012. // so create the directory
  2013. std::string resultFile;
  2014. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2015. std::string destPath = cwd + "/__cmake_systeminformation";
  2016. cmSystemTools::RemoveADirectory(destPath);
  2017. if (!cmSystemTools::MakeDirectory(destPath.c_str()))
  2018. {
  2019. std::cerr << "Error: --system-information must be run from a "
  2020. "writable directory!\n";
  2021. return 1;
  2022. }
  2023. // process the arguments
  2024. bool writeToStdout = true;
  2025. for(unsigned int i=1; i < args.size(); ++i)
  2026. {
  2027. std::string arg = args[i];
  2028. if(arg.find("-V",0) == 0)
  2029. {
  2030. this->Verbose = true;
  2031. }
  2032. else if(arg.find("-G",0) == 0)
  2033. {
  2034. std::string value = arg.substr(2);
  2035. if(value.empty())
  2036. {
  2037. ++i;
  2038. if(i >= args.size())
  2039. {
  2040. cmSystemTools::Error("No generator specified for -G");
  2041. this->PrintGeneratorList();
  2042. return -1;
  2043. }
  2044. value = args[i];
  2045. }
  2046. cmGlobalGenerator* gen =
  2047. this->CreateGlobalGenerator(value);
  2048. if(!gen)
  2049. {
  2050. cmSystemTools::Error("Could not create named generator ",
  2051. value.c_str());
  2052. this->PrintGeneratorList();
  2053. }
  2054. else
  2055. {
  2056. this->SetGlobalGenerator(gen);
  2057. }
  2058. }
  2059. // no option assume it is the output file
  2060. else
  2061. {
  2062. if (!cmSystemTools::FileIsFullPath(arg.c_str()))
  2063. {
  2064. resultFile = cwd;
  2065. resultFile += "/";
  2066. }
  2067. resultFile += arg;
  2068. writeToStdout = false;
  2069. }
  2070. }
  2071. // we have to find the module directory, so we can copy the files
  2072. this->AddCMakePaths();
  2073. std::string modulesPath =
  2074. this->State->GetInitializedCacheValue("CMAKE_ROOT");
  2075. modulesPath += "/Modules";
  2076. std::string inFile = modulesPath;
  2077. inFile += "/SystemInformation.cmake";
  2078. std::string outFile = destPath;
  2079. outFile += "/CMakeLists.txt";
  2080. // Copy file
  2081. if(!cmSystemTools::cmCopyFile(inFile.c_str(), outFile.c_str()))
  2082. {
  2083. std::cerr << "Error copying file \"" << inFile
  2084. << "\" to \"" << outFile << "\".\n";
  2085. return 1;
  2086. }
  2087. // do we write to a file or to stdout?
  2088. if (resultFile.empty())
  2089. {
  2090. resultFile = cwd;
  2091. resultFile += "/__cmake_systeminformation/results.txt";
  2092. }
  2093. // now run cmake on the CMakeLists file
  2094. cmSystemTools::ChangeDirectory(destPath);
  2095. std::vector<std::string> args2;
  2096. args2.push_back(args[0]);
  2097. args2.push_back(destPath);
  2098. std::string resultArg = "-DRESULT_FILE=";
  2099. resultArg += resultFile;
  2100. args2.push_back(resultArg);
  2101. int res = this->Run(args2, false);
  2102. if (res != 0)
  2103. {
  2104. std::cerr << "Error: --system-information failed on internal CMake!\n";
  2105. return res;
  2106. }
  2107. // change back to the original directory
  2108. cmSystemTools::ChangeDirectory(cwd);
  2109. // echo results to stdout if needed
  2110. if (writeToStdout)
  2111. {
  2112. FILE* fin = cmsys::SystemTools::Fopen(resultFile, "r");
  2113. if(fin)
  2114. {
  2115. const int bufferSize = 4096;
  2116. char buffer[bufferSize];
  2117. size_t n;
  2118. while((n = fread(buffer, 1, bufferSize, fin)) > 0)
  2119. {
  2120. for(char* c = buffer; c < buffer+n; ++c)
  2121. {
  2122. putc(*c, stdout);
  2123. }
  2124. fflush(stdout);
  2125. }
  2126. fclose(fin);
  2127. }
  2128. }
  2129. // clean up the directory
  2130. cmSystemTools::RemoveADirectory(destPath);
  2131. return 0;
  2132. }
  2133. //----------------------------------------------------------------------------
  2134. static bool cmakeCheckStampFile(const char* stampName)
  2135. {
  2136. // The stamp file does not exist. Use the stamp dependencies to
  2137. // determine whether it is really out of date. This works in
  2138. // conjunction with cmLocalVisualStudio7Generator to avoid
  2139. // repeatedly re-running CMake when the user rebuilds the entire
  2140. // solution.
  2141. std::string stampDepends = stampName;
  2142. stampDepends += ".depend";
  2143. #if defined(_WIN32) || defined(__CYGWIN__)
  2144. cmsys::ifstream fin(stampDepends.c_str(), std::ios::in | std::ios::binary);
  2145. #else
  2146. cmsys::ifstream fin(stampDepends.c_str(), std::ios::in);
  2147. #endif
  2148. if(!fin)
  2149. {
  2150. // The stamp dependencies file cannot be read. Just assume the
  2151. // build system is really out of date.
  2152. std::cout << "CMake is re-running because " << stampName
  2153. << " dependency file is missing.\n";
  2154. return false;
  2155. }
  2156. // Compare the stamp dependencies against the dependency file itself.
  2157. cmFileTimeComparison ftc;
  2158. std::string dep;
  2159. while(cmSystemTools::GetLineFromStream(fin, dep))
  2160. {
  2161. int result;
  2162. if(!dep.empty() && dep[0] != '#' &&
  2163. (!ftc.FileTimeCompare(stampDepends.c_str(), dep.c_str(), &result)
  2164. || result < 0))
  2165. {
  2166. // The stamp depends file is older than this dependency. The
  2167. // build system is really out of date.
  2168. std::cout << "CMake is re-running because " << stampName
  2169. << " is out-of-date.\n";
  2170. std::cout << " the file '" << dep << "'\n";
  2171. std::cout << " is newer than '" << stampDepends << "'\n";
  2172. std::cout << " result='" << result << "'\n";
  2173. return false;
  2174. }
  2175. }
  2176. // The build system is up to date. The stamp file has been removed
  2177. // by the VS IDE due to a "rebuild" request. Restore it atomically.
  2178. std::ostringstream stampTempStream;
  2179. stampTempStream << stampName << ".tmp" << cmSystemTools::RandomSeed();
  2180. std::string stampTempString = stampTempStream.str();
  2181. const char* stampTemp = stampTempString.c_str();
  2182. {
  2183. // TODO: Teach cmGeneratedFileStream to use a random temp file (with
  2184. // multiple tries in unlikely case of conflict) and use that here.
  2185. cmsys::ofstream stamp(stampTemp);
  2186. stamp << "# CMake generation timestamp file for this directory.\n";
  2187. }
  2188. if(cmSystemTools::RenameFile(stampTemp, stampName))
  2189. {
  2190. // Notify the user why CMake is not re-running. It is safe to
  2191. // just print to stdout here because this code is only reachable
  2192. // through an undocumented flag used by the VS generator.
  2193. std::cout << "CMake does not need to re-run because "
  2194. << stampName << " is up-to-date.\n";
  2195. return true;
  2196. }
  2197. else
  2198. {
  2199. cmSystemTools::RemoveFile(stampTemp);
  2200. cmSystemTools::Error("Cannot restore timestamp ", stampName);
  2201. return false;
  2202. }
  2203. }
  2204. //----------------------------------------------------------------------------
  2205. static bool cmakeCheckStampList(const char* stampList)
  2206. {
  2207. // If the stamp list does not exist CMake must rerun to generate it.
  2208. if(!cmSystemTools::FileExists(stampList))
  2209. {
  2210. std::cout << "CMake is re-running because generate.stamp.list "
  2211. << "is missing.\n";
  2212. return false;
  2213. }
  2214. cmsys::ifstream fin(stampList);
  2215. if(!fin)
  2216. {
  2217. std::cout << "CMake is re-running because generate.stamp.list "
  2218. << "could not be read.\n";
  2219. return false;
  2220. }
  2221. // Check each stamp.
  2222. std::string stampName;
  2223. while(cmSystemTools::GetLineFromStream(fin, stampName))
  2224. {
  2225. if(!cmakeCheckStampFile(stampName.c_str()))
  2226. {
  2227. return false;
  2228. }
  2229. }
  2230. return true;
  2231. }
  2232. bool cmake::PrintMessagePreamble(cmake::MessageType t, std::ostream& msg)
  2233. {
  2234. // Construct the message header.
  2235. if(t == cmake::FATAL_ERROR)
  2236. {
  2237. msg << "CMake Error";
  2238. }
  2239. else if(t == cmake::INTERNAL_ERROR)
  2240. {
  2241. msg << "CMake Internal Error (please report a bug)";
  2242. }
  2243. else if(t == cmake::LOG)
  2244. {
  2245. msg << "CMake Debug Log";
  2246. }
  2247. else if(t == cmake::DEPRECATION_ERROR)
  2248. {
  2249. msg << "CMake Deprecation Error";
  2250. }
  2251. else if (t == cmake::DEPRECATION_WARNING)
  2252. {
  2253. msg << "CMake Deprecation Warning";
  2254. }
  2255. else
  2256. {
  2257. msg << "CMake Warning";
  2258. if(t == cmake::AUTHOR_WARNING)
  2259. {
  2260. // Allow suppression of these warnings.
  2261. const char* suppress = this->State->GetCacheEntryValue(
  2262. "CMAKE_SUPPRESS_DEVELOPER_WARNINGS");
  2263. if(suppress && cmSystemTools::IsOn(suppress))
  2264. {
  2265. return false;
  2266. }
  2267. msg << " (dev)";
  2268. }
  2269. }
  2270. return true;
  2271. }
  2272. void printMessageText(std::ostream& msg, std::string const& text)
  2273. {
  2274. msg << ":\n";
  2275. cmDocumentationFormatter formatter;
  2276. formatter.SetIndent(" ");
  2277. formatter.PrintFormatted(msg, text.c_str());
  2278. }
  2279. void displayMessage(cmake::MessageType t, std::ostringstream& msg)
  2280. {
  2281. // Add a note about warning suppression.
  2282. if(t == cmake::AUTHOR_WARNING)
  2283. {
  2284. msg <<
  2285. "This warning is for project developers. Use -Wno-dev to suppress it.";
  2286. }
  2287. // Add a terminating blank line.
  2288. msg << "\n";
  2289. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2290. // Add a C++ stack trace to internal errors.
  2291. if(t == cmake::INTERNAL_ERROR)
  2292. {
  2293. std::string stack = cmsys::SystemInformation::GetProgramStack(0,0);
  2294. if(!stack.empty())
  2295. {
  2296. if(cmHasLiteralPrefix(stack, "WARNING:"))
  2297. {
  2298. stack = "Note:" + stack.substr(8);
  2299. }
  2300. msg << stack << "\n";
  2301. }
  2302. }
  2303. #endif
  2304. // Output the message.
  2305. if(t == cmake::FATAL_ERROR
  2306. || t == cmake::INTERNAL_ERROR
  2307. || t == cmake::DEPRECATION_ERROR)
  2308. {
  2309. cmSystemTools::SetErrorOccured();
  2310. cmSystemTools::Message(msg.str().c_str(), "Error");
  2311. }
  2312. else
  2313. {
  2314. cmSystemTools::Message(msg.str().c_str(), "Warning");
  2315. }
  2316. }
  2317. //----------------------------------------------------------------------------
  2318. void cmake::IssueMessage(cmake::MessageType t, std::string const& text,
  2319. cmListFileBacktrace const& bt)
  2320. {
  2321. cmListFileBacktrace backtrace = bt;
  2322. std::ostringstream msg;
  2323. if (!this->PrintMessagePreamble(t, msg))
  2324. {
  2325. return;
  2326. }
  2327. // Add the immediate context.
  2328. backtrace.PrintTitle(msg);
  2329. printMessageText(msg, text);
  2330. // Add the rest of the context.
  2331. backtrace.PrintCallStack(msg);
  2332. displayMessage(t, msg);
  2333. }
  2334. //----------------------------------------------------------------------------
  2335. void cmake::IssueMessage(cmake::MessageType t, std::string const& text,
  2336. cmListFileContext const& lfc)
  2337. {
  2338. std::ostringstream msg;
  2339. if (!this->PrintMessagePreamble(t, msg))
  2340. {
  2341. return;
  2342. }
  2343. // Add the immediate context.
  2344. msg << (lfc.Line ? " at " : " in ") << lfc;
  2345. printMessageText(msg, text);
  2346. displayMessage(t, msg);
  2347. }
  2348. //----------------------------------------------------------------------------
  2349. std::vector<std::string> cmake::GetDebugConfigs()
  2350. {
  2351. std::vector<std::string> configs;
  2352. if(const char* config_list =
  2353. this->State->GetGlobalProperty("DEBUG_CONFIGURATIONS"))
  2354. {
  2355. // Expand the specified list and convert to upper-case.
  2356. cmSystemTools::ExpandListArgument(config_list, configs);
  2357. std::transform(configs.begin(),
  2358. configs.end(),
  2359. configs.begin(),
  2360. cmSystemTools::UpperCase);
  2361. }
  2362. // If no configurations were specified, use a default list.
  2363. if(configs.empty())
  2364. {
  2365. configs.push_back("DEBUG");
  2366. }
  2367. return configs;
  2368. }
  2369. int cmake::Build(const std::string& dir,
  2370. const std::string& target,
  2371. const std::string& config,
  2372. const std::vector<std::string>& nativeOptions,
  2373. bool clean)
  2374. {
  2375. this->SetHomeDirectory("");
  2376. this->SetHomeOutputDirectory("");
  2377. if(!cmSystemTools::FileIsDirectory(dir))
  2378. {
  2379. std::cerr << "Error: " << dir << " is not a directory\n";
  2380. return 1;
  2381. }
  2382. std::string cachePath = dir;
  2383. cmSystemTools::ConvertToUnixSlashes(cachePath);
  2384. if(!this->LoadCache(cachePath))
  2385. {
  2386. std::cerr << "Error: could not load cache\n";
  2387. return 1;
  2388. }
  2389. const char* cachedGenerator =
  2390. this->State->GetCacheEntryValue("CMAKE_GENERATOR");
  2391. if(!cachedGenerator)
  2392. {
  2393. std::cerr << "Error: could not find CMAKE_GENERATOR in Cache\n";
  2394. return 1;
  2395. }
  2396. cmsys::auto_ptr<cmGlobalGenerator> gen(
  2397. this->CreateGlobalGenerator(cachedGenerator));
  2398. if(!gen.get())
  2399. {
  2400. std::cerr << "Error: could create CMAKE_GENERATOR \""
  2401. << cachedGenerator << "\"\n";
  2402. return 1;
  2403. }
  2404. std::string output;
  2405. std::string projName;
  2406. const char* cachedProjectName =
  2407. this->State->GetCacheEntryValue("CMAKE_PROJECT_NAME");
  2408. if(!cachedProjectName)
  2409. {
  2410. std::cerr << "Error: could not find CMAKE_PROJECT_NAME in Cache\n";
  2411. return 1;
  2412. }
  2413. projName = cachedProjectName;
  2414. bool verbose = false;
  2415. const char* cachedVerbose =
  2416. this->State->GetCacheEntryValue("CMAKE_VERBOSE_MAKEFILE");
  2417. if(cachedVerbose)
  2418. {
  2419. verbose = cmSystemTools::IsOn(cachedVerbose);
  2420. }
  2421. return gen->Build("", dir,
  2422. projName, target,
  2423. output,
  2424. "",
  2425. config, clean, false, verbose, 0,
  2426. cmSystemTools::OUTPUT_PASSTHROUGH,
  2427. nativeOptions);
  2428. }
  2429. void cmake::WatchUnusedCli(const std::string& var)
  2430. {
  2431. #ifdef CMAKE_BUILD_WITH_CMAKE
  2432. this->VariableWatch->AddWatch(var, cmWarnUnusedCliWarning, this);
  2433. if(this->UsedCliVariables.find(var) == this->UsedCliVariables.end())
  2434. {
  2435. this->UsedCliVariables[var] = false;
  2436. }
  2437. #endif
  2438. }
  2439. void cmake::UnwatchUnusedCli(const std::string& var)
  2440. {
  2441. #ifdef CMAKE_BUILD_WITH_CMAKE
  2442. this->VariableWatch->RemoveWatch(var, cmWarnUnusedCliWarning);
  2443. this->UsedCliVariables.erase(var);
  2444. #endif
  2445. }
  2446. void cmake::RunCheckForUnusedVariables()
  2447. {
  2448. #ifdef CMAKE_BUILD_WITH_CMAKE
  2449. bool haveUnused = false;
  2450. std::ostringstream msg;
  2451. msg << "Manually-specified variables were not used by the project:";
  2452. for(std::map<std::string, bool>::const_iterator
  2453. it = this->UsedCliVariables.begin();
  2454. it != this->UsedCliVariables.end(); ++it)
  2455. {
  2456. if(!it->second)
  2457. {
  2458. haveUnused = true;
  2459. msg << "\n " << it->first;
  2460. }
  2461. }
  2462. if(haveUnused)
  2463. {
  2464. this->IssueMessage(cmake::WARNING, msg.str());
  2465. }
  2466. #endif
  2467. }