cmake.cxx 103 KB

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