cmake.cxx 33 KB

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