cmake.cxx 80 KB

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