cmake.cxx 81 KB

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