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