1
0

cmake.cxx 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391
  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. static char makeflags[] = "MAKEFLAGS=";
  77. putenv(makeflags);
  78. }
  79. m_Local = false;
  80. m_Verbose = false;
  81. m_InTryCompile = false;
  82. m_CacheManager = new cmCacheManager;
  83. m_GlobalGenerator = 0;
  84. m_ProgressCallback = 0;
  85. m_ProgressCallbackClientData = 0;
  86. m_VariableWatch = new cmVariableWatch;
  87. m_ScriptMode = false;
  88. this->AddDefaultGenerators();
  89. this->AddDefaultCommands();
  90. m_VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  91. cmNeedBackwardsCompatibility);
  92. m_VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  93. cmNeedBackwardsCompatibility);
  94. m_VariableWatch->AddWatch("CMAKE_X_LIBS",
  95. cmNeedBackwardsCompatibility);
  96. }
  97. cmake::~cmake()
  98. {
  99. delete m_CacheManager;
  100. if (m_GlobalGenerator)
  101. {
  102. delete m_GlobalGenerator;
  103. m_GlobalGenerator = 0;
  104. }
  105. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  106. j != m_Commands.end(); ++j)
  107. {
  108. delete (*j).second;
  109. }
  110. delete m_VariableWatch;
  111. }
  112. bool cmake::CommandExists(const char* name) const
  113. {
  114. return (m_Commands.find(name) != m_Commands.end());
  115. }
  116. cmCommand *cmake::GetCommand(const char *name)
  117. {
  118. cmCommand* rm = 0;
  119. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  120. if (pos != m_Commands.end())
  121. {
  122. rm = (*pos).second;
  123. }
  124. return rm;
  125. }
  126. void cmake::AddCommand(cmCommand* wg)
  127. {
  128. std::string name = wg->GetName();
  129. // if the command already exists, free the old one
  130. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  131. if (pos != m_Commands.end())
  132. {
  133. delete pos->second;
  134. m_Commands.erase(pos);
  135. }
  136. m_Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  137. }
  138. void cmake::Usage(const char* program)
  139. {
  140. cmOStringStream errorStream;
  141. errorStream << "cmake version " << cmMakefile::GetMajorVersion()
  142. << "." << cmMakefile::GetMinorVersion() << "\n";
  143. errorStream << "Usage: " << program << " [srcdir] [options]\n"
  144. << "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";
  145. errorStream << "Options are:\n";
  146. errorStream << "\n-i (puts cmake in wizard mode, not available for ccmake)\n";
  147. errorStream << "\n-DVAR:TYPE=VALUE (create a cache file entry)\n";
  148. errorStream << "\n-Cpath_to_initial_cache (a cmake list file that is used to pre-load the cache with values.)\n";
  149. errorStream << "\n[-GgeneratorName] (where generator name can be one of these: ";
  150. std::vector<std::string> names;
  151. this->GetRegisteredGenerators(names);
  152. for(std::vector<std::string>::iterator i =names.begin();
  153. i != names.end(); ++i)
  154. {
  155. errorStream << "\"" << i->c_str() << "\" ";
  156. }
  157. errorStream << ")\n";
  158. cmSystemTools::Error(errorStream.str().c_str());
  159. }
  160. // Parse the args
  161. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  162. {
  163. for(unsigned int i=1; i < args.size(); ++i)
  164. {
  165. std::string arg = args[i];
  166. if(arg.find("-D",0) == 0)
  167. {
  168. std::string entry = arg.substr(2);
  169. std::string var, value;
  170. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  171. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  172. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  173. {
  174. this->m_CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  175. "No help, variable specified on the command line.",
  176. type);
  177. }
  178. else
  179. {
  180. std::cerr << "Parse error in command line argument: " << arg << "\n"
  181. << "Should be: VAR:type=value\n";
  182. cmSystemTools::Error("No cmake scrpt provided.");
  183. return false;
  184. }
  185. }
  186. else if(arg.find("-C",0) == 0)
  187. {
  188. std::string path = arg.substr(2);
  189. if ( path.size() == 0 )
  190. {
  191. cmSystemTools::Error("No initial cache file provided.");
  192. return false;
  193. }
  194. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  195. this->ReadListFile(path.c_str());
  196. }
  197. else if(arg.find("-P",0) == 0)
  198. {
  199. std::string path = arg.substr(strlen("-P"));
  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. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  502. {
  503. this->m_CacheManager->AddCacheEntry
  504. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  505. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  506. }
  507. // do CMAKE_ROOT, look for the environment variable first
  508. std::string cMakeRoot;
  509. std::string modules;
  510. if (getenv("CMAKE_ROOT"))
  511. {
  512. cMakeRoot = getenv("CMAKE_ROOT");
  513. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  514. }
  515. if(!cmSystemTools::FileExists(modules.c_str()))
  516. {
  517. // next try exe/..
  518. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  519. std::string::size_type slashPos = cMakeRoot.rfind("/");
  520. if(slashPos != std::string::npos)
  521. {
  522. cMakeRoot = cMakeRoot.substr(0, slashPos);
  523. }
  524. // is there no Modules direcory there?
  525. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  526. }
  527. if (!cmSystemTools::FileExists(modules.c_str()))
  528. {
  529. // try exe/../share/cmake
  530. cMakeRoot += CMAKE_DATA_DIR;
  531. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  532. }
  533. #ifdef CMAKE_ROOT_DIR
  534. if (!cmSystemTools::FileExists(modules.c_str()))
  535. {
  536. // try compiled in root directory
  537. cMakeRoot = CMAKE_ROOT_DIR;
  538. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  539. }
  540. #endif
  541. #ifdef CMAKE_PREFIX
  542. if (!cmSystemTools::FileExists(modules.c_str()))
  543. {
  544. // try compiled in install prefix
  545. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  546. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  547. }
  548. #endif
  549. if (!cmSystemTools::FileExists(modules.c_str()))
  550. {
  551. // try
  552. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  553. cMakeRoot += CMAKE_DATA_DIR;
  554. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  555. }
  556. if(!cmSystemTools::FileExists(modules.c_str()))
  557. {
  558. // next try exe
  559. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  560. // is there no Modules direcory there?
  561. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  562. }
  563. if (!cmSystemTools::FileExists(modules.c_str()))
  564. {
  565. // couldn't find modules
  566. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  567. "CMake has most likely not been installed correctly.\n"
  568. "Modules directory not found in\n",
  569. cMakeRoot.c_str());
  570. return 0;
  571. }
  572. this->m_CacheManager->AddCacheEntry
  573. ("CMAKE_ROOT", cMakeRoot.c_str(),
  574. "Path to CMake installation.", cmCacheManager::INTERNAL);
  575. #ifdef _WIN32
  576. std::string comspec = "cmw9xcom.exe";
  577. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  578. #endif
  579. return 1;
  580. }
  581. void CMakeCommandUsage(const char* program)
  582. {
  583. cmOStringStream errorStream;
  584. errorStream
  585. << "cmake version " << cmMakefile::GetMajorVersion()
  586. << "." << cmMakefile::GetMinorVersion() << "\n";
  587. errorStream
  588. << "Usage: " << program << " -E [command] [arguments ...]\n"
  589. << "Available commands: \n"
  590. << " chdir dir cmd [args]... - run command in a given directory\n"
  591. << " copy file destination - copy file to destination (either file or directory)\n"
  592. << " copy_if_different in-file out-file - copy file if input has changed\n"
  593. << " echo [string]... - displays arguments as text\n"
  594. << " remove file1 file2 ... - remove the file(s)\n"
  595. << " time command [args] ... - run command and return elapsed time\n";
  596. #if defined(_WIN32) && !defined(__CYGWIN__)
  597. errorStream
  598. << " write_regv key value - write registry value\n"
  599. << " delete_regv key - delete registry value\n"
  600. << " comspec - on windows 9x use this for RunCommand\n";
  601. #endif
  602. cmSystemTools::Error(errorStream.str().c_str());
  603. }
  604. int cmake::CMakeCommand(std::vector<std::string>& args)
  605. {
  606. if (args.size() > 1)
  607. {
  608. // Copy file
  609. if (args[1] == "copy" && args.size() == 4)
  610. {
  611. cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str());
  612. return cmSystemTools::GetErrorOccuredFlag();
  613. }
  614. // Copy file if different.
  615. if (args[1] == "copy_if_different" && args.size() == 4)
  616. {
  617. cmSystemTools::CopyFileIfDifferent(args[2].c_str(), args[3].c_str());
  618. return cmSystemTools::GetErrorOccuredFlag();
  619. }
  620. // Echo string
  621. else if (args[1] == "echo" )
  622. {
  623. unsigned int cc;
  624. for ( cc = 2; cc < args.size(); cc ++ )
  625. {
  626. std::cout << args[cc] << " ";
  627. }
  628. std::cout << std::endl;
  629. return 0;
  630. }
  631. // Remove file
  632. else if (args[1] == "remove" && args.size() > 2)
  633. {
  634. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  635. {
  636. if(args[cc] != "-f")
  637. {
  638. if(args[cc] == "\\-f")
  639. {
  640. args[cc] = "-f";
  641. }
  642. cmSystemTools::RemoveFile(args[cc].c_str());
  643. }
  644. }
  645. return 0;
  646. }
  647. // Clock command
  648. else if (args[1] == "time" && args.size() > 2)
  649. {
  650. std::string command = args[2];
  651. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  652. {
  653. command += " ";
  654. command += args[cc];
  655. }
  656. clock_t clock_start, clock_finish;
  657. time_t time_start, time_finish;
  658. time(&time_start);
  659. clock_start = clock();
  660. cmSystemTools::RunSingleCommand(command.c_str());
  661. clock_finish = clock();
  662. time(&time_finish);
  663. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  664. std::cout << "Elapsed time: "
  665. << (long)(time_finish - time_start) << " s. (time)"
  666. << ", "
  667. << (double)(clock_finish - clock_start) / clocks_per_sec
  668. << " s. (clock)"
  669. << "\n";
  670. return 0;
  671. }
  672. // Clock command
  673. else if (args[1] == "chdir" && args.size() >= 4)
  674. {
  675. std::string directory = args[2];
  676. std::string command = "\"";
  677. command += args[3];
  678. command += "\"";
  679. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  680. {
  681. command += " \"";
  682. command += args[cc];
  683. command += "\"";
  684. }
  685. int retval = 0;
  686. int timeout = 0;
  687. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  688. directory.c_str(), true, timeout) )
  689. {
  690. return retval;
  691. }
  692. return 1;
  693. }
  694. // Internal CMake shared library support.
  695. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  696. {
  697. int result = 0;
  698. std::string realName = args[2];
  699. std::string soName = args[3];
  700. std::string name = args[4];
  701. if(soName != realName)
  702. {
  703. std::string fname = cmSystemTools::GetFilenameName(realName);
  704. if(cmSystemTools::FileExists(soName.c_str()))
  705. {
  706. cmSystemTools::RemoveFile(soName.c_str());
  707. }
  708. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  709. {
  710. result = 1;
  711. }
  712. }
  713. if(name != soName)
  714. {
  715. std::string fname = cmSystemTools::GetFilenameName(soName);
  716. if(cmSystemTools::FileExists(soName.c_str()))
  717. {
  718. cmSystemTools::RemoveFile(name.c_str());
  719. }
  720. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  721. {
  722. result = 1;
  723. }
  724. }
  725. return result;
  726. }
  727. #if defined(_WIN32) && !defined(__CYGWIN__)
  728. // Write registry value
  729. else if (args[1] == "write_regv" && args.size() > 3)
  730. {
  731. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  732. args[3].c_str()) ? 0 : 1;
  733. }
  734. // Delete registry value
  735. else if (args[1] == "delete_regv" && args.size() > 2)
  736. {
  737. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  738. }
  739. // Remove file
  740. else if (args[1] == "comspec" && args.size() > 2)
  741. {
  742. unsigned int cc;
  743. std::string command = args[2];
  744. for ( cc = 3; cc < args.size(); cc ++ )
  745. {
  746. command += " " + args[cc];
  747. }
  748. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  749. }
  750. #endif
  751. }
  752. ::CMakeCommandUsage(args[0].c_str());
  753. return 1;
  754. }
  755. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  756. {
  757. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  758. i != m_Generators.end(); ++i)
  759. {
  760. names.push_back(i->first);
  761. }
  762. }
  763. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  764. {
  765. RegisteredGeneratorsMap::const_iterator i = m_Generators.find(name);
  766. if(i != m_Generators.end())
  767. {
  768. cmGlobalGenerator* generator = (i->second)();
  769. generator->SetCMakeInstance(this);
  770. return generator;
  771. }
  772. else
  773. {
  774. return 0;
  775. }
  776. }
  777. void cmake::SetHomeDirectory(const char* dir)
  778. {
  779. m_cmHomeDirectory = dir;
  780. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  781. }
  782. void cmake::SetHomeOutputDirectory(const char* lib)
  783. {
  784. m_HomeOutputDirectory = lib;
  785. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  786. }
  787. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  788. {
  789. // delete the old generator
  790. if (m_GlobalGenerator)
  791. {
  792. delete m_GlobalGenerator;
  793. // restore the original environment variables CXX and CC
  794. // Restor CC
  795. static char envCC[5000];
  796. std::string env = "CC=";
  797. if(m_CCEnvironment)
  798. {
  799. env += m_CCEnvironment;
  800. }
  801. std::string::size_type size = env.size();
  802. if(size > 4999)
  803. {
  804. size = 4999;
  805. }
  806. strncpy(envCC, env.c_str(), size);
  807. envCC[4999] = 0;
  808. putenv(envCC);
  809. // Restore CXX
  810. static char envCXX[5000];
  811. env = "CXX=";
  812. if(m_CXXEnvironment)
  813. {
  814. env += m_CXXEnvironment;
  815. }
  816. size = env.size();
  817. if(size > 4999)
  818. {
  819. size = 4999;
  820. }
  821. strncpy(envCXX, env.c_str(), size);
  822. envCXX[4999] = 0;
  823. putenv(envCXX);
  824. }
  825. // set the new
  826. m_GlobalGenerator = gg;
  827. // set the global flag for unix style paths on cmSystemTools as
  828. // soon as the generator is set. This allows gmake to be used
  829. // on windows.
  830. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  831. // Save the environment variables CXX and CC
  832. m_CXXEnvironment = getenv("CXX");
  833. m_CCEnvironment = getenv("CC");
  834. // set the cmake instance just to be sure
  835. gg->SetCMakeInstance(this);
  836. }
  837. int cmake::DoPreConfigureChecks()
  838. {
  839. // Make sure the Start directory contains a CMakeLists.txt file.
  840. std::string srcList = this->GetHomeDirectory();
  841. srcList += "/CMakeLists.txt";
  842. if(!cmSystemTools::FileExists(srcList.c_str()))
  843. {
  844. cmOStringStream err;
  845. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  846. {
  847. err << "The source directory \"" << this->GetHomeDirectory()
  848. << "\" does not appear to contain CMakeLists.txt.\n";
  849. }
  850. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  851. {
  852. err << "The source directory \"" << this->GetHomeDirectory()
  853. << "\" is a file, not a directory.\n";
  854. }
  855. else
  856. {
  857. err << "The source directory \"" << this->GetHomeDirectory()
  858. << "\" does not exist.\n";
  859. }
  860. err << "Specify --help for usage, or press the help button on the CMake GUI.";
  861. cmSystemTools::Error(err.str().c_str());
  862. return -2;
  863. }
  864. // do a sanity check on some values
  865. if(m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  866. {
  867. std::string cacheStart =
  868. m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  869. cacheStart += "/CMakeLists.txt";
  870. std::string currentStart = this->GetHomeDirectory();
  871. currentStart += "/CMakeLists.txt";
  872. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  873. {
  874. std::string message = "The source \"";
  875. message += currentStart;
  876. message += "\" does not match the source \"";
  877. message += cacheStart;
  878. message += "\" used to generate cache. ";
  879. message += "Re-run cmake with a different source directory.";
  880. cmSystemTools::Error(message.c_str());
  881. return -2;
  882. }
  883. }
  884. else
  885. {
  886. return 0;
  887. }
  888. return 1;
  889. }
  890. int cmake::Configure()
  891. {
  892. int res = 0;
  893. if ( !m_ScriptMode )
  894. {
  895. res = this->DoPreConfigureChecks();
  896. }
  897. if ( res < 0 )
  898. {
  899. return -2;
  900. }
  901. if ( !res )
  902. {
  903. m_CacheManager->AddCacheEntry("CMAKE_HOME_DIRECTORY",
  904. this->GetHomeDirectory(),
  905. "Start directory with the top level CMakeLists.txt file for this project",
  906. cmCacheManager::INTERNAL);
  907. }
  908. // set the default BACKWARDS compatibility to the current version
  909. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  910. {
  911. char ver[256];
  912. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  913. cmMakefile::GetMinorVersion());
  914. this->m_CacheManager->AddCacheEntry
  915. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  916. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  917. cmCacheManager::STRING);
  918. }
  919. // no generator specified on the command line
  920. if(!m_GlobalGenerator)
  921. {
  922. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  923. if(genName)
  924. {
  925. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  926. // set the global flag for unix style paths on cmSystemTools as
  927. // soon as the generator is set. This allows gmake to be used
  928. // on windows.
  929. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  930. }
  931. else
  932. {
  933. #if defined(__BORLANDC__) && defined(_WIN32)
  934. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  935. #elif defined(_WIN32) && !defined(__CYGWIN__)
  936. this->SetGlobalGenerator(new cmGlobalVisualStudio6Generator);
  937. #else
  938. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator);
  939. #endif
  940. }
  941. if(!m_GlobalGenerator)
  942. {
  943. cmSystemTools::Error("Could not create generator");
  944. return -1;
  945. }
  946. }
  947. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  948. if(genName)
  949. {
  950. if(strcmp(m_GlobalGenerator->GetName(), genName) != 0)
  951. {
  952. std::string message = "Error: generator : ";
  953. message += m_GlobalGenerator->GetName();
  954. message += "\nDoes not match the generator used previously: ";
  955. message += genName;
  956. message +=
  957. "\nEither remove the CMakeCache.txt file or choose a different"
  958. " binary directory.";
  959. cmSystemTools::Error(message.c_str());
  960. return -2;
  961. }
  962. }
  963. if(!m_CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  964. {
  965. m_CacheManager->AddCacheEntry("CMAKE_GENERATOR", m_GlobalGenerator->GetName(),
  966. "Name of generator.",
  967. cmCacheManager::INTERNAL);
  968. }
  969. // reset any system configuration information, except for when we are
  970. // InTryCompile. With TryCompile the system info is taken from the parent's
  971. // info to save time
  972. if (!m_InTryCompile)
  973. {
  974. m_GlobalGenerator->ClearEnabledLanguages();
  975. }
  976. // actually do the configure
  977. m_GlobalGenerator->Configure();
  978. // Before saving the cache
  979. // if the project did not define one of the entries below, add them now
  980. // so users can edit the values in the cache:
  981. // LIBRARY_OUTPUT_PATH
  982. // EXECUTABLE_OUTPUT_PATH
  983. if(!m_CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  984. {
  985. m_CacheManager->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  986. "Single output directory for building all libraries.",
  987. cmCacheManager::PATH);
  988. }
  989. if(!m_CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  990. {
  991. m_CacheManager->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  992. "Single output directory for building all executables.",
  993. cmCacheManager::PATH);
  994. }
  995. if(cmSystemTools::GetFatalErrorOccured() &&
  996. (!this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  997. cmSystemTools::IsOff(this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  998. {
  999. // We must have a bad generator selection. Wipe the cache entry so the
  1000. // user can select another.
  1001. m_CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1002. }
  1003. if ( !m_ScriptMode )
  1004. {
  1005. this->m_CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1006. }
  1007. if(cmSystemTools::GetErrorOccuredFlag())
  1008. {
  1009. return -1;
  1010. }
  1011. return 0;
  1012. }
  1013. bool cmake::CacheVersionMatches()
  1014. {
  1015. const char* majv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1016. const char* minv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1017. const char* relv = m_CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1018. bool cacheSameCMake = false;
  1019. if(majv &&
  1020. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  1021. && minv &&
  1022. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  1023. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  1024. {
  1025. cacheSameCMake = true;
  1026. }
  1027. return cacheSameCMake;
  1028. }
  1029. // handle a command line invocation
  1030. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1031. {
  1032. // Process the arguments
  1033. this->SetArgs(args);
  1034. // set the cmake command
  1035. m_CMakeCommand = args[0];
  1036. if ( !m_ScriptMode )
  1037. {
  1038. // load the cache
  1039. if(this->LoadCache() < 0)
  1040. {
  1041. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1042. return -1;
  1043. }
  1044. }
  1045. // Add any cache args
  1046. if ( !this->SetCacheArgs(args) )
  1047. {
  1048. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1049. return -1;
  1050. }
  1051. std::string systemFile = this->GetHomeOutputDirectory();
  1052. systemFile += "/CMakeSystem.cmake";
  1053. if ( noconfigure )
  1054. {
  1055. return 0;
  1056. }
  1057. int ret = 0;
  1058. // if not local or the cmake version has changed since the last run
  1059. // of cmake, or CMakeSystem.cmake file is not in the root binary
  1060. // directory, run a global generate
  1061. if(m_ScriptMode || !m_Local || !this->CacheVersionMatches() ||
  1062. !cmSystemTools::FileExists(systemFile.c_str()) )
  1063. {
  1064. // If we are doing global generate, we better set start and start
  1065. // output directory to the root of the project.
  1066. std::string oldstartdir = this->GetStartDirectory();
  1067. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1068. this->SetStartDirectory(this->GetHomeDirectory());
  1069. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1070. bool saveLocalFlag = m_Local;
  1071. m_Local = false;
  1072. ret = this->Configure();
  1073. if (ret || m_ScriptMode)
  1074. {
  1075. return ret;
  1076. }
  1077. ret = this->Generate();
  1078. if(ret)
  1079. {
  1080. return ret;
  1081. }
  1082. m_Local = saveLocalFlag;
  1083. this->SetStartDirectory(oldstartdir.c_str());
  1084. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1085. }
  1086. // if we are local do the local thing
  1087. if (m_Local)
  1088. {
  1089. ret = this->LocalGenerate();
  1090. }
  1091. return ret;
  1092. }
  1093. int cmake::Generate()
  1094. {
  1095. if(!m_GlobalGenerator)
  1096. {
  1097. return -1;
  1098. }
  1099. m_GlobalGenerator->Generate();
  1100. if(cmSystemTools::GetErrorOccuredFlag())
  1101. {
  1102. return -1;
  1103. }
  1104. return 0;
  1105. }
  1106. int cmake::LocalGenerate()
  1107. {
  1108. // Read in the cache
  1109. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1110. // create the generator based on the cache if it isn't already there
  1111. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1112. if(genName)
  1113. {
  1114. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  1115. // set the global flag for unix style paths on cmSystemTools as
  1116. // soon as the generator is set. This allows gmake to be used
  1117. // on windows.
  1118. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  1119. }
  1120. else
  1121. {
  1122. cmSystemTools::Error("Could local Generate called without the GENERATOR being specified in the CMakeCache");
  1123. return -1;
  1124. }
  1125. // do the local generate
  1126. m_GlobalGenerator->LocalGenerate();
  1127. if(cmSystemTools::GetErrorOccuredFlag())
  1128. {
  1129. return -1;
  1130. }
  1131. return 0;
  1132. }
  1133. unsigned int cmake::GetMajorVersion()
  1134. {
  1135. return cmMakefile::GetMajorVersion();
  1136. }
  1137. unsigned int cmake::GetMinorVersion()
  1138. {
  1139. return cmMakefile::GetMinorVersion();
  1140. }
  1141. const char *cmake::GetReleaseVersion()
  1142. {
  1143. return cmMakefile::GetReleaseVersion();
  1144. }
  1145. void cmake::AddCacheEntry(const char* key, const char* value,
  1146. const char* helpString,
  1147. int type)
  1148. {
  1149. m_CacheManager->AddCacheEntry(key, value,
  1150. helpString,
  1151. cmCacheManager::CacheEntryType(type));
  1152. }
  1153. const char* cmake::GetCacheDefinition(const char* name) const
  1154. {
  1155. return m_CacheManager->GetCacheValue(name);
  1156. }
  1157. int cmake::DumpDocumentationToFile(std::ostream& f)
  1158. {
  1159. // Loop over all registered commands and print out documentation
  1160. const char *name;
  1161. const char *terse;
  1162. const char *full;
  1163. char tmp[1024];
  1164. sprintf(tmp,"Version %d.%d", cmake::GetMajorVersion(),
  1165. cmake::GetMinorVersion());
  1166. f << "<html>\n";
  1167. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1168. f << "<ul>\n";
  1169. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  1170. j != m_Commands.end(); ++j)
  1171. {
  1172. name = (*j).second->GetName();
  1173. terse = (*j).second->GetTerseDocumentation();
  1174. full = (*j).second->GetFullDocumentation();
  1175. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1176. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1177. }
  1178. f << "</ul></html>\n";
  1179. return 1;
  1180. }
  1181. void cmake::AddDefaultCommands()
  1182. {
  1183. std::list<cmCommand*> commands;
  1184. GetPredefinedCommands(commands);
  1185. for(std::list<cmCommand*>::iterator i = commands.begin();
  1186. i != commands.end(); ++i)
  1187. {
  1188. this->AddCommand(*i);
  1189. }
  1190. }
  1191. void cmake::AddDefaultGenerators()
  1192. {
  1193. #if defined(_WIN32) && !defined(__CYGWIN__)
  1194. m_Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1195. &cmGlobalVisualStudio6Generator::New;
  1196. #if !defined(__MINGW32__)
  1197. m_Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1198. &cmGlobalVisualStudio7Generator::New;
  1199. m_Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1200. &cmGlobalVisualStudio71Generator::New;
  1201. #endif
  1202. m_Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1203. &cmGlobalBorlandMakefileGenerator::New;
  1204. m_Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1205. &cmGlobalNMakeMakefileGenerator::New;
  1206. #else
  1207. # if defined(__APPLE__) && defined(CMAKE_BUILD_WITH_CMAKE)
  1208. m_Generators[cmGlobalCodeWarriorGenerator::GetActualName()] =
  1209. &cmGlobalCodeWarriorGenerator::New;
  1210. # endif
  1211. #endif
  1212. m_Generators[cmGlobalUnixMakefileGenerator::GetActualName()] =
  1213. &cmGlobalUnixMakefileGenerator::New;
  1214. }
  1215. int cmake::LoadCache()
  1216. {
  1217. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1218. if (m_CMakeCommand.size() < 2)
  1219. {
  1220. cmSystemTools::Error("cmake command was not specified prior to loading the cache in cmake.cxx");
  1221. return -1;
  1222. }
  1223. // setup CMAKE_ROOT and CMAKE_COMMAND
  1224. if(!this->AddCMakePaths(m_CMakeCommand.c_str()))
  1225. {
  1226. return -3;
  1227. }
  1228. // set the default BACKWARDS compatibility to the current version
  1229. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1230. {
  1231. char ver[256];
  1232. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1233. cmMakefile::GetMinorVersion());
  1234. this->m_CacheManager->AddCacheEntry
  1235. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1236. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1237. cmCacheManager::STRING);
  1238. }
  1239. return 0;
  1240. }
  1241. void cmake::SetProgressCallback(ProgressCallback f, void *cd)
  1242. {
  1243. m_ProgressCallback = f;
  1244. m_ProgressCallbackClientData = cd;
  1245. }
  1246. void cmake::UpdateProgress(const char *msg, float prog)
  1247. {
  1248. if(m_ProgressCallback && !m_InTryCompile)
  1249. {
  1250. (*m_ProgressCallback)(msg, prog, m_ProgressCallbackClientData);
  1251. return;
  1252. }
  1253. }
  1254. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v) const
  1255. {
  1256. for(RegisteredCommandsMap::const_iterator j = m_Commands.begin();
  1257. j != m_Commands.end(); ++j)
  1258. {
  1259. cmDocumentationEntry e =
  1260. {
  1261. (*j).second->GetName(),
  1262. (*j).second->GetTerseDocumentation(),
  1263. (*j).second->GetFullDocumentation()
  1264. };
  1265. v.push_back(e);
  1266. }
  1267. cmDocumentationEntry empty = {0,0,0};
  1268. v.push_back(empty);
  1269. }
  1270. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1271. {
  1272. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  1273. i != m_Generators.end(); ++i)
  1274. {
  1275. cmDocumentationEntry e;
  1276. cmGlobalGenerator* generator = (i->second)();
  1277. generator->GetDocumentation(e);
  1278. delete generator;
  1279. v.push_back(e);
  1280. }
  1281. cmDocumentationEntry empty = {0,0,0};
  1282. v.push_back(empty);
  1283. }