cmake.cxx 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  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. #if defined(CMAKE_BUILD_WITH_CMAKE)
  21. # include "cmVariableWatch.h"
  22. # include "cmVersion.h"
  23. #endif
  24. #if defined(CMAKE_BUILD_WITH_CMAKE)
  25. #include "cmLocalUnixMakefileGenerator2.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. // include the generator
  35. #if defined(_WIN32) && !defined(__CYGWIN__)
  36. # include "cmGlobalVisualStudio6Generator.h"
  37. # if !defined(__MINGW32__)
  38. # include "cmGlobalVisualStudio7Generator.h"
  39. # include "cmGlobalVisualStudio71Generator.h"
  40. # include "cmGlobalVisualStudio8Generator.h"
  41. # endif
  42. # include "cmGlobalBorlandMakefileGenerator.h"
  43. # include "cmGlobalNMakeMakefileGenerator.h"
  44. # include "cmWin32ProcessExecution.h"
  45. #else
  46. #endif
  47. #include "cmGlobalUnixMakefileGenerator.h"
  48. #ifdef CMAKE_USE_KDEVELOP
  49. # include "cmGlobalKdevelopGenerator.h"
  50. #endif
  51. #include <stdlib.h> // required for atoi
  52. #ifdef __APPLE__
  53. # include <sys/types.h>
  54. # include <sys/time.h>
  55. # include <sys/resource.h>
  56. # if defined(CMAKE_BUILD_WITH_CMAKE)
  57. # include "cmGlobalCodeWarriorGenerator.h"
  58. # endif
  59. #endif
  60. #include <memory> // auto_ptr
  61. void cmNeedBackwardsCompatibility(const std::string& variable,
  62. int access_type, void* )
  63. {
  64. #ifdef CMAKE_BUILD_WITH_CMAKE
  65. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  66. {
  67. std::string message = "An attempt was made to access a variable: ";
  68. message += variable;
  69. message += " that has not been defined. Some variables were always defined "
  70. "by CMake in versions prior to 1.6. To fix this you might need to set the "
  71. "cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If you are "
  72. "writing a CMakeList file, (or have already set "
  73. "CMAKE_BACKWARDS_COMPATABILITY to 1.4 or less) then you probably need to "
  74. "include a CMake module to test for the feature this variable defines.";
  75. cmSystemTools::Error(message.c_str());
  76. }
  77. #else
  78. (void)variable;
  79. (void)access_type;
  80. #endif
  81. }
  82. cmake::cmake()
  83. {
  84. m_DebugTryCompile = false;
  85. #ifdef __APPLE__
  86. struct rlimit rlp;
  87. if(!getrlimit(RLIMIT_STACK, &rlp))
  88. {
  89. if(rlp.rlim_cur != rlp.rlim_max)
  90. {
  91. rlp.rlim_cur = rlp.rlim_max;
  92. setrlimit(RLIMIT_STACK, &rlp);
  93. }
  94. }
  95. #endif
  96. // If MAKEFLAGS are given in the environment, remove the environment
  97. // variable. This will prevent try-compile from succeeding when it
  98. // should fail (if "-i" is an option). We cannot simply test
  99. // whether "-i" is given and remove it because some make programs
  100. // encode the MAKEFLAGS variable in a strange way.
  101. if(getenv("MAKEFLAGS"))
  102. {
  103. cmSystemTools::PutEnv("MAKEFLAGS=");
  104. }
  105. m_Local = false;
  106. m_Verbose = false;
  107. m_InTryCompile = false;
  108. m_CacheManager = new cmCacheManager;
  109. m_GlobalGenerator = 0;
  110. m_ProgressCallback = 0;
  111. m_ProgressCallbackClientData = 0;
  112. m_ScriptMode = false;
  113. #ifdef CMAKE_BUILD_WITH_CMAKE
  114. m_VariableWatch = new cmVariableWatch;
  115. m_VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  116. cmNeedBackwardsCompatibility);
  117. m_VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  118. cmNeedBackwardsCompatibility);
  119. m_VariableWatch->AddWatch("CMAKE_X_LIBS",
  120. cmNeedBackwardsCompatibility);
  121. #endif
  122. this->AddDefaultGenerators();
  123. this->AddDefaultCommands();
  124. }
  125. cmake::~cmake()
  126. {
  127. delete m_CacheManager;
  128. if (m_GlobalGenerator)
  129. {
  130. delete m_GlobalGenerator;
  131. m_GlobalGenerator = 0;
  132. }
  133. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  134. j != m_Commands.end(); ++j)
  135. {
  136. delete (*j).second;
  137. }
  138. #ifdef CMAKE_BUILD_WITH_CMAKE
  139. delete m_VariableWatch;
  140. #endif
  141. }
  142. bool cmake::CommandExists(const char* name) const
  143. {
  144. return (m_Commands.find(name) != m_Commands.end());
  145. }
  146. cmCommand *cmake::GetCommand(const char *name)
  147. {
  148. cmCommand* rm = 0;
  149. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  150. if (pos != m_Commands.end())
  151. {
  152. rm = (*pos).second;
  153. }
  154. return rm;
  155. }
  156. void cmake::AddCommand(cmCommand* wg)
  157. {
  158. std::string name = wg->GetName();
  159. // if the command already exists, free the old one
  160. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  161. if (pos != m_Commands.end())
  162. {
  163. delete pos->second;
  164. m_Commands.erase(pos);
  165. }
  166. m_Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  167. }
  168. // Parse the args
  169. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  170. {
  171. for(unsigned int i=1; i < args.size(); ++i)
  172. {
  173. std::string arg = args[i];
  174. if(arg.find("-D",0) == 0)
  175. {
  176. std::string entry = arg.substr(2);
  177. std::string var, value;
  178. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  179. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  180. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  181. {
  182. this->m_CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  183. "No help, variable specified on the command line.",
  184. type);
  185. }
  186. else
  187. {
  188. std::cerr << "Parse error in command line argument: " << arg << "\n"
  189. << "Should be: VAR:type=value\n";
  190. cmSystemTools::Error("No cmake scrpt provided.");
  191. return false;
  192. }
  193. }
  194. else if(arg.find("-C",0) == 0)
  195. {
  196. std::string path = arg.substr(2);
  197. if ( path.size() == 0 )
  198. {
  199. cmSystemTools::Error("No initial cache file provided.");
  200. return false;
  201. }
  202. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  203. this->ReadListFile(path.c_str());
  204. }
  205. else if(arg.find("-P",0) == 0)
  206. {
  207. i++;
  208. std::string path = args[i];
  209. if ( path.size() == 0 )
  210. {
  211. cmSystemTools::Error("No cmake scrpt provided.");
  212. return false;
  213. }
  214. std::cerr << "Running cmake script file " << path.c_str() << "\n";
  215. this->ReadListFile(path.c_str());
  216. }
  217. }
  218. return true;
  219. }
  220. void cmake::ReadListFile(const char *path)
  221. {
  222. // if a generator was not yet created, temporarily create one
  223. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  224. bool created = false;
  225. // if a generator was not specified use a generic one
  226. if (!gg)
  227. {
  228. gg = new cmGlobalGenerator;
  229. gg->SetCMakeInstance(this);
  230. created = true;
  231. }
  232. // read in the list file to fill the cache
  233. if(path)
  234. {
  235. std::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  236. lg->SetGlobalGenerator(gg);
  237. if (!lg->GetMakefile()->ReadListFile(0, path))
  238. {
  239. std::cerr << "Error in reading cmake initial cache file:"
  240. << path << "\n";
  241. }
  242. }
  243. // free generic one if generated
  244. if (created)
  245. {
  246. delete gg;
  247. }
  248. }
  249. // Parse the args
  250. void cmake::SetArgs(const std::vector<std::string>& args)
  251. {
  252. m_Local = false;
  253. bool directoriesSet = false;
  254. for(unsigned int i=1; i < args.size(); ++i)
  255. {
  256. std::string arg = args[i];
  257. if(arg.find("-H",0) == 0)
  258. {
  259. directoriesSet = true;
  260. std::string path = arg.substr(2);
  261. path = cmSystemTools::CollapseFullPath(path.c_str());
  262. cmSystemTools::ConvertToUnixSlashes(path);
  263. this->SetHomeDirectory(path.c_str());
  264. }
  265. else if(arg.find("-S",0) == 0)
  266. {
  267. directoriesSet = true;
  268. m_Local = true;
  269. std::string path = arg.substr(2);
  270. path = cmSystemTools::CollapseFullPath(path.c_str());
  271. cmSystemTools::ConvertToUnixSlashes(path);
  272. this->SetStartDirectory(path.c_str());
  273. }
  274. else if(arg.find("-O",0) == 0)
  275. {
  276. directoriesSet = true;
  277. std::string path = arg.substr(2);
  278. path = cmSystemTools::CollapseFullPath(path.c_str());
  279. cmSystemTools::ConvertToUnixSlashes(path);
  280. this->SetStartOutputDirectory(path.c_str());
  281. }
  282. else if(arg.find("-B",0) == 0)
  283. {
  284. directoriesSet = true;
  285. std::string path = arg.substr(2);
  286. path = cmSystemTools::CollapseFullPath(path.c_str());
  287. cmSystemTools::ConvertToUnixSlashes(path);
  288. this->SetHomeOutputDirectory(path.c_str());
  289. }
  290. else if((i < args.size()-1) && (arg.find("--check-rerun",0) == 0))
  291. {
  292. m_CheckRerun = args[++i];
  293. }
  294. else if(arg.find("-V",0) == 0)
  295. {
  296. m_Verbose = true;
  297. }
  298. else if(arg.find("-D",0) == 0)
  299. {
  300. // skip for now
  301. }
  302. else if(arg.find("-C",0) == 0)
  303. {
  304. // skip for now
  305. }
  306. else if(arg.find("--script",0) == 0)
  307. {
  308. // skip for now
  309. }
  310. else if(arg.find("--debug-trycompile",0) == 0)
  311. {
  312. std::cout << "debug trycompile on\n";
  313. this->DebugTryCompileOn();
  314. }
  315. else if(arg.find("-G",0) == 0)
  316. {
  317. std::string value = arg.substr(2);
  318. cmGlobalGenerator* gen =
  319. this->CreateGlobalGenerator(value.c_str());
  320. if(!gen)
  321. {
  322. cmSystemTools::Error("Could not create named generator ",
  323. value.c_str());
  324. }
  325. else
  326. {
  327. this->SetGlobalGenerator(gen);
  328. }
  329. }
  330. // no option assume it is the path to the source
  331. else
  332. {
  333. directoriesSet = true;
  334. this->SetDirectoriesFromFile(arg.c_str());
  335. }
  336. }
  337. if(!directoriesSet)
  338. {
  339. this->SetHomeOutputDirectory
  340. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  341. this->SetStartOutputDirectory
  342. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  343. this->SetHomeDirectory
  344. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  345. this->SetStartDirectory
  346. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  347. }
  348. if (!m_Local)
  349. {
  350. this->SetStartDirectory(this->GetHomeDirectory());
  351. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  352. }
  353. }
  354. //----------------------------------------------------------------------------
  355. void cmake::SetDirectoriesFromFile(const char* arg)
  356. {
  357. // Check if the argument refers to a CMakeCache.txt or
  358. // CMakeLists.txt file.
  359. std::string listPath;
  360. std::string cachePath;
  361. bool argIsFile = false;
  362. if(cmSystemTools::FileIsDirectory(arg))
  363. {
  364. std::string path = cmSystemTools::CollapseFullPath(arg);
  365. cmSystemTools::ConvertToUnixSlashes(path);
  366. std::string cacheFile = path;
  367. cacheFile += "/CMakeCache.txt";
  368. std::string listFile = path;
  369. listFile += "/CMakeLists.txt";
  370. if(cmSystemTools::FileExists(cacheFile.c_str()))
  371. {
  372. cachePath = path;
  373. }
  374. if(cmSystemTools::FileExists(listFile.c_str()))
  375. {
  376. listPath = path;
  377. }
  378. }
  379. else if(cmSystemTools::FileExists(arg))
  380. {
  381. argIsFile = true;
  382. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  383. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  384. name = cmSystemTools::LowerCase(name);
  385. if(name == "cmakecache.txt")
  386. {
  387. cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  388. }
  389. else if(name == "cmakelists.txt")
  390. {
  391. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  392. }
  393. }
  394. else
  395. {
  396. // Specified file or directory does not exist. Try to set things
  397. // up to produce a meaningful error message.
  398. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  399. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  400. name = cmSystemTools::LowerCase(name);
  401. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  402. {
  403. argIsFile = true;
  404. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  405. }
  406. else
  407. {
  408. listPath = fullPath;
  409. }
  410. }
  411. // If there is a CMakeCache.txt file, use its settings.
  412. if(cachePath.length() > 0)
  413. {
  414. cmCacheManager* cachem = this->GetCacheManager();
  415. cmCacheManager::CacheIterator it = cachem->NewIterator();
  416. if(cachem->LoadCache(cachePath.c_str()) && it.Find("CMAKE_HOME_DIRECTORY"))
  417. {
  418. this->SetHomeOutputDirectory(cachePath.c_str());
  419. this->SetStartOutputDirectory(cachePath.c_str());
  420. this->SetHomeDirectory(it.GetValue());
  421. this->SetStartDirectory(it.GetValue());
  422. return;
  423. }
  424. }
  425. // If there is a CMakeLists.txt file, use it as the source tree.
  426. if(listPath.length() > 0)
  427. {
  428. this->SetHomeDirectory(listPath.c_str());
  429. this->SetStartDirectory(listPath.c_str());
  430. if(argIsFile)
  431. {
  432. // Source CMakeLists.txt file given. It was probably dropped
  433. // onto the executable in a GUI. Default to an in-source build.
  434. this->SetHomeOutputDirectory(listPath.c_str());
  435. this->SetStartOutputDirectory(listPath.c_str());
  436. }
  437. else
  438. {
  439. // Source directory given on command line. Use current working
  440. // directory as build tree.
  441. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  442. this->SetHomeOutputDirectory(cwd.c_str());
  443. this->SetStartOutputDirectory(cwd.c_str());
  444. }
  445. return;
  446. }
  447. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  448. // argument. Assume it is the path to the source tree, and use the
  449. // current working directory as the build tree.
  450. std::string full = cmSystemTools::CollapseFullPath(arg);
  451. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  452. this->SetHomeDirectory(full.c_str());
  453. this->SetStartDirectory(full.c_str());
  454. this->SetHomeOutputDirectory(cwd.c_str());
  455. this->SetStartOutputDirectory(cwd.c_str());
  456. }
  457. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the cache
  458. int cmake::AddCMakePaths(const char *arg0)
  459. {
  460. // Find our own executable.
  461. std::vector<cmStdString> failures;
  462. std::string cMakeSelf = arg0;
  463. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  464. failures.push_back(cMakeSelf);
  465. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  466. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  467. {
  468. #ifdef CMAKE_BUILD_DIR
  469. std::string intdir = ".";
  470. #ifdef CMAKE_INTDIR
  471. intdir = CMAKE_INTDIR;
  472. #endif
  473. cMakeSelf = CMAKE_BUILD_DIR;
  474. cMakeSelf += "/bin/";
  475. cMakeSelf += intdir;
  476. cMakeSelf += "/cmake";
  477. cMakeSelf += cmSystemTools::GetExecutableExtension();
  478. #endif
  479. }
  480. #ifdef CMAKE_PREFIX
  481. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  482. {
  483. failures.push_back(cMakeSelf);
  484. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  485. }
  486. #endif
  487. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  488. {
  489. failures.push_back(cMakeSelf);
  490. cmOStringStream msg;
  491. msg << "CMAKE can not find the command line program cmake.\n";
  492. msg << " argv[0] = \"" << arg0 << "\"\n";
  493. msg << " Attempted paths:\n";
  494. std::vector<cmStdString>::iterator i;
  495. for(i=failures.begin(); i != failures.end(); ++i)
  496. {
  497. msg << " \"" << i->c_str() << "\"\n";
  498. }
  499. cmSystemTools::Error(msg.str().c_str());
  500. return 0;
  501. }
  502. // Save the value in the cache
  503. this->m_CacheManager->AddCacheEntry
  504. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  505. cmCacheManager::INTERNAL);
  506. // Find and save the command to edit the cache
  507. std::string editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  508. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  509. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  510. {
  511. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  512. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  513. }
  514. std::string ctestCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  515. "/ctest" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  516. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  517. {
  518. this->m_CacheManager->AddCacheEntry
  519. ("CMAKE_CTEST_COMMAND", ctestCommand.c_str(),
  520. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  521. }
  522. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  523. {
  524. this->m_CacheManager->AddCacheEntry
  525. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  526. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  527. }
  528. // do CMAKE_ROOT, look for the environment variable first
  529. std::string cMakeRoot;
  530. std::string modules;
  531. if (getenv("CMAKE_ROOT"))
  532. {
  533. cMakeRoot = getenv("CMAKE_ROOT");
  534. modules = cMakeRoot + "/Modules/CMake.cmake";
  535. }
  536. if(!cmSystemTools::FileExists(modules.c_str()))
  537. {
  538. // next try exe/..
  539. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  540. std::string::size_type slashPos = cMakeRoot.rfind("/");
  541. if(slashPos != std::string::npos)
  542. {
  543. cMakeRoot = cMakeRoot.substr(0, slashPos);
  544. }
  545. // is there no Modules direcory there?
  546. modules = cMakeRoot + "/Modules/CMake.cmake";
  547. }
  548. if (!cmSystemTools::FileExists(modules.c_str()))
  549. {
  550. // try exe/../share/cmake
  551. cMakeRoot += CMAKE_DATA_DIR;
  552. modules = cMakeRoot + "/Modules/CMake.cmake";
  553. }
  554. #ifdef CMAKE_ROOT_DIR
  555. if (!cmSystemTools::FileExists(modules.c_str()))
  556. {
  557. // try compiled in root directory
  558. cMakeRoot = CMAKE_ROOT_DIR;
  559. modules = cMakeRoot + "/Modules/CMake.cmake";
  560. }
  561. #endif
  562. #ifdef CMAKE_PREFIX
  563. if (!cmSystemTools::FileExists(modules.c_str()))
  564. {
  565. // try compiled in install prefix
  566. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  567. modules = cMakeRoot + "/Modules/CMake.cmake";
  568. }
  569. #endif
  570. if (!cmSystemTools::FileExists(modules.c_str()))
  571. {
  572. // try
  573. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  574. cMakeRoot += CMAKE_DATA_DIR;
  575. modules = cMakeRoot + "/Modules/CMake.cmake";
  576. }
  577. if(!cmSystemTools::FileExists(modules.c_str()))
  578. {
  579. // next try exe
  580. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  581. // is there no Modules direcory there?
  582. modules = cMakeRoot + "/Modules/CMake.cmake";
  583. }
  584. if (!cmSystemTools::FileExists(modules.c_str()))
  585. {
  586. // couldn't find modules
  587. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  588. "CMake has most likely not been installed correctly.\n"
  589. "Modules directory not found in\n",
  590. cMakeRoot.c_str());
  591. return 0;
  592. }
  593. this->m_CacheManager->AddCacheEntry
  594. ("CMAKE_ROOT", cMakeRoot.c_str(),
  595. "Path to CMake installation.", cmCacheManager::INTERNAL);
  596. #ifdef _WIN32
  597. std::string comspec = "cmw9xcom.exe";
  598. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  599. #endif
  600. return 1;
  601. }
  602. void CMakeCommandUsage(const char* program)
  603. {
  604. cmOStringStream errorStream;
  605. #ifdef CMAKE_BUILD_WITH_CMAKE
  606. errorStream
  607. << "cmake version " << cmVersion::GetCMakeVersion() << "\n";
  608. #else
  609. errorStream
  610. << "cmake bootstrap\n";
  611. #endif
  612. errorStream
  613. << "Usage: " << program << " -E [command] [arguments ...]\n"
  614. << "Available commands: \n"
  615. << " chdir dir cmd [args]... - run command in a given directory\n"
  616. << " copy file destination - copy file to destination (either file or directory)\n"
  617. << " copy_if_different in-file out-file - copy file if input has changed\n"
  618. << " echo [string]... - displays arguments as text\n"
  619. << " remove file1 file2 ... - remove the file(s)\n"
  620. << " time command [args] ... - run command and return elapsed time\n";
  621. #if defined(_WIN32) && !defined(__CYGWIN__)
  622. errorStream
  623. << " write_regv key value - write registry value\n"
  624. << " delete_regv key - delete registry value\n"
  625. << " comspec - on windows 9x use this for RunCommand\n";
  626. #endif
  627. cmSystemTools::Error(errorStream.str().c_str());
  628. }
  629. int cmake::CMakeCommand(std::vector<std::string>& args)
  630. {
  631. if (args.size() > 1)
  632. {
  633. // Copy file
  634. if (args[1] == "copy" && args.size() == 4)
  635. {
  636. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  637. {
  638. std::cerr << "Error copying file \"" << args[2].c_str()
  639. << "\" to \"" << args[3].c_str() << "\".\n";
  640. return 1;
  641. }
  642. return 0;
  643. }
  644. // Copy file if different.
  645. if (args[1] == "copy_if_different" && args.size() == 4)
  646. {
  647. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(), args[3].c_str()))
  648. {
  649. std::cerr << "Error copying file (if different) from \""
  650. << args[2].c_str() << "\" to \"" << args[3].c_str()
  651. << "\".\n";
  652. return 1;
  653. }
  654. return 0;
  655. }
  656. // Echo string
  657. else if (args[1] == "echo" )
  658. {
  659. unsigned int cc;
  660. for ( cc = 2; cc < args.size(); cc ++ )
  661. {
  662. std::cout << args[cc] << " ";
  663. }
  664. std::cout << std::endl;
  665. return 0;
  666. }
  667. // Remove file
  668. else if (args[1] == "remove" && args.size() > 2)
  669. {
  670. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  671. {
  672. if(args[cc] != "-f")
  673. {
  674. if(args[cc] == "\\-f")
  675. {
  676. args[cc] = "-f";
  677. }
  678. cmSystemTools::RemoveFile(args[cc].c_str());
  679. }
  680. }
  681. return 0;
  682. }
  683. // Clock command
  684. else if (args[1] == "time" && args.size() > 2)
  685. {
  686. std::string command = args[2];
  687. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  688. {
  689. command += " ";
  690. command += args[cc];
  691. }
  692. clock_t clock_start, clock_finish;
  693. time_t time_start, time_finish;
  694. time(&time_start);
  695. clock_start = clock();
  696. cmSystemTools::RunSingleCommand(command.c_str());
  697. clock_finish = clock();
  698. time(&time_finish);
  699. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  700. std::cout << "Elapsed time: "
  701. << (long)(time_finish - time_start) << " s. (time)"
  702. << ", "
  703. << (double)(clock_finish - clock_start) / clocks_per_sec
  704. << " s. (clock)"
  705. << "\n";
  706. return 0;
  707. }
  708. // Clock command
  709. else if (args[1] == "chdir" && args.size() >= 4)
  710. {
  711. std::string directory = args[2];
  712. std::string command = "\"";
  713. command += args[3];
  714. command += "\"";
  715. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  716. {
  717. command += " \"";
  718. command += args[cc];
  719. command += "\"";
  720. }
  721. int retval = 0;
  722. int timeout = 0;
  723. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  724. directory.c_str(), true, timeout) )
  725. {
  726. return retval;
  727. }
  728. return 1;
  729. }
  730. // Internal CMake shared library support.
  731. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  732. {
  733. int result = 0;
  734. std::string realName = args[2];
  735. std::string soName = args[3];
  736. std::string name = args[4];
  737. if(soName != realName)
  738. {
  739. std::string fname = cmSystemTools::GetFilenameName(realName);
  740. if(cmSystemTools::FileExists(soName.c_str()))
  741. {
  742. cmSystemTools::RemoveFile(soName.c_str());
  743. }
  744. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  745. {
  746. result = 1;
  747. }
  748. }
  749. if(name != soName)
  750. {
  751. std::string fname = cmSystemTools::GetFilenameName(soName);
  752. if(cmSystemTools::FileExists(soName.c_str()))
  753. {
  754. cmSystemTools::RemoveFile(name.c_str());
  755. }
  756. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  757. {
  758. result = 1;
  759. }
  760. }
  761. return result;
  762. }
  763. #ifdef CMAKE_BUILD_WITH_CMAKE
  764. // Internal CMake dependency scanning support.
  765. else if (args[1] == "cmake_depends" && args.size() >= 5)
  766. {
  767. return cmLocalUnixMakefileGenerator2::ScanDependencies(args)? 0 : 1;
  768. }
  769. #endif
  770. #if defined(_WIN32) && !defined(__CYGWIN__)
  771. // Write registry value
  772. else if (args[1] == "write_regv" && args.size() > 3)
  773. {
  774. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  775. args[3].c_str()) ? 0 : 1;
  776. }
  777. // Delete registry value
  778. else if (args[1] == "delete_regv" && args.size() > 2)
  779. {
  780. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  781. }
  782. // Remove file
  783. else if (args[1] == "comspec" && args.size() > 2)
  784. {
  785. unsigned int cc;
  786. std::string command = args[2];
  787. for ( cc = 3; cc < args.size(); cc ++ )
  788. {
  789. command += " " + args[cc];
  790. }
  791. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  792. }
  793. #endif
  794. }
  795. ::CMakeCommandUsage(args[0].c_str());
  796. return 1;
  797. }
  798. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  799. {
  800. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  801. i != m_Generators.end(); ++i)
  802. {
  803. names.push_back(i->first);
  804. }
  805. }
  806. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  807. {
  808. RegisteredGeneratorsMap::const_iterator i = m_Generators.find(name);
  809. if(i != m_Generators.end())
  810. {
  811. cmGlobalGenerator* generator = (i->second)();
  812. generator->SetCMakeInstance(this);
  813. return generator;
  814. }
  815. else
  816. {
  817. return 0;
  818. }
  819. }
  820. void cmake::SetHomeDirectory(const char* dir)
  821. {
  822. m_cmHomeDirectory = dir;
  823. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  824. }
  825. void cmake::SetHomeOutputDirectory(const char* lib)
  826. {
  827. m_HomeOutputDirectory = lib;
  828. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  829. }
  830. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  831. {
  832. // delete the old generator
  833. if (m_GlobalGenerator)
  834. {
  835. delete m_GlobalGenerator;
  836. // restore the original environment variables CXX and CC
  837. // Restor CC
  838. std::string env = "CC=";
  839. if(m_CCEnvironment.size())
  840. {
  841. env += m_CCEnvironment;
  842. }
  843. cmSystemTools::PutEnv(env.c_str());
  844. env = "CXX=";
  845. if(m_CXXEnvironment.size())
  846. {
  847. env += m_CXXEnvironment;
  848. }
  849. cmSystemTools::PutEnv(env.c_str());
  850. }
  851. // set the new
  852. m_GlobalGenerator = gg;
  853. // set the global flag for unix style paths on cmSystemTools as
  854. // soon as the generator is set. This allows gmake to be used
  855. // on windows.
  856. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  857. // Save the environment variables CXX and CC
  858. const char* cxx = getenv("CXX");
  859. const char* cc = getenv("CC");
  860. if(cxx)
  861. {
  862. m_CXXEnvironment = cxx;
  863. }
  864. else
  865. {
  866. m_CXXEnvironment = "";
  867. }
  868. if(cc)
  869. {
  870. m_CCEnvironment = cc;
  871. }
  872. else
  873. {
  874. m_CCEnvironment = "";
  875. }
  876. // set the cmake instance just to be sure
  877. gg->SetCMakeInstance(this);
  878. }
  879. int cmake::DoPreConfigureChecks()
  880. {
  881. // Make sure the Start directory contains a CMakeLists.txt file.
  882. std::string srcList = this->GetHomeDirectory();
  883. srcList += "/CMakeLists.txt";
  884. if(!cmSystemTools::FileExists(srcList.c_str()))
  885. {
  886. cmOStringStream err;
  887. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  888. {
  889. err << "The source directory \"" << this->GetHomeDirectory()
  890. << "\" does not appear to contain CMakeLists.txt.\n";
  891. }
  892. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  893. {
  894. err << "The source directory \"" << this->GetHomeDirectory()
  895. << "\" is a file, not a directory.\n";
  896. }
  897. else
  898. {
  899. err << "The source directory \"" << this->GetHomeDirectory()
  900. << "\" does not exist.\n";
  901. }
  902. err << "Specify --help for usage, or press the help button on the CMake GUI.";
  903. cmSystemTools::Error(err.str().c_str());
  904. return -2;
  905. }
  906. // do a sanity check on some values
  907. if(m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  908. {
  909. std::string cacheStart =
  910. m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  911. cacheStart += "/CMakeLists.txt";
  912. std::string currentStart = this->GetHomeDirectory();
  913. currentStart += "/CMakeLists.txt";
  914. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  915. {
  916. std::string message = "The source \"";
  917. message += currentStart;
  918. message += "\" does not match the source \"";
  919. message += cacheStart;
  920. message += "\" used to generate cache. ";
  921. message += "Re-run cmake with a different source directory.";
  922. cmSystemTools::Error(message.c_str());
  923. return -2;
  924. }
  925. }
  926. else
  927. {
  928. return 0;
  929. }
  930. return 1;
  931. }
  932. int cmake::Configure()
  933. {
  934. // Construct right now our path conversion table before it's too late:
  935. this->UpdateConversionPathTable();
  936. int res = 0;
  937. if ( !m_ScriptMode )
  938. {
  939. res = this->DoPreConfigureChecks();
  940. }
  941. if ( res < 0 )
  942. {
  943. return -2;
  944. }
  945. if ( !res )
  946. {
  947. m_CacheManager->AddCacheEntry("CMAKE_HOME_DIRECTORY",
  948. this->GetHomeDirectory(),
  949. "Start directory with the top level CMakeLists.txt file for this project",
  950. cmCacheManager::INTERNAL);
  951. }
  952. // set the default BACKWARDS compatibility to the current version
  953. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  954. {
  955. char ver[256];
  956. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  957. cmMakefile::GetMinorVersion());
  958. this->m_CacheManager->AddCacheEntry
  959. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  960. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  961. cmCacheManager::STRING);
  962. }
  963. // no generator specified on the command line
  964. if(!m_GlobalGenerator)
  965. {
  966. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  967. if(genName)
  968. {
  969. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  970. }
  971. if(m_GlobalGenerator)
  972. {
  973. // set the global flag for unix style paths on cmSystemTools as
  974. // soon as the generator is set. This allows gmake to be used
  975. // on windows.
  976. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  977. }
  978. else
  979. {
  980. #if defined(__BORLANDC__) && defined(_WIN32)
  981. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  982. #elif defined(_WIN32) && !defined(__CYGWIN__)
  983. std::string installedCompiler;
  984. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  985. cmSystemTools::ExpandRegistryValues(mp);
  986. if (!(mp == "/registry"))
  987. {
  988. installedCompiler = "Visual Studio 8 2005";
  989. }
  990. else
  991. {
  992. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.1;InstallDir]";
  993. cmSystemTools::ExpandRegistryValues(mp);
  994. if (!(mp == "/registry"))
  995. {
  996. installedCompiler = "Visual Studio 7 .NET 2003";
  997. }
  998. else
  999. {
  1000. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.0;InstallDir]";
  1001. cmSystemTools::ExpandRegistryValues(mp);
  1002. if (!(mp == "/registry"))
  1003. {
  1004. installedCompiler = "Visual Studio 7";
  1005. }
  1006. else
  1007. {
  1008. installedCompiler = "Visual Studio 6";
  1009. }
  1010. }
  1011. }
  1012. cmGlobalGenerator* gen = this->CreateGlobalGenerator(installedCompiler.c_str());
  1013. if(!gen)
  1014. {
  1015. gen = new cmGlobalNMakeMakefileGenerator;
  1016. }
  1017. this->SetGlobalGenerator(gen);
  1018. #else
  1019. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator);
  1020. #endif
  1021. }
  1022. if(!m_GlobalGenerator)
  1023. {
  1024. cmSystemTools::Error("Could not create generator");
  1025. return -1;
  1026. }
  1027. }
  1028. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1029. if(genName)
  1030. {
  1031. if(strcmp(m_GlobalGenerator->GetName(), genName) != 0)
  1032. {
  1033. std::string message = "Error: generator : ";
  1034. message += m_GlobalGenerator->GetName();
  1035. message += "\nDoes not match the generator used previously: ";
  1036. message += genName;
  1037. message +=
  1038. "\nEither remove the CMakeCache.txt file or choose a different"
  1039. " binary directory.";
  1040. cmSystemTools::Error(message.c_str());
  1041. return -2;
  1042. }
  1043. }
  1044. if(!m_CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1045. {
  1046. m_CacheManager->AddCacheEntry("CMAKE_GENERATOR", m_GlobalGenerator->GetName(),
  1047. "Name of generator.",
  1048. cmCacheManager::INTERNAL);
  1049. }
  1050. // reset any system configuration information, except for when we are
  1051. // InTryCompile. With TryCompile the system info is taken from the parent's
  1052. // info to save time
  1053. if (!m_InTryCompile)
  1054. {
  1055. m_GlobalGenerator->ClearEnabledLanguages();
  1056. }
  1057. this->CleanupWrittenFiles();
  1058. // actually do the configure
  1059. m_GlobalGenerator->Configure();
  1060. // Before saving the cache
  1061. // if the project did not define one of the entries below, add them now
  1062. // so users can edit the values in the cache:
  1063. // LIBRARY_OUTPUT_PATH
  1064. // EXECUTABLE_OUTPUT_PATH
  1065. if(!m_CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1066. {
  1067. m_CacheManager->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  1068. "Single output directory for building all libraries.",
  1069. cmCacheManager::PATH);
  1070. }
  1071. if(!m_CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1072. {
  1073. m_CacheManager->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  1074. "Single output directory for building all executables.",
  1075. cmCacheManager::PATH);
  1076. }
  1077. if(!m_CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1078. {
  1079. m_CacheManager->AddCacheEntry("CMAKE_USE_RELATIVE_PATHS", false,
  1080. "If true, cmake will use relative paths in makefiles and projects.");
  1081. cmCacheManager::CacheIterator it =
  1082. m_CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1083. if ( !it.PropertyExists("ADVANCED") )
  1084. {
  1085. it.SetProperty("ADVANCED", "1");
  1086. }
  1087. }
  1088. if(cmSystemTools::GetFatalErrorOccured() &&
  1089. (!this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1090. cmSystemTools::IsOff(this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1091. {
  1092. // We must have a bad generator selection. Wipe the cache entry so the
  1093. // user can select another.
  1094. m_CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1095. }
  1096. if ( !m_ScriptMode )
  1097. {
  1098. this->m_CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1099. }
  1100. if(cmSystemTools::GetErrorOccuredFlag())
  1101. {
  1102. return -1;
  1103. }
  1104. return 0;
  1105. }
  1106. bool cmake::CacheVersionMatches()
  1107. {
  1108. const char* majv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1109. const char* minv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1110. const char* relv = m_CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1111. bool cacheSameCMake = false;
  1112. if(majv &&
  1113. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  1114. && minv &&
  1115. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  1116. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  1117. {
  1118. cacheSameCMake = true;
  1119. }
  1120. return cacheSameCMake;
  1121. }
  1122. void cmake::PreLoadCMakeFiles()
  1123. {
  1124. std::string pre_load = this->GetHomeDirectory();
  1125. if ( pre_load.size() > 0 )
  1126. {
  1127. pre_load += "/PreLoad.cmake";
  1128. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1129. {
  1130. this->ReadListFile(pre_load.c_str());
  1131. }
  1132. }
  1133. pre_load = this->GetHomeOutputDirectory();
  1134. if ( pre_load.size() > 0 )
  1135. {
  1136. pre_load += "/PreLoad.cmake";
  1137. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1138. {
  1139. this->ReadListFile(pre_load.c_str());
  1140. }
  1141. }
  1142. }
  1143. // handle a command line invocation
  1144. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1145. {
  1146. // Process the arguments
  1147. this->SetArgs(args);
  1148. // set the cmake command
  1149. m_CMakeCommand = args[0];
  1150. if ( !m_ScriptMode )
  1151. {
  1152. // load the cache
  1153. if(this->LoadCache() < 0)
  1154. {
  1155. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1156. return -1;
  1157. }
  1158. }
  1159. // Add any cache args
  1160. if ( !this->SetCacheArgs(args) )
  1161. {
  1162. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1163. return -1;
  1164. }
  1165. this->PreLoadCMakeFiles();
  1166. std::string systemFile = this->GetHomeOutputDirectory();
  1167. systemFile += "/CMakeSystem.cmake";
  1168. if ( noconfigure )
  1169. {
  1170. return 0;
  1171. }
  1172. int ret = 0;
  1173. // if not local or the cmake version has changed since the last run
  1174. // of cmake, or CMakeSystem.cmake file is not in the root binary
  1175. // directory, run a global generate
  1176. if(m_ScriptMode || !m_Local || !this->CacheVersionMatches() ||
  1177. !cmSystemTools::FileExists(systemFile.c_str()) )
  1178. {
  1179. // Check whether we should really do a generate.
  1180. if(!this->CheckRerun())
  1181. {
  1182. return 0;
  1183. }
  1184. // If we are doing global generate, we better set start and start
  1185. // output directory to the root of the project.
  1186. std::string oldstartdir = this->GetStartDirectory();
  1187. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1188. this->SetStartDirectory(this->GetHomeDirectory());
  1189. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1190. bool saveLocalFlag = m_Local;
  1191. m_Local = false;
  1192. ret = this->Configure();
  1193. if (ret || m_ScriptMode)
  1194. {
  1195. return ret;
  1196. }
  1197. ret = this->Generate();
  1198. std::string message = "Build files have been written to: ";
  1199. message += this->GetHomeOutputDirectory();
  1200. this->UpdateProgress(message.c_str(), -1);
  1201. if(ret)
  1202. {
  1203. return ret;
  1204. }
  1205. m_Local = saveLocalFlag;
  1206. this->SetStartDirectory(oldstartdir.c_str());
  1207. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1208. }
  1209. // if we are local do the local thing
  1210. if (m_Local)
  1211. {
  1212. ret = this->LocalGenerate();
  1213. }
  1214. return ret;
  1215. }
  1216. int cmake::Generate()
  1217. {
  1218. if(!m_GlobalGenerator)
  1219. {
  1220. return -1;
  1221. }
  1222. m_GlobalGenerator->Generate();
  1223. if(cmSystemTools::GetErrorOccuredFlag())
  1224. {
  1225. return -1;
  1226. }
  1227. return 0;
  1228. }
  1229. int cmake::LocalGenerate()
  1230. {
  1231. // Read in the cache
  1232. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1233. // create the generator based on the cache if it isn't already there
  1234. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1235. if(genName)
  1236. {
  1237. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  1238. // set the global flag for unix style paths on cmSystemTools as
  1239. // soon as the generator is set. This allows gmake to be used
  1240. // on windows.
  1241. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  1242. }
  1243. else
  1244. {
  1245. cmSystemTools::Error("Could local Generate called without the GENERATOR being specified in the CMakeCache");
  1246. return -1;
  1247. }
  1248. // do the local generate
  1249. m_GlobalGenerator->LocalGenerate();
  1250. if(cmSystemTools::GetErrorOccuredFlag())
  1251. {
  1252. return -1;
  1253. }
  1254. return 0;
  1255. }
  1256. unsigned int cmake::GetMajorVersion()
  1257. {
  1258. return cmMakefile::GetMajorVersion();
  1259. }
  1260. unsigned int cmake::GetMinorVersion()
  1261. {
  1262. return cmMakefile::GetMinorVersion();
  1263. }
  1264. const char *cmake::GetReleaseVersion()
  1265. {
  1266. return cmMakefile::GetReleaseVersion();
  1267. }
  1268. void cmake::AddCacheEntry(const char* key, const char* value,
  1269. const char* helpString,
  1270. int type)
  1271. {
  1272. m_CacheManager->AddCacheEntry(key, value,
  1273. helpString,
  1274. cmCacheManager::CacheEntryType(type));
  1275. }
  1276. const char* cmake::GetCacheDefinition(const char* name) const
  1277. {
  1278. return m_CacheManager->GetCacheValue(name);
  1279. }
  1280. int cmake::DumpDocumentationToFile(std::ostream& f)
  1281. {
  1282. #ifdef CMAKE_BUILD_WITH_CMAKE
  1283. // Loop over all registered commands and print out documentation
  1284. const char *name;
  1285. const char *terse;
  1286. const char *full;
  1287. char tmp[1024];
  1288. sprintf(tmp,"Version %d.%d (%s)", cmake::GetMajorVersion(),
  1289. cmake::GetMinorVersion(), cmVersion::GetReleaseVersion().c_str());
  1290. f << "<html>\n";
  1291. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1292. f << "<ul>\n";
  1293. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  1294. j != m_Commands.end(); ++j)
  1295. {
  1296. name = (*j).second->GetName();
  1297. terse = (*j).second->GetTerseDocumentation();
  1298. full = (*j).second->GetFullDocumentation();
  1299. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1300. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1301. }
  1302. f << "</ul></html>\n";
  1303. #else
  1304. (void)f;
  1305. #endif
  1306. return 1;
  1307. }
  1308. void cmake::AddDefaultCommands()
  1309. {
  1310. std::list<cmCommand*> commands;
  1311. GetPredefinedCommands(commands);
  1312. for(std::list<cmCommand*>::iterator i = commands.begin();
  1313. i != commands.end(); ++i)
  1314. {
  1315. this->AddCommand(*i);
  1316. }
  1317. }
  1318. void cmake::AddDefaultGenerators()
  1319. {
  1320. #if defined(_WIN32) && !defined(__CYGWIN__)
  1321. m_Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1322. &cmGlobalVisualStudio6Generator::New;
  1323. #if !defined(__MINGW32__)
  1324. m_Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1325. &cmGlobalVisualStudio7Generator::New;
  1326. m_Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1327. &cmGlobalVisualStudio71Generator::New;
  1328. m_Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  1329. &cmGlobalVisualStudio8Generator::New;
  1330. #endif
  1331. m_Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1332. &cmGlobalBorlandMakefileGenerator::New;
  1333. m_Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1334. &cmGlobalNMakeMakefileGenerator::New;
  1335. #else
  1336. # if defined(__APPLE__) && defined(CMAKE_BUILD_WITH_CMAKE)
  1337. m_Generators[cmGlobalCodeWarriorGenerator::GetActualName()] =
  1338. &cmGlobalCodeWarriorGenerator::New;
  1339. # endif
  1340. #endif
  1341. m_Generators[cmGlobalUnixMakefileGenerator::GetActualName()] =
  1342. &cmGlobalUnixMakefileGenerator::New;
  1343. #ifdef CMAKE_USE_KDEVELOP
  1344. m_Generators[cmGlobalKdevelopGenerator::GetActualName()] =
  1345. &cmGlobalKdevelopGenerator::New;
  1346. #endif
  1347. }
  1348. int cmake::LoadCache()
  1349. {
  1350. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1351. if (m_CMakeCommand.size() < 2)
  1352. {
  1353. cmSystemTools::Error("cmake command was not specified prior to loading the cache in cmake.cxx");
  1354. return -1;
  1355. }
  1356. // setup CMAKE_ROOT and CMAKE_COMMAND
  1357. if(!this->AddCMakePaths(m_CMakeCommand.c_str()))
  1358. {
  1359. return -3;
  1360. }
  1361. // set the default BACKWARDS compatibility to the current version
  1362. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1363. {
  1364. char ver[256];
  1365. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1366. cmMakefile::GetMinorVersion());
  1367. this->m_CacheManager->AddCacheEntry
  1368. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1369. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1370. cmCacheManager::STRING);
  1371. }
  1372. return 0;
  1373. }
  1374. void cmake::SetProgressCallback(ProgressCallback f, void *cd)
  1375. {
  1376. m_ProgressCallback = f;
  1377. m_ProgressCallbackClientData = cd;
  1378. }
  1379. void cmake::UpdateProgress(const char *msg, float prog)
  1380. {
  1381. if(m_ProgressCallback && !m_InTryCompile)
  1382. {
  1383. (*m_ProgressCallback)(msg, prog, m_ProgressCallbackClientData);
  1384. return;
  1385. }
  1386. }
  1387. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v) const
  1388. {
  1389. for(RegisteredCommandsMap::const_iterator j = m_Commands.begin();
  1390. j != m_Commands.end(); ++j)
  1391. {
  1392. cmDocumentationEntry e =
  1393. {
  1394. (*j).second->GetName(),
  1395. (*j).second->GetTerseDocumentation(),
  1396. (*j).second->GetFullDocumentation()
  1397. };
  1398. v.push_back(e);
  1399. }
  1400. cmDocumentationEntry empty = {0,0,0};
  1401. v.push_back(empty);
  1402. }
  1403. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1404. {
  1405. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  1406. i != m_Generators.end(); ++i)
  1407. {
  1408. cmDocumentationEntry e;
  1409. cmGlobalGenerator* generator = (i->second)();
  1410. generator->GetDocumentation(e);
  1411. delete generator;
  1412. v.push_back(e);
  1413. }
  1414. cmDocumentationEntry empty = {0,0,0};
  1415. v.push_back(empty);
  1416. }
  1417. void cmake::AddWrittenFile(const char* file)
  1418. {
  1419. m_WrittenFiles.insert(file);
  1420. }
  1421. bool cmake::HasWrittenFile(const char* file)
  1422. {
  1423. return m_WrittenFiles.find(file) != m_WrittenFiles.end();
  1424. }
  1425. void cmake::CleanupWrittenFiles()
  1426. {
  1427. m_WrittenFiles.clear();
  1428. }
  1429. void cmake::UpdateConversionPathTable()
  1430. {
  1431. // Update the path conversion table with any specified file:
  1432. const char* tablepath =
  1433. m_CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1434. if(tablepath)
  1435. {
  1436. std::ifstream table( tablepath );
  1437. if(!table)
  1438. {
  1439. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath, ". CMake can not open file.");
  1440. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1441. }
  1442. else
  1443. {
  1444. std::string a, b;
  1445. while(!table.eof())
  1446. {
  1447. // two entries per line
  1448. table >> a; table >> b;
  1449. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1450. }
  1451. }
  1452. }
  1453. }
  1454. int cmake::CheckRerun()
  1455. {
  1456. // If no file is provided for the check, we have to rerun.
  1457. if(m_CheckRerun.size() == 0)
  1458. {
  1459. return 1;
  1460. }
  1461. // If the file provided does not exist, we have to rerun.
  1462. if(!cmSystemTools::FileExists(m_CheckRerun.c_str()))
  1463. {
  1464. return 1;
  1465. }
  1466. // Read the rerun check file and use it to decide whether to do the
  1467. // global generate.
  1468. cmake cm;
  1469. cmGlobalGenerator gg;
  1470. gg.SetCMakeInstance(&cm);
  1471. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1472. lg->SetGlobalGenerator(&gg);
  1473. cmMakefile* mf = lg->GetMakefile();
  1474. if(!mf->ReadListFile(0, m_CheckRerun.c_str()) ||
  1475. cmSystemTools::GetErrorOccuredFlag())
  1476. {
  1477. // There was an error reading the file. Just rerun.
  1478. return 1;
  1479. }
  1480. // Get the set of dependencies and outputs.
  1481. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1482. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1483. if(!dependsStr || !outputsStr)
  1484. {
  1485. // Not enough information was provided to do the test. Just rerun.
  1486. return 1;
  1487. }
  1488. std::vector<std::string> depends;
  1489. std::vector<std::string> outputs;
  1490. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1491. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1492. // If any output is older than any dependency then rerun.
  1493. for(std::vector<std::string>::iterator dep = depends.begin();
  1494. dep != depends.end(); ++dep)
  1495. {
  1496. for(std::vector<std::string>::iterator out = outputs.begin();
  1497. out != outputs.end(); ++out)
  1498. {
  1499. int result = 0;
  1500. if(!cmSystemTools::FileTimeCompare(out->c_str(), dep->c_str(), &result) ||
  1501. result < 0)
  1502. {
  1503. return 1;
  1504. }
  1505. }
  1506. }
  1507. // No need to rerun.
  1508. return 0;
  1509. }