cmake.cxx 81 KB

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