cmake.cxx 81 KB

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