cmake.cxx 80 KB

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