cmake.cxx 72 KB

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