cmake.cxx 34 KB

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