cmake.cxx 92 KB

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