cmake.cxx 29 KB

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