cmake.cxx 88 KB

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