cmake.cxx 34 KB

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