cmake.cxx 81 KB

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