cmake.cxx 91 KB

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