cmake.cxx 86 KB

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