cmake.cxx 92 KB

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