cmake.cxx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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* )
  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 && args[1].find("-G") != 0)
  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/CMakeDefaultMakeRuleVariables.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/CMakeDefaultMakeRuleVariables.cmake";
  396. }
  397. if (!cmSystemTools::FileExists(modules.c_str()))
  398. {
  399. // try exe/../share/cmake
  400. cMakeRoot += "/share/CMake";
  401. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.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/CMakeDefaultMakeRuleVariables.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/CMakeDefaultMakeRuleVariables.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/CMakeDefaultMakeRuleVariables.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/CMakeDefaultMakeRuleVariables.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. // restore the original environment variables CXX and CC
  652. // Restor CC
  653. static char envCC[5000];
  654. std::string env = "CC=";
  655. if(m_CCEnvironment)
  656. {
  657. env += m_CCEnvironment;
  658. }
  659. std::string::size_type size = env.size();
  660. if(size > 4999)
  661. {
  662. size = 4999;
  663. }
  664. strncpy(envCC, env.c_str(), size);
  665. envCC[4999] = 0;
  666. putenv(envCC);
  667. // Restore CXX
  668. static char envCXX[5000];
  669. env = "CXX=";
  670. if(m_CXXEnvironment)
  671. {
  672. env += m_CXXEnvironment;
  673. }
  674. size = env.size();
  675. if(size > 4999)
  676. {
  677. size = 4999;
  678. }
  679. strncpy(envCXX, env.c_str(), size);
  680. envCXX[4999] = 0;
  681. putenv(envCXX);
  682. }
  683. // set the new
  684. m_GlobalGenerator = gg;
  685. // Save the environment variables CXX and CC
  686. m_CXXEnvironment = getenv("CXX");
  687. m_CCEnvironment = getenv("CC");
  688. // set the cmake instance just to be sure
  689. gg->SetCMakeInstance(this);
  690. }
  691. int cmake::Configure()
  692. {
  693. // do a sanity check on some values
  694. if(m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  695. {
  696. std::string cacheStart =
  697. m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  698. cacheStart += "/CMakeLists.txt";
  699. std::string currentStart = this->GetHomeDirectory();
  700. currentStart += "/CMakeLists.txt";
  701. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  702. {
  703. std::string message = "Error: source : ";
  704. message += currentStart;
  705. message += "\nDoes not match source used to generate cache: ";
  706. message += cacheStart;
  707. message += "\nRe-run cmake with a different source directory.";
  708. cmSystemTools::Error(message.c_str());
  709. return -2;
  710. }
  711. }
  712. else
  713. {
  714. m_CacheManager->AddCacheEntry("CMAKE_HOME_DIRECTORY",
  715. this->GetHomeDirectory(),
  716. "Start directory with the top level CMakeLists.txt file for this project",
  717. cmCacheManager::INTERNAL);
  718. }
  719. // set the default BACKWARDS compatibility to the current version
  720. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  721. {
  722. char ver[256];
  723. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  724. cmMakefile::GetMinorVersion());
  725. this->m_CacheManager->AddCacheEntry
  726. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  727. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  728. cmCacheManager::STRING);
  729. }
  730. // no generator specified on the command line
  731. if(!m_GlobalGenerator)
  732. {
  733. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  734. if(genName)
  735. {
  736. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  737. }
  738. else
  739. {
  740. #if defined(__BORLANDC__) && defined(_WIN32)
  741. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  742. #elif defined(_WIN32) && !defined(__CYGWIN__)
  743. this->SetGlobalGenerator(new cmGlobalVisualStudio6Generator);
  744. #else
  745. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator);
  746. #endif
  747. }
  748. if(!m_GlobalGenerator)
  749. {
  750. cmSystemTools::Error("Could not create generator");
  751. return -1;
  752. }
  753. }
  754. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  755. if(genName)
  756. {
  757. if(strcmp(m_GlobalGenerator->GetName(), genName) != 0)
  758. {
  759. std::string message = "Error: generator : ";
  760. message += m_GlobalGenerator->GetName();
  761. message += "\nDoes not match the generator used previously: ";
  762. message += genName;
  763. message +=
  764. "\nEither remove the CMakeCache.txt file or choose a different"
  765. " binary directory.";
  766. cmSystemTools::Error(message.c_str());
  767. return -2;
  768. }
  769. }
  770. if(!m_CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  771. {
  772. m_CacheManager->AddCacheEntry("CMAKE_GENERATOR", m_GlobalGenerator->GetName(),
  773. "Name of generator.",
  774. cmCacheManager::INTERNAL);
  775. }
  776. // reset any system configuration information, except for when we are
  777. // InTryCompile. With TryCompile the system info is taken from the parent's
  778. // info to save time
  779. if (!m_InTryCompile)
  780. {
  781. m_GlobalGenerator->ClearEnabledLanguages();
  782. }
  783. // actually do the configure
  784. m_GlobalGenerator->Configure();
  785. // Before saving the cache
  786. // if the project did not define one of the entries below, add them now
  787. // so users can edit the values in the cache:
  788. // LIBRARY_OUTPUT_PATH
  789. // EXECUTABLE_OUTPUT_PATH
  790. if(!m_CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  791. {
  792. m_CacheManager->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  793. "Single output directory for building all libraries.",
  794. cmCacheManager::PATH);
  795. }
  796. if(!m_CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  797. {
  798. m_CacheManager->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  799. "Single output directory for building all executables.",
  800. cmCacheManager::PATH);
  801. }
  802. this->m_CacheManager->SaveCache(this->GetHomeOutputDirectory());
  803. if(cmSystemTools::GetErrorOccuredFlag())
  804. {
  805. return -1;
  806. }
  807. return 0;
  808. }
  809. bool cmake::CacheVersionMatches()
  810. {
  811. const char* majv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  812. const char* minv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  813. const char* relv = m_CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  814. bool cacheSameCMake = false;
  815. if(majv &&
  816. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  817. && minv &&
  818. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  819. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  820. {
  821. cacheSameCMake = true;
  822. }
  823. return cacheSameCMake;
  824. }
  825. // handle a command line invocation
  826. int cmake::Run(const std::vector<std::string>& args)
  827. {
  828. // Process the arguments
  829. this->SetArgs(args);
  830. // make sure rthe Start directory contains a CMakeLists.txt file
  831. std::string srcList = this->GetHomeDirectory();
  832. srcList += "/CMakeLists.txt";
  833. if(!cmSystemTools::FileExists(srcList.c_str()))
  834. {
  835. cmSystemTools::Error(
  836. "The source directory does not appear to contain CMakeLists.txt.\n"
  837. "Specify --help for usage.");
  838. return -1;
  839. }
  840. // set the cmake command
  841. m_CMakeCommand = args[0];
  842. // load the cache
  843. if(this->LoadCache() < 0)
  844. {
  845. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  846. return -1;
  847. }
  848. // Add any cache args
  849. this->SetCacheArgs(args);
  850. std::string systemFile = this->GetHomeOutputDirectory();
  851. systemFile += "/CMakeSystem.cmake";
  852. int ret = 0;
  853. // if not local or the cmake version has changed since the last run
  854. // of cmake, or CMakeSystem.cmake file is not in the root binary
  855. // directory, run a global generate
  856. if(!m_Local || !this->CacheVersionMatches() ||
  857. !cmSystemTools::FileExists(systemFile.c_str()) )
  858. {
  859. // If we are doing global generate, we better set start and start
  860. // output directory to the root of the project.
  861. std::string oldstartdir = this->GetStartDirectory();
  862. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  863. this->SetStartDirectory(this->GetHomeDirectory());
  864. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  865. bool saveLocalFlag = m_Local;
  866. m_Local = false;
  867. ret = this->Configure();
  868. if (ret)
  869. {
  870. return ret;
  871. }
  872. ret = this->Generate();
  873. if(ret)
  874. {
  875. return ret;
  876. }
  877. m_Local = saveLocalFlag;
  878. this->SetStartDirectory(oldstartdir.c_str());
  879. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  880. }
  881. // if we are local do the local thing
  882. if (m_Local)
  883. {
  884. ret = this->LocalGenerate();
  885. }
  886. return ret;
  887. }
  888. int cmake::Generate()
  889. {
  890. m_GlobalGenerator->Generate();
  891. if(cmSystemTools::GetErrorOccuredFlag())
  892. {
  893. return -1;
  894. }
  895. return 0;
  896. }
  897. int cmake::LocalGenerate()
  898. {
  899. // Read in the cache
  900. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  901. // create the generator based on the cache if it isn't already there
  902. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  903. if(genName)
  904. {
  905. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  906. }
  907. else
  908. {
  909. cmSystemTools::Error("Could local Generate called without the GENERATOR being specified in the CMakeCache");
  910. return -1;
  911. }
  912. // do the local generate
  913. m_GlobalGenerator->LocalGenerate();
  914. if(cmSystemTools::GetErrorOccuredFlag())
  915. {
  916. return -1;
  917. }
  918. return 0;
  919. }
  920. unsigned int cmake::GetMajorVersion()
  921. {
  922. return cmMakefile::GetMajorVersion();
  923. }
  924. unsigned int cmake::GetMinorVersion()
  925. {
  926. return cmMakefile::GetMinorVersion();
  927. }
  928. const char *cmake::GetReleaseVersion()
  929. {
  930. return cmMakefile::GetReleaseVersion();
  931. }
  932. void cmake::AddCacheEntry(const char* key, const char* value,
  933. const char* helpString,
  934. int type)
  935. {
  936. m_CacheManager->AddCacheEntry(key, value,
  937. helpString,
  938. cmCacheManager::CacheEntryType(type));
  939. }
  940. const char* cmake::GetCacheDefinition(const char* name) const
  941. {
  942. return m_CacheManager->GetCacheValue(name);
  943. }
  944. int cmake::DumpDocumentationToFile(std::ostream& f)
  945. {
  946. // Loop over all registered commands and print out documentation
  947. const char *name;
  948. const char *terse;
  949. const char *full;
  950. char tmp[1024];
  951. sprintf(tmp,"Version %d.%d", cmake::GetMajorVersion(),
  952. cmake::GetMinorVersion());
  953. f << "<html>\n";
  954. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  955. f << "<ul>\n";
  956. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  957. j != m_Commands.end(); ++j)
  958. {
  959. name = (*j).second->GetName();
  960. terse = (*j).second->GetTerseDocumentation();
  961. full = (*j).second->GetFullDocumentation();
  962. f << "<li><b>" << name << "</b> - " << terse << std::endl
  963. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  964. }
  965. f << "</ul></html>\n";
  966. return 1;
  967. }
  968. void cmake::AddDefaultCommands()
  969. {
  970. std::list<cmCommand*> commands;
  971. GetPredefinedCommands(commands);
  972. for(std::list<cmCommand*>::iterator i = commands.begin();
  973. i != commands.end(); ++i)
  974. {
  975. this->AddCommand(*i);
  976. }
  977. }
  978. int cmake::LoadCache()
  979. {
  980. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  981. if (m_CMakeCommand.size() < 2)
  982. {
  983. cmSystemTools::Error("cmake command was not specified prior to loading the cache in cmake.cxx");
  984. return -1;
  985. }
  986. // setup CMAKE_ROOT and CMAKE_COMMAND
  987. if(!this->AddCMakePaths(m_CMakeCommand.c_str()))
  988. {
  989. return -3;
  990. }
  991. // set the default BACKWARDS compatibility to the current version
  992. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  993. {
  994. char ver[256];
  995. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  996. cmMakefile::GetMinorVersion());
  997. this->m_CacheManager->AddCacheEntry
  998. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  999. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1000. cmCacheManager::STRING);
  1001. }
  1002. return 0;
  1003. }
  1004. void cmake::SetProgressCallback(ProgressCallback f, void *cd)
  1005. {
  1006. m_ProgressCallback = f;
  1007. m_ProgressCallbackClientData = cd;
  1008. }
  1009. void cmake::UpdateProgress(const char *msg, float prog)
  1010. {
  1011. if(m_ProgressCallback && !m_InTryCompile)
  1012. {
  1013. (*m_ProgressCallback)(msg, prog, m_ProgressCallbackClientData);
  1014. return;
  1015. }
  1016. }
  1017. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v) const
  1018. {
  1019. for(RegisteredCommandsMap::const_iterator j = m_Commands.begin();
  1020. j != m_Commands.end(); ++j)
  1021. {
  1022. cmDocumentationEntry e =
  1023. {
  1024. (*j).second->GetName(),
  1025. (*j).second->GetTerseDocumentation(),
  1026. (*j).second->GetFullDocumentation()
  1027. };
  1028. v.push_back(e);
  1029. }
  1030. cmDocumentationEntry empty = {0,0,0};
  1031. v.push_back(empty);
  1032. }