cmake.cxx 72 KB

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