cmake.cxx 81 KB

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