cmake.cxx 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489
  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. // 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. << " echo_append [string]... - displays arguments as text but no new"
  703. " line\n"
  704. << " environment - display the current enviroment\n"
  705. << " remove file1 file2 ... - remove the file(s)\n"
  706. << " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar.\n"
  707. << " time command [args] ... - run command and return elapsed time\n"
  708. #if defined(_WIN32) && !defined(__CYGWIN__)
  709. << " write_regv key value - write registry value\n"
  710. << " delete_regv key - delete registry value\n"
  711. << " comspec - on windows 9x use this for RunCommand\n"
  712. #else
  713. << " create_symlink old new - create a symbolic link new -> old\n"
  714. #endif
  715. ;
  716. cmSystemTools::Error(errorStream.str().c_str());
  717. }
  718. int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
  719. {
  720. if (args.size() > 1)
  721. {
  722. // Copy file
  723. if (args[1] == "copy" && args.size() == 4)
  724. {
  725. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  726. {
  727. std::cerr << "Error copying file \"" << args[2].c_str()
  728. << "\" to \"" << args[3].c_str() << "\".\n";
  729. return 1;
  730. }
  731. return 0;
  732. }
  733. // Copy file if different.
  734. if (args[1] == "copy_if_different" && args.size() == 4)
  735. {
  736. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(),
  737. args[3].c_str()))
  738. {
  739. std::cerr << "Error copying file (if different) from \""
  740. << args[2].c_str() << "\" to \"" << args[3].c_str()
  741. << "\".\n";
  742. return 1;
  743. }
  744. return 0;
  745. }
  746. // Copy directory content
  747. if (args[1] == "copy_directory" && args.size() == 4)
  748. {
  749. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  750. {
  751. std::cerr << "Error copying directory from \""
  752. << args[2].c_str() << "\" to \"" << args[3].c_str()
  753. << "\".\n";
  754. return 1;
  755. }
  756. return 0;
  757. }
  758. // Compare files
  759. if (args[1] == "compare_files" && args.size() == 4)
  760. {
  761. if(cmSystemTools::FilesDiffer(args[2].c_str(), args[3].c_str()))
  762. {
  763. std::cerr << "Files \""
  764. << args[2].c_str() << "\" to \"" << args[3].c_str()
  765. << "\" are different.\n";
  766. return 1;
  767. }
  768. return 0;
  769. }
  770. // Echo string
  771. else if (args[1] == "echo" )
  772. {
  773. unsigned int cc;
  774. const char* space = "";
  775. for ( cc = 2; cc < args.size(); cc ++ )
  776. {
  777. std::cout << space << args[cc];
  778. space = " ";
  779. }
  780. std::cout << std::endl;
  781. return 0;
  782. }
  783. // Echo string no new line
  784. else if (args[1] == "echo_append" )
  785. {
  786. unsigned int cc;
  787. const char* space = "";
  788. for ( cc = 2; cc < args.size(); cc ++ )
  789. {
  790. std::cout << space << args[cc];
  791. space = " ";
  792. }
  793. return 0;
  794. }
  795. #if defined(CMAKE_BUILD_WITH_CMAKE)
  796. // Command to create a symbolic link. Fails on platforms not
  797. // supporting them.
  798. else if (args[1] == "environment" )
  799. {
  800. std::vector<std::string> env = cmSystemTools::GetEnvironmentVariables();
  801. std::vector<std::string>::iterator it;
  802. for ( it = env.begin(); it != env.end(); ++ it )
  803. {
  804. std::cout << it->c_str() << std::endl;
  805. }
  806. return 0;
  807. }
  808. #endif
  809. // Remove file
  810. else if (args[1] == "remove" && args.size() > 2)
  811. {
  812. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  813. {
  814. if(args[cc] != "-f")
  815. {
  816. if(args[cc] == "\\-f")
  817. {
  818. args[cc] = "-f";
  819. }
  820. cmSystemTools::RemoveFile(args[cc].c_str());
  821. }
  822. }
  823. return 0;
  824. }
  825. // Clock command
  826. else if (args[1] == "time" && args.size() > 2)
  827. {
  828. std::string command = args[2];
  829. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  830. {
  831. command += " ";
  832. command += args[cc];
  833. }
  834. clock_t clock_start, clock_finish;
  835. time_t time_start, time_finish;
  836. time(&time_start);
  837. clock_start = clock();
  838. cmSystemTools::RunSingleCommand(command.c_str());
  839. clock_finish = clock();
  840. time(&time_finish);
  841. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  842. std::cout << "Elapsed time: "
  843. << (long)(time_finish - time_start) << " s. (time)"
  844. << ", "
  845. << (double)(clock_finish - clock_start) / clocks_per_sec
  846. << " s. (clock)"
  847. << "\n";
  848. return 0;
  849. }
  850. // Command to change directory and run a program.
  851. else if (args[1] == "chdir" && args.size() >= 4)
  852. {
  853. std::string directory = args[2];
  854. if(!cmSystemTools::FileExists(directory.c_str()))
  855. {
  856. cmSystemTools::Error("Directory does not exist for chdir command: ",
  857. args[2].c_str());
  858. return 0;
  859. }
  860. std::string command = "\"";
  861. command += args[3];
  862. command += "\"";
  863. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  864. {
  865. command += " \"";
  866. command += args[cc];
  867. command += "\"";
  868. }
  869. int retval = 0;
  870. int timeout = 0;
  871. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  872. directory.c_str(), true, timeout) )
  873. {
  874. return retval;
  875. }
  876. return 1;
  877. }
  878. // Command to create a symbolic link. Fails on platforms not
  879. // supporting them.
  880. else if (args[1] == "create_symlink" && args.size() == 4)
  881. {
  882. return cmSystemTools::CreateSymlink(args[2].c_str(),
  883. args[3].c_str())? 0:1;
  884. }
  885. // Internal CMake shared library support.
  886. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  887. {
  888. int result = 0;
  889. std::string realName = args[2];
  890. std::string soName = args[3];
  891. std::string name = args[4];
  892. if(soName != realName)
  893. {
  894. std::string fname = cmSystemTools::GetFilenameName(realName);
  895. if(cmSystemTools::FileExists(soName.c_str()))
  896. {
  897. cmSystemTools::RemoveFile(soName.c_str());
  898. }
  899. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  900. {
  901. result = 1;
  902. }
  903. }
  904. if(name != soName)
  905. {
  906. std::string fname = cmSystemTools::GetFilenameName(soName);
  907. if(cmSystemTools::FileExists(soName.c_str()))
  908. {
  909. cmSystemTools::RemoveFile(name.c_str());
  910. }
  911. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  912. {
  913. result = 1;
  914. }
  915. }
  916. return result;
  917. }
  918. // Internal CMake versioned executable support.
  919. else if (args[1] == "cmake_symlink_executable" && args.size() == 4)
  920. {
  921. int result = 0;
  922. std::string realName = args[2];
  923. std::string name = args[3];
  924. if(name != realName)
  925. {
  926. std::string fname = cmSystemTools::GetFilenameName(realName);
  927. if(cmSystemTools::FileExists(realName.c_str()))
  928. {
  929. cmSystemTools::RemoveFile(name.c_str());
  930. }
  931. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  932. {
  933. result = 1;
  934. }
  935. }
  936. return result;
  937. }
  938. // Internal CMake dependency scanning support.
  939. else if (args[1] == "cmake_depends" && args.size() >= 6)
  940. {
  941. cmake cm;
  942. cmGlobalGenerator *ggd = cm.CreateGlobalGenerator(args[2].c_str());
  943. if (ggd)
  944. {
  945. ggd->SetCMakeInstance(&cm);
  946. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  947. lgd->SetGlobalGenerator(ggd);
  948. return lgd->ScanDependencies(args)? 0 : 2;
  949. }
  950. return 1;
  951. }
  952. #ifdef CMAKE_BUILD_WITH_CMAKE
  953. // Internal CMake color makefile support.
  954. else if (args[1] == "cmake_echo_color")
  955. {
  956. return cmake::ExecuteEchoColor(args);
  957. }
  958. #endif
  959. // Tar files
  960. else if (args[1] == "tar" && args.size() > 3)
  961. {
  962. std::string flags = args[2];
  963. std::string outFile = args[3];
  964. std::vector<cmStdString> files;
  965. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  966. {
  967. files.push_back(args[cc]);
  968. }
  969. bool gzip = false;
  970. bool verbose = false;
  971. if ( flags.find_first_of('z') != flags.npos )
  972. {
  973. gzip = true;
  974. }
  975. if ( flags.find_first_of('v') != flags.npos )
  976. {
  977. verbose = true;
  978. }
  979. if ( flags.find_first_of('t') != flags.npos )
  980. {
  981. if ( !cmSystemTools::ListTar(outFile.c_str(), files, gzip, verbose) )
  982. {
  983. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  984. return 1;
  985. }
  986. }
  987. else if ( flags.find_first_of('c') != flags.npos )
  988. {
  989. if ( !cmSystemTools::CreateTar(
  990. outFile.c_str(), files, gzip, verbose) )
  991. {
  992. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  993. return 1;
  994. }
  995. }
  996. else if ( flags.find_first_of('x') != flags.npos )
  997. {
  998. if ( !cmSystemTools::ExtractTar(
  999. outFile.c_str(), files, gzip, verbose) )
  1000. {
  1001. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  1002. return 1;
  1003. }
  1004. }
  1005. return 0;
  1006. }
  1007. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1008. // Internal CMake Fortran module support.
  1009. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  1010. {
  1011. return cmDependsFortran::CopyModule(args)? 0 : 1;
  1012. }
  1013. #endif
  1014. #if defined(_WIN32) && !defined(__CYGWIN__)
  1015. // Write registry value
  1016. else if (args[1] == "write_regv" && args.size() > 3)
  1017. {
  1018. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  1019. args[3].c_str()) ? 0 : 1;
  1020. }
  1021. // Delete registry value
  1022. else if (args[1] == "delete_regv" && args.size() > 2)
  1023. {
  1024. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  1025. }
  1026. // Remove file
  1027. else if (args[1] == "comspec" && args.size() > 2)
  1028. {
  1029. unsigned int cc;
  1030. std::string command = args[2];
  1031. for ( cc = 3; cc < args.size(); cc ++ )
  1032. {
  1033. command += " " + args[cc];
  1034. }
  1035. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  1036. }
  1037. #endif
  1038. }
  1039. ::CMakeCommandUsage(args[0].c_str());
  1040. return 1;
  1041. }
  1042. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  1043. {
  1044. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1045. i != this->Generators.end(); ++i)
  1046. {
  1047. names.push_back(i->first);
  1048. }
  1049. }
  1050. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  1051. {
  1052. RegisteredGeneratorsMap::const_iterator i = this->Generators.find(name);
  1053. if(i != this->Generators.end())
  1054. {
  1055. cmGlobalGenerator* generator = (i->second)();
  1056. generator->SetCMakeInstance(this);
  1057. return generator;
  1058. }
  1059. else
  1060. {
  1061. return 0;
  1062. }
  1063. }
  1064. void cmake::SetHomeDirectory(const char* dir)
  1065. {
  1066. this->cmHomeDirectory = dir;
  1067. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1068. }
  1069. void cmake::SetHomeOutputDirectory(const char* lib)
  1070. {
  1071. this->HomeOutputDirectory = lib;
  1072. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1073. }
  1074. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  1075. {
  1076. // delete the old generator
  1077. if (this->GlobalGenerator)
  1078. {
  1079. delete this->GlobalGenerator;
  1080. // restore the original environment variables CXX and CC
  1081. // Restor CC
  1082. std::string env = "CC=";
  1083. if(this->CCEnvironment.size())
  1084. {
  1085. env += this->CCEnvironment;
  1086. }
  1087. cmSystemTools::PutEnv(env.c_str());
  1088. env = "CXX=";
  1089. if(this->CXXEnvironment.size())
  1090. {
  1091. env += this->CXXEnvironment;
  1092. }
  1093. cmSystemTools::PutEnv(env.c_str());
  1094. }
  1095. // set the new
  1096. this->GlobalGenerator = gg;
  1097. // set the global flag for unix style paths on cmSystemTools as soon as
  1098. // the generator is set. This allows gmake to be used on windows.
  1099. cmSystemTools::SetForceUnixPaths
  1100. (this->GlobalGenerator->GetForceUnixPaths());
  1101. // Save the environment variables CXX and CC
  1102. const char* cxx = getenv("CXX");
  1103. const char* cc = getenv("CC");
  1104. if(cxx)
  1105. {
  1106. this->CXXEnvironment = cxx;
  1107. }
  1108. else
  1109. {
  1110. this->CXXEnvironment = "";
  1111. }
  1112. if(cc)
  1113. {
  1114. this->CCEnvironment = cc;
  1115. }
  1116. else
  1117. {
  1118. this->CCEnvironment = "";
  1119. }
  1120. // set the cmake instance just to be sure
  1121. gg->SetCMakeInstance(this);
  1122. }
  1123. int cmake::DoPreConfigureChecks()
  1124. {
  1125. // Make sure the Start directory contains a CMakeLists.txt file.
  1126. std::string srcList = this->GetHomeDirectory();
  1127. srcList += "/CMakeLists.txt";
  1128. if(!cmSystemTools::FileExists(srcList.c_str()))
  1129. {
  1130. cmOStringStream err;
  1131. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1132. {
  1133. err << "The source directory \"" << this->GetHomeDirectory()
  1134. << "\" does not appear to contain CMakeLists.txt.\n";
  1135. }
  1136. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1137. {
  1138. err << "The source directory \"" << this->GetHomeDirectory()
  1139. << "\" is a file, not a directory.\n";
  1140. }
  1141. else
  1142. {
  1143. err << "The source directory \"" << this->GetHomeDirectory()
  1144. << "\" does not exist.\n";
  1145. }
  1146. err << "Specify --help for usage, or press the help button on the CMake "
  1147. "GUI.";
  1148. cmSystemTools::Error(err.str().c_str());
  1149. return -2;
  1150. }
  1151. // do a sanity check on some values
  1152. if(this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1153. {
  1154. std::string cacheStart =
  1155. this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1156. cacheStart += "/CMakeLists.txt";
  1157. std::string currentStart = this->GetHomeDirectory();
  1158. currentStart += "/CMakeLists.txt";
  1159. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1160. {
  1161. std::string message = "The source \"";
  1162. message += currentStart;
  1163. message += "\" does not match the source \"";
  1164. message += cacheStart;
  1165. message += "\" used to generate cache. ";
  1166. message += "Re-run cmake with a different source directory.";
  1167. cmSystemTools::Error(message.c_str());
  1168. return -2;
  1169. }
  1170. }
  1171. else
  1172. {
  1173. return 0;
  1174. }
  1175. return 1;
  1176. }
  1177. int cmake::Configure()
  1178. {
  1179. // Construct right now our path conversion table before it's too late:
  1180. this->UpdateConversionPathTable();
  1181. this->CleanupCommandsAndMacros();
  1182. int res = 0;
  1183. if ( !this->ScriptMode )
  1184. {
  1185. res = this->DoPreConfigureChecks();
  1186. }
  1187. if ( res < 0 )
  1188. {
  1189. return -2;
  1190. }
  1191. if ( !res )
  1192. {
  1193. this->CacheManager->AddCacheEntry
  1194. ("CMAKE_HOME_DIRECTORY",
  1195. this->GetHomeDirectory(),
  1196. "Start directory with the top level CMakeLists.txt file for this "
  1197. "project",
  1198. cmCacheManager::INTERNAL);
  1199. }
  1200. // set the default BACKWARDS compatibility to the current version
  1201. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1202. {
  1203. char ver[256];
  1204. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1205. cmMakefile::GetMinorVersion());
  1206. this->CacheManager->AddCacheEntry
  1207. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1208. "For backwards compatibility, what version of CMake commands and "
  1209. "syntax should this version of CMake allow.",
  1210. cmCacheManager::STRING);
  1211. }
  1212. // no generator specified on the command line
  1213. if(!this->GlobalGenerator)
  1214. {
  1215. const char* genName =
  1216. this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1217. if(genName)
  1218. {
  1219. this->GlobalGenerator = this->CreateGlobalGenerator(genName);
  1220. }
  1221. if(this->GlobalGenerator)
  1222. {
  1223. // set the global flag for unix style paths on cmSystemTools as
  1224. // soon as the generator is set. This allows gmake to be used
  1225. // on windows.
  1226. cmSystemTools::SetForceUnixPaths
  1227. (this->GlobalGenerator->GetForceUnixPaths());
  1228. }
  1229. else
  1230. {
  1231. #if defined(__BORLANDC__) && defined(_WIN32)
  1232. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1233. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1234. std::string installedCompiler;
  1235. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1236. "\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  1237. cmSystemTools::ExpandRegistryValues(mp);
  1238. if (!(mp == "/registry"))
  1239. {
  1240. installedCompiler = "Visual Studio 8 2005";
  1241. }
  1242. else
  1243. {
  1244. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1245. "\\VisualStudio\\7.1;InstallDir]";
  1246. cmSystemTools::ExpandRegistryValues(mp);
  1247. if (!(mp == "/registry"))
  1248. {
  1249. installedCompiler = "Visual Studio 7 .NET 2003";
  1250. }
  1251. else
  1252. {
  1253. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1254. "\\VisualStudio\\7.0;InstallDir]";
  1255. cmSystemTools::ExpandRegistryValues(mp);
  1256. if (!(mp == "/registry"))
  1257. {
  1258. installedCompiler = "Visual Studio 7";
  1259. }
  1260. else
  1261. {
  1262. installedCompiler = "Visual Studio 6";
  1263. }
  1264. }
  1265. }
  1266. cmGlobalGenerator* gen
  1267. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1268. if(!gen)
  1269. {
  1270. gen = new cmGlobalNMakeMakefileGenerator;
  1271. }
  1272. this->SetGlobalGenerator(gen);
  1273. #else
  1274. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1275. #endif
  1276. }
  1277. if(!this->GlobalGenerator)
  1278. {
  1279. cmSystemTools::Error("Could not create generator");
  1280. return -1;
  1281. }
  1282. }
  1283. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1284. if(genName)
  1285. {
  1286. if(strcmp(this->GlobalGenerator->GetName(), genName) != 0)
  1287. {
  1288. std::string message = "Error: generator : ";
  1289. message += this->GlobalGenerator->GetName();
  1290. message += "\nDoes not match the generator used previously: ";
  1291. message += genName;
  1292. message +=
  1293. "\nEither remove the CMakeCache.txt file or choose a different"
  1294. " binary directory.";
  1295. cmSystemTools::Error(message.c_str());
  1296. return -2;
  1297. }
  1298. }
  1299. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1300. {
  1301. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1302. this->GlobalGenerator->GetName(),
  1303. "Name of generator.",
  1304. cmCacheManager::INTERNAL);
  1305. }
  1306. // reset any system configuration information, except for when we are
  1307. // InTryCompile. With TryCompile the system info is taken from the parent's
  1308. // info to save time
  1309. if (!this->InTryCompile)
  1310. {
  1311. this->GlobalGenerator->ClearEnabledLanguages();
  1312. }
  1313. this->CleanupWrittenFiles();
  1314. // Truncate log files
  1315. if (!this->InTryCompile)
  1316. {
  1317. this->TruncateOutputLog("CMakeOutput.log");
  1318. this->TruncateOutputLog("CMakeError.log");
  1319. }
  1320. // actually do the configure
  1321. this->GlobalGenerator->Configure();
  1322. // Before saving the cache
  1323. // if the project did not define one of the entries below, add them now
  1324. // so users can edit the values in the cache:
  1325. // LIBRARY_OUTPUT_PATH
  1326. // EXECUTABLE_OUTPUT_PATH
  1327. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1328. {
  1329. this->CacheManager->AddCacheEntry
  1330. ("LIBRARY_OUTPUT_PATH", "",
  1331. "Single output directory for building all libraries.",
  1332. cmCacheManager::PATH);
  1333. }
  1334. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1335. {
  1336. this->CacheManager->AddCacheEntry
  1337. ("EXECUTABLE_OUTPUT_PATH", "",
  1338. "Single output directory for building all executables.",
  1339. cmCacheManager::PATH);
  1340. }
  1341. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1342. {
  1343. this->CacheManager->AddCacheEntry
  1344. ("CMAKE_USE_RELATIVE_PATHS", false,
  1345. "If true, cmake will use relative paths in makefiles and projects.");
  1346. cmCacheManager::CacheIterator it =
  1347. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1348. if ( !it.PropertyExists("ADVANCED") )
  1349. {
  1350. it.SetProperty("ADVANCED", "1");
  1351. }
  1352. }
  1353. if(cmSystemTools::GetFatalErrorOccured() &&
  1354. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1355. cmSystemTools::IsOff(this->CacheManager->
  1356. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1357. {
  1358. // We must have a bad generator selection. Wipe the cache entry so the
  1359. // user can select another.
  1360. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1361. }
  1362. // only save the cache if there were no fatal errors
  1363. if ( !this->ScriptMode )
  1364. {
  1365. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1366. }
  1367. if ( !this->GraphVizFile.empty() )
  1368. {
  1369. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1370. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1371. }
  1372. if(cmSystemTools::GetErrorOccuredFlag())
  1373. {
  1374. return -1;
  1375. }
  1376. return 0;
  1377. }
  1378. bool cmake::CacheVersionMatches()
  1379. {
  1380. const char* majv =
  1381. this->CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1382. const char* minv =
  1383. this->CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1384. const char* relv =
  1385. this->CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1386. bool cacheSameCMake = false;
  1387. if(majv &&
  1388. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  1389. && minv &&
  1390. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  1391. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  1392. {
  1393. cacheSameCMake = true;
  1394. }
  1395. return cacheSameCMake;
  1396. }
  1397. void cmake::PreLoadCMakeFiles()
  1398. {
  1399. std::string pre_load = this->GetHomeDirectory();
  1400. if ( pre_load.size() > 0 )
  1401. {
  1402. pre_load += "/PreLoad.cmake";
  1403. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1404. {
  1405. this->ReadListFile(pre_load.c_str());
  1406. }
  1407. }
  1408. pre_load = this->GetHomeOutputDirectory();
  1409. if ( pre_load.size() > 0 )
  1410. {
  1411. pre_load += "/PreLoad.cmake";
  1412. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1413. {
  1414. this->ReadListFile(pre_load.c_str());
  1415. }
  1416. }
  1417. }
  1418. // handle a command line invocation
  1419. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1420. {
  1421. // Process the arguments
  1422. this->SetArgs(args);
  1423. if(cmSystemTools::GetErrorOccuredFlag())
  1424. {
  1425. CMakeCommandUsage(args[0].c_str());
  1426. return -1;
  1427. }
  1428. // set the cmake command
  1429. this->CMakeCommand = args[0];
  1430. if ( !this->ScriptMode )
  1431. {
  1432. // load the cache
  1433. if(this->LoadCache() < 0)
  1434. {
  1435. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1436. return -1;
  1437. }
  1438. }
  1439. else
  1440. {
  1441. this->AddCMakePaths(this->CMakeCommand.c_str());
  1442. }
  1443. // Add any cache args
  1444. if ( !this->SetCacheArgs(args) )
  1445. {
  1446. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1447. return -1;
  1448. }
  1449. // In script mode we terminate after running the script.
  1450. if(this->ScriptMode)
  1451. {
  1452. if(cmSystemTools::GetErrorOccuredFlag())
  1453. {
  1454. return -1;
  1455. }
  1456. else
  1457. {
  1458. return 0;
  1459. }
  1460. }
  1461. this->PreLoadCMakeFiles();
  1462. std::string systemFile = this->GetHomeOutputDirectory();
  1463. systemFile += "/CMakeSystem.cmake";
  1464. if ( noconfigure )
  1465. {
  1466. return 0;
  1467. }
  1468. // now run the global generate
  1469. // Check the state of the build system to see if we need to regenerate.
  1470. if(!this->CheckBuildSystem())
  1471. {
  1472. return 0;
  1473. }
  1474. // If we are doing global generate, we better set start and start
  1475. // output directory to the root of the project.
  1476. std::string oldstartdir = this->GetStartDirectory();
  1477. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1478. this->SetStartDirectory(this->GetHomeDirectory());
  1479. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1480. int ret = this->Configure();
  1481. if (ret || this->ScriptMode)
  1482. {
  1483. return ret;
  1484. }
  1485. ret = this->Generate();
  1486. std::string message = "Build files have been written to: ";
  1487. message += this->GetHomeOutputDirectory();
  1488. this->UpdateProgress(message.c_str(), -1);
  1489. if(ret)
  1490. {
  1491. return ret;
  1492. }
  1493. this->SetStartDirectory(oldstartdir.c_str());
  1494. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1495. return ret;
  1496. }
  1497. int cmake::Generate()
  1498. {
  1499. if(!this->GlobalGenerator)
  1500. {
  1501. return -1;
  1502. }
  1503. this->GlobalGenerator->Generate();
  1504. if(cmSystemTools::GetErrorOccuredFlag())
  1505. {
  1506. return -1;
  1507. }
  1508. return 0;
  1509. }
  1510. unsigned int cmake::GetMajorVersion()
  1511. {
  1512. return cmMakefile::GetMajorVersion();
  1513. }
  1514. unsigned int cmake::GetMinorVersion()
  1515. {
  1516. return cmMakefile::GetMinorVersion();
  1517. }
  1518. const char *cmake::GetReleaseVersion()
  1519. {
  1520. return cmMakefile::GetReleaseVersion();
  1521. }
  1522. void cmake::AddCacheEntry(const char* key, const char* value,
  1523. const char* helpString,
  1524. int type)
  1525. {
  1526. this->CacheManager->AddCacheEntry(key, value,
  1527. helpString,
  1528. cmCacheManager::CacheEntryType(type));
  1529. }
  1530. const char* cmake::GetCacheDefinition(const char* name) const
  1531. {
  1532. return this->CacheManager->GetCacheValue(name);
  1533. }
  1534. int cmake::DumpDocumentationToFile(std::ostream& f)
  1535. {
  1536. #ifdef CMAKE_BUILD_WITH_CMAKE
  1537. // Loop over all registered commands and print out documentation
  1538. const char *name;
  1539. const char *terse;
  1540. const char *full;
  1541. char tmp[1024];
  1542. sprintf(tmp,"Version %d.%d (%s)", cmake::GetMajorVersion(),
  1543. cmake::GetMinorVersion(), cmVersion::GetReleaseVersion().c_str());
  1544. f << "<html>\n";
  1545. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1546. f << "<ul>\n";
  1547. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  1548. j != this->Commands.end(); ++j)
  1549. {
  1550. name = (*j).second->GetName();
  1551. terse = (*j).second->GetTerseDocumentation();
  1552. full = (*j).second->GetFullDocumentation();
  1553. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1554. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1555. }
  1556. f << "</ul></html>\n";
  1557. #else
  1558. (void)f;
  1559. #endif
  1560. return 1;
  1561. }
  1562. void cmake::AddDefaultCommands()
  1563. {
  1564. std::list<cmCommand*> commands;
  1565. GetBootstrapCommands(commands);
  1566. GetPredefinedCommands(commands);
  1567. for(std::list<cmCommand*>::iterator i = commands.begin();
  1568. i != commands.end(); ++i)
  1569. {
  1570. this->AddCommand(*i);
  1571. }
  1572. }
  1573. void cmake::AddDefaultGenerators()
  1574. {
  1575. #if defined(_WIN32) && !defined(__CYGWIN__)
  1576. # if !defined(CMAKE_BOOT_MINGW)
  1577. this->Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1578. &cmGlobalVisualStudio6Generator::New;
  1579. this->Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1580. &cmGlobalVisualStudio7Generator::New;
  1581. this->Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1582. &cmGlobalVisualStudio71Generator::New;
  1583. this->Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  1584. &cmGlobalVisualStudio8Generator::New;
  1585. this->Generators[cmGlobalVisualStudio8Win64Generator::GetActualName()] =
  1586. &cmGlobalVisualStudio8Win64Generator::New;
  1587. this->Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1588. &cmGlobalBorlandMakefileGenerator::New;
  1589. this->Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1590. &cmGlobalNMakeMakefileGenerator::New;
  1591. this->Generators[cmGlobalWatcomWMakeGenerator::GetActualName()] =
  1592. &cmGlobalWatcomWMakeGenerator::New;
  1593. # endif
  1594. this->Generators[cmGlobalMSYSMakefileGenerator::GetActualName()] =
  1595. &cmGlobalMSYSMakefileGenerator::New;
  1596. this->Generators[cmGlobalMinGWMakefileGenerator::GetActualName()] =
  1597. &cmGlobalMinGWMakefileGenerator::New;
  1598. #endif
  1599. this->Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
  1600. &cmGlobalUnixMakefileGenerator3::New;
  1601. #ifdef CMAKE_USE_XCODE
  1602. this->Generators[cmGlobalXCodeGenerator::GetActualName()] =
  1603. &cmGlobalXCodeGenerator::New;
  1604. #endif
  1605. #ifdef CMAKE_USE_KDEVELOP
  1606. this->Generators[cmGlobalKdevelopGenerator::GetActualName()] =
  1607. &cmGlobalKdevelopGenerator::New;
  1608. #endif
  1609. }
  1610. int cmake::LoadCache()
  1611. {
  1612. // could we not read the cache
  1613. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  1614. {
  1615. // if it does exist, but isn;t readable then warn the user
  1616. std::string cacheFile = this->GetHomeOutputDirectory();
  1617. cacheFile += "/CMakeCache.txt";
  1618. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1619. {
  1620. cmSystemTools::Error(
  1621. "There is a CMakeCache.txt file for the current binary tree but "
  1622. "cmake does not have permission to read it. Please check the "
  1623. "permissions of the directory you are trying to run CMake on.");
  1624. return -1;
  1625. }
  1626. }
  1627. if (this->CMakeCommand.size() < 2)
  1628. {
  1629. cmSystemTools::Error(
  1630. "cmake command was not specified prior to loading the cache in "
  1631. "cmake.cxx");
  1632. return -1;
  1633. }
  1634. // setup CMAKE_ROOT and CMAKE_COMMAND
  1635. if(!this->AddCMakePaths(this->CMakeCommand.c_str()))
  1636. {
  1637. return -3;
  1638. }
  1639. // set the default BACKWARDS compatibility to the current version
  1640. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1641. {
  1642. char ver[256];
  1643. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1644. cmMakefile::GetMinorVersion());
  1645. this->CacheManager->AddCacheEntry
  1646. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1647. "For backwards compatibility, what version of CMake commands and "
  1648. "syntax should this version of CMake allow.",
  1649. cmCacheManager::STRING);
  1650. }
  1651. return 0;
  1652. }
  1653. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  1654. {
  1655. this->ProgressCallback = f;
  1656. this->ProgressCallbackClientData = cd;
  1657. }
  1658. void cmake::UpdateProgress(const char *msg, float prog)
  1659. {
  1660. if(this->ProgressCallback && !this->InTryCompile)
  1661. {
  1662. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  1663. return;
  1664. }
  1665. }
  1666. void cmake::GetCommandDocumentation(
  1667. std::vector<cmDocumentationEntry>& v) const
  1668. {
  1669. for(RegisteredCommandsMap::const_iterator j = this->Commands.begin();
  1670. j != this->Commands.end(); ++j)
  1671. {
  1672. cmDocumentationEntry e =
  1673. {
  1674. (*j).second->GetName(),
  1675. (*j).second->GetTerseDocumentation(),
  1676. (*j).second->GetFullDocumentation()
  1677. };
  1678. v.push_back(e);
  1679. }
  1680. cmDocumentationEntry empty = {0,0,0};
  1681. v.push_back(empty);
  1682. }
  1683. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1684. {
  1685. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1686. i != this->Generators.end(); ++i)
  1687. {
  1688. cmDocumentationEntry e;
  1689. cmGlobalGenerator* generator = (i->second)();
  1690. generator->GetDocumentation(e);
  1691. delete generator;
  1692. v.push_back(e);
  1693. }
  1694. cmDocumentationEntry empty = {0,0,0};
  1695. v.push_back(empty);
  1696. }
  1697. void cmake::AddWrittenFile(const char* file)
  1698. {
  1699. this->WrittenFiles.insert(file);
  1700. }
  1701. bool cmake::HasWrittenFile(const char* file)
  1702. {
  1703. return this->WrittenFiles.find(file) != this->WrittenFiles.end();
  1704. }
  1705. void cmake::CleanupWrittenFiles()
  1706. {
  1707. this->WrittenFiles.clear();
  1708. }
  1709. void cmake::UpdateConversionPathTable()
  1710. {
  1711. // Update the path conversion table with any specified file:
  1712. const char* tablepath =
  1713. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1714. if(tablepath)
  1715. {
  1716. std::ifstream table( tablepath );
  1717. if(!table)
  1718. {
  1719. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  1720. ". CMake can not open file.");
  1721. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1722. }
  1723. else
  1724. {
  1725. std::string a, b;
  1726. while(!table.eof())
  1727. {
  1728. // two entries per line
  1729. table >> a; table >> b;
  1730. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1731. }
  1732. }
  1733. }
  1734. }
  1735. //----------------------------------------------------------------------------
  1736. int cmake::CheckBuildSystem()
  1737. {
  1738. // We do not need to rerun CMake. Check dependency integrity. Use
  1739. // the make system's VERBOSE environment variable to enable verbose
  1740. // output.
  1741. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  1742. // This method will check the integrity of the build system if the
  1743. // option was given on the command line. It reads the given file to
  1744. // determine whether CMake should rerun. If it does rerun then the
  1745. // generation step will check the integrity of dependencies. If it
  1746. // does not then we need to check the integrity here.
  1747. // If no file is provided for the check, we have to rerun.
  1748. if(this->CheckBuildSystemArgument.size() == 0)
  1749. {
  1750. if(verbose)
  1751. {
  1752. cmOStringStream msg;
  1753. msg << "Re-run cmake no build system arguments\n";
  1754. cmSystemTools::Stdout(msg.str().c_str());
  1755. }
  1756. return 1;
  1757. }
  1758. // If the file provided does not exist, we have to rerun.
  1759. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  1760. {
  1761. if(verbose)
  1762. {
  1763. cmOStringStream msg;
  1764. msg << "Re-run cmake missing file: "
  1765. << this->CheckBuildSystemArgument.c_str() << "\n";
  1766. cmSystemTools::Stdout(msg.str().c_str());
  1767. }
  1768. return 1;
  1769. }
  1770. // Read the rerun check file and use it to decide whether to do the
  1771. // global generate.
  1772. cmake cm;
  1773. cmGlobalGenerator gg;
  1774. gg.SetCMakeInstance(&cm);
  1775. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1776. lg->SetGlobalGenerator(&gg);
  1777. cmMakefile* mf = lg->GetMakefile();
  1778. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  1779. cmSystemTools::GetErrorOccuredFlag())
  1780. {
  1781. if(verbose)
  1782. {
  1783. cmOStringStream msg;
  1784. msg << "Re-run cmake error reading : "
  1785. << this->CheckBuildSystemArgument.c_str() << "\n";
  1786. cmSystemTools::Stdout(msg.str().c_str());
  1787. }
  1788. // There was an error reading the file. Just rerun.
  1789. return 1;
  1790. }
  1791. // Get the set of dependencies and outputs.
  1792. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1793. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1794. if(!dependsStr || !outputsStr)
  1795. {
  1796. // Not enough information was provided to do the test. Just rerun.
  1797. if(verbose)
  1798. {
  1799. cmOStringStream msg;
  1800. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  1801. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  1802. cmSystemTools::Stdout(msg.str().c_str());
  1803. }
  1804. return 1;
  1805. }
  1806. std::vector<std::string> depends;
  1807. std::vector<std::string> outputs;
  1808. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1809. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1810. // If any output is older than any dependency then rerun.
  1811. for(std::vector<std::string>::iterator dep = depends.begin();
  1812. dep != depends.end(); ++dep)
  1813. {
  1814. for(std::vector<std::string>::iterator out = outputs.begin();
  1815. out != outputs.end(); ++out)
  1816. {
  1817. int result = 0;
  1818. if(!this->FileComparison->FileTimeCompare(out->c_str(),
  1819. dep->c_str(), &result) ||
  1820. result < 0)
  1821. {
  1822. if(verbose)
  1823. {
  1824. cmOStringStream msg;
  1825. msg << "Re-run cmake file: " << out->c_str()
  1826. << " older than: " << dep->c_str() << "\n";
  1827. cmSystemTools::Stdout(msg.str().c_str());
  1828. }
  1829. return 1;
  1830. }
  1831. }
  1832. }
  1833. // compute depends based on the generator specified
  1834. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1835. if (!genName || genName[0] == '\0')
  1836. {
  1837. genName = "Unix Makefiles";
  1838. }
  1839. cmGlobalGenerator *ggd = this->CreateGlobalGenerator(genName);
  1840. if (ggd)
  1841. {
  1842. // Check the dependencies in case source files were removed.
  1843. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1844. lgd->SetGlobalGenerator(ggd);
  1845. lgd->CheckDependencies(mf, verbose, this->ClearBuildSystem);
  1846. // Check for multiple output pairs.
  1847. ggd->CheckMultipleOutputs(mf, verbose);
  1848. }
  1849. // No need to rerun.
  1850. return 0;
  1851. }
  1852. //----------------------------------------------------------------------------
  1853. void cmake::TruncateOutputLog(const char* fname)
  1854. {
  1855. std::string fullPath = this->GetHomeOutputDirectory();
  1856. fullPath += "/";
  1857. fullPath += fname;
  1858. struct stat st;
  1859. if ( ::stat(fullPath.c_str(), &st) )
  1860. {
  1861. return;
  1862. }
  1863. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  1864. {
  1865. cmSystemTools::RemoveFile(fullPath.c_str());
  1866. return;
  1867. }
  1868. size_t fsize = st.st_size;
  1869. const size_t maxFileSize = 50 * 1024;
  1870. if ( fsize < maxFileSize )
  1871. {
  1872. //TODO: truncate file
  1873. return;
  1874. }
  1875. }
  1876. inline std::string removeQuotes(const std::string& s)
  1877. {
  1878. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1879. {
  1880. return s.substr(1, s.size()-2);
  1881. }
  1882. return s;
  1883. }
  1884. const char* cmake::GetCTestCommand()
  1885. {
  1886. if ( !this->CTestCommand.empty() )
  1887. {
  1888. return this->CTestCommand.c_str();
  1889. }
  1890. cmMakefile* mf
  1891. = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  1892. #ifdef CMAKE_BUILD_WITH_CMAKE
  1893. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1894. this->CTestCommand = removeQuotes(this->CTestCommand);
  1895. this->CTestCommand =
  1896. cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  1897. this->CTestCommand += "/";
  1898. this->CTestCommand += "ctest";
  1899. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1900. if(!cmSystemTools::FileExists(this->CTestCommand.c_str()))
  1901. {
  1902. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1903. this->CTestCommand =
  1904. cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  1905. this->CTestCommand += "/Debug/";
  1906. this->CTestCommand += "ctest";
  1907. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1908. }
  1909. if(!cmSystemTools::FileExists(this->CTestCommand.c_str()))
  1910. {
  1911. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1912. this->CTestCommand =
  1913. cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  1914. this->CTestCommand += "/Release/";
  1915. this->CTestCommand += "ctest";
  1916. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1917. }
  1918. #else
  1919. // Only for bootstrap
  1920. this->CTestCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  1921. this->CTestCommand += "/ctest";
  1922. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1923. #endif
  1924. if ( this->CTestCommand.empty() )
  1925. {
  1926. cmSystemTools::Error("Cannot find the CTest executable");
  1927. this->CTestCommand = "CTEST-COMMAND-NOT-FOUND";
  1928. }
  1929. return this->CTestCommand.c_str();
  1930. }
  1931. const char* cmake::GetCPackCommand()
  1932. {
  1933. if ( !this->CPackCommand.empty() )
  1934. {
  1935. return this->CPackCommand.c_str();
  1936. }
  1937. cmMakefile* mf
  1938. = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  1939. #ifdef CMAKE_BUILD_WITH_CMAKE
  1940. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1941. this->CPackCommand = removeQuotes(this->CPackCommand);
  1942. this->CPackCommand =
  1943. cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  1944. this->CPackCommand += "/";
  1945. this->CPackCommand += "cpack";
  1946. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1947. if(!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  1948. {
  1949. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1950. this->CPackCommand =
  1951. cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  1952. this->CPackCommand += "/Debug/";
  1953. this->CPackCommand += "cpack";
  1954. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1955. }
  1956. if(!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  1957. {
  1958. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1959. this->CPackCommand =
  1960. cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  1961. this->CPackCommand += "/Release/";
  1962. this->CPackCommand += "cpack";
  1963. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1964. }
  1965. if (!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  1966. {
  1967. cmSystemTools::Error("Cannot find the CPack executable");
  1968. this->CPackCommand = "CPACK-COMMAND-NOT-FOUND";
  1969. }
  1970. #else
  1971. // Only for bootstrap
  1972. this->CPackCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  1973. this->CPackCommand += "/cpack";
  1974. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1975. #endif
  1976. return this->CPackCommand.c_str();
  1977. }
  1978. void cmake::GenerateGraphViz(const char* fileName)
  1979. {
  1980. cmGeneratedFileStream str(fileName);
  1981. if ( !str )
  1982. {
  1983. return;
  1984. }
  1985. cmake cm;
  1986. cmGlobalGenerator ggi;
  1987. ggi.SetCMakeInstance(&cm);
  1988. std::auto_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator());
  1989. lg->SetGlobalGenerator(&ggi);
  1990. cmMakefile *mf = lg->GetMakefile();
  1991. std::string infile = this->GetHomeOutputDirectory();
  1992. infile += "/CMakeGraphVizOptions.cmake";
  1993. if ( !cmSystemTools::FileExists(infile.c_str()) )
  1994. {
  1995. infile = this->GetHomeDirectory();
  1996. infile += "/CMakeGraphVizOptions.cmake";
  1997. if ( !cmSystemTools::FileExists(infile.c_str()) )
  1998. {
  1999. infile = "";
  2000. }
  2001. }
  2002. if ( !infile.empty() )
  2003. {
  2004. if ( !mf->ReadListFile(0, infile.c_str()) )
  2005. {
  2006. cmSystemTools::Error("Problem opening GraphViz options file: ",
  2007. infile.c_str());
  2008. return;
  2009. }
  2010. std::cout << "Read GraphViz options file: " << infile.c_str()
  2011. << std::endl;
  2012. }
  2013. #define __set_if_not_set(var, value, cmakeDefinition) \
  2014. const char* var = mf->GetDefinition(cmakeDefinition); \
  2015. if ( !var ) \
  2016. { \
  2017. var = value; \
  2018. }
  2019. __set_if_not_set(graphType, "digraph", "GRAPHVIZ_GRAPH_TYPE");
  2020. __set_if_not_set(graphName, "GG", "GRAPHVIZ_GRAPH_NAME");
  2021. __set_if_not_set(graphHeader, "node [\n fontsize = \"12\"\n];",
  2022. "GRAPHVIZ_GRAPH_HEADER");
  2023. __set_if_not_set(graphNodePrefix, "node", "GRAPHVIZ_NODE_PREFIX");
  2024. const char* ignoreTargets = mf->GetDefinition("GRAPHVIZ_IGNORE_TARGETS");
  2025. std::set<cmStdString> ignoreTargetsSet;
  2026. if ( ignoreTargets )
  2027. {
  2028. std::vector<std::string> ignoreTargetsVector;
  2029. cmSystemTools::ExpandListArgument(ignoreTargets,ignoreTargetsVector);
  2030. std::vector<std::string>::iterator itvIt;
  2031. for ( itvIt = ignoreTargetsVector.begin();
  2032. itvIt != ignoreTargetsVector.end();
  2033. ++ itvIt )
  2034. {
  2035. ignoreTargetsSet.insert(itvIt->c_str());
  2036. }
  2037. }
  2038. str << graphType << " " << graphName << " {" << std::endl;
  2039. str << graphHeader << std::endl;
  2040. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  2041. std::vector<cmLocalGenerator*> localGenerators;
  2042. gg->GetLocalGenerators(localGenerators);
  2043. std::vector<cmLocalGenerator*>::iterator lit;
  2044. // for target deps
  2045. // 1 - cmake target
  2046. // 2 - external target
  2047. // 0 - no deps
  2048. std::map<cmStdString, int> targetDeps;
  2049. std::map<cmStdString, cmTarget*> targetPtrs;
  2050. std::map<cmStdString, cmStdString> targetNamesNodes;
  2051. char tgtName[100];
  2052. int cnt = 0;
  2053. // First pass get the list of all cmake targets
  2054. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2055. {
  2056. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2057. cmTargets::iterator tit;
  2058. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2059. {
  2060. const char* realTargetName = tit->first.c_str();
  2061. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2062. {
  2063. // Skip ignored targets
  2064. continue;
  2065. }
  2066. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  2067. sprintf(tgtName, "%s%d", graphNodePrefix, cnt++);
  2068. targetNamesNodes[realTargetName] = tgtName;
  2069. targetPtrs[realTargetName] = &tit->second;
  2070. }
  2071. }
  2072. // Ok, now find all the stuff we link to that is not in cmake
  2073. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2074. {
  2075. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2076. cmTargets::iterator tit;
  2077. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2078. {
  2079. const cmTarget::LinkLibraryVectorType* ll
  2080. = &(tit->second.GetOriginalLinkLibraries());
  2081. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2082. const char* realTargetName = tit->first.c_str();
  2083. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2084. {
  2085. // Skip ignored targets
  2086. continue;
  2087. }
  2088. if ( ll->size() > 0 )
  2089. {
  2090. targetDeps[realTargetName] = 1;
  2091. }
  2092. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2093. {
  2094. const char* libName = llit->first.c_str();
  2095. std::map<cmStdString, cmStdString>::iterator tarIt
  2096. = targetNamesNodes.find(libName);
  2097. if ( ignoreTargetsSet.find(libName) != ignoreTargetsSet.end() )
  2098. {
  2099. // Skip ignored targets
  2100. continue;
  2101. }
  2102. if ( tarIt == targetNamesNodes.end() )
  2103. {
  2104. sprintf(tgtName, "%s%d", graphNodePrefix, cnt++);
  2105. targetDeps[libName] = 2;
  2106. targetNamesNodes[libName] = tgtName;
  2107. //str << " \"" << tgtName << "\" [ label=\"" << libName
  2108. //<< "\" shape=\"ellipse\"];" << std::endl;
  2109. }
  2110. else
  2111. {
  2112. std::map<cmStdString, int>::iterator depIt
  2113. = targetDeps.find(libName);
  2114. if ( depIt == targetDeps.end() )
  2115. {
  2116. targetDeps[libName] = 1;
  2117. }
  2118. }
  2119. }
  2120. }
  2121. }
  2122. // Write out nodes
  2123. std::map<cmStdString, int>::iterator depIt;
  2124. for ( depIt = targetDeps.begin(); depIt != targetDeps.end(); ++ depIt )
  2125. {
  2126. const char* newTargetName = depIt->first.c_str();
  2127. std::map<cmStdString, cmStdString>::iterator tarIt
  2128. = targetNamesNodes.find(newTargetName);
  2129. if ( tarIt == targetNamesNodes.end() )
  2130. {
  2131. // We should not be here.
  2132. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2133. << " even though it was added in the previous pass" << std::endl;
  2134. abort();
  2135. }
  2136. str << " \"" << tarIt->second.c_str() << "\" [ label=\""
  2137. << newTargetName << "\" shape=\"";
  2138. if ( depIt->second == 1 )
  2139. {
  2140. std::map<cmStdString, cmTarget*>::iterator tarTypeIt= targetPtrs.find(
  2141. newTargetName);
  2142. if ( tarTypeIt == targetPtrs.end() )
  2143. {
  2144. // We should not be here.
  2145. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2146. << " even though it was added in the previous pass" << std::endl;
  2147. abort();
  2148. }
  2149. cmTarget* tg = tarTypeIt->second;
  2150. switch ( tg->GetType() )
  2151. {
  2152. case cmTarget::EXECUTABLE:
  2153. str << "house";
  2154. break;
  2155. case cmTarget::STATIC_LIBRARY:
  2156. str << "diamond";
  2157. break;
  2158. case cmTarget::SHARED_LIBRARY:
  2159. str << "polygon";
  2160. break;
  2161. case cmTarget::MODULE_LIBRARY:
  2162. str << "octagon";
  2163. break;
  2164. default:
  2165. str << "box";
  2166. }
  2167. }
  2168. else
  2169. {
  2170. str << "ellipse";
  2171. }
  2172. str << "\"];" << std::endl;
  2173. }
  2174. // Now generate the connectivity
  2175. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2176. {
  2177. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2178. cmTargets::iterator tit;
  2179. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2180. {
  2181. std::map<cmStdString, int>::iterator dependIt
  2182. = targetDeps.find(tit->first.c_str());
  2183. if ( dependIt == targetDeps.end() )
  2184. {
  2185. continue;
  2186. }
  2187. std::map<cmStdString, cmStdString>::iterator cmakeTarIt
  2188. = targetNamesNodes.find(tit->first.c_str());
  2189. const cmTarget::LinkLibraryVectorType* ll
  2190. = &(tit->second.GetOriginalLinkLibraries());
  2191. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2192. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2193. {
  2194. const char* libName = llit->first.c_str();
  2195. std::map<cmStdString, cmStdString>::iterator tarIt
  2196. = targetNamesNodes.find(libName);
  2197. if ( tarIt == targetNamesNodes.end() )
  2198. {
  2199. // We should not be here.
  2200. std::cout << __LINE__ << " Cannot find library: " << libName
  2201. << " even though it was added in the previous pass" << std::endl;
  2202. abort();
  2203. }
  2204. str << " \"" << cmakeTarIt->second.c_str() << "\" -> \""
  2205. << tarIt->second.c_str() << "\"" << std::endl;
  2206. }
  2207. }
  2208. }
  2209. // TODO: Use dotted or something for external libraries
  2210. //str << " \"node0\":f4 -> \"node12\"[color=\"#0000ff\" style=dotted]"
  2211. //<< std::endl;
  2212. //
  2213. str << "}" << std::endl;
  2214. }
  2215. //----------------------------------------------------------------------------
  2216. #ifdef CMAKE_BUILD_WITH_CMAKE
  2217. int cmake::ExecuteEchoColor(std::vector<std::string>& args)
  2218. {
  2219. // The arguments are
  2220. // argv[0] == <cmake-executable>
  2221. // argv[1] == cmake_echo_color
  2222. // On some platforms (an MSYS prompt) cmsysTerminal may not be able
  2223. // to determine whether the stream is displayed on a tty. In this
  2224. // case it assumes no unless we tell it otherwise. Since we want
  2225. // color messages to be displayed for users we will assume yes.
  2226. // However, we can test for some situations when the answer is most
  2227. // likely no.
  2228. int assumeTTY = cmsysTerminal_Color_AssumeTTY;
  2229. if(cmSystemTools::GetEnv("DART_TEST_FROM_DART") ||
  2230. cmSystemTools::GetEnv("DASHBOARD_TEST_FROM_CTEST"))
  2231. {
  2232. // Avoid printing color escapes during dashboard builds.
  2233. assumeTTY = 0;
  2234. }
  2235. bool enabled = true;
  2236. int color = cmsysTerminal_Color_Normal;
  2237. bool newline = true;
  2238. for(unsigned int i=2; i < args.size(); ++i)
  2239. {
  2240. if(args[i].find("--switch=") == 0)
  2241. {
  2242. // Enable or disable color based on the switch value.
  2243. std::string value = args[i].substr(9);
  2244. if(!value.empty())
  2245. {
  2246. if(cmSystemTools::IsOn(value.c_str()))
  2247. {
  2248. enabled = true;
  2249. }
  2250. else
  2251. {
  2252. enabled = false;
  2253. }
  2254. }
  2255. }
  2256. else if(args[i] == "--normal")
  2257. {
  2258. color = cmsysTerminal_Color_Normal;
  2259. }
  2260. else if(args[i] == "--black")
  2261. {
  2262. color = cmsysTerminal_Color_ForegroundBlack;
  2263. }
  2264. else if(args[i] == "--red")
  2265. {
  2266. color = cmsysTerminal_Color_ForegroundRed;
  2267. }
  2268. else if(args[i] == "--green")
  2269. {
  2270. color = cmsysTerminal_Color_ForegroundGreen;
  2271. }
  2272. else if(args[i] == "--yellow")
  2273. {
  2274. color = cmsysTerminal_Color_ForegroundYellow;
  2275. }
  2276. else if(args[i] == "--blue")
  2277. {
  2278. color = cmsysTerminal_Color_ForegroundBlue;
  2279. }
  2280. else if(args[i] == "--magenta")
  2281. {
  2282. color = cmsysTerminal_Color_ForegroundMagenta;
  2283. }
  2284. else if(args[i] == "--cyan")
  2285. {
  2286. color = cmsysTerminal_Color_ForegroundCyan;
  2287. }
  2288. else if(args[i] == "--white")
  2289. {
  2290. color = cmsysTerminal_Color_ForegroundWhite;
  2291. }
  2292. else if(args[i] == "--bold")
  2293. {
  2294. color |= cmsysTerminal_Color_ForegroundBold;
  2295. }
  2296. else if(args[i] == "--no-newline")
  2297. {
  2298. newline = false;
  2299. }
  2300. else if(args[i] == "--newline")
  2301. {
  2302. newline = true;
  2303. }
  2304. else if(enabled)
  2305. {
  2306. // Color is enabled. Print with the current color.
  2307. cmsysTerminal_cfprintf(color | assumeTTY, stdout, "%s%s",
  2308. args[i].c_str(), newline? "\n" : "");
  2309. }
  2310. else
  2311. {
  2312. // Color is disabled. Print without color.
  2313. fprintf(stdout, "%s%s", args[i].c_str(), newline? "\n" : "");
  2314. }
  2315. }
  2316. return 0;
  2317. }
  2318. #else
  2319. int cmake::ExecuteEchoColor(std::vector<std::string>&)
  2320. {
  2321. return 1;
  2322. }
  2323. #endif