cmake.cxx 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmake.h"
  14. #include "time.h"
  15. #include "cmCacheManager.h"
  16. #include "cmMakefile.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmCommands.h"
  19. #include "cmCommand.h"
  20. #include "cmFileTimeComparison.h"
  21. #include "cmGeneratedFileStream.h"
  22. #if defined(CMAKE_BUILD_WITH_CMAKE)
  23. # include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback.
  24. # include "cmVariableWatch.h"
  25. # include "cmVersion.h"
  26. # include <cmsys/Terminal.h>
  27. #endif
  28. # include <cmsys/Directory.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(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
  37. # define CMAKE_BOOT_MINGW
  38. #endif
  39. // include the generator
  40. #if defined(_WIN32) && !defined(__CYGWIN__)
  41. # if !defined(CMAKE_BOOT_MINGW)
  42. # include "cmGlobalVisualStudio6Generator.h"
  43. # include "cmGlobalVisualStudio7Generator.h"
  44. # include "cmGlobalVisualStudio71Generator.h"
  45. # include "cmGlobalVisualStudio8Generator.h"
  46. # include "cmGlobalVisualStudio8Win64Generator.h"
  47. # include "cmGlobalBorlandMakefileGenerator.h"
  48. # include "cmGlobalNMakeMakefileGenerator.h"
  49. # include "cmGlobalWatcomWMakeGenerator.h"
  50. # endif
  51. # include "cmGlobalMSYSMakefileGenerator.h"
  52. # include "cmGlobalMinGWMakefileGenerator.h"
  53. # include "cmWin32ProcessExecution.h"
  54. #else
  55. #endif
  56. #include "cmGlobalUnixMakefileGenerator3.h"
  57. #ifdef CMAKE_USE_KDEVELOP
  58. # include "cmGlobalKdevelopGenerator.h"
  59. #endif
  60. #include <stdlib.h> // required for atoi
  61. #if defined( __APPLE__ )
  62. # if defined(CMAKE_BUILD_WITH_CMAKE)
  63. # include "cmGlobalXCodeGenerator.h"
  64. # define CMAKE_USE_XCODE 1
  65. # endif
  66. # include <sys/types.h>
  67. # include <sys/time.h>
  68. # include <sys/resource.h>
  69. #endif
  70. #include <sys/stat.h> // struct stat
  71. #include <memory> // auto_ptr
  72. void cmNeedBackwardsCompatibility(const std::string& variable,
  73. int access_type, void* )
  74. {
  75. #ifdef CMAKE_BUILD_WITH_CMAKE
  76. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  77. {
  78. std::string message = "An attempt was made to access a variable: ";
  79. message += variable;
  80. message +=
  81. " that has not been defined. Some variables were always defined "
  82. "by CMake in versions prior to 1.6. To fix this you might need to set "
  83. "the cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If "
  84. "you are writing a CMakeList file, (or have already set "
  85. "CMAKE_BACKWARDS_COMPATABILITY to 1.4 or less) then you probably need "
  86. "to include a CMake module to test for the feature this variable "
  87. "defines.";
  88. cmSystemTools::Error(message.c_str());
  89. }
  90. #else
  91. (void)variable;
  92. (void)access_type;
  93. #endif
  94. }
  95. cmake::cmake()
  96. {
  97. this->DebugTryCompile = false;
  98. this->ClearBuildSystem = false;
  99. this->FileComparison = new cmFileTimeComparison;
  100. #ifdef __APPLE__
  101. struct rlimit rlp;
  102. if(!getrlimit(RLIMIT_STACK, &rlp))
  103. {
  104. if(rlp.rlim_cur != rlp.rlim_max)
  105. {
  106. rlp.rlim_cur = rlp.rlim_max;
  107. setrlimit(RLIMIT_STACK, &rlp);
  108. }
  109. }
  110. #endif
  111. // If MAKEFLAGS are given in the environment, remove the environment
  112. // variable. This will prevent try-compile from succeeding when it
  113. // should fail (if "-i" is an option). We cannot simply test
  114. // whether "-i" is given and remove it because some make programs
  115. // encode the MAKEFLAGS variable in a strange way.
  116. if(getenv("MAKEFLAGS"))
  117. {
  118. cmSystemTools::PutEnv("MAKEFLAGS=");
  119. }
  120. this->Verbose = false;
  121. this->InTryCompile = false;
  122. this->CacheManager = new cmCacheManager;
  123. this->GlobalGenerator = 0;
  124. this->ProgressCallback = 0;
  125. this->ProgressCallbackClientData = 0;
  126. this->ScriptMode = false;
  127. #ifdef CMAKE_BUILD_WITH_CMAKE
  128. this->VariableWatch = new cmVariableWatch;
  129. this->VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  130. cmNeedBackwardsCompatibility);
  131. this->VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  132. cmNeedBackwardsCompatibility);
  133. this->VariableWatch->AddWatch("CMAKE_X_LIBS",
  134. cmNeedBackwardsCompatibility);
  135. #endif
  136. this->AddDefaultGenerators();
  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. if (this->GlobalGenerator)
  145. {
  146. delete this->GlobalGenerator;
  147. this->GlobalGenerator = 0;
  148. }
  149. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  150. j != this->Commands.end(); ++j)
  151. {
  152. delete (*j).second;
  153. }
  154. #ifdef CMAKE_BUILD_WITH_CMAKE
  155. delete this->VariableWatch;
  156. #endif
  157. delete this->FileComparison;
  158. }
  159. void cmake::CleanupCommandsAndMacros()
  160. {
  161. std::vector<cmCommand*> commands;
  162. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  163. j != this->Commands.end(); ++j)
  164. {
  165. if ( !j->second->IsA("cmMacroHelperCommand") )
  166. {
  167. commands.push_back(j->second);
  168. }
  169. else
  170. {
  171. delete j->second;
  172. }
  173. }
  174. this->Commands.erase(this->Commands.begin(), this->Commands.end());
  175. std::vector<cmCommand*>::iterator it;
  176. for ( it = commands.begin(); it != commands.end();
  177. ++ it )
  178. {
  179. this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  180. }
  181. }
  182. bool cmake::CommandExists(const char* name) const
  183. {
  184. std::string sName = cmSystemTools::LowerCase(name);
  185. return (this->Commands.find(sName) != this->Commands.end());
  186. }
  187. cmCommand *cmake::GetCommand(const char *name)
  188. {
  189. cmCommand* rm = 0;
  190. std::string sName = cmSystemTools::LowerCase(name);
  191. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  192. if (pos != this->Commands.end())
  193. {
  194. rm = (*pos).second;
  195. }
  196. return rm;
  197. }
  198. void cmake::RenameCommand(const char*oldName, const char* newName)
  199. {
  200. // if the command already exists, free the old one
  201. std::string sOldName = cmSystemTools::LowerCase(oldName);
  202. std::string sNewName = cmSystemTools::LowerCase(newName);
  203. RegisteredCommandsMap::iterator pos = this->Commands.find(sOldName);
  204. if ( pos == this->Commands.end() )
  205. {
  206. return;
  207. }
  208. cmCommand* cmd = pos->second;
  209. pos = this->Commands.find(sNewName);
  210. if (pos != this->Commands.end())
  211. {
  212. delete pos->second;
  213. this->Commands.erase(pos);
  214. }
  215. this->Commands.insert(RegisteredCommandsMap::value_type(sNewName, cmd));
  216. pos = this->Commands.find(sOldName);
  217. this->Commands.erase(pos);
  218. }
  219. void cmake::AddCommand(cmCommand* wg)
  220. {
  221. std::string name = cmSystemTools::LowerCase(wg->GetName());
  222. // if the command already exists, free the old one
  223. RegisteredCommandsMap::iterator pos = this->Commands.find(name);
  224. if (pos != this->Commands.end())
  225. {
  226. delete pos->second;
  227. this->Commands.erase(pos);
  228. }
  229. this->Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  230. }
  231. // Parse the args
  232. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  233. {
  234. for(unsigned int i=1; i < args.size(); ++i)
  235. {
  236. std::string arg = args[i];
  237. if(arg.find("-D",0) == 0)
  238. {
  239. std::string entry = arg.substr(2);
  240. if(entry.size() == 0)
  241. {
  242. ++i;
  243. if(i < args.size())
  244. {
  245. entry = args[i];
  246. }
  247. else
  248. {
  249. cmSystemTools::Error("-D must be followed with VAR=VALUE.");
  250. return false;
  251. }
  252. }
  253. std::string var, value;
  254. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  255. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  256. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  257. {
  258. this->CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  259. "No help, variable specified on the command line.",
  260. type);
  261. }
  262. else
  263. {
  264. std::cerr << "Parse error in command line argument: " << arg << "\n"
  265. << "Should be: VAR:type=value\n";
  266. cmSystemTools::Error("No cmake scrpt provided.");
  267. return false;
  268. }
  269. }
  270. else if(arg.find("-C",0) == 0)
  271. {
  272. std::string path = arg.substr(2);
  273. if ( path.size() == 0 )
  274. {
  275. ++i;
  276. if(i < args.size())
  277. {
  278. path = args[i];
  279. }
  280. else
  281. {
  282. cmSystemTools::Error("-C must be followed by a file name.");
  283. return false;
  284. }
  285. }
  286. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  287. this->ReadListFile(path.c_str());
  288. }
  289. else if(arg.find("-P",0) == 0)
  290. {
  291. i++;
  292. if(i >= args.size())
  293. {
  294. cmSystemTools::Error("-P must be followed by a file name.");
  295. return false;
  296. }
  297. std::string path = args[i];
  298. if ( path.size() == 0 )
  299. {
  300. cmSystemTools::Error("No cmake script provided.");
  301. return false;
  302. }
  303. this->ReadListFile(path.c_str());
  304. }
  305. }
  306. return true;
  307. }
  308. void cmake::ReadListFile(const char *path)
  309. {
  310. // if a generator was not yet created, temporarily create one
  311. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  312. bool created = false;
  313. // if a generator was not specified use a generic one
  314. if (!gg)
  315. {
  316. gg = new cmGlobalGenerator;
  317. gg->SetCMakeInstance(this);
  318. created = true;
  319. }
  320. // read in the list file to fill the cache
  321. if(path)
  322. {
  323. std::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  324. lg->SetGlobalGenerator(gg);
  325. lg->GetMakefile()->SetHomeOutputDirectory
  326. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  327. lg->GetMakefile()->SetStartOutputDirectory
  328. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  329. lg->GetMakefile()->SetHomeDirectory
  330. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  331. lg->GetMakefile()->SetStartDirectory
  332. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  333. if (!lg->GetMakefile()->ReadListFile(0, path))
  334. {
  335. std::cerr << "Error processing file:" << path << "\n";
  336. }
  337. }
  338. // free generic one if generated
  339. if (created)
  340. {
  341. delete gg;
  342. }
  343. }
  344. // Parse the args
  345. void cmake::SetArgs(const std::vector<std::string>& args)
  346. {
  347. bool directoriesSet = false;
  348. for(unsigned int i=1; i < args.size(); ++i)
  349. {
  350. std::string arg = args[i];
  351. if(arg.find("-H",0) == 0)
  352. {
  353. directoriesSet = true;
  354. std::string path = arg.substr(2);
  355. path = cmSystemTools::CollapseFullPath(path.c_str());
  356. cmSystemTools::ConvertToUnixSlashes(path);
  357. this->SetHomeDirectory(path.c_str());
  358. }
  359. else if(arg.find("-S",0) == 0)
  360. {
  361. // There is no local generate anymore. Ignore -S option.
  362. }
  363. else if(arg.find("-O",0) == 0)
  364. {
  365. // There is no local generate anymore. Ignore -O option.
  366. }
  367. else if(arg.find("-B",0) == 0)
  368. {
  369. directoriesSet = true;
  370. std::string path = arg.substr(2);
  371. path = cmSystemTools::CollapseFullPath(path.c_str());
  372. cmSystemTools::ConvertToUnixSlashes(path);
  373. this->SetHomeOutputDirectory(path.c_str());
  374. }
  375. else if((i < args.size()-1) && (arg.find("--check-build-system",0) == 0))
  376. {
  377. this->CheckBuildSystemArgument = args[++i];
  378. this->ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  379. }
  380. else if(arg.find("-V",0) == 0)
  381. {
  382. this->Verbose = true;
  383. }
  384. else if(arg.find("-D",0) == 0)
  385. {
  386. // skip for now
  387. }
  388. else if(arg.find("-C",0) == 0)
  389. {
  390. // skip for now
  391. }
  392. else if(arg.find("-P",0) == 0)
  393. {
  394. // skip for now
  395. i++;
  396. }
  397. else if(arg.find("--graphviz=",0) == 0)
  398. {
  399. std::string path = arg.substr(strlen("--graphviz="));
  400. path = cmSystemTools::CollapseFullPath(path.c_str());
  401. cmSystemTools::ConvertToUnixSlashes(path);
  402. this->GraphVizFile = path;
  403. if ( this->GraphVizFile.empty() )
  404. {
  405. cmSystemTools::Error("No file specified for --graphviz");
  406. }
  407. }
  408. else if(arg.find("--debug-trycompile",0) == 0)
  409. {
  410. std::cout << "debug trycompile on\n";
  411. this->DebugTryCompileOn();
  412. }
  413. else if(arg.find("-G",0) == 0)
  414. {
  415. std::string value = arg.substr(2);
  416. if(value.size() == 0)
  417. {
  418. ++i;
  419. if(i >= args.size())
  420. {
  421. cmSystemTools::Error("No generator specified for -G");
  422. return;
  423. }
  424. value = args[i];
  425. }
  426. cmGlobalGenerator* gen =
  427. this->CreateGlobalGenerator(value.c_str());
  428. if(!gen)
  429. {
  430. cmSystemTools::Error("Could not create named generator ",
  431. value.c_str());
  432. }
  433. else
  434. {
  435. this->SetGlobalGenerator(gen);
  436. }
  437. }
  438. // no option assume it is the path to the source
  439. else
  440. {
  441. directoriesSet = true;
  442. this->SetDirectoriesFromFile(arg.c_str());
  443. }
  444. }
  445. if(!directoriesSet)
  446. {
  447. this->SetHomeOutputDirectory
  448. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  449. this->SetStartOutputDirectory
  450. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  451. this->SetHomeDirectory
  452. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  453. this->SetStartDirectory
  454. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  455. }
  456. this->SetStartDirectory(this->GetHomeDirectory());
  457. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  458. }
  459. //----------------------------------------------------------------------------
  460. void cmake::SetDirectoriesFromFile(const char* arg)
  461. {
  462. // Check if the argument refers to a CMakeCache.txt or
  463. // CMakeLists.txt file.
  464. std::string listPath;
  465. std::string cachePath;
  466. bool argIsFile = false;
  467. if(cmSystemTools::FileIsDirectory(arg))
  468. {
  469. std::string path = cmSystemTools::CollapseFullPath(arg);
  470. cmSystemTools::ConvertToUnixSlashes(path);
  471. std::string cacheFile = path;
  472. cacheFile += "/CMakeCache.txt";
  473. std::string listFile = path;
  474. listFile += "/CMakeLists.txt";
  475. if(cmSystemTools::FileExists(cacheFile.c_str()))
  476. {
  477. cachePath = path;
  478. }
  479. if(cmSystemTools::FileExists(listFile.c_str()))
  480. {
  481. listPath = path;
  482. }
  483. }
  484. else if(cmSystemTools::FileExists(arg))
  485. {
  486. argIsFile = true;
  487. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  488. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  489. name = cmSystemTools::LowerCase(name);
  490. if(name == "cmakecache.txt")
  491. {
  492. cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  493. }
  494. else if(name == "cmakelists.txt")
  495. {
  496. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  497. }
  498. }
  499. else
  500. {
  501. // Specified file or directory does not exist. Try to set things
  502. // up to produce a meaningful error message.
  503. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  504. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  505. name = cmSystemTools::LowerCase(name);
  506. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  507. {
  508. argIsFile = true;
  509. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  510. }
  511. else
  512. {
  513. listPath = fullPath;
  514. }
  515. }
  516. // If there is a CMakeCache.txt file, use its settings.
  517. if(cachePath.length() > 0)
  518. {
  519. cmCacheManager* cachem = this->GetCacheManager();
  520. cmCacheManager::CacheIterator it = cachem->NewIterator();
  521. if(cachem->LoadCache(cachePath.c_str()) &&
  522. it.Find("CMAKE_HOME_DIRECTORY"))
  523. {
  524. this->SetHomeOutputDirectory(cachePath.c_str());
  525. this->SetStartOutputDirectory(cachePath.c_str());
  526. this->SetHomeDirectory(it.GetValue());
  527. this->SetStartDirectory(it.GetValue());
  528. return;
  529. }
  530. }
  531. // If there is a CMakeLists.txt file, use it as the source tree.
  532. if(listPath.length() > 0)
  533. {
  534. this->SetHomeDirectory(listPath.c_str());
  535. this->SetStartDirectory(listPath.c_str());
  536. if(argIsFile)
  537. {
  538. // Source CMakeLists.txt file given. It was probably dropped
  539. // onto the executable in a GUI. Default to an in-source build.
  540. this->SetHomeOutputDirectory(listPath.c_str());
  541. this->SetStartOutputDirectory(listPath.c_str());
  542. }
  543. else
  544. {
  545. // Source directory given on command line. Use current working
  546. // directory as build tree.
  547. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  548. this->SetHomeOutputDirectory(cwd.c_str());
  549. this->SetStartOutputDirectory(cwd.c_str());
  550. }
  551. return;
  552. }
  553. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  554. // argument. Assume it is the path to the source tree, and use the
  555. // current working directory as the build tree.
  556. std::string full = cmSystemTools::CollapseFullPath(arg);
  557. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  558. this->SetHomeDirectory(full.c_str());
  559. this->SetStartDirectory(full.c_str());
  560. this->SetHomeOutputDirectory(cwd.c_str());
  561. this->SetStartOutputDirectory(cwd.c_str());
  562. }
  563. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
  564. // cache
  565. int cmake::AddCMakePaths(const char *arg0)
  566. {
  567. // Find our own executable.
  568. std::vector<cmStdString> failures;
  569. std::string cMakeSelf = arg0;
  570. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  571. failures.push_back(cMakeSelf);
  572. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  573. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  574. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  575. {
  576. #ifdef CMAKE_BUILD_DIR
  577. std::string intdir = ".";
  578. #ifdef CMAKE_INTDIR
  579. intdir = CMAKE_INTDIR;
  580. #endif
  581. cMakeSelf = CMAKE_BUILD_DIR;
  582. cMakeSelf += "/bin/";
  583. cMakeSelf += intdir;
  584. cMakeSelf += "/cmake";
  585. cMakeSelf += cmSystemTools::GetExecutableExtension();
  586. #endif
  587. }
  588. #ifdef CMAKE_PREFIX
  589. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  590. {
  591. failures.push_back(cMakeSelf);
  592. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  593. }
  594. #endif
  595. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  596. {
  597. failures.push_back(cMakeSelf);
  598. cmOStringStream msg;
  599. msg << "CMAKE can not find the command line program cmake.\n";
  600. msg << " argv[0] = \"" << arg0 << "\"\n";
  601. msg << " Attempted paths:\n";
  602. std::vector<cmStdString>::iterator i;
  603. for(i=failures.begin(); i != failures.end(); ++i)
  604. {
  605. msg << " \"" << i->c_str() << "\"\n";
  606. }
  607. cmSystemTools::Error(msg.str().c_str());
  608. return 0;
  609. }
  610. // Save the value in the cache
  611. this->CacheManager->AddCacheEntry
  612. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  613. cmCacheManager::INTERNAL);
  614. // Find and save the command to edit the cache
  615. std::string editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  616. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  617. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  618. {
  619. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  620. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  621. }
  622. std::string ctestCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  623. "/ctest" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  624. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  625. {
  626. this->CacheManager->AddCacheEntry
  627. ("CMAKE_CTEST_COMMAND", ctestCommand.c_str(),
  628. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  629. }
  630. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  631. {
  632. this->CacheManager->AddCacheEntry
  633. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  634. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  635. }
  636. // do CMAKE_ROOT, look for the environment variable first
  637. std::string cMakeRoot;
  638. std::string modules;
  639. if (getenv("CMAKE_ROOT"))
  640. {
  641. cMakeRoot = getenv("CMAKE_ROOT");
  642. modules = cMakeRoot + "/Modules/CMake.cmake";
  643. }
  644. if(!cmSystemTools::FileExists(modules.c_str()))
  645. {
  646. // next try exe/..
  647. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  648. std::string::size_type slashPos = cMakeRoot.rfind("/");
  649. if(slashPos != std::string::npos)
  650. {
  651. cMakeRoot = cMakeRoot.substr(0, slashPos);
  652. }
  653. // is there no Modules direcory there?
  654. modules = cMakeRoot + "/Modules/CMake.cmake";
  655. }
  656. if (!cmSystemTools::FileExists(modules.c_str()))
  657. {
  658. // try exe/../share/cmake
  659. cMakeRoot += CMAKE_DATA_DIR;
  660. modules = cMakeRoot + "/Modules/CMake.cmake";
  661. }
  662. #ifdef CMAKE_ROOT_DIR
  663. if (!cmSystemTools::FileExists(modules.c_str()))
  664. {
  665. // try compiled in root directory
  666. cMakeRoot = CMAKE_ROOT_DIR;
  667. modules = cMakeRoot + "/Modules/CMake.cmake";
  668. }
  669. #endif
  670. #ifdef CMAKE_PREFIX
  671. if (!cmSystemTools::FileExists(modules.c_str()))
  672. {
  673. // try compiled in install prefix
  674. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  675. modules = cMakeRoot + "/Modules/CMake.cmake";
  676. }
  677. #endif
  678. if (!cmSystemTools::FileExists(modules.c_str()))
  679. {
  680. // try
  681. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  682. cMakeRoot += CMAKE_DATA_DIR;
  683. modules = cMakeRoot + "/Modules/CMake.cmake";
  684. }
  685. if(!cmSystemTools::FileExists(modules.c_str()))
  686. {
  687. // next try exe
  688. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  689. // is there no Modules direcory there?
  690. modules = cMakeRoot + "/Modules/CMake.cmake";
  691. }
  692. if (!cmSystemTools::FileExists(modules.c_str()))
  693. {
  694. // couldn't find modules
  695. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  696. "CMake has most likely not been installed correctly.\n"
  697. "Modules directory not found in\n",
  698. cMakeRoot.c_str());
  699. return 0;
  700. }
  701. this->CacheManager->AddCacheEntry
  702. ("CMAKE_ROOT", cMakeRoot.c_str(),
  703. "Path to CMake installation.", cmCacheManager::INTERNAL);
  704. #ifdef _WIN32
  705. std::string comspec = "cmw9xcom.exe";
  706. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  707. #endif
  708. return 1;
  709. }
  710. void CMakeCommandUsage(const char* program)
  711. {
  712. cmOStringStream errorStream;
  713. #ifdef CMAKE_BUILD_WITH_CMAKE
  714. errorStream
  715. << "cmake version " << cmVersion::GetCMakeVersion() << "\n";
  716. #else
  717. errorStream
  718. << "cmake bootstrap\n";
  719. #endif
  720. errorStream
  721. << "Usage: " << program << " -E [command] [arguments ...]\n"
  722. << "Available commands: \n"
  723. << " chdir dir cmd [args]... - run command in a given directory\n"
  724. << " copy file destination - copy file to destination (either file or "
  725. "directory)\n"
  726. << " copy_if_different in-file out-file - copy file if input has "
  727. "changed\n"
  728. << " copy_directory source destination - copy directory 'source' "
  729. "content to directory 'destination'\n"
  730. << " compare_files file1 file2 - check if file1 is same as file2\n"
  731. << " echo [string]... - displays arguments as text\n"
  732. << " echo_append [string]... - displays arguments as text but no new"
  733. " line\n"
  734. << " environment - display the current enviroment\n"
  735. << " remove file1 file2 ... - remove the file(s)\n"
  736. << " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar.\n"
  737. << " time command [args] ... - run command and return elapsed time\n"
  738. #if defined(_WIN32) && !defined(__CYGWIN__)
  739. << " write_regv key value - write registry value\n"
  740. << " delete_regv key - delete registry value\n"
  741. << " comspec - on windows 9x use this for RunCommand\n"
  742. #else
  743. << " create_symlink old new - create a symbolic link new -> old\n"
  744. #endif
  745. ;
  746. cmSystemTools::Error(errorStream.str().c_str());
  747. }
  748. int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
  749. {
  750. if (args.size() > 1)
  751. {
  752. // Copy file
  753. if (args[1] == "copy" && args.size() == 4)
  754. {
  755. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  756. {
  757. std::cerr << "Error copying file \"" << args[2].c_str()
  758. << "\" to \"" << args[3].c_str() << "\".\n";
  759. return 1;
  760. }
  761. return 0;
  762. }
  763. // Copy file if different.
  764. if (args[1] == "copy_if_different" && args.size() == 4)
  765. {
  766. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(),
  767. args[3].c_str()))
  768. {
  769. std::cerr << "Error copying file (if different) from \""
  770. << args[2].c_str() << "\" to \"" << args[3].c_str()
  771. << "\".\n";
  772. return 1;
  773. }
  774. return 0;
  775. }
  776. // Copy directory content
  777. if (args[1] == "copy_directory" && args.size() == 4)
  778. {
  779. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  780. {
  781. std::cerr << "Error copying directory from \""
  782. << args[2].c_str() << "\" to \"" << args[3].c_str()
  783. << "\".\n";
  784. return 1;
  785. }
  786. return 0;
  787. }
  788. // Compare files
  789. if (args[1] == "compare_files" && args.size() == 4)
  790. {
  791. if(cmSystemTools::FilesDiffer(args[2].c_str(), args[3].c_str()))
  792. {
  793. std::cerr << "Files \""
  794. << args[2].c_str() << "\" to \"" << args[3].c_str()
  795. << "\" are different.\n";
  796. return 1;
  797. }
  798. return 0;
  799. }
  800. // Echo string
  801. else if (args[1] == "echo" )
  802. {
  803. unsigned int cc;
  804. const char* space = "";
  805. for ( cc = 2; cc < args.size(); cc ++ )
  806. {
  807. std::cout << space << args[cc];
  808. space = " ";
  809. }
  810. std::cout << std::endl;
  811. return 0;
  812. }
  813. // Echo string no new line
  814. else if (args[1] == "echo_append" )
  815. {
  816. unsigned int cc;
  817. const char* space = "";
  818. for ( cc = 2; cc < args.size(); cc ++ )
  819. {
  820. std::cout << space << args[cc];
  821. space = " ";
  822. }
  823. return 0;
  824. }
  825. #if defined(CMAKE_BUILD_WITH_CMAKE)
  826. // Command to create a symbolic link. Fails on platforms not
  827. // supporting them.
  828. else if (args[1] == "environment" )
  829. {
  830. std::vector<std::string> env = cmSystemTools::GetEnvironmentVariables();
  831. std::vector<std::string>::iterator it;
  832. for ( it = env.begin(); it != env.end(); ++ it )
  833. {
  834. std::cout << it->c_str() << std::endl;
  835. }
  836. return 0;
  837. }
  838. #endif
  839. // Remove file
  840. else if (args[1] == "remove" && args.size() > 2)
  841. {
  842. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  843. {
  844. if(args[cc] != "-f")
  845. {
  846. if(args[cc] == "\\-f")
  847. {
  848. args[cc] = "-f";
  849. }
  850. cmSystemTools::RemoveFile(args[cc].c_str());
  851. }
  852. }
  853. return 0;
  854. }
  855. // Clock command
  856. else if (args[1] == "time" && args.size() > 2)
  857. {
  858. std::string command = args[2];
  859. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  860. {
  861. command += " ";
  862. command += args[cc];
  863. }
  864. clock_t clock_start, clock_finish;
  865. time_t time_start, time_finish;
  866. time(&time_start);
  867. clock_start = clock();
  868. cmSystemTools::RunSingleCommand(command.c_str());
  869. clock_finish = clock();
  870. time(&time_finish);
  871. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  872. std::cout << "Elapsed time: "
  873. << (long)(time_finish - time_start) << " s. (time)"
  874. << ", "
  875. << (double)(clock_finish - clock_start) / clocks_per_sec
  876. << " s. (clock)"
  877. << "\n";
  878. return 0;
  879. }
  880. // Command to change directory and run a program.
  881. else if (args[1] == "chdir" && args.size() >= 4)
  882. {
  883. std::string directory = args[2];
  884. if(!cmSystemTools::FileExists(directory.c_str()))
  885. {
  886. cmSystemTools::Error("Directory does not exist for chdir command: ",
  887. args[2].c_str());
  888. return 0;
  889. }
  890. std::string command = "\"";
  891. command += args[3];
  892. command += "\"";
  893. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  894. {
  895. command += " \"";
  896. command += args[cc];
  897. command += "\"";
  898. }
  899. int retval = 0;
  900. int timeout = 0;
  901. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  902. directory.c_str(), true, timeout) )
  903. {
  904. return retval;
  905. }
  906. return 1;
  907. }
  908. // Command to start progress for a build
  909. else if (args[1] == "cmake_progress_start" && args.size() == 4)
  910. {
  911. // bascially remove the directory
  912. std::string dirName = args[2];
  913. dirName += "/Progress";
  914. cmSystemTools::RemoveADirectory(dirName.c_str());
  915. cmSystemTools::MakeDirectory(dirName.c_str());
  916. // write the count into the directory
  917. std::string fName = dirName;
  918. fName += "/count.txt";
  919. FILE *progFile = fopen(fName.c_str(),"w");
  920. if (progFile)
  921. {
  922. int count = atoi(args[3].c_str());
  923. fprintf(progFile,"%i\n",count);
  924. fclose(progFile);
  925. }
  926. return 0;
  927. }
  928. // Command to report progress for a build
  929. else if (args[1] == "cmake_progress_report" && args.size() >= 4)
  930. {
  931. std::string dirName = args[2];
  932. dirName += "/Progress";
  933. std::string fName;
  934. FILE *progFile;
  935. unsigned int i;
  936. for (i = 3; i < args.size(); ++i)
  937. {
  938. fName = dirName;
  939. fName += "/";
  940. fName += args[i];
  941. progFile = fopen(fName.c_str(),"w");
  942. if (progFile)
  943. {
  944. fprintf(progFile,"empty");
  945. fclose(progFile);
  946. }
  947. }
  948. cmsys::Directory dir;
  949. dir.Load(dirName.c_str());
  950. int fileNum = static_cast<int>(dir.GetNumberOfFiles());
  951. // read the count
  952. fName = dirName;
  953. fName += "/count.txt";
  954. progFile = fopen(fName.c_str(),"r");
  955. if (progFile)
  956. {
  957. int count = 0;
  958. fscanf(progFile,"%i",&count);
  959. if (count > 0)
  960. {
  961. // print the progress
  962. fprintf(stdout,"%3i%% complete\n",((fileNum-3)*100)/count);
  963. }
  964. fclose(progFile);
  965. }
  966. return 0;
  967. }
  968. // Command to create a symbolic link. Fails on platforms not
  969. // supporting them.
  970. else if (args[1] == "create_symlink" && args.size() == 4)
  971. {
  972. return cmSystemTools::CreateSymlink(args[2].c_str(),
  973. args[3].c_str())? 0:1;
  974. }
  975. // Internal CMake shared library support.
  976. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  977. {
  978. int result = 0;
  979. std::string realName = args[2];
  980. std::string soName = args[3];
  981. std::string name = args[4];
  982. if(soName != realName)
  983. {
  984. std::string fname = cmSystemTools::GetFilenameName(realName);
  985. if(cmSystemTools::FileExists(soName.c_str()))
  986. {
  987. cmSystemTools::RemoveFile(soName.c_str());
  988. }
  989. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  990. {
  991. result = 1;
  992. }
  993. }
  994. if(name != soName)
  995. {
  996. std::string fname = cmSystemTools::GetFilenameName(soName);
  997. if(cmSystemTools::FileExists(soName.c_str()))
  998. {
  999. cmSystemTools::RemoveFile(name.c_str());
  1000. }
  1001. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1002. {
  1003. result = 1;
  1004. }
  1005. }
  1006. return result;
  1007. }
  1008. // Internal CMake versioned executable support.
  1009. else if (args[1] == "cmake_symlink_executable" && args.size() == 4)
  1010. {
  1011. int result = 0;
  1012. std::string realName = args[2];
  1013. std::string name = args[3];
  1014. if(name != realName)
  1015. {
  1016. std::string fname = cmSystemTools::GetFilenameName(realName);
  1017. if(cmSystemTools::FileExists(realName.c_str()))
  1018. {
  1019. cmSystemTools::RemoveFile(name.c_str());
  1020. }
  1021. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1022. {
  1023. result = 1;
  1024. }
  1025. }
  1026. return result;
  1027. }
  1028. // Internal CMake dependency scanning support.
  1029. else if (args[1] == "cmake_depends" && args.size() >= 6)
  1030. {
  1031. // Create a cmake object instance to process dependencies.
  1032. cmake cm;
  1033. std::string gen;
  1034. std::string homeDir;
  1035. std::string startDir;
  1036. std::string homeOutDir;
  1037. std::string startOutDir;
  1038. std::string depInfo;
  1039. if(args.size() >= 8)
  1040. {
  1041. // Full signature:
  1042. //
  1043. // -E cmake_depends <generator>
  1044. // <home-src-dir> <start-src-dir>
  1045. // <home-out-dir> <start-out-dir>
  1046. // <dep-info>
  1047. //
  1048. // All paths are provided.
  1049. gen = args[2];
  1050. homeDir = args[3];
  1051. startDir = args[4];
  1052. homeOutDir = args[5];
  1053. startOutDir = args[6];
  1054. depInfo = args[7];
  1055. }
  1056. else
  1057. {
  1058. // Support older signature for existing makefiles:
  1059. //
  1060. // -E cmake_depends <generator>
  1061. // <home-out-dir> <start-out-dir>
  1062. // <dep-info>
  1063. //
  1064. // Just pretend the source directories are the same as the
  1065. // binary directories so at least scanning will work.
  1066. gen = args[2];
  1067. homeDir = args[3];
  1068. startDir = args[4];
  1069. homeOutDir = args[3];
  1070. startOutDir = args[3];
  1071. depInfo = args[5];
  1072. }
  1073. // Create a local generator configured for the directory in
  1074. // which dependencies will be scanned.
  1075. homeDir = cmSystemTools::CollapseFullPath(homeDir.c_str());
  1076. startDir = cmSystemTools::CollapseFullPath(startDir.c_str());
  1077. homeOutDir = cmSystemTools::CollapseFullPath(homeOutDir.c_str());
  1078. startOutDir = cmSystemTools::CollapseFullPath(startOutDir.c_str());
  1079. cm.SetHomeDirectory(homeDir.c_str());
  1080. cm.SetStartDirectory(startDir.c_str());
  1081. cm.SetHomeOutputDirectory(homeOutDir.c_str());
  1082. cm.SetStartOutputDirectory(startOutDir.c_str());
  1083. if(cmGlobalGenerator* ggd = cm.CreateGlobalGenerator(gen.c_str()))
  1084. {
  1085. cm.SetGlobalGenerator(ggd);
  1086. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1087. lgd->SetGlobalGenerator(ggd);
  1088. lgd->GetMakefile()->SetStartDirectory(startDir.c_str());
  1089. lgd->GetMakefile()->SetStartOutputDirectory(startOutDir.c_str());
  1090. lgd->GetMakefile()->MakeStartDirectoriesCurrent();
  1091. lgd->SetupPathConversions();
  1092. // Actually scan dependencies.
  1093. return lgd->ScanDependencies(depInfo.c_str())? 0 : 2;
  1094. }
  1095. return 1;
  1096. }
  1097. #ifdef CMAKE_BUILD_WITH_CMAKE
  1098. // Internal CMake color makefile support.
  1099. else if (args[1] == "cmake_echo_color")
  1100. {
  1101. return cmake::ExecuteEchoColor(args);
  1102. }
  1103. #endif
  1104. // Tar files
  1105. else if (args[1] == "tar" && args.size() > 3)
  1106. {
  1107. std::string flags = args[2];
  1108. std::string outFile = args[3];
  1109. std::vector<cmStdString> files;
  1110. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  1111. {
  1112. files.push_back(args[cc]);
  1113. }
  1114. bool gzip = false;
  1115. bool verbose = false;
  1116. if ( flags.find_first_of('z') != flags.npos )
  1117. {
  1118. gzip = true;
  1119. }
  1120. if ( flags.find_first_of('v') != flags.npos )
  1121. {
  1122. verbose = true;
  1123. }
  1124. if ( flags.find_first_of('t') != flags.npos )
  1125. {
  1126. if ( !cmSystemTools::ListTar(outFile.c_str(), files, gzip, verbose) )
  1127. {
  1128. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1129. return 1;
  1130. }
  1131. }
  1132. else if ( flags.find_first_of('c') != flags.npos )
  1133. {
  1134. if ( !cmSystemTools::CreateTar(
  1135. outFile.c_str(), files, gzip, verbose) )
  1136. {
  1137. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1138. return 1;
  1139. }
  1140. }
  1141. else if ( flags.find_first_of('x') != flags.npos )
  1142. {
  1143. if ( !cmSystemTools::ExtractTar(
  1144. outFile.c_str(), files, gzip, verbose) )
  1145. {
  1146. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  1147. return 1;
  1148. }
  1149. }
  1150. return 0;
  1151. }
  1152. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1153. // Internal CMake Fortran module support.
  1154. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  1155. {
  1156. return cmDependsFortran::CopyModule(args)? 0 : 1;
  1157. }
  1158. #endif
  1159. #if defined(_WIN32) && !defined(__CYGWIN__)
  1160. // Write registry value
  1161. else if (args[1] == "write_regv" && args.size() > 3)
  1162. {
  1163. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  1164. args[3].c_str()) ? 0 : 1;
  1165. }
  1166. // Delete registry value
  1167. else if (args[1] == "delete_regv" && args.size() > 2)
  1168. {
  1169. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  1170. }
  1171. // Remove file
  1172. else if (args[1] == "comspec" && args.size() > 2)
  1173. {
  1174. unsigned int cc;
  1175. std::string command = args[2];
  1176. for ( cc = 3; cc < args.size(); cc ++ )
  1177. {
  1178. command += " " + args[cc];
  1179. }
  1180. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  1181. }
  1182. #endif
  1183. }
  1184. ::CMakeCommandUsage(args[0].c_str());
  1185. return 1;
  1186. }
  1187. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  1188. {
  1189. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1190. i != this->Generators.end(); ++i)
  1191. {
  1192. names.push_back(i->first);
  1193. }
  1194. }
  1195. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  1196. {
  1197. RegisteredGeneratorsMap::const_iterator i = this->Generators.find(name);
  1198. if(i != this->Generators.end())
  1199. {
  1200. cmGlobalGenerator* generator = (i->second)();
  1201. generator->SetCMakeInstance(this);
  1202. return generator;
  1203. }
  1204. else
  1205. {
  1206. return 0;
  1207. }
  1208. }
  1209. void cmake::SetHomeDirectory(const char* dir)
  1210. {
  1211. this->cmHomeDirectory = dir;
  1212. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1213. }
  1214. void cmake::SetHomeOutputDirectory(const char* lib)
  1215. {
  1216. this->HomeOutputDirectory = lib;
  1217. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1218. }
  1219. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  1220. {
  1221. // delete the old generator
  1222. if (this->GlobalGenerator)
  1223. {
  1224. delete this->GlobalGenerator;
  1225. // restore the original environment variables CXX and CC
  1226. // Restor CC
  1227. std::string env = "CC=";
  1228. if(this->CCEnvironment.size())
  1229. {
  1230. env += this->CCEnvironment;
  1231. }
  1232. cmSystemTools::PutEnv(env.c_str());
  1233. env = "CXX=";
  1234. if(this->CXXEnvironment.size())
  1235. {
  1236. env += this->CXXEnvironment;
  1237. }
  1238. cmSystemTools::PutEnv(env.c_str());
  1239. }
  1240. // set the new
  1241. this->GlobalGenerator = gg;
  1242. // set the global flag for unix style paths on cmSystemTools as soon as
  1243. // the generator is set. This allows gmake to be used on windows.
  1244. cmSystemTools::SetForceUnixPaths
  1245. (this->GlobalGenerator->GetForceUnixPaths());
  1246. // Save the environment variables CXX and CC
  1247. const char* cxx = getenv("CXX");
  1248. const char* cc = getenv("CC");
  1249. if(cxx)
  1250. {
  1251. this->CXXEnvironment = cxx;
  1252. }
  1253. else
  1254. {
  1255. this->CXXEnvironment = "";
  1256. }
  1257. if(cc)
  1258. {
  1259. this->CCEnvironment = cc;
  1260. }
  1261. else
  1262. {
  1263. this->CCEnvironment = "";
  1264. }
  1265. // set the cmake instance just to be sure
  1266. gg->SetCMakeInstance(this);
  1267. }
  1268. int cmake::DoPreConfigureChecks()
  1269. {
  1270. // Make sure the Start directory contains a CMakeLists.txt file.
  1271. std::string srcList = this->GetHomeDirectory();
  1272. srcList += "/CMakeLists.txt";
  1273. if(!cmSystemTools::FileExists(srcList.c_str()))
  1274. {
  1275. cmOStringStream err;
  1276. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1277. {
  1278. err << "The source directory \"" << this->GetHomeDirectory()
  1279. << "\" does not appear to contain CMakeLists.txt.\n";
  1280. }
  1281. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1282. {
  1283. err << "The source directory \"" << this->GetHomeDirectory()
  1284. << "\" is a file, not a directory.\n";
  1285. }
  1286. else
  1287. {
  1288. err << "The source directory \"" << this->GetHomeDirectory()
  1289. << "\" does not exist.\n";
  1290. }
  1291. err << "Specify --help for usage, or press the help button on the CMake "
  1292. "GUI.";
  1293. cmSystemTools::Error(err.str().c_str());
  1294. return -2;
  1295. }
  1296. // do a sanity check on some values
  1297. if(this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1298. {
  1299. std::string cacheStart =
  1300. this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1301. cacheStart += "/CMakeLists.txt";
  1302. std::string currentStart = this->GetHomeDirectory();
  1303. currentStart += "/CMakeLists.txt";
  1304. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1305. {
  1306. std::string message = "The source \"";
  1307. message += currentStart;
  1308. message += "\" does not match the source \"";
  1309. message += cacheStart;
  1310. message += "\" used to generate cache. ";
  1311. message += "Re-run cmake with a different source directory.";
  1312. cmSystemTools::Error(message.c_str());
  1313. return -2;
  1314. }
  1315. }
  1316. else
  1317. {
  1318. return 0;
  1319. }
  1320. return 1;
  1321. }
  1322. int cmake::Configure()
  1323. {
  1324. // Construct right now our path conversion table before it's too late:
  1325. this->UpdateConversionPathTable();
  1326. this->CleanupCommandsAndMacros();
  1327. int res = 0;
  1328. if ( !this->ScriptMode )
  1329. {
  1330. res = this->DoPreConfigureChecks();
  1331. }
  1332. if ( res < 0 )
  1333. {
  1334. return -2;
  1335. }
  1336. if ( !res )
  1337. {
  1338. this->CacheManager->AddCacheEntry
  1339. ("CMAKE_HOME_DIRECTORY",
  1340. this->GetHomeDirectory(),
  1341. "Start directory with the top level CMakeLists.txt file for this "
  1342. "project",
  1343. cmCacheManager::INTERNAL);
  1344. }
  1345. // set the default BACKWARDS compatibility to the current version
  1346. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1347. {
  1348. char ver[256];
  1349. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1350. cmMakefile::GetMinorVersion());
  1351. this->CacheManager->AddCacheEntry
  1352. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1353. "For backwards compatibility, what version of CMake commands and "
  1354. "syntax should this version of CMake allow.",
  1355. cmCacheManager::STRING);
  1356. }
  1357. // no generator specified on the command line
  1358. if(!this->GlobalGenerator)
  1359. {
  1360. const char* genName =
  1361. this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1362. if(genName)
  1363. {
  1364. this->GlobalGenerator = this->CreateGlobalGenerator(genName);
  1365. }
  1366. if(this->GlobalGenerator)
  1367. {
  1368. // set the global flag for unix style paths on cmSystemTools as
  1369. // soon as the generator is set. This allows gmake to be used
  1370. // on windows.
  1371. cmSystemTools::SetForceUnixPaths
  1372. (this->GlobalGenerator->GetForceUnixPaths());
  1373. }
  1374. else
  1375. {
  1376. #if defined(__BORLANDC__) && defined(_WIN32)
  1377. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1378. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1379. std::string installedCompiler;
  1380. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1381. "\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  1382. cmSystemTools::ExpandRegistryValues(mp);
  1383. if (!(mp == "/registry"))
  1384. {
  1385. installedCompiler = "Visual Studio 8 2005";
  1386. }
  1387. else
  1388. {
  1389. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1390. "\\VisualStudio\\7.1;InstallDir]";
  1391. cmSystemTools::ExpandRegistryValues(mp);
  1392. if (!(mp == "/registry"))
  1393. {
  1394. installedCompiler = "Visual Studio 7 .NET 2003";
  1395. }
  1396. else
  1397. {
  1398. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1399. "\\VisualStudio\\7.0;InstallDir]";
  1400. cmSystemTools::ExpandRegistryValues(mp);
  1401. if (!(mp == "/registry"))
  1402. {
  1403. installedCompiler = "Visual Studio 7";
  1404. }
  1405. else
  1406. {
  1407. installedCompiler = "Visual Studio 6";
  1408. }
  1409. }
  1410. }
  1411. cmGlobalGenerator* gen
  1412. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1413. if(!gen)
  1414. {
  1415. gen = new cmGlobalNMakeMakefileGenerator;
  1416. }
  1417. this->SetGlobalGenerator(gen);
  1418. #else
  1419. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1420. #endif
  1421. }
  1422. if(!this->GlobalGenerator)
  1423. {
  1424. cmSystemTools::Error("Could not create generator");
  1425. return -1;
  1426. }
  1427. }
  1428. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1429. if(genName)
  1430. {
  1431. if(strcmp(this->GlobalGenerator->GetName(), genName) != 0)
  1432. {
  1433. std::string message = "Error: generator : ";
  1434. message += this->GlobalGenerator->GetName();
  1435. message += "\nDoes not match the generator used previously: ";
  1436. message += genName;
  1437. message +=
  1438. "\nEither remove the CMakeCache.txt file or choose a different"
  1439. " binary directory.";
  1440. cmSystemTools::Error(message.c_str());
  1441. return -2;
  1442. }
  1443. }
  1444. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1445. {
  1446. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1447. this->GlobalGenerator->GetName(),
  1448. "Name of generator.",
  1449. cmCacheManager::INTERNAL);
  1450. }
  1451. // reset any system configuration information, except for when we are
  1452. // InTryCompile. With TryCompile the system info is taken from the parent's
  1453. // info to save time
  1454. if (!this->InTryCompile)
  1455. {
  1456. this->GlobalGenerator->ClearEnabledLanguages();
  1457. }
  1458. this->CleanupWrittenFiles();
  1459. // Truncate log files
  1460. if (!this->InTryCompile)
  1461. {
  1462. this->TruncateOutputLog("CMakeOutput.log");
  1463. this->TruncateOutputLog("CMakeError.log");
  1464. }
  1465. // actually do the configure
  1466. this->GlobalGenerator->Configure();
  1467. // Before saving the cache
  1468. // if the project did not define one of the entries below, add them now
  1469. // so users can edit the values in the cache:
  1470. // LIBRARY_OUTPUT_PATH
  1471. // EXECUTABLE_OUTPUT_PATH
  1472. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1473. {
  1474. this->CacheManager->AddCacheEntry
  1475. ("LIBRARY_OUTPUT_PATH", "",
  1476. "Single output directory for building all libraries.",
  1477. cmCacheManager::PATH);
  1478. }
  1479. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1480. {
  1481. this->CacheManager->AddCacheEntry
  1482. ("EXECUTABLE_OUTPUT_PATH", "",
  1483. "Single output directory for building all executables.",
  1484. cmCacheManager::PATH);
  1485. }
  1486. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1487. {
  1488. this->CacheManager->AddCacheEntry
  1489. ("CMAKE_USE_RELATIVE_PATHS", false,
  1490. "If true, cmake will use relative paths in makefiles and projects.");
  1491. cmCacheManager::CacheIterator it =
  1492. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1493. if ( !it.PropertyExists("ADVANCED") )
  1494. {
  1495. it.SetProperty("ADVANCED", "1");
  1496. }
  1497. }
  1498. if(cmSystemTools::GetFatalErrorOccured() &&
  1499. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1500. cmSystemTools::IsOff(this->CacheManager->
  1501. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1502. {
  1503. // We must have a bad generator selection. Wipe the cache entry so the
  1504. // user can select another.
  1505. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1506. }
  1507. // only save the cache if there were no fatal errors
  1508. if ( !this->ScriptMode )
  1509. {
  1510. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1511. }
  1512. if ( !this->GraphVizFile.empty() )
  1513. {
  1514. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1515. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1516. }
  1517. if(cmSystemTools::GetErrorOccuredFlag())
  1518. {
  1519. return -1;
  1520. }
  1521. return 0;
  1522. }
  1523. bool cmake::CacheVersionMatches()
  1524. {
  1525. const char* majv =
  1526. this->CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1527. const char* minv =
  1528. this->CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1529. const char* relv =
  1530. this->CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1531. bool cacheSameCMake = false;
  1532. if(majv &&
  1533. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  1534. && minv &&
  1535. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  1536. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  1537. {
  1538. cacheSameCMake = true;
  1539. }
  1540. return cacheSameCMake;
  1541. }
  1542. void cmake::PreLoadCMakeFiles()
  1543. {
  1544. std::string pre_load = this->GetHomeDirectory();
  1545. if ( pre_load.size() > 0 )
  1546. {
  1547. pre_load += "/PreLoad.cmake";
  1548. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1549. {
  1550. this->ReadListFile(pre_load.c_str());
  1551. }
  1552. }
  1553. pre_load = this->GetHomeOutputDirectory();
  1554. if ( pre_load.size() > 0 )
  1555. {
  1556. pre_load += "/PreLoad.cmake";
  1557. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1558. {
  1559. this->ReadListFile(pre_load.c_str());
  1560. }
  1561. }
  1562. }
  1563. // handle a command line invocation
  1564. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1565. {
  1566. // Process the arguments
  1567. this->SetArgs(args);
  1568. if(cmSystemTools::GetErrorOccuredFlag())
  1569. {
  1570. CMakeCommandUsage(args[0].c_str());
  1571. return -1;
  1572. }
  1573. // set the cmake command
  1574. this->CMakeCommand = args[0];
  1575. if ( !this->ScriptMode )
  1576. {
  1577. // load the cache
  1578. if(this->LoadCache() < 0)
  1579. {
  1580. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1581. return -1;
  1582. }
  1583. }
  1584. else
  1585. {
  1586. this->AddCMakePaths(this->CMakeCommand.c_str());
  1587. }
  1588. // Add any cache args
  1589. if ( !this->SetCacheArgs(args) )
  1590. {
  1591. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1592. return -1;
  1593. }
  1594. // In script mode we terminate after running the script.
  1595. if(this->ScriptMode)
  1596. {
  1597. if(cmSystemTools::GetErrorOccuredFlag())
  1598. {
  1599. return -1;
  1600. }
  1601. else
  1602. {
  1603. return 0;
  1604. }
  1605. }
  1606. this->PreLoadCMakeFiles();
  1607. std::string systemFile = this->GetHomeOutputDirectory();
  1608. systemFile += "/CMakeSystem.cmake";
  1609. if ( noconfigure )
  1610. {
  1611. return 0;
  1612. }
  1613. // now run the global generate
  1614. // Check the state of the build system to see if we need to regenerate.
  1615. if(!this->CheckBuildSystem())
  1616. {
  1617. return 0;
  1618. }
  1619. // If we are doing global generate, we better set start and start
  1620. // output directory to the root of the project.
  1621. std::string oldstartdir = this->GetStartDirectory();
  1622. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1623. this->SetStartDirectory(this->GetHomeDirectory());
  1624. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1625. int ret = this->Configure();
  1626. if (ret || this->ScriptMode)
  1627. {
  1628. return ret;
  1629. }
  1630. ret = this->Generate();
  1631. std::string message = "Build files have been written to: ";
  1632. message += this->GetHomeOutputDirectory();
  1633. this->UpdateProgress(message.c_str(), -1);
  1634. if(ret)
  1635. {
  1636. return ret;
  1637. }
  1638. this->SetStartDirectory(oldstartdir.c_str());
  1639. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1640. return ret;
  1641. }
  1642. int cmake::Generate()
  1643. {
  1644. if(!this->GlobalGenerator)
  1645. {
  1646. return -1;
  1647. }
  1648. this->GlobalGenerator->Generate();
  1649. if(cmSystemTools::GetErrorOccuredFlag())
  1650. {
  1651. return -1;
  1652. }
  1653. return 0;
  1654. }
  1655. unsigned int cmake::GetMajorVersion()
  1656. {
  1657. return cmMakefile::GetMajorVersion();
  1658. }
  1659. unsigned int cmake::GetMinorVersion()
  1660. {
  1661. return cmMakefile::GetMinorVersion();
  1662. }
  1663. const char *cmake::GetReleaseVersion()
  1664. {
  1665. return cmMakefile::GetReleaseVersion();
  1666. }
  1667. void cmake::AddCacheEntry(const char* key, const char* value,
  1668. const char* helpString,
  1669. int type)
  1670. {
  1671. this->CacheManager->AddCacheEntry(key, value,
  1672. helpString,
  1673. cmCacheManager::CacheEntryType(type));
  1674. }
  1675. const char* cmake::GetCacheDefinition(const char* name) const
  1676. {
  1677. return this->CacheManager->GetCacheValue(name);
  1678. }
  1679. int cmake::DumpDocumentationToFile(std::ostream& f)
  1680. {
  1681. #ifdef CMAKE_BUILD_WITH_CMAKE
  1682. // Loop over all registered commands and print out documentation
  1683. const char *name;
  1684. const char *terse;
  1685. const char *full;
  1686. char tmp[1024];
  1687. sprintf(tmp,"Version %d.%d (%s)", cmake::GetMajorVersion(),
  1688. cmake::GetMinorVersion(), cmVersion::GetReleaseVersion().c_str());
  1689. f << "<html>\n";
  1690. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1691. f << "<ul>\n";
  1692. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  1693. j != this->Commands.end(); ++j)
  1694. {
  1695. name = (*j).second->GetName();
  1696. terse = (*j).second->GetTerseDocumentation();
  1697. full = (*j).second->GetFullDocumentation();
  1698. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1699. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1700. }
  1701. f << "</ul></html>\n";
  1702. #else
  1703. (void)f;
  1704. #endif
  1705. return 1;
  1706. }
  1707. void cmake::AddDefaultCommands()
  1708. {
  1709. std::list<cmCommand*> commands;
  1710. GetBootstrapCommands(commands);
  1711. GetPredefinedCommands(commands);
  1712. for(std::list<cmCommand*>::iterator i = commands.begin();
  1713. i != commands.end(); ++i)
  1714. {
  1715. this->AddCommand(*i);
  1716. }
  1717. }
  1718. void cmake::AddDefaultGenerators()
  1719. {
  1720. #if defined(_WIN32) && !defined(__CYGWIN__)
  1721. # if !defined(CMAKE_BOOT_MINGW)
  1722. this->Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1723. &cmGlobalVisualStudio6Generator::New;
  1724. this->Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1725. &cmGlobalVisualStudio7Generator::New;
  1726. this->Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1727. &cmGlobalVisualStudio71Generator::New;
  1728. this->Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  1729. &cmGlobalVisualStudio8Generator::New;
  1730. this->Generators[cmGlobalVisualStudio8Win64Generator::GetActualName()] =
  1731. &cmGlobalVisualStudio8Win64Generator::New;
  1732. this->Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1733. &cmGlobalBorlandMakefileGenerator::New;
  1734. this->Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1735. &cmGlobalNMakeMakefileGenerator::New;
  1736. this->Generators[cmGlobalWatcomWMakeGenerator::GetActualName()] =
  1737. &cmGlobalWatcomWMakeGenerator::New;
  1738. # endif
  1739. this->Generators[cmGlobalMSYSMakefileGenerator::GetActualName()] =
  1740. &cmGlobalMSYSMakefileGenerator::New;
  1741. this->Generators[cmGlobalMinGWMakefileGenerator::GetActualName()] =
  1742. &cmGlobalMinGWMakefileGenerator::New;
  1743. #endif
  1744. this->Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
  1745. &cmGlobalUnixMakefileGenerator3::New;
  1746. #ifdef CMAKE_USE_XCODE
  1747. this->Generators[cmGlobalXCodeGenerator::GetActualName()] =
  1748. &cmGlobalXCodeGenerator::New;
  1749. #endif
  1750. #ifdef CMAKE_USE_KDEVELOP
  1751. this->Generators[cmGlobalKdevelopGenerator::GetActualName()] =
  1752. &cmGlobalKdevelopGenerator::New;
  1753. #endif
  1754. }
  1755. int cmake::LoadCache()
  1756. {
  1757. // could we not read the cache
  1758. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  1759. {
  1760. // if it does exist, but isn;t readable then warn the user
  1761. std::string cacheFile = this->GetHomeOutputDirectory();
  1762. cacheFile += "/CMakeCache.txt";
  1763. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1764. {
  1765. cmSystemTools::Error(
  1766. "There is a CMakeCache.txt file for the current binary tree but "
  1767. "cmake does not have permission to read it. Please check the "
  1768. "permissions of the directory you are trying to run CMake on.");
  1769. return -1;
  1770. }
  1771. }
  1772. if (this->CMakeCommand.size() < 2)
  1773. {
  1774. cmSystemTools::Error(
  1775. "cmake command was not specified prior to loading the cache in "
  1776. "cmake.cxx");
  1777. return -1;
  1778. }
  1779. // setup CMAKE_ROOT and CMAKE_COMMAND
  1780. if(!this->AddCMakePaths(this->CMakeCommand.c_str()))
  1781. {
  1782. return -3;
  1783. }
  1784. // set the default BACKWARDS compatibility to the current version
  1785. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1786. {
  1787. char ver[256];
  1788. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1789. cmMakefile::GetMinorVersion());
  1790. this->CacheManager->AddCacheEntry
  1791. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1792. "For backwards compatibility, what version of CMake commands and "
  1793. "syntax should this version of CMake allow.",
  1794. cmCacheManager::STRING);
  1795. }
  1796. return 0;
  1797. }
  1798. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  1799. {
  1800. this->ProgressCallback = f;
  1801. this->ProgressCallbackClientData = cd;
  1802. }
  1803. void cmake::UpdateProgress(const char *msg, float prog)
  1804. {
  1805. if(this->ProgressCallback && !this->InTryCompile)
  1806. {
  1807. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  1808. return;
  1809. }
  1810. }
  1811. void cmake::GetCommandDocumentation(
  1812. std::vector<cmDocumentationEntry>& v) const
  1813. {
  1814. for(RegisteredCommandsMap::const_iterator j = this->Commands.begin();
  1815. j != this->Commands.end(); ++j)
  1816. {
  1817. cmDocumentationEntry e =
  1818. {
  1819. (*j).second->GetName(),
  1820. (*j).second->GetTerseDocumentation(),
  1821. (*j).second->GetFullDocumentation()
  1822. };
  1823. v.push_back(e);
  1824. }
  1825. cmDocumentationEntry empty = {0,0,0};
  1826. v.push_back(empty);
  1827. }
  1828. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1829. {
  1830. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1831. i != this->Generators.end(); ++i)
  1832. {
  1833. cmDocumentationEntry e;
  1834. cmGlobalGenerator* generator = (i->second)();
  1835. generator->GetDocumentation(e);
  1836. delete generator;
  1837. v.push_back(e);
  1838. }
  1839. cmDocumentationEntry empty = {0,0,0};
  1840. v.push_back(empty);
  1841. }
  1842. void cmake::AddWrittenFile(const char* file)
  1843. {
  1844. this->WrittenFiles.insert(file);
  1845. }
  1846. bool cmake::HasWrittenFile(const char* file)
  1847. {
  1848. return this->WrittenFiles.find(file) != this->WrittenFiles.end();
  1849. }
  1850. void cmake::CleanupWrittenFiles()
  1851. {
  1852. this->WrittenFiles.clear();
  1853. }
  1854. void cmake::UpdateConversionPathTable()
  1855. {
  1856. // Update the path conversion table with any specified file:
  1857. const char* tablepath =
  1858. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1859. if(tablepath)
  1860. {
  1861. std::ifstream table( tablepath );
  1862. if(!table)
  1863. {
  1864. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  1865. ". CMake can not open file.");
  1866. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1867. }
  1868. else
  1869. {
  1870. std::string a, b;
  1871. while(!table.eof())
  1872. {
  1873. // two entries per line
  1874. table >> a; table >> b;
  1875. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1876. }
  1877. }
  1878. }
  1879. }
  1880. //----------------------------------------------------------------------------
  1881. int cmake::CheckBuildSystem()
  1882. {
  1883. // We do not need to rerun CMake. Check dependency integrity. Use
  1884. // the make system's VERBOSE environment variable to enable verbose
  1885. // output.
  1886. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  1887. // This method will check the integrity of the build system if the
  1888. // option was given on the command line. It reads the given file to
  1889. // determine whether CMake should rerun. If it does rerun then the
  1890. // generation step will check the integrity of dependencies. If it
  1891. // does not then we need to check the integrity here.
  1892. // If no file is provided for the check, we have to rerun.
  1893. if(this->CheckBuildSystemArgument.size() == 0)
  1894. {
  1895. if(verbose)
  1896. {
  1897. cmOStringStream msg;
  1898. msg << "Re-run cmake no build system arguments\n";
  1899. cmSystemTools::Stdout(msg.str().c_str());
  1900. }
  1901. return 1;
  1902. }
  1903. // If the file provided does not exist, we have to rerun.
  1904. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  1905. {
  1906. if(verbose)
  1907. {
  1908. cmOStringStream msg;
  1909. msg << "Re-run cmake missing file: "
  1910. << this->CheckBuildSystemArgument.c_str() << "\n";
  1911. cmSystemTools::Stdout(msg.str().c_str());
  1912. }
  1913. return 1;
  1914. }
  1915. // Read the rerun check file and use it to decide whether to do the
  1916. // global generate.
  1917. cmake cm;
  1918. cmGlobalGenerator gg;
  1919. gg.SetCMakeInstance(&cm);
  1920. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1921. lg->SetGlobalGenerator(&gg);
  1922. cmMakefile* mf = lg->GetMakefile();
  1923. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  1924. cmSystemTools::GetErrorOccuredFlag())
  1925. {
  1926. if(verbose)
  1927. {
  1928. cmOStringStream msg;
  1929. msg << "Re-run cmake error reading : "
  1930. << this->CheckBuildSystemArgument.c_str() << "\n";
  1931. cmSystemTools::Stdout(msg.str().c_str());
  1932. }
  1933. // There was an error reading the file. Just rerun.
  1934. return 1;
  1935. }
  1936. // Get the set of dependencies and outputs.
  1937. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1938. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1939. if(!dependsStr || !outputsStr)
  1940. {
  1941. // Not enough information was provided to do the test. Just rerun.
  1942. if(verbose)
  1943. {
  1944. cmOStringStream msg;
  1945. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  1946. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  1947. cmSystemTools::Stdout(msg.str().c_str());
  1948. }
  1949. return 1;
  1950. }
  1951. std::vector<std::string> depends;
  1952. std::vector<std::string> outputs;
  1953. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1954. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1955. // If any output is older than any dependency then rerun.
  1956. for(std::vector<std::string>::iterator dep = depends.begin();
  1957. dep != depends.end(); ++dep)
  1958. {
  1959. for(std::vector<std::string>::iterator out = outputs.begin();
  1960. out != outputs.end(); ++out)
  1961. {
  1962. int result = 0;
  1963. if(!this->FileComparison->FileTimeCompare(out->c_str(),
  1964. dep->c_str(), &result) ||
  1965. result < 0)
  1966. {
  1967. if(verbose)
  1968. {
  1969. cmOStringStream msg;
  1970. msg << "Re-run cmake file: " << out->c_str()
  1971. << " older than: " << dep->c_str() << "\n";
  1972. cmSystemTools::Stdout(msg.str().c_str());
  1973. }
  1974. return 1;
  1975. }
  1976. }
  1977. }
  1978. // compute depends based on the generator specified
  1979. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1980. if (!genName || genName[0] == '\0')
  1981. {
  1982. genName = "Unix Makefiles";
  1983. }
  1984. cmGlobalGenerator *ggd = this->CreateGlobalGenerator(genName);
  1985. if (ggd)
  1986. {
  1987. // Check the dependencies in case source files were removed.
  1988. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1989. lgd->SetGlobalGenerator(ggd);
  1990. lgd->CheckDependencies(mf, verbose, this->ClearBuildSystem);
  1991. // Check for multiple output pairs.
  1992. ggd->CheckMultipleOutputs(mf, verbose);
  1993. }
  1994. // No need to rerun.
  1995. return 0;
  1996. }
  1997. //----------------------------------------------------------------------------
  1998. void cmake::TruncateOutputLog(const char* fname)
  1999. {
  2000. std::string fullPath = this->GetHomeOutputDirectory();
  2001. fullPath += "/";
  2002. fullPath += fname;
  2003. struct stat st;
  2004. if ( ::stat(fullPath.c_str(), &st) )
  2005. {
  2006. return;
  2007. }
  2008. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  2009. {
  2010. cmSystemTools::RemoveFile(fullPath.c_str());
  2011. return;
  2012. }
  2013. size_t fsize = st.st_size;
  2014. const size_t maxFileSize = 50 * 1024;
  2015. if ( fsize < maxFileSize )
  2016. {
  2017. //TODO: truncate file
  2018. return;
  2019. }
  2020. }
  2021. inline std::string removeQuotes(const std::string& s)
  2022. {
  2023. if(s[0] == '\"' && s[s.size()-1] == '\"')
  2024. {
  2025. return s.substr(1, s.size()-2);
  2026. }
  2027. return s;
  2028. }
  2029. const char* cmake::GetCTestCommand()
  2030. {
  2031. if ( !this->CTestCommand.empty() )
  2032. {
  2033. return this->CTestCommand.c_str();
  2034. }
  2035. cmMakefile* mf
  2036. = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  2037. #ifdef CMAKE_BUILD_WITH_CMAKE
  2038. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2039. this->CTestCommand = removeQuotes(this->CTestCommand);
  2040. this->CTestCommand =
  2041. cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  2042. this->CTestCommand += "/";
  2043. this->CTestCommand += "ctest";
  2044. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  2045. if(!cmSystemTools::FileExists(this->CTestCommand.c_str()))
  2046. {
  2047. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2048. this->CTestCommand =
  2049. cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  2050. this->CTestCommand += "/Debug/";
  2051. this->CTestCommand += "ctest";
  2052. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  2053. }
  2054. if(!cmSystemTools::FileExists(this->CTestCommand.c_str()))
  2055. {
  2056. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2057. this->CTestCommand =
  2058. cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  2059. this->CTestCommand += "/Release/";
  2060. this->CTestCommand += "ctest";
  2061. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  2062. }
  2063. #else
  2064. // Only for bootstrap
  2065. this->CTestCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  2066. this->CTestCommand += "/ctest";
  2067. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  2068. #endif
  2069. if ( this->CTestCommand.empty() )
  2070. {
  2071. cmSystemTools::Error("Cannot find the CTest executable");
  2072. this->CTestCommand = "CTEST-COMMAND-NOT-FOUND";
  2073. }
  2074. return this->CTestCommand.c_str();
  2075. }
  2076. const char* cmake::GetCPackCommand()
  2077. {
  2078. if ( !this->CPackCommand.empty() )
  2079. {
  2080. return this->CPackCommand.c_str();
  2081. }
  2082. cmMakefile* mf
  2083. = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  2084. #ifdef CMAKE_BUILD_WITH_CMAKE
  2085. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2086. this->CPackCommand = removeQuotes(this->CPackCommand);
  2087. this->CPackCommand =
  2088. cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  2089. this->CPackCommand += "/";
  2090. this->CPackCommand += "cpack";
  2091. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  2092. if(!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  2093. {
  2094. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2095. this->CPackCommand =
  2096. cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  2097. this->CPackCommand += "/Debug/";
  2098. this->CPackCommand += "cpack";
  2099. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  2100. }
  2101. if(!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  2102. {
  2103. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2104. this->CPackCommand =
  2105. cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  2106. this->CPackCommand += "/Release/";
  2107. this->CPackCommand += "cpack";
  2108. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  2109. }
  2110. if (!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  2111. {
  2112. cmSystemTools::Error("Cannot find the CPack executable");
  2113. this->CPackCommand = "CPACK-COMMAND-NOT-FOUND";
  2114. }
  2115. #else
  2116. // Only for bootstrap
  2117. this->CPackCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  2118. this->CPackCommand += "/cpack";
  2119. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  2120. #endif
  2121. return this->CPackCommand.c_str();
  2122. }
  2123. void cmake::GenerateGraphViz(const char* fileName)
  2124. {
  2125. cmGeneratedFileStream str(fileName);
  2126. if ( !str )
  2127. {
  2128. return;
  2129. }
  2130. cmake cm;
  2131. cmGlobalGenerator ggi;
  2132. ggi.SetCMakeInstance(&cm);
  2133. std::auto_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator());
  2134. lg->SetGlobalGenerator(&ggi);
  2135. cmMakefile *mf = lg->GetMakefile();
  2136. std::string infile = this->GetHomeOutputDirectory();
  2137. infile += "/CMakeGraphVizOptions.cmake";
  2138. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2139. {
  2140. infile = this->GetHomeDirectory();
  2141. infile += "/CMakeGraphVizOptions.cmake";
  2142. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2143. {
  2144. infile = "";
  2145. }
  2146. }
  2147. if ( !infile.empty() )
  2148. {
  2149. if ( !mf->ReadListFile(0, infile.c_str()) )
  2150. {
  2151. cmSystemTools::Error("Problem opening GraphViz options file: ",
  2152. infile.c_str());
  2153. return;
  2154. }
  2155. std::cout << "Read GraphViz options file: " << infile.c_str()
  2156. << std::endl;
  2157. }
  2158. #define __set_if_not_set(var, value, cmakeDefinition) \
  2159. const char* var = mf->GetDefinition(cmakeDefinition); \
  2160. if ( !var ) \
  2161. { \
  2162. var = value; \
  2163. }
  2164. __set_if_not_set(graphType, "digraph", "GRAPHVIZ_GRAPH_TYPE");
  2165. __set_if_not_set(graphName, "GG", "GRAPHVIZ_GRAPH_NAME");
  2166. __set_if_not_set(graphHeader, "node [\n fontsize = \"12\"\n];",
  2167. "GRAPHVIZ_GRAPH_HEADER");
  2168. __set_if_not_set(graphNodePrefix, "node", "GRAPHVIZ_NODE_PREFIX");
  2169. const char* ignoreTargets = mf->GetDefinition("GRAPHVIZ_IGNORE_TARGETS");
  2170. std::set<cmStdString> ignoreTargetsSet;
  2171. if ( ignoreTargets )
  2172. {
  2173. std::vector<std::string> ignoreTargetsVector;
  2174. cmSystemTools::ExpandListArgument(ignoreTargets,ignoreTargetsVector);
  2175. std::vector<std::string>::iterator itvIt;
  2176. for ( itvIt = ignoreTargetsVector.begin();
  2177. itvIt != ignoreTargetsVector.end();
  2178. ++ itvIt )
  2179. {
  2180. ignoreTargetsSet.insert(itvIt->c_str());
  2181. }
  2182. }
  2183. str << graphType << " " << graphName << " {" << std::endl;
  2184. str << graphHeader << std::endl;
  2185. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  2186. std::vector<cmLocalGenerator*> localGenerators;
  2187. gg->GetLocalGenerators(localGenerators);
  2188. std::vector<cmLocalGenerator*>::iterator lit;
  2189. // for target deps
  2190. // 1 - cmake target
  2191. // 2 - external target
  2192. // 0 - no deps
  2193. std::map<cmStdString, int> targetDeps;
  2194. std::map<cmStdString, cmTarget*> targetPtrs;
  2195. std::map<cmStdString, cmStdString> targetNamesNodes;
  2196. char tgtName[100];
  2197. int cnt = 0;
  2198. // First pass get the list of all cmake targets
  2199. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2200. {
  2201. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2202. cmTargets::iterator tit;
  2203. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2204. {
  2205. const char* realTargetName = tit->first.c_str();
  2206. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2207. {
  2208. // Skip ignored targets
  2209. continue;
  2210. }
  2211. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  2212. sprintf(tgtName, "%s%d", graphNodePrefix, cnt++);
  2213. targetNamesNodes[realTargetName] = tgtName;
  2214. targetPtrs[realTargetName] = &tit->second;
  2215. }
  2216. }
  2217. // Ok, now find all the stuff we link to that is not in cmake
  2218. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2219. {
  2220. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2221. cmTargets::iterator tit;
  2222. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2223. {
  2224. const cmTarget::LinkLibraryVectorType* ll
  2225. = &(tit->second.GetOriginalLinkLibraries());
  2226. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2227. const char* realTargetName = tit->first.c_str();
  2228. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2229. {
  2230. // Skip ignored targets
  2231. continue;
  2232. }
  2233. if ( ll->size() > 0 )
  2234. {
  2235. targetDeps[realTargetName] = 1;
  2236. }
  2237. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2238. {
  2239. const char* libName = llit->first.c_str();
  2240. std::map<cmStdString, cmStdString>::iterator tarIt
  2241. = targetNamesNodes.find(libName);
  2242. if ( ignoreTargetsSet.find(libName) != ignoreTargetsSet.end() )
  2243. {
  2244. // Skip ignored targets
  2245. continue;
  2246. }
  2247. if ( tarIt == targetNamesNodes.end() )
  2248. {
  2249. sprintf(tgtName, "%s%d", graphNodePrefix, cnt++);
  2250. targetDeps[libName] = 2;
  2251. targetNamesNodes[libName] = tgtName;
  2252. //str << " \"" << tgtName << "\" [ label=\"" << libName
  2253. //<< "\" shape=\"ellipse\"];" << std::endl;
  2254. }
  2255. else
  2256. {
  2257. std::map<cmStdString, int>::iterator depIt
  2258. = targetDeps.find(libName);
  2259. if ( depIt == targetDeps.end() )
  2260. {
  2261. targetDeps[libName] = 1;
  2262. }
  2263. }
  2264. }
  2265. }
  2266. }
  2267. // Write out nodes
  2268. std::map<cmStdString, int>::iterator depIt;
  2269. for ( depIt = targetDeps.begin(); depIt != targetDeps.end(); ++ depIt )
  2270. {
  2271. const char* newTargetName = depIt->first.c_str();
  2272. std::map<cmStdString, cmStdString>::iterator tarIt
  2273. = targetNamesNodes.find(newTargetName);
  2274. if ( tarIt == targetNamesNodes.end() )
  2275. {
  2276. // We should not be here.
  2277. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2278. << " even though it was added in the previous pass" << std::endl;
  2279. abort();
  2280. }
  2281. str << " \"" << tarIt->second.c_str() << "\" [ label=\""
  2282. << newTargetName << "\" shape=\"";
  2283. if ( depIt->second == 1 )
  2284. {
  2285. std::map<cmStdString, cmTarget*>::iterator tarTypeIt= targetPtrs.find(
  2286. newTargetName);
  2287. if ( tarTypeIt == targetPtrs.end() )
  2288. {
  2289. // We should not be here.
  2290. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2291. << " even though it was added in the previous pass" << std::endl;
  2292. abort();
  2293. }
  2294. cmTarget* tg = tarTypeIt->second;
  2295. switch ( tg->GetType() )
  2296. {
  2297. case cmTarget::EXECUTABLE:
  2298. str << "house";
  2299. break;
  2300. case cmTarget::STATIC_LIBRARY:
  2301. str << "diamond";
  2302. break;
  2303. case cmTarget::SHARED_LIBRARY:
  2304. str << "polygon";
  2305. break;
  2306. case cmTarget::MODULE_LIBRARY:
  2307. str << "octagon";
  2308. break;
  2309. default:
  2310. str << "box";
  2311. }
  2312. }
  2313. else
  2314. {
  2315. str << "ellipse";
  2316. }
  2317. str << "\"];" << std::endl;
  2318. }
  2319. // Now generate the connectivity
  2320. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2321. {
  2322. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2323. cmTargets::iterator tit;
  2324. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2325. {
  2326. std::map<cmStdString, int>::iterator dependIt
  2327. = targetDeps.find(tit->first.c_str());
  2328. if ( dependIt == targetDeps.end() )
  2329. {
  2330. continue;
  2331. }
  2332. std::map<cmStdString, cmStdString>::iterator cmakeTarIt
  2333. = targetNamesNodes.find(tit->first.c_str());
  2334. const cmTarget::LinkLibraryVectorType* ll
  2335. = &(tit->second.GetOriginalLinkLibraries());
  2336. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2337. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2338. {
  2339. const char* libName = llit->first.c_str();
  2340. std::map<cmStdString, cmStdString>::iterator tarIt
  2341. = targetNamesNodes.find(libName);
  2342. if ( tarIt == targetNamesNodes.end() )
  2343. {
  2344. // We should not be here.
  2345. std::cout << __LINE__ << " Cannot find library: " << libName
  2346. << " even though it was added in the previous pass" << std::endl;
  2347. abort();
  2348. }
  2349. str << " \"" << cmakeTarIt->second.c_str() << "\" -> \""
  2350. << tarIt->second.c_str() << "\"" << std::endl;
  2351. }
  2352. }
  2353. }
  2354. // TODO: Use dotted or something for external libraries
  2355. //str << " \"node0\":f4 -> \"node12\"[color=\"#0000ff\" style=dotted]"
  2356. //<< std::endl;
  2357. //
  2358. str << "}" << std::endl;
  2359. }
  2360. //----------------------------------------------------------------------------
  2361. #ifdef CMAKE_BUILD_WITH_CMAKE
  2362. int cmake::ExecuteEchoColor(std::vector<std::string>& args)
  2363. {
  2364. // The arguments are
  2365. // argv[0] == <cmake-executable>
  2366. // argv[1] == cmake_echo_color
  2367. // On some platforms (an MSYS prompt) cmsysTerminal may not be able
  2368. // to determine whether the stream is displayed on a tty. In this
  2369. // case it assumes no unless we tell it otherwise. Since we want
  2370. // color messages to be displayed for users we will assume yes.
  2371. // However, we can test for some situations when the answer is most
  2372. // likely no.
  2373. int assumeTTY = cmsysTerminal_Color_AssumeTTY;
  2374. if(cmSystemTools::GetEnv("DART_TEST_FROM_DART") ||
  2375. cmSystemTools::GetEnv("DASHBOARD_TEST_FROM_CTEST"))
  2376. {
  2377. // Avoid printing color escapes during dashboard builds.
  2378. assumeTTY = 0;
  2379. }
  2380. bool enabled = true;
  2381. int color = cmsysTerminal_Color_Normal;
  2382. bool newline = true;
  2383. for(unsigned int i=2; i < args.size(); ++i)
  2384. {
  2385. if(args[i].find("--switch=") == 0)
  2386. {
  2387. // Enable or disable color based on the switch value.
  2388. std::string value = args[i].substr(9);
  2389. if(!value.empty())
  2390. {
  2391. if(cmSystemTools::IsOn(value.c_str()))
  2392. {
  2393. enabled = true;
  2394. }
  2395. else
  2396. {
  2397. enabled = false;
  2398. }
  2399. }
  2400. }
  2401. else if(args[i] == "--normal")
  2402. {
  2403. color = cmsysTerminal_Color_Normal;
  2404. }
  2405. else if(args[i] == "--black")
  2406. {
  2407. color = cmsysTerminal_Color_ForegroundBlack;
  2408. }
  2409. else if(args[i] == "--red")
  2410. {
  2411. color = cmsysTerminal_Color_ForegroundRed;
  2412. }
  2413. else if(args[i] == "--green")
  2414. {
  2415. color = cmsysTerminal_Color_ForegroundGreen;
  2416. }
  2417. else if(args[i] == "--yellow")
  2418. {
  2419. color = cmsysTerminal_Color_ForegroundYellow;
  2420. }
  2421. else if(args[i] == "--blue")
  2422. {
  2423. color = cmsysTerminal_Color_ForegroundBlue;
  2424. }
  2425. else if(args[i] == "--magenta")
  2426. {
  2427. color = cmsysTerminal_Color_ForegroundMagenta;
  2428. }
  2429. else if(args[i] == "--cyan")
  2430. {
  2431. color = cmsysTerminal_Color_ForegroundCyan;
  2432. }
  2433. else if(args[i] == "--white")
  2434. {
  2435. color = cmsysTerminal_Color_ForegroundWhite;
  2436. }
  2437. else if(args[i] == "--bold")
  2438. {
  2439. color |= cmsysTerminal_Color_ForegroundBold;
  2440. }
  2441. else if(args[i] == "--no-newline")
  2442. {
  2443. newline = false;
  2444. }
  2445. else if(args[i] == "--newline")
  2446. {
  2447. newline = true;
  2448. }
  2449. else if(enabled)
  2450. {
  2451. // Color is enabled. Print with the current color.
  2452. cmsysTerminal_cfprintf(color | assumeTTY, stdout, "%s%s",
  2453. args[i].c_str(), newline? "\n" : "");
  2454. }
  2455. else
  2456. {
  2457. // Color is disabled. Print without color.
  2458. fprintf(stdout, "%s%s", args[i].c_str(), newline? "\n" : "");
  2459. }
  2460. }
  2461. return 0;
  2462. }
  2463. #else
  2464. int cmake::ExecuteEchoColor(std::vector<std::string>&)
  2465. {
  2466. return 1;
  2467. }
  2468. #endif