cmake.cxx 81 KB

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