cmake.cxx 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmake.h"
  14. #include "time.h"
  15. #include "cmCacheManager.h"
  16. #include "cmMakefile.h"
  17. #include "cmLocalGenerator.h"
  18. #include "cmCommands.h"
  19. #include "cmCommand.h"
  20. #include "cmVariableWatch.h"
  21. // include the generator
  22. #if defined(_WIN32) && !defined(__CYGWIN__)
  23. #include "cmGlobalVisualStudio6Generator.h"
  24. #include "cmGlobalVisualStudio7Generator.h"
  25. #include "cmGlobalVisualStudio71Generator.h"
  26. #include "cmGlobalBorlandMakefileGenerator.h"
  27. #include "cmGlobalNMakeMakefileGenerator.h"
  28. #include "cmWin32ProcessExecution.h"
  29. #else
  30. #include "cmGlobalUnixMakefileGenerator.h"
  31. #endif
  32. #include <stdio.h>
  33. #include <stdlib.h> // required for atoi
  34. #ifdef __APPLE__
  35. #include <sys/types.h>
  36. #include <sys/time.h>
  37. #include <sys/resource.h>
  38. #if defined(CMAKE_BUILD_WITH_CMAKE)
  39. #include "cmGlobalCodeWarriorGenerator.h"
  40. #endif
  41. #endif
  42. void cmNeedBackwardsCompatibility(const std::string& variable,
  43. int access_type, void* )
  44. {
  45. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  46. {
  47. std::string message = "Error: An atttempt was made to access a variable: ";
  48. message += variable;
  49. message += " that has not been defined. Some variables were always defined by CMake in versions prior to 1.6. To fix this you might need to set the cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If you are writing a CMakeList file, (or have already set CMAKE_BACKWARDS_COMPATABILITY to 1.4 or less) then you probably need to include a CMake module to test for the feature this variable defines.";
  50. cmSystemTools::Error(message.c_str());
  51. }
  52. }
  53. cmake::cmake()
  54. {
  55. #ifdef __APPLE__
  56. struct rlimit rlp;
  57. if(!getrlimit(RLIMIT_STACK, &rlp))
  58. {
  59. if(rlp.rlim_cur != rlp.rlim_max)
  60. {
  61. rlp.rlim_cur = rlp.rlim_max;
  62. setrlimit(RLIMIT_STACK, &rlp);
  63. }
  64. }
  65. #endif
  66. // If MAKEFLAGS are given in the environment, remove the environment
  67. // variable. This will prevent try-compile from succeeding when it
  68. // should fail (if "-i" is an option). We cannot simply test
  69. // whether "-i" is given and remove it because some make programs
  70. // encode the MAKEFLAGS variable in a strange way.
  71. if(getenv("MAKEFLAGS"))
  72. {
  73. static char makeflags[] = "MAKEFLAGS=";
  74. putenv(makeflags);
  75. }
  76. m_Local = false;
  77. m_Verbose = false;
  78. m_InTryCompile = false;
  79. m_CacheManager = new cmCacheManager;
  80. m_GlobalGenerator = 0;
  81. m_ProgressCallback = 0;
  82. m_ProgressCallbackClientData = 0;
  83. m_VariableWatch = new cmVariableWatch;
  84. this->AddDefaultGenerators();
  85. this->AddDefaultCommands();
  86. m_VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  87. cmNeedBackwardsCompatibility);
  88. m_VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  89. cmNeedBackwardsCompatibility);
  90. m_VariableWatch->AddWatch("CMAKE_X_LIBS",
  91. cmNeedBackwardsCompatibility);
  92. }
  93. cmake::~cmake()
  94. {
  95. delete m_CacheManager;
  96. if (m_GlobalGenerator)
  97. {
  98. delete m_GlobalGenerator;
  99. m_GlobalGenerator = 0;
  100. }
  101. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  102. j != m_Commands.end(); ++j)
  103. {
  104. delete (*j).second;
  105. }
  106. delete m_VariableWatch;
  107. }
  108. bool cmake::CommandExists(const char* name) const
  109. {
  110. return (m_Commands.find(name) != m_Commands.end());
  111. }
  112. cmCommand *cmake::GetCommand(const char *name)
  113. {
  114. cmCommand* rm = 0;
  115. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  116. if (pos != m_Commands.end())
  117. {
  118. rm = (*pos).second;
  119. }
  120. return rm;
  121. }
  122. void cmake::AddCommand(cmCommand* wg)
  123. {
  124. std::string name = wg->GetName();
  125. // if the command already exists, free the old one
  126. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  127. if (pos != m_Commands.end())
  128. {
  129. delete pos->second;
  130. m_Commands.erase(pos);
  131. }
  132. m_Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  133. }
  134. void cmake::Usage(const char* program)
  135. {
  136. cmOStringStream errorStream;
  137. errorStream << "cmake version " << cmMakefile::GetMajorVersion()
  138. << "." << cmMakefile::GetMinorVersion() << "\n";
  139. errorStream << "Usage: " << program << " [srcdir] [options]\n"
  140. << "Where cmake is run from the directory where you want the object files written. If srcdir is not specified, the current directory is used for both source and object files.\n";
  141. errorStream << "Options are:\n";
  142. errorStream << "\n-i (puts cmake in wizard mode, not available for ccmake)\n";
  143. errorStream << "\n-DVAR:TYPE=VALUE (create a cache file entry)\n";
  144. errorStream << "\n-Cpath_to_initial_cache (a cmake list file that is used to pre-load the cache with values.)\n";
  145. errorStream << "\n[-GgeneratorName] (where generator name can be one of these: ";
  146. std::vector<std::string> names;
  147. this->GetRegisteredGenerators(names);
  148. for(std::vector<std::string>::iterator i =names.begin();
  149. i != names.end(); ++i)
  150. {
  151. errorStream << "\"" << i->c_str() << "\" ";
  152. }
  153. errorStream << ")\n";
  154. cmSystemTools::Error(errorStream.str().c_str());
  155. }
  156. // Parse the args
  157. void cmake::SetCacheArgs(const std::vector<std::string>& args)
  158. {
  159. for(unsigned int i=1; i < args.size(); ++i)
  160. {
  161. std::string arg = args[i];
  162. if(arg.find("-D",0) == 0)
  163. {
  164. std::string entry = arg.substr(2);
  165. std::string var, value;
  166. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  167. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  168. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  169. {
  170. this->m_CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  171. "No help, variable specified on the command line.",
  172. type);
  173. }
  174. else
  175. {
  176. std::cerr << "Parse error in command line argument: " << arg << "\n"
  177. << "Should be: VAR:type=value\n";
  178. }
  179. }
  180. else if(arg.find("-C",0) == 0)
  181. {
  182. std::string path = arg.substr(2);
  183. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  184. this->ReadListFile(path.c_str());
  185. }
  186. }
  187. }
  188. void cmake::ReadListFile(const char *path)
  189. {
  190. // if a generator was not yet created, temporarily create one
  191. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  192. bool created = false;
  193. // if a generator was not specified use a generic one
  194. if (!gg)
  195. {
  196. gg = new cmGlobalGenerator;
  197. gg->SetCMakeInstance(this);
  198. created = true;
  199. }
  200. // read in the list file to fill the cache
  201. if(path)
  202. {
  203. cmLocalGenerator *lg = gg->CreateLocalGenerator();
  204. lg->SetGlobalGenerator(gg);
  205. if (!lg->GetMakefile()->ReadListFile(0, path))
  206. {
  207. std::cerr << "Error in reading cmake initial cache file:"
  208. << path << "\n";
  209. }
  210. }
  211. // free generic one if generated
  212. if (created)
  213. {
  214. delete gg;
  215. }
  216. }
  217. // Parse the args
  218. void cmake::SetArgs(const std::vector<std::string>& args)
  219. {
  220. m_Local = false;
  221. bool directoriesSet = false;
  222. // watch for cmake and cmake srcdir invocations
  223. if (args.size() <= 2)
  224. {
  225. directoriesSet = true;
  226. this->SetHomeOutputDirectory
  227. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  228. this->SetStartOutputDirectory
  229. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  230. if (args.size() == 2 && args[1].find("-G") != 0)
  231. {
  232. this->SetHomeDirectory
  233. (cmSystemTools::CollapseFullPath(args[1].c_str()).c_str());
  234. this->SetStartDirectory
  235. (cmSystemTools::CollapseFullPath(args[1].c_str()).c_str());
  236. }
  237. else
  238. {
  239. this->SetHomeDirectory
  240. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  241. this->SetStartDirectory
  242. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  243. }
  244. }
  245. for(unsigned int i=1; i < args.size(); ++i)
  246. {
  247. std::string arg = args[i];
  248. if(arg.find("-H",0) == 0)
  249. {
  250. directoriesSet = true;
  251. std::string path = arg.substr(2);
  252. this->SetHomeDirectory(path.c_str());
  253. }
  254. else if(arg.find("-S",0) == 0)
  255. {
  256. directoriesSet = true;
  257. m_Local = true;
  258. std::string path = arg.substr(2);
  259. this->SetStartDirectory(path.c_str());
  260. }
  261. else if(arg.find("-O",0) == 0)
  262. {
  263. directoriesSet = true;
  264. std::string path = arg.substr(2);
  265. this->SetStartOutputDirectory(path.c_str());
  266. }
  267. else if(arg.find("-B",0) == 0)
  268. {
  269. directoriesSet = true;
  270. std::string path = arg.substr(2);
  271. this->SetHomeOutputDirectory(path.c_str());
  272. }
  273. else if(arg.find("-V",0) == 0)
  274. {
  275. m_Verbose = true;
  276. }
  277. else if(arg.find("-D",0) == 0)
  278. {
  279. // skip for now
  280. }
  281. else if(arg.find("-C",0) == 0)
  282. {
  283. // skip for now
  284. }
  285. else if(arg.find("-G",0) == 0)
  286. {
  287. std::string value = arg.substr(2);
  288. cmGlobalGenerator* gen =
  289. this->CreateGlobalGenerator(value.c_str());
  290. if(!gen)
  291. {
  292. cmSystemTools::Error("Could not create named generator ",
  293. value.c_str());
  294. }
  295. else
  296. {
  297. this->SetGlobalGenerator(gen);
  298. }
  299. }
  300. // no option assume it is the path to the source
  301. else
  302. {
  303. directoriesSet = true;
  304. this->SetHomeOutputDirectory
  305. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  306. this->SetStartOutputDirectory
  307. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  308. this->SetHomeDirectory
  309. (cmSystemTools::CollapseFullPath(arg.c_str()).c_str());
  310. this->SetStartDirectory
  311. (cmSystemTools::CollapseFullPath(arg.c_str()).c_str());
  312. }
  313. }
  314. if(!directoriesSet)
  315. {
  316. this->SetHomeOutputDirectory
  317. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  318. this->SetStartOutputDirectory
  319. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  320. this->SetHomeDirectory
  321. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  322. this->SetStartDirectory
  323. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  324. }
  325. if (!m_Local)
  326. {
  327. this->SetStartDirectory(this->GetHomeDirectory());
  328. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  329. }
  330. }
  331. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the cache
  332. int cmake::AddCMakePaths(const char *arg0)
  333. {
  334. // Find our own executable.
  335. std::vector<cmStdString> failures;
  336. std::string cMakeSelf = arg0;
  337. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  338. failures.push_back(cMakeSelf);
  339. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  340. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  341. {
  342. #ifdef CMAKE_BUILD_DIR
  343. std::string intdir = ".";
  344. #ifdef CMAKE_INTDIR
  345. intdir = CMAKE_INTDIR;
  346. #endif
  347. cMakeSelf = CMAKE_BUILD_DIR;
  348. cMakeSelf += "/bin/";
  349. cMakeSelf += intdir;
  350. cMakeSelf += "/cmake";
  351. cMakeSelf += cmSystemTools::GetExecutableExtension();
  352. #endif
  353. }
  354. #ifdef CMAKE_PREFIX
  355. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  356. {
  357. failures.push_back(cMakeSelf);
  358. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  359. }
  360. #endif
  361. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  362. {
  363. failures.push_back(cMakeSelf);
  364. cmOStringStream msg;
  365. msg << "CMAKE can not find the command line program cmake.\n";
  366. msg << " argv[0] = \"" << arg0 << "\"\n";
  367. msg << " Attempted paths:\n";
  368. std::vector<cmStdString>::iterator i;
  369. for(i=failures.begin(); i != failures.end(); ++i)
  370. {
  371. msg << " \"" << i->c_str() << "\"\n";
  372. }
  373. cmSystemTools::Error(msg.str().c_str());
  374. return 0;
  375. }
  376. // Save the value in the cache
  377. this->m_CacheManager->AddCacheEntry
  378. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  379. cmCacheManager::INTERNAL);
  380. // Find and save the command to edit the cache
  381. std::string editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  382. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  383. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  384. {
  385. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  386. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  387. }
  388. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  389. {
  390. this->m_CacheManager->AddCacheEntry
  391. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  392. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  393. }
  394. // do CMAKE_ROOT, look for the environment variable first
  395. std::string cMakeRoot;
  396. std::string modules;
  397. if (getenv("CMAKE_ROOT"))
  398. {
  399. cMakeRoot = getenv("CMAKE_ROOT");
  400. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  401. }
  402. if(!cmSystemTools::FileExists(modules.c_str()))
  403. {
  404. // next try exe/..
  405. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  406. std::string::size_type slashPos = cMakeRoot.rfind("/");
  407. if(slashPos != std::string::npos)
  408. {
  409. cMakeRoot = cMakeRoot.substr(0, slashPos);
  410. }
  411. // is there no Modules direcory there?
  412. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  413. }
  414. if (!cmSystemTools::FileExists(modules.c_str()))
  415. {
  416. // try exe/../share/cmake
  417. cMakeRoot += CMAKE_DATA_DIR;
  418. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  419. }
  420. #ifdef CMAKE_ROOT_DIR
  421. if (!cmSystemTools::FileExists(modules.c_str()))
  422. {
  423. // try compiled in root directory
  424. cMakeRoot = CMAKE_ROOT_DIR;
  425. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  426. }
  427. #endif
  428. #ifdef CMAKE_PREFIX
  429. if (!cmSystemTools::FileExists(modules.c_str()))
  430. {
  431. // try compiled in install prefix
  432. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  433. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  434. }
  435. #endif
  436. if (!cmSystemTools::FileExists(modules.c_str()))
  437. {
  438. // try
  439. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  440. cMakeRoot += CMAKE_DATA_DIR;
  441. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  442. }
  443. if(!cmSystemTools::FileExists(modules.c_str()))
  444. {
  445. // next try exe
  446. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  447. // is there no Modules direcory there?
  448. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  449. }
  450. if (!cmSystemTools::FileExists(modules.c_str()))
  451. {
  452. // couldn't find modules
  453. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n",
  454. "Modules directory not in directory:\n",
  455. modules.c_str());
  456. return 0;
  457. }
  458. this->m_CacheManager->AddCacheEntry
  459. ("CMAKE_ROOT", cMakeRoot.c_str(),
  460. "Path to CMake installation.", cmCacheManager::INTERNAL);
  461. #ifdef _WIN32
  462. std::string comspec = "cmw9xcom.exe";
  463. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  464. #endif
  465. return 1;
  466. }
  467. void CMakeCommandUsage(const char* program)
  468. {
  469. cmOStringStream errorStream;
  470. errorStream
  471. << "cmake version " << cmMakefile::GetMajorVersion()
  472. << "." << cmMakefile::GetMinorVersion() << "\n";
  473. errorStream
  474. << "Usage: " << program << " -E [command] [arguments ...]\n"
  475. << "Available commands: \n"
  476. << " chdir dir cmd [args]... - run command in a given directory\n"
  477. << " copy file destination - copy file to destination (either file or directory)\n"
  478. << " copy_if_different in-file out-file - copy file if input has changed\n"
  479. << " echo [string]... - displays arguments as text\n"
  480. << " remove file1 file2 ... - remove the file(s)\n"
  481. << " time command [args] ... - run command and return elapsed time\n";
  482. #if defined(_WIN32) && !defined(__CYGWIN__)
  483. errorStream
  484. << " write_regv key value - write registry value\n"
  485. << " delete_regv key - delete registry value\n"
  486. << " comspec - on windows 9x use this for RunCommand\n";
  487. #endif
  488. cmSystemTools::Error(errorStream.str().c_str());
  489. }
  490. int cmake::CMakeCommand(std::vector<std::string>& args)
  491. {
  492. if (args.size() > 1)
  493. {
  494. // Copy file
  495. if (args[1] == "copy" && args.size() == 4)
  496. {
  497. cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str());
  498. return cmSystemTools::GetErrorOccuredFlag();
  499. }
  500. // Copy file if different.
  501. if (args[1] == "copy_if_different" && args.size() == 4)
  502. {
  503. cmSystemTools::CopyFileIfDifferent(args[2].c_str(), args[3].c_str());
  504. return cmSystemTools::GetErrorOccuredFlag();
  505. }
  506. // Echo string
  507. else if (args[1] == "echo" )
  508. {
  509. unsigned int cc;
  510. for ( cc = 2; cc < args.size(); cc ++ )
  511. {
  512. std::cout << args[cc] << " ";
  513. }
  514. std::cout << std::endl;
  515. return 0;
  516. }
  517. // Remove file
  518. else if (args[1] == "remove" && args.size() > 2)
  519. {
  520. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  521. {
  522. if(args[cc] != "-f")
  523. {
  524. if(args[cc] == "\\-f")
  525. {
  526. args[cc] = "-f";
  527. }
  528. cmSystemTools::RemoveFile(args[cc].c_str());
  529. }
  530. }
  531. return 0;
  532. }
  533. // Clock command
  534. else if (args[1] == "time" && args.size() > 2)
  535. {
  536. std::string command = args[2];
  537. std::string output;
  538. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  539. {
  540. command += " ";
  541. command += args[cc];
  542. }
  543. clock_t clock_start, clock_finish;
  544. time_t time_start, time_finish;
  545. time(&time_start);
  546. clock_start = clock();
  547. cmSystemTools::RunSingleCommand(command.c_str());
  548. clock_finish = clock();
  549. time(&time_finish);
  550. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  551. std::cout << "Elapsed time: "
  552. << (long)(time_finish - time_start) << " s. (time)"
  553. << ", "
  554. << (double)(clock_finish - clock_start) / clocks_per_sec
  555. << " s. (clock)"
  556. << "\n";
  557. return 0;
  558. }
  559. // Clock command
  560. else if (args[1] == "chdir" && args.size() >= 4)
  561. {
  562. std::string directory = args[2];
  563. std::string command = args[3];
  564. std::string output;
  565. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  566. {
  567. command += " ";
  568. command += args[cc];
  569. }
  570. int retval = 0;
  571. int timeout = 0;
  572. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  573. directory.c_str(), true, timeout) )
  574. {
  575. return retval;
  576. }
  577. return 1;
  578. }
  579. #if defined(_WIN32) && !defined(__CYGWIN__)
  580. // Write registry value
  581. else if (args[1] == "write_regv" && args.size() > 3)
  582. {
  583. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  584. args[3].c_str()) ? 0 : 1;
  585. }
  586. // Delete registry value
  587. else if (args[1] == "delete_regv" && args.size() > 2)
  588. {
  589. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  590. }
  591. // Remove file
  592. else if (args[1] == "comspec" && args.size() > 2)
  593. {
  594. unsigned int cc;
  595. std::string command = args[2];
  596. for ( cc = 3; cc < args.size(); cc ++ )
  597. {
  598. command += " " + args[cc];
  599. }
  600. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  601. }
  602. #endif
  603. }
  604. ::CMakeCommandUsage(args[0].c_str());
  605. return 1;
  606. }
  607. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  608. {
  609. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  610. i != m_Generators.end(); ++i)
  611. {
  612. names.push_back(i->first);
  613. }
  614. }
  615. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  616. {
  617. RegisteredGeneratorsMap::const_iterator i = m_Generators.find(name);
  618. if(i != m_Generators.end())
  619. {
  620. cmGlobalGenerator* generator = (i->second)();
  621. generator->SetCMakeInstance(this);
  622. return generator;
  623. }
  624. else
  625. {
  626. return 0;
  627. }
  628. }
  629. void cmake::SetHomeDirectory(const char* dir)
  630. {
  631. m_cmHomeDirectory = dir;
  632. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  633. }
  634. void cmake::SetHomeOutputDirectory(const char* lib)
  635. {
  636. m_HomeOutputDirectory = lib;
  637. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  638. }
  639. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  640. {
  641. // delete the old generator
  642. if (m_GlobalGenerator)
  643. {
  644. delete m_GlobalGenerator;
  645. // restore the original environment variables CXX and CC
  646. // Restor CC
  647. static char envCC[5000];
  648. std::string env = "CC=";
  649. if(m_CCEnvironment)
  650. {
  651. env += m_CCEnvironment;
  652. }
  653. std::string::size_type size = env.size();
  654. if(size > 4999)
  655. {
  656. size = 4999;
  657. }
  658. strncpy(envCC, env.c_str(), size);
  659. envCC[4999] = 0;
  660. putenv(envCC);
  661. // Restore CXX
  662. static char envCXX[5000];
  663. env = "CXX=";
  664. if(m_CXXEnvironment)
  665. {
  666. env += m_CXXEnvironment;
  667. }
  668. size = env.size();
  669. if(size > 4999)
  670. {
  671. size = 4999;
  672. }
  673. strncpy(envCXX, env.c_str(), size);
  674. envCXX[4999] = 0;
  675. putenv(envCXX);
  676. }
  677. // set the new
  678. m_GlobalGenerator = gg;
  679. // Save the environment variables CXX and CC
  680. m_CXXEnvironment = getenv("CXX");
  681. m_CCEnvironment = getenv("CC");
  682. // set the cmake instance just to be sure
  683. gg->SetCMakeInstance(this);
  684. }
  685. int cmake::DoPreConfigureChecks()
  686. {
  687. // Make sure the Start directory contains a CMakeLists.txt file.
  688. std::string srcList = this->GetHomeDirectory();
  689. srcList += "/CMakeLists.txt";
  690. if(!cmSystemTools::FileExists(srcList.c_str()))
  691. {
  692. cmSystemTools::Error(
  693. "The source directory does not appear to contain CMakeLists.txt.\n"
  694. "Specify --help for usage, or press the help button on the CMake GUI.");
  695. return -2;
  696. }
  697. // do a sanity check on some values
  698. if(m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  699. {
  700. std::string cacheStart =
  701. m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  702. cacheStart += "/CMakeLists.txt";
  703. std::string currentStart = this->GetHomeDirectory();
  704. currentStart += "/CMakeLists.txt";
  705. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  706. {
  707. std::string message = "Error: source : ";
  708. message += currentStart;
  709. message += "\nDoes not match source used to generate cache: ";
  710. message += cacheStart;
  711. message += "\nRe-run cmake with a different source directory.";
  712. cmSystemTools::Error(message.c_str());
  713. return -2;
  714. }
  715. }
  716. else
  717. {
  718. return 0;
  719. }
  720. return 1;
  721. }
  722. int cmake::Configure()
  723. {
  724. int res = this->DoPreConfigureChecks();
  725. if ( res < 0 )
  726. {
  727. return -2;
  728. }
  729. if ( !res )
  730. {
  731. m_CacheManager->AddCacheEntry("CMAKE_HOME_DIRECTORY",
  732. this->GetHomeDirectory(),
  733. "Start directory with the top level CMakeLists.txt file for this project",
  734. cmCacheManager::INTERNAL);
  735. }
  736. // set the default BACKWARDS compatibility to the current version
  737. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  738. {
  739. char ver[256];
  740. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  741. cmMakefile::GetMinorVersion());
  742. this->m_CacheManager->AddCacheEntry
  743. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  744. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  745. cmCacheManager::STRING);
  746. }
  747. // no generator specified on the command line
  748. if(!m_GlobalGenerator)
  749. {
  750. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  751. if(genName)
  752. {
  753. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  754. }
  755. else
  756. {
  757. #if defined(__BORLANDC__) && defined(_WIN32)
  758. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  759. #elif defined(_WIN32) && !defined(__CYGWIN__)
  760. this->SetGlobalGenerator(new cmGlobalVisualStudio6Generator);
  761. #else
  762. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator);
  763. #endif
  764. }
  765. if(!m_GlobalGenerator)
  766. {
  767. cmSystemTools::Error("Could not create generator");
  768. return -1;
  769. }
  770. }
  771. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  772. if(genName)
  773. {
  774. if(strcmp(m_GlobalGenerator->GetName(), genName) != 0)
  775. {
  776. std::string message = "Error: generator : ";
  777. message += m_GlobalGenerator->GetName();
  778. message += "\nDoes not match the generator used previously: ";
  779. message += genName;
  780. message +=
  781. "\nEither remove the CMakeCache.txt file or choose a different"
  782. " binary directory.";
  783. cmSystemTools::Error(message.c_str());
  784. return -2;
  785. }
  786. }
  787. if(!m_CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  788. {
  789. m_CacheManager->AddCacheEntry("CMAKE_GENERATOR", m_GlobalGenerator->GetName(),
  790. "Name of generator.",
  791. cmCacheManager::INTERNAL);
  792. }
  793. // reset any system configuration information, except for when we are
  794. // InTryCompile. With TryCompile the system info is taken from the parent's
  795. // info to save time
  796. if (!m_InTryCompile)
  797. {
  798. m_GlobalGenerator->ClearEnabledLanguages();
  799. }
  800. // actually do the configure
  801. m_GlobalGenerator->Configure();
  802. // Before saving the cache
  803. // if the project did not define one of the entries below, add them now
  804. // so users can edit the values in the cache:
  805. // LIBRARY_OUTPUT_PATH
  806. // EXECUTABLE_OUTPUT_PATH
  807. if(!m_CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  808. {
  809. m_CacheManager->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  810. "Single output directory for building all libraries.",
  811. cmCacheManager::PATH);
  812. }
  813. if(!m_CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  814. {
  815. m_CacheManager->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  816. "Single output directory for building all executables.",
  817. cmCacheManager::PATH);
  818. }
  819. if(cmSystemTools::GetFatalErrorOccured() &&
  820. (!this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  821. cmSystemTools::IsOff(this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  822. {
  823. // We must have a bad generator selection. Wipe the cache entry so the
  824. // user can select another.
  825. m_CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  826. }
  827. this->m_CacheManager->SaveCache(this->GetHomeOutputDirectory());
  828. if(cmSystemTools::GetErrorOccuredFlag())
  829. {
  830. return -1;
  831. }
  832. return 0;
  833. }
  834. bool cmake::CacheVersionMatches()
  835. {
  836. const char* majv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  837. const char* minv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  838. const char* relv = m_CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  839. bool cacheSameCMake = false;
  840. if(majv &&
  841. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  842. && minv &&
  843. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  844. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  845. {
  846. cacheSameCMake = true;
  847. }
  848. return cacheSameCMake;
  849. }
  850. // handle a command line invocation
  851. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  852. {
  853. // Process the arguments
  854. this->SetArgs(args);
  855. // set the cmake command
  856. m_CMakeCommand = args[0];
  857. // load the cache
  858. if(this->LoadCache() < 0)
  859. {
  860. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  861. return -1;
  862. }
  863. // Add any cache args
  864. this->SetCacheArgs(args);
  865. std::string systemFile = this->GetHomeOutputDirectory();
  866. systemFile += "/CMakeSystem.cmake";
  867. if ( noconfigure )
  868. {
  869. return 0;
  870. }
  871. int ret = 0;
  872. // if not local or the cmake version has changed since the last run
  873. // of cmake, or CMakeSystem.cmake file is not in the root binary
  874. // directory, run a global generate
  875. if(!m_Local || !this->CacheVersionMatches() ||
  876. !cmSystemTools::FileExists(systemFile.c_str()) )
  877. {
  878. // If we are doing global generate, we better set start and start
  879. // output directory to the root of the project.
  880. std::string oldstartdir = this->GetStartDirectory();
  881. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  882. this->SetStartDirectory(this->GetHomeDirectory());
  883. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  884. bool saveLocalFlag = m_Local;
  885. m_Local = false;
  886. ret = this->Configure();
  887. if (ret)
  888. {
  889. return ret;
  890. }
  891. ret = this->Generate();
  892. if(ret)
  893. {
  894. return ret;
  895. }
  896. m_Local = saveLocalFlag;
  897. this->SetStartDirectory(oldstartdir.c_str());
  898. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  899. }
  900. // if we are local do the local thing
  901. if (m_Local)
  902. {
  903. ret = this->LocalGenerate();
  904. }
  905. return ret;
  906. }
  907. int cmake::Generate()
  908. {
  909. m_GlobalGenerator->Generate();
  910. if(cmSystemTools::GetErrorOccuredFlag())
  911. {
  912. return -1;
  913. }
  914. return 0;
  915. }
  916. int cmake::LocalGenerate()
  917. {
  918. // Read in the cache
  919. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  920. // create the generator based on the cache if it isn't already there
  921. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  922. if(genName)
  923. {
  924. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  925. }
  926. else
  927. {
  928. cmSystemTools::Error("Could local Generate called without the GENERATOR being specified in the CMakeCache");
  929. return -1;
  930. }
  931. // do the local generate
  932. m_GlobalGenerator->LocalGenerate();
  933. if(cmSystemTools::GetErrorOccuredFlag())
  934. {
  935. return -1;
  936. }
  937. return 0;
  938. }
  939. unsigned int cmake::GetMajorVersion()
  940. {
  941. return cmMakefile::GetMajorVersion();
  942. }
  943. unsigned int cmake::GetMinorVersion()
  944. {
  945. return cmMakefile::GetMinorVersion();
  946. }
  947. const char *cmake::GetReleaseVersion()
  948. {
  949. return cmMakefile::GetReleaseVersion();
  950. }
  951. void cmake::AddCacheEntry(const char* key, const char* value,
  952. const char* helpString,
  953. int type)
  954. {
  955. m_CacheManager->AddCacheEntry(key, value,
  956. helpString,
  957. cmCacheManager::CacheEntryType(type));
  958. }
  959. const char* cmake::GetCacheDefinition(const char* name) const
  960. {
  961. return m_CacheManager->GetCacheValue(name);
  962. }
  963. int cmake::DumpDocumentationToFile(std::ostream& f)
  964. {
  965. // Loop over all registered commands and print out documentation
  966. const char *name;
  967. const char *terse;
  968. const char *full;
  969. char tmp[1024];
  970. sprintf(tmp,"Version %d.%d", cmake::GetMajorVersion(),
  971. cmake::GetMinorVersion());
  972. f << "<html>\n";
  973. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  974. f << "<ul>\n";
  975. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  976. j != m_Commands.end(); ++j)
  977. {
  978. name = (*j).second->GetName();
  979. terse = (*j).second->GetTerseDocumentation();
  980. full = (*j).second->GetFullDocumentation();
  981. f << "<li><b>" << name << "</b> - " << terse << std::endl
  982. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  983. }
  984. f << "</ul></html>\n";
  985. return 1;
  986. }
  987. void cmake::AddDefaultCommands()
  988. {
  989. std::list<cmCommand*> commands;
  990. GetPredefinedCommands(commands);
  991. for(std::list<cmCommand*>::iterator i = commands.begin();
  992. i != commands.end(); ++i)
  993. {
  994. this->AddCommand(*i);
  995. }
  996. }
  997. void cmake::AddDefaultGenerators()
  998. {
  999. #if defined(_WIN32) && !defined(__CYGWIN__)
  1000. m_Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1001. &cmGlobalVisualStudio6Generator::New;
  1002. m_Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1003. &cmGlobalVisualStudio7Generator::New;
  1004. m_Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1005. &cmGlobalVisualStudio71Generator::New;
  1006. m_Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1007. &cmGlobalBorlandMakefileGenerator::New;
  1008. m_Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1009. &cmGlobalNMakeMakefileGenerator::New;
  1010. #else
  1011. # if defined(__APPLE__) && defined(CMAKE_BUILD_WITH_CMAKE)
  1012. m_Generators[cmGlobalCodeWarriorGenerator::GetActualName()] =
  1013. &cmGlobalCodeWarriorGenerator::New;
  1014. # endif
  1015. m_Generators[cmGlobalUnixMakefileGenerator::GetActualName()] =
  1016. &cmGlobalUnixMakefileGenerator::New;
  1017. #endif
  1018. }
  1019. int cmake::LoadCache()
  1020. {
  1021. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1022. if (m_CMakeCommand.size() < 2)
  1023. {
  1024. cmSystemTools::Error("cmake command was not specified prior to loading the cache in cmake.cxx");
  1025. return -1;
  1026. }
  1027. // setup CMAKE_ROOT and CMAKE_COMMAND
  1028. if(!this->AddCMakePaths(m_CMakeCommand.c_str()))
  1029. {
  1030. return -3;
  1031. }
  1032. // set the default BACKWARDS compatibility to the current version
  1033. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1034. {
  1035. char ver[256];
  1036. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1037. cmMakefile::GetMinorVersion());
  1038. this->m_CacheManager->AddCacheEntry
  1039. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1040. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1041. cmCacheManager::STRING);
  1042. }
  1043. return 0;
  1044. }
  1045. void cmake::SetProgressCallback(ProgressCallback f, void *cd)
  1046. {
  1047. m_ProgressCallback = f;
  1048. m_ProgressCallbackClientData = cd;
  1049. }
  1050. void cmake::UpdateProgress(const char *msg, float prog)
  1051. {
  1052. if(m_ProgressCallback && !m_InTryCompile)
  1053. {
  1054. (*m_ProgressCallback)(msg, prog, m_ProgressCallbackClientData);
  1055. return;
  1056. }
  1057. }
  1058. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v) const
  1059. {
  1060. for(RegisteredCommandsMap::const_iterator j = m_Commands.begin();
  1061. j != m_Commands.end(); ++j)
  1062. {
  1063. cmDocumentationEntry e =
  1064. {
  1065. (*j).second->GetName(),
  1066. (*j).second->GetTerseDocumentation(),
  1067. (*j).second->GetFullDocumentation()
  1068. };
  1069. v.push_back(e);
  1070. }
  1071. cmDocumentationEntry empty = {0,0,0};
  1072. v.push_back(empty);
  1073. }
  1074. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1075. {
  1076. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  1077. i != m_Generators.end(); ++i)
  1078. {
  1079. cmDocumentationEntry e;
  1080. cmGlobalGenerator* generator = (i->second)();
  1081. generator->GetDocumentation(e);
  1082. delete generator;
  1083. v.push_back(e);
  1084. }
  1085. cmDocumentationEntry empty = {0,0,0};
  1086. v.push_back(empty);
  1087. }