cmake.cxx 82 KB

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