cmake.cxx 78 KB

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