cmake.cxx 93 KB

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