cmake.cxx 78 KB

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