cmake.cxx 80 KB

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