cmake.cxx 45 KB

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