cmake.cxx 92 KB

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