cmake.cxx 91 KB

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