cmake.cxx 47 KB

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