cmake.cxx 91 KB

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