cmake.cxx 84 KB

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