cmake.cxx 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662
  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("-P",0) == 0)
  306. {
  307. // skip for now
  308. i++;
  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. << " copy_directory source destination - copy directory 'source' content to directory 'destination'\n"
  619. << " echo [string]... - displays arguments as text\n"
  620. << " remove file1 file2 ... - remove the file(s)\n"
  621. << " time command [args] ... - run command and return elapsed time\n";
  622. #if defined(_WIN32) && !defined(__CYGWIN__)
  623. errorStream
  624. << " write_regv key value - write registry value\n"
  625. << " delete_regv key - delete registry value\n"
  626. << " comspec - on windows 9x use this for RunCommand\n";
  627. #endif
  628. cmSystemTools::Error(errorStream.str().c_str());
  629. }
  630. int cmake::CMakeCommand(std::vector<std::string>& args)
  631. {
  632. if (args.size() > 1)
  633. {
  634. // Copy file
  635. if (args[1] == "copy" && args.size() == 4)
  636. {
  637. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  638. {
  639. std::cerr << "Error copying file \"" << args[2].c_str()
  640. << "\" to \"" << args[3].c_str() << "\".\n";
  641. return 1;
  642. }
  643. return 0;
  644. }
  645. // Copy file if different.
  646. if (args[1] == "copy_if_different" && args.size() == 4)
  647. {
  648. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(), args[3].c_str()))
  649. {
  650. std::cerr << "Error copying file (if different) from \""
  651. << args[2].c_str() << "\" to \"" << args[3].c_str()
  652. << "\".\n";
  653. return 1;
  654. }
  655. return 0;
  656. }
  657. // Copy directory content
  658. if (args[1] == "copy_directory" && args.size() == 4)
  659. {
  660. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  661. {
  662. std::cerr << "Error copying directory from \""
  663. << args[2].c_str() << "\" to \"" << args[3].c_str()
  664. << "\".\n";
  665. return 1;
  666. }
  667. return 0;
  668. }
  669. // Echo string
  670. else if (args[1] == "echo" )
  671. {
  672. unsigned int cc;
  673. for ( cc = 2; cc < args.size(); cc ++ )
  674. {
  675. std::cout << args[cc] << " ";
  676. }
  677. std::cout << std::endl;
  678. return 0;
  679. }
  680. // Remove file
  681. else if (args[1] == "remove" && args.size() > 2)
  682. {
  683. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  684. {
  685. if(args[cc] != "-f")
  686. {
  687. if(args[cc] == "\\-f")
  688. {
  689. args[cc] = "-f";
  690. }
  691. cmSystemTools::RemoveFile(args[cc].c_str());
  692. }
  693. }
  694. return 0;
  695. }
  696. // Clock command
  697. else if (args[1] == "time" && args.size() > 2)
  698. {
  699. std::string command = args[2];
  700. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  701. {
  702. command += " ";
  703. command += args[cc];
  704. }
  705. clock_t clock_start, clock_finish;
  706. time_t time_start, time_finish;
  707. time(&time_start);
  708. clock_start = clock();
  709. cmSystemTools::RunSingleCommand(command.c_str());
  710. clock_finish = clock();
  711. time(&time_finish);
  712. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  713. std::cout << "Elapsed time: "
  714. << (long)(time_finish - time_start) << " s. (time)"
  715. << ", "
  716. << (double)(clock_finish - clock_start) / clocks_per_sec
  717. << " s. (clock)"
  718. << "\n";
  719. return 0;
  720. }
  721. // Clock command
  722. else if (args[1] == "chdir" && args.size() >= 4)
  723. {
  724. std::string directory = args[2];
  725. std::string command = "\"";
  726. command += args[3];
  727. command += "\"";
  728. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  729. {
  730. command += " \"";
  731. command += args[cc];
  732. command += "\"";
  733. }
  734. int retval = 0;
  735. int timeout = 0;
  736. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  737. directory.c_str(), true, timeout) )
  738. {
  739. return retval;
  740. }
  741. return 1;
  742. }
  743. // Internal CMake shared library support.
  744. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  745. {
  746. int result = 0;
  747. std::string realName = args[2];
  748. std::string soName = args[3];
  749. std::string name = args[4];
  750. if(soName != realName)
  751. {
  752. std::string fname = cmSystemTools::GetFilenameName(realName);
  753. if(cmSystemTools::FileExists(soName.c_str()))
  754. {
  755. cmSystemTools::RemoveFile(soName.c_str());
  756. }
  757. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  758. {
  759. result = 1;
  760. }
  761. }
  762. if(name != soName)
  763. {
  764. std::string fname = cmSystemTools::GetFilenameName(soName);
  765. if(cmSystemTools::FileExists(soName.c_str()))
  766. {
  767. cmSystemTools::RemoveFile(name.c_str());
  768. }
  769. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  770. {
  771. result = 1;
  772. }
  773. }
  774. return result;
  775. }
  776. #ifdef CMAKE_BUILD_WITH_CMAKE
  777. // Internal CMake dependency scanning support.
  778. else if (args[1] == "cmake_depends" && args.size() >= 5)
  779. {
  780. return cmLocalUnixMakefileGenerator2::ScanDependencies(args)? 0 : 1;
  781. }
  782. #endif
  783. #if defined(_WIN32) && !defined(__CYGWIN__)
  784. // Write registry value
  785. else if (args[1] == "write_regv" && args.size() > 3)
  786. {
  787. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  788. args[3].c_str()) ? 0 : 1;
  789. }
  790. // Delete registry value
  791. else if (args[1] == "delete_regv" && args.size() > 2)
  792. {
  793. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  794. }
  795. // Remove file
  796. else if (args[1] == "comspec" && args.size() > 2)
  797. {
  798. unsigned int cc;
  799. std::string command = args[2];
  800. for ( cc = 3; cc < args.size(); cc ++ )
  801. {
  802. command += " " + args[cc];
  803. }
  804. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  805. }
  806. #endif
  807. }
  808. ::CMakeCommandUsage(args[0].c_str());
  809. return 1;
  810. }
  811. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  812. {
  813. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  814. i != m_Generators.end(); ++i)
  815. {
  816. names.push_back(i->first);
  817. }
  818. }
  819. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  820. {
  821. RegisteredGeneratorsMap::const_iterator i = m_Generators.find(name);
  822. if(i != m_Generators.end())
  823. {
  824. cmGlobalGenerator* generator = (i->second)();
  825. generator->SetCMakeInstance(this);
  826. return generator;
  827. }
  828. else
  829. {
  830. return 0;
  831. }
  832. }
  833. void cmake::SetHomeDirectory(const char* dir)
  834. {
  835. m_cmHomeDirectory = dir;
  836. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  837. }
  838. void cmake::SetHomeOutputDirectory(const char* lib)
  839. {
  840. m_HomeOutputDirectory = lib;
  841. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  842. }
  843. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  844. {
  845. // delete the old generator
  846. if (m_GlobalGenerator)
  847. {
  848. delete m_GlobalGenerator;
  849. // restore the original environment variables CXX and CC
  850. // Restor CC
  851. std::string env = "CC=";
  852. if(m_CCEnvironment.size())
  853. {
  854. env += m_CCEnvironment;
  855. }
  856. cmSystemTools::PutEnv(env.c_str());
  857. env = "CXX=";
  858. if(m_CXXEnvironment.size())
  859. {
  860. env += m_CXXEnvironment;
  861. }
  862. cmSystemTools::PutEnv(env.c_str());
  863. }
  864. // set the new
  865. m_GlobalGenerator = gg;
  866. // set the global flag for unix style paths on cmSystemTools as
  867. // soon as the generator is set. This allows gmake to be used
  868. // on windows.
  869. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  870. // Save the environment variables CXX and CC
  871. const char* cxx = getenv("CXX");
  872. const char* cc = getenv("CC");
  873. if(cxx)
  874. {
  875. m_CXXEnvironment = cxx;
  876. }
  877. else
  878. {
  879. m_CXXEnvironment = "";
  880. }
  881. if(cc)
  882. {
  883. m_CCEnvironment = cc;
  884. }
  885. else
  886. {
  887. m_CCEnvironment = "";
  888. }
  889. // set the cmake instance just to be sure
  890. gg->SetCMakeInstance(this);
  891. }
  892. int cmake::DoPreConfigureChecks()
  893. {
  894. // Make sure the Start directory contains a CMakeLists.txt file.
  895. std::string srcList = this->GetHomeDirectory();
  896. srcList += "/CMakeLists.txt";
  897. if(!cmSystemTools::FileExists(srcList.c_str()))
  898. {
  899. cmOStringStream err;
  900. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  901. {
  902. err << "The source directory \"" << this->GetHomeDirectory()
  903. << "\" does not appear to contain CMakeLists.txt.\n";
  904. }
  905. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  906. {
  907. err << "The source directory \"" << this->GetHomeDirectory()
  908. << "\" is a file, not a directory.\n";
  909. }
  910. else
  911. {
  912. err << "The source directory \"" << this->GetHomeDirectory()
  913. << "\" does not exist.\n";
  914. }
  915. err << "Specify --help for usage, or press the help button on the CMake GUI.";
  916. cmSystemTools::Error(err.str().c_str());
  917. return -2;
  918. }
  919. // do a sanity check on some values
  920. if(m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  921. {
  922. std::string cacheStart =
  923. m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  924. cacheStart += "/CMakeLists.txt";
  925. std::string currentStart = this->GetHomeDirectory();
  926. currentStart += "/CMakeLists.txt";
  927. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  928. {
  929. std::string message = "The source \"";
  930. message += currentStart;
  931. message += "\" does not match the source \"";
  932. message += cacheStart;
  933. message += "\" used to generate cache. ";
  934. message += "Re-run cmake with a different source directory.";
  935. cmSystemTools::Error(message.c_str());
  936. return -2;
  937. }
  938. }
  939. else
  940. {
  941. return 0;
  942. }
  943. return 1;
  944. }
  945. int cmake::Configure()
  946. {
  947. // Construct right now our path conversion table before it's too late:
  948. this->UpdateConversionPathTable();
  949. int res = 0;
  950. if ( !m_ScriptMode )
  951. {
  952. res = this->DoPreConfigureChecks();
  953. }
  954. if ( res < 0 )
  955. {
  956. return -2;
  957. }
  958. if ( !res )
  959. {
  960. m_CacheManager->AddCacheEntry("CMAKE_HOME_DIRECTORY",
  961. this->GetHomeDirectory(),
  962. "Start directory with the top level CMakeLists.txt file for this project",
  963. cmCacheManager::INTERNAL);
  964. }
  965. // set the default BACKWARDS compatibility to the current version
  966. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  967. {
  968. char ver[256];
  969. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  970. cmMakefile::GetMinorVersion());
  971. this->m_CacheManager->AddCacheEntry
  972. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  973. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  974. cmCacheManager::STRING);
  975. }
  976. // no generator specified on the command line
  977. if(!m_GlobalGenerator)
  978. {
  979. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  980. if(genName)
  981. {
  982. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  983. }
  984. if(m_GlobalGenerator)
  985. {
  986. // set the global flag for unix style paths on cmSystemTools as
  987. // soon as the generator is set. This allows gmake to be used
  988. // on windows.
  989. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  990. }
  991. else
  992. {
  993. #if defined(__BORLANDC__) && defined(_WIN32)
  994. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  995. #elif defined(_WIN32) && !defined(__CYGWIN__)
  996. std::string installedCompiler;
  997. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  998. cmSystemTools::ExpandRegistryValues(mp);
  999. if (!(mp == "/registry"))
  1000. {
  1001. installedCompiler = "Visual Studio 8 2005";
  1002. }
  1003. else
  1004. {
  1005. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.1;InstallDir]";
  1006. cmSystemTools::ExpandRegistryValues(mp);
  1007. if (!(mp == "/registry"))
  1008. {
  1009. installedCompiler = "Visual Studio 7 .NET 2003";
  1010. }
  1011. else
  1012. {
  1013. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VisualStudio\\7.0;InstallDir]";
  1014. cmSystemTools::ExpandRegistryValues(mp);
  1015. if (!(mp == "/registry"))
  1016. {
  1017. installedCompiler = "Visual Studio 7";
  1018. }
  1019. else
  1020. {
  1021. installedCompiler = "Visual Studio 6";
  1022. }
  1023. }
  1024. }
  1025. cmGlobalGenerator* gen = this->CreateGlobalGenerator(installedCompiler.c_str());
  1026. if(!gen)
  1027. {
  1028. gen = new cmGlobalNMakeMakefileGenerator;
  1029. }
  1030. this->SetGlobalGenerator(gen);
  1031. #else
  1032. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator);
  1033. #endif
  1034. }
  1035. if(!m_GlobalGenerator)
  1036. {
  1037. cmSystemTools::Error("Could not create generator");
  1038. return -1;
  1039. }
  1040. }
  1041. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1042. if(genName)
  1043. {
  1044. if(strcmp(m_GlobalGenerator->GetName(), genName) != 0)
  1045. {
  1046. std::string message = "Error: generator : ";
  1047. message += m_GlobalGenerator->GetName();
  1048. message += "\nDoes not match the generator used previously: ";
  1049. message += genName;
  1050. message +=
  1051. "\nEither remove the CMakeCache.txt file or choose a different"
  1052. " binary directory.";
  1053. cmSystemTools::Error(message.c_str());
  1054. return -2;
  1055. }
  1056. }
  1057. if(!m_CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1058. {
  1059. m_CacheManager->AddCacheEntry("CMAKE_GENERATOR", m_GlobalGenerator->GetName(),
  1060. "Name of generator.",
  1061. cmCacheManager::INTERNAL);
  1062. }
  1063. // reset any system configuration information, except for when we are
  1064. // InTryCompile. With TryCompile the system info is taken from the parent's
  1065. // info to save time
  1066. if (!m_InTryCompile)
  1067. {
  1068. m_GlobalGenerator->ClearEnabledLanguages();
  1069. }
  1070. this->CleanupWrittenFiles();
  1071. // actually do the configure
  1072. m_GlobalGenerator->Configure();
  1073. // Before saving the cache
  1074. // if the project did not define one of the entries below, add them now
  1075. // so users can edit the values in the cache:
  1076. // LIBRARY_OUTPUT_PATH
  1077. // EXECUTABLE_OUTPUT_PATH
  1078. if(!m_CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1079. {
  1080. m_CacheManager->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  1081. "Single output directory for building all libraries.",
  1082. cmCacheManager::PATH);
  1083. }
  1084. if(!m_CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1085. {
  1086. m_CacheManager->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  1087. "Single output directory for building all executables.",
  1088. cmCacheManager::PATH);
  1089. }
  1090. if(!m_CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1091. {
  1092. m_CacheManager->AddCacheEntry("CMAKE_USE_RELATIVE_PATHS", false,
  1093. "If true, cmake will use relative paths in makefiles and projects.");
  1094. cmCacheManager::CacheIterator it =
  1095. m_CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1096. if ( !it.PropertyExists("ADVANCED") )
  1097. {
  1098. it.SetProperty("ADVANCED", "1");
  1099. }
  1100. }
  1101. if(cmSystemTools::GetFatalErrorOccured() &&
  1102. (!this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1103. cmSystemTools::IsOff(this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1104. {
  1105. // We must have a bad generator selection. Wipe the cache entry so the
  1106. // user can select another.
  1107. m_CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1108. }
  1109. if ( !m_ScriptMode )
  1110. {
  1111. this->m_CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1112. }
  1113. if(cmSystemTools::GetErrorOccuredFlag())
  1114. {
  1115. return -1;
  1116. }
  1117. return 0;
  1118. }
  1119. bool cmake::CacheVersionMatches()
  1120. {
  1121. const char* majv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1122. const char* minv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1123. const char* relv = m_CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1124. bool cacheSameCMake = false;
  1125. if(majv &&
  1126. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  1127. && minv &&
  1128. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  1129. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  1130. {
  1131. cacheSameCMake = true;
  1132. }
  1133. return cacheSameCMake;
  1134. }
  1135. void cmake::PreLoadCMakeFiles()
  1136. {
  1137. std::string pre_load = this->GetHomeDirectory();
  1138. if ( pre_load.size() > 0 )
  1139. {
  1140. pre_load += "/PreLoad.cmake";
  1141. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1142. {
  1143. this->ReadListFile(pre_load.c_str());
  1144. }
  1145. }
  1146. pre_load = this->GetHomeOutputDirectory();
  1147. if ( pre_load.size() > 0 )
  1148. {
  1149. pre_load += "/PreLoad.cmake";
  1150. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1151. {
  1152. this->ReadListFile(pre_load.c_str());
  1153. }
  1154. }
  1155. }
  1156. // handle a command line invocation
  1157. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1158. {
  1159. // Process the arguments
  1160. this->SetArgs(args);
  1161. // set the cmake command
  1162. m_CMakeCommand = args[0];
  1163. if ( !m_ScriptMode )
  1164. {
  1165. // load the cache
  1166. if(this->LoadCache() < 0)
  1167. {
  1168. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1169. return -1;
  1170. }
  1171. }
  1172. // Add any cache args
  1173. if ( !this->SetCacheArgs(args) )
  1174. {
  1175. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1176. return -1;
  1177. }
  1178. this->PreLoadCMakeFiles();
  1179. std::string systemFile = this->GetHomeOutputDirectory();
  1180. systemFile += "/CMakeSystem.cmake";
  1181. if ( noconfigure )
  1182. {
  1183. return 0;
  1184. }
  1185. int ret = 0;
  1186. // if not local or the cmake version has changed since the last run
  1187. // of cmake, or CMakeSystem.cmake file is not in the root binary
  1188. // directory, run a global generate
  1189. if(m_ScriptMode || !m_Local || !this->CacheVersionMatches() ||
  1190. !cmSystemTools::FileExists(systemFile.c_str()) )
  1191. {
  1192. // Check the state of the build system to see if we need to regenerate.
  1193. if(!this->CheckBuildSystem())
  1194. {
  1195. return 0;
  1196. }
  1197. // If we are doing global generate, we better set start and start
  1198. // output directory to the root of the project.
  1199. std::string oldstartdir = this->GetStartDirectory();
  1200. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1201. this->SetStartDirectory(this->GetHomeDirectory());
  1202. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1203. bool saveLocalFlag = m_Local;
  1204. m_Local = false;
  1205. ret = this->Configure();
  1206. if (ret || m_ScriptMode)
  1207. {
  1208. return ret;
  1209. }
  1210. ret = this->Generate();
  1211. std::string message = "Build files have been written to: ";
  1212. message += this->GetHomeOutputDirectory();
  1213. this->UpdateProgress(message.c_str(), -1);
  1214. if(ret)
  1215. {
  1216. return ret;
  1217. }
  1218. m_Local = saveLocalFlag;
  1219. this->SetStartDirectory(oldstartdir.c_str());
  1220. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1221. }
  1222. // if we are local do the local thing
  1223. if (m_Local)
  1224. {
  1225. ret = this->LocalGenerate();
  1226. }
  1227. return ret;
  1228. }
  1229. int cmake::Generate()
  1230. {
  1231. if(!m_GlobalGenerator)
  1232. {
  1233. return -1;
  1234. }
  1235. m_GlobalGenerator->Generate();
  1236. if(cmSystemTools::GetErrorOccuredFlag())
  1237. {
  1238. return -1;
  1239. }
  1240. return 0;
  1241. }
  1242. int cmake::LocalGenerate()
  1243. {
  1244. // Read in the cache
  1245. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1246. // create the generator based on the cache if it isn't already there
  1247. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1248. if(genName)
  1249. {
  1250. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  1251. // set the global flag for unix style paths on cmSystemTools as
  1252. // soon as the generator is set. This allows gmake to be used
  1253. // on windows.
  1254. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  1255. }
  1256. else
  1257. {
  1258. cmSystemTools::Error("Could local Generate called without the GENERATOR being specified in the CMakeCache");
  1259. return -1;
  1260. }
  1261. // do the local generate
  1262. m_GlobalGenerator->LocalGenerate();
  1263. if(cmSystemTools::GetErrorOccuredFlag())
  1264. {
  1265. return -1;
  1266. }
  1267. return 0;
  1268. }
  1269. unsigned int cmake::GetMajorVersion()
  1270. {
  1271. return cmMakefile::GetMajorVersion();
  1272. }
  1273. unsigned int cmake::GetMinorVersion()
  1274. {
  1275. return cmMakefile::GetMinorVersion();
  1276. }
  1277. const char *cmake::GetReleaseVersion()
  1278. {
  1279. return cmMakefile::GetReleaseVersion();
  1280. }
  1281. void cmake::AddCacheEntry(const char* key, const char* value,
  1282. const char* helpString,
  1283. int type)
  1284. {
  1285. m_CacheManager->AddCacheEntry(key, value,
  1286. helpString,
  1287. cmCacheManager::CacheEntryType(type));
  1288. }
  1289. const char* cmake::GetCacheDefinition(const char* name) const
  1290. {
  1291. return m_CacheManager->GetCacheValue(name);
  1292. }
  1293. int cmake::DumpDocumentationToFile(std::ostream& f)
  1294. {
  1295. #ifdef CMAKE_BUILD_WITH_CMAKE
  1296. // Loop over all registered commands and print out documentation
  1297. const char *name;
  1298. const char *terse;
  1299. const char *full;
  1300. char tmp[1024];
  1301. sprintf(tmp,"Version %d.%d (%s)", cmake::GetMajorVersion(),
  1302. cmake::GetMinorVersion(), cmVersion::GetReleaseVersion().c_str());
  1303. f << "<html>\n";
  1304. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1305. f << "<ul>\n";
  1306. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  1307. j != m_Commands.end(); ++j)
  1308. {
  1309. name = (*j).second->GetName();
  1310. terse = (*j).second->GetTerseDocumentation();
  1311. full = (*j).second->GetFullDocumentation();
  1312. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1313. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1314. }
  1315. f << "</ul></html>\n";
  1316. #else
  1317. (void)f;
  1318. #endif
  1319. return 1;
  1320. }
  1321. void cmake::AddDefaultCommands()
  1322. {
  1323. std::list<cmCommand*> commands;
  1324. GetBootstrapCommands(commands);
  1325. GetPredefinedCommands(commands);
  1326. for(std::list<cmCommand*>::iterator i = commands.begin();
  1327. i != commands.end(); ++i)
  1328. {
  1329. this->AddCommand(*i);
  1330. }
  1331. }
  1332. void cmake::AddDefaultGenerators()
  1333. {
  1334. #if defined(_WIN32) && !defined(__CYGWIN__)
  1335. m_Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1336. &cmGlobalVisualStudio6Generator::New;
  1337. #if !defined(__MINGW32__)
  1338. m_Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1339. &cmGlobalVisualStudio7Generator::New;
  1340. m_Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1341. &cmGlobalVisualStudio71Generator::New;
  1342. m_Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  1343. &cmGlobalVisualStudio8Generator::New;
  1344. #endif
  1345. m_Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1346. &cmGlobalBorlandMakefileGenerator::New;
  1347. m_Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1348. &cmGlobalNMakeMakefileGenerator::New;
  1349. #else
  1350. # if defined(__APPLE__) && defined(CMAKE_BUILD_WITH_CMAKE)
  1351. m_Generators[cmGlobalCodeWarriorGenerator::GetActualName()] =
  1352. &cmGlobalCodeWarriorGenerator::New;
  1353. # endif
  1354. #endif
  1355. m_Generators[cmGlobalUnixMakefileGenerator::GetActualName()] =
  1356. &cmGlobalUnixMakefileGenerator::New;
  1357. m_Generators[cmGlobalXCodeGenerator::GetActualName()] =
  1358. &cmGlobalXCodeGenerator::New;
  1359. #ifdef CMAKE_USE_KDEVELOP
  1360. m_Generators[cmGlobalKdevelopGenerator::GetActualName()] =
  1361. &cmGlobalKdevelopGenerator::New;
  1362. #endif
  1363. }
  1364. int cmake::LoadCache()
  1365. {
  1366. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1367. if (m_CMakeCommand.size() < 2)
  1368. {
  1369. cmSystemTools::Error("cmake command was not specified prior to loading the cache in cmake.cxx");
  1370. return -1;
  1371. }
  1372. // setup CMAKE_ROOT and CMAKE_COMMAND
  1373. if(!this->AddCMakePaths(m_CMakeCommand.c_str()))
  1374. {
  1375. return -3;
  1376. }
  1377. // set the default BACKWARDS compatibility to the current version
  1378. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1379. {
  1380. char ver[256];
  1381. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1382. cmMakefile::GetMinorVersion());
  1383. this->m_CacheManager->AddCacheEntry
  1384. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1385. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1386. cmCacheManager::STRING);
  1387. }
  1388. return 0;
  1389. }
  1390. void cmake::SetProgressCallback(ProgressCallback f, void *cd)
  1391. {
  1392. m_ProgressCallback = f;
  1393. m_ProgressCallbackClientData = cd;
  1394. }
  1395. void cmake::UpdateProgress(const char *msg, float prog)
  1396. {
  1397. if(m_ProgressCallback && !m_InTryCompile)
  1398. {
  1399. (*m_ProgressCallback)(msg, prog, m_ProgressCallbackClientData);
  1400. return;
  1401. }
  1402. }
  1403. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v) const
  1404. {
  1405. for(RegisteredCommandsMap::const_iterator j = m_Commands.begin();
  1406. j != m_Commands.end(); ++j)
  1407. {
  1408. cmDocumentationEntry e =
  1409. {
  1410. (*j).second->GetName(),
  1411. (*j).second->GetTerseDocumentation(),
  1412. (*j).second->GetFullDocumentation()
  1413. };
  1414. v.push_back(e);
  1415. }
  1416. cmDocumentationEntry empty = {0,0,0};
  1417. v.push_back(empty);
  1418. }
  1419. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1420. {
  1421. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  1422. i != m_Generators.end(); ++i)
  1423. {
  1424. cmDocumentationEntry e;
  1425. cmGlobalGenerator* generator = (i->second)();
  1426. generator->GetDocumentation(e);
  1427. delete generator;
  1428. v.push_back(e);
  1429. }
  1430. cmDocumentationEntry empty = {0,0,0};
  1431. v.push_back(empty);
  1432. }
  1433. void cmake::AddWrittenFile(const char* file)
  1434. {
  1435. m_WrittenFiles.insert(file);
  1436. }
  1437. bool cmake::HasWrittenFile(const char* file)
  1438. {
  1439. return m_WrittenFiles.find(file) != m_WrittenFiles.end();
  1440. }
  1441. void cmake::CleanupWrittenFiles()
  1442. {
  1443. m_WrittenFiles.clear();
  1444. }
  1445. void cmake::UpdateConversionPathTable()
  1446. {
  1447. // Update the path conversion table with any specified file:
  1448. const char* tablepath =
  1449. m_CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1450. if(tablepath)
  1451. {
  1452. std::ifstream table( tablepath );
  1453. if(!table)
  1454. {
  1455. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath, ". CMake can not open file.");
  1456. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1457. }
  1458. else
  1459. {
  1460. std::string a, b;
  1461. while(!table.eof())
  1462. {
  1463. // two entries per line
  1464. table >> a; table >> b;
  1465. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1466. }
  1467. }
  1468. }
  1469. }
  1470. //----------------------------------------------------------------------------
  1471. int cmake::CheckBuildSystem()
  1472. {
  1473. // This method will check the integrity of the build system if the
  1474. // option was given on the command line. It reads the given file to
  1475. // determine whether CMake should rerun. If it does rerun then the
  1476. // generation step will check the integrity of dependencies. If it
  1477. // does not then we need to check the integrity here.
  1478. // If no file is provided for the check, we have to rerun.
  1479. if(m_CheckBuildSystem.size() == 0)
  1480. {
  1481. return 1;
  1482. }
  1483. // If the file provided does not exist, we have to rerun.
  1484. if(!cmSystemTools::FileExists(m_CheckBuildSystem.c_str()))
  1485. {
  1486. return 1;
  1487. }
  1488. // Read the rerun check file and use it to decide whether to do the
  1489. // global generate.
  1490. cmake cm;
  1491. cmGlobalGenerator gg;
  1492. gg.SetCMakeInstance(&cm);
  1493. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1494. lg->SetGlobalGenerator(&gg);
  1495. cmMakefile* mf = lg->GetMakefile();
  1496. if(!mf->ReadListFile(0, m_CheckBuildSystem.c_str()) ||
  1497. cmSystemTools::GetErrorOccuredFlag())
  1498. {
  1499. // There was an error reading the file. Just rerun.
  1500. return 1;
  1501. }
  1502. // Get the set of dependencies and outputs.
  1503. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1504. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1505. if(!dependsStr || !outputsStr)
  1506. {
  1507. // Not enough information was provided to do the test. Just rerun.
  1508. return 1;
  1509. }
  1510. std::vector<std::string> depends;
  1511. std::vector<std::string> outputs;
  1512. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1513. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1514. // If any output is older than any dependency then rerun.
  1515. for(std::vector<std::string>::iterator dep = depends.begin();
  1516. dep != depends.end(); ++dep)
  1517. {
  1518. for(std::vector<std::string>::iterator out = outputs.begin();
  1519. out != outputs.end(); ++out)
  1520. {
  1521. int result = 0;
  1522. if(!cmSystemTools::FileTimeCompare(out->c_str(), dep->c_str(), &result) ||
  1523. result < 0)
  1524. {
  1525. return 1;
  1526. }
  1527. }
  1528. }
  1529. #if defined(CMAKE_BUILD_WITH_CMAKE)
  1530. // We do not need to rerun CMake. Check dependency integrity.
  1531. cmLocalUnixMakefileGenerator2::CheckDependencies(mf);
  1532. #endif
  1533. // No need to rerun.
  1534. return 0;
  1535. }