cmake.cxx 81 KB

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