cmake.cxx 42 KB

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