cmake.cxx 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012
  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. << " remove file1 file2 ... - remove the file(s)\n"
  751. << " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar.\n"
  752. << " time command [args] ... - run command and return elapsed time\n"
  753. #if defined(_WIN32) && !defined(__CYGWIN__)
  754. << " write_regv key value - write registry value\n"
  755. << " delete_regv key - delete registry value\n"
  756. << " comspec - on windows 9x use this for RunCommand\n"
  757. #else
  758. << " create_symlink old new - create a symbolic link new -> old\n"
  759. #endif
  760. ;
  761. cmSystemTools::Error(errorStream.str().c_str());
  762. }
  763. int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
  764. {
  765. if (args.size() > 1)
  766. {
  767. // Copy file
  768. if (args[1] == "copy" && args.size() == 4)
  769. {
  770. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  771. {
  772. std::cerr << "Error copying file \"" << args[2].c_str()
  773. << "\" to \"" << args[3].c_str() << "\".\n";
  774. return 1;
  775. }
  776. return 0;
  777. }
  778. // Copy file if different.
  779. if (args[1] == "copy_if_different" && args.size() == 4)
  780. {
  781. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(),
  782. args[3].c_str()))
  783. {
  784. std::cerr << "Error copying file (if different) from \""
  785. << args[2].c_str() << "\" to \"" << args[3].c_str()
  786. << "\".\n";
  787. return 1;
  788. }
  789. return 0;
  790. }
  791. // Copy directory content
  792. if (args[1] == "copy_directory" && args.size() == 4)
  793. {
  794. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  795. {
  796. std::cerr << "Error copying directory from \""
  797. << args[2].c_str() << "\" to \"" << args[3].c_str()
  798. << "\".\n";
  799. return 1;
  800. }
  801. return 0;
  802. }
  803. // Compare files
  804. if (args[1] == "compare_files" && args.size() == 4)
  805. {
  806. if(cmSystemTools::FilesDiffer(args[2].c_str(), args[3].c_str()))
  807. {
  808. std::cerr << "Files \""
  809. << args[2].c_str() << "\" to \"" << args[3].c_str()
  810. << "\" are different.\n";
  811. return 1;
  812. }
  813. return 0;
  814. }
  815. // Echo string
  816. else if (args[1] == "echo" )
  817. {
  818. unsigned int cc;
  819. const char* space = "";
  820. for ( cc = 2; cc < args.size(); cc ++ )
  821. {
  822. std::cout << space << args[cc];
  823. space = " ";
  824. }
  825. std::cout << std::endl;
  826. return 0;
  827. }
  828. // Echo string no new line
  829. else if (args[1] == "echo_append" )
  830. {
  831. unsigned int cc;
  832. const char* space = "";
  833. for ( cc = 2; cc < args.size(); cc ++ )
  834. {
  835. std::cout << space << args[cc];
  836. space = " ";
  837. }
  838. return 0;
  839. }
  840. #if defined(CMAKE_BUILD_WITH_CMAKE)
  841. // Command to create a symbolic link. Fails on platforms not
  842. // supporting them.
  843. else if (args[1] == "environment" )
  844. {
  845. std::vector<std::string> env = cmSystemTools::GetEnvironmentVariables();
  846. std::vector<std::string>::iterator it;
  847. for ( it = env.begin(); it != env.end(); ++ it )
  848. {
  849. std::cout << it->c_str() << std::endl;
  850. }
  851. return 0;
  852. }
  853. #endif
  854. // Remove file
  855. else if (args[1] == "remove" && args.size() > 2)
  856. {
  857. bool force = false;
  858. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  859. {
  860. if(args[cc] == "\\-f" || args[cc] == "-f")
  861. {
  862. force = true;
  863. }
  864. else
  865. {
  866. // Complain if the file could not be removed, still exists,
  867. // and the -f option was not given.
  868. if(!cmSystemTools::RemoveFile(args[cc].c_str()) && !force &&
  869. cmSystemTools::FileExists(args[cc].c_str()))
  870. {
  871. return 1;
  872. }
  873. }
  874. }
  875. return 0;
  876. }
  877. // Clock command
  878. else if (args[1] == "time" && args.size() > 2)
  879. {
  880. std::string command = args[2];
  881. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  882. {
  883. command += " ";
  884. command += args[cc];
  885. }
  886. clock_t clock_start, clock_finish;
  887. time_t time_start, time_finish;
  888. time(&time_start);
  889. clock_start = clock();
  890. cmSystemTools::RunSingleCommand(command.c_str());
  891. clock_finish = clock();
  892. time(&time_finish);
  893. double clocks_per_sec = static_cast<double>(CLOCKS_PER_SEC);
  894. std::cout << "Elapsed time: "
  895. << static_cast<long>(time_finish - time_start) << " s. (time)"
  896. << ", "
  897. << static_cast<double>(clock_finish - clock_start) / clocks_per_sec
  898. << " s. (clock)"
  899. << "\n";
  900. return 0;
  901. }
  902. // Command to change directory and run a program.
  903. else if (args[1] == "chdir" && args.size() >= 4)
  904. {
  905. std::string directory = args[2];
  906. if(!cmSystemTools::FileExists(directory.c_str()))
  907. {
  908. cmSystemTools::Error("Directory does not exist for chdir command: ",
  909. args[2].c_str());
  910. return 0;
  911. }
  912. std::string command = "\"";
  913. command += args[3];
  914. command += "\"";
  915. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  916. {
  917. command += " \"";
  918. command += args[cc];
  919. command += "\"";
  920. }
  921. int retval = 0;
  922. int timeout = 0;
  923. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  924. directory.c_str(), true, timeout) )
  925. {
  926. return retval;
  927. }
  928. return 1;
  929. }
  930. // Command to start progress for a build
  931. else if (args[1] == "cmake_progress_start" && args.size() == 4)
  932. {
  933. // bascially remove the directory
  934. std::string dirName = args[2];
  935. dirName += "/Progress";
  936. cmSystemTools::RemoveADirectory(dirName.c_str());
  937. int count = atoi(args[3].c_str());
  938. if (count)
  939. {
  940. cmSystemTools::MakeDirectory(dirName.c_str());
  941. // write the count into the directory
  942. std::string fName = dirName;
  943. fName += "/count.txt";
  944. FILE *progFile = fopen(fName.c_str(),"w");
  945. if (progFile)
  946. {
  947. fprintf(progFile,"%i\n",count);
  948. fclose(progFile);
  949. }
  950. }
  951. return 0;
  952. }
  953. // Command to report progress for a build
  954. else if (args[1] == "cmake_progress_report" && args.size() >= 3)
  955. {
  956. std::string dirName = args[2];
  957. dirName += "/Progress";
  958. std::string fName;
  959. FILE *progFile;
  960. // read the count
  961. fName = dirName;
  962. fName += "/count.txt";
  963. progFile = fopen(fName.c_str(),"r");
  964. int count = 0;
  965. if (!progFile)
  966. {
  967. return 0;
  968. }
  969. else
  970. {
  971. fscanf(progFile,"%i",&count);
  972. fclose(progFile);
  973. }
  974. unsigned int i;
  975. for (i = 3; i < args.size(); ++i)
  976. {
  977. fName = dirName;
  978. fName += "/";
  979. fName += args[i];
  980. progFile = fopen(fName.c_str(),"w");
  981. if (progFile)
  982. {
  983. fprintf(progFile,"empty");
  984. fclose(progFile);
  985. }
  986. }
  987. int fileNum = static_cast<int>
  988. (cmsys::Directory::GetNumberOfFilesInDirectory(dirName.c_str()));
  989. if (count > 0)
  990. {
  991. // print the progress
  992. fprintf(stdout,"[%3i%%] ",((fileNum-3)*100)/count);
  993. }
  994. return 0;
  995. }
  996. // Command to create a symbolic link. Fails on platforms not
  997. // supporting them.
  998. else if (args[1] == "create_symlink" && args.size() == 4)
  999. {
  1000. const char* destinationFileName = args[3].c_str();
  1001. if ( cmSystemTools::FileExists(destinationFileName) )
  1002. {
  1003. if ( cmSystemTools::FileIsSymlink(destinationFileName) )
  1004. {
  1005. if ( !cmSystemTools::RemoveFile(destinationFileName) ||
  1006. cmSystemTools::FileExists(destinationFileName) )
  1007. {
  1008. return 0;
  1009. }
  1010. }
  1011. else
  1012. {
  1013. return 0;
  1014. }
  1015. }
  1016. return cmSystemTools::CreateSymlink(args[2].c_str(),
  1017. args[3].c_str())? 0:1;
  1018. }
  1019. // Internal CMake shared library support.
  1020. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  1021. {
  1022. int result = 0;
  1023. std::string realName = args[2];
  1024. std::string soName = args[3];
  1025. std::string name = args[4];
  1026. if(soName != realName)
  1027. {
  1028. std::string fname = cmSystemTools::GetFilenameName(realName);
  1029. if(cmSystemTools::FileExists(soName.c_str()))
  1030. {
  1031. cmSystemTools::RemoveFile(soName.c_str());
  1032. }
  1033. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  1034. {
  1035. result = 1;
  1036. }
  1037. }
  1038. if(name != soName)
  1039. {
  1040. std::string fname = cmSystemTools::GetFilenameName(soName);
  1041. if(cmSystemTools::FileExists(soName.c_str()))
  1042. {
  1043. cmSystemTools::RemoveFile(name.c_str());
  1044. }
  1045. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1046. {
  1047. result = 1;
  1048. }
  1049. }
  1050. return result;
  1051. }
  1052. // Internal CMake versioned executable support.
  1053. else if (args[1] == "cmake_symlink_executable" && args.size() == 4)
  1054. {
  1055. int result = 0;
  1056. std::string realName = args[2];
  1057. std::string name = args[3];
  1058. if(name != realName)
  1059. {
  1060. std::string fname = cmSystemTools::GetFilenameName(realName);
  1061. if(cmSystemTools::FileExists(realName.c_str()))
  1062. {
  1063. cmSystemTools::RemoveFile(name.c_str());
  1064. }
  1065. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  1066. {
  1067. result = 1;
  1068. }
  1069. }
  1070. return result;
  1071. }
  1072. // Internal CMake dependency scanning support.
  1073. else if (args[1] == "cmake_depends" && args.size() >= 6)
  1074. {
  1075. // Create a cmake object instance to process dependencies.
  1076. cmake cm;
  1077. std::string gen;
  1078. std::string homeDir;
  1079. std::string startDir;
  1080. std::string homeOutDir;
  1081. std::string startOutDir;
  1082. std::string depInfo;
  1083. if(args.size() >= 8)
  1084. {
  1085. // Full signature:
  1086. //
  1087. // -E cmake_depends <generator>
  1088. // <home-src-dir> <start-src-dir>
  1089. // <home-out-dir> <start-out-dir>
  1090. // <dep-info>
  1091. //
  1092. // All paths are provided.
  1093. gen = args[2];
  1094. homeDir = args[3];
  1095. startDir = args[4];
  1096. homeOutDir = args[5];
  1097. startOutDir = args[6];
  1098. depInfo = args[7];
  1099. }
  1100. else
  1101. {
  1102. // Support older signature for existing makefiles:
  1103. //
  1104. // -E cmake_depends <generator>
  1105. // <home-out-dir> <start-out-dir>
  1106. // <dep-info>
  1107. //
  1108. // Just pretend the source directories are the same as the
  1109. // binary directories so at least scanning will work.
  1110. gen = args[2];
  1111. homeDir = args[3];
  1112. startDir = args[4];
  1113. homeOutDir = args[3];
  1114. startOutDir = args[3];
  1115. depInfo = args[5];
  1116. }
  1117. // Create a local generator configured for the directory in
  1118. // which dependencies will be scanned.
  1119. homeDir = cmSystemTools::CollapseFullPath(homeDir.c_str());
  1120. startDir = cmSystemTools::CollapseFullPath(startDir.c_str());
  1121. homeOutDir = cmSystemTools::CollapseFullPath(homeOutDir.c_str());
  1122. startOutDir = cmSystemTools::CollapseFullPath(startOutDir.c_str());
  1123. cm.SetHomeDirectory(homeDir.c_str());
  1124. cm.SetStartDirectory(startDir.c_str());
  1125. cm.SetHomeOutputDirectory(homeOutDir.c_str());
  1126. cm.SetStartOutputDirectory(startOutDir.c_str());
  1127. if(cmGlobalGenerator* ggd = cm.CreateGlobalGenerator(gen.c_str()))
  1128. {
  1129. cm.SetGlobalGenerator(ggd);
  1130. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1131. lgd->SetGlobalGenerator(ggd);
  1132. lgd->GetMakefile()->SetStartDirectory(startDir.c_str());
  1133. lgd->GetMakefile()->SetStartOutputDirectory(startOutDir.c_str());
  1134. lgd->GetMakefile()->MakeStartDirectoriesCurrent();
  1135. lgd->SetupPathConversions();
  1136. // Actually scan dependencies.
  1137. return lgd->ScanDependencies(depInfo.c_str())? 0 : 2;
  1138. }
  1139. return 1;
  1140. }
  1141. // Internal CMake link script support.
  1142. else if (args[1] == "cmake_link_script" && args.size() >= 3)
  1143. {
  1144. return cmake::ExecuteLinkScript(args);
  1145. }
  1146. // Internal CMake unimplemented feature notification.
  1147. else if (args[1] == "cmake_unimplemented_variable")
  1148. {
  1149. std::cerr << "Feature not implemented for this platform.";
  1150. if(args.size() == 3)
  1151. {
  1152. std::cerr << " Variable " << args[2] << " is not set.";
  1153. }
  1154. std::cerr << std::endl;
  1155. return 1;
  1156. }
  1157. #ifdef CMAKE_BUILD_WITH_CMAKE
  1158. // Internal CMake color makefile support.
  1159. else if (args[1] == "cmake_echo_color")
  1160. {
  1161. return cmake::ExecuteEchoColor(args);
  1162. }
  1163. #endif
  1164. // Tar files
  1165. else if (args[1] == "tar" && args.size() > 3)
  1166. {
  1167. std::string flags = args[2];
  1168. std::string outFile = args[3];
  1169. std::vector<cmStdString> files;
  1170. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  1171. {
  1172. files.push_back(args[cc]);
  1173. }
  1174. bool gzip = false;
  1175. bool verbose = false;
  1176. if ( flags.find_first_of('z') != flags.npos )
  1177. {
  1178. gzip = true;
  1179. }
  1180. if ( flags.find_first_of('v') != flags.npos )
  1181. {
  1182. verbose = true;
  1183. }
  1184. if ( flags.find_first_of('t') != flags.npos )
  1185. {
  1186. if ( !cmSystemTools::ListTar(outFile.c_str(), files, gzip, verbose) )
  1187. {
  1188. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1189. return 1;
  1190. }
  1191. }
  1192. else if ( flags.find_first_of('c') != flags.npos )
  1193. {
  1194. if ( !cmSystemTools::CreateTar(
  1195. outFile.c_str(), files, gzip, verbose) )
  1196. {
  1197. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  1198. return 1;
  1199. }
  1200. }
  1201. else if ( flags.find_first_of('x') != flags.npos )
  1202. {
  1203. if ( !cmSystemTools::ExtractTar(
  1204. outFile.c_str(), files, gzip, verbose) )
  1205. {
  1206. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  1207. return 1;
  1208. }
  1209. }
  1210. return 0;
  1211. }
  1212. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1213. // Internal CMake Fortran module support.
  1214. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  1215. {
  1216. return cmDependsFortran::CopyModule(args)? 0 : 1;
  1217. }
  1218. #endif
  1219. #if defined(_WIN32) && !defined(__CYGWIN__)
  1220. // Write registry value
  1221. else if (args[1] == "write_regv" && args.size() > 3)
  1222. {
  1223. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  1224. args[3].c_str()) ? 0 : 1;
  1225. }
  1226. // Delete registry value
  1227. else if (args[1] == "delete_regv" && args.size() > 2)
  1228. {
  1229. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  1230. }
  1231. // Remove file
  1232. else if (args[1] == "comspec" && args.size() > 2)
  1233. {
  1234. unsigned int cc;
  1235. std::string command = args[2];
  1236. for ( cc = 3; cc < args.size(); cc ++ )
  1237. {
  1238. command += " " + args[cc];
  1239. }
  1240. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  1241. }
  1242. #endif
  1243. }
  1244. ::CMakeCommandUsage(args[0].c_str());
  1245. return 1;
  1246. }
  1247. //----------------------------------------------------------------------------
  1248. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  1249. {
  1250. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1251. i != this->Generators.end(); ++i)
  1252. {
  1253. names.push_back(i->first);
  1254. }
  1255. }
  1256. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  1257. {
  1258. RegisteredGeneratorsMap::const_iterator i = this->Generators.find(name);
  1259. if(i != this->Generators.end())
  1260. {
  1261. cmGlobalGenerator* generator = (i->second)();
  1262. generator->SetCMakeInstance(this);
  1263. return generator;
  1264. }
  1265. else
  1266. {
  1267. return 0;
  1268. }
  1269. }
  1270. void cmake::SetHomeDirectory(const char* dir)
  1271. {
  1272. this->cmHomeDirectory = dir;
  1273. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1274. }
  1275. void cmake::SetHomeOutputDirectory(const char* lib)
  1276. {
  1277. this->HomeOutputDirectory = lib;
  1278. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1279. }
  1280. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  1281. {
  1282. if(!gg)
  1283. {
  1284. cmSystemTools::Error("Error SetGlobalGenerator called with null");
  1285. return;
  1286. }
  1287. // delete the old generator
  1288. if (this->GlobalGenerator)
  1289. {
  1290. delete this->GlobalGenerator;
  1291. // restore the original environment variables CXX and CC
  1292. // Restor CC
  1293. std::string env = "CC=";
  1294. if(this->CCEnvironment.size())
  1295. {
  1296. env += this->CCEnvironment;
  1297. }
  1298. cmSystemTools::PutEnv(env.c_str());
  1299. env = "CXX=";
  1300. if(this->CXXEnvironment.size())
  1301. {
  1302. env += this->CXXEnvironment;
  1303. }
  1304. cmSystemTools::PutEnv(env.c_str());
  1305. }
  1306. // set the new
  1307. this->GlobalGenerator = gg;
  1308. // set the global flag for unix style paths on cmSystemTools as soon as
  1309. // the generator is set. This allows gmake to be used on windows.
  1310. cmSystemTools::SetForceUnixPaths
  1311. (this->GlobalGenerator->GetForceUnixPaths());
  1312. // Save the environment variables CXX and CC
  1313. const char* cxx = getenv("CXX");
  1314. const char* cc = getenv("CC");
  1315. if(cxx)
  1316. {
  1317. this->CXXEnvironment = cxx;
  1318. }
  1319. else
  1320. {
  1321. this->CXXEnvironment = "";
  1322. }
  1323. if(cc)
  1324. {
  1325. this->CCEnvironment = cc;
  1326. }
  1327. else
  1328. {
  1329. this->CCEnvironment = "";
  1330. }
  1331. // set the cmake instance just to be sure
  1332. gg->SetCMakeInstance(this);
  1333. }
  1334. int cmake::DoPreConfigureChecks()
  1335. {
  1336. // Make sure the Start directory contains a CMakeLists.txt file.
  1337. std::string srcList = this->GetHomeDirectory();
  1338. srcList += "/CMakeLists.txt";
  1339. if(!cmSystemTools::FileExists(srcList.c_str()))
  1340. {
  1341. cmOStringStream err;
  1342. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1343. {
  1344. err << "The source directory \"" << this->GetHomeDirectory()
  1345. << "\" does not appear to contain CMakeLists.txt.\n";
  1346. }
  1347. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1348. {
  1349. err << "The source directory \"" << this->GetHomeDirectory()
  1350. << "\" is a file, not a directory.\n";
  1351. }
  1352. else
  1353. {
  1354. err << "The source directory \"" << this->GetHomeDirectory()
  1355. << "\" does not exist.\n";
  1356. }
  1357. err << "Specify --help for usage, or press the help button on the CMake "
  1358. "GUI.";
  1359. cmSystemTools::Error(err.str().c_str());
  1360. return -2;
  1361. }
  1362. // do a sanity check on some values
  1363. if(this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1364. {
  1365. std::string cacheStart =
  1366. this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1367. cacheStart += "/CMakeLists.txt";
  1368. std::string currentStart = this->GetHomeDirectory();
  1369. currentStart += "/CMakeLists.txt";
  1370. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1371. {
  1372. std::string message = "The source \"";
  1373. message += currentStart;
  1374. message += "\" does not match the source \"";
  1375. message += cacheStart;
  1376. message += "\" used to generate cache. ";
  1377. message += "Re-run cmake with a different source directory.";
  1378. cmSystemTools::Error(message.c_str());
  1379. return -2;
  1380. }
  1381. }
  1382. else
  1383. {
  1384. return 0;
  1385. }
  1386. return 1;
  1387. }
  1388. int cmake::Configure()
  1389. {
  1390. // Construct right now our path conversion table before it's too late:
  1391. this->UpdateConversionPathTable();
  1392. this->CleanupCommandsAndMacros();
  1393. int res = 0;
  1394. if ( !this->ScriptMode )
  1395. {
  1396. res = this->DoPreConfigureChecks();
  1397. }
  1398. if ( res < 0 )
  1399. {
  1400. return -2;
  1401. }
  1402. if ( !res )
  1403. {
  1404. this->CacheManager->AddCacheEntry
  1405. ("CMAKE_HOME_DIRECTORY",
  1406. this->GetHomeDirectory(),
  1407. "Start directory with the top level CMakeLists.txt file for this "
  1408. "project",
  1409. cmCacheManager::INTERNAL);
  1410. }
  1411. // set the default BACKWARDS compatibility to the current version
  1412. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1413. {
  1414. char ver[256];
  1415. sprintf(ver,"%i.%i",cmVersion::GetMajorVersion(),
  1416. cmVersion::GetMinorVersion());
  1417. this->CacheManager->AddCacheEntry
  1418. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1419. "For backwards compatibility, what version of CMake commands and "
  1420. "syntax should this version of CMake allow.",
  1421. cmCacheManager::STRING);
  1422. }
  1423. // no generator specified on the command line
  1424. if(!this->GlobalGenerator)
  1425. {
  1426. const char* genName =
  1427. this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1428. if(genName)
  1429. {
  1430. this->GlobalGenerator = this->CreateGlobalGenerator(genName);
  1431. }
  1432. if(this->GlobalGenerator)
  1433. {
  1434. // set the global flag for unix style paths on cmSystemTools as
  1435. // soon as the generator is set. This allows gmake to be used
  1436. // on windows.
  1437. cmSystemTools::SetForceUnixPaths
  1438. (this->GlobalGenerator->GetForceUnixPaths());
  1439. }
  1440. else
  1441. {
  1442. #if defined(__BORLANDC__) && defined(_WIN32)
  1443. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1444. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1445. std::string installedCompiler;
  1446. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1447. "\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  1448. cmSystemTools::ExpandRegistryValues(mp);
  1449. if (!(mp == "/registry"))
  1450. {
  1451. installedCompiler = "Visual Studio 8 2005";
  1452. }
  1453. else
  1454. {
  1455. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1456. "\\VisualStudio\\7.1;InstallDir]";
  1457. cmSystemTools::ExpandRegistryValues(mp);
  1458. if (!(mp == "/registry"))
  1459. {
  1460. installedCompiler = "Visual Studio 7 .NET 2003";
  1461. }
  1462. else
  1463. {
  1464. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1465. "\\VisualStudio\\7.0;InstallDir]";
  1466. cmSystemTools::ExpandRegistryValues(mp);
  1467. if (!(mp == "/registry"))
  1468. {
  1469. installedCompiler = "Visual Studio 7";
  1470. }
  1471. else
  1472. {
  1473. installedCompiler = "Visual Studio 6";
  1474. }
  1475. }
  1476. }
  1477. cmGlobalGenerator* gen
  1478. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1479. if(!gen)
  1480. {
  1481. gen = new cmGlobalNMakeMakefileGenerator;
  1482. }
  1483. this->SetGlobalGenerator(gen);
  1484. #else
  1485. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1486. #endif
  1487. }
  1488. if(!this->GlobalGenerator)
  1489. {
  1490. cmSystemTools::Error("Could not create generator");
  1491. return -1;
  1492. }
  1493. }
  1494. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1495. if(genName)
  1496. {
  1497. if(strcmp(this->GlobalGenerator->GetName(), genName) != 0)
  1498. {
  1499. std::string message = "Error: generator : ";
  1500. message += this->GlobalGenerator->GetName();
  1501. message += "\nDoes not match the generator used previously: ";
  1502. message += genName;
  1503. message +=
  1504. "\nEither remove the CMakeCache.txt file or choose a different"
  1505. " binary directory.";
  1506. cmSystemTools::Error(message.c_str());
  1507. return -2;
  1508. }
  1509. }
  1510. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1511. {
  1512. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1513. this->GlobalGenerator->GetName(),
  1514. "Name of generator.",
  1515. cmCacheManager::INTERNAL);
  1516. }
  1517. // reset any system configuration information, except for when we are
  1518. // InTryCompile. With TryCompile the system info is taken from the parent's
  1519. // info to save time
  1520. if (!this->InTryCompile)
  1521. {
  1522. this->GlobalGenerator->ClearEnabledLanguages();
  1523. }
  1524. this->CleanupWrittenFiles();
  1525. // Truncate log files
  1526. if (!this->InTryCompile)
  1527. {
  1528. this->TruncateOutputLog("CMakeOutput.log");
  1529. this->TruncateOutputLog("CMakeError.log");
  1530. }
  1531. // actually do the configure
  1532. this->GlobalGenerator->Configure();
  1533. // Before saving the cache
  1534. // if the project did not define one of the entries below, add them now
  1535. // so users can edit the values in the cache:
  1536. // LIBRARY_OUTPUT_PATH
  1537. // EXECUTABLE_OUTPUT_PATH
  1538. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1539. {
  1540. this->CacheManager->AddCacheEntry
  1541. ("LIBRARY_OUTPUT_PATH", "",
  1542. "Single output directory for building all libraries.",
  1543. cmCacheManager::PATH);
  1544. }
  1545. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1546. {
  1547. this->CacheManager->AddCacheEntry
  1548. ("EXECUTABLE_OUTPUT_PATH", "",
  1549. "Single output directory for building all executables.",
  1550. cmCacheManager::PATH);
  1551. }
  1552. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1553. {
  1554. this->CacheManager->AddCacheEntry
  1555. ("CMAKE_USE_RELATIVE_PATHS", false,
  1556. "If true, cmake will use relative paths in makefiles and projects.");
  1557. cmCacheManager::CacheIterator it =
  1558. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1559. if ( !it.PropertyExists("ADVANCED") )
  1560. {
  1561. it.SetProperty("ADVANCED", "1");
  1562. }
  1563. }
  1564. if(cmSystemTools::GetFatalErrorOccured() &&
  1565. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1566. cmSystemTools::IsOff(this->CacheManager->
  1567. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1568. {
  1569. // We must have a bad generator selection. Wipe the cache entry so the
  1570. // user can select another.
  1571. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1572. }
  1573. // only save the cache if there were no fatal errors
  1574. if ( !this->ScriptMode )
  1575. {
  1576. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1577. }
  1578. if ( !this->GraphVizFile.empty() )
  1579. {
  1580. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1581. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1582. }
  1583. if(cmSystemTools::GetErrorOccuredFlag())
  1584. {
  1585. return -1;
  1586. }
  1587. return 0;
  1588. }
  1589. bool cmake::CacheVersionMatches()
  1590. {
  1591. const char* majv =
  1592. this->CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1593. const char* minv =
  1594. this->CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1595. const char* relv =
  1596. this->CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1597. bool cacheSameCMake = false;
  1598. if(majv &&
  1599. atoi(majv) == static_cast<int>(cmVersion::GetMajorVersion())
  1600. && minv &&
  1601. atoi(minv) == static_cast<int>(cmVersion::GetMinorVersion())
  1602. && relv && (strcmp(relv, cmVersion::GetReleaseVersion().c_str()) == 0))
  1603. {
  1604. cacheSameCMake = true;
  1605. }
  1606. return cacheSameCMake;
  1607. }
  1608. void cmake::PreLoadCMakeFiles()
  1609. {
  1610. std::string pre_load = this->GetHomeDirectory();
  1611. if ( pre_load.size() > 0 )
  1612. {
  1613. pre_load += "/PreLoad.cmake";
  1614. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1615. {
  1616. this->ReadListFile(pre_load.c_str());
  1617. }
  1618. }
  1619. pre_load = this->GetHomeOutputDirectory();
  1620. if ( pre_load.size() > 0 )
  1621. {
  1622. pre_load += "/PreLoad.cmake";
  1623. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1624. {
  1625. this->ReadListFile(pre_load.c_str());
  1626. }
  1627. }
  1628. }
  1629. // handle a command line invocation
  1630. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1631. {
  1632. // Process the arguments
  1633. this->SetArgs(args);
  1634. if(cmSystemTools::GetErrorOccuredFlag())
  1635. {
  1636. return -1;
  1637. }
  1638. // set the cmake command
  1639. this->CMakeCommand = args[0];
  1640. if ( !this->ScriptMode )
  1641. {
  1642. // load the cache
  1643. if(this->LoadCache() < 0)
  1644. {
  1645. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1646. return -1;
  1647. }
  1648. }
  1649. else
  1650. {
  1651. this->AddCMakePaths(this->CMakeCommand.c_str());
  1652. }
  1653. // Add any cache args
  1654. if ( !this->SetCacheArgs(args) )
  1655. {
  1656. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1657. return -1;
  1658. }
  1659. // In script mode we terminate after running the script.
  1660. if(this->ScriptMode)
  1661. {
  1662. if(cmSystemTools::GetErrorOccuredFlag())
  1663. {
  1664. return -1;
  1665. }
  1666. else
  1667. {
  1668. return 0;
  1669. }
  1670. }
  1671. this->PreLoadCMakeFiles();
  1672. std::string systemFile = this->GetHomeOutputDirectory();
  1673. systemFile += "/CMakeSystem.cmake";
  1674. if ( noconfigure )
  1675. {
  1676. return 0;
  1677. }
  1678. // now run the global generate
  1679. // Check the state of the build system to see if we need to regenerate.
  1680. if(!this->CheckBuildSystem())
  1681. {
  1682. return 0;
  1683. }
  1684. // If we are doing global generate, we better set start and start
  1685. // output directory to the root of the project.
  1686. std::string oldstartdir = this->GetStartDirectory();
  1687. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1688. this->SetStartDirectory(this->GetHomeDirectory());
  1689. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1690. int ret = this->Configure();
  1691. if (ret || this->ScriptMode)
  1692. {
  1693. return ret;
  1694. }
  1695. ret = this->Generate();
  1696. std::string message = "Build files have been written to: ";
  1697. message += this->GetHomeOutputDirectory();
  1698. this->UpdateProgress(message.c_str(), -1);
  1699. if(ret)
  1700. {
  1701. return ret;
  1702. }
  1703. this->SetStartDirectory(oldstartdir.c_str());
  1704. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1705. return ret;
  1706. }
  1707. int cmake::Generate()
  1708. {
  1709. if(!this->GlobalGenerator)
  1710. {
  1711. return -1;
  1712. }
  1713. this->GlobalGenerator->Generate();
  1714. if(cmSystemTools::GetErrorOccuredFlag())
  1715. {
  1716. return -1;
  1717. }
  1718. return 0;
  1719. }
  1720. void cmake::AddCacheEntry(const char* key, const char* value,
  1721. const char* helpString,
  1722. int type)
  1723. {
  1724. this->CacheManager->AddCacheEntry(key, value,
  1725. helpString,
  1726. cmCacheManager::CacheEntryType(type));
  1727. }
  1728. const char* cmake::GetCacheDefinition(const char* name) const
  1729. {
  1730. return this->CacheManager->GetCacheValue(name);
  1731. }
  1732. int cmake::DumpDocumentationToFile(std::ostream& f)
  1733. {
  1734. #ifdef CMAKE_BUILD_WITH_CMAKE
  1735. // Loop over all registered commands and print out documentation
  1736. const char *name;
  1737. const char *terse;
  1738. const char *full;
  1739. char tmp[1024];
  1740. sprintf(tmp,"Version %d.%d (%s)", cmVersion::GetMajorVersion(),
  1741. cmVersion::GetMinorVersion(),
  1742. cmVersion::GetReleaseVersion().c_str());
  1743. f << "<html>\n";
  1744. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1745. f << "<ul>\n";
  1746. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  1747. j != this->Commands.end(); ++j)
  1748. {
  1749. name = (*j).second->GetName();
  1750. terse = (*j).second->GetTerseDocumentation();
  1751. full = (*j).second->GetFullDocumentation();
  1752. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1753. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1754. }
  1755. f << "</ul></html>\n";
  1756. #else
  1757. (void)f;
  1758. #endif
  1759. return 1;
  1760. }
  1761. void cmake::AddDefaultCommands()
  1762. {
  1763. std::list<cmCommand*> commands;
  1764. GetBootstrapCommands(commands);
  1765. GetPredefinedCommands(commands);
  1766. for(std::list<cmCommand*>::iterator i = commands.begin();
  1767. i != commands.end(); ++i)
  1768. {
  1769. this->AddCommand(*i);
  1770. }
  1771. }
  1772. void cmake::AddDefaultGenerators()
  1773. {
  1774. #if defined(_WIN32) && !defined(__CYGWIN__)
  1775. # if !defined(CMAKE_BOOT_MINGW)
  1776. this->Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1777. &cmGlobalVisualStudio6Generator::New;
  1778. this->Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1779. &cmGlobalVisualStudio7Generator::New;
  1780. this->Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1781. &cmGlobalVisualStudio71Generator::New;
  1782. this->Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  1783. &cmGlobalVisualStudio8Generator::New;
  1784. this->Generators[cmGlobalVisualStudio8Win64Generator::GetActualName()] =
  1785. &cmGlobalVisualStudio8Win64Generator::New;
  1786. this->Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1787. &cmGlobalBorlandMakefileGenerator::New;
  1788. this->Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1789. &cmGlobalNMakeMakefileGenerator::New;
  1790. this->Generators[cmGlobalWatcomWMakeGenerator::GetActualName()] =
  1791. &cmGlobalWatcomWMakeGenerator::New;
  1792. # endif
  1793. this->Generators[cmGlobalMSYSMakefileGenerator::GetActualName()] =
  1794. &cmGlobalMSYSMakefileGenerator::New;
  1795. this->Generators[cmGlobalMinGWMakefileGenerator::GetActualName()] =
  1796. &cmGlobalMinGWMakefileGenerator::New;
  1797. #endif
  1798. this->Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
  1799. &cmGlobalUnixMakefileGenerator3::New;
  1800. #ifdef CMAKE_USE_XCODE
  1801. this->Generators[cmGlobalXCodeGenerator::GetActualName()] =
  1802. &cmGlobalXCodeGenerator::New;
  1803. #endif
  1804. #ifdef CMAKE_USE_KDEVELOP
  1805. this->Generators[cmGlobalKdevelopGenerator::GetActualName()] =
  1806. &cmGlobalKdevelopGenerator::New;
  1807. #endif
  1808. }
  1809. int cmake::LoadCache()
  1810. {
  1811. // could we not read the cache
  1812. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  1813. {
  1814. // if it does exist, but isn;t readable then warn the user
  1815. std::string cacheFile = this->GetHomeOutputDirectory();
  1816. cacheFile += "/CMakeCache.txt";
  1817. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1818. {
  1819. cmSystemTools::Error(
  1820. "There is a CMakeCache.txt file for the current binary tree but "
  1821. "cmake does not have permission to read it. Please check the "
  1822. "permissions of the directory you are trying to run CMake on.");
  1823. return -1;
  1824. }
  1825. }
  1826. if (this->CMakeCommand.size() < 2)
  1827. {
  1828. cmSystemTools::Error(
  1829. "cmake command was not specified prior to loading the cache in "
  1830. "cmake.cxx");
  1831. return -1;
  1832. }
  1833. // setup CMAKE_ROOT and CMAKE_COMMAND
  1834. if(!this->AddCMakePaths(this->CMakeCommand.c_str()))
  1835. {
  1836. return -3;
  1837. }
  1838. // set the default BACKWARDS compatibility to the current version
  1839. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1840. {
  1841. char ver[256];
  1842. sprintf(ver,"%i.%i",cmVersion::GetMajorVersion(),
  1843. cmVersion::GetMinorVersion());
  1844. this->CacheManager->AddCacheEntry
  1845. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1846. "For backwards compatibility, what version of CMake commands and "
  1847. "syntax should this version of CMake allow.",
  1848. cmCacheManager::STRING);
  1849. }
  1850. return 0;
  1851. }
  1852. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  1853. {
  1854. this->ProgressCallback = f;
  1855. this->ProgressCallbackClientData = cd;
  1856. }
  1857. void cmake::UpdateProgress(const char *msg, float prog)
  1858. {
  1859. if(this->ProgressCallback && !this->InTryCompile)
  1860. {
  1861. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  1862. return;
  1863. }
  1864. }
  1865. void cmake::GetCommandDocumentation(
  1866. std::vector<cmDocumentationEntry>& v) const
  1867. {
  1868. for(RegisteredCommandsMap::const_iterator j = this->Commands.begin();
  1869. j != this->Commands.end(); ++j)
  1870. {
  1871. cmDocumentationEntry e =
  1872. {
  1873. (*j).second->GetName(),
  1874. (*j).second->GetTerseDocumentation(),
  1875. (*j).second->GetFullDocumentation()
  1876. };
  1877. v.push_back(e);
  1878. }
  1879. cmDocumentationEntry empty = {0,0,0};
  1880. v.push_back(empty);
  1881. }
  1882. void cmake::GetPropertiesDocumentation(std::vector<cmDocumentationEntry>& v)
  1883. {
  1884. // get the properties for cmake
  1885. // get them for any generators
  1886. // get them for Directories
  1887. this->DirectoryProperties.GetPropertiesDocumentation(v);
  1888. // get them for targets
  1889. this->TargetProperties.GetPropertiesDocumentation(v);
  1890. // get them for source files
  1891. this->SourceFileProperties.GetPropertiesDocumentation(v);
  1892. // get them for tests
  1893. this->TestProperties.GetPropertiesDocumentation(v);
  1894. cmDocumentationEntry empty = {0,0,0};
  1895. v.push_back(empty);
  1896. }
  1897. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1898. {
  1899. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1900. i != this->Generators.end(); ++i)
  1901. {
  1902. cmDocumentationEntry e;
  1903. cmGlobalGenerator* generator = (i->second)();
  1904. generator->GetDocumentation(e);
  1905. delete generator;
  1906. v.push_back(e);
  1907. }
  1908. cmDocumentationEntry empty = {0,0,0};
  1909. v.push_back(empty);
  1910. }
  1911. void cmake::AddWrittenFile(const char* file)
  1912. {
  1913. this->WrittenFiles.insert(file);
  1914. }
  1915. bool cmake::HasWrittenFile(const char* file)
  1916. {
  1917. return this->WrittenFiles.find(file) != this->WrittenFiles.end();
  1918. }
  1919. void cmake::CleanupWrittenFiles()
  1920. {
  1921. this->WrittenFiles.clear();
  1922. }
  1923. void cmake::UpdateConversionPathTable()
  1924. {
  1925. // Update the path conversion table with any specified file:
  1926. const char* tablepath =
  1927. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1928. if(tablepath)
  1929. {
  1930. std::ifstream table( tablepath );
  1931. if(!table)
  1932. {
  1933. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  1934. ". CMake can not open file.");
  1935. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1936. }
  1937. else
  1938. {
  1939. std::string a, b;
  1940. while(!table.eof())
  1941. {
  1942. // two entries per line
  1943. table >> a; table >> b;
  1944. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1945. }
  1946. }
  1947. }
  1948. }
  1949. //----------------------------------------------------------------------------
  1950. int cmake::CheckBuildSystem()
  1951. {
  1952. // We do not need to rerun CMake. Check dependency integrity. Use
  1953. // the make system's VERBOSE environment variable to enable verbose
  1954. // output.
  1955. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  1956. // This method will check the integrity of the build system if the
  1957. // option was given on the command line. It reads the given file to
  1958. // determine whether CMake should rerun.
  1959. // If no file is provided for the check, we have to rerun.
  1960. if(this->CheckBuildSystemArgument.size() == 0)
  1961. {
  1962. if(verbose)
  1963. {
  1964. cmOStringStream msg;
  1965. msg << "Re-run cmake no build system arguments\n";
  1966. cmSystemTools::Stdout(msg.str().c_str());
  1967. }
  1968. return 1;
  1969. }
  1970. // If the file provided does not exist, we have to rerun.
  1971. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  1972. {
  1973. if(verbose)
  1974. {
  1975. cmOStringStream msg;
  1976. msg << "Re-run cmake missing file: "
  1977. << this->CheckBuildSystemArgument.c_str() << "\n";
  1978. cmSystemTools::Stdout(msg.str().c_str());
  1979. }
  1980. return 1;
  1981. }
  1982. // Read the rerun check file and use it to decide whether to do the
  1983. // global generate.
  1984. cmake cm;
  1985. cmGlobalGenerator gg;
  1986. gg.SetCMakeInstance(&cm);
  1987. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1988. lg->SetGlobalGenerator(&gg);
  1989. cmMakefile* mf = lg->GetMakefile();
  1990. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  1991. cmSystemTools::GetErrorOccuredFlag())
  1992. {
  1993. if(verbose)
  1994. {
  1995. cmOStringStream msg;
  1996. msg << "Re-run cmake error reading : "
  1997. << this->CheckBuildSystemArgument.c_str() << "\n";
  1998. cmSystemTools::Stdout(msg.str().c_str());
  1999. }
  2000. // There was an error reading the file. Just rerun.
  2001. return 1;
  2002. }
  2003. // Now that we know the generator used to build the project, use it
  2004. // to check the dependency integrity.
  2005. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  2006. if (!genName || genName[0] == '\0')
  2007. {
  2008. genName = "Unix Makefiles";
  2009. }
  2010. cmGlobalGenerator *ggd = this->CreateGlobalGenerator(genName);
  2011. if (ggd)
  2012. {
  2013. // Check the dependencies in case source files were removed.
  2014. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  2015. lgd->SetGlobalGenerator(ggd);
  2016. lgd->CheckDependencies(mf, verbose, this->ClearBuildSystem);
  2017. // Check for multiple output pairs.
  2018. ggd->CheckMultipleOutputs(mf, verbose);
  2019. }
  2020. // Get the set of dependencies and outputs.
  2021. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  2022. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  2023. if(!dependsStr || !outputsStr)
  2024. {
  2025. // Not enough information was provided to do the test. Just rerun.
  2026. if(verbose)
  2027. {
  2028. cmOStringStream msg;
  2029. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  2030. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  2031. cmSystemTools::Stdout(msg.str().c_str());
  2032. }
  2033. return 1;
  2034. }
  2035. std::vector<std::string> depends;
  2036. std::vector<std::string> outputs;
  2037. cmSystemTools::ExpandListArgument(dependsStr, depends);
  2038. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  2039. // If any output is older than any dependency then rerun.
  2040. for(std::vector<std::string>::iterator dep = depends.begin();
  2041. dep != depends.end(); ++dep)
  2042. {
  2043. for(std::vector<std::string>::iterator out = outputs.begin();
  2044. out != outputs.end(); ++out)
  2045. {
  2046. int result = 0;
  2047. if(!this->FileComparison->FileTimeCompare(out->c_str(),
  2048. dep->c_str(), &result) ||
  2049. result < 0)
  2050. {
  2051. if(verbose)
  2052. {
  2053. cmOStringStream msg;
  2054. msg << "Re-run cmake file: " << out->c_str()
  2055. << " older than: " << dep->c_str() << "\n";
  2056. cmSystemTools::Stdout(msg.str().c_str());
  2057. }
  2058. return 1;
  2059. }
  2060. }
  2061. }
  2062. // No need to rerun.
  2063. return 0;
  2064. }
  2065. //----------------------------------------------------------------------------
  2066. void cmake::TruncateOutputLog(const char* fname)
  2067. {
  2068. std::string fullPath = this->GetHomeOutputDirectory();
  2069. fullPath += "/";
  2070. fullPath += fname;
  2071. struct stat st;
  2072. if ( ::stat(fullPath.c_str(), &st) )
  2073. {
  2074. return;
  2075. }
  2076. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  2077. {
  2078. cmSystemTools::RemoveFile(fullPath.c_str());
  2079. return;
  2080. }
  2081. size_t fsize = st.st_size;
  2082. const size_t maxFileSize = 50 * 1024;
  2083. if ( fsize < maxFileSize )
  2084. {
  2085. //TODO: truncate file
  2086. return;
  2087. }
  2088. }
  2089. inline std::string removeQuotes(const std::string& s)
  2090. {
  2091. if(s[0] == '\"' && s[s.size()-1] == '\"')
  2092. {
  2093. return s.substr(1, s.size()-2);
  2094. }
  2095. return s;
  2096. }
  2097. const char* cmake::GetCTestCommand()
  2098. {
  2099. if ( !this->CTestCommand.empty() )
  2100. {
  2101. return this->CTestCommand.c_str();
  2102. }
  2103. cmMakefile* mf
  2104. = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  2105. #ifdef CMAKE_BUILD_WITH_CMAKE
  2106. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2107. this->CTestCommand = removeQuotes(this->CTestCommand);
  2108. this->CTestCommand =
  2109. cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  2110. this->CTestCommand += "/";
  2111. this->CTestCommand += "ctest";
  2112. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  2113. if(!cmSystemTools::FileExists(this->CTestCommand.c_str()))
  2114. {
  2115. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2116. this->CTestCommand =
  2117. cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  2118. this->CTestCommand += "/Debug/";
  2119. this->CTestCommand += "ctest";
  2120. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  2121. }
  2122. if(!cmSystemTools::FileExists(this->CTestCommand.c_str()))
  2123. {
  2124. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2125. this->CTestCommand =
  2126. cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  2127. this->CTestCommand += "/Release/";
  2128. this->CTestCommand += "ctest";
  2129. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  2130. }
  2131. #else
  2132. // Only for bootstrap
  2133. this->CTestCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  2134. this->CTestCommand += "/ctest";
  2135. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  2136. #endif
  2137. if ( this->CTestCommand.empty() )
  2138. {
  2139. cmSystemTools::Error("Cannot find the CTest executable");
  2140. this->CTestCommand = "CTEST-COMMAND-NOT-FOUND";
  2141. }
  2142. return this->CTestCommand.c_str();
  2143. }
  2144. const char* cmake::GetCPackCommand()
  2145. {
  2146. if ( !this->CPackCommand.empty() )
  2147. {
  2148. return this->CPackCommand.c_str();
  2149. }
  2150. cmMakefile* mf
  2151. = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  2152. #ifdef CMAKE_BUILD_WITH_CMAKE
  2153. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2154. this->CPackCommand = removeQuotes(this->CPackCommand);
  2155. this->CPackCommand =
  2156. cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  2157. this->CPackCommand += "/";
  2158. this->CPackCommand += "cpack";
  2159. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  2160. if(!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  2161. {
  2162. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2163. this->CPackCommand =
  2164. cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  2165. this->CPackCommand += "/Debug/";
  2166. this->CPackCommand += "cpack";
  2167. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  2168. }
  2169. if(!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  2170. {
  2171. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  2172. this->CPackCommand =
  2173. cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  2174. this->CPackCommand += "/Release/";
  2175. this->CPackCommand += "cpack";
  2176. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  2177. }
  2178. if (!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  2179. {
  2180. cmSystemTools::Error("Cannot find the CPack executable");
  2181. this->CPackCommand = "CPACK-COMMAND-NOT-FOUND";
  2182. }
  2183. #else
  2184. // Only for bootstrap
  2185. this->CPackCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  2186. this->CPackCommand += "/cpack";
  2187. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  2188. #endif
  2189. return this->CPackCommand.c_str();
  2190. }
  2191. void cmake::GenerateGraphViz(const char* fileName)
  2192. {
  2193. cmGeneratedFileStream str(fileName);
  2194. if ( !str )
  2195. {
  2196. return;
  2197. }
  2198. cmake cm;
  2199. cmGlobalGenerator ggi;
  2200. ggi.SetCMakeInstance(&cm);
  2201. std::auto_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator());
  2202. lg->SetGlobalGenerator(&ggi);
  2203. cmMakefile *mf = lg->GetMakefile();
  2204. std::string infile = this->GetHomeOutputDirectory();
  2205. infile += "/CMakeGraphVizOptions.cmake";
  2206. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2207. {
  2208. infile = this->GetHomeDirectory();
  2209. infile += "/CMakeGraphVizOptions.cmake";
  2210. if ( !cmSystemTools::FileExists(infile.c_str()) )
  2211. {
  2212. infile = "";
  2213. }
  2214. }
  2215. if ( !infile.empty() )
  2216. {
  2217. if ( !mf->ReadListFile(0, infile.c_str()) )
  2218. {
  2219. cmSystemTools::Error("Problem opening GraphViz options file: ",
  2220. infile.c_str());
  2221. return;
  2222. }
  2223. std::cout << "Read GraphViz options file: " << infile.c_str()
  2224. << std::endl;
  2225. }
  2226. #define __set_if_not_set(var, value, cmakeDefinition) \
  2227. const char* var = mf->GetDefinition(cmakeDefinition); \
  2228. if ( !var ) \
  2229. { \
  2230. var = value; \
  2231. }
  2232. __set_if_not_set(graphType, "digraph", "GRAPHVIZ_GRAPH_TYPE");
  2233. __set_if_not_set(graphName, "GG", "GRAPHVIZ_GRAPH_NAME");
  2234. __set_if_not_set(graphHeader, "node [\n fontsize = \"12\"\n];",
  2235. "GRAPHVIZ_GRAPH_HEADER");
  2236. __set_if_not_set(graphNodePrefix, "node", "GRAPHVIZ_NODE_PREFIX");
  2237. const char* ignoreTargets = mf->GetDefinition("GRAPHVIZ_IGNORE_TARGETS");
  2238. std::set<cmStdString> ignoreTargetsSet;
  2239. if ( ignoreTargets )
  2240. {
  2241. std::vector<std::string> ignoreTargetsVector;
  2242. cmSystemTools::ExpandListArgument(ignoreTargets,ignoreTargetsVector);
  2243. std::vector<std::string>::iterator itvIt;
  2244. for ( itvIt = ignoreTargetsVector.begin();
  2245. itvIt != ignoreTargetsVector.end();
  2246. ++ itvIt )
  2247. {
  2248. ignoreTargetsSet.insert(itvIt->c_str());
  2249. }
  2250. }
  2251. str << graphType << " " << graphName << " {" << std::endl;
  2252. str << graphHeader << std::endl;
  2253. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  2254. std::vector<cmLocalGenerator*> localGenerators;
  2255. gg->GetLocalGenerators(localGenerators);
  2256. std::vector<cmLocalGenerator*>::iterator lit;
  2257. // for target deps
  2258. // 1 - cmake target
  2259. // 2 - external target
  2260. // 0 - no deps
  2261. std::map<cmStdString, int> targetDeps;
  2262. std::map<cmStdString, cmTarget*> targetPtrs;
  2263. std::map<cmStdString, cmStdString> targetNamesNodes;
  2264. int cnt = 0;
  2265. // First pass get the list of all cmake targets
  2266. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2267. {
  2268. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2269. cmTargets::iterator tit;
  2270. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2271. {
  2272. const char* realTargetName = tit->first.c_str();
  2273. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2274. {
  2275. // Skip ignored targets
  2276. continue;
  2277. }
  2278. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  2279. cmOStringStream ostr;
  2280. ostr << graphNodePrefix << cnt++;
  2281. targetNamesNodes[realTargetName] = ostr.str();
  2282. targetPtrs[realTargetName] = &tit->second;
  2283. }
  2284. }
  2285. // Ok, now find all the stuff we link to that is not in cmake
  2286. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2287. {
  2288. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2289. cmTargets::iterator tit;
  2290. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2291. {
  2292. const cmTarget::LinkLibraryVectorType* ll
  2293. = &(tit->second.GetOriginalLinkLibraries());
  2294. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2295. const char* realTargetName = tit->first.c_str();
  2296. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2297. {
  2298. // Skip ignored targets
  2299. continue;
  2300. }
  2301. if ( ll->size() > 0 )
  2302. {
  2303. targetDeps[realTargetName] = 1;
  2304. }
  2305. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2306. {
  2307. const char* libName = llit->first.c_str();
  2308. std::map<cmStdString, cmStdString>::iterator tarIt
  2309. = targetNamesNodes.find(libName);
  2310. if ( ignoreTargetsSet.find(libName) != ignoreTargetsSet.end() )
  2311. {
  2312. // Skip ignored targets
  2313. continue;
  2314. }
  2315. if ( tarIt == targetNamesNodes.end() )
  2316. {
  2317. cmOStringStream ostr;
  2318. ostr << graphNodePrefix << cnt++;
  2319. targetDeps[libName] = 2;
  2320. targetNamesNodes[libName] = ostr.str();
  2321. //str << " \"" << ostr.c_str() << "\" [ label=\"" << libName
  2322. //<< "\" shape=\"ellipse\"];" << std::endl;
  2323. }
  2324. else
  2325. {
  2326. std::map<cmStdString, int>::iterator depIt
  2327. = targetDeps.find(libName);
  2328. if ( depIt == targetDeps.end() )
  2329. {
  2330. targetDeps[libName] = 1;
  2331. }
  2332. }
  2333. }
  2334. }
  2335. }
  2336. // Write out nodes
  2337. std::map<cmStdString, int>::iterator depIt;
  2338. for ( depIt = targetDeps.begin(); depIt != targetDeps.end(); ++ depIt )
  2339. {
  2340. const char* newTargetName = depIt->first.c_str();
  2341. std::map<cmStdString, cmStdString>::iterator tarIt
  2342. = targetNamesNodes.find(newTargetName);
  2343. if ( tarIt == targetNamesNodes.end() )
  2344. {
  2345. // We should not be here.
  2346. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2347. << " even though it was added in the previous pass" << std::endl;
  2348. abort();
  2349. }
  2350. str << " \"" << tarIt->second.c_str() << "\" [ label=\""
  2351. << newTargetName << "\" shape=\"";
  2352. if ( depIt->second == 1 )
  2353. {
  2354. std::map<cmStdString, cmTarget*>::iterator tarTypeIt= targetPtrs.find(
  2355. newTargetName);
  2356. if ( tarTypeIt == targetPtrs.end() )
  2357. {
  2358. // We should not be here.
  2359. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2360. << " even though it was added in the previous pass" << std::endl;
  2361. abort();
  2362. }
  2363. cmTarget* tg = tarTypeIt->second;
  2364. switch ( tg->GetType() )
  2365. {
  2366. case cmTarget::EXECUTABLE:
  2367. str << "house";
  2368. break;
  2369. case cmTarget::STATIC_LIBRARY:
  2370. str << "diamond";
  2371. break;
  2372. case cmTarget::SHARED_LIBRARY:
  2373. str << "polygon";
  2374. break;
  2375. case cmTarget::MODULE_LIBRARY:
  2376. str << "octagon";
  2377. break;
  2378. default:
  2379. str << "box";
  2380. }
  2381. }
  2382. else
  2383. {
  2384. str << "ellipse";
  2385. }
  2386. str << "\"];" << std::endl;
  2387. }
  2388. // Now generate the connectivity
  2389. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2390. {
  2391. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2392. cmTargets::iterator tit;
  2393. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2394. {
  2395. std::map<cmStdString, int>::iterator dependIt
  2396. = targetDeps.find(tit->first.c_str());
  2397. if ( dependIt == targetDeps.end() )
  2398. {
  2399. continue;
  2400. }
  2401. std::map<cmStdString, cmStdString>::iterator cmakeTarIt
  2402. = targetNamesNodes.find(tit->first.c_str());
  2403. const cmTarget::LinkLibraryVectorType* ll
  2404. = &(tit->second.GetOriginalLinkLibraries());
  2405. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2406. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2407. {
  2408. const char* libName = llit->first.c_str();
  2409. std::map<cmStdString, cmStdString>::iterator tarIt
  2410. = targetNamesNodes.find(libName);
  2411. if ( tarIt == targetNamesNodes.end() )
  2412. {
  2413. // We should not be here.
  2414. std::cout << __LINE__ << " Cannot find library: " << libName
  2415. << " even though it was added in the previous pass" << std::endl;
  2416. abort();
  2417. }
  2418. str << " \"" << cmakeTarIt->second.c_str() << "\" -> \""
  2419. << tarIt->second.c_str() << "\"" << std::endl;
  2420. }
  2421. }
  2422. }
  2423. // TODO: Use dotted or something for external libraries
  2424. //str << " \"node0\":f4 -> \"node12\"[color=\"#0000ff\" style=dotted]"
  2425. //<< std::endl;
  2426. //
  2427. str << "}" << std::endl;
  2428. }
  2429. //----------------------------------------------------------------------------
  2430. #ifdef CMAKE_BUILD_WITH_CMAKE
  2431. int cmake::ExecuteEchoColor(std::vector<std::string>& args)
  2432. {
  2433. // The arguments are
  2434. // argv[0] == <cmake-executable>
  2435. // argv[1] == cmake_echo_color
  2436. // On some platforms (an MSYS prompt) cmsysTerminal may not be able
  2437. // to determine whether the stream is displayed on a tty. In this
  2438. // case it assumes no unless we tell it otherwise. Since we want
  2439. // color messages to be displayed for users we will assume yes.
  2440. // However, we can test for some situations when the answer is most
  2441. // likely no.
  2442. int assumeTTY = cmsysTerminal_Color_AssumeTTY;
  2443. if(cmSystemTools::GetEnv("DART_TEST_FROM_DART") ||
  2444. cmSystemTools::GetEnv("DASHBOARD_TEST_FROM_CTEST") ||
  2445. cmSystemTools::GetEnv("CTEST_INTERACTIVE_DEBUG_MODE"))
  2446. {
  2447. // Avoid printing color escapes during dashboard builds.
  2448. assumeTTY = 0;
  2449. }
  2450. bool enabled = true;
  2451. int color = cmsysTerminal_Color_Normal;
  2452. bool newline = true;
  2453. for(unsigned int i=2; i < args.size(); ++i)
  2454. {
  2455. if(args[i].find("--switch=") == 0)
  2456. {
  2457. // Enable or disable color based on the switch value.
  2458. std::string value = args[i].substr(9);
  2459. if(!value.empty())
  2460. {
  2461. if(cmSystemTools::IsOn(value.c_str()))
  2462. {
  2463. enabled = true;
  2464. }
  2465. else
  2466. {
  2467. enabled = false;
  2468. }
  2469. }
  2470. }
  2471. else if(args[i] == "--normal")
  2472. {
  2473. color = cmsysTerminal_Color_Normal;
  2474. }
  2475. else if(args[i] == "--black")
  2476. {
  2477. color = cmsysTerminal_Color_ForegroundBlack;
  2478. }
  2479. else if(args[i] == "--red")
  2480. {
  2481. color = cmsysTerminal_Color_ForegroundRed;
  2482. }
  2483. else if(args[i] == "--green")
  2484. {
  2485. color = cmsysTerminal_Color_ForegroundGreen;
  2486. }
  2487. else if(args[i] == "--yellow")
  2488. {
  2489. color = cmsysTerminal_Color_ForegroundYellow;
  2490. }
  2491. else if(args[i] == "--blue")
  2492. {
  2493. color = cmsysTerminal_Color_ForegroundBlue;
  2494. }
  2495. else if(args[i] == "--magenta")
  2496. {
  2497. color = cmsysTerminal_Color_ForegroundMagenta;
  2498. }
  2499. else if(args[i] == "--cyan")
  2500. {
  2501. color = cmsysTerminal_Color_ForegroundCyan;
  2502. }
  2503. else if(args[i] == "--white")
  2504. {
  2505. color = cmsysTerminal_Color_ForegroundWhite;
  2506. }
  2507. else if(args[i] == "--bold")
  2508. {
  2509. color |= cmsysTerminal_Color_ForegroundBold;
  2510. }
  2511. else if(args[i] == "--no-newline")
  2512. {
  2513. newline = false;
  2514. }
  2515. else if(args[i] == "--newline")
  2516. {
  2517. newline = true;
  2518. }
  2519. else if(enabled)
  2520. {
  2521. // Color is enabled. Print with the current color.
  2522. cmsysTerminal_cfprintf(color | assumeTTY, stdout, "%s%s",
  2523. args[i].c_str(), newline? "\n" : "");
  2524. }
  2525. else
  2526. {
  2527. // Color is disabled. Print without color.
  2528. fprintf(stdout, "%s%s", args[i].c_str(), newline? "\n" : "");
  2529. }
  2530. }
  2531. return 0;
  2532. }
  2533. #else
  2534. int cmake::ExecuteEchoColor(std::vector<std::string>&)
  2535. {
  2536. return 1;
  2537. }
  2538. #endif
  2539. //----------------------------------------------------------------------------
  2540. int cmake::ExecuteLinkScript(std::vector<std::string>& args)
  2541. {
  2542. // The arguments are
  2543. // argv[0] == <cmake-executable>
  2544. // argv[1] == cmake_link_script
  2545. // argv[2] == <link-script-name>
  2546. // argv[3] == --verbose=?
  2547. bool verbose = false;
  2548. if(args.size() >= 4)
  2549. {
  2550. if(args[3].find("--verbose=") == 0)
  2551. {
  2552. if(!cmSystemTools::IsOff(args[3].substr(10).c_str()))
  2553. {
  2554. verbose = true;
  2555. }
  2556. }
  2557. }
  2558. // Allocate a process instance.
  2559. cmsysProcess* cp = cmsysProcess_New();
  2560. if(!cp)
  2561. {
  2562. std::cerr << "Error allocating process instance in link script."
  2563. << std::endl;
  2564. return 1;
  2565. }
  2566. // Children should share stdout and stderr with this process.
  2567. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDOUT, 1);
  2568. cmsysProcess_SetPipeShared(cp, cmsysProcess_Pipe_STDERR, 1);
  2569. // Run the command lines verbatim.
  2570. cmsysProcess_SetOption(cp, cmsysProcess_Option_Verbatim, 1);
  2571. // Read command lines from the script.
  2572. std::ifstream fin(args[2].c_str());
  2573. if(!fin)
  2574. {
  2575. std::cerr << "Error opening link script \""
  2576. << args[2] << "\"" << std::endl;
  2577. return 1;
  2578. }
  2579. // Run one command at a time.
  2580. std::string command;
  2581. int result = 0;
  2582. while(result == 0 && cmSystemTools::GetLineFromStream(fin, command))
  2583. {
  2584. // Setup this command line.
  2585. const char* cmd[2] = {command.c_str(), 0};
  2586. cmsysProcess_SetCommand(cp, cmd);
  2587. // Report the command if verbose output is enabled.
  2588. if(verbose)
  2589. {
  2590. std::cout << command << std::endl;
  2591. }
  2592. // Run the command and wait for it to exit.
  2593. cmsysProcess_Execute(cp);
  2594. cmsysProcess_WaitForExit(cp, 0);
  2595. // Report failure if any.
  2596. switch(cmsysProcess_GetState(cp))
  2597. {
  2598. case cmsysProcess_State_Exited:
  2599. {
  2600. int value = cmsysProcess_GetExitValue(cp);
  2601. if(value != 0)
  2602. {
  2603. result = value;
  2604. }
  2605. }
  2606. break;
  2607. case cmsysProcess_State_Exception:
  2608. std::cerr << "Error running link command: "
  2609. << cmsysProcess_GetExceptionString(cp) << std::endl;
  2610. result = 1;
  2611. break;
  2612. case cmsysProcess_State_Error:
  2613. std::cerr << "Error running link command: "
  2614. << cmsysProcess_GetErrorString(cp) << std::endl;
  2615. result = 2;
  2616. break;
  2617. default:
  2618. break;
  2619. };
  2620. }
  2621. // Free the process instance.
  2622. cmsysProcess_Delete(cp);
  2623. // Return the final resulting return value.
  2624. return result;
  2625. }
  2626. void cmake::DefineProperty(const char *name, cmProperty::ScopeType scope,
  2627. const char *ShortDescription,
  2628. const char *FullDescription,
  2629. bool chained)
  2630. {
  2631. switch (scope)
  2632. {
  2633. case cmProperty::GLOBAL:
  2634. this->GlobalProperties.DefineProperty(name,scope,ShortDescription,
  2635. FullDescription, chained);
  2636. break;
  2637. case cmProperty::TARGET:
  2638. this->TargetProperties.DefineProperty(name,scope,ShortDescription,
  2639. FullDescription, chained);
  2640. break;
  2641. case cmProperty::SOURCE_FILE:
  2642. this->SourceFileProperties.DefineProperty(name,scope,ShortDescription,
  2643. FullDescription, chained);
  2644. break;
  2645. case cmProperty::DIRECTORY:
  2646. this->DirectoryProperties.DefineProperty(name,scope,ShortDescription,
  2647. FullDescription, chained);
  2648. break;
  2649. case cmProperty::TEST:
  2650. this->TestProperties.DefineProperty(name,scope,ShortDescription,
  2651. FullDescription, chained);
  2652. break;
  2653. }
  2654. }
  2655. bool cmake::IsPropertyDefined(const char *name, cmProperty::ScopeType scope)
  2656. {
  2657. switch (scope)
  2658. {
  2659. case cmProperty::GLOBAL:
  2660. return this->GlobalProperties.IsPropertyDefined(name);
  2661. case cmProperty::TARGET:
  2662. return this->TargetProperties.IsPropertyDefined(name);
  2663. case cmProperty::SOURCE_FILE:
  2664. return this->SourceFileProperties.IsPropertyDefined(name);
  2665. case cmProperty::DIRECTORY:
  2666. return this->DirectoryProperties.IsPropertyDefined(name);
  2667. case cmProperty::TEST:
  2668. return this->TestProperties.IsPropertyDefined(name);
  2669. }
  2670. return false;
  2671. }
  2672. bool cmake::IsPropertyChained(const char *name, cmProperty::ScopeType scope)
  2673. {
  2674. switch (scope)
  2675. {
  2676. case cmProperty::GLOBAL:
  2677. return this->GlobalProperties.IsPropertyChained(name);
  2678. case cmProperty::TARGET:
  2679. return this->TargetProperties.IsPropertyChained(name);
  2680. case cmProperty::SOURCE_FILE:
  2681. return this->SourceFileProperties.IsPropertyChained(name);
  2682. case cmProperty::DIRECTORY:
  2683. return this->DirectoryProperties.IsPropertyChained(name);
  2684. case cmProperty::TEST:
  2685. return this->DirectoryProperties.IsPropertyChained(name);
  2686. }
  2687. return false;
  2688. }
  2689. void cmake::SetProperty(const char* prop, const char* value)
  2690. {
  2691. if (!prop)
  2692. {
  2693. return;
  2694. }
  2695. if (!value)
  2696. {
  2697. value = "NOTFOUND";
  2698. }
  2699. this->Properties.SetProperty(prop, value, cmProperty::TARGET);
  2700. }
  2701. const char *cmake::GetProperty(const char* prop)
  2702. {
  2703. return this->GetProperty(prop, cmProperty::GLOBAL);
  2704. }
  2705. const char *cmake::GetProperty(const char* prop, cmProperty::ScopeType scope)
  2706. {
  2707. bool chain = false;
  2708. return this->Properties.GetPropertyValue(prop, scope, chain);
  2709. }
  2710. bool cmake::GetPropertyAsBool(const char* prop)
  2711. {
  2712. return cmSystemTools::IsOn(this->GetProperty(prop));
  2713. }
  2714. int cmake::GetSystemInformation(std::vector<std::string>& args)
  2715. {
  2716. // we must create a temporary directory, copy some files to it
  2717. // run cmake on it, and then collect the results.
  2718. // so create the directory
  2719. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2720. std::string destPath = cwd + "/__cmake_systeminformation";
  2721. cmSystemTools::RemoveADirectory(destPath.c_str());
  2722. if (!cmSystemTools::MakeDirectory(destPath.c_str()))
  2723. {
  2724. std::cerr << "Error: --system-information must be run from a "
  2725. "writable directory!\n";
  2726. return 1;
  2727. }
  2728. // we have to find the module directory, so we can copy the files
  2729. this->AddCMakePaths(args[0].c_str());
  2730. std::string modulesPath =
  2731. this->CacheManager->GetCacheValue("CMAKE_ROOT");
  2732. modulesPath += "/Modules";
  2733. std::string inFile = modulesPath;
  2734. inFile += "/SystemInformation.cmake";
  2735. std::string outFile = destPath;
  2736. outFile += "/CMakeLists.txt";
  2737. // Copy file
  2738. if(!cmSystemTools::cmCopyFile(inFile.c_str(), outFile.c_str()))
  2739. {
  2740. std::cerr << "Error copying file \"" << inFile.c_str()
  2741. << "\" to \"" << outFile.c_str() << "\".\n";
  2742. return 1;
  2743. }
  2744. // do we write to a file or to stdout?
  2745. std::string resultFile;
  2746. if (args.size() == 1)
  2747. {
  2748. resultFile = cwd;
  2749. resultFile += "/__cmake_systeminformation/results.txt";
  2750. }
  2751. else
  2752. {
  2753. if (!cmSystemTools::FileIsFullPath(args[1].c_str()))
  2754. {
  2755. resultFile += cwd;
  2756. resultFile += "/";
  2757. }
  2758. resultFile = args[1];
  2759. }
  2760. // now run cmake on the CMakeLists file
  2761. cmSystemTools::ChangeDirectory(destPath.c_str());
  2762. cmake cm;
  2763. std::vector<std::string> args2;
  2764. args2.push_back(args[0]);
  2765. args2.push_back(destPath);
  2766. std::string resultArg = "-DRESULT_FILE=";
  2767. resultArg += resultFile;
  2768. args2.push_back(resultArg);
  2769. int res = cm.Run(args2, false);
  2770. if (res != 0)
  2771. {
  2772. std::cerr << "Error: --system-information failed on internal CMake!\n";
  2773. return res;
  2774. }
  2775. // change back to the original directory
  2776. cmSystemTools::ChangeDirectory(cwd.c_str());
  2777. // echo results to stdout if needed
  2778. if (args.size() == 1)
  2779. {
  2780. FILE* fin = fopen(resultFile.c_str(), "r");
  2781. if(fin)
  2782. {
  2783. const int bufferSize = 4096;
  2784. char buffer[bufferSize];
  2785. int n;
  2786. while((n = fread(buffer, 1, bufferSize, fin)) > 0)
  2787. {
  2788. for(char* c = buffer; c < buffer+n; ++c)
  2789. {
  2790. putc(*c, stdout);
  2791. }
  2792. fflush(stdout);
  2793. }
  2794. fclose(fin);
  2795. }
  2796. }
  2797. // clean up the directory
  2798. cmSystemTools::RemoveADirectory(destPath.c_str());
  2799. return 0;
  2800. }