cmake.cxx 49 KB

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