cmake.cxx 85 KB

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