cmake.cxx 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163
  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. #endif
  27. // only build kdevelop generator on non-windows platforms
  28. // when not bootstrapping cmake
  29. #if !defined(_WIN32)
  30. # if defined(CMAKE_BUILD_WITH_CMAKE)
  31. # define CMAKE_USE_KDEVELOP
  32. # endif
  33. #endif
  34. #if defined(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
  35. # define CMAKE_BOOT_MINGW
  36. #endif
  37. // include the generator
  38. #if defined(_WIN32) && !defined(__CYGWIN__)
  39. # if !defined(CMAKE_BOOT_MINGW)
  40. # include "cmGlobalVisualStudio6Generator.h"
  41. # include "cmGlobalVisualStudio7Generator.h"
  42. # include "cmGlobalVisualStudio71Generator.h"
  43. # include "cmGlobalVisualStudio8Generator.h"
  44. # include "cmGlobalBorlandMakefileGenerator.h"
  45. # include "cmGlobalNMakeMakefileGenerator.h"
  46. # include "cmGlobalWatcomWMakeGenerator.h"
  47. # endif
  48. # include "cmGlobalMSYSMakefileGenerator.h"
  49. # include "cmGlobalMinGWMakefileGenerator.h"
  50. # include "cmWin32ProcessExecution.h"
  51. #else
  52. #endif
  53. #include "cmGlobalUnixMakefileGenerator3.h"
  54. #ifdef CMAKE_USE_KDEVELOP
  55. # include "cmGlobalKdevelopGenerator.h"
  56. #endif
  57. #include <stdlib.h> // required for atoi
  58. #ifdef __APPLE__
  59. # include "cmGlobalXCodeGenerator.h"
  60. # define CMAKE_USE_XCODE 1
  61. # include <sys/types.h>
  62. # include <sys/time.h>
  63. # include <sys/resource.h>
  64. #endif
  65. #include <sys/stat.h> // struct stat
  66. #include <memory> // auto_ptr
  67. void cmNeedBackwardsCompatibility(const std::string& variable,
  68. int access_type, void* )
  69. {
  70. #ifdef CMAKE_BUILD_WITH_CMAKE
  71. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  72. {
  73. std::string message = "An attempt was made to access a variable: ";
  74. message += variable;
  75. message += " that has not been defined. Some variables were always defined "
  76. "by CMake in versions prior to 1.6. To fix this you might need to set the "
  77. "cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If you are "
  78. "writing a CMakeList file, (or have already set "
  79. "CMAKE_BACKWARDS_COMPATABILITY to 1.4 or less) then you probably need to "
  80. "include a CMake module to test for the feature this variable defines.";
  81. cmSystemTools::Error(message.c_str());
  82. }
  83. #else
  84. (void)variable;
  85. (void)access_type;
  86. #endif
  87. }
  88. cmake::cmake()
  89. {
  90. m_DebugTryCompile = false;
  91. m_ClearBuildSystem = false;
  92. m_FileComparison = new cmFileTimeComparison;
  93. #ifdef __APPLE__
  94. struct rlimit rlp;
  95. if(!getrlimit(RLIMIT_STACK, &rlp))
  96. {
  97. if(rlp.rlim_cur != rlp.rlim_max)
  98. {
  99. rlp.rlim_cur = rlp.rlim_max;
  100. setrlimit(RLIMIT_STACK, &rlp);
  101. }
  102. }
  103. #endif
  104. // If MAKEFLAGS are given in the environment, remove the environment
  105. // variable. This will prevent try-compile from succeeding when it
  106. // should fail (if "-i" is an option). We cannot simply test
  107. // whether "-i" is given and remove it because some make programs
  108. // encode the MAKEFLAGS variable in a strange way.
  109. if(getenv("MAKEFLAGS"))
  110. {
  111. cmSystemTools::PutEnv("MAKEFLAGS=");
  112. }
  113. m_Verbose = false;
  114. m_InTryCompile = false;
  115. m_CacheManager = new cmCacheManager;
  116. m_GlobalGenerator = 0;
  117. m_ProgressCallback = 0;
  118. m_ProgressCallbackClientData = 0;
  119. m_ScriptMode = false;
  120. #ifdef CMAKE_BUILD_WITH_CMAKE
  121. m_VariableWatch = new cmVariableWatch;
  122. m_VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  123. cmNeedBackwardsCompatibility);
  124. m_VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  125. cmNeedBackwardsCompatibility);
  126. m_VariableWatch->AddWatch("CMAKE_X_LIBS",
  127. cmNeedBackwardsCompatibility);
  128. #endif
  129. this->AddDefaultGenerators();
  130. this->AddDefaultCommands();
  131. // Make sure we can capture the build tool output.
  132. cmSystemTools::EnableVSConsoleOutput();
  133. }
  134. cmake::~cmake()
  135. {
  136. delete m_CacheManager;
  137. if (m_GlobalGenerator)
  138. {
  139. delete m_GlobalGenerator;
  140. m_GlobalGenerator = 0;
  141. }
  142. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  143. j != m_Commands.end(); ++j)
  144. {
  145. delete (*j).second;
  146. }
  147. #ifdef CMAKE_BUILD_WITH_CMAKE
  148. delete m_VariableWatch;
  149. #endif
  150. delete m_FileComparison;
  151. }
  152. void cmake::CleanupCommandsAndMacros()
  153. {
  154. std::vector<cmCommand*> commands;
  155. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  156. j != m_Commands.end(); ++j)
  157. {
  158. if ( !j->second->IsA("cmMacroHelperCommand") )
  159. {
  160. commands.push_back(j->second);
  161. }
  162. else
  163. {
  164. delete j->second;
  165. }
  166. }
  167. m_Commands.erase(m_Commands.begin(), m_Commands.end());
  168. std::vector<cmCommand*>::iterator it;
  169. for ( it = commands.begin(); it != commands.end();
  170. ++ it )
  171. {
  172. m_Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  173. }
  174. }
  175. bool cmake::CommandExists(const char* name) const
  176. {
  177. std::string sName = cmSystemTools::LowerCase(name);
  178. return (m_Commands.find(sName) != m_Commands.end());
  179. }
  180. cmCommand *cmake::GetCommand(const char *name)
  181. {
  182. cmCommand* rm = 0;
  183. std::string sName = cmSystemTools::LowerCase(name);
  184. RegisteredCommandsMap::iterator pos = m_Commands.find(sName);
  185. if (pos != m_Commands.end())
  186. {
  187. rm = (*pos).second;
  188. }
  189. return rm;
  190. }
  191. void cmake::RenameCommand(const char*oldName, const char* newName)
  192. {
  193. // if the command already exists, free the old one
  194. std::string sOldName = cmSystemTools::LowerCase(oldName);
  195. std::string sNewName = cmSystemTools::LowerCase(newName);
  196. RegisteredCommandsMap::iterator pos = m_Commands.find(sOldName);
  197. if ( pos == m_Commands.end() )
  198. {
  199. return;
  200. }
  201. cmCommand* cmd = pos->second;
  202. pos = m_Commands.find(sNewName);
  203. if (pos != m_Commands.end())
  204. {
  205. delete pos->second;
  206. m_Commands.erase(pos);
  207. }
  208. m_Commands.insert(RegisteredCommandsMap::value_type(sNewName, cmd));
  209. pos = m_Commands.find(sOldName);
  210. m_Commands.erase(pos);
  211. }
  212. void cmake::AddCommand(cmCommand* wg)
  213. {
  214. std::string name = cmSystemTools::LowerCase(wg->GetName());
  215. // if the command already exists, free the old one
  216. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  217. if (pos != m_Commands.end())
  218. {
  219. delete pos->second;
  220. m_Commands.erase(pos);
  221. }
  222. m_Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  223. }
  224. // Parse the args
  225. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  226. {
  227. for(unsigned int i=1; i < args.size(); ++i)
  228. {
  229. std::string arg = args[i];
  230. if(arg.find("-D",0) == 0)
  231. {
  232. std::string entry = arg.substr(2);
  233. if(entry.size() == 0)
  234. {
  235. entry = args[++i];
  236. }
  237. std::string var, value;
  238. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  239. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  240. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  241. {
  242. this->m_CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  243. "No help, variable specified on the command line.",
  244. type);
  245. }
  246. else
  247. {
  248. std::cerr << "Parse error in command line argument: " << arg << "\n"
  249. << "Should be: VAR:type=value\n";
  250. cmSystemTools::Error("No cmake scrpt provided.");
  251. return false;
  252. }
  253. }
  254. else if(arg.find("-C",0) == 0)
  255. {
  256. std::string path = arg.substr(2);
  257. if ( path.size() == 0 )
  258. {
  259. path = args[++i];
  260. }
  261. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  262. this->ReadListFile(path.c_str());
  263. }
  264. else if(arg.find("-P",0) == 0)
  265. {
  266. i++;
  267. std::string path = args[i];
  268. if ( path.size() == 0 )
  269. {
  270. cmSystemTools::Error("No cmake script provided.");
  271. return false;
  272. }
  273. std::cerr << "Running cmake script file " << path.c_str() << "\n";
  274. this->ReadListFile(path.c_str());
  275. }
  276. }
  277. return true;
  278. }
  279. void cmake::ReadListFile(const char *path)
  280. {
  281. // if a generator was not yet created, temporarily create one
  282. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  283. bool created = false;
  284. // if a generator was not specified use a generic one
  285. if (!gg)
  286. {
  287. gg = new cmGlobalGenerator;
  288. gg->SetCMakeInstance(this);
  289. created = true;
  290. }
  291. // read in the list file to fill the cache
  292. if(path)
  293. {
  294. std::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  295. lg->SetGlobalGenerator(gg);
  296. lg->GetMakefile()->SetHomeOutputDirectory
  297. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  298. lg->GetMakefile()->SetStartOutputDirectory
  299. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  300. lg->GetMakefile()->SetHomeDirectory
  301. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  302. lg->GetMakefile()->SetStartDirectory
  303. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  304. if (!lg->GetMakefile()->ReadListFile(0, path))
  305. {
  306. std::cerr << "Error processing file:" << path << "\n";
  307. }
  308. }
  309. // free generic one if generated
  310. if (created)
  311. {
  312. delete gg;
  313. }
  314. }
  315. // Parse the args
  316. void cmake::SetArgs(const std::vector<std::string>& args)
  317. {
  318. bool directoriesSet = false;
  319. for(unsigned int i=1; i < args.size(); ++i)
  320. {
  321. std::string arg = args[i];
  322. if(arg.find("-H",0) == 0)
  323. {
  324. directoriesSet = true;
  325. std::string path = arg.substr(2);
  326. path = cmSystemTools::CollapseFullPath(path.c_str());
  327. cmSystemTools::ConvertToUnixSlashes(path);
  328. this->SetHomeDirectory(path.c_str());
  329. }
  330. else if(arg.find("-S",0) == 0)
  331. {
  332. // There is no local generate anymore. Ignore -S option.
  333. }
  334. else if(arg.find("-O",0) == 0)
  335. {
  336. // There is no local generate anymore. Ignore -O option.
  337. }
  338. else if(arg.find("-B",0) == 0)
  339. {
  340. directoriesSet = true;
  341. std::string path = arg.substr(2);
  342. path = cmSystemTools::CollapseFullPath(path.c_str());
  343. cmSystemTools::ConvertToUnixSlashes(path);
  344. this->SetHomeOutputDirectory(path.c_str());
  345. }
  346. else if((i < args.size()-1) && (arg.find("--check-build-system",0) == 0))
  347. {
  348. m_CheckBuildSystem = args[++i];
  349. m_ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  350. }
  351. else if(arg.find("-V",0) == 0)
  352. {
  353. m_Verbose = true;
  354. }
  355. else if(arg.find("-D",0) == 0)
  356. {
  357. // skip for now
  358. }
  359. else if(arg.find("-C",0) == 0)
  360. {
  361. // skip for now
  362. }
  363. else if(arg.find("-P",0) == 0)
  364. {
  365. // skip for now
  366. i++;
  367. }
  368. else if(arg.find("--graphviz=",0) == 0)
  369. {
  370. std::string path = arg.substr(strlen("--graphviz="));
  371. path = cmSystemTools::CollapseFullPath(path.c_str());
  372. cmSystemTools::ConvertToUnixSlashes(path);
  373. m_GraphVizFile = path;
  374. if ( m_GraphVizFile.empty() )
  375. {
  376. cmSystemTools::Error("No file specified for --graphviz");
  377. }
  378. }
  379. else if(arg.find("--debug-trycompile",0) == 0)
  380. {
  381. std::cout << "debug trycompile on\n";
  382. this->DebugTryCompileOn();
  383. }
  384. else if(arg.find("-G",0) == 0)
  385. {
  386. std::string value = arg.substr(2);
  387. if(value.size() == 0)
  388. {
  389. value = args[++i];
  390. }
  391. cmGlobalGenerator* gen =
  392. this->CreateGlobalGenerator(value.c_str());
  393. if(!gen)
  394. {
  395. cmSystemTools::Error("Could not create named generator ",
  396. value.c_str());
  397. }
  398. else
  399. {
  400. this->SetGlobalGenerator(gen);
  401. }
  402. }
  403. // no option assume it is the path to the source
  404. else
  405. {
  406. directoriesSet = true;
  407. this->SetDirectoriesFromFile(arg.c_str());
  408. }
  409. }
  410. if(!directoriesSet)
  411. {
  412. this->SetHomeOutputDirectory
  413. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  414. this->SetStartOutputDirectory
  415. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  416. this->SetHomeDirectory
  417. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  418. this->SetStartDirectory
  419. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  420. }
  421. this->SetStartDirectory(this->GetHomeDirectory());
  422. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  423. }
  424. //----------------------------------------------------------------------------
  425. void cmake::SetDirectoriesFromFile(const char* arg)
  426. {
  427. // Check if the argument refers to a CMakeCache.txt or
  428. // CMakeLists.txt file.
  429. std::string listPath;
  430. std::string cachePath;
  431. bool argIsFile = false;
  432. if(cmSystemTools::FileIsDirectory(arg))
  433. {
  434. std::string path = cmSystemTools::CollapseFullPath(arg);
  435. cmSystemTools::ConvertToUnixSlashes(path);
  436. std::string cacheFile = path;
  437. cacheFile += "/CMakeCache.txt";
  438. std::string listFile = path;
  439. listFile += "/CMakeLists.txt";
  440. if(cmSystemTools::FileExists(cacheFile.c_str()))
  441. {
  442. cachePath = path;
  443. }
  444. if(cmSystemTools::FileExists(listFile.c_str()))
  445. {
  446. listPath = path;
  447. }
  448. }
  449. else if(cmSystemTools::FileExists(arg))
  450. {
  451. argIsFile = true;
  452. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  453. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  454. name = cmSystemTools::LowerCase(name);
  455. if(name == "cmakecache.txt")
  456. {
  457. cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  458. }
  459. else if(name == "cmakelists.txt")
  460. {
  461. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  462. }
  463. }
  464. else
  465. {
  466. // Specified file or directory does not exist. Try to set things
  467. // up to produce a meaningful error message.
  468. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  469. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  470. name = cmSystemTools::LowerCase(name);
  471. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  472. {
  473. argIsFile = true;
  474. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  475. }
  476. else
  477. {
  478. listPath = fullPath;
  479. }
  480. }
  481. // If there is a CMakeCache.txt file, use its settings.
  482. if(cachePath.length() > 0)
  483. {
  484. cmCacheManager* cachem = this->GetCacheManager();
  485. cmCacheManager::CacheIterator it = cachem->NewIterator();
  486. if(cachem->LoadCache(cachePath.c_str()) && it.Find("CMAKE_HOME_DIRECTORY"))
  487. {
  488. this->SetHomeOutputDirectory(cachePath.c_str());
  489. this->SetStartOutputDirectory(cachePath.c_str());
  490. this->SetHomeDirectory(it.GetValue());
  491. this->SetStartDirectory(it.GetValue());
  492. return;
  493. }
  494. }
  495. // If there is a CMakeLists.txt file, use it as the source tree.
  496. if(listPath.length() > 0)
  497. {
  498. this->SetHomeDirectory(listPath.c_str());
  499. this->SetStartDirectory(listPath.c_str());
  500. if(argIsFile)
  501. {
  502. // Source CMakeLists.txt file given. It was probably dropped
  503. // onto the executable in a GUI. Default to an in-source build.
  504. this->SetHomeOutputDirectory(listPath.c_str());
  505. this->SetStartOutputDirectory(listPath.c_str());
  506. }
  507. else
  508. {
  509. // Source directory given on command line. Use current working
  510. // directory as build tree.
  511. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  512. this->SetHomeOutputDirectory(cwd.c_str());
  513. this->SetStartOutputDirectory(cwd.c_str());
  514. }
  515. return;
  516. }
  517. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  518. // argument. Assume it is the path to the source tree, and use the
  519. // current working directory as the build tree.
  520. std::string full = cmSystemTools::CollapseFullPath(arg);
  521. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  522. this->SetHomeDirectory(full.c_str());
  523. this->SetStartDirectory(full.c_str());
  524. this->SetHomeOutputDirectory(cwd.c_str());
  525. this->SetStartOutputDirectory(cwd.c_str());
  526. }
  527. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the cache
  528. int cmake::AddCMakePaths(const char *arg0)
  529. {
  530. // Find our own executable.
  531. std::vector<cmStdString> failures;
  532. std::string cMakeSelf = arg0;
  533. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  534. failures.push_back(cMakeSelf);
  535. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  536. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  537. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  538. {
  539. #ifdef CMAKE_BUILD_DIR
  540. std::string intdir = ".";
  541. #ifdef CMAKE_INTDIR
  542. intdir = CMAKE_INTDIR;
  543. #endif
  544. cMakeSelf = CMAKE_BUILD_DIR;
  545. cMakeSelf += "/bin/";
  546. cMakeSelf += intdir;
  547. cMakeSelf += "/cmake";
  548. cMakeSelf += cmSystemTools::GetExecutableExtension();
  549. #endif
  550. }
  551. #ifdef CMAKE_PREFIX
  552. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  553. {
  554. failures.push_back(cMakeSelf);
  555. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  556. }
  557. #endif
  558. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  559. {
  560. failures.push_back(cMakeSelf);
  561. cmOStringStream msg;
  562. msg << "CMAKE can not find the command line program cmake.\n";
  563. msg << " argv[0] = \"" << arg0 << "\"\n";
  564. msg << " Attempted paths:\n";
  565. std::vector<cmStdString>::iterator i;
  566. for(i=failures.begin(); i != failures.end(); ++i)
  567. {
  568. msg << " \"" << i->c_str() << "\"\n";
  569. }
  570. cmSystemTools::Error(msg.str().c_str());
  571. return 0;
  572. }
  573. // Save the value in the cache
  574. this->m_CacheManager->AddCacheEntry
  575. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  576. cmCacheManager::INTERNAL);
  577. // Find and save the command to edit the cache
  578. std::string editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  579. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  580. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  581. {
  582. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  583. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  584. }
  585. std::string ctestCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  586. "/ctest" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  587. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  588. {
  589. this->m_CacheManager->AddCacheEntry
  590. ("CMAKE_CTEST_COMMAND", ctestCommand.c_str(),
  591. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  592. }
  593. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  594. {
  595. this->m_CacheManager->AddCacheEntry
  596. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  597. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  598. }
  599. // do CMAKE_ROOT, look for the environment variable first
  600. std::string cMakeRoot;
  601. std::string modules;
  602. if (getenv("CMAKE_ROOT"))
  603. {
  604. cMakeRoot = getenv("CMAKE_ROOT");
  605. modules = cMakeRoot + "/Modules/CMake.cmake";
  606. }
  607. if(!cmSystemTools::FileExists(modules.c_str()))
  608. {
  609. // next try exe/..
  610. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  611. std::string::size_type slashPos = cMakeRoot.rfind("/");
  612. if(slashPos != std::string::npos)
  613. {
  614. cMakeRoot = cMakeRoot.substr(0, slashPos);
  615. }
  616. // is there no Modules direcory there?
  617. modules = cMakeRoot + "/Modules/CMake.cmake";
  618. }
  619. if (!cmSystemTools::FileExists(modules.c_str()))
  620. {
  621. // try exe/../share/cmake
  622. cMakeRoot += CMAKE_DATA_DIR;
  623. modules = cMakeRoot + "/Modules/CMake.cmake";
  624. }
  625. #ifdef CMAKE_ROOT_DIR
  626. if (!cmSystemTools::FileExists(modules.c_str()))
  627. {
  628. // try compiled in root directory
  629. cMakeRoot = CMAKE_ROOT_DIR;
  630. modules = cMakeRoot + "/Modules/CMake.cmake";
  631. }
  632. #endif
  633. #ifdef CMAKE_PREFIX
  634. if (!cmSystemTools::FileExists(modules.c_str()))
  635. {
  636. // try compiled in install prefix
  637. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  638. modules = cMakeRoot + "/Modules/CMake.cmake";
  639. }
  640. #endif
  641. if (!cmSystemTools::FileExists(modules.c_str()))
  642. {
  643. // try
  644. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  645. cMakeRoot += CMAKE_DATA_DIR;
  646. modules = cMakeRoot + "/Modules/CMake.cmake";
  647. }
  648. if(!cmSystemTools::FileExists(modules.c_str()))
  649. {
  650. // next try exe
  651. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  652. // is there no Modules direcory there?
  653. modules = cMakeRoot + "/Modules/CMake.cmake";
  654. }
  655. if (!cmSystemTools::FileExists(modules.c_str()))
  656. {
  657. // couldn't find modules
  658. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  659. "CMake has most likely not been installed correctly.\n"
  660. "Modules directory not found in\n",
  661. cMakeRoot.c_str());
  662. return 0;
  663. }
  664. this->m_CacheManager->AddCacheEntry
  665. ("CMAKE_ROOT", cMakeRoot.c_str(),
  666. "Path to CMake installation.", cmCacheManager::INTERNAL);
  667. #ifdef _WIN32
  668. std::string comspec = "cmw9xcom.exe";
  669. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  670. #endif
  671. return 1;
  672. }
  673. void CMakeCommandUsage(const char* program)
  674. {
  675. cmOStringStream errorStream;
  676. #ifdef CMAKE_BUILD_WITH_CMAKE
  677. errorStream
  678. << "cmake version " << cmVersion::GetCMakeVersion() << "\n";
  679. #else
  680. errorStream
  681. << "cmake bootstrap\n";
  682. #endif
  683. errorStream
  684. << "Usage: " << program << " -E [command] [arguments ...]\n"
  685. << "Available commands: \n"
  686. << " chdir dir cmd [args]... - run command in a given directory\n"
  687. << " copy file destination - copy file to destination (either file or directory)\n"
  688. << " copy_if_different in-file out-file - copy file if input has changed\n"
  689. << " copy_directory source destination - copy directory 'source' content to directory 'destination'\n"
  690. << " compare_files file1 file2 - check if file1 is same as file2\n"
  691. << " echo [string]... - displays arguments as text\n"
  692. << " remove file1 file2 ... - remove the file(s)\n"
  693. << " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar.\n"
  694. << " time command [args] ... - run command and return elapsed time\n";
  695. #if defined(_WIN32) && !defined(__CYGWIN__)
  696. errorStream
  697. << " write_regv key value - write registry value\n"
  698. << " delete_regv key - delete registry value\n"
  699. << " comspec - on windows 9x use this for RunCommand\n";
  700. #else
  701. errorStream
  702. << " create_symlink old new - create a symbolic link new -> old\n";
  703. #endif
  704. cmSystemTools::Error(errorStream.str().c_str());
  705. }
  706. int cmake::CMakeCommand(std::vector<std::string>& args)
  707. {
  708. if (args.size() > 1)
  709. {
  710. // Copy file
  711. if (args[1] == "copy" && args.size() == 4)
  712. {
  713. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  714. {
  715. std::cerr << "Error copying file \"" << args[2].c_str()
  716. << "\" to \"" << args[3].c_str() << "\".\n";
  717. return 1;
  718. }
  719. return 0;
  720. }
  721. // Copy file if different.
  722. if (args[1] == "copy_if_different" && args.size() == 4)
  723. {
  724. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(), args[3].c_str()))
  725. {
  726. std::cerr << "Error copying file (if different) from \""
  727. << args[2].c_str() << "\" to \"" << args[3].c_str()
  728. << "\".\n";
  729. return 1;
  730. }
  731. return 0;
  732. }
  733. // Copy directory content
  734. if (args[1] == "copy_directory" && args.size() == 4)
  735. {
  736. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  737. {
  738. std::cerr << "Error copying directory from \""
  739. << args[2].c_str() << "\" to \"" << args[3].c_str()
  740. << "\".\n";
  741. return 1;
  742. }
  743. return 0;
  744. }
  745. // Compare files
  746. if (args[1] == "compare_files" && args.size() == 4)
  747. {
  748. if(cmSystemTools::FilesDiffer(args[2].c_str(), args[3].c_str()))
  749. {
  750. std::cerr << "Files \""
  751. << args[2].c_str() << "\" to \"" << args[3].c_str()
  752. << "\" are different.\n";
  753. return 1;
  754. }
  755. return 0;
  756. }
  757. // Echo string
  758. else if (args[1] == "echo" )
  759. {
  760. unsigned int cc;
  761. const char* space = "";
  762. for ( cc = 2; cc < args.size(); cc ++ )
  763. {
  764. std::cout << space << args[cc];
  765. space = " ";
  766. }
  767. std::cout << std::endl;
  768. return 0;
  769. }
  770. // Remove file
  771. else if (args[1] == "remove" && args.size() > 2)
  772. {
  773. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  774. {
  775. if(args[cc] != "-f")
  776. {
  777. if(args[cc] == "\\-f")
  778. {
  779. args[cc] = "-f";
  780. }
  781. cmSystemTools::RemoveFile(args[cc].c_str());
  782. }
  783. }
  784. return 0;
  785. }
  786. // Clock command
  787. else if (args[1] == "time" && args.size() > 2)
  788. {
  789. std::string command = args[2];
  790. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  791. {
  792. command += " ";
  793. command += args[cc];
  794. }
  795. clock_t clock_start, clock_finish;
  796. time_t time_start, time_finish;
  797. time(&time_start);
  798. clock_start = clock();
  799. cmSystemTools::RunSingleCommand(command.c_str());
  800. clock_finish = clock();
  801. time(&time_finish);
  802. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  803. std::cout << "Elapsed time: "
  804. << (long)(time_finish - time_start) << " s. (time)"
  805. << ", "
  806. << (double)(clock_finish - clock_start) / clocks_per_sec
  807. << " s. (clock)"
  808. << "\n";
  809. return 0;
  810. }
  811. // Command to change directory and run a program.
  812. else if (args[1] == "chdir" && args.size() >= 4)
  813. {
  814. std::string directory = args[2];
  815. if(!cmSystemTools::FileExists(directory.c_str()))
  816. {
  817. cmSystemTools::Error("Directory does not exist for chdir command: ",
  818. args[2].c_str());
  819. return 0;
  820. }
  821. std::string command = "\"";
  822. command += args[3];
  823. command += "\"";
  824. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  825. {
  826. command += " \"";
  827. command += args[cc];
  828. command += "\"";
  829. }
  830. int retval = 0;
  831. int timeout = 0;
  832. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  833. directory.c_str(), true, timeout) )
  834. {
  835. return retval;
  836. }
  837. return 1;
  838. }
  839. // Command to create a symbolic link. Fails on platforms not
  840. // supporting them.
  841. else if (args[1] == "create_symlink" && args.size() == 4)
  842. {
  843. return cmSystemTools::CreateSymlink(args[2].c_str(),
  844. args[3].c_str())? 0:1;
  845. }
  846. // Internal CMake shared library support.
  847. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  848. {
  849. int result = 0;
  850. std::string realName = args[2];
  851. std::string soName = args[3];
  852. std::string name = args[4];
  853. if(soName != realName)
  854. {
  855. std::string fname = cmSystemTools::GetFilenameName(realName);
  856. if(cmSystemTools::FileExists(soName.c_str()))
  857. {
  858. cmSystemTools::RemoveFile(soName.c_str());
  859. }
  860. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  861. {
  862. result = 1;
  863. }
  864. }
  865. if(name != soName)
  866. {
  867. std::string fname = cmSystemTools::GetFilenameName(soName);
  868. if(cmSystemTools::FileExists(soName.c_str()))
  869. {
  870. cmSystemTools::RemoveFile(name.c_str());
  871. }
  872. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  873. {
  874. result = 1;
  875. }
  876. }
  877. return result;
  878. }
  879. // Internal CMake versioned executable support.
  880. else if (args[1] == "cmake_symlink_executable" && args.size() == 4)
  881. {
  882. int result = 0;
  883. std::string realName = args[2];
  884. std::string name = args[3];
  885. if(name != realName)
  886. {
  887. std::string fname = cmSystemTools::GetFilenameName(realName);
  888. if(cmSystemTools::FileExists(realName.c_str()))
  889. {
  890. cmSystemTools::RemoveFile(name.c_str());
  891. }
  892. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  893. {
  894. result = 1;
  895. }
  896. }
  897. return result;
  898. }
  899. // Internal CMake dependency scanning support.
  900. else if (args[1] == "cmake_depends" && args.size() >= 6)
  901. {
  902. cmake cm;
  903. cmGlobalGenerator *ggd = cm.CreateGlobalGenerator(args[2].c_str());
  904. if (ggd)
  905. {
  906. ggd->SetCMakeInstance(&cm);
  907. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  908. lgd->SetGlobalGenerator(ggd);
  909. return lgd->ScanDependencies(args)? 0 : 2;
  910. }
  911. return 1;
  912. }
  913. // Tar files
  914. else if (args[1] == "tar" && args.size() > 3)
  915. {
  916. std::string flags = args[2];
  917. std::string outFile = args[3];
  918. std::vector<cmStdString> files;
  919. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  920. {
  921. files.push_back(args[cc]);
  922. }
  923. bool gzip = false;
  924. bool verbose = false;
  925. if ( flags.find_first_of('z') != flags.npos )
  926. {
  927. gzip = true;
  928. }
  929. if ( flags.find_first_of('v') != flags.npos )
  930. {
  931. verbose = true;
  932. }
  933. if ( flags.find_first_of('t') != flags.npos )
  934. {
  935. if ( !cmSystemTools::ListTar(outFile.c_str(), files, gzip, verbose) )
  936. {
  937. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  938. return 1;
  939. }
  940. }
  941. else if ( flags.find_first_of('c') != flags.npos )
  942. {
  943. if ( !cmSystemTools::CreateTar(outFile.c_str(), files, gzip, verbose) )
  944. {
  945. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  946. return 1;
  947. }
  948. }
  949. else if ( flags.find_first_of('x') != flags.npos )
  950. {
  951. if ( !cmSystemTools::ExtractTar(outFile.c_str(), files, gzip, verbose) )
  952. {
  953. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  954. return 1;
  955. }
  956. }
  957. return 0;
  958. }
  959. #if defined(CMAKE_BUILD_WITH_CMAKE)
  960. // Internal CMake Fortran module support.
  961. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  962. {
  963. return cmDependsFortran::CopyModule(args)? 0 : 1;
  964. }
  965. #endif
  966. #if defined(_WIN32) && !defined(__CYGWIN__)
  967. // Write registry value
  968. else if (args[1] == "write_regv" && args.size() > 3)
  969. {
  970. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  971. args[3].c_str()) ? 0 : 1;
  972. }
  973. // Delete registry value
  974. else if (args[1] == "delete_regv" && args.size() > 2)
  975. {
  976. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  977. }
  978. // Remove file
  979. else if (args[1] == "comspec" && args.size() > 2)
  980. {
  981. unsigned int cc;
  982. std::string command = args[2];
  983. for ( cc = 3; cc < args.size(); cc ++ )
  984. {
  985. command += " " + args[cc];
  986. }
  987. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  988. }
  989. #endif
  990. }
  991. ::CMakeCommandUsage(args[0].c_str());
  992. return 1;
  993. }
  994. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  995. {
  996. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  997. i != m_Generators.end(); ++i)
  998. {
  999. names.push_back(i->first);
  1000. }
  1001. }
  1002. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  1003. {
  1004. RegisteredGeneratorsMap::const_iterator i = m_Generators.find(name);
  1005. if(i != m_Generators.end())
  1006. {
  1007. cmGlobalGenerator* generator = (i->second)();
  1008. generator->SetCMakeInstance(this);
  1009. return generator;
  1010. }
  1011. else
  1012. {
  1013. return 0;
  1014. }
  1015. }
  1016. void cmake::SetHomeDirectory(const char* dir)
  1017. {
  1018. m_cmHomeDirectory = dir;
  1019. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  1020. }
  1021. void cmake::SetHomeOutputDirectory(const char* lib)
  1022. {
  1023. m_HomeOutputDirectory = lib;
  1024. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  1025. }
  1026. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  1027. {
  1028. // delete the old generator
  1029. if (m_GlobalGenerator)
  1030. {
  1031. delete m_GlobalGenerator;
  1032. // restore the original environment variables CXX and CC
  1033. // Restor CC
  1034. std::string env = "CC=";
  1035. if(m_CCEnvironment.size())
  1036. {
  1037. env += m_CCEnvironment;
  1038. }
  1039. cmSystemTools::PutEnv(env.c_str());
  1040. env = "CXX=";
  1041. if(m_CXXEnvironment.size())
  1042. {
  1043. env += m_CXXEnvironment;
  1044. }
  1045. cmSystemTools::PutEnv(env.c_str());
  1046. }
  1047. // set the new
  1048. m_GlobalGenerator = gg;
  1049. // set the global flag for unix style paths on cmSystemTools as
  1050. // soon as the generator is set. This allows gmake to be used
  1051. // on windows.
  1052. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  1053. // Save the environment variables CXX and CC
  1054. const char* cxx = getenv("CXX");
  1055. const char* cc = getenv("CC");
  1056. if(cxx)
  1057. {
  1058. m_CXXEnvironment = cxx;
  1059. }
  1060. else
  1061. {
  1062. m_CXXEnvironment = "";
  1063. }
  1064. if(cc)
  1065. {
  1066. m_CCEnvironment = cc;
  1067. }
  1068. else
  1069. {
  1070. m_CCEnvironment = "";
  1071. }
  1072. // set the cmake instance just to be sure
  1073. gg->SetCMakeInstance(this);
  1074. }
  1075. int cmake::DoPreConfigureChecks()
  1076. {
  1077. // Make sure the Start directory contains a CMakeLists.txt file.
  1078. std::string srcList = this->GetHomeDirectory();
  1079. srcList += "/CMakeLists.txt";
  1080. if(!cmSystemTools::FileExists(srcList.c_str()))
  1081. {
  1082. cmOStringStream err;
  1083. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1084. {
  1085. err << "The source directory \"" << this->GetHomeDirectory()
  1086. << "\" does not appear to contain CMakeLists.txt.\n";
  1087. }
  1088. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1089. {
  1090. err << "The source directory \"" << this->GetHomeDirectory()
  1091. << "\" is a file, not a directory.\n";
  1092. }
  1093. else
  1094. {
  1095. err << "The source directory \"" << this->GetHomeDirectory()
  1096. << "\" does not exist.\n";
  1097. }
  1098. err << "Specify --help for usage, or press the help button on the CMake GUI.";
  1099. cmSystemTools::Error(err.str().c_str());
  1100. return -2;
  1101. }
  1102. // do a sanity check on some values
  1103. if(m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1104. {
  1105. std::string cacheStart =
  1106. m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1107. cacheStart += "/CMakeLists.txt";
  1108. std::string currentStart = this->GetHomeDirectory();
  1109. currentStart += "/CMakeLists.txt";
  1110. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1111. {
  1112. std::string message = "The source \"";
  1113. message += currentStart;
  1114. message += "\" does not match the source \"";
  1115. message += cacheStart;
  1116. message += "\" used to generate cache. ";
  1117. message += "Re-run cmake with a different source directory.";
  1118. cmSystemTools::Error(message.c_str());
  1119. return -2;
  1120. }
  1121. }
  1122. else
  1123. {
  1124. return 0;
  1125. }
  1126. return 1;
  1127. }
  1128. int cmake::Configure()
  1129. {
  1130. // Construct right now our path conversion table before it's too late:
  1131. this->UpdateConversionPathTable();
  1132. this->CleanupCommandsAndMacros();
  1133. int res = 0;
  1134. if ( !m_ScriptMode )
  1135. {
  1136. res = this->DoPreConfigureChecks();
  1137. }
  1138. if ( res < 0 )
  1139. {
  1140. return -2;
  1141. }
  1142. if ( !res )
  1143. {
  1144. m_CacheManager->AddCacheEntry("CMAKE_HOME_DIRECTORY",
  1145. this->GetHomeDirectory(),
  1146. "Start directory with the top level CMakeLists.txt file for this project",
  1147. cmCacheManager::INTERNAL);
  1148. }
  1149. // set the default BACKWARDS compatibility to the current version
  1150. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1151. {
  1152. char ver[256];
  1153. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1154. cmMakefile::GetMinorVersion());
  1155. this->m_CacheManager->AddCacheEntry
  1156. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1157. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1158. cmCacheManager::STRING);
  1159. }
  1160. // no generator specified on the command line
  1161. if(!m_GlobalGenerator)
  1162. {
  1163. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1164. if(genName)
  1165. {
  1166. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  1167. }
  1168. if(m_GlobalGenerator)
  1169. {
  1170. // set the global flag for unix style paths on cmSystemTools as
  1171. // soon as the generator is set. This allows gmake to be used
  1172. // on windows.
  1173. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  1174. }
  1175. else
  1176. {
  1177. #if defined(__BORLANDC__) && defined(_WIN32)
  1178. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1179. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1180. std::string installedCompiler;
  1181. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  1182. cmSystemTools::ExpandRegistryValues(mp);
  1183. if (!(mp == "/registry"))
  1184. {
  1185. installedCompiler = "Visual Studio 8 2005";
  1186. }
  1187. else
  1188. {
  1189. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.1;InstallDir]";
  1190. cmSystemTools::ExpandRegistryValues(mp);
  1191. if (!(mp == "/registry"))
  1192. {
  1193. installedCompiler = "Visual Studio 7 .NET 2003";
  1194. }
  1195. else
  1196. {
  1197. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.0;InstallDir]";
  1198. cmSystemTools::ExpandRegistryValues(mp);
  1199. if (!(mp == "/registry"))
  1200. {
  1201. installedCompiler = "Visual Studio 7";
  1202. }
  1203. else
  1204. {
  1205. installedCompiler = "Visual Studio 6";
  1206. }
  1207. }
  1208. }
  1209. cmGlobalGenerator* gen = this->CreateGlobalGenerator(installedCompiler.c_str());
  1210. if(!gen)
  1211. {
  1212. gen = new cmGlobalNMakeMakefileGenerator;
  1213. }
  1214. this->SetGlobalGenerator(gen);
  1215. #else
  1216. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1217. #endif
  1218. }
  1219. if(!m_GlobalGenerator)
  1220. {
  1221. cmSystemTools::Error("Could not create generator");
  1222. return -1;
  1223. }
  1224. }
  1225. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1226. if(genName)
  1227. {
  1228. if(strcmp(m_GlobalGenerator->GetName(), genName) != 0)
  1229. {
  1230. std::string message = "Error: generator : ";
  1231. message += m_GlobalGenerator->GetName();
  1232. message += "\nDoes not match the generator used previously: ";
  1233. message += genName;
  1234. message +=
  1235. "\nEither remove the CMakeCache.txt file or choose a different"
  1236. " binary directory.";
  1237. cmSystemTools::Error(message.c_str());
  1238. return -2;
  1239. }
  1240. }
  1241. if(!m_CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1242. {
  1243. m_CacheManager->AddCacheEntry("CMAKE_GENERATOR", m_GlobalGenerator->GetName(),
  1244. "Name of generator.",
  1245. cmCacheManager::INTERNAL);
  1246. }
  1247. // reset any system configuration information, except for when we are
  1248. // InTryCompile. With TryCompile the system info is taken from the parent's
  1249. // info to save time
  1250. if (!m_InTryCompile)
  1251. {
  1252. m_GlobalGenerator->ClearEnabledLanguages();
  1253. }
  1254. this->CleanupWrittenFiles();
  1255. // Truncate log files
  1256. if (!m_InTryCompile)
  1257. {
  1258. this->TruncateOutputLog("CMakeOutput.log");
  1259. this->TruncateOutputLog("CMakeError.log");
  1260. }
  1261. // actually do the configure
  1262. m_GlobalGenerator->Configure();
  1263. // Before saving the cache
  1264. // if the project did not define one of the entries below, add them now
  1265. // so users can edit the values in the cache:
  1266. // LIBRARY_OUTPUT_PATH
  1267. // EXECUTABLE_OUTPUT_PATH
  1268. if(!m_CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1269. {
  1270. m_CacheManager->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  1271. "Single output directory for building all libraries.",
  1272. cmCacheManager::PATH);
  1273. }
  1274. if(!m_CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1275. {
  1276. m_CacheManager->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  1277. "Single output directory for building all executables.",
  1278. cmCacheManager::PATH);
  1279. }
  1280. if(!m_CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1281. {
  1282. m_CacheManager->AddCacheEntry("CMAKE_USE_RELATIVE_PATHS", false,
  1283. "If true, cmake will use relative paths in makefiles and projects.");
  1284. cmCacheManager::CacheIterator it =
  1285. m_CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1286. if ( !it.PropertyExists("ADVANCED") )
  1287. {
  1288. it.SetProperty("ADVANCED", "1");
  1289. }
  1290. }
  1291. if(cmSystemTools::GetFatalErrorOccured() &&
  1292. (!this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1293. cmSystemTools::IsOff(this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1294. {
  1295. // We must have a bad generator selection. Wipe the cache entry so the
  1296. // user can select another.
  1297. m_CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1298. }
  1299. // only save the cache if there were no fatal errors
  1300. if ( !m_ScriptMode && !cmSystemTools::GetFatalErrorOccured() )
  1301. {
  1302. this->m_CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1303. }
  1304. if ( !m_GraphVizFile.empty() )
  1305. {
  1306. std::cout << "Generate graphviz: " << m_GraphVizFile << std::endl;
  1307. this->GenerateGraphViz(m_GraphVizFile.c_str());
  1308. }
  1309. if(cmSystemTools::GetErrorOccuredFlag())
  1310. {
  1311. return -1;
  1312. }
  1313. return 0;
  1314. }
  1315. bool cmake::CacheVersionMatches()
  1316. {
  1317. const char* majv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1318. const char* minv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1319. const char* relv = m_CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1320. bool cacheSameCMake = false;
  1321. if(majv &&
  1322. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  1323. && minv &&
  1324. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  1325. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  1326. {
  1327. cacheSameCMake = true;
  1328. }
  1329. return cacheSameCMake;
  1330. }
  1331. void cmake::PreLoadCMakeFiles()
  1332. {
  1333. std::string pre_load = this->GetHomeDirectory();
  1334. if ( pre_load.size() > 0 )
  1335. {
  1336. pre_load += "/PreLoad.cmake";
  1337. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1338. {
  1339. this->ReadListFile(pre_load.c_str());
  1340. }
  1341. }
  1342. pre_load = this->GetHomeOutputDirectory();
  1343. if ( pre_load.size() > 0 )
  1344. {
  1345. pre_load += "/PreLoad.cmake";
  1346. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1347. {
  1348. this->ReadListFile(pre_load.c_str());
  1349. }
  1350. }
  1351. }
  1352. // handle a command line invocation
  1353. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1354. {
  1355. // Process the arguments
  1356. this->SetArgs(args);
  1357. if(cmSystemTools::GetErrorOccuredFlag())
  1358. {
  1359. CMakeCommandUsage(args[0].c_str());
  1360. return -1;
  1361. }
  1362. // set the cmake command
  1363. m_CMakeCommand = args[0];
  1364. if ( !m_ScriptMode )
  1365. {
  1366. // load the cache
  1367. if(this->LoadCache() < 0)
  1368. {
  1369. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1370. return -1;
  1371. }
  1372. }
  1373. else
  1374. {
  1375. this->AddCMakePaths(m_CMakeCommand.c_str());
  1376. }
  1377. // Add any cache args
  1378. if ( !this->SetCacheArgs(args) )
  1379. {
  1380. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1381. return -1;
  1382. }
  1383. // In script mode we terminate after running the script.
  1384. if(m_ScriptMode)
  1385. {
  1386. if(cmSystemTools::GetErrorOccuredFlag())
  1387. {
  1388. return -1;
  1389. }
  1390. else
  1391. {
  1392. return 0;
  1393. }
  1394. }
  1395. this->PreLoadCMakeFiles();
  1396. std::string systemFile = this->GetHomeOutputDirectory();
  1397. systemFile += "/CMakeSystem.cmake";
  1398. if ( noconfigure )
  1399. {
  1400. return 0;
  1401. }
  1402. // now run the global generate
  1403. // Check the state of the build system to see if we need to regenerate.
  1404. if(!this->CheckBuildSystem())
  1405. {
  1406. return 0;
  1407. }
  1408. // If we are doing global generate, we better set start and start
  1409. // output directory to the root of the project.
  1410. std::string oldstartdir = this->GetStartDirectory();
  1411. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1412. this->SetStartDirectory(this->GetHomeDirectory());
  1413. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1414. int ret = this->Configure();
  1415. if (ret || m_ScriptMode)
  1416. {
  1417. return ret;
  1418. }
  1419. ret = this->Generate();
  1420. std::string message = "Build files have been written to: ";
  1421. message += this->GetHomeOutputDirectory();
  1422. this->UpdateProgress(message.c_str(), -1);
  1423. if(ret)
  1424. {
  1425. return ret;
  1426. }
  1427. this->SetStartDirectory(oldstartdir.c_str());
  1428. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1429. return ret;
  1430. }
  1431. int cmake::Generate()
  1432. {
  1433. if(!m_GlobalGenerator)
  1434. {
  1435. return -1;
  1436. }
  1437. m_GlobalGenerator->Generate();
  1438. if(cmSystemTools::GetErrorOccuredFlag())
  1439. {
  1440. return -1;
  1441. }
  1442. return 0;
  1443. }
  1444. unsigned int cmake::GetMajorVersion()
  1445. {
  1446. return cmMakefile::GetMajorVersion();
  1447. }
  1448. unsigned int cmake::GetMinorVersion()
  1449. {
  1450. return cmMakefile::GetMinorVersion();
  1451. }
  1452. const char *cmake::GetReleaseVersion()
  1453. {
  1454. return cmMakefile::GetReleaseVersion();
  1455. }
  1456. void cmake::AddCacheEntry(const char* key, const char* value,
  1457. const char* helpString,
  1458. int type)
  1459. {
  1460. m_CacheManager->AddCacheEntry(key, value,
  1461. helpString,
  1462. cmCacheManager::CacheEntryType(type));
  1463. }
  1464. const char* cmake::GetCacheDefinition(const char* name) const
  1465. {
  1466. return m_CacheManager->GetCacheValue(name);
  1467. }
  1468. int cmake::DumpDocumentationToFile(std::ostream& f)
  1469. {
  1470. #ifdef CMAKE_BUILD_WITH_CMAKE
  1471. // Loop over all registered commands and print out documentation
  1472. const char *name;
  1473. const char *terse;
  1474. const char *full;
  1475. char tmp[1024];
  1476. sprintf(tmp,"Version %d.%d (%s)", cmake::GetMajorVersion(),
  1477. cmake::GetMinorVersion(), cmVersion::GetReleaseVersion().c_str());
  1478. f << "<html>\n";
  1479. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1480. f << "<ul>\n";
  1481. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  1482. j != m_Commands.end(); ++j)
  1483. {
  1484. name = (*j).second->GetName();
  1485. terse = (*j).second->GetTerseDocumentation();
  1486. full = (*j).second->GetFullDocumentation();
  1487. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1488. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1489. }
  1490. f << "</ul></html>\n";
  1491. #else
  1492. (void)f;
  1493. #endif
  1494. return 1;
  1495. }
  1496. void cmake::AddDefaultCommands()
  1497. {
  1498. std::list<cmCommand*> commands;
  1499. GetBootstrapCommands(commands);
  1500. GetPredefinedCommands(commands);
  1501. for(std::list<cmCommand*>::iterator i = commands.begin();
  1502. i != commands.end(); ++i)
  1503. {
  1504. this->AddCommand(*i);
  1505. }
  1506. }
  1507. void cmake::AddDefaultGenerators()
  1508. {
  1509. #if defined(_WIN32) && !defined(__CYGWIN__)
  1510. # if !defined(CMAKE_BOOT_MINGW)
  1511. m_Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1512. &cmGlobalVisualStudio6Generator::New;
  1513. m_Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1514. &cmGlobalVisualStudio7Generator::New;
  1515. m_Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1516. &cmGlobalVisualStudio71Generator::New;
  1517. m_Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  1518. &cmGlobalVisualStudio8Generator::New;
  1519. m_Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1520. &cmGlobalBorlandMakefileGenerator::New;
  1521. m_Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1522. &cmGlobalNMakeMakefileGenerator::New;
  1523. m_Generators[cmGlobalWatcomWMakeGenerator::GetActualName()] =
  1524. &cmGlobalWatcomWMakeGenerator::New;
  1525. # endif
  1526. m_Generators[cmGlobalMSYSMakefileGenerator::GetActualName()] =
  1527. &cmGlobalMSYSMakefileGenerator::New;
  1528. m_Generators[cmGlobalMinGWMakefileGenerator::GetActualName()] =
  1529. &cmGlobalMinGWMakefileGenerator::New;
  1530. #endif
  1531. m_Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
  1532. &cmGlobalUnixMakefileGenerator3::New;
  1533. #ifdef CMAKE_USE_XCODE
  1534. m_Generators[cmGlobalXCodeGenerator::GetActualName()] =
  1535. &cmGlobalXCodeGenerator::New;
  1536. #endif
  1537. #ifdef CMAKE_USE_KDEVELOP
  1538. m_Generators[cmGlobalKdevelopGenerator::GetActualName()] =
  1539. &cmGlobalKdevelopGenerator::New;
  1540. #endif
  1541. }
  1542. int cmake::LoadCache()
  1543. {
  1544. // could we not read the cache
  1545. if (!m_CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  1546. {
  1547. // if it does exist, but isn;t readable then warn the user
  1548. std::string cacheFile = this->GetHomeOutputDirectory();
  1549. cacheFile += "/CMakeCache.txt";
  1550. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1551. {
  1552. cmSystemTools::Error("There is a CMakeCache.txt file for the current binary tree but cmake does not have permission to read it. Please check the permissions of the directory you are trying to run CMake on.");
  1553. return -1;
  1554. }
  1555. }
  1556. if (m_CMakeCommand.size() < 2)
  1557. {
  1558. cmSystemTools::Error("cmake command was not specified prior to loading the cache in cmake.cxx");
  1559. return -1;
  1560. }
  1561. // setup CMAKE_ROOT and CMAKE_COMMAND
  1562. if(!this->AddCMakePaths(m_CMakeCommand.c_str()))
  1563. {
  1564. return -3;
  1565. }
  1566. // set the default BACKWARDS compatibility to the current version
  1567. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1568. {
  1569. char ver[256];
  1570. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1571. cmMakefile::GetMinorVersion());
  1572. this->m_CacheManager->AddCacheEntry
  1573. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1574. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1575. cmCacheManager::STRING);
  1576. }
  1577. return 0;
  1578. }
  1579. void cmake::SetProgressCallback(ProgressCallback f, void *cd)
  1580. {
  1581. m_ProgressCallback = f;
  1582. m_ProgressCallbackClientData = cd;
  1583. }
  1584. void cmake::UpdateProgress(const char *msg, float prog)
  1585. {
  1586. if(m_ProgressCallback && !m_InTryCompile)
  1587. {
  1588. (*m_ProgressCallback)(msg, prog, m_ProgressCallbackClientData);
  1589. return;
  1590. }
  1591. }
  1592. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v) const
  1593. {
  1594. for(RegisteredCommandsMap::const_iterator j = m_Commands.begin();
  1595. j != m_Commands.end(); ++j)
  1596. {
  1597. cmDocumentationEntry e =
  1598. {
  1599. (*j).second->GetName(),
  1600. (*j).second->GetTerseDocumentation(),
  1601. (*j).second->GetFullDocumentation()
  1602. };
  1603. v.push_back(e);
  1604. }
  1605. cmDocumentationEntry empty = {0,0,0};
  1606. v.push_back(empty);
  1607. }
  1608. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1609. {
  1610. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  1611. i != m_Generators.end(); ++i)
  1612. {
  1613. cmDocumentationEntry e;
  1614. cmGlobalGenerator* generator = (i->second)();
  1615. generator->GetDocumentation(e);
  1616. delete generator;
  1617. v.push_back(e);
  1618. }
  1619. cmDocumentationEntry empty = {0,0,0};
  1620. v.push_back(empty);
  1621. }
  1622. void cmake::AddWrittenFile(const char* file)
  1623. {
  1624. m_WrittenFiles.insert(file);
  1625. }
  1626. bool cmake::HasWrittenFile(const char* file)
  1627. {
  1628. return m_WrittenFiles.find(file) != m_WrittenFiles.end();
  1629. }
  1630. void cmake::CleanupWrittenFiles()
  1631. {
  1632. m_WrittenFiles.clear();
  1633. }
  1634. void cmake::UpdateConversionPathTable()
  1635. {
  1636. // Update the path conversion table with any specified file:
  1637. const char* tablepath =
  1638. m_CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1639. if(tablepath)
  1640. {
  1641. std::ifstream table( tablepath );
  1642. if(!table)
  1643. {
  1644. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath, ". CMake can not open file.");
  1645. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1646. }
  1647. else
  1648. {
  1649. std::string a, b;
  1650. while(!table.eof())
  1651. {
  1652. // two entries per line
  1653. table >> a; table >> b;
  1654. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1655. }
  1656. }
  1657. }
  1658. }
  1659. //----------------------------------------------------------------------------
  1660. int cmake::CheckBuildSystem()
  1661. {
  1662. // This method will check the integrity of the build system if the
  1663. // option was given on the command line. It reads the given file to
  1664. // determine whether CMake should rerun. If it does rerun then the
  1665. // generation step will check the integrity of dependencies. If it
  1666. // does not then we need to check the integrity here.
  1667. // If no file is provided for the check, we have to rerun.
  1668. if(m_CheckBuildSystem.size() == 0)
  1669. {
  1670. return 1;
  1671. }
  1672. // If the file provided does not exist, we have to rerun.
  1673. if(!cmSystemTools::FileExists(m_CheckBuildSystem.c_str()))
  1674. {
  1675. return 1;
  1676. }
  1677. // Read the rerun check file and use it to decide whether to do the
  1678. // global generate.
  1679. cmake cm;
  1680. cmGlobalGenerator gg;
  1681. gg.SetCMakeInstance(&cm);
  1682. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1683. lg->SetGlobalGenerator(&gg);
  1684. cmMakefile* mf = lg->GetMakefile();
  1685. if(!mf->ReadListFile(0, m_CheckBuildSystem.c_str()) ||
  1686. cmSystemTools::GetErrorOccuredFlag())
  1687. {
  1688. // There was an error reading the file. Just rerun.
  1689. return 1;
  1690. }
  1691. // Get the set of dependencies and outputs.
  1692. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1693. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1694. if(!dependsStr || !outputsStr)
  1695. {
  1696. // Not enough information was provided to do the test. Just rerun.
  1697. return 1;
  1698. }
  1699. std::vector<std::string> depends;
  1700. std::vector<std::string> outputs;
  1701. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1702. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1703. // If any output is older than any dependency then rerun.
  1704. for(std::vector<std::string>::iterator dep = depends.begin();
  1705. dep != depends.end(); ++dep)
  1706. {
  1707. for(std::vector<std::string>::iterator out = outputs.begin();
  1708. out != outputs.end(); ++out)
  1709. {
  1710. int result = 0;
  1711. if(!m_FileComparison->FileTimeCompare(out->c_str(),
  1712. dep->c_str(), &result) ||
  1713. result < 0)
  1714. {
  1715. return 1;
  1716. }
  1717. }
  1718. }
  1719. // We do not need to rerun CMake. Check dependency integrity. Use
  1720. // the make system's VERBOSE environment variable to enable verbose
  1721. // output.
  1722. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  1723. // compute depends based on the generator specified
  1724. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1725. if (!genName || genName[0] == '\0')
  1726. {
  1727. genName = "Unix Makefiles";
  1728. }
  1729. cmGlobalGenerator *ggd = this->CreateGlobalGenerator(genName);
  1730. if (ggd)
  1731. {
  1732. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1733. lgd->SetGlobalGenerator(ggd);
  1734. lgd->CheckDependencies(mf, verbose, m_ClearBuildSystem);
  1735. }
  1736. // No need to rerun.
  1737. return 0;
  1738. }
  1739. //----------------------------------------------------------------------------
  1740. void cmake::TruncateOutputLog(const char* fname)
  1741. {
  1742. std::string fullPath = this->GetHomeOutputDirectory();
  1743. fullPath += "/";
  1744. fullPath += fname;
  1745. struct stat st;
  1746. if ( ::stat(fullPath.c_str(), &st) )
  1747. {
  1748. return;
  1749. }
  1750. if ( !m_CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  1751. {
  1752. cmSystemTools::RemoveFile(fullPath.c_str());
  1753. return;
  1754. }
  1755. size_t fsize = st.st_size;
  1756. const size_t maxFileSize = 50 * 1024;
  1757. if ( fsize < maxFileSize )
  1758. {
  1759. //TODO: truncate file
  1760. return;
  1761. }
  1762. }
  1763. inline std::string removeQuotes(const std::string& s)
  1764. {
  1765. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1766. {
  1767. return s.substr(1, s.size()-2);
  1768. }
  1769. return s;
  1770. }
  1771. const char* cmake::GetCTestCommand()
  1772. {
  1773. if ( !m_CTestCommand.empty() )
  1774. {
  1775. return m_CTestCommand.c_str();
  1776. }
  1777. cmMakefile* mf = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  1778. #ifdef CMAKE_BUILD_WITH_CMAKE
  1779. m_CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1780. m_CTestCommand = removeQuotes(m_CTestCommand);
  1781. m_CTestCommand = cmSystemTools::GetFilenamePath(m_CTestCommand.c_str());
  1782. m_CTestCommand += "/";
  1783. m_CTestCommand += "ctest";
  1784. m_CTestCommand += cmSystemTools::GetExecutableExtension();
  1785. if(!cmSystemTools::FileExists(m_CTestCommand.c_str()))
  1786. {
  1787. m_CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1788. m_CTestCommand = cmSystemTools::GetFilenamePath(m_CTestCommand.c_str());
  1789. m_CTestCommand += "/Debug/";
  1790. m_CTestCommand += "ctest";
  1791. m_CTestCommand += cmSystemTools::GetExecutableExtension();
  1792. }
  1793. if(!cmSystemTools::FileExists(m_CTestCommand.c_str()))
  1794. {
  1795. m_CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1796. m_CTestCommand = cmSystemTools::GetFilenamePath(m_CTestCommand.c_str());
  1797. m_CTestCommand += "/Release/";
  1798. m_CTestCommand += "ctest";
  1799. m_CTestCommand += cmSystemTools::GetExecutableExtension();
  1800. }
  1801. #else
  1802. // Only for bootstrap
  1803. m_CTestCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  1804. m_CTestCommand += "/ctest";
  1805. m_CTestCommand += cmSystemTools::GetExecutableExtension();
  1806. #endif
  1807. if ( m_CTestCommand.empty() )
  1808. {
  1809. cmSystemTools::Error("Cannot find the CTest executable");
  1810. m_CTestCommand = "CTEST-COMMAND-NOT-FOUND";
  1811. }
  1812. return m_CTestCommand.c_str();
  1813. }
  1814. const char* cmake::GetCPackCommand()
  1815. {
  1816. if ( !m_CPackCommand.empty() )
  1817. {
  1818. return m_CPackCommand.c_str();
  1819. }
  1820. cmMakefile* mf = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  1821. #ifdef CMAKE_BUILD_WITH_CMAKE
  1822. m_CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1823. m_CPackCommand = removeQuotes(m_CPackCommand);
  1824. m_CPackCommand = cmSystemTools::GetFilenamePath(m_CPackCommand.c_str());
  1825. m_CPackCommand += "/";
  1826. m_CPackCommand += "cpack";
  1827. m_CPackCommand += cmSystemTools::GetExecutableExtension();
  1828. if(!cmSystemTools::FileExists(m_CPackCommand.c_str()))
  1829. {
  1830. m_CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1831. m_CPackCommand = cmSystemTools::GetFilenamePath(m_CPackCommand.c_str());
  1832. m_CPackCommand += "/Debug/";
  1833. m_CPackCommand += "cpack";
  1834. m_CPackCommand += cmSystemTools::GetExecutableExtension();
  1835. }
  1836. if(!cmSystemTools::FileExists(m_CPackCommand.c_str()))
  1837. {
  1838. m_CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1839. m_CPackCommand = cmSystemTools::GetFilenamePath(m_CPackCommand.c_str());
  1840. m_CPackCommand += "/Release/";
  1841. m_CPackCommand += "cpack";
  1842. m_CPackCommand += cmSystemTools::GetExecutableExtension();
  1843. }
  1844. if (!cmSystemTools::FileExists(m_CPackCommand.c_str()))
  1845. {
  1846. cmSystemTools::Error("Cannot find the CPack executable");
  1847. m_CPackCommand = "CPACK-COMMAND-NOT-FOUND";
  1848. }
  1849. #else
  1850. // Only for bootstrap
  1851. m_CPackCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  1852. m_CPackCommand += "/cpack";
  1853. m_CPackCommand += cmSystemTools::GetExecutableExtension();
  1854. #endif
  1855. return m_CPackCommand.c_str();
  1856. }
  1857. void cmake::GenerateGraphViz(const char* fileName)
  1858. {
  1859. cmGeneratedFileStream str(fileName);
  1860. if ( !str )
  1861. {
  1862. return;
  1863. }
  1864. str << "digraph G {" << std::endl;
  1865. str << "node [" << std::endl;
  1866. str << " fontsize = \"12\"" << std::endl;
  1867. str << "];" << std::endl;
  1868. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  1869. std::vector<cmLocalGenerator*> localGenerators;
  1870. gg->GetLocalGenerators(localGenerators);
  1871. std::vector<cmLocalGenerator*>::iterator lit;
  1872. // for target deps
  1873. // 1 - cmake target
  1874. // 2 - external target
  1875. // 0 - no deps
  1876. std::map<cmStdString, int> targetDeps;
  1877. std::map<cmStdString, cmTarget*> targetPtrs;
  1878. std::map<cmStdString, cmStdString> targetNamesNodes;
  1879. char tgtName[100];
  1880. int cnt = 0;
  1881. // First pass get the list of all cmake targets
  1882. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  1883. {
  1884. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  1885. cmTargets::iterator tit;
  1886. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  1887. {
  1888. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  1889. sprintf(tgtName, "node%d", cnt++);
  1890. targetNamesNodes[tit->first.c_str()] = tgtName;
  1891. targetPtrs[tit->first.c_str()] = &tit->second;
  1892. //str << " \"" << tgtName << "\" [ label=\"" << tit->first.c_str() << "\" shape=\"box\"];" << std::endl;
  1893. }
  1894. }
  1895. // Ok, now find all the stuff we link to that is not in cmake
  1896. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  1897. {
  1898. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  1899. cmTargets::iterator tit;
  1900. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  1901. {
  1902. const cmTarget::LinkLibraries* ll = &(tit->second.GetOriginalLinkLibraries());
  1903. cmTarget::LinkLibraries::const_iterator llit;
  1904. if ( ll->size() > 0 )
  1905. {
  1906. targetDeps[tit->first.c_str()] = 1;
  1907. }
  1908. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  1909. {
  1910. const char* libName = llit->first.c_str();
  1911. std::map<cmStdString, cmStdString>::iterator tarIt = targetNamesNodes.find(libName);
  1912. if ( tarIt == targetNamesNodes.end() )
  1913. {
  1914. sprintf(tgtName, "node%d", cnt++);
  1915. targetDeps[libName] = 2;
  1916. targetNamesNodes[libName] = tgtName;
  1917. //str << " \"" << tgtName << "\" [ label=\"" << libName << "\" shape=\"ellipse\"];" << std::endl;
  1918. }
  1919. else
  1920. {
  1921. std::map<cmStdString, int>::iterator depIt = targetDeps.find(libName);
  1922. if ( depIt == targetDeps.end() )
  1923. {
  1924. targetDeps[libName] = 1;
  1925. }
  1926. }
  1927. }
  1928. }
  1929. }
  1930. // Write out nodes
  1931. std::map<cmStdString, int>::iterator depIt;
  1932. for ( depIt = targetDeps.begin(); depIt != targetDeps.end(); ++ depIt )
  1933. {
  1934. const char* newTargetName = depIt->first.c_str();
  1935. std::map<cmStdString, cmStdString>::iterator tarIt = targetNamesNodes.find(newTargetName);
  1936. if ( tarIt == targetNamesNodes.end() )
  1937. {
  1938. // We should not be here.
  1939. std::cout << "Cannot find library: " << newTargetName << " even though it was added in the previous pass" << std::endl;
  1940. abort();
  1941. }
  1942. str << " \"" << tarIt->second.c_str() << "\" [ label=\"" << newTargetName << "\" shape=\"";
  1943. if ( depIt->second == 1 )
  1944. {
  1945. std::map<cmStdString, cmTarget*>::iterator tarTypeIt= targetPtrs.find(newTargetName);
  1946. if ( tarTypeIt == targetPtrs.end() )
  1947. {
  1948. // We should not be here.
  1949. std::cout << "Cannot find library: " << newTargetName << " even though it was added in the previous pass" << std::endl;
  1950. abort();
  1951. }
  1952. cmTarget* tg = tarTypeIt->second;
  1953. switch ( tg->GetType() )
  1954. {
  1955. case cmTarget::EXECUTABLE:
  1956. str << "house";
  1957. break;
  1958. case cmTarget::STATIC_LIBRARY:
  1959. str << "diamond";
  1960. break;
  1961. case cmTarget::SHARED_LIBRARY:
  1962. str << "polygon";
  1963. break;
  1964. case cmTarget::MODULE_LIBRARY:
  1965. str << "octagon";
  1966. break;
  1967. default:
  1968. str << "box";
  1969. }
  1970. }
  1971. else
  1972. {
  1973. str << "ellipse";
  1974. }
  1975. str << "\"];" << std::endl;
  1976. }
  1977. // Now generate the connectivity
  1978. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  1979. {
  1980. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  1981. cmTargets::iterator tit;
  1982. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  1983. {
  1984. std::map<cmStdString, int>::iterator dependIt = targetDeps.find(tit->first.c_str());
  1985. if ( dependIt == targetDeps.end() )
  1986. {
  1987. continue;
  1988. }
  1989. std::map<cmStdString, cmStdString>::iterator cmakeTarIt = targetNamesNodes.find(tit->first.c_str());
  1990. const cmTarget::LinkLibraries* ll = &(tit->second.GetOriginalLinkLibraries());
  1991. cmTarget::LinkLibraries::const_iterator llit;
  1992. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  1993. {
  1994. const char* libName = llit->first.c_str();
  1995. std::map<cmStdString, cmStdString>::iterator tarIt = targetNamesNodes.find(libName);
  1996. if ( tarIt == targetNamesNodes.end() )
  1997. {
  1998. // We should not be here.
  1999. std::cout << "Cannot find library: " << libName << " even though it was added in the previous pass" << std::endl;
  2000. abort();
  2001. }
  2002. str << " \"" << cmakeTarIt->second.c_str() << "\" -> \"" << tarIt->second.c_str() << "\"" << std::endl;
  2003. }
  2004. }
  2005. }
  2006. // TODO: Use dotted or something for external libraries
  2007. //str << " \"node0\":f4 -> \"node12\"[color=\"#0000ff\" style=dotted]" << std::endl;
  2008. //
  2009. str << "}" << std::endl;
  2010. }