cmake.cxx 97 KB

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