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