cmake.cxx 80 KB

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