cmake.cxx 31 KB

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