cmake.cxx 81 KB

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