cmake.cxx 78 KB

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