cmake.cxx 92 KB

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