cmake.cxx 78 KB

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