cmake.cxx 47 KB

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