cmake.cxx 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738
  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. if(const char* help = ci.GetProperty("HELPSTRING"))
  1141. {
  1142. save.help = help;
  1143. }
  1144. }
  1145. saved.push_back(save);
  1146. }
  1147. // remove the cache
  1148. this->CacheManager->DeleteCache(this->GetStartOutputDirectory());
  1149. // load the empty cache
  1150. this->LoadCache();
  1151. // restore the changed compilers
  1152. for(std::vector<SaveCacheEntry>::iterator i = saved.begin();
  1153. i != saved.end(); ++i)
  1154. {
  1155. this->AddCacheEntry(i->key.c_str(), i->value.c_str(),
  1156. i->help.c_str(), i->type);
  1157. }
  1158. cmSystemTools::Message(warning.str().c_str());
  1159. // avoid reconfigure if there were errors
  1160. if(!cmSystemTools::GetErrorOccuredFlag())
  1161. {
  1162. // re-run configure
  1163. return this->Configure();
  1164. }
  1165. return 0;
  1166. }
  1167. int cmake::Configure()
  1168. {
  1169. if(this->DoSuppressDevWarnings)
  1170. {
  1171. if(this->SuppressDevWarnings)
  1172. {
  1173. this->CacheManager->
  1174. AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "TRUE",
  1175. "Suppress Warnings that are meant for"
  1176. " the author of the CMakeLists.txt files.",
  1177. cmCacheManager::INTERNAL);
  1178. }
  1179. else
  1180. {
  1181. this->CacheManager->
  1182. AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "FALSE",
  1183. "Suppress Warnings that are meant for"
  1184. " the author of the CMakeLists.txt files.",
  1185. cmCacheManager::INTERNAL);
  1186. }
  1187. }
  1188. int ret = this->ActualConfigure();
  1189. const char* delCacheVars =
  1190. this->GetProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_");
  1191. if(delCacheVars && delCacheVars[0] != 0)
  1192. {
  1193. return this->HandleDeleteCacheVariables(delCacheVars);
  1194. }
  1195. return ret;
  1196. }
  1197. int cmake::ActualConfigure()
  1198. {
  1199. // Construct right now our path conversion table before it's too late:
  1200. this->UpdateConversionPathTable();
  1201. this->CleanupCommandsAndMacros();
  1202. int res = 0;
  1203. if ( this->GetWorkingMode() == NORMAL_MODE )
  1204. {
  1205. res = this->DoPreConfigureChecks();
  1206. }
  1207. if ( res < 0 )
  1208. {
  1209. return -2;
  1210. }
  1211. if ( !res )
  1212. {
  1213. this->CacheManager->AddCacheEntry
  1214. ("CMAKE_HOME_DIRECTORY",
  1215. this->GetHomeDirectory(),
  1216. "Start directory with the top level CMakeLists.txt file for this "
  1217. "project",
  1218. cmCacheManager::INTERNAL);
  1219. }
  1220. // no generator specified on the command line
  1221. if(!this->GlobalGenerator)
  1222. {
  1223. const char* genName =
  1224. this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1225. const char* extraGenName =
  1226. this->CacheManager->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  1227. if(genName)
  1228. {
  1229. std::string fullName = cmExternalMakefileProjectGenerator::
  1230. CreateFullGeneratorName(genName, extraGenName);
  1231. this->GlobalGenerator = this->CreateGlobalGenerator(fullName.c_str());
  1232. }
  1233. if(this->GlobalGenerator)
  1234. {
  1235. // set the global flag for unix style paths on cmSystemTools as
  1236. // soon as the generator is set. This allows gmake to be used
  1237. // on windows.
  1238. cmSystemTools::SetForceUnixPaths
  1239. (this->GlobalGenerator->GetForceUnixPaths());
  1240. }
  1241. else
  1242. {
  1243. #if defined(__BORLANDC__) && defined(_WIN32)
  1244. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1245. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1246. std::string installedCompiler;
  1247. // Try to find the newest VS installed on the computer and
  1248. // use that as a default if -G is not specified
  1249. const std::string vsregBase =
  1250. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\";
  1251. std::vector<std::string> vsVerions;
  1252. vsVerions.push_back("VisualStudio\\");
  1253. vsVerions.push_back("VCExpress\\");
  1254. vsVerions.push_back("WDExpress\\");
  1255. struct VSRegistryEntryName
  1256. {
  1257. const char* MSVersion;
  1258. const char* GeneratorName;
  1259. };
  1260. VSRegistryEntryName version[] = {
  1261. {"6.0", "Visual Studio 6"},
  1262. {"7.0", "Visual Studio 7"},
  1263. {"7.1", "Visual Studio 7 .NET 2003"},
  1264. {"8.0", "Visual Studio 8 2005"},
  1265. {"9.0", "Visual Studio 9 2008"},
  1266. {"10.0", "Visual Studio 10 2010"},
  1267. {"11.0", "Visual Studio 11 2012"},
  1268. {"12.0", "Visual Studio 12 2013"},
  1269. {0, 0}};
  1270. for(int i=0; version[i].MSVersion != 0; i++)
  1271. {
  1272. for(size_t b=0; b < vsVerions.size(); b++)
  1273. {
  1274. std::string reg = vsregBase + vsVerions[b] + version[i].MSVersion;
  1275. reg += ";InstallDir]";
  1276. cmSystemTools::ExpandRegistryValues(reg,
  1277. cmSystemTools::KeyWOW64_32);
  1278. if (!(reg == "/registry"))
  1279. {
  1280. installedCompiler = version[i].GeneratorName;
  1281. break;
  1282. }
  1283. }
  1284. }
  1285. cmGlobalGenerator* gen
  1286. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1287. if(!gen)
  1288. {
  1289. gen = new cmGlobalNMakeMakefileGenerator;
  1290. }
  1291. this->SetGlobalGenerator(gen);
  1292. std::cout << "-- Building for: " << gen->GetName() << "\n";
  1293. #else
  1294. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1295. #endif
  1296. }
  1297. if(!this->GlobalGenerator)
  1298. {
  1299. cmSystemTools::Error("Could not create generator");
  1300. return -1;
  1301. }
  1302. }
  1303. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1304. if(genName)
  1305. {
  1306. if(!this->GlobalGenerator->MatchesGeneratorName(genName))
  1307. {
  1308. std::string message = "Error: generator : ";
  1309. message += this->GlobalGenerator->GetName();
  1310. message += "\nDoes not match the generator used previously: ";
  1311. message += genName;
  1312. message +=
  1313. "\nEither remove the CMakeCache.txt file or choose a different"
  1314. " binary directory.";
  1315. cmSystemTools::Error(message.c_str());
  1316. return -2;
  1317. }
  1318. }
  1319. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1320. {
  1321. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1322. this->GlobalGenerator->GetName(),
  1323. "Name of generator.",
  1324. cmCacheManager::INTERNAL);
  1325. this->CacheManager->AddCacheEntry("CMAKE_EXTRA_GENERATOR",
  1326. this->GlobalGenerator->GetExtraGeneratorName(),
  1327. "Name of external makefile project generator.",
  1328. cmCacheManager::INTERNAL);
  1329. }
  1330. if(const char* tsName =
  1331. this->CacheManager->GetCacheValue("CMAKE_GENERATOR_TOOLSET"))
  1332. {
  1333. if(this->GeneratorToolset.empty())
  1334. {
  1335. this->GeneratorToolset = tsName;
  1336. }
  1337. else if(this->GeneratorToolset != tsName)
  1338. {
  1339. std::string message = "Error: generator toolset: ";
  1340. message += this->GeneratorToolset;
  1341. message += "\nDoes not match the toolset used previously: ";
  1342. message += tsName;
  1343. message +=
  1344. "\nEither remove the CMakeCache.txt file or choose a different"
  1345. " binary directory.";
  1346. cmSystemTools::Error(message.c_str());
  1347. return -2;
  1348. }
  1349. }
  1350. else
  1351. {
  1352. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR_TOOLSET",
  1353. this->GeneratorToolset.c_str(),
  1354. "Name of generator toolset.",
  1355. cmCacheManager::INTERNAL);
  1356. }
  1357. if(!this->GeneratorToolset.empty() &&
  1358. !this->GlobalGenerator->SetGeneratorToolset(this->GeneratorToolset))
  1359. {
  1360. return -2;
  1361. }
  1362. // reset any system configuration information, except for when we are
  1363. // InTryCompile. With TryCompile the system info is taken from the parent's
  1364. // info to save time
  1365. if (!this->InTryCompile)
  1366. {
  1367. this->GlobalGenerator->ClearEnabledLanguages();
  1368. }
  1369. // Truncate log files
  1370. if (!this->InTryCompile)
  1371. {
  1372. this->TruncateOutputLog("CMakeOutput.log");
  1373. this->TruncateOutputLog("CMakeError.log");
  1374. }
  1375. // actually do the configure
  1376. this->GlobalGenerator->Configure();
  1377. // Before saving the cache
  1378. // if the project did not define one of the entries below, add them now
  1379. // so users can edit the values in the cache:
  1380. // We used to always present LIBRARY_OUTPUT_PATH and
  1381. // EXECUTABLE_OUTPUT_PATH. They are now documented as old-style and
  1382. // should no longer be used. Therefore we present them only if the
  1383. // project requires compatibility with CMake 2.4. We detect this
  1384. // here by looking for the old CMAKE_BACKWARDS_COMPATIBILITY
  1385. // variable created when CMP0001 is not set to NEW.
  1386. if(this->GetCacheManager()->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1387. {
  1388. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1389. {
  1390. this->CacheManager->AddCacheEntry
  1391. ("LIBRARY_OUTPUT_PATH", "",
  1392. "Single output directory for building all libraries.",
  1393. cmCacheManager::PATH);
  1394. }
  1395. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1396. {
  1397. this->CacheManager->AddCacheEntry
  1398. ("EXECUTABLE_OUTPUT_PATH", "",
  1399. "Single output directory for building all executables.",
  1400. cmCacheManager::PATH);
  1401. }
  1402. }
  1403. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1404. {
  1405. this->CacheManager->AddCacheEntry
  1406. ("CMAKE_USE_RELATIVE_PATHS", "OFF",
  1407. "If true, cmake will use relative paths in makefiles and projects.",
  1408. cmCacheManager::BOOL);
  1409. cmCacheManager::CacheIterator it =
  1410. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1411. if ( !it.PropertyExists("ADVANCED") )
  1412. {
  1413. it.SetProperty("ADVANCED", "1");
  1414. }
  1415. }
  1416. if(cmSystemTools::GetFatalErrorOccured() &&
  1417. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1418. cmSystemTools::IsOff(this->CacheManager->
  1419. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1420. {
  1421. // We must have a bad generator selection. Wipe the cache entry so the
  1422. // user can select another.
  1423. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1424. this->CacheManager->RemoveCacheEntry("CMAKE_EXTRA_GENERATOR");
  1425. }
  1426. cmMakefile* mf=this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  1427. if (mf->IsOn("CTEST_USE_LAUNCHERS")
  1428. && !this->GetProperty("RULE_LAUNCH_COMPILE", cmProperty::GLOBAL))
  1429. {
  1430. cmSystemTools::Error("CTEST_USE_LAUNCHERS is enabled, but the "
  1431. "RULE_LAUNCH_COMPILE global property is not defined.\n"
  1432. "Did you forget to include(CTest) in the toplevel "
  1433. "CMakeLists.txt ?");
  1434. }
  1435. // only save the cache if there were no fatal errors
  1436. if ( this->GetWorkingMode() == NORMAL_MODE )
  1437. {
  1438. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1439. }
  1440. if(cmSystemTools::GetErrorOccuredFlag())
  1441. {
  1442. return -1;
  1443. }
  1444. return 0;
  1445. }
  1446. void cmake::PreLoadCMakeFiles()
  1447. {
  1448. std::vector<std::string> args;
  1449. std::string pre_load = this->GetHomeDirectory();
  1450. if ( pre_load.size() > 0 )
  1451. {
  1452. pre_load += "/PreLoad.cmake";
  1453. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1454. {
  1455. this->ReadListFile(args, pre_load.c_str());
  1456. }
  1457. }
  1458. pre_load = this->GetHomeOutputDirectory();
  1459. if ( pre_load.size() > 0 )
  1460. {
  1461. pre_load += "/PreLoad.cmake";
  1462. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1463. {
  1464. this->ReadListFile(args, pre_load.c_str());
  1465. }
  1466. }
  1467. }
  1468. // handle a command line invocation
  1469. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1470. {
  1471. // Process the arguments
  1472. this->SetArgs(args);
  1473. if(cmSystemTools::GetErrorOccuredFlag())
  1474. {
  1475. return -1;
  1476. }
  1477. // If we are given a stamp list file check if it is really out of date.
  1478. if(!this->CheckStampList.empty() &&
  1479. cmakeCheckStampList(this->CheckStampList.c_str()))
  1480. {
  1481. return 0;
  1482. }
  1483. // If we are given a stamp file check if it is really out of date.
  1484. if(!this->CheckStampFile.empty() &&
  1485. cmakeCheckStampFile(this->CheckStampFile.c_str()))
  1486. {
  1487. return 0;
  1488. }
  1489. if ( this->GetWorkingMode() == NORMAL_MODE )
  1490. {
  1491. // load the cache
  1492. if(this->LoadCache() < 0)
  1493. {
  1494. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1495. return -1;
  1496. }
  1497. }
  1498. else
  1499. {
  1500. this->AddCMakePaths();
  1501. }
  1502. // Add any cache args
  1503. if ( !this->SetCacheArgs(args) )
  1504. {
  1505. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1506. return -1;
  1507. }
  1508. // In script mode we terminate after running the script.
  1509. if(this->GetWorkingMode() != NORMAL_MODE)
  1510. {
  1511. if(cmSystemTools::GetErrorOccuredFlag())
  1512. {
  1513. return -1;
  1514. }
  1515. else
  1516. {
  1517. return 0;
  1518. }
  1519. }
  1520. // If MAKEFLAGS are given in the environment, remove the environment
  1521. // variable. This will prevent try-compile from succeeding when it
  1522. // should fail (if "-i" is an option). We cannot simply test
  1523. // whether "-i" is given and remove it because some make programs
  1524. // encode the MAKEFLAGS variable in a strange way.
  1525. if(getenv("MAKEFLAGS"))
  1526. {
  1527. cmSystemTools::PutEnv("MAKEFLAGS=");
  1528. }
  1529. this->PreLoadCMakeFiles();
  1530. if ( noconfigure )
  1531. {
  1532. return 0;
  1533. }
  1534. // now run the global generate
  1535. // Check the state of the build system to see if we need to regenerate.
  1536. if(!this->CheckBuildSystem())
  1537. {
  1538. return 0;
  1539. }
  1540. // If we are doing global generate, we better set start and start
  1541. // output directory to the root of the project.
  1542. std::string oldstartdir = this->GetStartDirectory();
  1543. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1544. this->SetStartDirectory(this->GetHomeDirectory());
  1545. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1546. int ret = this->Configure();
  1547. if (ret || this->GetWorkingMode() != NORMAL_MODE)
  1548. {
  1549. #if defined(CMAKE_HAVE_VS_GENERATORS)
  1550. if(!this->VSSolutionFile.empty() && this->GlobalGenerator)
  1551. {
  1552. // CMake is running to regenerate a Visual Studio build tree
  1553. // during a build from the VS IDE. The build files cannot be
  1554. // regenerated, so we should stop the build.
  1555. cmSystemTools::Message(
  1556. "CMake Configure step failed. "
  1557. "Build files cannot be regenerated correctly. "
  1558. "Attempting to stop IDE build.");
  1559. cmGlobalVisualStudioGenerator* gg =
  1560. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  1561. gg->CallVisualStudioMacro(cmGlobalVisualStudioGenerator::MacroStop,
  1562. this->VSSolutionFile.c_str());
  1563. }
  1564. #endif
  1565. return ret;
  1566. }
  1567. ret = this->Generate();
  1568. std::string message = "Build files have been written to: ";
  1569. message += this->GetHomeOutputDirectory();
  1570. this->UpdateProgress(message.c_str(), -1);
  1571. if(ret)
  1572. {
  1573. return ret;
  1574. }
  1575. this->SetStartDirectory(oldstartdir.c_str());
  1576. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1577. return ret;
  1578. }
  1579. int cmake::Generate()
  1580. {
  1581. if(!this->GlobalGenerator)
  1582. {
  1583. return -1;
  1584. }
  1585. this->GlobalGenerator->Generate();
  1586. if ( !this->GraphVizFile.empty() )
  1587. {
  1588. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1589. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1590. }
  1591. if(this->WarnUnusedCli)
  1592. {
  1593. this->RunCheckForUnusedVariables();
  1594. }
  1595. if(cmSystemTools::GetErrorOccuredFlag())
  1596. {
  1597. return -1;
  1598. }
  1599. // Save the cache again after a successful Generate so that any internal
  1600. // variables created during Generate are saved. (Specifically target GUIDs
  1601. // for the Visual Studio and Xcode generators.)
  1602. if ( this->GetWorkingMode() == NORMAL_MODE )
  1603. {
  1604. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1605. }
  1606. return 0;
  1607. }
  1608. void cmake::AddCacheEntry(const char* key, const char* value,
  1609. const char* helpString,
  1610. int type)
  1611. {
  1612. this->CacheManager->AddCacheEntry(key, value,
  1613. helpString,
  1614. cmCacheManager::CacheEntryType(type));
  1615. }
  1616. const char* cmake::GetCacheDefinition(const char* name) const
  1617. {
  1618. return this->CacheManager->GetCacheValue(name);
  1619. }
  1620. void cmake::AddDefaultCommands()
  1621. {
  1622. std::list<cmCommand*> commands;
  1623. GetBootstrapCommands1(commands);
  1624. GetBootstrapCommands2(commands);
  1625. GetPredefinedCommands(commands);
  1626. for(std::list<cmCommand*>::iterator i = commands.begin();
  1627. i != commands.end(); ++i)
  1628. {
  1629. this->AddCommand(*i);
  1630. }
  1631. }
  1632. void cmake::AddDefaultGenerators()
  1633. {
  1634. #if defined(_WIN32) && !defined(__CYGWIN__)
  1635. # if !defined(CMAKE_BOOT_MINGW)
  1636. this->Generators.push_back(
  1637. cmGlobalVisualStudio6Generator::NewFactory());
  1638. this->Generators.push_back(
  1639. cmGlobalVisualStudio7Generator::NewFactory());
  1640. this->Generators.push_back(
  1641. cmGlobalVisualStudio10Generator::NewFactory());
  1642. this->Generators.push_back(
  1643. cmGlobalVisualStudio11Generator::NewFactory());
  1644. this->Generators.push_back(
  1645. cmGlobalVisualStudio12Generator::NewFactory());
  1646. this->Generators.push_back(
  1647. cmGlobalVisualStudio71Generator::NewFactory());
  1648. this->Generators.push_back(
  1649. cmGlobalVisualStudio8Generator::NewFactory());
  1650. this->Generators.push_back(
  1651. cmGlobalVisualStudio9Generator::NewFactory());
  1652. this->Generators.push_back(
  1653. cmGlobalBorlandMakefileGenerator::NewFactory());
  1654. this->Generators.push_back(
  1655. cmGlobalNMakeMakefileGenerator::NewFactory());
  1656. this->Generators.push_back(
  1657. cmGlobalJOMMakefileGenerator::NewFactory());
  1658. this->Generators.push_back(
  1659. cmGlobalWatcomWMakeGenerator::NewFactory());
  1660. # endif
  1661. this->Generators.push_back(
  1662. cmGlobalMSYSMakefileGenerator::NewFactory());
  1663. this->Generators.push_back(
  1664. cmGlobalMinGWMakefileGenerator::NewFactory());
  1665. #endif
  1666. this->Generators.push_back(
  1667. cmGlobalUnixMakefileGenerator3::NewFactory());
  1668. this->Generators.push_back(
  1669. cmGlobalNinjaGenerator::NewFactory());
  1670. #ifdef CMAKE_USE_XCODE
  1671. this->Generators.push_back(
  1672. cmGlobalXCodeGenerator::NewFactory());
  1673. #endif
  1674. }
  1675. int cmake::LoadCache()
  1676. {
  1677. // could we not read the cache
  1678. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  1679. {
  1680. // if it does exist, but isn't readable then warn the user
  1681. std::string cacheFile = this->GetHomeOutputDirectory();
  1682. cacheFile += "/CMakeCache.txt";
  1683. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1684. {
  1685. cmSystemTools::Error(
  1686. "There is a CMakeCache.txt file for the current binary tree but "
  1687. "cmake does not have permission to read it. Please check the "
  1688. "permissions of the directory you are trying to run CMake on.");
  1689. return -1;
  1690. }
  1691. }
  1692. // setup CMAKE_ROOT and CMAKE_COMMAND
  1693. if(!this->AddCMakePaths())
  1694. {
  1695. return -3;
  1696. }
  1697. return 0;
  1698. }
  1699. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  1700. {
  1701. this->ProgressCallback = f;
  1702. this->ProgressCallbackClientData = cd;
  1703. }
  1704. void cmake::UpdateProgress(const char *msg, float prog)
  1705. {
  1706. if(this->ProgressCallback && !this->InTryCompile)
  1707. {
  1708. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  1709. return;
  1710. }
  1711. }
  1712. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1713. {
  1714. for(RegisteredGeneratorsVector::const_iterator i =
  1715. this->Generators.begin(); i != this->Generators.end(); ++i)
  1716. {
  1717. cmDocumentationEntry e;
  1718. (*i)->GetDocumentation(e);
  1719. v.push_back(e);
  1720. }
  1721. for(RegisteredExtraGeneratorsMap::const_iterator i =
  1722. this->ExtraGenerators.begin(); i != this->ExtraGenerators.end(); ++i)
  1723. {
  1724. cmDocumentationEntry e;
  1725. cmExternalMakefileProjectGenerator* generator = (i->second)();
  1726. generator->GetDocumentation(e, i->first.c_str());
  1727. e.Name = i->first;
  1728. delete generator;
  1729. v.push_back(e);
  1730. }
  1731. }
  1732. void cmake::UpdateConversionPathTable()
  1733. {
  1734. // Update the path conversion table with any specified file:
  1735. const char* tablepath =
  1736. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1737. if(tablepath)
  1738. {
  1739. cmsys::ifstream table( tablepath );
  1740. if(!table)
  1741. {
  1742. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  1743. ". CMake can not open file.");
  1744. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1745. }
  1746. else
  1747. {
  1748. std::string a, b;
  1749. while(!table.eof())
  1750. {
  1751. // two entries per line
  1752. table >> a; table >> b;
  1753. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1754. }
  1755. }
  1756. }
  1757. }
  1758. //----------------------------------------------------------------------------
  1759. int cmake::CheckBuildSystem()
  1760. {
  1761. // We do not need to rerun CMake. Check dependency integrity. Use
  1762. // the make system's VERBOSE environment variable to enable verbose
  1763. // output. This can be skipped by setting CMAKE_NO_VERBOSE (which is set
  1764. // by the Eclipse and KDevelop generators).
  1765. bool verbose = ((cmSystemTools::GetEnv("VERBOSE") != 0)
  1766. && (cmSystemTools::GetEnv("CMAKE_NO_VERBOSE") == 0));
  1767. // This method will check the integrity of the build system if the
  1768. // option was given on the command line. It reads the given file to
  1769. // determine whether CMake should rerun.
  1770. // If no file is provided for the check, we have to rerun.
  1771. if(this->CheckBuildSystemArgument.size() == 0)
  1772. {
  1773. if(verbose)
  1774. {
  1775. cmOStringStream msg;
  1776. msg << "Re-run cmake no build system arguments\n";
  1777. cmSystemTools::Stdout(msg.str().c_str());
  1778. }
  1779. return 1;
  1780. }
  1781. // If the file provided does not exist, we have to rerun.
  1782. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  1783. {
  1784. if(verbose)
  1785. {
  1786. cmOStringStream msg;
  1787. msg << "Re-run cmake missing file: "
  1788. << this->CheckBuildSystemArgument.c_str() << "\n";
  1789. cmSystemTools::Stdout(msg.str().c_str());
  1790. }
  1791. return 1;
  1792. }
  1793. // Read the rerun check file and use it to decide whether to do the
  1794. // global generate.
  1795. cmake cm;
  1796. cmGlobalGenerator gg;
  1797. gg.SetCMakeInstance(&cm);
  1798. cmsys::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1799. cmMakefile* mf = lg->GetMakefile();
  1800. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  1801. cmSystemTools::GetErrorOccuredFlag())
  1802. {
  1803. if(verbose)
  1804. {
  1805. cmOStringStream msg;
  1806. msg << "Re-run cmake error reading : "
  1807. << this->CheckBuildSystemArgument.c_str() << "\n";
  1808. cmSystemTools::Stdout(msg.str().c_str());
  1809. }
  1810. // There was an error reading the file. Just rerun.
  1811. return 1;
  1812. }
  1813. if(this->ClearBuildSystem)
  1814. {
  1815. // Get the generator used for this build system.
  1816. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1817. if(!genName || genName[0] == '\0')
  1818. {
  1819. genName = "Unix Makefiles";
  1820. }
  1821. // Create the generator and use it to clear the dependencies.
  1822. cmsys::auto_ptr<cmGlobalGenerator>
  1823. ggd(this->CreateGlobalGenerator(genName));
  1824. if(ggd.get())
  1825. {
  1826. cmsys::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1827. lgd->ClearDependencies(mf, verbose);
  1828. }
  1829. }
  1830. // If any byproduct of makefile generation is missing we must re-run.
  1831. std::vector<std::string> products;
  1832. if(const char* productStr = mf->GetDefinition("CMAKE_MAKEFILE_PRODUCTS"))
  1833. {
  1834. cmSystemTools::ExpandListArgument(productStr, products);
  1835. }
  1836. for(std::vector<std::string>::const_iterator pi = products.begin();
  1837. pi != products.end(); ++pi)
  1838. {
  1839. if(!(cmSystemTools::FileExists(pi->c_str()) ||
  1840. cmSystemTools::FileIsSymlink(pi->c_str())))
  1841. {
  1842. if(verbose)
  1843. {
  1844. cmOStringStream msg;
  1845. msg << "Re-run cmake, missing byproduct: " << *pi << "\n";
  1846. cmSystemTools::Stdout(msg.str().c_str());
  1847. }
  1848. return 1;
  1849. }
  1850. }
  1851. // Get the set of dependencies and outputs.
  1852. std::vector<std::string> depends;
  1853. std::vector<std::string> outputs;
  1854. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1855. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1856. if(dependsStr && outputsStr)
  1857. {
  1858. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1859. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1860. }
  1861. if(depends.empty() || outputs.empty())
  1862. {
  1863. // Not enough information was provided to do the test. Just rerun.
  1864. if(verbose)
  1865. {
  1866. cmOStringStream msg;
  1867. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  1868. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  1869. cmSystemTools::Stdout(msg.str().c_str());
  1870. }
  1871. return 1;
  1872. }
  1873. // Find the newest dependency.
  1874. std::vector<std::string>::iterator dep = depends.begin();
  1875. std::string dep_newest = *dep++;
  1876. for(;dep != depends.end(); ++dep)
  1877. {
  1878. int result = 0;
  1879. if(this->FileComparison->FileTimeCompare(dep_newest.c_str(),
  1880. dep->c_str(), &result))
  1881. {
  1882. if(result < 0)
  1883. {
  1884. dep_newest = *dep;
  1885. }
  1886. }
  1887. else
  1888. {
  1889. if(verbose)
  1890. {
  1891. cmOStringStream msg;
  1892. msg << "Re-run cmake: build system dependency is missing\n";
  1893. cmSystemTools::Stdout(msg.str().c_str());
  1894. }
  1895. return 1;
  1896. }
  1897. }
  1898. // Find the oldest output.
  1899. std::vector<std::string>::iterator out = outputs.begin();
  1900. std::string out_oldest = *out++;
  1901. for(;out != outputs.end(); ++out)
  1902. {
  1903. int result = 0;
  1904. if(this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  1905. out->c_str(), &result))
  1906. {
  1907. if(result > 0)
  1908. {
  1909. out_oldest = *out;
  1910. }
  1911. }
  1912. else
  1913. {
  1914. if(verbose)
  1915. {
  1916. cmOStringStream msg;
  1917. msg << "Re-run cmake: build system output is missing\n";
  1918. cmSystemTools::Stdout(msg.str().c_str());
  1919. }
  1920. return 1;
  1921. }
  1922. }
  1923. // If any output is older than any dependency then rerun.
  1924. {
  1925. int result = 0;
  1926. if(!this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  1927. dep_newest.c_str(),
  1928. &result) ||
  1929. result < 0)
  1930. {
  1931. if(verbose)
  1932. {
  1933. cmOStringStream msg;
  1934. msg << "Re-run cmake file: " << out_oldest.c_str()
  1935. << " older than: " << dep_newest.c_str() << "\n";
  1936. cmSystemTools::Stdout(msg.str().c_str());
  1937. }
  1938. return 1;
  1939. }
  1940. }
  1941. // No need to rerun.
  1942. return 0;
  1943. }
  1944. //----------------------------------------------------------------------------
  1945. void cmake::TruncateOutputLog(const char* fname)
  1946. {
  1947. std::string fullPath = this->GetHomeOutputDirectory();
  1948. fullPath += "/";
  1949. fullPath += fname;
  1950. struct stat st;
  1951. if ( ::stat(fullPath.c_str(), &st) )
  1952. {
  1953. return;
  1954. }
  1955. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  1956. {
  1957. cmSystemTools::RemoveFile(fullPath.c_str());
  1958. return;
  1959. }
  1960. off_t fsize = st.st_size;
  1961. const off_t maxFileSize = 50 * 1024;
  1962. if ( fsize < maxFileSize )
  1963. {
  1964. //TODO: truncate file
  1965. return;
  1966. }
  1967. }
  1968. inline std::string removeQuotes(const std::string& s)
  1969. {
  1970. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1971. {
  1972. return s.substr(1, s.size()-2);
  1973. }
  1974. return s;
  1975. }
  1976. void cmake::MarkCliAsUsed(const std::string& variable)
  1977. {
  1978. this->UsedCliVariables[variable] = true;
  1979. }
  1980. void cmake::GenerateGraphViz(const char* fileName) const
  1981. {
  1982. #ifdef CMAKE_BUILD_WITH_CMAKE
  1983. cmsys::auto_ptr<cmGraphVizWriter> gvWriter(
  1984. new cmGraphVizWriter(this->GetGlobalGenerator()->GetLocalGenerators()));
  1985. std::string settingsFile = this->GetHomeOutputDirectory();
  1986. settingsFile += "/CMakeGraphVizOptions.cmake";
  1987. std::string fallbackSettingsFile = this->GetHomeDirectory();
  1988. fallbackSettingsFile += "/CMakeGraphVizOptions.cmake";
  1989. gvWriter->ReadSettings(settingsFile.c_str(), fallbackSettingsFile.c_str());
  1990. gvWriter->WritePerTargetFiles(fileName);
  1991. gvWriter->WriteTargetDependersFiles(fileName);
  1992. gvWriter->WriteGlobalFile(fileName);
  1993. #endif
  1994. }
  1995. void cmake::DefineProperty(const char *name, cmProperty::ScopeType scope,
  1996. const char *ShortDescription,
  1997. const char *FullDescription,
  1998. bool chained)
  1999. {
  2000. this->PropertyDefinitions[scope].DefineProperty(name,scope,ShortDescription,
  2001. FullDescription,
  2002. chained);
  2003. }
  2004. cmPropertyDefinition *cmake
  2005. ::GetPropertyDefinition(const char *name,
  2006. cmProperty::ScopeType scope)
  2007. {
  2008. if (this->IsPropertyDefined(name,scope))
  2009. {
  2010. return &(this->PropertyDefinitions[scope][name]);
  2011. }
  2012. return 0;
  2013. }
  2014. bool cmake::IsPropertyDefined(const char *name, cmProperty::ScopeType scope)
  2015. {
  2016. return this->PropertyDefinitions[scope].IsPropertyDefined(name);
  2017. }
  2018. bool cmake::IsPropertyChained(const char *name, cmProperty::ScopeType scope)
  2019. {
  2020. return this->PropertyDefinitions[scope].IsPropertyChained(name);
  2021. }
  2022. void cmake::SetProperty(const char* prop, const char* value)
  2023. {
  2024. if (!prop)
  2025. {
  2026. return;
  2027. }
  2028. // Special hook to invalidate cached value.
  2029. if(strcmp(prop, "DEBUG_CONFIGURATIONS") == 0)
  2030. {
  2031. this->DebugConfigs.clear();
  2032. }
  2033. this->Properties.SetProperty(prop, value, cmProperty::GLOBAL);
  2034. }
  2035. void cmake::AppendProperty(const char* prop, const char* value, bool asString)
  2036. {
  2037. if (!prop)
  2038. {
  2039. return;
  2040. }
  2041. // Special hook to invalidate cached value.
  2042. if(strcmp(prop, "DEBUG_CONFIGURATIONS") == 0)
  2043. {
  2044. this->DebugConfigs.clear();
  2045. }
  2046. this->Properties.AppendProperty(prop, value, cmProperty::GLOBAL, asString);
  2047. }
  2048. const char *cmake::GetProperty(const char* prop)
  2049. {
  2050. return this->GetProperty(prop, cmProperty::GLOBAL);
  2051. }
  2052. const char *cmake::GetProperty(const char* prop, cmProperty::ScopeType scope)
  2053. {
  2054. if(!prop)
  2055. {
  2056. return 0;
  2057. }
  2058. bool chain = false;
  2059. // watch for special properties
  2060. std::string propname = prop;
  2061. std::string output = "";
  2062. if ( propname == "CACHE_VARIABLES" )
  2063. {
  2064. cmCacheManager::CacheIterator cit =
  2065. this->GetCacheManager()->GetCacheIterator();
  2066. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  2067. {
  2068. if ( output.size() )
  2069. {
  2070. output += ";";
  2071. }
  2072. output += cit.GetName();
  2073. }
  2074. this->SetProperty("CACHE_VARIABLES", output.c_str());
  2075. }
  2076. else if ( propname == "COMMANDS" )
  2077. {
  2078. cmake::RegisteredCommandsMap::iterator cmds
  2079. = this->GetCommands()->begin();
  2080. for (unsigned int cc=0 ; cmds != this->GetCommands()->end(); ++ cmds )
  2081. {
  2082. if ( cc > 0 )
  2083. {
  2084. output += ";";
  2085. }
  2086. output += cmds->first.c_str();
  2087. cc++;
  2088. }
  2089. this->SetProperty("COMMANDS",output.c_str());
  2090. }
  2091. else if ( propname == "IN_TRY_COMPILE" )
  2092. {
  2093. this->SetProperty("IN_TRY_COMPILE",
  2094. this->GetIsInTryCompile()? "1":"0");
  2095. }
  2096. else if ( propname == "ENABLED_LANGUAGES" )
  2097. {
  2098. std::string lang;
  2099. if(this->GlobalGenerator)
  2100. {
  2101. std::vector<std::string> enLangs;
  2102. this->GlobalGenerator->GetEnabledLanguages(enLangs);
  2103. const char* sep = "";
  2104. for(std::vector<std::string>::iterator i = enLangs.begin();
  2105. i != enLangs.end(); ++i)
  2106. {
  2107. lang += sep;
  2108. sep = ";";
  2109. lang += *i;
  2110. }
  2111. }
  2112. this->SetProperty("ENABLED_LANGUAGES", lang.c_str());
  2113. }
  2114. return this->Properties.GetPropertyValue(prop, scope, chain);
  2115. }
  2116. bool cmake::GetPropertyAsBool(const char* prop)
  2117. {
  2118. return cmSystemTools::IsOn(this->GetProperty(prop));
  2119. }
  2120. int cmake::GetSystemInformation(std::vector<std::string>& args)
  2121. {
  2122. // so create the directory
  2123. std::string resultFile;
  2124. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2125. std::string destPath = cwd + "/__cmake_systeminformation";
  2126. cmSystemTools::RemoveADirectory(destPath.c_str());
  2127. if (!cmSystemTools::MakeDirectory(destPath.c_str()))
  2128. {
  2129. std::cerr << "Error: --system-information must be run from a "
  2130. "writable directory!\n";
  2131. return 1;
  2132. }
  2133. // process the arguments
  2134. bool writeToStdout = true;
  2135. for(unsigned int i=1; i < args.size(); ++i)
  2136. {
  2137. std::string arg = args[i];
  2138. if(arg.find("-V",0) == 0)
  2139. {
  2140. this->Verbose = true;
  2141. }
  2142. else if(arg.find("-G",0) == 0)
  2143. {
  2144. std::string value = arg.substr(2);
  2145. if(value.size() == 0)
  2146. {
  2147. ++i;
  2148. if(i >= args.size())
  2149. {
  2150. cmSystemTools::Error("No generator specified for -G");
  2151. return -1;
  2152. }
  2153. value = args[i];
  2154. }
  2155. cmGlobalGenerator* gen =
  2156. this->CreateGlobalGenerator(value.c_str());
  2157. if(!gen)
  2158. {
  2159. cmSystemTools::Error("Could not create named generator ",
  2160. value.c_str());
  2161. }
  2162. else
  2163. {
  2164. this->SetGlobalGenerator(gen);
  2165. }
  2166. }
  2167. // no option assume it is the output file
  2168. else
  2169. {
  2170. if (!cmSystemTools::FileIsFullPath(arg.c_str()))
  2171. {
  2172. resultFile = cwd;
  2173. resultFile += "/";
  2174. }
  2175. resultFile += arg;
  2176. writeToStdout = false;
  2177. }
  2178. }
  2179. // we have to find the module directory, so we can copy the files
  2180. this->AddCMakePaths();
  2181. std::string modulesPath =
  2182. this->CacheManager->GetCacheValue("CMAKE_ROOT");
  2183. modulesPath += "/Modules";
  2184. std::string inFile = modulesPath;
  2185. inFile += "/SystemInformation.cmake";
  2186. std::string outFile = destPath;
  2187. outFile += "/CMakeLists.txt";
  2188. // Copy file
  2189. if(!cmSystemTools::cmCopyFile(inFile.c_str(), outFile.c_str()))
  2190. {
  2191. std::cerr << "Error copying file \"" << inFile.c_str()
  2192. << "\" to \"" << outFile.c_str() << "\".\n";
  2193. return 1;
  2194. }
  2195. // do we write to a file or to stdout?
  2196. if (resultFile.size() == 0)
  2197. {
  2198. resultFile = cwd;
  2199. resultFile += "/__cmake_systeminformation/results.txt";
  2200. }
  2201. // now run cmake on the CMakeLists file
  2202. cmSystemTools::ChangeDirectory(destPath.c_str());
  2203. std::vector<std::string> args2;
  2204. args2.push_back(args[0]);
  2205. args2.push_back(destPath);
  2206. std::string resultArg = "-DRESULT_FILE=";
  2207. resultArg += resultFile;
  2208. args2.push_back(resultArg);
  2209. int res = this->Run(args2, false);
  2210. if (res != 0)
  2211. {
  2212. std::cerr << "Error: --system-information failed on internal CMake!\n";
  2213. return res;
  2214. }
  2215. // change back to the original directory
  2216. cmSystemTools::ChangeDirectory(cwd.c_str());
  2217. // echo results to stdout if needed
  2218. if (writeToStdout)
  2219. {
  2220. FILE* fin = cmsys::SystemTools::Fopen(resultFile.c_str(), "r");
  2221. if(fin)
  2222. {
  2223. const int bufferSize = 4096;
  2224. char buffer[bufferSize];
  2225. size_t n;
  2226. while((n = fread(buffer, 1, bufferSize, fin)) > 0)
  2227. {
  2228. for(char* c = buffer; c < buffer+n; ++c)
  2229. {
  2230. putc(*c, stdout);
  2231. }
  2232. fflush(stdout);
  2233. }
  2234. fclose(fin);
  2235. }
  2236. }
  2237. // clean up the directory
  2238. cmSystemTools::RemoveADirectory(destPath.c_str());
  2239. return 0;
  2240. }
  2241. //----------------------------------------------------------------------------
  2242. static bool cmakeCheckStampFile(const char* stampName)
  2243. {
  2244. // The stamp file does not exist. Use the stamp dependencies to
  2245. // determine whether it is really out of date. This works in
  2246. // conjunction with cmLocalVisualStudio7Generator to avoid
  2247. // repeatedly re-running CMake when the user rebuilds the entire
  2248. // solution.
  2249. std::string stampDepends = stampName;
  2250. stampDepends += ".depend";
  2251. #if defined(_WIN32) || defined(__CYGWIN__)
  2252. cmsys::ifstream fin(stampDepends.c_str(), std::ios::in | std::ios::binary);
  2253. #else
  2254. cmsys::ifstream fin(stampDepends.c_str(), std::ios::in);
  2255. #endif
  2256. if(!fin)
  2257. {
  2258. // The stamp dependencies file cannot be read. Just assume the
  2259. // build system is really out of date.
  2260. std::cout << "CMake is re-running because " << stampName
  2261. << " dependency file is missing.\n";
  2262. return false;
  2263. }
  2264. // Compare the stamp dependencies against the dependency file itself.
  2265. cmFileTimeComparison ftc;
  2266. std::string dep;
  2267. while(cmSystemTools::GetLineFromStream(fin, dep))
  2268. {
  2269. int result;
  2270. if(dep.length() >= 1 && dep[0] != '#' &&
  2271. (!ftc.FileTimeCompare(stampDepends.c_str(), dep.c_str(), &result)
  2272. || result < 0))
  2273. {
  2274. // The stamp depends file is older than this dependency. The
  2275. // build system is really out of date.
  2276. std::cout << "CMake is re-running because " << stampName
  2277. << " is out-of-date.\n";
  2278. std::cout << " the file '" << dep << "'\n";
  2279. std::cout << " is newer than '" << stampDepends << "'\n";
  2280. std::cout << " result='" << result << "'\n";
  2281. return false;
  2282. }
  2283. }
  2284. // The build system is up to date. The stamp file has been removed
  2285. // by the VS IDE due to a "rebuild" request. Restore it atomically.
  2286. cmOStringStream stampTempStream;
  2287. stampTempStream << stampName << ".tmp" << cmSystemTools::RandomSeed();
  2288. std::string stampTempString = stampTempStream.str();
  2289. const char* stampTemp = stampTempString.c_str();
  2290. {
  2291. // TODO: Teach cmGeneratedFileStream to use a random temp file (with
  2292. // multiple tries in unlikely case of conflict) and use that here.
  2293. cmsys::ofstream stamp(stampTemp);
  2294. stamp << "# CMake generation timestamp file for this directory.\n";
  2295. }
  2296. if(cmSystemTools::RenameFile(stampTemp, stampName))
  2297. {
  2298. // Notify the user why CMake is not re-running. It is safe to
  2299. // just print to stdout here because this code is only reachable
  2300. // through an undocumented flag used by the VS generator.
  2301. std::cout << "CMake does not need to re-run because "
  2302. << stampName << " is up-to-date.\n";
  2303. return true;
  2304. }
  2305. else
  2306. {
  2307. cmSystemTools::RemoveFile(stampTemp);
  2308. cmSystemTools::Error("Cannot restore timestamp ", stampName);
  2309. return false;
  2310. }
  2311. }
  2312. //----------------------------------------------------------------------------
  2313. static bool cmakeCheckStampList(const char* stampList)
  2314. {
  2315. // If the stamp list does not exist CMake must rerun to generate it.
  2316. if(!cmSystemTools::FileExists(stampList))
  2317. {
  2318. std::cout << "CMake is re-running because generate.stamp.list "
  2319. << "is missing.\n";
  2320. return false;
  2321. }
  2322. cmsys::ifstream fin(stampList);
  2323. if(!fin)
  2324. {
  2325. std::cout << "CMake is re-running because generate.stamp.list "
  2326. << "could not be read.\n";
  2327. return false;
  2328. }
  2329. // Check each stamp.
  2330. std::string stampName;
  2331. while(cmSystemTools::GetLineFromStream(fin, stampName))
  2332. {
  2333. if(!cmakeCheckStampFile(stampName.c_str()))
  2334. {
  2335. return false;
  2336. }
  2337. }
  2338. return true;
  2339. }
  2340. //----------------------------------------------------------------------------
  2341. void cmake::IssueMessage(cmake::MessageType t, std::string const& text,
  2342. cmListFileBacktrace const& backtrace)
  2343. {
  2344. cmOStringStream msg;
  2345. bool isError = false;
  2346. // Construct the message header.
  2347. if(t == cmake::FATAL_ERROR)
  2348. {
  2349. isError = true;
  2350. msg << "CMake Error";
  2351. }
  2352. else if(t == cmake::INTERNAL_ERROR)
  2353. {
  2354. isError = true;
  2355. msg << "CMake Internal Error (please report a bug)";
  2356. }
  2357. else if(t == cmake::LOG)
  2358. {
  2359. msg << "CMake Debug Log";
  2360. }
  2361. else if(t == cmake::DEPRECATION_ERROR)
  2362. {
  2363. msg << "CMake Deprecation Error";
  2364. isError = true;
  2365. }
  2366. else if (t == cmake::DEPRECATION_WARNING)
  2367. {
  2368. msg << "CMake Deprecation Warning";
  2369. }
  2370. else
  2371. {
  2372. msg << "CMake Warning";
  2373. if(t == cmake::AUTHOR_WARNING)
  2374. {
  2375. // Allow suppression of these warnings.
  2376. cmCacheManager::CacheIterator it = this->CacheManager
  2377. ->GetCacheIterator("CMAKE_SUPPRESS_DEVELOPER_WARNINGS");
  2378. if(!it.IsAtEnd() && it.GetValueAsBool())
  2379. {
  2380. return;
  2381. }
  2382. msg << " (dev)";
  2383. }
  2384. }
  2385. // Add the immediate context.
  2386. cmListFileBacktrace::const_iterator i = backtrace.begin();
  2387. if(i != backtrace.end())
  2388. {
  2389. cmListFileContext const& lfc = *i;
  2390. msg << (lfc.Line? " at ": " in ") << lfc;
  2391. ++i;
  2392. }
  2393. // Add the message text.
  2394. {
  2395. msg << ":\n";
  2396. cmDocumentationFormatter formatter;
  2397. formatter.SetIndent(" ");
  2398. formatter.PrintFormatted(msg, text.c_str());
  2399. }
  2400. // Add the rest of the context.
  2401. if(i != backtrace.end())
  2402. {
  2403. msg << "Call Stack (most recent call first):\n";
  2404. while(i != backtrace.end())
  2405. {
  2406. cmListFileContext const& lfc = *i;
  2407. msg << " " << lfc << "\n";
  2408. ++i;
  2409. }
  2410. }
  2411. // Add a note about warning suppression.
  2412. if(t == cmake::AUTHOR_WARNING)
  2413. {
  2414. msg <<
  2415. "This warning is for project developers. Use -Wno-dev to suppress it.";
  2416. }
  2417. // Add a terminating blank line.
  2418. msg << "\n";
  2419. // Output the message.
  2420. if(isError)
  2421. {
  2422. cmSystemTools::SetErrorOccured();
  2423. cmSystemTools::Message(msg.str().c_str(), "Error");
  2424. }
  2425. else
  2426. {
  2427. cmSystemTools::Message(msg.str().c_str(), "Warning");
  2428. }
  2429. }
  2430. //----------------------------------------------------------------------------
  2431. std::vector<std::string> const& cmake::GetDebugConfigs()
  2432. {
  2433. // Compute on-demand.
  2434. if(this->DebugConfigs.empty())
  2435. {
  2436. if(const char* config_list = this->GetProperty("DEBUG_CONFIGURATIONS"))
  2437. {
  2438. // Expand the specified list and convert to upper-case.
  2439. cmSystemTools::ExpandListArgument(config_list, this->DebugConfigs);
  2440. for(std::vector<std::string>::iterator i = this->DebugConfigs.begin();
  2441. i != this->DebugConfigs.end(); ++i)
  2442. {
  2443. *i = cmSystemTools::UpperCase(*i);
  2444. }
  2445. }
  2446. // If no configurations were specified, use a default list.
  2447. if(this->DebugConfigs.empty())
  2448. {
  2449. this->DebugConfigs.push_back("DEBUG");
  2450. }
  2451. }
  2452. return this->DebugConfigs;
  2453. }
  2454. int cmake::Build(const std::string& dir,
  2455. const std::string& target,
  2456. const std::string& config,
  2457. const std::vector<std::string>& nativeOptions,
  2458. bool clean)
  2459. {
  2460. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  2461. {
  2462. std::cerr << "Error: " << dir << " is not a directory\n";
  2463. return 1;
  2464. }
  2465. std::string cachePath = dir;
  2466. cmSystemTools::ConvertToUnixSlashes(cachePath);
  2467. cmCacheManager* cachem = this->GetCacheManager();
  2468. cmCacheManager::CacheIterator it = cachem->NewIterator();
  2469. if(!cachem->LoadCache(cachePath.c_str()))
  2470. {
  2471. std::cerr << "Error: could not load cache\n";
  2472. return 1;
  2473. }
  2474. if(!it.Find("CMAKE_GENERATOR"))
  2475. {
  2476. std::cerr << "Error: could not find CMAKE_GENERATOR in Cache\n";
  2477. return 1;
  2478. }
  2479. cmsys::auto_ptr<cmGlobalGenerator> gen(
  2480. this->CreateGlobalGenerator(it.GetValue()));
  2481. if(!gen.get())
  2482. {
  2483. std::cerr << "Error: could create CMAKE_GENERATOR \""
  2484. << it.GetValue() << "\"\n";
  2485. return 1;
  2486. }
  2487. std::string output;
  2488. std::string projName;
  2489. if(!it.Find("CMAKE_PROJECT_NAME"))
  2490. {
  2491. std::cerr << "Error: could not find CMAKE_PROJECT_NAME in Cache\n";
  2492. return 1;
  2493. }
  2494. projName = it.GetValue();
  2495. return gen->Build(0, dir.c_str(),
  2496. projName.c_str(), target.c_str(),
  2497. &output,
  2498. 0,
  2499. config.c_str(), clean, false, 0,
  2500. cmSystemTools::OUTPUT_PASSTHROUGH,
  2501. nativeOptions);
  2502. }
  2503. void cmake::WatchUnusedCli(const char* var)
  2504. {
  2505. #ifdef CMAKE_BUILD_WITH_CMAKE
  2506. this->VariableWatch->AddWatch(var, cmWarnUnusedCliWarning, this);
  2507. if(this->UsedCliVariables.find(var) == this->UsedCliVariables.end())
  2508. {
  2509. this->UsedCliVariables[var] = false;
  2510. }
  2511. #endif
  2512. }
  2513. void cmake::UnwatchUnusedCli(const char* var)
  2514. {
  2515. #ifdef CMAKE_BUILD_WITH_CMAKE
  2516. this->VariableWatch->RemoveWatch(var, cmWarnUnusedCliWarning);
  2517. this->UsedCliVariables.erase(var);
  2518. #endif
  2519. }
  2520. void cmake::RunCheckForUnusedVariables()
  2521. {
  2522. #ifdef CMAKE_BUILD_WITH_CMAKE
  2523. bool haveUnused = false;
  2524. cmOStringStream msg;
  2525. msg << "Manually-specified variables were not used by the project:";
  2526. for(std::map<cmStdString, bool>::const_iterator
  2527. it = this->UsedCliVariables.begin();
  2528. it != this->UsedCliVariables.end(); ++it)
  2529. {
  2530. if(!it->second)
  2531. {
  2532. haveUnused = true;
  2533. msg << "\n " << it->first;
  2534. }
  2535. }
  2536. if(haveUnused)
  2537. {
  2538. this->IssueMessage(cmake::WARNING, msg.str(), cmListFileBacktrace());
  2539. }
  2540. #endif
  2541. }