cmake.cxx 81 KB

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