cmake.cxx 91 KB

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