cmake.cxx 77 KB

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