cmake.cxx 76 KB

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