cmake.cxx 97 KB

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