cmake.cxx 80 KB

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