cmake.cxx 42 KB

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