cmake.cxx 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048
  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 "cmDocumentVariables.h"
  15. #include "time.h"
  16. #include "cmCacheManager.h"
  17. #include "cmMakefile.h"
  18. #include "cmLocalGenerator.h"
  19. #include "cmExternalMakefileProjectGenerator.h"
  20. #include "cmCommands.h"
  21. #include "cmCommand.h"
  22. #include "cmFileTimeComparison.h"
  23. #include "cmGeneratedFileStream.h"
  24. #include "cmSourceFile.h"
  25. #include "cmVersion.h"
  26. #include "cmTest.h"
  27. #if defined(CMAKE_BUILD_WITH_CMAKE)
  28. # include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback.
  29. # include "cmVariableWatch.h"
  30. # include <cmsys/Terminal.h>
  31. #endif
  32. #include <cmsys/Directory.hxx>
  33. #include <cmsys/Process.h>
  34. #include <cmsys/Glob.hxx>
  35. #include <cmsys/RegularExpression.hxx>
  36. // only build kdevelop generator on non-windows platforms
  37. // when not bootstrapping cmake
  38. #if !defined(_WIN32)
  39. # if defined(CMAKE_BUILD_WITH_CMAKE)
  40. # define CMAKE_USE_KDEVELOP
  41. # endif
  42. #endif
  43. #if defined(CMAKE_BUILD_WITH_CMAKE)
  44. # define CMAKE_USE_ECLIPSE
  45. #endif
  46. #if defined(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
  47. # define CMAKE_BOOT_MINGW
  48. #endif
  49. // include the generator
  50. #if defined(_WIN32) && !defined(__CYGWIN__)
  51. # if !defined(CMAKE_BOOT_MINGW)
  52. # include "cmGlobalVisualStudio6Generator.h"
  53. # include "cmGlobalVisualStudio7Generator.h"
  54. # include "cmGlobalVisualStudio71Generator.h"
  55. # include "cmGlobalVisualStudio8Generator.h"
  56. # include "cmGlobalVisualStudio9Generator.h"
  57. # include "cmGlobalVisualStudio9Win64Generator.h"
  58. # include "cmGlobalVisualStudio8Win64Generator.h"
  59. # include "cmGlobalBorlandMakefileGenerator.h"
  60. # include "cmGlobalNMakeMakefileGenerator.h"
  61. # include "cmGlobalWatcomWMakeGenerator.h"
  62. # define CMAKE_HAVE_VS_GENERATORS
  63. # endif
  64. # include "cmGlobalMSYSMakefileGenerator.h"
  65. # include "cmGlobalMinGWMakefileGenerator.h"
  66. # include "cmWin32ProcessExecution.h"
  67. #else
  68. #endif
  69. #include "cmGlobalUnixMakefileGenerator3.h"
  70. #if defined(CMAKE_HAVE_VS_GENERATORS)
  71. #include "cmCallVisualStudioMacro.h"
  72. #endif
  73. #if !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  74. # include "cmExtraCodeBlocksGenerator.h"
  75. #endif
  76. #ifdef CMAKE_USE_KDEVELOP
  77. # include "cmGlobalKdevelopGenerator.h"
  78. #endif
  79. #ifdef CMAKE_USE_ECLIPSE
  80. # include "cmExtraEclipseCDT4Generator.h"
  81. #endif
  82. #include <stdlib.h> // required for atoi
  83. #if defined( __APPLE__ )
  84. # if defined(CMAKE_BUILD_WITH_CMAKE)
  85. # include "cmGlobalXCodeGenerator.h"
  86. # define CMAKE_USE_XCODE 1
  87. # endif
  88. # include <sys/types.h>
  89. # include <sys/time.h>
  90. # include <sys/resource.h>
  91. #endif
  92. #include <sys/stat.h> // struct stat
  93. #include <memory> // auto_ptr
  94. static bool cmakeCheckStampFile(const char* stampName);
  95. void cmNeedBackwardsCompatibility(const std::string& variable,
  96. int access_type, void*, const char*, const cmMakefile*)
  97. {
  98. #ifdef CMAKE_BUILD_WITH_CMAKE
  99. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  100. {
  101. std::string message = "An attempt was made to access a variable: ";
  102. message += variable;
  103. message +=
  104. " that has not been defined. Some variables were always defined "
  105. "by CMake in versions prior to 1.6. To fix this you might need to set "
  106. "the cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If "
  107. "you are writing a CMakeList file, (or have already set "
  108. "CMAKE_BACKWARDS_COMPATABILITY to 1.4 or less) then you probably need "
  109. "to include a CMake module to test for the feature this variable "
  110. "defines.";
  111. cmSystemTools::Error(message.c_str());
  112. }
  113. #else
  114. (void)variable;
  115. (void)access_type;
  116. #endif
  117. }
  118. cmake::cmake()
  119. {
  120. this->DebugOutput = false;
  121. this->DebugTryCompile = false;
  122. this->ClearBuildSystem = false;
  123. this->FileComparison = new cmFileTimeComparison;
  124. this->Policies = new cmPolicies();
  125. this->Properties.SetCMakeInstance(this);
  126. // initialize properties
  127. cmSourceFile::DefineProperties(this);
  128. cmTarget::DefineProperties(this);
  129. cmMakefile::DefineProperties(this);
  130. cmTest::DefineProperties(this);
  131. cmake::DefineProperties(this);
  132. #ifdef __APPLE__
  133. struct rlimit rlp;
  134. if(!getrlimit(RLIMIT_STACK, &rlp))
  135. {
  136. if(rlp.rlim_cur != rlp.rlim_max)
  137. {
  138. rlp.rlim_cur = rlp.rlim_max;
  139. setrlimit(RLIMIT_STACK, &rlp);
  140. }
  141. }
  142. #endif
  143. // If MAKEFLAGS are given in the environment, remove the environment
  144. // variable. This will prevent try-compile from succeeding when it
  145. // should fail (if "-i" is an option). We cannot simply test
  146. // whether "-i" is given and remove it because some make programs
  147. // encode the MAKEFLAGS variable in a strange way.
  148. if(getenv("MAKEFLAGS"))
  149. {
  150. cmSystemTools::PutEnv("MAKEFLAGS=");
  151. }
  152. this->Verbose = false;
  153. this->InTryCompile = false;
  154. this->CacheManager = new cmCacheManager;
  155. this->GlobalGenerator = 0;
  156. this->ProgressCallback = 0;
  157. this->ProgressCallbackClientData = 0;
  158. this->ScriptMode = false;
  159. #ifdef CMAKE_BUILD_WITH_CMAKE
  160. this->VariableWatch = new cmVariableWatch;
  161. this->VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  162. cmNeedBackwardsCompatibility);
  163. this->VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  164. cmNeedBackwardsCompatibility);
  165. this->VariableWatch->AddWatch("CMAKE_X_LIBS",
  166. cmNeedBackwardsCompatibility);
  167. #endif
  168. this->AddDefaultGenerators();
  169. this->AddDefaultExtraGenerators();
  170. this->AddDefaultCommands();
  171. // Make sure we can capture the build tool output.
  172. cmSystemTools::EnableVSConsoleOutput();
  173. }
  174. cmake::~cmake()
  175. {
  176. delete this->CacheManager;
  177. delete this->Policies;
  178. if (this->GlobalGenerator)
  179. {
  180. delete this->GlobalGenerator;
  181. this->GlobalGenerator = 0;
  182. }
  183. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  184. j != this->Commands.end(); ++j)
  185. {
  186. delete (*j).second;
  187. }
  188. #ifdef CMAKE_BUILD_WITH_CMAKE
  189. delete this->VariableWatch;
  190. #endif
  191. delete this->FileComparison;
  192. }
  193. void cmake::CleanupCommandsAndMacros()
  194. {
  195. std::vector<cmCommand*> commands;
  196. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  197. j != this->Commands.end(); ++j)
  198. {
  199. if ( !j->second->IsA("cmMacroHelperCommand") &&
  200. !j->second->IsA("cmFunctionHelperCommand"))
  201. {
  202. commands.push_back(j->second);
  203. }
  204. else
  205. {
  206. delete j->second;
  207. }
  208. }
  209. this->Commands.erase(this->Commands.begin(), this->Commands.end());
  210. std::vector<cmCommand*>::iterator it;
  211. for ( it = commands.begin(); it != commands.end();
  212. ++ it )
  213. {
  214. this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  215. }
  216. }
  217. bool cmake::CommandExists(const char* name) const
  218. {
  219. std::string sName = cmSystemTools::LowerCase(name);
  220. return (this->Commands.find(sName) != this->Commands.end());
  221. }
  222. cmCommand *cmake::GetCommand(const char *name)
  223. {
  224. cmCommand* rm = 0;
  225. std::string sName = cmSystemTools::LowerCase(name);
  226. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  227. if (pos != this->Commands.end())
  228. {
  229. rm = (*pos).second;
  230. }
  231. return rm;
  232. }
  233. void cmake::RenameCommand(const char*oldName, const char* newName)
  234. {
  235. // if the command already exists, free the old one
  236. std::string sOldName = cmSystemTools::LowerCase(oldName);
  237. std::string sNewName = cmSystemTools::LowerCase(newName);
  238. RegisteredCommandsMap::iterator pos = this->Commands.find(sOldName);
  239. if ( pos == this->Commands.end() )
  240. {
  241. return;
  242. }
  243. cmCommand* cmd = pos->second;
  244. pos = this->Commands.find(sNewName);
  245. if (pos != this->Commands.end())
  246. {
  247. delete pos->second;
  248. this->Commands.erase(pos);
  249. }
  250. this->Commands.insert(RegisteredCommandsMap::value_type(sNewName, cmd));
  251. pos = this->Commands.find(sOldName);
  252. this->Commands.erase(pos);
  253. }
  254. void cmake::RemoveCommand(const char* name)
  255. {
  256. std::string sName = cmSystemTools::LowerCase(name);
  257. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  258. if ( pos != this->Commands.end() )
  259. {
  260. delete pos->second;
  261. this->Commands.erase(pos);
  262. }
  263. }
  264. void cmake::AddCommand(cmCommand* wg)
  265. {
  266. std::string name = cmSystemTools::LowerCase(wg->GetName());
  267. // if the command already exists, free the old one
  268. RegisteredCommandsMap::iterator pos = this->Commands.find(name);
  269. if (pos != this->Commands.end())
  270. {
  271. delete pos->second;
  272. this->Commands.erase(pos);
  273. }
  274. this->Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  275. }
  276. void cmake::RemoveUnscriptableCommands()
  277. {
  278. std::vector<std::string> unscriptableCommands;
  279. cmake::RegisteredCommandsMap* commands = this->GetCommands();
  280. for (cmake::RegisteredCommandsMap::const_iterator pos = commands->begin();
  281. pos != commands->end();
  282. ++pos)
  283. {
  284. if (!pos->second->IsScriptable())
  285. {
  286. unscriptableCommands.push_back(pos->first);
  287. }
  288. }
  289. for(std::vector<std::string>::const_iterator it=unscriptableCommands.begin();
  290. it != unscriptableCommands.end();
  291. ++it)
  292. {
  293. this->RemoveCommand(it->c_str());
  294. }
  295. }
  296. // Parse the args
  297. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  298. {
  299. for(unsigned int i=1; i < args.size(); ++i)
  300. {
  301. std::string arg = args[i];
  302. if(arg.find("-D",0) == 0)
  303. {
  304. std::string entry = arg.substr(2);
  305. if(entry.size() == 0)
  306. {
  307. ++i;
  308. if(i < args.size())
  309. {
  310. entry = args[i];
  311. }
  312. else
  313. {
  314. cmSystemTools::Error("-D must be followed with VAR=VALUE.");
  315. return false;
  316. }
  317. }
  318. std::string var, value;
  319. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  320. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  321. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  322. {
  323. this->CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  324. "No help, variable specified on the command line.", type);
  325. }
  326. else
  327. {
  328. std::cerr << "Parse error in command line argument: " << arg << "\n"
  329. << "Should be: VAR:type=value\n";
  330. cmSystemTools::Error("No cmake scrpt provided.");
  331. return false;
  332. }
  333. }
  334. else if(arg.find("-Wno-dev",0) == 0)
  335. {
  336. this->CacheManager->
  337. AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "TRUE",
  338. "Suppress Warnings that are meant for"
  339. " the author of the CMakeLists.txt files.",
  340. cmCacheManager::INTERNAL);
  341. }
  342. else if(arg.find("-Wdev",0) == 0)
  343. {
  344. this->CacheManager->
  345. AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "FALSE",
  346. "Suppress Warnings that are meant for"
  347. " the author of the CMakeLists.txt files.",
  348. cmCacheManager::INTERNAL);
  349. }
  350. else if(arg.find("-U",0) == 0)
  351. {
  352. std::string entryPattern = arg.substr(2);
  353. if(entryPattern.size() == 0)
  354. {
  355. ++i;
  356. if(i < args.size())
  357. {
  358. entryPattern = args[i];
  359. }
  360. else
  361. {
  362. cmSystemTools::Error("-U must be followed with VAR.");
  363. return false;
  364. }
  365. }
  366. cmsys::RegularExpression regex(
  367. cmsys::Glob::PatternToRegex(entryPattern.c_str(), true).c_str());
  368. //go through all cache entries and collect the vars which will be removed
  369. std::vector<std::string> entriesToDelete;
  370. cmCacheManager::CacheIterator it =
  371. this->CacheManager->GetCacheIterator();
  372. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  373. {
  374. cmCacheManager::CacheEntryType t = it.GetType();
  375. if(t != cmCacheManager::STATIC)
  376. {
  377. std::string entryName = it.GetName();
  378. if (regex.find(entryName.c_str()))
  379. {
  380. entriesToDelete.push_back(entryName);
  381. }
  382. }
  383. }
  384. // now remove them from the cache
  385. for(std::vector<std::string>::const_iterator currentEntry =
  386. entriesToDelete.begin();
  387. currentEntry != entriesToDelete.end();
  388. ++currentEntry)
  389. {
  390. this->CacheManager->RemoveCacheEntry(currentEntry->c_str());
  391. }
  392. }
  393. else if(arg.find("-C",0) == 0)
  394. {
  395. std::string path = arg.substr(2);
  396. if ( path.size() == 0 )
  397. {
  398. ++i;
  399. if(i < args.size())
  400. {
  401. path = args[i];
  402. }
  403. else
  404. {
  405. cmSystemTools::Error("-C must be followed by a file name.");
  406. return false;
  407. }
  408. }
  409. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  410. this->ReadListFile(path.c_str());
  411. }
  412. else if(arg.find("-P",0) == 0)
  413. {
  414. i++;
  415. if(i >= args.size())
  416. {
  417. cmSystemTools::Error("-P must be followed by a file name.");
  418. return false;
  419. }
  420. std::string path = args[i];
  421. if ( path.size() == 0 )
  422. {
  423. cmSystemTools::Error("No cmake script provided.");
  424. return false;
  425. }
  426. this->ReadListFile(path.c_str());
  427. }
  428. }
  429. return true;
  430. }
  431. void cmake::ReadListFile(const char *path)
  432. {
  433. // if a generator was not yet created, temporarily create one
  434. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  435. bool created = false;
  436. // if a generator was not specified use a generic one
  437. if (!gg)
  438. {
  439. gg = new cmGlobalGenerator;
  440. gg->SetCMakeInstance(this);
  441. created = true;
  442. }
  443. // read in the list file to fill the cache
  444. if(path)
  445. {
  446. std::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  447. lg->SetGlobalGenerator(gg);
  448. lg->GetMakefile()->SetHomeOutputDirectory
  449. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  450. lg->GetMakefile()->SetStartOutputDirectory
  451. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  452. lg->GetMakefile()->SetHomeDirectory
  453. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  454. lg->GetMakefile()->SetStartDirectory
  455. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  456. if (!lg->GetMakefile()->ReadListFile(0, path))
  457. {
  458. cmSystemTools::Error("Error processing file:", path);
  459. }
  460. }
  461. // free generic one if generated
  462. if (created)
  463. {
  464. delete gg;
  465. }
  466. }
  467. // Parse the args
  468. void cmake::SetArgs(const std::vector<std::string>& args)
  469. {
  470. bool directoriesSet = false;
  471. for(unsigned int i=1; i < args.size(); ++i)
  472. {
  473. std::string arg = args[i];
  474. if(arg.find("-H",0) == 0)
  475. {
  476. directoriesSet = true;
  477. std::string path = arg.substr(2);
  478. path = cmSystemTools::CollapseFullPath(path.c_str());
  479. cmSystemTools::ConvertToUnixSlashes(path);
  480. this->SetHomeDirectory(path.c_str());
  481. }
  482. else if(arg.find("-S",0) == 0)
  483. {
  484. // There is no local generate anymore. Ignore -S option.
  485. }
  486. else if(arg.find("-O",0) == 0)
  487. {
  488. // There is no local generate anymore. Ignore -O option.
  489. }
  490. else if(arg.find("-B",0) == 0)
  491. {
  492. directoriesSet = true;
  493. std::string path = arg.substr(2);
  494. path = cmSystemTools::CollapseFullPath(path.c_str());
  495. cmSystemTools::ConvertToUnixSlashes(path);
  496. this->SetHomeOutputDirectory(path.c_str());
  497. }
  498. else if((i < args.size()-1) && (arg.find("--check-build-system",0) == 0))
  499. {
  500. this->CheckBuildSystemArgument = args[++i];
  501. this->ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  502. }
  503. else if((i < args.size()-1) && (arg.find("--check-stamp-file",0) == 0))
  504. {
  505. this->CheckStampFile = args[++i];
  506. }
  507. #if defined(CMAKE_HAVE_VS_GENERATORS)
  508. else if((i < args.size()-1) && (arg.find("--vs-solution-file",0) == 0))
  509. {
  510. this->VSSolutionFile = args[++i];
  511. }
  512. #endif
  513. else if(arg.find("-V",0) == 0)
  514. {
  515. this->Verbose = true;
  516. }
  517. else if(arg.find("-D",0) == 0)
  518. {
  519. // skip for now
  520. }
  521. else if(arg.find("-U",0) == 0)
  522. {
  523. // skip for now
  524. }
  525. else if(arg.find("-C",0) == 0)
  526. {
  527. // skip for now
  528. }
  529. else if(arg.find("-P",0) == 0)
  530. {
  531. // skip for now
  532. i++;
  533. }
  534. else if(arg.find("--graphviz=",0) == 0)
  535. {
  536. std::string path = arg.substr(strlen("--graphviz="));
  537. path = cmSystemTools::CollapseFullPath(path.c_str());
  538. cmSystemTools::ConvertToUnixSlashes(path);
  539. this->GraphVizFile = path;
  540. if ( this->GraphVizFile.empty() )
  541. {
  542. cmSystemTools::Error("No file specified for --graphviz");
  543. }
  544. }
  545. else if(arg.find("--debug-trycompile",0) == 0)
  546. {
  547. std::cout << "debug trycompile on\n";
  548. this->DebugTryCompileOn();
  549. }
  550. else if(arg.find("--debug-output",0) == 0)
  551. {
  552. std::cout << "Running with debug output on.\n";
  553. this->DebugOutputOn();
  554. }
  555. else if(arg.find("-G",0) == 0)
  556. {
  557. std::string value = arg.substr(2);
  558. if(value.size() == 0)
  559. {
  560. ++i;
  561. if(i >= args.size())
  562. {
  563. cmSystemTools::Error("No generator specified for -G");
  564. return;
  565. }
  566. value = args[i];
  567. }
  568. cmGlobalGenerator* gen =
  569. this->CreateGlobalGenerator(value.c_str());
  570. if(!gen)
  571. {
  572. cmSystemTools::Error("Could not create named generator ",
  573. value.c_str());
  574. }
  575. else
  576. {
  577. this->SetGlobalGenerator(gen);
  578. }
  579. }
  580. // no option assume it is the path to the source
  581. else
  582. {
  583. directoriesSet = true;
  584. this->SetDirectoriesFromFile(arg.c_str());
  585. }
  586. }
  587. if(!directoriesSet)
  588. {
  589. this->SetHomeOutputDirectory
  590. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  591. this->SetStartOutputDirectory
  592. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  593. this->SetHomeDirectory
  594. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  595. this->SetStartDirectory
  596. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  597. }
  598. this->SetStartDirectory(this->GetHomeDirectory());
  599. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  600. }
  601. //----------------------------------------------------------------------------
  602. void cmake::SetDirectoriesFromFile(const char* arg)
  603. {
  604. // Check if the argument refers to a CMakeCache.txt or
  605. // CMakeLists.txt file.
  606. std::string listPath;
  607. std::string cachePath;
  608. bool argIsFile = false;
  609. if(cmSystemTools::FileIsDirectory(arg))
  610. {
  611. std::string path = cmSystemTools::CollapseFullPath(arg);
  612. cmSystemTools::ConvertToUnixSlashes(path);
  613. std::string cacheFile = path;
  614. cacheFile += "/CMakeCache.txt";
  615. std::string listFile = path;
  616. listFile += "/CMakeLists.txt";
  617. if(cmSystemTools::FileExists(cacheFile.c_str()))
  618. {
  619. cachePath = path;
  620. }
  621. if(cmSystemTools::FileExists(listFile.c_str()))
  622. {
  623. listPath = path;
  624. }
  625. }
  626. else if(cmSystemTools::FileExists(arg))
  627. {
  628. argIsFile = true;
  629. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  630. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  631. name = cmSystemTools::LowerCase(name);
  632. if(name == "cmakecache.txt")
  633. {
  634. cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  635. }
  636. else if(name == "cmakelists.txt")
  637. {
  638. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  639. }
  640. }
  641. else
  642. {
  643. // Specified file or directory does not exist. Try to set things
  644. // up to produce a meaningful error message.
  645. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  646. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  647. name = cmSystemTools::LowerCase(name);
  648. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  649. {
  650. argIsFile = true;
  651. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  652. }
  653. else
  654. {
  655. listPath = fullPath;
  656. }
  657. }
  658. // If there is a CMakeCache.txt file, use its settings.
  659. if(cachePath.length() > 0)
  660. {
  661. cmCacheManager* cachem = this->GetCacheManager();
  662. cmCacheManager::CacheIterator it = cachem->NewIterator();
  663. if(cachem->LoadCache(cachePath.c_str()) &&
  664. it.Find("CMAKE_HOME_DIRECTORY"))
  665. {
  666. this->SetHomeOutputDirectory(cachePath.c_str());
  667. this->SetStartOutputDirectory(cachePath.c_str());
  668. this->SetHomeDirectory(it.GetValue());
  669. this->SetStartDirectory(it.GetValue());
  670. return;
  671. }
  672. }
  673. // If there is a CMakeLists.txt file, use it as the source tree.
  674. if(listPath.length() > 0)
  675. {
  676. this->SetHomeDirectory(listPath.c_str());
  677. this->SetStartDirectory(listPath.c_str());
  678. if(argIsFile)
  679. {
  680. // Source CMakeLists.txt file given. It was probably dropped
  681. // onto the executable in a GUI. Default to an in-source build.
  682. this->SetHomeOutputDirectory(listPath.c_str());
  683. this->SetStartOutputDirectory(listPath.c_str());
  684. }
  685. else
  686. {
  687. // Source directory given on command line. Use current working
  688. // directory as build tree.
  689. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  690. this->SetHomeOutputDirectory(cwd.c_str());
  691. this->SetStartOutputDirectory(cwd.c_str());
  692. }
  693. return;
  694. }
  695. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  696. // argument. Assume it is the path to the source tree, and use the
  697. // current working directory as the build tree.
  698. std::string full = cmSystemTools::CollapseFullPath(arg);
  699. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  700. this->SetHomeDirectory(full.c_str());
  701. this->SetStartDirectory(full.c_str());
  702. this->SetHomeOutputDirectory(cwd.c_str());
  703. this->SetStartOutputDirectory(cwd.c_str());
  704. }
  705. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
  706. // cache
  707. int cmake::AddCMakePaths()
  708. {
  709. // Find the cmake executable
  710. std::string cMakeSelf = cmSystemTools::GetExecutableDirectory();
  711. cMakeSelf += "/cmake";
  712. cMakeSelf += cmSystemTools::GetExecutableExtension();
  713. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  714. {
  715. cmSystemTools::Error("CMake executable cannot be found at ",
  716. cMakeSelf.c_str());
  717. return 0;
  718. }
  719. // Save the value in the cache
  720. this->CacheManager->AddCacheEntry
  721. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  722. cmCacheManager::INTERNAL);
  723. // if the edit command is not yet in the cache,
  724. // or if CMakeEditCommand has been set on this object,
  725. // then set the CMAKE_EDIT_COMMAND in the cache
  726. // This will mean that the last gui to edit the cache
  727. // will be the one that make edit_cache uses.
  728. if(!this->GetCacheDefinition("CMAKE_EDIT_COMMAND")
  729. || !this->CMakeEditCommand.empty())
  730. {
  731. // Find and save the command to edit the cache
  732. std::string editCacheCommand;
  733. if(!this->CMakeEditCommand.empty())
  734. {
  735. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf)
  736. + std::string("/")
  737. + this->CMakeEditCommand
  738. + cmSystemTools::GetFilenameExtension(cMakeSelf);
  739. }
  740. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  741. {
  742. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  743. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  744. }
  745. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  746. {
  747. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  748. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  749. }
  750. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  751. {
  752. this->CacheManager->AddCacheEntry
  753. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  754. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  755. }
  756. }
  757. std::string ctestCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  758. "/ctest" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  759. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  760. {
  761. this->CacheManager->AddCacheEntry
  762. ("CMAKE_CTEST_COMMAND", ctestCommand.c_str(),
  763. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  764. }
  765. std::string cpackCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  766. "/cpack" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  767. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  768. {
  769. this->CacheManager->AddCacheEntry
  770. ("CMAKE_CPACK_COMMAND", cpackCommand.c_str(),
  771. "Path to cpack program executable.", cmCacheManager::INTERNAL);
  772. }
  773. // do CMAKE_ROOT, look for the environment variable first
  774. std::string cMakeRoot;
  775. std::string modules;
  776. if (getenv("CMAKE_ROOT"))
  777. {
  778. cMakeRoot = getenv("CMAKE_ROOT");
  779. modules = cMakeRoot + "/Modules/CMake.cmake";
  780. }
  781. if(!cmSystemTools::FileExists(modules.c_str()))
  782. {
  783. // next try exe/..
  784. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  785. std::string::size_type slashPos = cMakeRoot.rfind("/");
  786. if(slashPos != std::string::npos)
  787. {
  788. cMakeRoot = cMakeRoot.substr(0, slashPos);
  789. }
  790. // is there no Modules direcory there?
  791. modules = cMakeRoot + "/Modules/CMake.cmake";
  792. }
  793. if (!cmSystemTools::FileExists(modules.c_str()))
  794. {
  795. // try exe/../share/cmake
  796. cMakeRoot += CMAKE_DATA_DIR;
  797. modules = cMakeRoot + "/Modules/CMake.cmake";
  798. }
  799. #ifdef CMAKE_ROOT_DIR
  800. if (!cmSystemTools::FileExists(modules.c_str()))
  801. {
  802. // try compiled in root directory
  803. cMakeRoot = CMAKE_ROOT_DIR;
  804. modules = cMakeRoot + "/Modules/CMake.cmake";
  805. }
  806. #endif
  807. if (!cmSystemTools::FileExists(modules.c_str()))
  808. {
  809. // try
  810. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  811. cMakeRoot += CMAKE_DATA_DIR;
  812. modules = cMakeRoot + "/Modules/CMake.cmake";
  813. }
  814. if(!cmSystemTools::FileExists(modules.c_str()))
  815. {
  816. // next try exe
  817. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  818. // is there no Modules direcory there?
  819. modules = cMakeRoot + "/Modules/CMake.cmake";
  820. }
  821. if (!cmSystemTools::FileExists(modules.c_str()))
  822. {
  823. // couldn't find modules
  824. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  825. "CMake has most likely not been installed correctly.\n"
  826. "Modules directory not found in\n",
  827. cMakeRoot.c_str());
  828. return 0;
  829. }
  830. this->CacheManager->AddCacheEntry
  831. ("CMAKE_ROOT", cMakeRoot.c_str(),
  832. "Path to CMake installation.", cmCacheManager::INTERNAL);
  833. #ifdef _WIN32
  834. std::string comspec = "cmw9xcom.exe";
  835. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  836. #endif
  837. return 1;
  838. }
  839. void CMakeCommandUsage(const char* program)
  840. {
  841. cmOStringStream errorStream;
  842. #ifdef CMAKE_BUILD_WITH_CMAKE
  843. errorStream
  844. << "cmake version " << cmVersion::GetCMakeVersion() << "\n";
  845. #else
  846. errorStream
  847. << "cmake bootstrap\n";
  848. #endif
  849. errorStream
  850. << "Usage: " << program << " -E [command] [arguments ...]\n"
  851. << "Available commands: \n"
  852. << " chdir dir cmd [args]... - run command in a given directory\n"
  853. << " copy file destination - copy file to destination (either file "
  854. "or directory)\n"
  855. << " copy_if_different in-file out-file - copy file if input has "
  856. "changed\n"
  857. << " copy_directory source destination - copy directory 'source' "
  858. "content to directory 'destination'\n"
  859. << " compare_files file1 file2 - check if file1 is same as file2\n"
  860. << " echo [string]... - displays arguments as text\n"
  861. << " echo_append [string]... - displays arguments as text but no new "
  862. "line\n"
  863. << " environment - display the current enviroment\n"
  864. << " make_directory dir - create a directory\n"
  865. << " md5sum file1 [...] - compute md5sum of files\n"
  866. << " remove_directory dir - remove a directory and its contents\n"
  867. << " remove [-f] file1 file2 ... - remove the file(s), use -f to force "
  868. "it\n"
  869. << " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar "
  870. "archive\n"
  871. << " time command [args] ... - run command and return elapsed time\n"
  872. << " touch file - touch a file.\n"
  873. << " touch_nocreate file - touch a file but do not create it.\n"
  874. #if defined(_WIN32) && !defined(__CYGWIN__)
  875. << " write_regv key value - write registry value\n"
  876. << " delete_regv key - delete registry value\n"
  877. << " comspec - on windows 9x use this for RunCommand\n"
  878. #else
  879. << " create_symlink old new - create a symbolic link new -> old\n"
  880. #endif
  881. ;
  882. cmSystemTools::Error(errorStream.str().c_str());
  883. }
  884. int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
  885. {
  886. if (args.size() > 1)
  887. {
  888. // Copy file
  889. if (args[1] == "copy" && args.size() == 4)
  890. {
  891. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  892. {
  893. std::cerr << "Error copying file \"" << args[2].c_str()
  894. << "\" to \"" << args[3].c_str() << "\".\n";
  895. return 1;
  896. }
  897. return 0;
  898. }
  899. // Copy file if different.
  900. if (args[1] == "copy_if_different" && args.size() == 4)
  901. {
  902. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(),
  903. args[3].c_str()))
  904. {
  905. std::cerr << "Error copying file (if different) from \""
  906. << args[2].c_str() << "\" to \"" << args[3].c_str()
  907. << "\".\n";
  908. return 1;
  909. }
  910. return 0;
  911. }
  912. // Copy directory content
  913. if (args[1] == "copy_directory" && args.size() == 4)
  914. {
  915. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  916. {
  917. std::cerr << "Error copying directory from \""
  918. << args[2].c_str() << "\" to \"" << args[3].c_str()
  919. << "\".\n";
  920. return 1;
  921. }
  922. return 0;
  923. }
  924. // Compare files
  925. if (args[1] == "compare_files" && args.size() == 4)
  926. {
  927. if(cmSystemTools::FilesDiffer(args[2].c_str(), args[3].c_str()))
  928. {
  929. std::cerr << "Files \""
  930. << args[2].c_str() << "\" to \"" << args[3].c_str()
  931. << "\" are different.\n";
  932. return 1;
  933. }
  934. return 0;
  935. }
  936. // Echo string
  937. else if (args[1] == "echo" )
  938. {
  939. unsigned int cc;
  940. const char* space = "";
  941. for ( cc = 2; cc < args.size(); cc ++ )
  942. {
  943. std::cout << space << args[cc];
  944. space = " ";
  945. }
  946. std::cout << std::endl;
  947. return 0;
  948. }
  949. // Echo string no new line
  950. else if (args[1] == "echo_append" )
  951. {
  952. unsigned int cc;
  953. const char* space = "";
  954. for ( cc = 2; cc < args.size(); cc ++ )
  955. {
  956. std::cout << space << args[cc];
  957. space = " ";
  958. }
  959. return 0;
  960. }
  961. #if defined(CMAKE_BUILD_WITH_CMAKE)
  962. // Command to create a symbolic link. Fails on platforms not
  963. // supporting them.
  964. else if (args[1] == "environment" )
  965. {
  966. std::vector<std::string> env = cmSystemTools::GetEnvironmentVariables();
  967. std::vector<std::string>::iterator it;
  968. for ( it = env.begin(); it != env.end(); ++ it )
  969. {
  970. std::cout << it->c_str() << std::endl;
  971. }
  972. return 0;
  973. }
  974. #endif
  975. else if (args[1] == "make_directory" && args.size() == 3)
  976. {
  977. if(!cmSystemTools::MakeDirectory(args[2].c_str()))
  978. {
  979. std::cerr << "Error making directory \"" << args[2].c_str()
  980. << "\".\n";
  981. return 1;
  982. }
  983. return 0;
  984. }
  985. else if (args[1] == "remove_directory" && args.size() == 3)
  986. {
  987. if(cmSystemTools::FileIsDirectory(args[2].c_str()) &&
  988. !cmSystemTools::RemoveADirectory(args[2].c_str()))
  989. {
  990. std::cerr << "Error removing directory \"" << args[2].c_str()
  991. << "\".\n";
  992. return 1;
  993. }
  994. return 0;
  995. }
  996. // Remove file
  997. else if (args[1] == "remove" && args.size() > 2)
  998. {
  999. bool force = false;
  1000. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  1001. {
  1002. if(args[cc] == "\\-f" || args[cc] == "-f")
  1003. {
  1004. force = true;
  1005. }
  1006. else
  1007. {
  1008. // Complain if the file could not be removed, still exists,
  1009. // and the -f option was not given.
  1010. if(!cmSystemTools::RemoveFile(args[cc].c_str()) && !force &&
  1011. cmSystemTools::FileExists(args[cc].c_str()))
  1012. {
  1013. return 1;
  1014. }
  1015. }
  1016. }
  1017. return 0;
  1018. }
  1019. // Touch file
  1020. else if (args[1] == "touch" && args.size() > 2)
  1021. {
  1022. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  1023. {
  1024. // Complain if the file could not be removed, still exists,
  1025. // and the -f option was not given.
  1026. if(!cmSystemTools::Touch(args[cc].c_str(), true))
  1027. {
  1028. return 1;
  1029. }
  1030. }
  1031. return 0;
  1032. }
  1033. // Touch file
  1034. else if (args[1] == "touch_nocreate" && args.size() > 2)
  1035. {
  1036. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  1037. {
  1038. // Complain if the file could not be removed, still exists,
  1039. // and the -f option was not given.
  1040. if(!cmSystemTools::Touch(args[cc].c_str(), false))
  1041. {
  1042. return 1;
  1043. }
  1044. }
  1045. return 0;
  1046. }
  1047. // Clock command
  1048. else if (args[1] == "time" && args.size() > 2)
  1049. {
  1050. std::string command = args[2];
  1051. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  1052. {
  1053. command += " ";
  1054. command += args[cc];
  1055. }
  1056. clock_t clock_start, clock_finish;
  1057. time_t time_start, time_finish;
  1058. time(&time_start);
  1059. clock_start = clock();
  1060. cmSystemTools::RunSingleCommand(command.c_str());
  1061. clock_finish = clock();
  1062. time(&time_finish);
  1063. double clocks_per_sec = static_cast<double>(CLOCKS_PER_SEC);
  1064. std::cout << "Elapsed time: "
  1065. << static_cast<long>(time_finish - time_start) << " s. (time)"
  1066. << ", "
  1067. << static_cast<double>(clock_finish - clock_start) / clocks_per_sec
  1068. << " s. (clock)"
  1069. << "\n";
  1070. return 0;
  1071. }
  1072. // Command to calculate the md5sum of a file
  1073. else if (args[1] == "md5sum" && args.size() >= 3)
  1074. {
  1075. char md5out[32];
  1076. int retval = 0;
  1077. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  1078. {
  1079. const char *filename = args[cc].c_str();
  1080. // Cannot compute md5sum of a directory
  1081. if(cmSystemTools::FileIsDirectory(filename))
  1082. {
  1083. std::cerr << "Error: " << filename << " is a directory" << std::endl;
  1084. retval++;
  1085. }
  1086. else if(!cmSystemTools::ComputeFileMD5(filename, md5out))
  1087. {
  1088. // To mimic md5sum behavior in a shell:
  1089. std::cerr << filename << ": No such file or directory" << std::endl;
  1090. retval++;
  1091. }
  1092. else
  1093. {
  1094. std::cout << std::string(md5out,32) << " " << filename << std::endl;
  1095. }
  1096. }
  1097. return retval;
  1098. }
  1099. // Command to change directory and run a program.
  1100. else if (args[1] == "chdir" && args.size() >= 4)
  1101. {
  1102. std::string directory = args[2];
  1103. if(!cmSystemTools::FileExists(directory.c_str()))
  1104. {
  1105. cmSystemTools::Error("Directory does not exist for chdir command: ",
  1106. args[2].c_str());
  1107. return 0;
  1108. }
  1109. std::string command = "\"";
  1110. command += args[3];
  1111. command += "\"";
  1112. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  1113. {
  1114. command += " \"";
  1115. command += args[cc];
  1116. command += "\"";
  1117. }
  1118. int retval = 0;
  1119. int timeout = 0;
  1120. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  1121. directory.c_str(), true, timeout) )
  1122. {
  1123. return retval;
  1124. }
  1125. return 1;
  1126. }
  1127. // Command to start progress for a build
  1128. else if (args[1] == "cmake_progress_start" && args.size() == 4)
  1129. {
  1130. // bascially remove the directory
  1131. std::string dirName = args[2];
  1132. dirName += "/Progress";
  1133. cmSystemTools::RemoveADirectory(dirName.c_str());
  1134. // is the last argument a filename that exists?
  1135. FILE *countFile = fopen(args[3].c_str(),"r");
  1136. int count;
  1137. if (countFile)
  1138. {
  1139. fscanf(countFile,"%i",&count);
  1140. fclose(countFile);
  1141. }
  1142. else
  1143. {
  1144. count = atoi(args[3].c_str());
  1145. }
  1146. if (count)
  1147. {
  1148. cmSystemTools::MakeDirectory(dirName.c_str());
  1149. // write the count into the directory
  1150. std::string fName = dirName;
  1151. fName += "/count.txt";
  1152. FILE *progFile = fopen(fName.c_str(),"w");
  1153. if (progFile)
  1154. {
  1155. fprintf(progFile,"%i\n",count);
  1156. fclose(progFile);
  1157. }
  1158. }
  1159. return 0;
  1160. }
  1161. // Command to report progress for a build
  1162. else if (args[1] == "cmake_progress_report" && args.size() >= 3)
  1163. {
  1164. std::string dirName = args[2];
  1165. dirName += "/Progress";
  1166. std::string fName;
  1167. FILE *progFile;
  1168. // read the count
  1169. fName = dirName;
  1170. fName += "/count.txt";
  1171. progFile = fopen(fName.c_str(),"r");
  1172. int count = 0;
  1173. if (!progFile)
  1174. {
  1175. return 0;
  1176. }
  1177. else
  1178. {
  1179. fscanf(progFile,"%i",&count);
  1180. fclose(progFile);
  1181. }
  1182. unsigned int i;
  1183. for (i = 3; i < args.size(); ++i)
  1184. {
  1185. fName = dirName;
  1186. fName += "/";
  1187. fName += args[i];
  1188. progFile = fopen(fName.c_str(),"w");
  1189. if (progFile)
  1190. {
  1191. fprintf(progFile,"empty");
  1192. fclose(progFile);
  1193. }
  1194. }
  1195. int fileNum = static_cast<int>
  1196. (cmsys::Directory::GetNumberOfFilesInDirectory(dirName.c_str()));
  1197. if (count > 0)
  1198. {
  1199. // print the progress
  1200. fprintf(stdout,"[%3i%%] ",((fileNum-3)*100)/count);
  1201. }
  1202. return 0;
  1203. }
  1204. // Command to create a symbolic link. Fails on platforms not
  1205. // supporting them.
  1206. else if (args[1] == "create_symlink" && args.size() == 4)
  1207. {
  1208. const char* destinationFileName = args[3].c_str();
  1209. if ( cmSystemTools::FileExists(destinationFileName) )
  1210. {
  1211. if ( cmSystemTools::FileIsSymlink(destinationFileName) )
  1212. {
  1213. if ( !cmSystemTools::RemoveFile(destinationFileName) ||
  1214. cmSystemTools::FileExists(destinationFileName) )
  1215. {
  1216. return 0;
  1217. }
  1218. }
  1219. else
  1220. {
  1221. return 0;
  1222. }
  1223. }
  1224. return cmSystemTools::CreateSymlink(args[2].c_str(),
  1225. args[3].c_str())? 0:1;
  1226. }
  1227. // Internal CMake shared library support.
  1228. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  1229. {
  1230. int result = 0;
  1231. std::string realName = args[2];
  1232. std::string soName = args[3];
  1233. std::string name = args[4];
  1234. if(soName != realName)
  1235. {
  1236. std::string fname = cmSystemTools::GetFilenameName(realName);
  1237. if(cmSystemTools::FileExists(soName.c_str()))
  1238. {
  1239. cmSystemTools::RemoveFile(soName.c_str());
  1240. }
  1241. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  1242. {
  1243. result = 1;
  1244. }
  1245. }
  1246. if(name != soName)
  1247. {
  1248. std::string fname = cmSystemTools::GetFilenameName(soName);
  1249. if(cmSystemTools::FileExists(soName.c_str()))
  1250. {
  1251. cmSystemTools::RemoveFile(name.c_str());
  1252. }
  1253. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1254. {
  1255. result = 1;
  1256. }
  1257. }
  1258. return result;
  1259. }
  1260. // Internal CMake versioned executable support.
  1261. else if (args[1] == "cmake_symlink_executable" && args.size() == 4)
  1262. {
  1263. int result = 0;
  1264. std::string realName = args[2];
  1265. std::string name = args[3];
  1266. if(name != realName)
  1267. {
  1268. std::string fname = cmSystemTools::GetFilenameName(realName);
  1269. if(cmSystemTools::FileExists(realName.c_str()))
  1270. {
  1271. cmSystemTools::RemoveFile(name.c_str());
  1272. }
  1273. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1274. {
  1275. result = 1;
  1276. }
  1277. }
  1278. return result;
  1279. }
  1280. #if defined(CMAKE_HAVE_VS_GENERATORS)
  1281. // Internal CMake support for calling Visual Studio macros.
  1282. else if (args[1] == "cmake_call_visual_studio_macro" && args.size() >= 4)
  1283. {
  1284. // args[2] = full path to .sln file or "ALL"
  1285. // args[3] = name of Visual Studio macro to call
  1286. // args[4..args.size()-1] = [optional] args for Visual Studio macro
  1287. std::string macroArgs;
  1288. if (args.size() > 4)
  1289. {
  1290. macroArgs = args[4];
  1291. for (size_t i = 5; i < args.size(); ++i)
  1292. {
  1293. macroArgs += " ";
  1294. macroArgs += args[i];
  1295. }
  1296. }
  1297. return cmCallVisualStudioMacro::CallMacro(args[2], args[3], macroArgs);
  1298. }
  1299. #endif
  1300. // Internal CMake dependency scanning support.
  1301. else if (args[1] == "cmake_depends" && args.size() >= 6)
  1302. {
  1303. // Use the make system's VERBOSE environment variable to enable
  1304. // verbose output.
  1305. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  1306. // Create a cmake object instance to process dependencies.
  1307. cmake cm;
  1308. std::string gen;
  1309. std::string homeDir;
  1310. std::string startDir;
  1311. std::string homeOutDir;
  1312. std::string startOutDir;
  1313. std::string depInfo;
  1314. bool color = true;
  1315. if(args.size() >= 8)
  1316. {
  1317. // Full signature:
  1318. //
  1319. // -E cmake_depends <generator>
  1320. // <home-src-dir> <start-src-dir>
  1321. // <home-out-dir> <start-out-dir>
  1322. // <dep-info> [--color=$(COLOR)]
  1323. //
  1324. // All paths are provided.
  1325. gen = args[2];
  1326. homeDir = args[3];
  1327. startDir = args[4];
  1328. homeOutDir = args[5];
  1329. startOutDir = args[6];
  1330. depInfo = args[7];
  1331. if(args.size() >= 9 &&
  1332. args[8].length() > 8 &&
  1333. args[8].substr(0, 8) == "--color=")
  1334. {
  1335. // Enable or disable color based on the switch value.
  1336. color = cmSystemTools::IsOn(args[8].substr(8).c_str());
  1337. }
  1338. }
  1339. else
  1340. {
  1341. // Support older signature for existing makefiles:
  1342. //
  1343. // -E cmake_depends <generator>
  1344. // <home-out-dir> <start-out-dir>
  1345. // <dep-info>
  1346. //
  1347. // Just pretend the source directories are the same as the
  1348. // binary directories so at least scanning will work.
  1349. gen = args[2];
  1350. homeDir = args[3];
  1351. startDir = args[4];
  1352. homeOutDir = args[3];
  1353. startOutDir = args[3];
  1354. depInfo = args[5];
  1355. }
  1356. // Create a local generator configured for the directory in
  1357. // which dependencies will be scanned.
  1358. homeDir = cmSystemTools::CollapseFullPath(homeDir.c_str());
  1359. startDir = cmSystemTools::CollapseFullPath(startDir.c_str());
  1360. homeOutDir = cmSystemTools::CollapseFullPath(homeOutDir.c_str());
  1361. startOutDir = cmSystemTools::CollapseFullPath(startOutDir.c_str());
  1362. cm.SetHomeDirectory(homeDir.c_str());
  1363. cm.SetStartDirectory(startDir.c_str());
  1364. cm.SetHomeOutputDirectory(homeOutDir.c_str());
  1365. cm.SetStartOutputDirectory(startOutDir.c_str());
  1366. if(cmGlobalGenerator* ggd = cm.CreateGlobalGenerator(gen.c_str()))
  1367. {
  1368. cm.SetGlobalGenerator(ggd);
  1369. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1370. lgd->SetGlobalGenerator(ggd);
  1371. lgd->GetMakefile()->SetStartDirectory(startDir.c_str());
  1372. lgd->GetMakefile()->SetStartOutputDirectory(startOutDir.c_str());
  1373. lgd->GetMakefile()->MakeStartDirectoriesCurrent();
  1374. // Actually scan dependencies.
  1375. return lgd->UpdateDependencies(depInfo.c_str(),
  1376. verbose, color)? 0 : 2;
  1377. }
  1378. return 1;
  1379. }
  1380. // Internal CMake link script support.
  1381. else if (args[1] == "cmake_link_script" && args.size() >= 3)
  1382. {
  1383. return cmake::ExecuteLinkScript(args);
  1384. }
  1385. // Internal CMake unimplemented feature notification.
  1386. else if (args[1] == "cmake_unimplemented_variable")
  1387. {
  1388. std::cerr << "Feature not implemented for this platform.";
  1389. if(args.size() == 3)
  1390. {
  1391. std::cerr << " Variable " << args[2] << " is not set.";
  1392. }
  1393. std::cerr << std::endl;
  1394. return 1;
  1395. }
  1396. else if (args[1] == "vs_link_exe")
  1397. {
  1398. return cmake::VisualStudioLink(args, 1);
  1399. }
  1400. else if (args[1] == "vs_link_dll")
  1401. {
  1402. return cmake::VisualStudioLink(args, 2);
  1403. }
  1404. #ifdef CMAKE_BUILD_WITH_CMAKE
  1405. // Internal CMake color makefile support.
  1406. else if (args[1] == "cmake_echo_color")
  1407. {
  1408. return cmake::ExecuteEchoColor(args);
  1409. }
  1410. #endif
  1411. // Tar files
  1412. else if (args[1] == "tar" && args.size() > 3)
  1413. {
  1414. std::string flags = args[2];
  1415. std::string outFile = args[3];
  1416. std::vector<cmStdString> files;
  1417. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  1418. {
  1419. files.push_back(args[cc]);
  1420. }
  1421. bool gzip = false;
  1422. bool verbose = false;
  1423. if ( flags.find_first_of('z') != flags.npos )
  1424. {
  1425. gzip = true;
  1426. }
  1427. if ( flags.find_first_of('v') != flags.npos )
  1428. {
  1429. verbose = true;
  1430. }
  1431. if ( flags.find_first_of('t') != flags.npos )
  1432. {
  1433. if ( !cmSystemTools::ListTar(outFile.c_str(), files, gzip, verbose) )
  1434. {
  1435. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1436. return 1;
  1437. }
  1438. }
  1439. else if ( flags.find_first_of('c') != flags.npos )
  1440. {
  1441. if ( !cmSystemTools::CreateTar(
  1442. outFile.c_str(), files, gzip, verbose) )
  1443. {
  1444. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1445. return 1;
  1446. }
  1447. }
  1448. else if ( flags.find_first_of('x') != flags.npos )
  1449. {
  1450. if ( !cmSystemTools::ExtractTar(
  1451. outFile.c_str(), files, gzip, verbose) )
  1452. {
  1453. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  1454. return 1;
  1455. }
  1456. }
  1457. return 0;
  1458. }
  1459. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1460. // Internal CMake Fortran module support.
  1461. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  1462. {
  1463. return cmDependsFortran::CopyModule(args)? 0 : 1;
  1464. }
  1465. #endif
  1466. #if defined(_WIN32) && !defined(__CYGWIN__)
  1467. // Write registry value
  1468. else if (args[1] == "write_regv" && args.size() > 3)
  1469. {
  1470. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  1471. args[3].c_str()) ? 0 : 1;
  1472. }
  1473. // Delete registry value
  1474. else if (args[1] == "delete_regv" && args.size() > 2)
  1475. {
  1476. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  1477. }
  1478. // Remove file
  1479. else if (args[1] == "comspec" && args.size() > 2)
  1480. {
  1481. unsigned int cc;
  1482. std::string command = args[2];
  1483. for ( cc = 3; cc < args.size(); cc ++ )
  1484. {
  1485. command += " " + args[cc];
  1486. }
  1487. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  1488. }
  1489. #endif
  1490. }
  1491. ::CMakeCommandUsage(args[0].c_str());
  1492. return 1;
  1493. }
  1494. void cmake::AddExtraGenerator(const char* name,
  1495. CreateExtraGeneratorFunctionType newFunction)
  1496. {
  1497. cmExternalMakefileProjectGenerator* extraGenerator = newFunction();
  1498. const std::vector<std::string>& supportedGlobalGenerators =
  1499. extraGenerator->GetSupportedGlobalGenerators();
  1500. for(std::vector<std::string>::const_iterator
  1501. it = supportedGlobalGenerators.begin();
  1502. it != supportedGlobalGenerators.end();
  1503. ++it )
  1504. {
  1505. std::string fullName = cmExternalMakefileProjectGenerator::
  1506. CreateFullGeneratorName(it->c_str(), name);
  1507. this->ExtraGenerators[fullName.c_str()] = newFunction;
  1508. }
  1509. delete extraGenerator;
  1510. }
  1511. void cmake::AddDefaultExtraGenerators()
  1512. {
  1513. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1514. #if defined(_WIN32) && !defined(__CYGWIN__)
  1515. // e.g. kdevelop4 ?
  1516. #endif
  1517. #if !defined(__CYGWIN__)
  1518. this->AddExtraGenerator(cmExtraCodeBlocksGenerator::GetActualName(),
  1519. &cmExtraCodeBlocksGenerator::New);
  1520. #endif
  1521. #ifdef CMAKE_USE_ECLIPSE
  1522. this->AddExtraGenerator(cmExtraEclipseCDT4Generator::GetActualName(),
  1523. &cmExtraEclipseCDT4Generator::New);
  1524. #endif
  1525. #ifdef CMAKE_USE_KDEVELOP
  1526. this->AddExtraGenerator(cmGlobalKdevelopGenerator::GetActualName(),
  1527. &cmGlobalKdevelopGenerator::New);
  1528. // for kdevelop also add the generator with just the name of the
  1529. // extra generator, since it was this way since cmake 2.2
  1530. this->ExtraGenerators[cmGlobalKdevelopGenerator::GetActualName()]
  1531. = &cmGlobalKdevelopGenerator::New;
  1532. #endif
  1533. #endif
  1534. }
  1535. //----------------------------------------------------------------------------
  1536. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  1537. {
  1538. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1539. i != this->Generators.end(); ++i)
  1540. {
  1541. names.push_back(i->first);
  1542. }
  1543. for(RegisteredExtraGeneratorsMap::const_iterator
  1544. i = this->ExtraGenerators.begin();
  1545. i != this->ExtraGenerators.end(); ++i)
  1546. {
  1547. names.push_back(i->first);
  1548. }
  1549. }
  1550. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  1551. {
  1552. cmGlobalGenerator* generator = 0;
  1553. cmExternalMakefileProjectGenerator* extraGenerator = 0;
  1554. RegisteredGeneratorsMap::const_iterator genIt = this->Generators.find(name);
  1555. if(genIt == this->Generators.end())
  1556. {
  1557. RegisteredExtraGeneratorsMap::const_iterator extraGenIt =
  1558. this->ExtraGenerators.find(name);
  1559. if (extraGenIt == this->ExtraGenerators.end())
  1560. {
  1561. return 0;
  1562. }
  1563. extraGenerator = (extraGenIt->second)();
  1564. genIt=this->Generators.find(extraGenerator->GetGlobalGeneratorName(name));
  1565. if(genIt == this->Generators.end())
  1566. {
  1567. delete extraGenerator;
  1568. return 0;
  1569. }
  1570. }
  1571. generator = (genIt->second)();
  1572. generator->SetCMakeInstance(this);
  1573. generator->SetExternalMakefileProjectGenerator(extraGenerator);
  1574. return generator;
  1575. }
  1576. void cmake::SetHomeDirectory(const char* dir)
  1577. {
  1578. this->cmHomeDirectory = dir;
  1579. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1580. }
  1581. void cmake::SetHomeOutputDirectory(const char* lib)
  1582. {
  1583. this->HomeOutputDirectory = lib;
  1584. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1585. }
  1586. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  1587. {
  1588. if(!gg)
  1589. {
  1590. cmSystemTools::Error("Error SetGlobalGenerator called with null");
  1591. return;
  1592. }
  1593. // delete the old generator
  1594. if (this->GlobalGenerator)
  1595. {
  1596. delete this->GlobalGenerator;
  1597. // restore the original environment variables CXX and CC
  1598. // Restor CC
  1599. std::string env = "CC=";
  1600. if(this->CCEnvironment.size())
  1601. {
  1602. env += this->CCEnvironment;
  1603. }
  1604. cmSystemTools::PutEnv(env.c_str());
  1605. env = "CXX=";
  1606. if(this->CXXEnvironment.size())
  1607. {
  1608. env += this->CXXEnvironment;
  1609. }
  1610. cmSystemTools::PutEnv(env.c_str());
  1611. }
  1612. // set the new
  1613. this->GlobalGenerator = gg;
  1614. // set the global flag for unix style paths on cmSystemTools as soon as
  1615. // the generator is set. This allows gmake to be used on windows.
  1616. cmSystemTools::SetForceUnixPaths
  1617. (this->GlobalGenerator->GetForceUnixPaths());
  1618. // Save the environment variables CXX and CC
  1619. const char* cxx = getenv("CXX");
  1620. const char* cc = getenv("CC");
  1621. if(cxx)
  1622. {
  1623. this->CXXEnvironment = cxx;
  1624. }
  1625. else
  1626. {
  1627. this->CXXEnvironment = "";
  1628. }
  1629. if(cc)
  1630. {
  1631. this->CCEnvironment = cc;
  1632. }
  1633. else
  1634. {
  1635. this->CCEnvironment = "";
  1636. }
  1637. // set the cmake instance just to be sure
  1638. gg->SetCMakeInstance(this);
  1639. }
  1640. int cmake::DoPreConfigureChecks()
  1641. {
  1642. // Make sure the Start directory contains a CMakeLists.txt file.
  1643. std::string srcList = this->GetHomeDirectory();
  1644. srcList += "/CMakeLists.txt";
  1645. if(!cmSystemTools::FileExists(srcList.c_str()))
  1646. {
  1647. cmOStringStream err;
  1648. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1649. {
  1650. err << "The source directory \"" << this->GetHomeDirectory()
  1651. << "\" does not appear to contain CMakeLists.txt.\n";
  1652. }
  1653. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1654. {
  1655. err << "The source directory \"" << this->GetHomeDirectory()
  1656. << "\" is a file, not a directory.\n";
  1657. }
  1658. else
  1659. {
  1660. err << "The source directory \"" << this->GetHomeDirectory()
  1661. << "\" does not exist.\n";
  1662. }
  1663. err << "Specify --help for usage, or press the help button on the CMake "
  1664. "GUI.";
  1665. cmSystemTools::Error(err.str().c_str());
  1666. return -2;
  1667. }
  1668. // do a sanity check on some values
  1669. if(this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1670. {
  1671. std::string cacheStart =
  1672. this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1673. cacheStart += "/CMakeLists.txt";
  1674. std::string currentStart = this->GetHomeDirectory();
  1675. currentStart += "/CMakeLists.txt";
  1676. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1677. {
  1678. std::string message = "The source \"";
  1679. message += currentStart;
  1680. message += "\" does not match the source \"";
  1681. message += cacheStart;
  1682. message += "\" used to generate cache. ";
  1683. message += "Re-run cmake with a different source directory.";
  1684. cmSystemTools::Error(message.c_str());
  1685. return -2;
  1686. }
  1687. }
  1688. else
  1689. {
  1690. return 0;
  1691. }
  1692. return 1;
  1693. }
  1694. struct SaveCacheEntry
  1695. {
  1696. std::string key;
  1697. std::string value;
  1698. std::string help;
  1699. cmCacheManager::CacheEntryType type;
  1700. };
  1701. int cmake::HandleDeleteCacheVariables(const char* var)
  1702. {
  1703. std::vector<std::string> argsSplit;
  1704. cmSystemTools::ExpandListArgument(std::string(var), argsSplit);
  1705. // erase the property to avoid infinite recursion
  1706. this->SetProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_", "");
  1707. cmCacheManager::CacheIterator ci = this->CacheManager->NewIterator();
  1708. std::vector<SaveCacheEntry> saved;
  1709. cmOStringStream warning;
  1710. warning
  1711. << "You have changed variables that require your cache to be deleted.\n"
  1712. << "Configure will be re-run and you may have to reset some variables.\n"
  1713. << "The following variables have changed:\n";
  1714. for(std::vector<std::string>::iterator i = argsSplit.begin();
  1715. i != argsSplit.end(); ++i)
  1716. {
  1717. SaveCacheEntry save;
  1718. save.key = *i;
  1719. warning << *i << "= ";
  1720. i++;
  1721. save.value = *i;
  1722. warning << *i << "\n";
  1723. if(ci.Find(save.key.c_str()))
  1724. {
  1725. save.type = ci.GetType();
  1726. save.help = ci.GetProperty("HELPSTRING");
  1727. }
  1728. saved.push_back(save);
  1729. }
  1730. // remove the cache
  1731. this->CacheManager->DeleteCache(this->GetStartOutputDirectory());
  1732. // load the empty cache
  1733. this->LoadCache();
  1734. // restore the changed compilers
  1735. for(std::vector<SaveCacheEntry>::iterator i = saved.begin();
  1736. i != saved.end(); ++i)
  1737. {
  1738. this->AddCacheEntry(i->key.c_str(), i->value.c_str(),
  1739. i->help.c_str(), i->type);
  1740. }
  1741. cmSystemTools::Message(warning.str().c_str());
  1742. // avoid reconfigure if there were errors
  1743. if(!cmSystemTools::GetErrorOccuredFlag())
  1744. {
  1745. // re-run configure
  1746. return this->Configure();
  1747. }
  1748. return 0;
  1749. }
  1750. int cmake::Configure()
  1751. {
  1752. int ret = this->ActualConfigure();
  1753. const char* delCacheVars =
  1754. this->GetProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_");
  1755. if(delCacheVars && delCacheVars[0] != 0)
  1756. {
  1757. return this->HandleDeleteCacheVariables(delCacheVars);
  1758. }
  1759. return ret;
  1760. }
  1761. int cmake::ActualConfigure()
  1762. {
  1763. // Construct right now our path conversion table before it's too late:
  1764. this->UpdateConversionPathTable();
  1765. this->CleanupCommandsAndMacros();
  1766. int res = 0;
  1767. if ( !this->ScriptMode )
  1768. {
  1769. res = this->DoPreConfigureChecks();
  1770. }
  1771. if ( res < 0 )
  1772. {
  1773. return -2;
  1774. }
  1775. if ( !res )
  1776. {
  1777. this->CacheManager->AddCacheEntry
  1778. ("CMAKE_HOME_DIRECTORY",
  1779. this->GetHomeDirectory(),
  1780. "Start directory with the top level CMakeLists.txt file for this "
  1781. "project",
  1782. cmCacheManager::INTERNAL);
  1783. }
  1784. // no generator specified on the command line
  1785. if(!this->GlobalGenerator)
  1786. {
  1787. const char* genName =
  1788. this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1789. const char* extraGenName =
  1790. this->CacheManager->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  1791. if(genName)
  1792. {
  1793. std::string fullName = cmExternalMakefileProjectGenerator::
  1794. CreateFullGeneratorName(genName, extraGenName);
  1795. this->GlobalGenerator = this->CreateGlobalGenerator(fullName.c_str());
  1796. }
  1797. if(this->GlobalGenerator)
  1798. {
  1799. // set the global flag for unix style paths on cmSystemTools as
  1800. // soon as the generator is set. This allows gmake to be used
  1801. // on windows.
  1802. cmSystemTools::SetForceUnixPaths
  1803. (this->GlobalGenerator->GetForceUnixPaths());
  1804. }
  1805. else
  1806. {
  1807. #if defined(__BORLANDC__) && defined(_WIN32)
  1808. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1809. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1810. std::string installedCompiler;
  1811. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1812. "\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  1813. cmSystemTools::ExpandRegistryValues(mp);
  1814. if (!(mp == "/registry"))
  1815. {
  1816. installedCompiler = "Visual Studio 8 2005";
  1817. }
  1818. else
  1819. {
  1820. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1821. "\\VisualStudio\\7.1;InstallDir]";
  1822. cmSystemTools::ExpandRegistryValues(mp);
  1823. if (!(mp == "/registry"))
  1824. {
  1825. installedCompiler = "Visual Studio 7 .NET 2003";
  1826. }
  1827. else
  1828. {
  1829. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1830. "\\VisualStudio\\7.0;InstallDir]";
  1831. cmSystemTools::ExpandRegistryValues(mp);
  1832. if (!(mp == "/registry"))
  1833. {
  1834. installedCompiler = "Visual Studio 7";
  1835. }
  1836. else
  1837. {
  1838. installedCompiler = "Visual Studio 6";
  1839. }
  1840. }
  1841. }
  1842. cmGlobalGenerator* gen
  1843. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1844. if(!gen)
  1845. {
  1846. gen = new cmGlobalNMakeMakefileGenerator;
  1847. }
  1848. this->SetGlobalGenerator(gen);
  1849. #else
  1850. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1851. #endif
  1852. }
  1853. if(!this->GlobalGenerator)
  1854. {
  1855. cmSystemTools::Error("Could not create generator");
  1856. return -1;
  1857. }
  1858. }
  1859. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1860. if(genName)
  1861. {
  1862. if(strcmp(this->GlobalGenerator->GetName(), genName) != 0)
  1863. {
  1864. std::string message = "Error: generator : ";
  1865. message += this->GlobalGenerator->GetName();
  1866. message += "\nDoes not match the generator used previously: ";
  1867. message += genName;
  1868. message +=
  1869. "\nEither remove the CMakeCache.txt file or choose a different"
  1870. " binary directory.";
  1871. cmSystemTools::Error(message.c_str());
  1872. return -2;
  1873. }
  1874. }
  1875. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1876. {
  1877. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1878. this->GlobalGenerator->GetName(),
  1879. "Name of generator.",
  1880. cmCacheManager::INTERNAL);
  1881. this->CacheManager->AddCacheEntry("CMAKE_EXTRA_GENERATOR",
  1882. this->GlobalGenerator->GetExtraGeneratorName(),
  1883. "Name of external makefile project generator.",
  1884. cmCacheManager::INTERNAL);
  1885. }
  1886. // reset any system configuration information, except for when we are
  1887. // InTryCompile. With TryCompile the system info is taken from the parent's
  1888. // info to save time
  1889. if (!this->InTryCompile)
  1890. {
  1891. this->GlobalGenerator->ClearEnabledLanguages();
  1892. }
  1893. this->CleanupWrittenFiles();
  1894. // Truncate log files
  1895. if (!this->InTryCompile)
  1896. {
  1897. this->TruncateOutputLog("CMakeOutput.log");
  1898. this->TruncateOutputLog("CMakeError.log");
  1899. }
  1900. // actually do the configure
  1901. this->GlobalGenerator->Configure();
  1902. // Before saving the cache
  1903. // if the project did not define one of the entries below, add them now
  1904. // so users can edit the values in the cache:
  1905. // LIBRARY_OUTPUT_PATH
  1906. // EXECUTABLE_OUTPUT_PATH
  1907. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1908. {
  1909. this->CacheManager->AddCacheEntry
  1910. ("LIBRARY_OUTPUT_PATH", "",
  1911. "Single output directory for building all libraries.",
  1912. cmCacheManager::PATH);
  1913. }
  1914. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1915. {
  1916. this->CacheManager->AddCacheEntry
  1917. ("EXECUTABLE_OUTPUT_PATH", "",
  1918. "Single output directory for building all executables.",
  1919. cmCacheManager::PATH);
  1920. }
  1921. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1922. {
  1923. this->CacheManager->AddCacheEntry
  1924. ("CMAKE_USE_RELATIVE_PATHS", false,
  1925. "If true, cmake will use relative paths in makefiles and projects.");
  1926. cmCacheManager::CacheIterator it =
  1927. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1928. if ( !it.PropertyExists("ADVANCED") )
  1929. {
  1930. it.SetProperty("ADVANCED", "1");
  1931. }
  1932. }
  1933. if(cmSystemTools::GetFatalErrorOccured() &&
  1934. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1935. cmSystemTools::IsOff(this->CacheManager->
  1936. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1937. {
  1938. // We must have a bad generator selection. Wipe the cache entry so the
  1939. // user can select another.
  1940. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1941. this->CacheManager->RemoveCacheEntry("CMAKE_EXTRA_GENERATOR");
  1942. }
  1943. // only save the cache if there were no fatal errors
  1944. if ( !this->ScriptMode )
  1945. {
  1946. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1947. }
  1948. if ( !this->GraphVizFile.empty() )
  1949. {
  1950. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1951. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1952. }
  1953. if(cmSystemTools::GetErrorOccuredFlag())
  1954. {
  1955. return -1;
  1956. }
  1957. return 0;
  1958. }
  1959. bool cmake::CacheVersionMatches()
  1960. {
  1961. const char* majv =
  1962. this->CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1963. const char* minv =
  1964. this->CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1965. const char* relv =
  1966. this->CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1967. bool cacheSameCMake = false;
  1968. if(majv &&
  1969. atoi(majv) == static_cast<int>(cmVersion::GetMajorVersion())
  1970. && minv &&
  1971. atoi(minv) == static_cast<int>(cmVersion::GetMinorVersion())
  1972. && relv && (strcmp(relv, cmVersion::GetReleaseVersion().c_str()) == 0))
  1973. {
  1974. cacheSameCMake = true;
  1975. }
  1976. return cacheSameCMake;
  1977. }
  1978. void cmake::PreLoadCMakeFiles()
  1979. {
  1980. std::string pre_load = this->GetHomeDirectory();
  1981. if ( pre_load.size() > 0 )
  1982. {
  1983. pre_load += "/PreLoad.cmake";
  1984. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1985. {
  1986. this->ReadListFile(pre_load.c_str());
  1987. }
  1988. }
  1989. pre_load = this->GetHomeOutputDirectory();
  1990. if ( pre_load.size() > 0 )
  1991. {
  1992. pre_load += "/PreLoad.cmake";
  1993. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1994. {
  1995. this->ReadListFile(pre_load.c_str());
  1996. }
  1997. }
  1998. }
  1999. // handle a command line invocation
  2000. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  2001. {
  2002. // Process the arguments
  2003. this->SetArgs(args);
  2004. if(cmSystemTools::GetErrorOccuredFlag())
  2005. {
  2006. return -1;
  2007. }
  2008. // If we are given a stamp file check if it is really out of date.
  2009. if(!this->CheckStampFile.empty() &&
  2010. cmakeCheckStampFile(this->CheckStampFile.c_str()))
  2011. {
  2012. return 0;
  2013. }
  2014. // set the cmake command
  2015. this->CMakeCommand = args[0];
  2016. if ( !this->ScriptMode )
  2017. {
  2018. // load the cache
  2019. if(this->LoadCache() < 0)
  2020. {
  2021. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  2022. return -1;
  2023. }
  2024. }
  2025. else
  2026. {
  2027. this->AddCMakePaths();
  2028. }
  2029. // Add any cache args
  2030. if ( !this->SetCacheArgs(args) )
  2031. {
  2032. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  2033. return -1;
  2034. }
  2035. // In script mode we terminate after running the script.
  2036. if(this->ScriptMode)
  2037. {
  2038. if(cmSystemTools::GetErrorOccuredFlag())
  2039. {
  2040. return -1;
  2041. }
  2042. else
  2043. {
  2044. return 0;
  2045. }
  2046. }
  2047. this->PreLoadCMakeFiles();
  2048. std::string systemFile = this->GetHomeOutputDirectory();
  2049. systemFile += "/CMakeSystem.cmake";
  2050. if ( noconfigure )
  2051. {
  2052. return 0;
  2053. }
  2054. // now run the global generate
  2055. // Check the state of the build system to see if we need to regenerate.
  2056. if(!this->CheckBuildSystem())
  2057. {
  2058. return 0;
  2059. }
  2060. // If we are doing global generate, we better set start and start
  2061. // output directory to the root of the project.
  2062. std::string oldstartdir = this->GetStartDirectory();
  2063. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  2064. this->SetStartDirectory(this->GetHomeDirectory());
  2065. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  2066. int ret = this->Configure();
  2067. if (ret || this->ScriptMode)
  2068. {
  2069. #if defined(CMAKE_HAVE_VS_GENERATORS)
  2070. if(!this->VSSolutionFile.empty() && this->GlobalGenerator)
  2071. {
  2072. // CMake is running to regenerate a Visual Studio build tree
  2073. // during a build from the VS IDE. The build files cannot be
  2074. // regenerated, so we should stop the build.
  2075. cmSystemTools::Message(
  2076. "CMake Configure step failed. "
  2077. "Build files cannot be regenerated correctly. "
  2078. "Attempting to stop IDE build.");
  2079. cmGlobalVisualStudioGenerator* gg =
  2080. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  2081. gg->CallVisualStudioMacro(cmGlobalVisualStudioGenerator::MacroStop,
  2082. this->VSSolutionFile.c_str());
  2083. }
  2084. #endif
  2085. return ret;
  2086. }
  2087. ret = this->Generate();
  2088. std::string message = "Build files have been written to: ";
  2089. message += this->GetHomeOutputDirectory();
  2090. this->UpdateProgress(message.c_str(), -1);
  2091. if(ret)
  2092. {
  2093. return ret;
  2094. }
  2095. this->SetStartDirectory(oldstartdir.c_str());
  2096. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  2097. return ret;
  2098. }
  2099. int cmake::Generate()
  2100. {
  2101. if(!this->GlobalGenerator)
  2102. {
  2103. return -1;
  2104. }
  2105. this->GlobalGenerator->Generate();
  2106. if(cmSystemTools::GetErrorOccuredFlag())
  2107. {
  2108. return -1;
  2109. }
  2110. if (this->GetProperty("REPORT_UNDEFINED_PROPERTIES"))
  2111. {
  2112. this->ReportUndefinedPropertyAccesses
  2113. (this->GetProperty("REPORT_UNDEFINED_PROPERTIES"));
  2114. }
  2115. return 0;
  2116. }
  2117. void cmake::AddCacheEntry(const char* key, const char* value,
  2118. const char* helpString,
  2119. int type)
  2120. {
  2121. this->CacheManager->AddCacheEntry(key, value,
  2122. helpString,
  2123. cmCacheManager::CacheEntryType(type));
  2124. }
  2125. const char* cmake::GetCacheDefinition(const char* name) const
  2126. {
  2127. return this->CacheManager->GetCacheValue(name);
  2128. }
  2129. int cmake::DumpDocumentationToFile(std::ostream& f)
  2130. {
  2131. #ifdef CMAKE_BUILD_WITH_CMAKE
  2132. // Loop over all registered commands and print out documentation
  2133. const char *name;
  2134. const char *terse;
  2135. const char *full;
  2136. char tmp[1024];
  2137. sprintf(tmp,"Version %d.%d (%s)", cmVersion::GetMajorVersion(),
  2138. cmVersion::GetMinorVersion(),
  2139. cmVersion::GetReleaseVersion().c_str());
  2140. f << "<html>\n";
  2141. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  2142. f << "<ul>\n";
  2143. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  2144. j != this->Commands.end(); ++j)
  2145. {
  2146. name = (*j).second->GetName();
  2147. terse = (*j).second->GetTerseDocumentation();
  2148. full = (*j).second->GetFullDocumentation();
  2149. f << "<li><b>" << name << "</b> - " << terse << std::endl
  2150. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  2151. }
  2152. f << "</ul></html>\n";
  2153. #else
  2154. (void)f;
  2155. #endif
  2156. return 1;
  2157. }
  2158. void cmake::AddDefaultCommands()
  2159. {
  2160. std::list<cmCommand*> commands;
  2161. GetBootstrapCommands(commands);
  2162. GetPredefinedCommands(commands);
  2163. for(std::list<cmCommand*>::iterator i = commands.begin();
  2164. i != commands.end(); ++i)
  2165. {
  2166. this->AddCommand(*i);
  2167. }
  2168. }
  2169. void cmake::AddDefaultGenerators()
  2170. {
  2171. #if defined(_WIN32) && !defined(__CYGWIN__)
  2172. # if !defined(CMAKE_BOOT_MINGW)
  2173. this->Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  2174. &cmGlobalVisualStudio6Generator::New;
  2175. this->Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  2176. &cmGlobalVisualStudio7Generator::New;
  2177. this->Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  2178. &cmGlobalVisualStudio71Generator::New;
  2179. this->Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  2180. &cmGlobalVisualStudio8Generator::New;
  2181. this->Generators[cmGlobalVisualStudio9Generator::GetActualName()] =
  2182. &cmGlobalVisualStudio9Generator::New;
  2183. this->Generators[cmGlobalVisualStudio9Win64Generator::GetActualName()] =
  2184. &cmGlobalVisualStudio9Win64Generator::New;
  2185. this->Generators[cmGlobalVisualStudio8Win64Generator::GetActualName()] =
  2186. &cmGlobalVisualStudio8Win64Generator::New;
  2187. this->Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  2188. &cmGlobalBorlandMakefileGenerator::New;
  2189. this->Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  2190. &cmGlobalNMakeMakefileGenerator::New;
  2191. this->Generators[cmGlobalWatcomWMakeGenerator::GetActualName()] =
  2192. &cmGlobalWatcomWMakeGenerator::New;
  2193. # endif
  2194. this->Generators[cmGlobalMSYSMakefileGenerator::GetActualName()] =
  2195. &cmGlobalMSYSMakefileGenerator::New;
  2196. this->Generators[cmGlobalMinGWMakefileGenerator::GetActualName()] =
  2197. &cmGlobalMinGWMakefileGenerator::New;
  2198. #endif
  2199. this->Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
  2200. &cmGlobalUnixMakefileGenerator3::New;
  2201. #ifdef CMAKE_USE_XCODE
  2202. this->Generators[cmGlobalXCodeGenerator::GetActualName()] =
  2203. &cmGlobalXCodeGenerator::New;
  2204. #endif
  2205. }
  2206. int cmake::LoadCache()
  2207. {
  2208. // could we not read the cache
  2209. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  2210. {
  2211. // if it does exist, but isn;t readable then warn the user
  2212. std::string cacheFile = this->GetHomeOutputDirectory();
  2213. cacheFile += "/CMakeCache.txt";
  2214. if(cmSystemTools::FileExists(cacheFile.c_str()))
  2215. {
  2216. cmSystemTools::Error(
  2217. "There is a CMakeCache.txt file for the current binary tree but "
  2218. "cmake does not have permission to read it. Please check the "
  2219. "permissions of the directory you are trying to run CMake on.");
  2220. return -1;
  2221. }
  2222. }
  2223. if (this->CMakeCommand.size() < 2)
  2224. {
  2225. cmSystemTools::Error(
  2226. "cmake command was not specified prior to loading the cache in "
  2227. "cmake.cxx");
  2228. return -1;
  2229. }
  2230. // setup CMAKE_ROOT and CMAKE_COMMAND
  2231. if(!this->AddCMakePaths())
  2232. {
  2233. return -3;
  2234. }
  2235. return 0;
  2236. }
  2237. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  2238. {
  2239. this->ProgressCallback = f;
  2240. this->ProgressCallbackClientData = cd;
  2241. }
  2242. void cmake::UpdateProgress(const char *msg, float prog)
  2243. {
  2244. if(this->ProgressCallback && !this->InTryCompile)
  2245. {
  2246. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  2247. return;
  2248. }
  2249. }
  2250. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v,
  2251. bool withCurrentCommands,
  2252. bool withCompatCommands) const
  2253. {
  2254. for(RegisteredCommandsMap::const_iterator j = this->Commands.begin();
  2255. j != this->Commands.end(); ++j)
  2256. {
  2257. if ((( withCompatCommands == false) && ( (*j).second->IsDiscouraged()))
  2258. || ((withCurrentCommands == false) && (!(*j).second->IsDiscouraged())))
  2259. {
  2260. continue;
  2261. }
  2262. cmDocumentationEntry e((*j).second->GetName(),
  2263. (*j).second->GetTerseDocumentation(),
  2264. (*j).second->GetFullDocumentation());
  2265. v.push_back(e);
  2266. }
  2267. }
  2268. void cmake::GetPolicyDocumentation(std::vector<cmDocumentationEntry>& v)
  2269. {
  2270. this->Policies->GetDocumentation(v);
  2271. }
  2272. void cmake::GetPropertiesDocumentation(std::map<std::string,
  2273. cmDocumentationSection *>& v)
  2274. {
  2275. // loop over the properties and put them into the doc structure
  2276. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::iterator i;
  2277. i = this->PropertyDefinitions.begin();
  2278. for (;i != this->PropertyDefinitions.end(); ++i)
  2279. {
  2280. i->second.GetPropertiesDocumentation(v);
  2281. }
  2282. }
  2283. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  2284. {
  2285. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  2286. i != this->Generators.end(); ++i)
  2287. {
  2288. cmDocumentationEntry e;
  2289. cmGlobalGenerator* generator = (i->second)();
  2290. generator->GetDocumentation(e);
  2291. delete generator;
  2292. v.push_back(e);
  2293. }
  2294. for(RegisteredExtraGeneratorsMap::const_iterator
  2295. i = this->ExtraGenerators.begin(); i != this->ExtraGenerators.end(); ++i)
  2296. {
  2297. cmDocumentationEntry e;
  2298. cmExternalMakefileProjectGenerator* generator = (i->second)();
  2299. generator->GetDocumentation(e, i->first.c_str());
  2300. e.Name = i->first;
  2301. delete generator;
  2302. v.push_back(e);
  2303. }
  2304. }
  2305. void cmake::AddWrittenFile(const char* file)
  2306. {
  2307. this->WrittenFiles.insert(file);
  2308. }
  2309. bool cmake::HasWrittenFile(const char* file)
  2310. {
  2311. return this->WrittenFiles.find(file) != this->WrittenFiles.end();
  2312. }
  2313. void cmake::CleanupWrittenFiles()
  2314. {
  2315. this->WrittenFiles.clear();
  2316. }
  2317. void cmake::UpdateConversionPathTable()
  2318. {
  2319. // Update the path conversion table with any specified file:
  2320. const char* tablepath =
  2321. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  2322. if(tablepath)
  2323. {
  2324. std::ifstream table( tablepath );
  2325. if(!table)
  2326. {
  2327. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  2328. ". CMake can not open file.");
  2329. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  2330. }
  2331. else
  2332. {
  2333. std::string a, b;
  2334. while(!table.eof())
  2335. {
  2336. // two entries per line
  2337. table >> a; table >> b;
  2338. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  2339. }
  2340. }
  2341. }
  2342. }
  2343. //----------------------------------------------------------------------------
  2344. int cmake::CheckBuildSystem()
  2345. {
  2346. // We do not need to rerun CMake. Check dependency integrity. Use
  2347. // the make system's VERBOSE environment variable to enable verbose
  2348. // output.
  2349. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  2350. // This method will check the integrity of the build system if the
  2351. // option was given on the command line. It reads the given file to
  2352. // determine whether CMake should rerun.
  2353. // If no file is provided for the check, we have to rerun.
  2354. if(this->CheckBuildSystemArgument.size() == 0)
  2355. {
  2356. if(verbose)
  2357. {
  2358. cmOStringStream msg;
  2359. msg << "Re-run cmake no build system arguments\n";
  2360. cmSystemTools::Stdout(msg.str().c_str());
  2361. }
  2362. return 1;
  2363. }
  2364. // If the file provided does not exist, we have to rerun.
  2365. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  2366. {
  2367. if(verbose)
  2368. {
  2369. cmOStringStream msg;
  2370. msg << "Re-run cmake missing file: "
  2371. << this->CheckBuildSystemArgument.c_str() << "\n";
  2372. cmSystemTools::Stdout(msg.str().c_str());
  2373. }
  2374. return 1;
  2375. }
  2376. // Read the rerun check file and use it to decide whether to do the
  2377. // global generate.
  2378. cmake cm;
  2379. cmGlobalGenerator gg;
  2380. gg.SetCMakeInstance(&cm);
  2381. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  2382. lg->SetGlobalGenerator(&gg);
  2383. cmMakefile* mf = lg->GetMakefile();
  2384. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  2385. cmSystemTools::GetErrorOccuredFlag())
  2386. {
  2387. if(verbose)
  2388. {
  2389. cmOStringStream msg;
  2390. msg << "Re-run cmake error reading : "
  2391. << this->CheckBuildSystemArgument.c_str() << "\n";
  2392. cmSystemTools::Stdout(msg.str().c_str());
  2393. }
  2394. // There was an error reading the file. Just rerun.
  2395. return 1;
  2396. }
  2397. if(this->ClearBuildSystem)
  2398. {
  2399. // Get the generator used for this build system.
  2400. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  2401. if(!genName || genName[0] == '\0')
  2402. {
  2403. genName = "Unix Makefiles";
  2404. }
  2405. // Create the generator and use it to clear the dependencies.
  2406. std::auto_ptr<cmGlobalGenerator>
  2407. ggd(this->CreateGlobalGenerator(genName));
  2408. if(ggd.get())
  2409. {
  2410. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  2411. lgd->SetGlobalGenerator(ggd.get());
  2412. lgd->ClearDependencies(mf, verbose);
  2413. }
  2414. }
  2415. // Get the set of dependencies and outputs.
  2416. std::vector<std::string> depends;
  2417. std::vector<std::string> outputs;
  2418. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  2419. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  2420. if(dependsStr && outputsStr)
  2421. {
  2422. cmSystemTools::ExpandListArgument(dependsStr, depends);
  2423. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  2424. }
  2425. if(depends.empty() || outputs.empty())
  2426. {
  2427. // Not enough information was provided to do the test. Just rerun.
  2428. if(verbose)
  2429. {
  2430. cmOStringStream msg;
  2431. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  2432. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  2433. cmSystemTools::Stdout(msg.str().c_str());
  2434. }
  2435. return 1;
  2436. }
  2437. // Find find the newest dependency.
  2438. std::vector<std::string>::iterator dep = depends.begin();
  2439. std::string dep_newest = *dep++;
  2440. for(;dep != depends.end(); ++dep)
  2441. {
  2442. int result = 0;
  2443. if(this->FileComparison->FileTimeCompare(dep_newest.c_str(),
  2444. dep->c_str(), &result))
  2445. {
  2446. if(result < 0)
  2447. {
  2448. dep_newest = *dep;
  2449. }
  2450. }
  2451. else
  2452. {
  2453. if(verbose)
  2454. {
  2455. cmOStringStream msg;
  2456. msg << "Re-run cmake: build system dependency is missing\n";
  2457. cmSystemTools::Stdout(msg.str().c_str());
  2458. }
  2459. return 1;
  2460. }
  2461. }
  2462. // Find find the oldest output.
  2463. std::vector<std::string>::iterator out = outputs.begin();
  2464. std::string out_oldest = *out++;
  2465. for(;out != outputs.end(); ++out)
  2466. {
  2467. int result = 0;
  2468. if(this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  2469. out->c_str(), &result))
  2470. {
  2471. if(result > 0)
  2472. {
  2473. out_oldest = *out;
  2474. }
  2475. }
  2476. else
  2477. {
  2478. if(verbose)
  2479. {
  2480. cmOStringStream msg;
  2481. msg << "Re-run cmake: build system output is missing\n";
  2482. cmSystemTools::Stdout(msg.str().c_str());
  2483. }
  2484. return 1;
  2485. }
  2486. }
  2487. // If any output is older than any dependency then rerun.
  2488. {
  2489. int result = 0;
  2490. if(!this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  2491. dep_newest.c_str(),
  2492. &result) ||
  2493. result < 0)
  2494. {
  2495. if(verbose)
  2496. {
  2497. cmOStringStream msg;
  2498. msg << "Re-run cmake file: " << out_oldest.c_str()
  2499. << " older than: " << dep_newest.c_str() << "\n";
  2500. cmSystemTools::Stdout(msg.str().c_str());
  2501. }
  2502. return 1;
  2503. }
  2504. }
  2505. // No need to rerun.
  2506. return 0;
  2507. }
  2508. //----------------------------------------------------------------------------
  2509. void cmake::TruncateOutputLog(const char* fname)
  2510. {
  2511. std::string fullPath = this->GetHomeOutputDirectory();
  2512. fullPath += "/";
  2513. fullPath += fname;
  2514. struct stat st;
  2515. if ( ::stat(fullPath.c_str(), &st) )
  2516. {
  2517. return;
  2518. }
  2519. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  2520. {
  2521. cmSystemTools::RemoveFile(fullPath.c_str());
  2522. return;
  2523. }
  2524. off_t fsize = st.st_size;
  2525. const off_t maxFileSize = 50 * 1024;
  2526. if ( fsize < maxFileSize )
  2527. {
  2528. //TODO: truncate file
  2529. return;
  2530. }
  2531. }
  2532. inline std::string removeQuotes(const std::string& s)
  2533. {
  2534. if(s[0] == '\"' && s[s.size()-1] == '\"')
  2535. {
  2536. return s.substr(1, s.size()-2);
  2537. }
  2538. return s;
  2539. }
  2540. std::string cmake::FindCMakeProgram(const char* name) const
  2541. {
  2542. std::string path;
  2543. if ((name) && (*name))
  2544. {
  2545. const cmMakefile* mf
  2546. = this->GetGlobalGenerator()->GetLocalGenerators()[0]->GetMakefile();
  2547. #ifdef CMAKE_BUILD_WITH_CMAKE
  2548. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2549. path = removeQuotes(path);
  2550. path = cmSystemTools::GetFilenamePath(path.c_str());
  2551. path += "/";
  2552. path += name;
  2553. path += cmSystemTools::GetExecutableExtension();
  2554. if(!cmSystemTools::FileExists(path.c_str()))
  2555. {
  2556. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2557. path = cmSystemTools::GetFilenamePath(path.c_str());
  2558. path += "/Debug/";
  2559. path += name;
  2560. path += cmSystemTools::GetExecutableExtension();
  2561. }
  2562. if(!cmSystemTools::FileExists(path.c_str()))
  2563. {
  2564. path = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2565. path = cmSystemTools::GetFilenamePath(path.c_str());
  2566. path += "/Release/";
  2567. path += name;
  2568. path += cmSystemTools::GetExecutableExtension();
  2569. }
  2570. #else
  2571. // Only for bootstrap
  2572. path += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  2573. path += "/";
  2574. path += name;
  2575. path += cmSystemTools::GetExecutableExtension();
  2576. #endif
  2577. }
  2578. return path;
  2579. }
  2580. const char* cmake::GetCTestCommand()
  2581. {
  2582. if ( this->CTestCommand.empty() )
  2583. {
  2584. this->CTestCommand = this->FindCMakeProgram("ctest");
  2585. }
  2586. if ( this->CTestCommand.empty() )
  2587. {
  2588. cmSystemTools::Error("Cannot find the CTest executable");
  2589. this->CTestCommand = "CTEST-COMMAND-NOT-FOUND";
  2590. }
  2591. return this->CTestCommand.c_str();
  2592. }
  2593. const char* cmake::GetCPackCommand()
  2594. {
  2595. if ( this->CPackCommand.empty() )
  2596. {
  2597. this->CPackCommand = this->FindCMakeProgram("cpack");
  2598. }
  2599. if ( this->CPackCommand.empty() )
  2600. {
  2601. cmSystemTools::Error("Cannot find the CPack executable");
  2602. this->CPackCommand = "CPACK-COMMAND-NOT-FOUND";
  2603. }
  2604. return this->CPackCommand.c_str();
  2605. }
  2606. void cmake::GenerateGraphViz(const char* fileName) const
  2607. {
  2608. cmGeneratedFileStream str(fileName);
  2609. if ( !str )
  2610. {
  2611. return;
  2612. }
  2613. cmake cm;
  2614. cmGlobalGenerator ggi;
  2615. ggi.SetCMakeInstance(&cm);
  2616. std::auto_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator());
  2617. lg->SetGlobalGenerator(&ggi);
  2618. cmMakefile *mf = lg->GetMakefile();
  2619. std::string infile = this->GetHomeOutputDirectory();
  2620. infile += "/CMakeGraphVizOptions.cmake";
  2621. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2622. {
  2623. infile = this->GetHomeDirectory();
  2624. infile += "/CMakeGraphVizOptions.cmake";
  2625. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2626. {
  2627. infile = "";
  2628. }
  2629. }
  2630. if ( !infile.empty() )
  2631. {
  2632. if ( !mf->ReadListFile(0, infile.c_str()) )
  2633. {
  2634. cmSystemTools::Error("Problem opening GraphViz options file: ",
  2635. infile.c_str());
  2636. return;
  2637. }
  2638. std::cout << "Read GraphViz options file: " << infile.c_str()
  2639. << std::endl;
  2640. }
  2641. #define __set_if_not_set(var, value, cmakeDefinition) \
  2642. const char* var = mf->GetDefinition(cmakeDefinition); \
  2643. if ( !var ) \
  2644. { \
  2645. var = value; \
  2646. }
  2647. __set_if_not_set(graphType, "digraph", "GRAPHVIZ_GRAPH_TYPE");
  2648. __set_if_not_set(graphName, "GG", "GRAPHVIZ_GRAPH_NAME");
  2649. __set_if_not_set(graphHeader, "node [\n fontsize = \"12\"\n];",
  2650. "GRAPHVIZ_GRAPH_HEADER");
  2651. __set_if_not_set(graphNodePrefix, "node", "GRAPHVIZ_NODE_PREFIX");
  2652. const char* ignoreTargets = mf->GetDefinition("GRAPHVIZ_IGNORE_TARGETS");
  2653. std::set<cmStdString> ignoreTargetsSet;
  2654. if ( ignoreTargets )
  2655. {
  2656. std::vector<std::string> ignoreTargetsVector;
  2657. cmSystemTools::ExpandListArgument(ignoreTargets,ignoreTargetsVector);
  2658. std::vector<std::string>::iterator itvIt;
  2659. for ( itvIt = ignoreTargetsVector.begin();
  2660. itvIt != ignoreTargetsVector.end();
  2661. ++ itvIt )
  2662. {
  2663. ignoreTargetsSet.insert(itvIt->c_str());
  2664. }
  2665. }
  2666. str << graphType << " " << graphName << " {" << std::endl;
  2667. str << graphHeader << std::endl;
  2668. const cmGlobalGenerator* gg = this->GetGlobalGenerator();
  2669. const std::vector<cmLocalGenerator*>& localGenerators =
  2670. gg->GetLocalGenerators();
  2671. std::vector<cmLocalGenerator*>::const_iterator lit;
  2672. // for target deps
  2673. // 1 - cmake target
  2674. // 2 - external target
  2675. // 0 - no deps
  2676. std::map<cmStdString, int> targetDeps;
  2677. std::map<cmStdString, const cmTarget*> targetPtrs;
  2678. std::map<cmStdString, cmStdString> targetNamesNodes;
  2679. int cnt = 0;
  2680. // First pass get the list of all cmake targets
  2681. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2682. {
  2683. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2684. cmTargets::const_iterator tit;
  2685. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2686. {
  2687. const char* realTargetName = tit->first.c_str();
  2688. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2689. {
  2690. // Skip ignored targets
  2691. continue;
  2692. }
  2693. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  2694. cmOStringStream ostr;
  2695. ostr << graphNodePrefix << cnt++;
  2696. targetNamesNodes[realTargetName] = ostr.str();
  2697. targetPtrs[realTargetName] = &tit->second;
  2698. }
  2699. }
  2700. // Ok, now find all the stuff we link to that is not in cmake
  2701. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2702. {
  2703. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2704. cmTargets::const_iterator tit;
  2705. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2706. {
  2707. const cmTarget::LinkLibraryVectorType* ll
  2708. = &(tit->second.GetOriginalLinkLibraries());
  2709. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2710. const char* realTargetName = tit->first.c_str();
  2711. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2712. {
  2713. // Skip ignored targets
  2714. continue;
  2715. }
  2716. if ( ll->size() > 0 )
  2717. {
  2718. targetDeps[realTargetName] = 1;
  2719. }
  2720. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2721. {
  2722. const char* libName = llit->first.c_str();
  2723. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2724. = targetNamesNodes.find(libName);
  2725. if ( ignoreTargetsSet.find(libName) != ignoreTargetsSet.end() )
  2726. {
  2727. // Skip ignored targets
  2728. continue;
  2729. }
  2730. if ( tarIt == targetNamesNodes.end() )
  2731. {
  2732. cmOStringStream ostr;
  2733. ostr << graphNodePrefix << cnt++;
  2734. targetDeps[libName] = 2;
  2735. targetNamesNodes[libName] = ostr.str();
  2736. //str << " \"" << ostr.c_str() << "\" [ label=\"" << libName
  2737. //<< "\" shape=\"ellipse\"];" << std::endl;
  2738. }
  2739. else
  2740. {
  2741. std::map<cmStdString, int>::const_iterator depIt
  2742. = targetDeps.find(libName);
  2743. if ( depIt == targetDeps.end() )
  2744. {
  2745. targetDeps[libName] = 1;
  2746. }
  2747. }
  2748. }
  2749. }
  2750. }
  2751. // Write out nodes
  2752. std::map<cmStdString, int>::const_iterator depIt;
  2753. for ( depIt = targetDeps.begin(); depIt != targetDeps.end(); ++ depIt )
  2754. {
  2755. const char* newTargetName = depIt->first.c_str();
  2756. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2757. = targetNamesNodes.find(newTargetName);
  2758. if ( tarIt == targetNamesNodes.end() )
  2759. {
  2760. // We should not be here.
  2761. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2762. << " even though it was added in the previous pass" << std::endl;
  2763. abort();
  2764. }
  2765. str << " \"" << tarIt->second.c_str() << "\" [ label=\""
  2766. << newTargetName << "\" shape=\"";
  2767. if ( depIt->second == 1 )
  2768. {
  2769. std::map<cmStdString, const cmTarget*>::const_iterator tarTypeIt =
  2770. targetPtrs.find(newTargetName);
  2771. if ( tarTypeIt == targetPtrs.end() )
  2772. {
  2773. // We should not be here.
  2774. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2775. << " even though it was added in the previous pass" << std::endl;
  2776. abort();
  2777. }
  2778. const cmTarget* tg = tarTypeIt->second;
  2779. switch ( tg->GetType() )
  2780. {
  2781. case cmTarget::EXECUTABLE:
  2782. str << "house";
  2783. break;
  2784. case cmTarget::STATIC_LIBRARY:
  2785. str << "diamond";
  2786. break;
  2787. case cmTarget::SHARED_LIBRARY:
  2788. str << "polygon";
  2789. break;
  2790. case cmTarget::MODULE_LIBRARY:
  2791. str << "octagon";
  2792. break;
  2793. default:
  2794. str << "box";
  2795. }
  2796. }
  2797. else
  2798. {
  2799. str << "ellipse";
  2800. }
  2801. str << "\"];" << std::endl;
  2802. }
  2803. // Now generate the connectivity
  2804. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2805. {
  2806. const cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2807. cmTargets::const_iterator tit;
  2808. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2809. {
  2810. std::map<cmStdString, int>::iterator dependIt
  2811. = targetDeps.find(tit->first.c_str());
  2812. if ( dependIt == targetDeps.end() )
  2813. {
  2814. continue;
  2815. }
  2816. std::map<cmStdString, cmStdString>::iterator cmakeTarIt
  2817. = targetNamesNodes.find(tit->first.c_str());
  2818. const cmTarget::LinkLibraryVectorType* ll
  2819. = &(tit->second.GetOriginalLinkLibraries());
  2820. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2821. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2822. {
  2823. const char* libName = llit->first.c_str();
  2824. std::map<cmStdString, cmStdString>::const_iterator tarIt
  2825. = targetNamesNodes.find(libName);
  2826. if ( tarIt == targetNamesNodes.end() )
  2827. {
  2828. // We should not be here.
  2829. std::cout << __LINE__ << " Cannot find library: " << libName
  2830. << " even though it was added in the previous pass" << std::endl;
  2831. abort();
  2832. }
  2833. str << " \"" << cmakeTarIt->second.c_str() << "\" -> \""
  2834. << tarIt->second.c_str() << "\"" << std::endl;
  2835. }
  2836. }
  2837. }
  2838. // TODO: Use dotted or something for external libraries
  2839. //str << " \"node0\":f4 -> \"node12\"[color=\"#0000ff\" style=dotted]"
  2840. //<< std::endl;
  2841. //
  2842. str << "}" << std::endl;
  2843. }
  2844. //----------------------------------------------------------------------------
  2845. #ifdef CMAKE_BUILD_WITH_CMAKE
  2846. int cmake::ExecuteEchoColor(std::vector<std::string>& args)
  2847. {
  2848. // The arguments are
  2849. // argv[0] == <cmake-executable>
  2850. // argv[1] == cmake_echo_color
  2851. bool enabled = true;
  2852. int color = cmsysTerminal_Color_Normal;
  2853. bool newline = true;
  2854. for(unsigned int i=2; i < args.size(); ++i)
  2855. {
  2856. if(args[i].find("--switch=") == 0)
  2857. {
  2858. // Enable or disable color based on the switch value.
  2859. std::string value = args[i].substr(9);
  2860. if(!value.empty())
  2861. {
  2862. if(cmSystemTools::IsOn(value.c_str()))
  2863. {
  2864. enabled = true;
  2865. }
  2866. else
  2867. {
  2868. enabled = false;
  2869. }
  2870. }
  2871. }
  2872. else if(args[i] == "--normal")
  2873. {
  2874. color = cmsysTerminal_Color_Normal;
  2875. }
  2876. else if(args[i] == "--black")
  2877. {
  2878. color = cmsysTerminal_Color_ForegroundBlack;
  2879. }
  2880. else if(args[i] == "--red")
  2881. {
  2882. color = cmsysTerminal_Color_ForegroundRed;
  2883. }
  2884. else if(args[i] == "--green")
  2885. {
  2886. color = cmsysTerminal_Color_ForegroundGreen;
  2887. }
  2888. else if(args[i] == "--yellow")
  2889. {
  2890. color = cmsysTerminal_Color_ForegroundYellow;
  2891. }
  2892. else if(args[i] == "--blue")
  2893. {
  2894. color = cmsysTerminal_Color_ForegroundBlue;
  2895. }
  2896. else if(args[i] == "--magenta")
  2897. {
  2898. color = cmsysTerminal_Color_ForegroundMagenta;
  2899. }
  2900. else if(args[i] == "--cyan")
  2901. {
  2902. color = cmsysTerminal_Color_ForegroundCyan;
  2903. }
  2904. else if(args[i] == "--white")
  2905. {
  2906. color = cmsysTerminal_Color_ForegroundWhite;
  2907. }
  2908. else if(args[i] == "--bold")
  2909. {
  2910. color |= cmsysTerminal_Color_ForegroundBold;
  2911. }
  2912. else if(args[i] == "--no-newline")
  2913. {
  2914. newline = false;
  2915. }
  2916. else if(args[i] == "--newline")
  2917. {
  2918. newline = true;
  2919. }
  2920. else
  2921. {
  2922. // Color is enabled. Print with the current color.
  2923. cmSystemTools::MakefileColorEcho(color, args[i].c_str(),
  2924. newline, enabled);
  2925. }
  2926. }
  2927. return 0;
  2928. }
  2929. #else
  2930. int cmake::ExecuteEchoColor(std::vector<std::string>&)
  2931. {
  2932. return 1;
  2933. }
  2934. #endif
  2935. //----------------------------------------------------------------------------
  2936. int cmake::ExecuteLinkScript(std::vector<std::string>& args)
  2937. {
  2938. // The arguments are
  2939. // argv[0] == <cmake-executable>
  2940. // argv[1] == cmake_link_script
  2941. // argv[2] == <link-script-name>
  2942. // argv[3] == --verbose=?
  2943. bool verbose = false;
  2944. if(args.size() >= 4)
  2945. {
  2946. if(args[3].find("--verbose=") == 0)
  2947. {
  2948. if(!cmSystemTools::IsOff(args[3].substr(10).c_str()))
  2949. {
  2950. verbose = true;
  2951. }
  2952. }
  2953. }
  2954. // Allocate a process instance.
  2955. cmsysProcess* cp = cmsysProcess_New();
  2956. if(!cp)
  2957. {
  2958. std::cerr << "Error allocating process instance in link script."
  2959. << std::endl;
  2960. return 1;
  2961. }
  2962. // Children should share stdout and stderr with this process.
  2963. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDOUT, 1);
  2964. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1);
  2965. // Run the command lines verbatim.
  2966. cmsysProcess_SetOption(cp, cmsysProcess_Option_Verbatim, 1);
  2967. // Read command lines from the script.
  2968. std::ifstream fin(args[2].c_str());
  2969. if(!fin)
  2970. {
  2971. std::cerr << "Error opening link script \""
  2972. << args[2] << "\"" << std::endl;
  2973. return 1;
  2974. }
  2975. // Run one command at a time.
  2976. std::string command;
  2977. int result = 0;
  2978. while(result == 0 && cmSystemTools::GetLineFromStream(fin, command))
  2979. {
  2980. // Setup this command line.
  2981. const char* cmd[2] = {command.c_str(), 0};
  2982. cmsysProcess_SetCommand(cp, cmd);
  2983. // Report the command if verbose output is enabled.
  2984. if(verbose)
  2985. {
  2986. std::cout << command << std::endl;
  2987. }
  2988. // Run the command and wait for it to exit.
  2989. cmsysProcess_Execute(cp);
  2990. cmsysProcess_WaitForExit(cp, 0);
  2991. // Report failure if any.
  2992. switch(cmsysProcess_GetState(cp))
  2993. {
  2994. case cmsysProcess_State_Exited:
  2995. {
  2996. int value = cmsysProcess_GetExitValue(cp);
  2997. if(value != 0)
  2998. {
  2999. result = value;
  3000. }
  3001. }
  3002. break;
  3003. case cmsysProcess_State_Exception:
  3004. std::cerr << "Error running link command: "
  3005. << cmsysProcess_GetExceptionString(cp) << std::endl;
  3006. result = 1;
  3007. break;
  3008. case cmsysProcess_State_Error:
  3009. std::cerr << "Error running link command: "
  3010. << cmsysProcess_GetErrorString(cp) << std::endl;
  3011. result = 2;
  3012. break;
  3013. default:
  3014. break;
  3015. };
  3016. }
  3017. // Free the process instance.
  3018. cmsysProcess_Delete(cp);
  3019. // Return the final resulting return value.
  3020. return result;
  3021. }
  3022. void cmake::DefineProperties(cmake *cm)
  3023. {
  3024. cm->DefineProperty
  3025. ("REPORT_UNDEFINED_PROPERTIES", cmProperty::GLOBAL,
  3026. "If set, report any undefined properties to this file.",
  3027. "If this property is set to a filename then when CMake runs "
  3028. "it will report any properties or variables that were accessed "
  3029. "but not defined into the filename specified in this property."
  3030. );
  3031. cm->DefineProperty
  3032. ("TARGET_SUPPORTS_SHARED_LIBS", cmProperty::GLOBAL,
  3033. "Does the target platform support shared libraries.",
  3034. "TARGET_SUPPORTS_SHARED_LIBS is a boolean specifying whether the target "
  3035. "platform supports shared libraries. Basically all current general "
  3036. "general purpose OS do so, the exception are usually embedded systems "
  3037. "with no or special OSs.");
  3038. cm->DefineProperty
  3039. ("TARGET_ARCHIVES_MAY_BE_SHARED_LIBS", cmProperty::GLOBAL,
  3040. "Set if shared libraries may be named like archives.",
  3041. "On AIX shared libraries may be named \"lib<name>.a\". "
  3042. "This property is set to true on such platforms.");
  3043. cm->DefineProperty
  3044. ("FIND_LIBRARY_USE_LIB64_PATHS", cmProperty::GLOBAL,
  3045. "Whether FIND_LIBRARY should automatically search lib64 directories.",
  3046. "FIND_LIBRARY_USE_LIB64_PATHS is a boolean specifying whether the "
  3047. "FIND_LIBRARY command should automatically search the lib64 variant of "
  3048. "directories called lib in the search path when building 64-bit "
  3049. "binaries.");
  3050. cm->DefineProperty
  3051. ("ENABLED_FEATURES", cmProperty::GLOBAL,
  3052. "List of features which are enabled during the CMake run.",
  3053. "List of features which are enabled during the CMake run. Be default "
  3054. "it contains the names of all packages which were found. This is "
  3055. "determined using the <NAME>_FOUND variables. Packages which are "
  3056. "searched QUIET are not listed. A project can add its own features to "
  3057. "this list.This property is used by the macros in FeatureSummary.cmake.");
  3058. cm->DefineProperty
  3059. ("DISABLED_FEATURES", cmProperty::GLOBAL,
  3060. "List of features which are disabled during the CMake run.",
  3061. "List of features which are disabled during the CMake run. Be default "
  3062. "it contains the names of all packages which were not found. This is "
  3063. "determined using the <NAME>_FOUND variables. Packages which are "
  3064. "searched QUIET are not listed. A project can add its own features to "
  3065. "this list.This property is used by the macros in FeatureSummary.cmake.");
  3066. cm->DefineProperty
  3067. ("PACKAGES_FOUND", cmProperty::GLOBAL,
  3068. "List of packages which were found during the CMake run.",
  3069. "List of packages which were found during the CMake run. Whether a "
  3070. "package has been found is determined using the <NAME>_FOUND variables.");
  3071. cm->DefineProperty
  3072. ("PACKAGES_NOT_FOUND", cmProperty::GLOBAL,
  3073. "List of packages which were not found during the CMake run.",
  3074. "List of packages which were not found during the CMake run. Whether a "
  3075. "package has been found is determined using the <NAME>_FOUND variables.");
  3076. cm->DefineProperty
  3077. ("PACKAGES_NOT_FOUND", cmProperty::GLOBAL,
  3078. "List of packages which were not found during the CMake run.",
  3079. "List of packages which were not found during the CMake run. Whether a "
  3080. "package has been found is determined using the <NAME>_FOUND variables.");
  3081. cm->DefineProperty(
  3082. "__CMAKE_DELETE_CACHE_CHANGE_VARS_", cmProperty::GLOBAL,
  3083. "Internal property",
  3084. "Used to detect compiler changes, Do not set.");
  3085. cm->DefineProperty(
  3086. "GLOBAL_DEPENDS_DEBUG_MODE", cmProperty::GLOBAL,
  3087. "Enable global target dependency graph debug mode.",
  3088. "CMake automatically analyzes the global inter-target dependency graph "
  3089. "at the beginning of native build system generation. "
  3090. "This property causes it to display details of its analysis to stderr.");
  3091. cm->DefineProperty(
  3092. "ALLOW_DUPLICATE_CUSTOM_TARGETS", cmProperty::GLOBAL,
  3093. "Allow duplicate custom targets to be created.",
  3094. "Normally CMake requires that all targets built in a project have "
  3095. "globally unique logical names (see policy CMP_0002). "
  3096. "This is necessary to generate meaningful project file names in "
  3097. "Xcode and VS IDE generators. "
  3098. "It also allows the target names to be referenced unambiguously.\n"
  3099. "Makefile generators are capable of supporting duplicate custom target "
  3100. "names. "
  3101. "For projects that care only about Makefile generators and do "
  3102. "not wish to support Xcode or VS IDE generators, one may set this "
  3103. "property to true to allow duplicate custom targets. "
  3104. "The property allows multiple add_custom_target command calls in "
  3105. "different directories to specify the same target name. "
  3106. "However, setting this property will cause non-Makefile generators "
  3107. "to produce an error and refuse to generate the project."
  3108. );
  3109. cm->DefineProperty
  3110. ("IN_TRY_COMPILE", cmProperty::GLOBAL,
  3111. "Read-only property that is true during a try-compile configuration.",
  3112. "True when building a project inside a TRY_COMPILE or TRY_RUN command.");
  3113. // ================================================================
  3114. // define variables as well
  3115. // ================================================================
  3116. cmDocumentVariables::DefineVariables(cm);
  3117. }
  3118. void cmake::DefineProperty(const char *name, cmProperty::ScopeType scope,
  3119. const char *ShortDescription,
  3120. const char *FullDescription,
  3121. bool chained, const char *docSection)
  3122. {
  3123. this->PropertyDefinitions[scope].DefineProperty(name,scope,ShortDescription,
  3124. FullDescription,
  3125. docSection,
  3126. chained);
  3127. }
  3128. cmPropertyDefinition *cmake
  3129. ::GetPropertyDefinition(const char *name,
  3130. cmProperty::ScopeType scope)
  3131. {
  3132. if (this->IsPropertyDefined(name,scope))
  3133. {
  3134. return &(this->PropertyDefinitions[scope][name]);
  3135. }
  3136. return 0;
  3137. }
  3138. void cmake::RecordPropertyAccess(const char *name,
  3139. cmProperty::ScopeType scope)
  3140. {
  3141. this->AccessedProperties.insert
  3142. (std::pair<cmStdString,cmProperty::ScopeType>(name,scope));
  3143. }
  3144. void cmake::ReportUndefinedPropertyAccesses(const char *filename)
  3145. {
  3146. FILE *progFile = fopen(filename,"w");
  3147. if (!progFile || !this->GlobalGenerator)
  3148. {
  3149. return;
  3150. }
  3151. // what are the enabled languages?
  3152. std::vector<std::string> enLangs;
  3153. this->GlobalGenerator->GetEnabledLanguages(enLangs);
  3154. // Common configuration names.
  3155. // TODO: Compute current configuration(s).
  3156. std::vector<std::string> enConfigs;
  3157. enConfigs.push_back("");
  3158. enConfigs.push_back("DEBUG");
  3159. enConfigs.push_back("RELEASE");
  3160. enConfigs.push_back("MINSIZEREL");
  3161. enConfigs.push_back("RELWITHDEBINFO");
  3162. // take all the defined properties and add definitions for all the enabled
  3163. // languages
  3164. std::set<std::pair<cmStdString,cmProperty::ScopeType> > aliasedProperties;
  3165. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::iterator i;
  3166. i = this->PropertyDefinitions.begin();
  3167. for (;i != this->PropertyDefinitions.end(); ++i)
  3168. {
  3169. cmPropertyDefinitionMap::iterator j;
  3170. for (j = i->second.begin(); j != i->second.end(); ++j)
  3171. {
  3172. // TODO: What if both <LANG> and <CONFIG> appear?
  3173. if (j->first.find("<CONFIG>") != std::string::npos)
  3174. {
  3175. std::vector<std::string>::const_iterator k;
  3176. for (k = enConfigs.begin(); k != enConfigs.end(); ++k)
  3177. {
  3178. std::string tmp = j->first;
  3179. cmSystemTools::ReplaceString(tmp, "<CONFIG>", k->c_str());
  3180. // add alias
  3181. aliasedProperties.insert
  3182. (std::pair<cmStdString,cmProperty::ScopeType>(tmp,i->first));
  3183. }
  3184. }
  3185. if (j->first.find("<LANG>") != std::string::npos)
  3186. {
  3187. std::vector<std::string>::const_iterator k;
  3188. for (k = enLangs.begin(); k != enLangs.end(); ++k)
  3189. {
  3190. std::string tmp = j->first;
  3191. cmSystemTools::ReplaceString(tmp, "<LANG>", k->c_str());
  3192. // add alias
  3193. aliasedProperties.insert
  3194. (std::pair<cmStdString,cmProperty::ScopeType>(tmp,i->first));
  3195. }
  3196. }
  3197. }
  3198. }
  3199. std::set<std::pair<cmStdString,cmProperty::ScopeType> >::const_iterator ap;
  3200. ap = this->AccessedProperties.begin();
  3201. for (;ap != this->AccessedProperties.end(); ++ap)
  3202. {
  3203. if (!this->IsPropertyDefined(ap->first.c_str(),ap->second) &&
  3204. aliasedProperties.find(std::pair<cmStdString,cmProperty::ScopeType>
  3205. (ap->first,ap->second)) ==
  3206. aliasedProperties.end())
  3207. {
  3208. const char *scopeStr = "";
  3209. switch (ap->second)
  3210. {
  3211. case cmProperty::TARGET:
  3212. scopeStr = "TARGET";
  3213. break;
  3214. case cmProperty::SOURCE_FILE:
  3215. scopeStr = "SOURCE_FILE";
  3216. break;
  3217. case cmProperty::DIRECTORY:
  3218. scopeStr = "DIRECTORY";
  3219. break;
  3220. case cmProperty::TEST:
  3221. scopeStr = "TEST";
  3222. break;
  3223. case cmProperty::VARIABLE:
  3224. scopeStr = "VARIABLE";
  3225. break;
  3226. case cmProperty::CACHED_VARIABLE:
  3227. scopeStr = "CACHED_VARIABLE";
  3228. break;
  3229. default:
  3230. scopeStr = "unknown";
  3231. break;
  3232. }
  3233. fprintf(progFile,"%s with scope %s\n",ap->first.c_str(),scopeStr);
  3234. }
  3235. }
  3236. fclose(progFile);
  3237. }
  3238. bool cmake::IsPropertyDefined(const char *name, cmProperty::ScopeType scope)
  3239. {
  3240. return this->PropertyDefinitions[scope].IsPropertyDefined(name);
  3241. }
  3242. bool cmake::IsPropertyChained(const char *name, cmProperty::ScopeType scope)
  3243. {
  3244. return this->PropertyDefinitions[scope].IsPropertyChained(name);
  3245. }
  3246. void cmake::SetProperty(const char* prop, const char* value)
  3247. {
  3248. if (!prop)
  3249. {
  3250. return;
  3251. }
  3252. if (!value)
  3253. {
  3254. value = "NOTFOUND";
  3255. }
  3256. this->Properties.SetProperty(prop, value, cmProperty::GLOBAL);
  3257. }
  3258. void cmake::AppendProperty(const char* prop, const char* value)
  3259. {
  3260. if (!prop)
  3261. {
  3262. return;
  3263. }
  3264. this->Properties.AppendProperty(prop, value, cmProperty::GLOBAL);
  3265. }
  3266. const char *cmake::GetProperty(const char* prop)
  3267. {
  3268. return this->GetProperty(prop, cmProperty::GLOBAL);
  3269. }
  3270. const char *cmake::GetProperty(const char* prop, cmProperty::ScopeType scope)
  3271. {
  3272. bool chain = false;
  3273. // watch for special properties
  3274. std::string propname = prop;
  3275. std::string output = "";
  3276. if ( propname == "CACHE_VARIABLES" )
  3277. {
  3278. cmCacheManager::CacheIterator cit =
  3279. this->GetCacheManager()->GetCacheIterator();
  3280. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  3281. {
  3282. if ( output.size() )
  3283. {
  3284. output += ";";
  3285. }
  3286. output += cit.GetName();
  3287. }
  3288. this->SetProperty("CACHE_VARIABLES", output.c_str());
  3289. }
  3290. else if ( propname == "COMMANDS" )
  3291. {
  3292. cmake::RegisteredCommandsMap::iterator cmds
  3293. = this->GetCommands()->begin();
  3294. for (unsigned int cc=0 ; cmds != this->GetCommands()->end(); ++ cmds )
  3295. {
  3296. if ( cc > 0 )
  3297. {
  3298. output += ";";
  3299. }
  3300. output += cmds->first.c_str();
  3301. cc++;
  3302. }
  3303. this->SetProperty("COMMANDS",output.c_str());
  3304. }
  3305. else if ( propname == "IN_TRY_COMPILE" )
  3306. {
  3307. this->SetProperty("IN_TRY_COMPILE",
  3308. this->GetIsInTryCompile()? "1":"0");
  3309. }
  3310. return this->Properties.GetPropertyValue(prop, scope, chain);
  3311. }
  3312. bool cmake::GetPropertyAsBool(const char* prop)
  3313. {
  3314. return cmSystemTools::IsOn(this->GetProperty(prop));
  3315. }
  3316. int cmake::GetSystemInformation(std::vector<std::string>& args)
  3317. {
  3318. // so create the directory
  3319. std::string resultFile;
  3320. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  3321. std::string destPath = cwd + "/__cmake_systeminformation";
  3322. cmSystemTools::RemoveADirectory(destPath.c_str());
  3323. if (!cmSystemTools::MakeDirectory(destPath.c_str()))
  3324. {
  3325. std::cerr << "Error: --system-information must be run from a "
  3326. "writable directory!\n";
  3327. return 1;
  3328. }
  3329. // process the arguments
  3330. bool writeToStdout = true;
  3331. for(unsigned int i=1; i < args.size(); ++i)
  3332. {
  3333. std::string arg = args[i];
  3334. if(arg.find("-V",0) == 0)
  3335. {
  3336. this->Verbose = true;
  3337. }
  3338. else if(arg.find("-G",0) == 0)
  3339. {
  3340. std::string value = arg.substr(2);
  3341. if(value.size() == 0)
  3342. {
  3343. ++i;
  3344. if(i >= args.size())
  3345. {
  3346. cmSystemTools::Error("No generator specified for -G");
  3347. return -1;
  3348. }
  3349. value = args[i];
  3350. }
  3351. cmGlobalGenerator* gen =
  3352. this->CreateGlobalGenerator(value.c_str());
  3353. if(!gen)
  3354. {
  3355. cmSystemTools::Error("Could not create named generator ",
  3356. value.c_str());
  3357. }
  3358. else
  3359. {
  3360. this->SetGlobalGenerator(gen);
  3361. }
  3362. }
  3363. // no option assume it is the output file
  3364. else
  3365. {
  3366. if (!cmSystemTools::FileIsFullPath(arg.c_str()))
  3367. {
  3368. resultFile = cwd;
  3369. resultFile += "/";
  3370. }
  3371. resultFile += arg;
  3372. writeToStdout = false;
  3373. }
  3374. }
  3375. // we have to find the module directory, so we can copy the files
  3376. this->AddCMakePaths();
  3377. std::string modulesPath =
  3378. this->CacheManager->GetCacheValue("CMAKE_ROOT");
  3379. modulesPath += "/Modules";
  3380. std::string inFile = modulesPath;
  3381. inFile += "/SystemInformation.cmake";
  3382. std::string outFile = destPath;
  3383. outFile += "/CMakeLists.txt";
  3384. // Copy file
  3385. if(!cmSystemTools::cmCopyFile(inFile.c_str(), outFile.c_str()))
  3386. {
  3387. std::cerr << "Error copying file \"" << inFile.c_str()
  3388. << "\" to \"" << outFile.c_str() << "\".\n";
  3389. return 1;
  3390. }
  3391. // do we write to a file or to stdout?
  3392. if (resultFile.size() == 0)
  3393. {
  3394. resultFile = cwd;
  3395. resultFile += "/__cmake_systeminformation/results.txt";
  3396. }
  3397. // now run cmake on the CMakeLists file
  3398. cmSystemTools::ChangeDirectory(destPath.c_str());
  3399. std::vector<std::string> args2;
  3400. args2.push_back(args[0]);
  3401. args2.push_back(destPath);
  3402. std::string resultArg = "-DRESULT_FILE=";
  3403. resultArg += resultFile;
  3404. args2.push_back(resultArg);
  3405. int res = this->Run(args2, false);
  3406. if (res != 0)
  3407. {
  3408. std::cerr << "Error: --system-information failed on internal CMake!\n";
  3409. return res;
  3410. }
  3411. // change back to the original directory
  3412. cmSystemTools::ChangeDirectory(cwd.c_str());
  3413. // echo results to stdout if needed
  3414. if (writeToStdout)
  3415. {
  3416. FILE* fin = fopen(resultFile.c_str(), "r");
  3417. if(fin)
  3418. {
  3419. const int bufferSize = 4096;
  3420. char buffer[bufferSize];
  3421. size_t n;
  3422. while((n = fread(buffer, 1, bufferSize, fin)) > 0)
  3423. {
  3424. for(char* c = buffer; c < buffer+n; ++c)
  3425. {
  3426. putc(*c, stdout);
  3427. }
  3428. fflush(stdout);
  3429. }
  3430. fclose(fin);
  3431. }
  3432. }
  3433. // clean up the directory
  3434. cmSystemTools::RemoveADirectory(destPath.c_str());
  3435. return 0;
  3436. }
  3437. //----------------------------------------------------------------------------
  3438. static bool cmakeCheckStampFile(const char* stampName)
  3439. {
  3440. // If the stamp file still exists then it must really be out of
  3441. // date.
  3442. if(cmSystemTools::FileExists(stampName))
  3443. {
  3444. return false;
  3445. }
  3446. // The stamp file does not exist. Use the stamp dependencies to
  3447. // determine whether it is really out of date. This works in
  3448. // conjunction with cmLocalVisualStudio7Generator to avoid
  3449. // repeatedly re-running CMake when the user rebuilds the entire
  3450. // solution.
  3451. std::string stampDepends = stampName;
  3452. stampDepends += ".depend";
  3453. #if defined(_WIN32) || defined(__CYGWIN__)
  3454. std::ifstream fin(stampDepends.c_str(), std::ios::in | std::ios::binary);
  3455. #else
  3456. std::ifstream fin(stampDepends.c_str(), std::ios::in);
  3457. #endif
  3458. if(!fin)
  3459. {
  3460. // The stamp dependencies file cannot be read. Just assume the
  3461. // build system is really out of date.
  3462. return false;
  3463. }
  3464. // Compare the stamp dependencies against the dependency file itself.
  3465. cmFileTimeComparison ftc;
  3466. std::string dep;
  3467. while(cmSystemTools::GetLineFromStream(fin, dep))
  3468. {
  3469. int result;
  3470. if(dep.length() >= 1 && dep[0] != '#' &&
  3471. (!ftc.FileTimeCompare(stampDepends.c_str(), dep.c_str(), &result)
  3472. || result < 0))
  3473. {
  3474. // The stamp depends file is older than this dependency. The
  3475. // build system is really out of date.
  3476. return false;
  3477. }
  3478. }
  3479. // The build system is up to date. The stamp file has been removed
  3480. // by the VS IDE due to a "rebuild" request. Just restore it.
  3481. std::ofstream stamp(stampName);
  3482. stamp << "# CMake generation timestamp file this directory.\n";
  3483. if(stamp)
  3484. {
  3485. // Notify the user why CMake is not re-running. It is safe to
  3486. // just print to stdout here because this code is only reachable
  3487. // through an undocumented flag used by the VS generator.
  3488. std::cout << "CMake does not need to re-run because the "
  3489. << "generation timestamp is up-to-date.\n";
  3490. return true;
  3491. }
  3492. else
  3493. {
  3494. cmSystemTools::Error("Cannot restore timestamp ", stampName);
  3495. return false;
  3496. }
  3497. }
  3498. // For visual studio 2005 and newer manifest files need to be embeded into
  3499. // exe and dll's. This code does that in such a way that incremental linking
  3500. // still works.
  3501. int cmake::VisualStudioLink(std::vector<std::string>& args, int type)
  3502. {
  3503. if(args.size() < 2)
  3504. {
  3505. return -1;
  3506. }
  3507. bool verbose = false;
  3508. if(cmSystemTools::GetEnv("VERBOSE"))
  3509. {
  3510. verbose = true;
  3511. }
  3512. std::vector<std::string> expandedArgs;
  3513. for(std::vector<std::string>::iterator i = args.begin();
  3514. i != args.end(); ++i)
  3515. {
  3516. // check for nmake temporary files
  3517. if((*i)[0] == '@')
  3518. {
  3519. std::ifstream fin(i->substr(1).c_str());
  3520. std::string line;
  3521. while(cmSystemTools::GetLineFromStream(fin,
  3522. line))
  3523. {
  3524. cmSystemTools::ParseWindowsCommandLine(line.c_str(), expandedArgs);
  3525. }
  3526. }
  3527. else
  3528. {
  3529. expandedArgs.push_back(*i);
  3530. }
  3531. }
  3532. // figure out if this is an incremental link or not and run the correct
  3533. // link function.
  3534. for(std::vector<std::string>::iterator i = expandedArgs.begin();
  3535. i != expandedArgs.end(); ++i)
  3536. {
  3537. if(cmSystemTools::Strucmp(i->c_str(), "/INCREMENTAL:YES") == 0)
  3538. {
  3539. if(verbose)
  3540. {
  3541. std::cout << "Visual Studio Incremental Link\n";
  3542. }
  3543. return cmake::VisualStudioLinkIncremental(expandedArgs, type, verbose);
  3544. }
  3545. }
  3546. if(verbose)
  3547. {
  3548. std::cout << "Visual Studio Non-Incremental Link\n";
  3549. }
  3550. return cmake::VisualStudioLinkNonIncremental(expandedArgs, type, verbose);
  3551. }
  3552. int cmake::ParseVisualStudioLinkCommand(std::vector<std::string>& args,
  3553. std::vector<cmStdString>& command,
  3554. std::string& targetName)
  3555. {
  3556. std::vector<std::string>::iterator i = args.begin();
  3557. i++; // skip -E
  3558. i++; // skip vs_link_dll or vs_link_exe
  3559. command.push_back(*i);
  3560. i++; // move past link command
  3561. for(; i != args.end(); ++i)
  3562. {
  3563. command.push_back(*i);
  3564. if(i->find("/Fe") == 0)
  3565. {
  3566. targetName = i->substr(3);
  3567. }
  3568. if(i->find("/out:") == 0)
  3569. {
  3570. targetName = i->substr(5);
  3571. }
  3572. }
  3573. if(targetName.size() == 0 || command.size() == 0)
  3574. {
  3575. return -1;
  3576. }
  3577. return 0;
  3578. }
  3579. bool cmake::RunCommand(const char* comment,
  3580. std::vector<cmStdString>& command,
  3581. bool verbose,
  3582. int* retCodeOut)
  3583. {
  3584. if(verbose)
  3585. {
  3586. std::cout << comment << ":\n";
  3587. for(std::vector<cmStdString>::iterator i = command.begin();
  3588. i != command.end(); ++i)
  3589. {
  3590. std::cout << i->c_str() << " ";
  3591. }
  3592. std::cout << "\n";
  3593. }
  3594. std::string output;
  3595. int retCode =0;
  3596. // use rc command to create .res file
  3597. cmSystemTools::RunSingleCommand(command,
  3598. &output,
  3599. &retCode);
  3600. if(verbose)
  3601. {
  3602. std::cout << output << "\n";
  3603. }
  3604. // if retCodeOut is requested then always return true
  3605. // and set the retCodeOut to retCode
  3606. if(retCodeOut)
  3607. {
  3608. *retCodeOut = retCode;
  3609. return true;
  3610. }
  3611. if(retCode != 0)
  3612. {
  3613. std::cout << comment << " failed. with " << retCode << "\n";
  3614. }
  3615. return retCode == 0;
  3616. }
  3617. int cmake::VisualStudioLinkIncremental(std::vector<std::string>& args,
  3618. int type, bool verbose)
  3619. {
  3620. // This follows the steps listed here:
  3621. // http://blogs.msdn.com/zakramer/archive/2006/05/22/603558.aspx
  3622. // 1. Compiler compiles the application and generates the *.obj files.
  3623. // 2. An empty manifest file is generated if this is a clean build and if
  3624. // not the previous one is reused.
  3625. // 3. The resource compiler (rc.exe) compiles the *.manifest file to a
  3626. // *.res file.
  3627. // 4. Linker generates the binary (EXE or DLL) with the /incremental
  3628. // switch and embeds the dummy manifest file. The linker also generates
  3629. // the real manifest file based on the binaries that your binary depends
  3630. // on.
  3631. // 5. The manifest tool (mt.exe) is then used to generate the final
  3632. // manifest.
  3633. // If the final manifest is changed, then 6 and 7 are run, if not
  3634. // they are skipped, and it is done.
  3635. // 6. The resource compiler is invoked one more time.
  3636. // 7. Finally, the Linker does another incremental link, but since the
  3637. // only thing that has changed is the *.res file that contains the
  3638. // manifest it is a short link.
  3639. std::vector<cmStdString> linkCommand;
  3640. std::string targetName;
  3641. if(cmake::ParseVisualStudioLinkCommand(args, linkCommand, targetName) == -1)
  3642. {
  3643. return -1;
  3644. }
  3645. std::string manifestArg = "/MANIFESTFILE:";
  3646. std::vector<cmStdString> rcCommand;
  3647. rcCommand.push_back(cmSystemTools::FindProgram("rc.exe"));
  3648. std::vector<cmStdString> mtCommand;
  3649. mtCommand.push_back(cmSystemTools::FindProgram("mt.exe"));
  3650. std::string tempManifest;
  3651. tempManifest = targetName;
  3652. tempManifest += ".intermediate.manifest";
  3653. std::string resourceInputFile = targetName;
  3654. resourceInputFile += ".resource.txt";
  3655. if(verbose)
  3656. {
  3657. std::cout << "Create " << resourceInputFile.c_str() << "\n";
  3658. }
  3659. // Create input file for rc command
  3660. std::ofstream fout(resourceInputFile.c_str());
  3661. if(!fout)
  3662. {
  3663. return -1;
  3664. }
  3665. std::string manifestFile = targetName;
  3666. manifestFile += ".embed.manifest";
  3667. std::string fullPath= cmSystemTools::CollapseFullPath(manifestFile.c_str());
  3668. fout << type << " /* CREATEPROCESS_MANIFEST_RESOURCE_ID "
  3669. "*/ 24 /* RT_MANIFEST */ " << "\"" << fullPath.c_str() << "\"";
  3670. fout.close();
  3671. manifestArg += tempManifest;
  3672. // add the manifest arg to the linkCommand
  3673. linkCommand.push_back(manifestArg);
  3674. // if manifestFile is not yet created, create an
  3675. // empty one
  3676. if(!cmSystemTools::FileExists(manifestFile.c_str()))
  3677. {
  3678. if(verbose)
  3679. {
  3680. std::cout << "Create empty: " << manifestFile.c_str() << "\n";
  3681. }
  3682. std::ofstream foutTmp(manifestFile.c_str());
  3683. }
  3684. std::string resourceFile = manifestFile;
  3685. resourceFile += ".res";
  3686. // add the resource file to the end of the link command
  3687. linkCommand.push_back(resourceFile);
  3688. std::string outputOpt = "/fo";
  3689. outputOpt += resourceFile;
  3690. rcCommand.push_back(outputOpt);
  3691. rcCommand.push_back(resourceInputFile);
  3692. // Run rc command to create resource
  3693. if(!cmake::RunCommand("RC Pass 1", rcCommand, verbose))
  3694. {
  3695. return -1;
  3696. }
  3697. // Now run the link command to link and create manifest
  3698. if(!cmake::RunCommand("LINK Pass 1", linkCommand, verbose))
  3699. {
  3700. return -1;
  3701. }
  3702. // create mt command
  3703. std::string outArg("/out:");
  3704. outArg+= manifestFile;
  3705. mtCommand.push_back("/nologo");
  3706. mtCommand.push_back(outArg);
  3707. mtCommand.push_back("/notify_update");
  3708. mtCommand.push_back("/manifest");
  3709. mtCommand.push_back(tempManifest);
  3710. // now run mt.exe to create the final manifest file
  3711. int mtRet =0;
  3712. cmake::RunCommand("MT", mtCommand, verbose, &mtRet);
  3713. // if mt returns 0, then the manifest was not changed and
  3714. // we do not need to do another link step
  3715. if(mtRet == 0)
  3716. {
  3717. return 0;
  3718. }
  3719. // check for magic mt return value if mt returns the magic number
  3720. // 1090650113 then it means that it updated the manifest file and we need
  3721. // to do the final link. If mt has any value other than 0 or 1090650113
  3722. // then there was some problem with the command itself and there was an
  3723. // error so return the error code back out of cmake so make can report it.
  3724. if(mtRet != 1090650113)
  3725. {
  3726. return mtRet;
  3727. }
  3728. // update the resource file with the new manifest from the mt command.
  3729. if(!cmake::RunCommand("RC Pass 2", rcCommand, verbose))
  3730. {
  3731. return -1;
  3732. }
  3733. // Run the final incremental link that will put the new manifest resource
  3734. // into the file incrementally.
  3735. if(!cmake::RunCommand("FINAL LINK", linkCommand, verbose))
  3736. {
  3737. return -1;
  3738. }
  3739. return 0;
  3740. }
  3741. int cmake::VisualStudioLinkNonIncremental(std::vector<std::string>& args,
  3742. int type,
  3743. bool verbose)
  3744. {
  3745. std::vector<cmStdString> linkCommand;
  3746. std::string targetName;
  3747. if(cmake::ParseVisualStudioLinkCommand(args, linkCommand, targetName) == -1)
  3748. {
  3749. return -1;
  3750. }
  3751. // Run the link command as given
  3752. if(!cmake::RunCommand("LINK", linkCommand, verbose))
  3753. {
  3754. return -1;
  3755. }
  3756. std::vector<cmStdString> mtCommand;
  3757. mtCommand.push_back(cmSystemTools::FindProgram("mt.exe"));
  3758. mtCommand.push_back("/nologo");
  3759. mtCommand.push_back("/manifest");
  3760. std::string manifestFile = targetName;
  3761. manifestFile += ".manifest";
  3762. mtCommand.push_back(manifestFile);
  3763. std::string outresource = "/outputresource:";
  3764. outresource += targetName;
  3765. outresource += ";#";
  3766. if(type == 1)
  3767. {
  3768. outresource += "1";
  3769. }
  3770. else if(type == 2)
  3771. {
  3772. outresource += "2";
  3773. }
  3774. mtCommand.push_back(outresource);
  3775. // Now use the mt tool to embed the manifest into the exe or dll
  3776. if(!cmake::RunCommand("MT", mtCommand, verbose))
  3777. {
  3778. return -1;
  3779. }
  3780. return 0;
  3781. }