cmake.cxx 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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. this->CleanupWrittenFiles();
  995. // actually do the configure
  996. m_GlobalGenerator->Configure();
  997. // Before saving the cache
  998. // if the project did not define one of the entries below, add them now
  999. // so users can edit the values in the cache:
  1000. // LIBRARY_OUTPUT_PATH
  1001. // EXECUTABLE_OUTPUT_PATH
  1002. if(!m_CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1003. {
  1004. m_CacheManager->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  1005. "Single output directory for building all libraries.",
  1006. cmCacheManager::PATH);
  1007. }
  1008. if(!m_CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1009. {
  1010. m_CacheManager->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  1011. "Single output directory for building all executables.",
  1012. cmCacheManager::PATH);
  1013. }
  1014. if(!m_CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1015. {
  1016. m_CacheManager->AddCacheEntry("CMAKE_USE_RELATIVE_PATHS", false,
  1017. "If true, cmake will use relative paths in makefiles and projects.");
  1018. cmCacheManager::CacheIterator it =
  1019. m_CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1020. if ( !it.PropertyExists("ADVANCED") )
  1021. {
  1022. it.SetProperty("ADVANCED", "1");
  1023. }
  1024. }
  1025. if(cmSystemTools::GetFatalErrorOccured() &&
  1026. (!this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1027. cmSystemTools::IsOff(this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1028. {
  1029. // We must have a bad generator selection. Wipe the cache entry so the
  1030. // user can select another.
  1031. m_CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1032. }
  1033. if ( !m_ScriptMode )
  1034. {
  1035. this->m_CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1036. }
  1037. if(cmSystemTools::GetErrorOccuredFlag())
  1038. {
  1039. return -1;
  1040. }
  1041. return 0;
  1042. }
  1043. bool cmake::CacheVersionMatches()
  1044. {
  1045. const char* majv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1046. const char* minv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1047. const char* relv = m_CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1048. bool cacheSameCMake = false;
  1049. if(majv &&
  1050. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  1051. && minv &&
  1052. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  1053. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  1054. {
  1055. cacheSameCMake = true;
  1056. }
  1057. return cacheSameCMake;
  1058. }
  1059. // handle a command line invocation
  1060. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1061. {
  1062. // Process the arguments
  1063. this->SetArgs(args);
  1064. // set the cmake command
  1065. m_CMakeCommand = args[0];
  1066. if ( !m_ScriptMode )
  1067. {
  1068. // load the cache
  1069. if(this->LoadCache() < 0)
  1070. {
  1071. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1072. return -1;
  1073. }
  1074. }
  1075. // Add any cache args
  1076. if ( !this->SetCacheArgs(args) )
  1077. {
  1078. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1079. return -1;
  1080. }
  1081. std::string pre_load = this->GetHomeDirectory();
  1082. pre_load += "/PreLoad.cmake";
  1083. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1084. {
  1085. this->ReadListFile(pre_load.c_str());
  1086. }
  1087. std::string systemFile = this->GetHomeOutputDirectory();
  1088. systemFile += "/CMakeSystem.cmake";
  1089. if ( noconfigure )
  1090. {
  1091. return 0;
  1092. }
  1093. int ret = 0;
  1094. // if not local or the cmake version has changed since the last run
  1095. // of cmake, or CMakeSystem.cmake file is not in the root binary
  1096. // directory, run a global generate
  1097. if(m_ScriptMode || !m_Local || !this->CacheVersionMatches() ||
  1098. !cmSystemTools::FileExists(systemFile.c_str()) )
  1099. {
  1100. // If we are doing global generate, we better set start and start
  1101. // output directory to the root of the project.
  1102. std::string oldstartdir = this->GetStartDirectory();
  1103. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1104. this->SetStartDirectory(this->GetHomeDirectory());
  1105. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1106. bool saveLocalFlag = m_Local;
  1107. m_Local = false;
  1108. ret = this->Configure();
  1109. if (ret || m_ScriptMode)
  1110. {
  1111. return ret;
  1112. }
  1113. ret = this->Generate();
  1114. if(ret)
  1115. {
  1116. return ret;
  1117. }
  1118. m_Local = saveLocalFlag;
  1119. this->SetStartDirectory(oldstartdir.c_str());
  1120. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1121. }
  1122. // if we are local do the local thing
  1123. if (m_Local)
  1124. {
  1125. ret = this->LocalGenerate();
  1126. }
  1127. return ret;
  1128. }
  1129. int cmake::Generate()
  1130. {
  1131. if(!m_GlobalGenerator)
  1132. {
  1133. return -1;
  1134. }
  1135. m_GlobalGenerator->Generate();
  1136. if(cmSystemTools::GetErrorOccuredFlag())
  1137. {
  1138. return -1;
  1139. }
  1140. return 0;
  1141. }
  1142. int cmake::LocalGenerate()
  1143. {
  1144. // Read in the cache
  1145. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1146. // create the generator based on the cache if it isn't already there
  1147. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1148. if(genName)
  1149. {
  1150. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  1151. // set the global flag for unix style paths on cmSystemTools as
  1152. // soon as the generator is set. This allows gmake to be used
  1153. // on windows.
  1154. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  1155. }
  1156. else
  1157. {
  1158. cmSystemTools::Error("Could local Generate called without the GENERATOR being specified in the CMakeCache");
  1159. return -1;
  1160. }
  1161. // do the local generate
  1162. m_GlobalGenerator->LocalGenerate();
  1163. if(cmSystemTools::GetErrorOccuredFlag())
  1164. {
  1165. return -1;
  1166. }
  1167. return 0;
  1168. }
  1169. unsigned int cmake::GetMajorVersion()
  1170. {
  1171. return cmMakefile::GetMajorVersion();
  1172. }
  1173. unsigned int cmake::GetMinorVersion()
  1174. {
  1175. return cmMakefile::GetMinorVersion();
  1176. }
  1177. const char *cmake::GetReleaseVersion()
  1178. {
  1179. return cmMakefile::GetReleaseVersion();
  1180. }
  1181. void cmake::AddCacheEntry(const char* key, const char* value,
  1182. const char* helpString,
  1183. int type)
  1184. {
  1185. m_CacheManager->AddCacheEntry(key, value,
  1186. helpString,
  1187. cmCacheManager::CacheEntryType(type));
  1188. }
  1189. const char* cmake::GetCacheDefinition(const char* name) const
  1190. {
  1191. return m_CacheManager->GetCacheValue(name);
  1192. }
  1193. int cmake::DumpDocumentationToFile(std::ostream& f)
  1194. {
  1195. // Loop over all registered commands and print out documentation
  1196. const char *name;
  1197. const char *terse;
  1198. const char *full;
  1199. char tmp[1024];
  1200. sprintf(tmp,"Version %d.%d", cmake::GetMajorVersion(),
  1201. cmake::GetMinorVersion());
  1202. f << "<html>\n";
  1203. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1204. f << "<ul>\n";
  1205. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  1206. j != m_Commands.end(); ++j)
  1207. {
  1208. name = (*j).second->GetName();
  1209. terse = (*j).second->GetTerseDocumentation();
  1210. full = (*j).second->GetFullDocumentation();
  1211. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1212. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1213. }
  1214. f << "</ul></html>\n";
  1215. return 1;
  1216. }
  1217. void cmake::AddDefaultCommands()
  1218. {
  1219. std::list<cmCommand*> commands;
  1220. GetPredefinedCommands(commands);
  1221. for(std::list<cmCommand*>::iterator i = commands.begin();
  1222. i != commands.end(); ++i)
  1223. {
  1224. this->AddCommand(*i);
  1225. }
  1226. }
  1227. void cmake::AddDefaultGenerators()
  1228. {
  1229. #if defined(_WIN32) && !defined(__CYGWIN__)
  1230. m_Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1231. &cmGlobalVisualStudio6Generator::New;
  1232. #if !defined(__MINGW32__)
  1233. m_Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1234. &cmGlobalVisualStudio7Generator::New;
  1235. m_Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1236. &cmGlobalVisualStudio71Generator::New;
  1237. #endif
  1238. m_Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1239. &cmGlobalBorlandMakefileGenerator::New;
  1240. m_Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1241. &cmGlobalNMakeMakefileGenerator::New;
  1242. #else
  1243. # if defined(__APPLE__) && defined(CMAKE_BUILD_WITH_CMAKE)
  1244. m_Generators[cmGlobalCodeWarriorGenerator::GetActualName()] =
  1245. &cmGlobalCodeWarriorGenerator::New;
  1246. # endif
  1247. #endif
  1248. m_Generators[cmGlobalUnixMakefileGenerator::GetActualName()] =
  1249. &cmGlobalUnixMakefileGenerator::New;
  1250. }
  1251. int cmake::LoadCache()
  1252. {
  1253. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1254. if (m_CMakeCommand.size() < 2)
  1255. {
  1256. cmSystemTools::Error("cmake command was not specified prior to loading the cache in cmake.cxx");
  1257. return -1;
  1258. }
  1259. // setup CMAKE_ROOT and CMAKE_COMMAND
  1260. if(!this->AddCMakePaths(m_CMakeCommand.c_str()))
  1261. {
  1262. return -3;
  1263. }
  1264. // set the default BACKWARDS compatibility to the current version
  1265. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1266. {
  1267. char ver[256];
  1268. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1269. cmMakefile::GetMinorVersion());
  1270. this->m_CacheManager->AddCacheEntry
  1271. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1272. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1273. cmCacheManager::STRING);
  1274. }
  1275. return 0;
  1276. }
  1277. void cmake::SetProgressCallback(ProgressCallback f, void *cd)
  1278. {
  1279. m_ProgressCallback = f;
  1280. m_ProgressCallbackClientData = cd;
  1281. }
  1282. void cmake::UpdateProgress(const char *msg, float prog)
  1283. {
  1284. if(m_ProgressCallback && !m_InTryCompile)
  1285. {
  1286. (*m_ProgressCallback)(msg, prog, m_ProgressCallbackClientData);
  1287. return;
  1288. }
  1289. }
  1290. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v) const
  1291. {
  1292. for(RegisteredCommandsMap::const_iterator j = m_Commands.begin();
  1293. j != m_Commands.end(); ++j)
  1294. {
  1295. cmDocumentationEntry e =
  1296. {
  1297. (*j).second->GetName(),
  1298. (*j).second->GetTerseDocumentation(),
  1299. (*j).second->GetFullDocumentation()
  1300. };
  1301. v.push_back(e);
  1302. }
  1303. cmDocumentationEntry empty = {0,0,0};
  1304. v.push_back(empty);
  1305. }
  1306. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1307. {
  1308. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  1309. i != m_Generators.end(); ++i)
  1310. {
  1311. cmDocumentationEntry e;
  1312. cmGlobalGenerator* generator = (i->second)();
  1313. generator->GetDocumentation(e);
  1314. delete generator;
  1315. v.push_back(e);
  1316. }
  1317. cmDocumentationEntry empty = {0,0,0};
  1318. v.push_back(empty);
  1319. }
  1320. void cmake::AddWrittenFile(const char* file)
  1321. {
  1322. m_WrittenFiles.insert(file);
  1323. }
  1324. bool cmake::HasWrittenFile(const char* file)
  1325. {
  1326. return m_WrittenFiles.find(file) != m_WrittenFiles.end();
  1327. }
  1328. void cmake::CleanupWrittenFiles()
  1329. {
  1330. m_WrittenFiles.clear();
  1331. }