cmake.cxx 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885
  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);
  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("--debug-find", 0) == 0) {
  676. std::cout << "Running with debug output on for the `find` commands.\n";
  677. this->SetDebugFindOutputOn(true);
  678. } else if (arg.find("--trace-expand", 0) == 0) {
  679. std::cout << "Running with expanded trace output on.\n";
  680. this->SetTrace(true);
  681. this->SetTraceExpand(true);
  682. } else if (arg.find("--trace-source=", 0) == 0) {
  683. std::string file = arg.substr(strlen("--trace-source="));
  684. cmSystemTools::ConvertToUnixSlashes(file);
  685. this->AddTraceSource(file);
  686. this->SetTrace(true);
  687. } else if (arg.find("--trace-redirect=", 0) == 0) {
  688. std::string file = arg.substr(strlen("--trace-redirect="));
  689. cmSystemTools::ConvertToUnixSlashes(file);
  690. this->SetTraceFile(file);
  691. this->SetTrace(true);
  692. } else if (arg.find("--trace", 0) == 0) {
  693. std::cout << "Running with trace output on.\n";
  694. this->SetTrace(true);
  695. this->SetTraceExpand(false);
  696. } else if (arg.find("--warn-uninitialized", 0) == 0) {
  697. std::cout << "Warn about uninitialized values.\n";
  698. this->SetWarnUninitialized(true);
  699. } else if (arg.find("--warn-unused-vars", 0) == 0) {
  700. std::cout << "Finding unused variables.\n";
  701. this->SetWarnUnused(true);
  702. } else if (arg.find("--no-warn-unused-cli", 0) == 0) {
  703. std::cout << "Not searching for unused variables given on the "
  704. << "command line.\n";
  705. this->SetWarnUnusedCli(false);
  706. } else if (arg.find("--check-system-vars", 0) == 0) {
  707. std::cout << "Also check system files when warning about unused and "
  708. << "uninitialized variables.\n";
  709. this->SetCheckSystemVars(true);
  710. } else if (arg.find("-A", 0) == 0) {
  711. std::string value = arg.substr(2);
  712. if (value.empty()) {
  713. ++i;
  714. if (i >= args.size()) {
  715. cmSystemTools::Error("No platform specified for -A");
  716. return;
  717. }
  718. value = args[i];
  719. }
  720. if (havePlatform) {
  721. cmSystemTools::Error("Multiple -A options not allowed");
  722. return;
  723. }
  724. this->SetGeneratorPlatform(value);
  725. havePlatform = true;
  726. } else if (arg.find("-T", 0) == 0) {
  727. std::string value = arg.substr(2);
  728. if (value.empty()) {
  729. ++i;
  730. if (i >= args.size()) {
  731. cmSystemTools::Error("No toolset specified for -T");
  732. return;
  733. }
  734. value = args[i];
  735. }
  736. if (haveToolset) {
  737. cmSystemTools::Error("Multiple -T options not allowed");
  738. return;
  739. }
  740. this->SetGeneratorToolset(value);
  741. haveToolset = true;
  742. } else if (arg.find("-G", 0) == 0) {
  743. std::string value = arg.substr(2);
  744. if (value.empty()) {
  745. ++i;
  746. if (i >= args.size()) {
  747. cmSystemTools::Error("No generator specified for -G");
  748. this->PrintGeneratorList();
  749. return;
  750. }
  751. value = args[i];
  752. }
  753. cmGlobalGenerator* gen = this->CreateGlobalGenerator(value);
  754. if (!gen) {
  755. std::string kdevError;
  756. if (value.find("KDevelop3", 0) != std::string::npos) {
  757. kdevError = "\nThe KDevelop3 generator is not supported anymore.";
  758. }
  759. cmSystemTools::Error(
  760. cmStrCat("Could not create named generator ", value, kdevError));
  761. this->PrintGeneratorList();
  762. return;
  763. }
  764. this->SetGlobalGenerator(gen);
  765. }
  766. // no option assume it is the path to the source or an existing build
  767. else {
  768. this->SetDirectoriesFromFile(arg);
  769. }
  770. // Empty instance, platform and toolset if only a generator is specified
  771. if (this->GlobalGenerator) {
  772. this->GeneratorInstance = "";
  773. if (!this->GeneratorPlatformSet) {
  774. this->GeneratorPlatform = "";
  775. }
  776. if (!this->GeneratorToolsetSet) {
  777. this->GeneratorToolset = "";
  778. }
  779. }
  780. }
  781. const bool haveSourceDir = !this->GetHomeDirectory().empty();
  782. const bool haveBinaryDir = !this->GetHomeOutputDirectory().empty();
  783. if (this->CurrentWorkingMode == cmake::NORMAL_MODE && !haveSourceDir &&
  784. !haveBinaryDir) {
  785. this->IssueMessage(
  786. MessageType::WARNING,
  787. "No source or binary directory provided. Both will be assumed to be "
  788. "the same as the current working directory, but note that this "
  789. "warning will become a fatal error in future CMake releases.");
  790. }
  791. if (!haveSourceDir) {
  792. this->SetHomeDirectory(cmSystemTools::GetCurrentWorkingDirectory());
  793. }
  794. if (!haveBinaryDir) {
  795. this->SetHomeOutputDirectory(cmSystemTools::GetCurrentWorkingDirectory());
  796. }
  797. }
  798. cmake::LogLevel cmake::StringToLogLevel(const std::string& levelStr)
  799. {
  800. using LevelsPair = std::pair<std::string, LogLevel>;
  801. static const std::vector<LevelsPair> levels = {
  802. { "error", LogLevel::LOG_ERROR }, { "warning", LogLevel::LOG_WARNING },
  803. { "notice", LogLevel::LOG_NOTICE }, { "status", LogLevel::LOG_STATUS },
  804. { "verbose", LogLevel::LOG_VERBOSE }, { "debug", LogLevel::LOG_DEBUG },
  805. { "trace", LogLevel::LOG_TRACE }
  806. };
  807. const auto levelStrLowCase = cmSystemTools::LowerCase(levelStr);
  808. const auto it = std::find_if(levels.cbegin(), levels.cend(),
  809. [&levelStrLowCase](const LevelsPair& p) {
  810. return p.first == levelStrLowCase;
  811. });
  812. return (it != levels.cend()) ? it->second : LogLevel::LOG_UNDEFINED;
  813. }
  814. void cmake::SetTraceFile(const std::string& file)
  815. {
  816. this->TraceFile.close();
  817. this->TraceFile.open(file.c_str());
  818. if (!this->TraceFile) {
  819. std::stringstream ss;
  820. ss << "Error opening trace file " << file << ": "
  821. << cmSystemTools::GetLastSystemError();
  822. cmSystemTools::Error(ss.str());
  823. return;
  824. }
  825. std::cout << "Trace will be written to " << file << "\n";
  826. }
  827. void cmake::SetDirectoriesFromFile(const std::string& arg)
  828. {
  829. // Check if the argument refers to a CMakeCache.txt or
  830. // CMakeLists.txt file.
  831. std::string listPath;
  832. std::string cachePath;
  833. bool argIsFile = false;
  834. if (cmSystemTools::FileIsDirectory(arg)) {
  835. std::string path = cmSystemTools::CollapseFullPath(arg);
  836. cmSystemTools::ConvertToUnixSlashes(path);
  837. std::string cacheFile = cmStrCat(path, "/CMakeCache.txt");
  838. std::string listFile = cmStrCat(path, "/CMakeLists.txt");
  839. if (cmSystemTools::FileExists(cacheFile)) {
  840. cachePath = path;
  841. }
  842. if (cmSystemTools::FileExists(listFile)) {
  843. listPath = path;
  844. }
  845. } else if (cmSystemTools::FileExists(arg)) {
  846. argIsFile = true;
  847. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  848. std::string name = cmSystemTools::GetFilenameName(fullPath);
  849. name = cmSystemTools::LowerCase(name);
  850. if (name == "cmakecache.txt") {
  851. cachePath = cmSystemTools::GetFilenamePath(fullPath);
  852. } else if (name == "cmakelists.txt") {
  853. listPath = cmSystemTools::GetFilenamePath(fullPath);
  854. }
  855. } else {
  856. // Specified file or directory does not exist. Try to set things
  857. // up to produce a meaningful error message.
  858. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  859. std::string name = cmSystemTools::GetFilenameName(fullPath);
  860. name = cmSystemTools::LowerCase(name);
  861. if (name == "cmakecache.txt" || name == "cmakelists.txt") {
  862. argIsFile = true;
  863. listPath = cmSystemTools::GetFilenamePath(fullPath);
  864. } else {
  865. listPath = fullPath;
  866. }
  867. }
  868. // If there is a CMakeCache.txt file, use its settings.
  869. if (!cachePath.empty()) {
  870. if (this->LoadCache(cachePath)) {
  871. const char* existingValue =
  872. this->State->GetCacheEntryValue("CMAKE_HOME_DIRECTORY");
  873. if (existingValue) {
  874. this->SetHomeOutputDirectory(cachePath);
  875. this->SetHomeDirectory(existingValue);
  876. return;
  877. }
  878. }
  879. }
  880. // If there is a CMakeLists.txt file, use it as the source tree.
  881. if (!listPath.empty()) {
  882. this->SetHomeDirectory(listPath);
  883. if (argIsFile) {
  884. // Source CMakeLists.txt file given. It was probably dropped
  885. // onto the executable in a GUI. Default to an in-source build.
  886. this->SetHomeOutputDirectory(listPath);
  887. } else {
  888. // Source directory given on command line. Use current working
  889. // directory as build tree if -B hasn't been given already
  890. if (this->GetHomeOutputDirectory().empty()) {
  891. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  892. this->SetHomeOutputDirectory(cwd);
  893. }
  894. }
  895. return;
  896. }
  897. if (this->GetHomeDirectory().empty()) {
  898. // We didn't find a CMakeLists.txt and it wasn't specified
  899. // with -S. Assume it is the path to the source tree
  900. std::string full = cmSystemTools::CollapseFullPath(arg);
  901. this->SetHomeDirectory(full);
  902. }
  903. if (this->GetHomeOutputDirectory().empty()) {
  904. // We didn't find a CMakeCache.txt and it wasn't specified
  905. // with -B. Assume the current working directory as the build tree.
  906. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  907. this->SetHomeOutputDirectory(cwd);
  908. }
  909. }
  910. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
  911. // cache
  912. int cmake::AddCMakePaths()
  913. {
  914. // Save the value in the cache
  915. this->AddCacheEntry("CMAKE_COMMAND",
  916. cmSystemTools::GetCMakeCommand().c_str(),
  917. "Path to CMake executable.", cmStateEnums::INTERNAL);
  918. #ifndef CMAKE_BOOTSTRAP
  919. this->AddCacheEntry(
  920. "CMAKE_CTEST_COMMAND", cmSystemTools::GetCTestCommand().c_str(),
  921. "Path to ctest program executable.", cmStateEnums::INTERNAL);
  922. this->AddCacheEntry(
  923. "CMAKE_CPACK_COMMAND", cmSystemTools::GetCPackCommand().c_str(),
  924. "Path to cpack program executable.", cmStateEnums::INTERNAL);
  925. #endif
  926. if (!cmSystemTools::FileExists(
  927. (cmSystemTools::GetCMakeRoot() + "/Modules/CMake.cmake"))) {
  928. // couldn't find modules
  929. cmSystemTools::Error(
  930. "Could not find CMAKE_ROOT !!!\n"
  931. "CMake has most likely not been installed correctly.\n"
  932. "Modules directory not found in\n" +
  933. cmSystemTools::GetCMakeRoot());
  934. return 0;
  935. }
  936. this->AddCacheEntry("CMAKE_ROOT", cmSystemTools::GetCMakeRoot().c_str(),
  937. "Path to CMake installation.", cmStateEnums::INTERNAL);
  938. return 1;
  939. }
  940. void cmake::AddDefaultExtraGenerators()
  941. {
  942. #if !defined(CMAKE_BOOTSTRAP)
  943. this->ExtraGenerators.push_back(cmExtraCodeBlocksGenerator::GetFactory());
  944. this->ExtraGenerators.push_back(cmExtraCodeLiteGenerator::GetFactory());
  945. this->ExtraGenerators.push_back(cmExtraSublimeTextGenerator::GetFactory());
  946. this->ExtraGenerators.push_back(cmExtraKateGenerator::GetFactory());
  947. # ifdef CMAKE_USE_ECLIPSE
  948. this->ExtraGenerators.push_back(cmExtraEclipseCDT4Generator::GetFactory());
  949. # endif
  950. #endif
  951. }
  952. void cmake::GetRegisteredGenerators(std::vector<GeneratorInfo>& generators,
  953. bool includeNamesWithPlatform) const
  954. {
  955. for (cmGlobalGeneratorFactory* gen : this->Generators) {
  956. std::vector<std::string> names = gen->GetGeneratorNames();
  957. if (includeNamesWithPlatform) {
  958. cm::append(names, gen->GetGeneratorNamesWithPlatform());
  959. }
  960. for (std::string const& name : names) {
  961. GeneratorInfo info;
  962. info.supportsToolset = gen->SupportsToolset();
  963. info.supportsPlatform = gen->SupportsPlatform();
  964. info.supportedPlatforms = gen->GetKnownPlatforms();
  965. info.defaultPlatform = gen->GetDefaultPlatformName();
  966. info.name = name;
  967. info.baseName = name;
  968. info.isAlias = false;
  969. generators.push_back(std::move(info));
  970. }
  971. }
  972. for (cmExternalMakefileProjectGeneratorFactory* eg : this->ExtraGenerators) {
  973. const std::vector<std::string> genList =
  974. eg->GetSupportedGlobalGenerators();
  975. for (std::string const& gen : genList) {
  976. GeneratorInfo info;
  977. info.name = cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  978. gen, eg->GetName());
  979. info.baseName = gen;
  980. info.extraName = eg->GetName();
  981. info.supportsPlatform = false;
  982. info.supportsToolset = false;
  983. info.isAlias = false;
  984. generators.push_back(std::move(info));
  985. }
  986. for (std::string const& a : eg->Aliases) {
  987. GeneratorInfo info;
  988. info.name = a;
  989. if (!genList.empty()) {
  990. info.baseName = genList.at(0);
  991. }
  992. info.extraName = eg->GetName();
  993. info.supportsPlatform = false;
  994. info.supportsToolset = false;
  995. info.isAlias = true;
  996. generators.push_back(std::move(info));
  997. }
  998. }
  999. }
  1000. static std::pair<cmExternalMakefileProjectGenerator*, std::string>
  1001. createExtraGenerator(
  1002. const std::vector<cmExternalMakefileProjectGeneratorFactory*>& in,
  1003. const std::string& name)
  1004. {
  1005. for (cmExternalMakefileProjectGeneratorFactory* i : in) {
  1006. const std::vector<std::string> generators =
  1007. i->GetSupportedGlobalGenerators();
  1008. if (i->GetName() == name) { // Match aliases
  1009. return { i->CreateExternalMakefileProjectGenerator(), generators.at(0) };
  1010. }
  1011. for (std::string const& g : generators) {
  1012. const std::string fullName =
  1013. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  1014. g, i->GetName());
  1015. if (fullName == name) {
  1016. return { i->CreateExternalMakefileProjectGenerator(), g };
  1017. }
  1018. }
  1019. }
  1020. return { nullptr, name };
  1021. }
  1022. cmGlobalGenerator* cmake::CreateGlobalGenerator(const std::string& gname)
  1023. {
  1024. std::pair<cmExternalMakefileProjectGenerator*, std::string> extra =
  1025. createExtraGenerator(this->ExtraGenerators, gname);
  1026. cmExternalMakefileProjectGenerator* extraGenerator = extra.first;
  1027. const std::string name = extra.second;
  1028. cmGlobalGenerator* generator = nullptr;
  1029. for (cmGlobalGeneratorFactory* g : this->Generators) {
  1030. generator = g->CreateGlobalGenerator(name, this);
  1031. if (generator) {
  1032. break;
  1033. }
  1034. }
  1035. if (generator) {
  1036. generator->SetExternalMakefileProjectGenerator(extraGenerator);
  1037. } else {
  1038. delete extraGenerator;
  1039. }
  1040. return generator;
  1041. }
  1042. void cmake::SetHomeDirectory(const std::string& dir)
  1043. {
  1044. this->State->SetSourceDirectory(dir);
  1045. if (this->CurrentSnapshot.IsValid()) {
  1046. this->CurrentSnapshot.SetDefinition("CMAKE_SOURCE_DIR", dir);
  1047. }
  1048. }
  1049. std::string const& cmake::GetHomeDirectory() const
  1050. {
  1051. return this->State->GetSourceDirectory();
  1052. }
  1053. void cmake::SetHomeOutputDirectory(const std::string& dir)
  1054. {
  1055. this->State->SetBinaryDirectory(dir);
  1056. if (this->CurrentSnapshot.IsValid()) {
  1057. this->CurrentSnapshot.SetDefinition("CMAKE_BINARY_DIR", dir);
  1058. }
  1059. }
  1060. std::string const& cmake::GetHomeOutputDirectory() const
  1061. {
  1062. return this->State->GetBinaryDirectory();
  1063. }
  1064. std::string cmake::FindCacheFile(const std::string& binaryDir)
  1065. {
  1066. std::string cachePath = binaryDir;
  1067. cmSystemTools::ConvertToUnixSlashes(cachePath);
  1068. std::string cacheFile = cmStrCat(cachePath, "/CMakeCache.txt");
  1069. if (!cmSystemTools::FileExists(cacheFile)) {
  1070. // search in parent directories for cache
  1071. std::string cmakeFiles = cmStrCat(cachePath, "/CMakeFiles");
  1072. if (cmSystemTools::FileExists(cmakeFiles)) {
  1073. std::string cachePathFound =
  1074. cmSystemTools::FileExistsInParentDirectories("CMakeCache.txt",
  1075. cachePath, "/");
  1076. if (!cachePathFound.empty()) {
  1077. cachePath = cmSystemTools::GetFilenamePath(cachePathFound);
  1078. }
  1079. }
  1080. }
  1081. return cachePath;
  1082. }
  1083. void cmake::SetGlobalGenerator(cmGlobalGenerator* gg)
  1084. {
  1085. if (!gg) {
  1086. cmSystemTools::Error("Error SetGlobalGenerator called with null");
  1087. return;
  1088. }
  1089. // delete the old generator
  1090. if (this->GlobalGenerator) {
  1091. delete this->GlobalGenerator;
  1092. // restore the original environment variables CXX and CC
  1093. // Restore CC
  1094. std::string env = "CC=";
  1095. if (!this->CCEnvironment.empty()) {
  1096. env += this->CCEnvironment;
  1097. }
  1098. cmSystemTools::PutEnv(env);
  1099. env = "CXX=";
  1100. if (!this->CXXEnvironment.empty()) {
  1101. env += this->CXXEnvironment;
  1102. }
  1103. cmSystemTools::PutEnv(env);
  1104. }
  1105. // set the new
  1106. this->GlobalGenerator = gg;
  1107. // set the global flag for unix style paths on cmSystemTools as soon as
  1108. // the generator is set. This allows gmake to be used on windows.
  1109. cmSystemTools::SetForceUnixPaths(this->GlobalGenerator->GetForceUnixPaths());
  1110. // Save the environment variables CXX and CC
  1111. if (!cmSystemTools::GetEnv("CXX", this->CXXEnvironment)) {
  1112. this->CXXEnvironment.clear();
  1113. }
  1114. if (!cmSystemTools::GetEnv("CC", this->CCEnvironment)) {
  1115. this->CCEnvironment.clear();
  1116. }
  1117. }
  1118. int cmake::DoPreConfigureChecks()
  1119. {
  1120. // Make sure the Source directory contains a CMakeLists.txt file.
  1121. std::string srcList = cmStrCat(this->GetHomeDirectory(), "/CMakeLists.txt");
  1122. if (!cmSystemTools::FileExists(srcList)) {
  1123. std::ostringstream err;
  1124. if (cmSystemTools::FileIsDirectory(this->GetHomeDirectory())) {
  1125. err << "The source directory \"" << this->GetHomeDirectory()
  1126. << "\" does not appear to contain CMakeLists.txt.\n";
  1127. } else if (cmSystemTools::FileExists(this->GetHomeDirectory())) {
  1128. err << "The source directory \"" << this->GetHomeDirectory()
  1129. << "\" is a file, not a directory.\n";
  1130. } else {
  1131. err << "The source directory \"" << this->GetHomeDirectory()
  1132. << "\" does not exist.\n";
  1133. }
  1134. err << "Specify --help for usage, or press the help button on the CMake "
  1135. "GUI.";
  1136. cmSystemTools::Error(err.str());
  1137. return -2;
  1138. }
  1139. // do a sanity check on some values
  1140. if (this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY")) {
  1141. std::string cacheStart =
  1142. cmStrCat(*this->State->GetInitializedCacheValue("CMAKE_HOME_DIRECTORY"),
  1143. "/CMakeLists.txt");
  1144. std::string currentStart =
  1145. cmStrCat(this->GetHomeDirectory(), "/CMakeLists.txt");
  1146. if (!cmSystemTools::SameFile(cacheStart, currentStart)) {
  1147. std::string message =
  1148. cmStrCat("The source \"", currentStart,
  1149. "\" does not match the source \"", cacheStart,
  1150. "\" used to generate cache. Re-run cmake with a different "
  1151. "source directory.");
  1152. cmSystemTools::Error(message);
  1153. return -2;
  1154. }
  1155. } else {
  1156. return 0;
  1157. }
  1158. return 1;
  1159. }
  1160. struct SaveCacheEntry
  1161. {
  1162. std::string key;
  1163. std::string value;
  1164. std::string help;
  1165. cmStateEnums::CacheEntryType type;
  1166. };
  1167. int cmake::HandleDeleteCacheVariables(const std::string& var)
  1168. {
  1169. std::vector<std::string> argsSplit = cmExpandedList(std::string(var), true);
  1170. // erase the property to avoid infinite recursion
  1171. this->State->SetGlobalProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_", "");
  1172. if (this->State->GetIsInTryCompile()) {
  1173. return 0;
  1174. }
  1175. std::vector<SaveCacheEntry> saved;
  1176. std::ostringstream warning;
  1177. /* clang-format off */
  1178. warning
  1179. << "You have changed variables that require your cache to be deleted.\n"
  1180. << "Configure will be re-run and you may have to reset some variables.\n"
  1181. << "The following variables have changed:\n";
  1182. /* clang-format on */
  1183. for (auto i = argsSplit.begin(); i != argsSplit.end(); ++i) {
  1184. SaveCacheEntry save;
  1185. save.key = *i;
  1186. warning << *i << "= ";
  1187. i++;
  1188. save.value = *i;
  1189. warning << *i << "\n";
  1190. const char* existingValue = this->State->GetCacheEntryValue(save.key);
  1191. if (existingValue) {
  1192. save.type = this->State->GetCacheEntryType(save.key);
  1193. if (const char* help =
  1194. this->State->GetCacheEntryProperty(save.key, "HELPSTRING")) {
  1195. save.help = help;
  1196. }
  1197. }
  1198. saved.push_back(std::move(save));
  1199. }
  1200. // remove the cache
  1201. this->DeleteCache(this->GetHomeOutputDirectory());
  1202. // load the empty cache
  1203. this->LoadCache();
  1204. // restore the changed compilers
  1205. for (SaveCacheEntry const& i : saved) {
  1206. this->AddCacheEntry(i.key, i.value.c_str(), i.help.c_str(), i.type);
  1207. }
  1208. cmSystemTools::Message(warning.str());
  1209. // avoid reconfigure if there were errors
  1210. if (!cmSystemTools::GetErrorOccuredFlag()) {
  1211. // re-run configure
  1212. return this->Configure();
  1213. }
  1214. return 0;
  1215. }
  1216. int cmake::Configure()
  1217. {
  1218. DiagLevel diagLevel;
  1219. if (this->DiagLevels.count("deprecated") == 1) {
  1220. diagLevel = this->DiagLevels["deprecated"];
  1221. if (diagLevel == DIAG_IGNORE) {
  1222. this->SetSuppressDeprecatedWarnings(true);
  1223. this->SetDeprecatedWarningsAsErrors(false);
  1224. } else if (diagLevel == DIAG_WARN) {
  1225. this->SetSuppressDeprecatedWarnings(false);
  1226. this->SetDeprecatedWarningsAsErrors(false);
  1227. } else if (diagLevel == DIAG_ERROR) {
  1228. this->SetSuppressDeprecatedWarnings(false);
  1229. this->SetDeprecatedWarningsAsErrors(true);
  1230. }
  1231. }
  1232. if (this->DiagLevels.count("dev") == 1) {
  1233. bool setDeprecatedVariables = false;
  1234. const char* cachedWarnDeprecated =
  1235. this->State->GetCacheEntryValue("CMAKE_WARN_DEPRECATED");
  1236. const char* cachedErrorDeprecated =
  1237. this->State->GetCacheEntryValue("CMAKE_ERROR_DEPRECATED");
  1238. // don't overwrite deprecated warning setting from a previous invocation
  1239. if (!cachedWarnDeprecated && !cachedErrorDeprecated) {
  1240. setDeprecatedVariables = true;
  1241. }
  1242. diagLevel = this->DiagLevels["dev"];
  1243. if (diagLevel == DIAG_IGNORE) {
  1244. this->SetSuppressDevWarnings(true);
  1245. this->SetDevWarningsAsErrors(false);
  1246. if (setDeprecatedVariables) {
  1247. this->SetSuppressDeprecatedWarnings(true);
  1248. this->SetDeprecatedWarningsAsErrors(false);
  1249. }
  1250. } else if (diagLevel == DIAG_WARN) {
  1251. this->SetSuppressDevWarnings(false);
  1252. this->SetDevWarningsAsErrors(false);
  1253. if (setDeprecatedVariables) {
  1254. this->SetSuppressDeprecatedWarnings(false);
  1255. this->SetDeprecatedWarningsAsErrors(false);
  1256. }
  1257. } else if (diagLevel == DIAG_ERROR) {
  1258. this->SetSuppressDevWarnings(false);
  1259. this->SetDevWarningsAsErrors(true);
  1260. if (setDeprecatedVariables) {
  1261. this->SetSuppressDeprecatedWarnings(false);
  1262. this->SetDeprecatedWarningsAsErrors(true);
  1263. }
  1264. }
  1265. }
  1266. // Cache variables may have already been set by a previous invocation,
  1267. // so we cannot rely on command line options alone. Always ensure our
  1268. // messenger is in sync with the cache.
  1269. const char* value = this->State->GetCacheEntryValue("CMAKE_WARN_DEPRECATED");
  1270. this->Messenger->SetSuppressDeprecatedWarnings(value && cmIsOff(value));
  1271. value = this->State->GetCacheEntryValue("CMAKE_ERROR_DEPRECATED");
  1272. this->Messenger->SetDeprecatedWarningsAsErrors(cmIsOn(value));
  1273. value = this->State->GetCacheEntryValue("CMAKE_SUPPRESS_DEVELOPER_WARNINGS");
  1274. this->Messenger->SetSuppressDevWarnings(cmIsOn(value));
  1275. value = this->State->GetCacheEntryValue("CMAKE_SUPPRESS_DEVELOPER_ERRORS");
  1276. this->Messenger->SetDevWarningsAsErrors(value && cmIsOff(value));
  1277. int ret = this->ActualConfigure();
  1278. const char* delCacheVars =
  1279. this->State->GetGlobalProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_");
  1280. if (delCacheVars && delCacheVars[0] != 0) {
  1281. return this->HandleDeleteCacheVariables(delCacheVars);
  1282. }
  1283. return ret;
  1284. }
  1285. int cmake::ActualConfigure()
  1286. {
  1287. // Construct right now our path conversion table before it's too late:
  1288. this->UpdateConversionPathTable();
  1289. this->CleanupCommandsAndMacros();
  1290. int res = this->DoPreConfigureChecks();
  1291. if (res < 0) {
  1292. return -2;
  1293. }
  1294. if (!res) {
  1295. this->AddCacheEntry(
  1296. "CMAKE_HOME_DIRECTORY", this->GetHomeDirectory().c_str(),
  1297. "Source directory with the top level CMakeLists.txt file for this "
  1298. "project",
  1299. cmStateEnums::INTERNAL);
  1300. }
  1301. // no generator specified on the command line
  1302. if (!this->GlobalGenerator) {
  1303. const std::string* genName =
  1304. this->State->GetInitializedCacheValue("CMAKE_GENERATOR");
  1305. const std::string* extraGenName =
  1306. this->State->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
  1307. if (genName) {
  1308. std::string fullName =
  1309. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  1310. *genName, extraGenName ? *extraGenName : "");
  1311. this->GlobalGenerator = this->CreateGlobalGenerator(fullName);
  1312. }
  1313. if (this->GlobalGenerator) {
  1314. // set the global flag for unix style paths on cmSystemTools as
  1315. // soon as the generator is set. This allows gmake to be used
  1316. // on windows.
  1317. cmSystemTools::SetForceUnixPaths(
  1318. this->GlobalGenerator->GetForceUnixPaths());
  1319. } else {
  1320. this->CreateDefaultGlobalGenerator();
  1321. }
  1322. if (!this->GlobalGenerator) {
  1323. cmSystemTools::Error("Could not create generator");
  1324. return -1;
  1325. }
  1326. }
  1327. const std::string* genName =
  1328. this->State->GetInitializedCacheValue("CMAKE_GENERATOR");
  1329. if (genName) {
  1330. if (!this->GlobalGenerator->MatchesGeneratorName(*genName)) {
  1331. std::string message =
  1332. cmStrCat("Error: generator : ", this->GlobalGenerator->GetName(),
  1333. "\nDoes not match the generator used previously: ", *genName,
  1334. "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1335. "directory or choose a different binary directory.");
  1336. cmSystemTools::Error(message);
  1337. return -2;
  1338. }
  1339. }
  1340. if (!this->State->GetInitializedCacheValue("CMAKE_GENERATOR")) {
  1341. this->AddCacheEntry("CMAKE_GENERATOR",
  1342. this->GlobalGenerator->GetName().c_str(),
  1343. "Name of generator.", cmStateEnums::INTERNAL);
  1344. this->AddCacheEntry("CMAKE_EXTRA_GENERATOR",
  1345. this->GlobalGenerator->GetExtraGeneratorName().c_str(),
  1346. "Name of external makefile project generator.",
  1347. cmStateEnums::INTERNAL);
  1348. }
  1349. if (const std::string* instance =
  1350. this->State->GetInitializedCacheValue("CMAKE_GENERATOR_INSTANCE")) {
  1351. if (this->GeneratorInstanceSet && this->GeneratorInstance != *instance) {
  1352. std::string message =
  1353. cmStrCat("Error: generator instance: ", this->GeneratorInstance,
  1354. "\nDoes not match the instance used previously: ", *instance,
  1355. "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1356. "directory or choose a different binary directory.");
  1357. cmSystemTools::Error(message);
  1358. return -2;
  1359. }
  1360. } else {
  1361. this->AddCacheEntry(
  1362. "CMAKE_GENERATOR_INSTANCE", this->GeneratorInstance.c_str(),
  1363. "Generator instance identifier.", cmStateEnums::INTERNAL);
  1364. }
  1365. if (const std::string* platformName =
  1366. this->State->GetInitializedCacheValue("CMAKE_GENERATOR_PLATFORM")) {
  1367. if (this->GeneratorPlatformSet &&
  1368. this->GeneratorPlatform != *platformName) {
  1369. std::string message = cmStrCat(
  1370. "Error: generator platform: ", this->GeneratorPlatform,
  1371. "\nDoes not match the platform used previously: ", *platformName,
  1372. "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1373. "directory or choose a different binary directory.");
  1374. cmSystemTools::Error(message);
  1375. return -2;
  1376. }
  1377. } else {
  1378. this->AddCacheEntry("CMAKE_GENERATOR_PLATFORM",
  1379. this->GeneratorPlatform.c_str(),
  1380. "Name of generator platform.", cmStateEnums::INTERNAL);
  1381. }
  1382. if (const std::string* tsName =
  1383. this->State->GetInitializedCacheValue("CMAKE_GENERATOR_TOOLSET")) {
  1384. if (this->GeneratorToolsetSet && this->GeneratorToolset != *tsName) {
  1385. std::string message =
  1386. cmStrCat("Error: generator toolset: ", this->GeneratorToolset,
  1387. "\nDoes not match the toolset used previously: ", *tsName,
  1388. "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1389. "directory or choose a different binary directory.");
  1390. cmSystemTools::Error(message);
  1391. return -2;
  1392. }
  1393. } else {
  1394. this->AddCacheEntry("CMAKE_GENERATOR_TOOLSET",
  1395. this->GeneratorToolset.c_str(),
  1396. "Name of generator toolset.", cmStateEnums::INTERNAL);
  1397. }
  1398. // reset any system configuration information, except for when we are
  1399. // InTryCompile. With TryCompile the system info is taken from the parent's
  1400. // info to save time
  1401. if (!this->State->GetIsInTryCompile()) {
  1402. this->GlobalGenerator->ClearEnabledLanguages();
  1403. this->TruncateOutputLog("CMakeOutput.log");
  1404. this->TruncateOutputLog("CMakeError.log");
  1405. }
  1406. #if !defined(CMAKE_BOOTSTRAP)
  1407. this->FileAPI = cm::make_unique<cmFileAPI>(this);
  1408. this->FileAPI->ReadQueries();
  1409. #endif
  1410. // actually do the configure
  1411. this->GlobalGenerator->Configure();
  1412. // Before saving the cache
  1413. // if the project did not define one of the entries below, add them now
  1414. // so users can edit the values in the cache:
  1415. // We used to always present LIBRARY_OUTPUT_PATH and
  1416. // EXECUTABLE_OUTPUT_PATH. They are now documented as old-style and
  1417. // should no longer be used. Therefore we present them only if the
  1418. // project requires compatibility with CMake 2.4. We detect this
  1419. // here by looking for the old CMAKE_BACKWARDS_COMPATIBILITY
  1420. // variable created when CMP0001 is not set to NEW.
  1421. if (this->State->GetInitializedCacheValue("CMAKE_BACKWARDS_COMPATIBILITY")) {
  1422. if (!this->State->GetInitializedCacheValue("LIBRARY_OUTPUT_PATH")) {
  1423. this->AddCacheEntry(
  1424. "LIBRARY_OUTPUT_PATH", "",
  1425. "Single output directory for building all libraries.",
  1426. cmStateEnums::PATH);
  1427. }
  1428. if (!this->State->GetInitializedCacheValue("EXECUTABLE_OUTPUT_PATH")) {
  1429. this->AddCacheEntry(
  1430. "EXECUTABLE_OUTPUT_PATH", "",
  1431. "Single output directory for building all executables.",
  1432. cmStateEnums::PATH);
  1433. }
  1434. }
  1435. cmMakefile* mf = this->GlobalGenerator->GetMakefiles()[0];
  1436. if (mf->IsOn("CTEST_USE_LAUNCHERS") &&
  1437. !this->State->GetGlobalProperty("RULE_LAUNCH_COMPILE")) {
  1438. cmSystemTools::Error(
  1439. "CTEST_USE_LAUNCHERS is enabled, but the "
  1440. "RULE_LAUNCH_COMPILE global property is not defined.\n"
  1441. "Did you forget to include(CTest) in the toplevel "
  1442. "CMakeLists.txt ?");
  1443. }
  1444. this->State->SaveVerificationScript(this->GetHomeOutputDirectory());
  1445. this->SaveCache(this->GetHomeOutputDirectory());
  1446. if (cmSystemTools::GetErrorOccuredFlag()) {
  1447. return -1;
  1448. }
  1449. return 0;
  1450. }
  1451. std::unique_ptr<cmGlobalGenerator> cmake::EvaluateDefaultGlobalGenerator()
  1452. {
  1453. if (!this->EnvironmentGenerator.empty()) {
  1454. cmGlobalGenerator* gen =
  1455. this->CreateGlobalGenerator(this->EnvironmentGenerator);
  1456. if (!gen) {
  1457. cmSystemTools::Error("CMAKE_GENERATOR was set but the specified "
  1458. "generator doesn't exist. Using CMake default.");
  1459. } else {
  1460. return std::unique_ptr<cmGlobalGenerator>(gen);
  1461. }
  1462. }
  1463. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1464. std::string found;
  1465. // Try to find the newest VS installed on the computer and
  1466. // use that as a default if -G is not specified
  1467. const std::string vsregBase = "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\";
  1468. static const char* const vsVariants[] = {
  1469. /* clang-format needs this comment to break after the opening brace */
  1470. "VisualStudio\\", "VCExpress\\", "WDExpress\\"
  1471. };
  1472. struct VSVersionedGenerator
  1473. {
  1474. const char* MSVersion;
  1475. const char* GeneratorName;
  1476. };
  1477. static VSVersionedGenerator const vsGenerators[] = {
  1478. { "14.0", "Visual Studio 14 2015" }, //
  1479. { "12.0", "Visual Studio 12 2013" }, //
  1480. { "11.0", "Visual Studio 11 2012" }, //
  1481. { "10.0", "Visual Studio 10 2010" }, //
  1482. { "9.0", "Visual Studio 9 2008" }
  1483. };
  1484. static const char* const vsEntries[] = {
  1485. "\\Setup\\VC;ProductDir", //
  1486. ";InstallDir" //
  1487. };
  1488. if (cmVSSetupAPIHelper(16).IsVSInstalled()) {
  1489. found = "Visual Studio 16 2019";
  1490. } else if (cmVSSetupAPIHelper(15).IsVSInstalled()) {
  1491. found = "Visual Studio 15 2017";
  1492. } else {
  1493. for (VSVersionedGenerator const* g = cm::cbegin(vsGenerators);
  1494. found.empty() && g != cm::cend(vsGenerators); ++g) {
  1495. for (const char* const* v = cm::cbegin(vsVariants);
  1496. found.empty() && v != cm::cend(vsVariants); ++v) {
  1497. for (const char* const* e = cm::cbegin(vsEntries);
  1498. found.empty() && e != cm::cend(vsEntries); ++e) {
  1499. std::string const reg = vsregBase + *v + g->MSVersion + *e;
  1500. std::string dir;
  1501. if (cmSystemTools::ReadRegistryValue(reg, dir,
  1502. cmSystemTools::KeyWOW64_32) &&
  1503. cmSystemTools::PathExists(dir)) {
  1504. found = g->GeneratorName;
  1505. }
  1506. }
  1507. }
  1508. }
  1509. }
  1510. cmGlobalGenerator* gen = this->CreateGlobalGenerator(found);
  1511. if (!gen) {
  1512. gen = new cmGlobalNMakeMakefileGenerator(this);
  1513. }
  1514. return std::unique_ptr<cmGlobalGenerator>(gen);
  1515. #else
  1516. return cm::make_unique<cmGlobalUnixMakefileGenerator3>(this);
  1517. #endif
  1518. }
  1519. void cmake::CreateDefaultGlobalGenerator()
  1520. {
  1521. auto gen = this->EvaluateDefaultGlobalGenerator();
  1522. #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1523. // This print could be unified for all platforms
  1524. std::cout << "-- Building for: " << gen->GetName() << "\n";
  1525. #endif
  1526. this->SetGlobalGenerator(gen.release());
  1527. }
  1528. void cmake::PreLoadCMakeFiles()
  1529. {
  1530. std::vector<std::string> args;
  1531. std::string pre_load = this->GetHomeDirectory();
  1532. if (!pre_load.empty()) {
  1533. pre_load += "/PreLoad.cmake";
  1534. if (cmSystemTools::FileExists(pre_load)) {
  1535. this->ReadListFile(args, pre_load);
  1536. }
  1537. }
  1538. pre_load = this->GetHomeOutputDirectory();
  1539. if (!pre_load.empty()) {
  1540. pre_load += "/PreLoad.cmake";
  1541. if (cmSystemTools::FileExists(pre_load)) {
  1542. this->ReadListFile(args, pre_load);
  1543. }
  1544. }
  1545. }
  1546. // handle a command line invocation
  1547. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1548. {
  1549. // Process the arguments
  1550. this->SetArgs(args);
  1551. if (cmSystemTools::GetErrorOccuredFlag()) {
  1552. return -1;
  1553. }
  1554. // If we are given a stamp list file check if it is really out of date.
  1555. if (!this->CheckStampList.empty() &&
  1556. cmakeCheckStampList(this->CheckStampList)) {
  1557. return 0;
  1558. }
  1559. // If we are given a stamp file check if it is really out of date.
  1560. if (!this->CheckStampFile.empty() &&
  1561. cmakeCheckStampFile(this->CheckStampFile)) {
  1562. return 0;
  1563. }
  1564. if (this->GetWorkingMode() == NORMAL_MODE) {
  1565. // load the cache
  1566. if (this->LoadCache() < 0) {
  1567. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1568. return -1;
  1569. }
  1570. } else {
  1571. this->AddCMakePaths();
  1572. }
  1573. // Add any cache args
  1574. if (!this->SetCacheArgs(args)) {
  1575. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1576. return -1;
  1577. }
  1578. // In script mode we terminate after running the script.
  1579. if (this->GetWorkingMode() != NORMAL_MODE) {
  1580. if (cmSystemTools::GetErrorOccuredFlag()) {
  1581. return -1;
  1582. }
  1583. return 0;
  1584. }
  1585. // If MAKEFLAGS are given in the environment, remove the environment
  1586. // variable. This will prevent try-compile from succeeding when it
  1587. // should fail (if "-i" is an option). We cannot simply test
  1588. // whether "-i" is given and remove it because some make programs
  1589. // encode the MAKEFLAGS variable in a strange way.
  1590. if (cmSystemTools::HasEnv("MAKEFLAGS")) {
  1591. cmSystemTools::PutEnv("MAKEFLAGS=");
  1592. }
  1593. this->PreLoadCMakeFiles();
  1594. if (noconfigure) {
  1595. return 0;
  1596. }
  1597. // now run the global generate
  1598. // Check the state of the build system to see if we need to regenerate.
  1599. if (!this->CheckBuildSystem()) {
  1600. return 0;
  1601. }
  1602. int ret = this->Configure();
  1603. if (ret) {
  1604. #if defined(CMAKE_HAVE_VS_GENERATORS)
  1605. if (!this->VSSolutionFile.empty() && this->GlobalGenerator) {
  1606. // CMake is running to regenerate a Visual Studio build tree
  1607. // during a build from the VS IDE. The build files cannot be
  1608. // regenerated, so we should stop the build.
  1609. cmSystemTools::Message("CMake Configure step failed. "
  1610. "Build files cannot be regenerated correctly. "
  1611. "Attempting to stop IDE build.");
  1612. cmGlobalVisualStudioGenerator* gg =
  1613. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  1614. gg->CallVisualStudioMacro(cmGlobalVisualStudioGenerator::MacroStop,
  1615. this->VSSolutionFile);
  1616. }
  1617. #endif
  1618. return ret;
  1619. }
  1620. ret = this->Generate();
  1621. if (ret) {
  1622. cmSystemTools::Message("CMake Generate step failed. "
  1623. "Build files cannot be regenerated correctly.");
  1624. return ret;
  1625. }
  1626. std::string message = cmStrCat("Build files have been written to: ",
  1627. this->GetHomeOutputDirectory());
  1628. this->UpdateProgress(message, -1);
  1629. return ret;
  1630. }
  1631. int cmake::Generate()
  1632. {
  1633. if (!this->GlobalGenerator) {
  1634. return -1;
  1635. }
  1636. if (!this->GlobalGenerator->Compute()) {
  1637. return -1;
  1638. }
  1639. this->GlobalGenerator->Generate();
  1640. if (!this->GraphVizFile.empty()) {
  1641. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1642. this->GenerateGraphViz(this->GraphVizFile);
  1643. }
  1644. if (this->WarnUnusedCli) {
  1645. this->RunCheckForUnusedVariables();
  1646. }
  1647. if (cmSystemTools::GetErrorOccuredFlag()) {
  1648. return -1;
  1649. }
  1650. // Save the cache again after a successful Generate so that any internal
  1651. // variables created during Generate are saved. (Specifically target GUIDs
  1652. // for the Visual Studio and Xcode generators.)
  1653. this->SaveCache(this->GetHomeOutputDirectory());
  1654. #if !defined(CMAKE_BOOTSTRAP)
  1655. this->FileAPI->WriteReplies();
  1656. #endif
  1657. return 0;
  1658. }
  1659. void cmake::AddCacheEntry(const std::string& key, const char* value,
  1660. const char* helpString, int type)
  1661. {
  1662. this->State->AddCacheEntry(key, value, helpString,
  1663. cmStateEnums::CacheEntryType(type));
  1664. this->UnwatchUnusedCli(key);
  1665. if (key == "CMAKE_WARN_DEPRECATED") {
  1666. this->Messenger->SetSuppressDeprecatedWarnings(value && cmIsOff(value));
  1667. } else if (key == "CMAKE_ERROR_DEPRECATED") {
  1668. this->Messenger->SetDeprecatedWarningsAsErrors(cmIsOn(value));
  1669. } else if (key == "CMAKE_SUPPRESS_DEVELOPER_WARNINGS") {
  1670. this->Messenger->SetSuppressDevWarnings(cmIsOn(value));
  1671. } else if (key == "CMAKE_SUPPRESS_DEVELOPER_ERRORS") {
  1672. this->Messenger->SetDevWarningsAsErrors(value && cmIsOff(value));
  1673. }
  1674. }
  1675. bool cmake::DoWriteGlobVerifyTarget() const
  1676. {
  1677. return this->State->DoWriteGlobVerifyTarget();
  1678. }
  1679. std::string const& cmake::GetGlobVerifyScript() const
  1680. {
  1681. return this->State->GetGlobVerifyScript();
  1682. }
  1683. std::string const& cmake::GetGlobVerifyStamp() const
  1684. {
  1685. return this->State->GetGlobVerifyStamp();
  1686. }
  1687. void cmake::AddGlobCacheEntry(bool recurse, bool listDirectories,
  1688. bool followSymlinks, const std::string& relative,
  1689. const std::string& expression,
  1690. const std::vector<std::string>& files,
  1691. const std::string& variable,
  1692. cmListFileBacktrace const& backtrace)
  1693. {
  1694. this->State->AddGlobCacheEntry(recurse, listDirectories, followSymlinks,
  1695. relative, expression, files, variable,
  1696. backtrace);
  1697. }
  1698. std::string cmake::StripExtension(const std::string& file) const
  1699. {
  1700. auto dotpos = file.rfind('.');
  1701. if (dotpos != std::string::npos) {
  1702. auto ext = file.substr(dotpos + 1);
  1703. #if defined(_WIN32) || defined(__APPLE__)
  1704. ext = cmSystemTools::LowerCase(ext);
  1705. #endif
  1706. if (this->IsSourceExtension(ext) || this->IsHeaderExtension(ext)) {
  1707. return file.substr(0, dotpos);
  1708. }
  1709. }
  1710. return file;
  1711. }
  1712. const char* cmake::GetCacheDefinition(const std::string& name) const
  1713. {
  1714. const std::string* p = this->State->GetInitializedCacheValue(name);
  1715. return p ? p->c_str() : nullptr;
  1716. }
  1717. void cmake::AddScriptingCommands()
  1718. {
  1719. GetScriptingCommands(this->GetState());
  1720. }
  1721. void cmake::AddProjectCommands()
  1722. {
  1723. GetProjectCommands(this->GetState());
  1724. }
  1725. void cmake::AddDefaultGenerators()
  1726. {
  1727. #if defined(_WIN32) && !defined(__CYGWIN__)
  1728. # if !defined(CMAKE_BOOT_MINGW)
  1729. this->Generators.push_back(
  1730. cmGlobalVisualStudioVersionedGenerator::NewFactory16());
  1731. this->Generators.push_back(
  1732. cmGlobalVisualStudioVersionedGenerator::NewFactory15());
  1733. this->Generators.push_back(cmGlobalVisualStudio14Generator::NewFactory());
  1734. this->Generators.push_back(cmGlobalVisualStudio12Generator::NewFactory());
  1735. this->Generators.push_back(cmGlobalVisualStudio11Generator::NewFactory());
  1736. this->Generators.push_back(cmGlobalVisualStudio10Generator::NewFactory());
  1737. this->Generators.push_back(cmGlobalVisualStudio9Generator::NewFactory());
  1738. this->Generators.push_back(cmGlobalBorlandMakefileGenerator::NewFactory());
  1739. this->Generators.push_back(cmGlobalNMakeMakefileGenerator::NewFactory());
  1740. this->Generators.push_back(cmGlobalJOMMakefileGenerator::NewFactory());
  1741. # endif
  1742. this->Generators.push_back(cmGlobalMSYSMakefileGenerator::NewFactory());
  1743. this->Generators.push_back(cmGlobalMinGWMakefileGenerator::NewFactory());
  1744. #endif
  1745. this->Generators.push_back(cmGlobalUnixMakefileGenerator3::NewFactory());
  1746. #if !defined(CMAKE_BOOTSTRAP)
  1747. # if defined(__linux__) || defined(_WIN32)
  1748. this->Generators.push_back(cmGlobalGhsMultiGenerator::NewFactory());
  1749. # endif
  1750. this->Generators.push_back(cmGlobalNinjaGenerator::NewFactory());
  1751. this->Generators.push_back(cmGlobalNinjaMultiGenerator::NewFactory());
  1752. #endif
  1753. #if defined(CMAKE_USE_WMAKE)
  1754. this->Generators.push_back(cmGlobalWatcomWMakeGenerator::NewFactory());
  1755. #endif
  1756. #ifdef CMAKE_USE_XCODE
  1757. this->Generators.push_back(cmGlobalXCodeGenerator::NewFactory());
  1758. #endif
  1759. }
  1760. bool cmake::ParseCacheEntry(const std::string& entry, std::string& var,
  1761. std::string& value,
  1762. cmStateEnums::CacheEntryType& type)
  1763. {
  1764. return cmState::ParseCacheEntry(entry, var, value, type);
  1765. }
  1766. int cmake::LoadCache()
  1767. {
  1768. // could we not read the cache
  1769. if (!this->LoadCache(this->GetHomeOutputDirectory())) {
  1770. // if it does exist, but isn't readable then warn the user
  1771. std::string cacheFile =
  1772. cmStrCat(this->GetHomeOutputDirectory(), "/CMakeCache.txt");
  1773. if (cmSystemTools::FileExists(cacheFile)) {
  1774. cmSystemTools::Error(
  1775. "There is a CMakeCache.txt file for the current binary tree but "
  1776. "cmake does not have permission to read it. Please check the "
  1777. "permissions of the directory you are trying to run CMake on.");
  1778. return -1;
  1779. }
  1780. }
  1781. // setup CMAKE_ROOT and CMAKE_COMMAND
  1782. if (!this->AddCMakePaths()) {
  1783. return -3;
  1784. }
  1785. return 0;
  1786. }
  1787. bool cmake::LoadCache(const std::string& path)
  1788. {
  1789. std::set<std::string> emptySet;
  1790. return this->LoadCache(path, true, emptySet, emptySet);
  1791. }
  1792. bool cmake::LoadCache(const std::string& path, bool internal,
  1793. std::set<std::string>& excludes,
  1794. std::set<std::string>& includes)
  1795. {
  1796. bool result = this->State->LoadCache(path, internal, excludes, includes);
  1797. static const auto entries = { "CMAKE_CACHE_MAJOR_VERSION",
  1798. "CMAKE_CACHE_MINOR_VERSION" };
  1799. for (auto const& entry : entries) {
  1800. this->UnwatchUnusedCli(entry);
  1801. }
  1802. return result;
  1803. }
  1804. bool cmake::SaveCache(const std::string& path)
  1805. {
  1806. bool result = this->State->SaveCache(path, this->GetMessenger());
  1807. static const auto entries = { "CMAKE_CACHE_MAJOR_VERSION",
  1808. "CMAKE_CACHE_MINOR_VERSION",
  1809. "CMAKE_CACHE_PATCH_VERSION",
  1810. "CMAKE_CACHEFILE_DIR" };
  1811. for (auto const& entry : entries) {
  1812. this->UnwatchUnusedCli(entry);
  1813. }
  1814. return result;
  1815. }
  1816. bool cmake::DeleteCache(const std::string& path)
  1817. {
  1818. return this->State->DeleteCache(path);
  1819. }
  1820. void cmake::SetProgressCallback(ProgressCallbackType f)
  1821. {
  1822. this->ProgressCallback = std::move(f);
  1823. }
  1824. void cmake::UpdateProgress(const std::string& msg, float prog)
  1825. {
  1826. if (this->ProgressCallback && !this->State->GetIsInTryCompile()) {
  1827. this->ProgressCallback(msg, prog);
  1828. }
  1829. }
  1830. bool cmake::GetIsInTryCompile() const
  1831. {
  1832. return this->State->GetIsInTryCompile();
  1833. }
  1834. void cmake::SetIsInTryCompile(bool b)
  1835. {
  1836. this->State->SetIsInTryCompile(b);
  1837. }
  1838. void cmake::AppendGlobalGeneratorsDocumentation(
  1839. std::vector<cmDocumentationEntry>& v)
  1840. {
  1841. const auto defaultGenerator = this->EvaluateDefaultGlobalGenerator();
  1842. const std::string defaultName = defaultGenerator->GetName();
  1843. bool foundDefaultOne = false;
  1844. for (cmGlobalGeneratorFactory* g : this->Generators) {
  1845. cmDocumentationEntry e;
  1846. g->GetDocumentation(e);
  1847. if (!foundDefaultOne && cmHasPrefix(e.Name, defaultName)) {
  1848. e.CustomNamePrefix = '*';
  1849. foundDefaultOne = true;
  1850. }
  1851. v.push_back(std::move(e));
  1852. }
  1853. }
  1854. void cmake::AppendExtraGeneratorsDocumentation(
  1855. std::vector<cmDocumentationEntry>& v)
  1856. {
  1857. for (cmExternalMakefileProjectGeneratorFactory* eg : this->ExtraGenerators) {
  1858. const std::string doc = eg->GetDocumentation();
  1859. const std::string name = eg->GetName();
  1860. // Aliases:
  1861. for (std::string const& a : eg->Aliases) {
  1862. cmDocumentationEntry e;
  1863. e.Name = a;
  1864. e.Brief = doc;
  1865. v.push_back(std::move(e));
  1866. }
  1867. // Full names:
  1868. const std::vector<std::string> generators =
  1869. eg->GetSupportedGlobalGenerators();
  1870. for (std::string const& g : generators) {
  1871. cmDocumentationEntry e;
  1872. e.Name =
  1873. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(g, name);
  1874. e.Brief = doc;
  1875. v.push_back(std::move(e));
  1876. }
  1877. }
  1878. }
  1879. std::vector<cmDocumentationEntry> cmake::GetGeneratorsDocumentation()
  1880. {
  1881. std::vector<cmDocumentationEntry> v;
  1882. this->AppendGlobalGeneratorsDocumentation(v);
  1883. this->AppendExtraGeneratorsDocumentation(v);
  1884. return v;
  1885. }
  1886. void cmake::PrintGeneratorList()
  1887. {
  1888. #ifndef CMAKE_BOOTSTRAP
  1889. cmDocumentation doc;
  1890. auto generators = this->GetGeneratorsDocumentation();
  1891. doc.AppendSection("Generators", generators);
  1892. std::cerr << "\n";
  1893. doc.PrintDocumentation(cmDocumentation::ListGenerators, std::cerr);
  1894. #endif
  1895. }
  1896. void cmake::UpdateConversionPathTable()
  1897. {
  1898. // Update the path conversion table with any specified file:
  1899. const std::string* tablepath =
  1900. this->State->GetInitializedCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1901. if (tablepath) {
  1902. cmsys::ifstream table(tablepath->c_str());
  1903. if (!table) {
  1904. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to " + *tablepath +
  1905. ". CMake can not open file.");
  1906. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1907. } else {
  1908. std::string a;
  1909. std::string b;
  1910. while (!table.eof()) {
  1911. // two entries per line
  1912. table >> a;
  1913. table >> b;
  1914. cmSystemTools::AddTranslationPath(a, b);
  1915. }
  1916. }
  1917. }
  1918. }
  1919. int cmake::CheckBuildSystem()
  1920. {
  1921. // We do not need to rerun CMake. Check dependency integrity.
  1922. const bool verbose = isCMakeVerbose();
  1923. // This method will check the integrity of the build system if the
  1924. // option was given on the command line. It reads the given file to
  1925. // determine whether CMake should rerun.
  1926. // If no file is provided for the check, we have to rerun.
  1927. if (this->CheckBuildSystemArgument.empty()) {
  1928. if (verbose) {
  1929. cmSystemTools::Stdout("Re-run cmake no build system arguments\n");
  1930. }
  1931. return 1;
  1932. }
  1933. // If the file provided does not exist, we have to rerun.
  1934. if (!cmSystemTools::FileExists(this->CheckBuildSystemArgument)) {
  1935. if (verbose) {
  1936. std::ostringstream msg;
  1937. msg << "Re-run cmake missing file: " << this->CheckBuildSystemArgument
  1938. << "\n";
  1939. cmSystemTools::Stdout(msg.str());
  1940. }
  1941. return 1;
  1942. }
  1943. // Read the rerun check file and use it to decide whether to do the
  1944. // global generate.
  1945. // Actually, all we need is the `set` command.
  1946. cmake cm(RoleScript, cmState::Unknown);
  1947. cm.SetHomeDirectory("");
  1948. cm.SetHomeOutputDirectory("");
  1949. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  1950. cmGlobalGenerator gg(&cm);
  1951. cmMakefile mf(&gg, cm.GetCurrentSnapshot());
  1952. if (!mf.ReadListFile(this->CheckBuildSystemArgument) ||
  1953. cmSystemTools::GetErrorOccuredFlag()) {
  1954. if (verbose) {
  1955. std::ostringstream msg;
  1956. msg << "Re-run cmake error reading : " << this->CheckBuildSystemArgument
  1957. << "\n";
  1958. cmSystemTools::Stdout(msg.str());
  1959. }
  1960. // There was an error reading the file. Just rerun.
  1961. return 1;
  1962. }
  1963. if (this->ClearBuildSystem) {
  1964. // Get the generator used for this build system.
  1965. const char* genName = mf.GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1966. if (!genName || genName[0] == '\0') {
  1967. genName = "Unix Makefiles";
  1968. }
  1969. // Create the generator and use it to clear the dependencies.
  1970. std::unique_ptr<cmGlobalGenerator> ggd(
  1971. this->CreateGlobalGenerator(genName));
  1972. if (ggd) {
  1973. cm.GetCurrentSnapshot().SetDefaultDefinitions();
  1974. cmMakefile mfd(ggd.get(), cm.GetCurrentSnapshot());
  1975. auto lgd = ggd->CreateLocalGenerator(&mfd);
  1976. lgd->ClearDependencies(&mfd, verbose);
  1977. }
  1978. }
  1979. // If any byproduct of makefile generation is missing we must re-run.
  1980. std::vector<std::string> products;
  1981. if (const char* productStr = mf.GetDefinition("CMAKE_MAKEFILE_PRODUCTS")) {
  1982. cmExpandList(productStr, products);
  1983. }
  1984. for (std::string const& p : products) {
  1985. if (!(cmSystemTools::FileExists(p) || cmSystemTools::FileIsSymlink(p))) {
  1986. if (verbose) {
  1987. std::ostringstream msg;
  1988. msg << "Re-run cmake, missing byproduct: " << p << "\n";
  1989. cmSystemTools::Stdout(msg.str());
  1990. }
  1991. return 1;
  1992. }
  1993. }
  1994. // Get the set of dependencies and outputs.
  1995. std::vector<std::string> depends;
  1996. std::vector<std::string> outputs;
  1997. const char* dependsStr = mf.GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1998. const char* outputsStr = mf.GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1999. if (dependsStr && outputsStr) {
  2000. cmExpandList(dependsStr, depends);
  2001. cmExpandList(outputsStr, outputs);
  2002. }
  2003. if (depends.empty() || outputs.empty()) {
  2004. // Not enough information was provided to do the test. Just rerun.
  2005. if (verbose) {
  2006. cmSystemTools::Stdout("Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  2007. "or CMAKE_MAKEFILE_OUTPUTS :\n");
  2008. }
  2009. return 1;
  2010. }
  2011. // Find the newest dependency.
  2012. auto dep = depends.begin();
  2013. std::string dep_newest = *dep++;
  2014. for (; dep != depends.end(); ++dep) {
  2015. int result = 0;
  2016. if (this->FileTimeCache->Compare(dep_newest, *dep, &result)) {
  2017. if (result < 0) {
  2018. dep_newest = *dep;
  2019. }
  2020. } else {
  2021. if (verbose) {
  2022. cmSystemTools::Stdout(
  2023. "Re-run cmake: build system dependency is missing\n");
  2024. }
  2025. return 1;
  2026. }
  2027. }
  2028. // Find the oldest output.
  2029. auto out = outputs.begin();
  2030. std::string out_oldest = *out++;
  2031. for (; out != outputs.end(); ++out) {
  2032. int result = 0;
  2033. if (this->FileTimeCache->Compare(out_oldest, *out, &result)) {
  2034. if (result > 0) {
  2035. out_oldest = *out;
  2036. }
  2037. } else {
  2038. if (verbose) {
  2039. cmSystemTools::Stdout(
  2040. "Re-run cmake: build system output is missing\n");
  2041. }
  2042. return 1;
  2043. }
  2044. }
  2045. // If any output is older than any dependency then rerun.
  2046. {
  2047. int result = 0;
  2048. if (!this->FileTimeCache->Compare(out_oldest, dep_newest, &result) ||
  2049. result < 0) {
  2050. if (verbose) {
  2051. std::ostringstream msg;
  2052. msg << "Re-run cmake file: " << out_oldest
  2053. << " older than: " << dep_newest << "\n";
  2054. cmSystemTools::Stdout(msg.str());
  2055. }
  2056. return 1;
  2057. }
  2058. }
  2059. // No need to rerun.
  2060. return 0;
  2061. }
  2062. void cmake::TruncateOutputLog(const char* fname)
  2063. {
  2064. std::string fullPath = cmStrCat(this->GetHomeOutputDirectory(), '/', fname);
  2065. struct stat st;
  2066. if (::stat(fullPath.c_str(), &st)) {
  2067. return;
  2068. }
  2069. if (!this->State->GetInitializedCacheValue("CMAKE_CACHEFILE_DIR")) {
  2070. cmSystemTools::RemoveFile(fullPath);
  2071. return;
  2072. }
  2073. off_t fsize = st.st_size;
  2074. const off_t maxFileSize = 50 * 1024;
  2075. if (fsize < maxFileSize) {
  2076. // TODO: truncate file
  2077. return;
  2078. }
  2079. }
  2080. void cmake::MarkCliAsUsed(const std::string& variable)
  2081. {
  2082. this->UsedCliVariables[variable] = true;
  2083. }
  2084. void cmake::GenerateGraphViz(const std::string& fileName) const
  2085. {
  2086. #ifndef CMAKE_BOOTSTRAP
  2087. cmGraphVizWriter gvWriter(fileName, this->GetGlobalGenerator());
  2088. std::string settingsFile =
  2089. cmStrCat(this->GetHomeOutputDirectory(), "/CMakeGraphVizOptions.cmake");
  2090. std::string fallbackSettingsFile =
  2091. cmStrCat(this->GetHomeDirectory(), "/CMakeGraphVizOptions.cmake");
  2092. gvWriter.ReadSettings(settingsFile, fallbackSettingsFile);
  2093. gvWriter.Write();
  2094. #endif
  2095. }
  2096. void cmake::SetProperty(const std::string& prop, const char* value)
  2097. {
  2098. this->State->SetGlobalProperty(prop, value);
  2099. }
  2100. void cmake::AppendProperty(const std::string& prop, const char* value,
  2101. bool asString)
  2102. {
  2103. this->State->AppendGlobalProperty(prop, value, asString);
  2104. }
  2105. const char* cmake::GetProperty(const std::string& prop)
  2106. {
  2107. return this->State->GetGlobalProperty(prop);
  2108. }
  2109. bool cmake::GetPropertyAsBool(const std::string& prop)
  2110. {
  2111. return this->State->GetGlobalPropertyAsBool(prop);
  2112. }
  2113. cmInstalledFile* cmake::GetOrCreateInstalledFile(cmMakefile* mf,
  2114. const std::string& name)
  2115. {
  2116. auto i = this->InstalledFiles.find(name);
  2117. if (i != this->InstalledFiles.end()) {
  2118. cmInstalledFile& file = i->second;
  2119. return &file;
  2120. }
  2121. cmInstalledFile& file = this->InstalledFiles[name];
  2122. file.SetName(mf, name);
  2123. return &file;
  2124. }
  2125. cmInstalledFile const* cmake::GetInstalledFile(const std::string& name) const
  2126. {
  2127. auto i = this->InstalledFiles.find(name);
  2128. if (i != this->InstalledFiles.end()) {
  2129. cmInstalledFile const& file = i->second;
  2130. return &file;
  2131. }
  2132. return nullptr;
  2133. }
  2134. int cmake::GetSystemInformation(std::vector<std::string>& args)
  2135. {
  2136. // so create the directory
  2137. std::string resultFile;
  2138. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2139. std::string destPath = cwd + "/__cmake_systeminformation";
  2140. cmSystemTools::RemoveADirectory(destPath);
  2141. if (!cmSystemTools::MakeDirectory(destPath)) {
  2142. std::cerr << "Error: --system-information must be run from a "
  2143. "writable directory!\n";
  2144. return 1;
  2145. }
  2146. // process the arguments
  2147. bool writeToStdout = true;
  2148. for (unsigned int i = 1; i < args.size(); ++i) {
  2149. std::string const& arg = args[i];
  2150. if (arg.find("-G", 0) == 0) {
  2151. std::string value = arg.substr(2);
  2152. if (value.empty()) {
  2153. ++i;
  2154. if (i >= args.size()) {
  2155. cmSystemTools::Error("No generator specified for -G");
  2156. this->PrintGeneratorList();
  2157. return -1;
  2158. }
  2159. value = args[i];
  2160. }
  2161. cmGlobalGenerator* gen = this->CreateGlobalGenerator(value);
  2162. if (!gen) {
  2163. cmSystemTools::Error("Could not create named generator " + value);
  2164. this->PrintGeneratorList();
  2165. } else {
  2166. this->SetGlobalGenerator(gen);
  2167. }
  2168. }
  2169. // no option assume it is the output file
  2170. else {
  2171. if (!cmSystemTools::FileIsFullPath(arg)) {
  2172. resultFile = cmStrCat(cwd, '/');
  2173. }
  2174. resultFile += arg;
  2175. writeToStdout = false;
  2176. }
  2177. }
  2178. // we have to find the module directory, so we can copy the files
  2179. this->AddCMakePaths();
  2180. std::string modulesPath =
  2181. cmStrCat(cmSystemTools::GetCMakeRoot(), "/Modules");
  2182. std::string inFile = cmStrCat(modulesPath, "/SystemInformation.cmake");
  2183. std::string outFile = cmStrCat(destPath, "/CMakeLists.txt");
  2184. // Copy file
  2185. if (!cmsys::SystemTools::CopyFileAlways(inFile, outFile)) {
  2186. std::cerr << "Error copying file \"" << inFile << "\" to \"" << outFile
  2187. << "\".\n";
  2188. return 1;
  2189. }
  2190. // do we write to a file or to stdout?
  2191. if (resultFile.empty()) {
  2192. resultFile = cmStrCat(cwd, "/__cmake_systeminformation/results.txt");
  2193. }
  2194. {
  2195. // now run cmake on the CMakeLists file
  2196. cmWorkingDirectory workdir(destPath);
  2197. if (workdir.Failed()) {
  2198. // We created the directory and we were able to copy the CMakeLists.txt
  2199. // file to it, so we wouldn't expect to get here unless the default
  2200. // permissions are questionable or some other process has deleted the
  2201. // directory
  2202. std::cerr << "Failed to change to directory " << destPath << " : "
  2203. << std::strerror(workdir.GetLastResult()) << std::endl;
  2204. return 1;
  2205. }
  2206. std::vector<std::string> args2;
  2207. args2.push_back(args[0]);
  2208. args2.push_back(destPath);
  2209. args2.push_back("-DRESULT_FILE=" + resultFile);
  2210. int res = this->Run(args2, false);
  2211. if (res != 0) {
  2212. std::cerr << "Error: --system-information failed on internal CMake!\n";
  2213. return res;
  2214. }
  2215. }
  2216. // echo results to stdout if needed
  2217. if (writeToStdout) {
  2218. FILE* fin = cmsys::SystemTools::Fopen(resultFile, "r");
  2219. if (fin) {
  2220. const int bufferSize = 4096;
  2221. char buffer[bufferSize];
  2222. size_t n;
  2223. while ((n = fread(buffer, 1, bufferSize, fin)) > 0) {
  2224. for (char* c = buffer; c < buffer + n; ++c) {
  2225. putc(*c, stdout);
  2226. }
  2227. fflush(stdout);
  2228. }
  2229. fclose(fin);
  2230. }
  2231. }
  2232. // clean up the directory
  2233. cmSystemTools::RemoveADirectory(destPath);
  2234. return 0;
  2235. }
  2236. static bool cmakeCheckStampFile(const std::string& stampName)
  2237. {
  2238. // The stamp file does not exist. Use the stamp dependencies to
  2239. // determine whether it is really out of date. This works in
  2240. // conjunction with cmLocalVisualStudio7Generator to avoid
  2241. // repeatedly re-running CMake when the user rebuilds the entire
  2242. // solution.
  2243. std::string stampDepends = cmStrCat(stampName, ".depend");
  2244. #if defined(_WIN32) || defined(__CYGWIN__)
  2245. cmsys::ifstream fin(stampDepends.c_str(), std::ios::in | std::ios::binary);
  2246. #else
  2247. cmsys::ifstream fin(stampDepends.c_str());
  2248. #endif
  2249. if (!fin) {
  2250. // The stamp dependencies file cannot be read. Just assume the
  2251. // build system is really out of date.
  2252. std::cout << "CMake is re-running because " << stampName
  2253. << " dependency file is missing.\n";
  2254. return false;
  2255. }
  2256. // Compare the stamp dependencies against the dependency file itself.
  2257. {
  2258. cmFileTimeCache ftc;
  2259. std::string dep;
  2260. while (cmSystemTools::GetLineFromStream(fin, dep)) {
  2261. int result;
  2262. if (!dep.empty() && dep[0] != '#' &&
  2263. (!ftc.Compare(stampDepends, dep, &result) || result < 0)) {
  2264. // The stamp depends file is older than this dependency. The
  2265. // build system is really out of date.
  2266. std::cout << "CMake is re-running because " << stampName
  2267. << " is out-of-date.\n";
  2268. std::cout << " the file '" << dep << "'\n";
  2269. std::cout << " is newer than '" << stampDepends << "'\n";
  2270. std::cout << " result='" << result << "'\n";
  2271. return false;
  2272. }
  2273. }
  2274. }
  2275. // The build system is up to date. The stamp file has been removed
  2276. // by the VS IDE due to a "rebuild" request. Restore it atomically.
  2277. std::ostringstream stampTempStream;
  2278. stampTempStream << stampName << ".tmp" << cmSystemTools::RandomSeed();
  2279. std::string stampTemp = stampTempStream.str();
  2280. {
  2281. // TODO: Teach cmGeneratedFileStream to use a random temp file (with
  2282. // multiple tries in unlikely case of conflict) and use that here.
  2283. cmsys::ofstream stamp(stampTemp.c_str());
  2284. stamp << "# CMake generation timestamp file for this directory.\n";
  2285. }
  2286. if (cmSystemTools::RenameFile(stampTemp, stampName)) {
  2287. // CMake does not need to re-run because the stamp file is up-to-date.
  2288. return true;
  2289. }
  2290. cmSystemTools::RemoveFile(stampTemp);
  2291. cmSystemTools::Error("Cannot restore timestamp " + stampName);
  2292. return false;
  2293. }
  2294. static bool cmakeCheckStampList(const std::string& stampList)
  2295. {
  2296. // If the stamp list does not exist CMake must rerun to generate it.
  2297. if (!cmSystemTools::FileExists(stampList)) {
  2298. std::cout << "CMake is re-running because generate.stamp.list "
  2299. << "is missing.\n";
  2300. return false;
  2301. }
  2302. cmsys::ifstream fin(stampList.c_str());
  2303. if (!fin) {
  2304. std::cout << "CMake is re-running because generate.stamp.list "
  2305. << "could not be read.\n";
  2306. return false;
  2307. }
  2308. // Check each stamp.
  2309. std::string stampName;
  2310. while (cmSystemTools::GetLineFromStream(fin, stampName)) {
  2311. if (!cmakeCheckStampFile(stampName)) {
  2312. return false;
  2313. }
  2314. }
  2315. return true;
  2316. }
  2317. void cmake::IssueMessage(MessageType t, std::string const& text,
  2318. cmListFileBacktrace const& backtrace) const
  2319. {
  2320. this->Messenger->IssueMessage(t, text, backtrace);
  2321. }
  2322. std::vector<std::string> cmake::GetDebugConfigs()
  2323. {
  2324. std::vector<std::string> configs;
  2325. if (const char* config_list =
  2326. this->State->GetGlobalProperty("DEBUG_CONFIGURATIONS")) {
  2327. // Expand the specified list and convert to upper-case.
  2328. cmExpandList(config_list, configs);
  2329. std::transform(configs.begin(), configs.end(), configs.begin(),
  2330. cmSystemTools::UpperCase);
  2331. }
  2332. // If no configurations were specified, use a default list.
  2333. if (configs.empty()) {
  2334. configs.emplace_back("DEBUG");
  2335. }
  2336. return configs;
  2337. }
  2338. int cmake::Build(int jobs, const std::string& dir,
  2339. const std::vector<std::string>& targets,
  2340. const std::string& config,
  2341. const std::vector<std::string>& nativeOptions, bool clean,
  2342. bool verbose)
  2343. {
  2344. this->SetHomeDirectory("");
  2345. this->SetHomeOutputDirectory("");
  2346. if (!cmSystemTools::FileIsDirectory(dir)) {
  2347. std::cerr << "Error: " << dir << " is not a directory\n";
  2348. return 1;
  2349. }
  2350. std::string cachePath = FindCacheFile(dir);
  2351. if (!this->LoadCache(cachePath)) {
  2352. std::cerr << "Error: could not load cache\n";
  2353. return 1;
  2354. }
  2355. const char* cachedGenerator =
  2356. this->State->GetCacheEntryValue("CMAKE_GENERATOR");
  2357. if (!cachedGenerator) {
  2358. std::cerr << "Error: could not find CMAKE_GENERATOR in Cache\n";
  2359. return 1;
  2360. }
  2361. cmGlobalGenerator* gen = this->CreateGlobalGenerator(cachedGenerator);
  2362. if (!gen) {
  2363. std::cerr << "Error: could create CMAKE_GENERATOR \"" << cachedGenerator
  2364. << "\"\n";
  2365. return 1;
  2366. }
  2367. this->SetGlobalGenerator(gen);
  2368. const char* cachedGeneratorInstance =
  2369. this->State->GetCacheEntryValue("CMAKE_GENERATOR_INSTANCE");
  2370. if (cachedGeneratorInstance) {
  2371. cmMakefile mf(gen, this->GetCurrentSnapshot());
  2372. if (!gen->SetGeneratorInstance(cachedGeneratorInstance, &mf)) {
  2373. return 1;
  2374. }
  2375. }
  2376. const char* cachedGeneratorPlatform =
  2377. this->State->GetCacheEntryValue("CMAKE_GENERATOR_PLATFORM");
  2378. if (cachedGeneratorPlatform) {
  2379. cmMakefile mf(gen, this->GetCurrentSnapshot());
  2380. if (!gen->SetGeneratorPlatform(cachedGeneratorPlatform, &mf)) {
  2381. return 1;
  2382. }
  2383. }
  2384. const char* cachedGeneratorToolset =
  2385. this->State->GetCacheEntryValue("CMAKE_GENERATOR_TOOLSET");
  2386. if (cachedGeneratorToolset) {
  2387. cmMakefile mf(gen, this->GetCurrentSnapshot());
  2388. if (!gen->SetGeneratorToolset(cachedGeneratorToolset, true, &mf)) {
  2389. return 1;
  2390. }
  2391. }
  2392. std::string output;
  2393. std::string projName;
  2394. const char* cachedProjectName =
  2395. this->State->GetCacheEntryValue("CMAKE_PROJECT_NAME");
  2396. if (!cachedProjectName) {
  2397. std::cerr << "Error: could not find CMAKE_PROJECT_NAME in Cache\n";
  2398. return 1;
  2399. }
  2400. projName = cachedProjectName;
  2401. const char* cachedVerbose =
  2402. this->State->GetCacheEntryValue("CMAKE_VERBOSE_MAKEFILE");
  2403. if (cmIsOn(cachedVerbose)) {
  2404. verbose = true;
  2405. }
  2406. #ifdef CMAKE_HAVE_VS_GENERATORS
  2407. // For VS generators, explicitly check if regeneration is necessary before
  2408. // actually starting the build. If not done separately from the build
  2409. // itself, there is the risk of building an out-of-date solution file due
  2410. // to limitations of the underlying build system.
  2411. std::string const stampList = cachePath + "/" + "CMakeFiles/" +
  2412. cmGlobalVisualStudio9Generator::GetGenerateStampList();
  2413. // Note that the stampList file only exists for VS generators.
  2414. if (cmSystemTools::FileExists(stampList)) {
  2415. // Check if running for Visual Studio 9 - we need to explicitly run
  2416. // the glob verification script before starting the build
  2417. this->AddScriptingCommands();
  2418. if (this->GlobalGenerator->MatchesGeneratorName("Visual Studio 9 2008")) {
  2419. std::string const globVerifyScript =
  2420. cachePath + "/" + "CMakeFiles/" + "VerifyGlobs.cmake";
  2421. if (cmSystemTools::FileExists(globVerifyScript)) {
  2422. std::vector<std::string> args;
  2423. this->ReadListFile(args, globVerifyScript);
  2424. }
  2425. }
  2426. if (!cmakeCheckStampList(stampList)) {
  2427. // Correctly initialize the home (=source) and home output (=binary)
  2428. // directories, which is required for running the generation step.
  2429. std::string homeOrig = this->GetHomeDirectory();
  2430. std::string homeOutputOrig = this->GetHomeOutputDirectory();
  2431. this->SetDirectoriesFromFile(cachePath);
  2432. this->AddProjectCommands();
  2433. int ret = this->Configure();
  2434. if (ret) {
  2435. cmSystemTools::Message("CMake Configure step failed. "
  2436. "Build files cannot be regenerated correctly.");
  2437. return ret;
  2438. }
  2439. ret = this->Generate();
  2440. if (ret) {
  2441. cmSystemTools::Message("CMake Generate step failed. "
  2442. "Build files cannot be regenerated correctly.");
  2443. return ret;
  2444. }
  2445. std::string message = cmStrCat("Build files have been written to: ",
  2446. this->GetHomeOutputDirectory());
  2447. this->UpdateProgress(message, -1);
  2448. // Restore the previously set directories to their original value.
  2449. this->SetHomeDirectory(homeOrig);
  2450. this->SetHomeOutputDirectory(homeOutputOrig);
  2451. }
  2452. }
  2453. #endif
  2454. gen->PrintBuildCommandAdvice(std::cerr, jobs);
  2455. return gen->Build(jobs, "", dir, projName, targets, output, "", config,
  2456. clean, false, verbose, cmDuration::zero(),
  2457. cmSystemTools::OUTPUT_PASSTHROUGH, nativeOptions);
  2458. }
  2459. bool cmake::Open(const std::string& dir, bool dryRun)
  2460. {
  2461. this->SetHomeDirectory("");
  2462. this->SetHomeOutputDirectory("");
  2463. if (!cmSystemTools::FileIsDirectory(dir)) {
  2464. std::cerr << "Error: " << dir << " is not a directory\n";
  2465. return false;
  2466. }
  2467. std::string cachePath = FindCacheFile(dir);
  2468. if (!this->LoadCache(cachePath)) {
  2469. std::cerr << "Error: could not load cache\n";
  2470. return false;
  2471. }
  2472. const char* genName = this->State->GetCacheEntryValue("CMAKE_GENERATOR");
  2473. if (!genName) {
  2474. std::cerr << "Error: could not find CMAKE_GENERATOR in Cache\n";
  2475. return false;
  2476. }
  2477. const std::string* extraGenName =
  2478. this->State->GetInitializedCacheValue("CMAKE_EXTRA_GENERATOR");
  2479. std::string fullName =
  2480. cmExternalMakefileProjectGenerator::CreateFullGeneratorName(
  2481. genName, extraGenName ? *extraGenName : "");
  2482. std::unique_ptr<cmGlobalGenerator> gen(
  2483. this->CreateGlobalGenerator(fullName));
  2484. if (!gen) {
  2485. std::cerr << "Error: could create CMAKE_GENERATOR \"" << fullName
  2486. << "\"\n";
  2487. return false;
  2488. }
  2489. const char* cachedProjectName =
  2490. this->State->GetCacheEntryValue("CMAKE_PROJECT_NAME");
  2491. if (!cachedProjectName) {
  2492. std::cerr << "Error: could not find CMAKE_PROJECT_NAME in Cache\n";
  2493. return false;
  2494. }
  2495. return gen->Open(dir, cachedProjectName, dryRun);
  2496. }
  2497. void cmake::WatchUnusedCli(const std::string& var)
  2498. {
  2499. #ifndef CMAKE_BOOTSTRAP
  2500. this->VariableWatch->AddWatch(var, cmWarnUnusedCliWarning, this);
  2501. if (!cmContains(this->UsedCliVariables, var)) {
  2502. this->UsedCliVariables[var] = false;
  2503. }
  2504. #endif
  2505. }
  2506. void cmake::UnwatchUnusedCli(const std::string& var)
  2507. {
  2508. #ifndef CMAKE_BOOTSTRAP
  2509. this->VariableWatch->RemoveWatch(var, cmWarnUnusedCliWarning);
  2510. this->UsedCliVariables.erase(var);
  2511. #endif
  2512. }
  2513. void cmake::RunCheckForUnusedVariables()
  2514. {
  2515. #ifndef CMAKE_BOOTSTRAP
  2516. bool haveUnused = false;
  2517. std::ostringstream msg;
  2518. msg << "Manually-specified variables were not used by the project:";
  2519. for (auto const& it : this->UsedCliVariables) {
  2520. if (!it.second) {
  2521. haveUnused = true;
  2522. msg << "\n " << it.first;
  2523. }
  2524. }
  2525. if (haveUnused) {
  2526. this->IssueMessage(MessageType::WARNING, msg.str());
  2527. }
  2528. #endif
  2529. }
  2530. bool cmake::GetSuppressDevWarnings() const
  2531. {
  2532. return this->Messenger->GetSuppressDevWarnings();
  2533. }
  2534. void cmake::SetSuppressDevWarnings(bool b)
  2535. {
  2536. std::string value;
  2537. // equivalent to -Wno-dev
  2538. if (b) {
  2539. value = "TRUE";
  2540. }
  2541. // equivalent to -Wdev
  2542. else {
  2543. value = "FALSE";
  2544. }
  2545. this->AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", value.c_str(),
  2546. "Suppress Warnings that are meant for"
  2547. " the author of the CMakeLists.txt files.",
  2548. cmStateEnums::INTERNAL);
  2549. }
  2550. bool cmake::GetSuppressDeprecatedWarnings() const
  2551. {
  2552. return this->Messenger->GetSuppressDeprecatedWarnings();
  2553. }
  2554. void cmake::SetSuppressDeprecatedWarnings(bool b)
  2555. {
  2556. std::string value;
  2557. // equivalent to -Wno-deprecated
  2558. if (b) {
  2559. value = "FALSE";
  2560. }
  2561. // equivalent to -Wdeprecated
  2562. else {
  2563. value = "TRUE";
  2564. }
  2565. this->AddCacheEntry("CMAKE_WARN_DEPRECATED", value.c_str(),
  2566. "Whether to issue warnings for deprecated "
  2567. "functionality.",
  2568. cmStateEnums::INTERNAL);
  2569. }
  2570. bool cmake::GetDevWarningsAsErrors() const
  2571. {
  2572. return this->Messenger->GetDevWarningsAsErrors();
  2573. }
  2574. void cmake::SetDevWarningsAsErrors(bool b)
  2575. {
  2576. std::string value;
  2577. // equivalent to -Werror=dev
  2578. if (b) {
  2579. value = "FALSE";
  2580. }
  2581. // equivalent to -Wno-error=dev
  2582. else {
  2583. value = "TRUE";
  2584. }
  2585. this->AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_ERRORS", value.c_str(),
  2586. "Suppress errors that are meant for"
  2587. " the author of the CMakeLists.txt files.",
  2588. cmStateEnums::INTERNAL);
  2589. }
  2590. bool cmake::GetDeprecatedWarningsAsErrors() const
  2591. {
  2592. return this->Messenger->GetDeprecatedWarningsAsErrors();
  2593. }
  2594. void cmake::SetDeprecatedWarningsAsErrors(bool b)
  2595. {
  2596. std::string value;
  2597. // equivalent to -Werror=deprecated
  2598. if (b) {
  2599. value = "TRUE";
  2600. }
  2601. // equivalent to -Wno-error=deprecated
  2602. else {
  2603. value = "FALSE";
  2604. }
  2605. this->AddCacheEntry("CMAKE_ERROR_DEPRECATED", value.c_str(),
  2606. "Whether to issue deprecation errors for macros"
  2607. " and functions.",
  2608. cmStateEnums::INTERNAL);
  2609. }