cmake.cxx 82 KB

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