cmake.cxx 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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)
  214. {
  215. this->SetHomeDirectory
  216. (cmSystemTools::CollapseFullPath(args[1].c_str()).c_str());
  217. this->SetStartDirectory
  218. (cmSystemTools::CollapseFullPath(args[1].c_str()).c_str());
  219. }
  220. else
  221. {
  222. this->SetHomeDirectory
  223. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  224. this->SetStartDirectory
  225. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  226. }
  227. }
  228. for(unsigned int i=1; i < args.size(); ++i)
  229. {
  230. std::string arg = args[i];
  231. if(arg.find("-H",0) == 0)
  232. {
  233. directoriesSet = true;
  234. std::string path = arg.substr(2);
  235. this->SetHomeDirectory(path.c_str());
  236. }
  237. else if(arg.find("-S",0) == 0)
  238. {
  239. directoriesSet = true;
  240. m_Local = true;
  241. std::string path = arg.substr(2);
  242. this->SetStartDirectory(path.c_str());
  243. }
  244. else if(arg.find("-O",0) == 0)
  245. {
  246. directoriesSet = true;
  247. std::string path = arg.substr(2);
  248. this->SetStartOutputDirectory(path.c_str());
  249. }
  250. else if(arg.find("-B",0) == 0)
  251. {
  252. directoriesSet = true;
  253. std::string path = arg.substr(2);
  254. this->SetHomeOutputDirectory(path.c_str());
  255. }
  256. else if(arg.find("-V",0) == 0)
  257. {
  258. m_Verbose = true;
  259. }
  260. else if(arg.find("-D",0) == 0)
  261. {
  262. // skip for now
  263. }
  264. else if(arg.find("-C",0) == 0)
  265. {
  266. // skip for now
  267. }
  268. else if(arg.find("-G",0) == 0)
  269. {
  270. std::string value = arg.substr(2);
  271. cmGlobalGenerator* gen =
  272. this->CreateGlobalGenerator(value.c_str());
  273. if(!gen)
  274. {
  275. cmSystemTools::Error("Could not create named generator ",
  276. value.c_str());
  277. }
  278. else
  279. {
  280. this->SetGlobalGenerator(gen);
  281. }
  282. }
  283. // no option assume it is the path to the source
  284. else
  285. {
  286. directoriesSet = true;
  287. this->SetHomeOutputDirectory
  288. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  289. this->SetStartOutputDirectory
  290. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  291. this->SetHomeDirectory
  292. (cmSystemTools::CollapseFullPath(arg.c_str()).c_str());
  293. this->SetStartDirectory
  294. (cmSystemTools::CollapseFullPath(arg.c_str()).c_str());
  295. }
  296. }
  297. if(!directoriesSet)
  298. {
  299. this->SetHomeOutputDirectory
  300. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  301. this->SetStartOutputDirectory
  302. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  303. this->SetHomeDirectory
  304. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  305. this->SetStartDirectory
  306. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  307. }
  308. if (!m_Local)
  309. {
  310. this->SetStartDirectory(this->GetHomeDirectory());
  311. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  312. }
  313. }
  314. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the cache
  315. int cmake::AddCMakePaths(const char *arg0)
  316. {
  317. // Find our own executable.
  318. std::vector<cmStdString> failures;
  319. std::string cMakeSelf = arg0;
  320. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  321. failures.push_back(cMakeSelf);
  322. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  323. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  324. {
  325. #ifdef CMAKE_BUILD_DIR
  326. std::string intdir = ".";
  327. #ifdef CMAKE_INTDIR
  328. intdir = CMAKE_INTDIR;
  329. #endif
  330. cMakeSelf = CMAKE_BUILD_DIR;
  331. cMakeSelf += "/Source/";
  332. cMakeSelf += intdir;
  333. cMakeSelf += "/cmake";
  334. cMakeSelf += cmSystemTools::GetExecutableExtension();
  335. #endif
  336. }
  337. #ifdef CMAKE_PREFIX
  338. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  339. {
  340. failures.push_back(cMakeSelf);
  341. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  342. }
  343. #endif
  344. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  345. {
  346. failures.push_back(cMakeSelf);
  347. cmOStringStream msg;
  348. msg << "CMAKE can not find the command line program cmake.\n";
  349. msg << " argv[0] = \"" << arg0 << "\"\n";
  350. msg << " Attempted paths:\n";
  351. std::vector<cmStdString>::iterator i;
  352. for(i=failures.begin(); i != failures.end(); ++i)
  353. {
  354. msg << " \"" << i->c_str() << "\"\n";
  355. }
  356. cmSystemTools::Error(msg.str().c_str());
  357. return 0;
  358. }
  359. // Save the value in the cache
  360. this->m_CacheManager->AddCacheEntry
  361. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  362. cmCacheManager::INTERNAL);
  363. // Find and save the command to edit the cache
  364. std::string editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  365. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  366. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  367. {
  368. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  369. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  370. }
  371. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  372. {
  373. this->m_CacheManager->AddCacheEntry
  374. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  375. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  376. }
  377. // do CMAKE_ROOT, look for the environment variable first
  378. std::string cMakeRoot;
  379. std::string modules;
  380. if (getenv("CMAKE_ROOT"))
  381. {
  382. cMakeRoot = getenv("CMAKE_ROOT");
  383. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  384. }
  385. if(!cmSystemTools::FileExists(modules.c_str()))
  386. {
  387. // next try exe/..
  388. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  389. std::string::size_type slashPos = cMakeRoot.rfind("/");
  390. if(slashPos != std::string::npos)
  391. {
  392. cMakeRoot = cMakeRoot.substr(0, slashPos);
  393. }
  394. // is there no Modules direcory there?
  395. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  396. }
  397. if (!cmSystemTools::FileExists(modules.c_str()))
  398. {
  399. // try exe/../share/cmake
  400. cMakeRoot += "/share/CMake";
  401. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  402. }
  403. #ifdef CMAKE_ROOT_DIR
  404. if (!cmSystemTools::FileExists(modules.c_str()))
  405. {
  406. // try compiled in root directory
  407. cMakeRoot = CMAKE_ROOT_DIR;
  408. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  409. }
  410. #endif
  411. #ifdef CMAKE_PREFIX
  412. if (!cmSystemTools::FileExists(modules.c_str()))
  413. {
  414. // try compiled in install prefix
  415. cMakeRoot = CMAKE_PREFIX "/share/CMake";
  416. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  417. }
  418. #endif
  419. if (!cmSystemTools::FileExists(modules.c_str()))
  420. {
  421. // try
  422. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  423. cMakeRoot += "/share/CMake";
  424. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  425. }
  426. if(!cmSystemTools::FileExists(modules.c_str()))
  427. {
  428. // next try exe
  429. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  430. // is there no Modules direcory there?
  431. modules = cMakeRoot + "/Modules/FindVTK.cmake";
  432. }
  433. if (!cmSystemTools::FileExists(modules.c_str()))
  434. {
  435. // couldn't find modules
  436. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n",
  437. "Modules directory not in directory:\n",
  438. modules.c_str());
  439. return 0;
  440. }
  441. this->m_CacheManager->AddCacheEntry
  442. ("CMAKE_ROOT", cMakeRoot.c_str(),
  443. "Path to CMake installation.", cmCacheManager::INTERNAL);
  444. #ifdef _WIN32
  445. std::string comspec = "cmw9xcom.exe";
  446. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  447. #endif
  448. return 1;
  449. }
  450. void CMakeCommandUsage(const char* program)
  451. {
  452. cmOStringStream errorStream;
  453. errorStream
  454. << "cmake version " << cmMakefile::GetMajorVersion()
  455. << "." << cmMakefile::GetMinorVersion() << "\n";
  456. errorStream
  457. << "Usage: " << program << " -E [command] [arguments ...]\n"
  458. << "Available commands: \n"
  459. << " chdir dir cmd [args]... - run command in a given directory\n"
  460. << " copy file destination - copy file to destination (either file or directory)\n"
  461. << " copy_if_different in-file out-file - copy file if input has changed\n"
  462. << " echo [string]... - displays arguments as text\n"
  463. << " remove file1 file2 ... - remove the file(s)\n"
  464. << " time command [args] ... - run command and return elapsed time\n";
  465. #if defined(_WIN32) && !defined(__CYGWIN__)
  466. errorStream
  467. << " write_regv key value - write registry value\n"
  468. << " delete_regv key - delete registry value\n"
  469. << " comspec - on windows 9x use this for RunCommand\n";
  470. #endif
  471. cmSystemTools::Error(errorStream.str().c_str());
  472. }
  473. int cmake::CMakeCommand(std::vector<std::string>& args)
  474. {
  475. if (args.size() > 1)
  476. {
  477. // Copy file
  478. if (args[1] == "copy" && args.size() == 4)
  479. {
  480. cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str());
  481. return cmSystemTools::GetErrorOccuredFlag();
  482. }
  483. // Copy file if different.
  484. if (args[1] == "copy_if_different" && args.size() == 4)
  485. {
  486. cmSystemTools::CopyFileIfDifferent(args[2].c_str(), args[3].c_str());
  487. return cmSystemTools::GetErrorOccuredFlag();
  488. }
  489. // Echo string
  490. else if (args[1] == "echo" )
  491. {
  492. unsigned int cc;
  493. for ( cc = 2; cc < args.size(); cc ++ )
  494. {
  495. std::cout << args[cc] << " ";
  496. }
  497. std::cout << std::endl;
  498. return 0;
  499. }
  500. // Remove file
  501. else if (args[1] == "remove" && args.size() > 2)
  502. {
  503. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  504. {
  505. if(args[cc] != "-f")
  506. {
  507. if(args[cc] == "\\-f")
  508. {
  509. args[cc] = "-f";
  510. }
  511. cmSystemTools::RemoveFile(args[cc].c_str());
  512. }
  513. }
  514. return 0;
  515. }
  516. // Clock command
  517. else if (args[1] == "time" && args.size() > 2)
  518. {
  519. std::string command = args[2];
  520. std::string output;
  521. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  522. {
  523. command += " ";
  524. command += args[cc];
  525. }
  526. clock_t clock_start, clock_finish;
  527. time_t time_start, time_finish;
  528. time(&time_start);
  529. clock_start = clock();
  530. cmSystemTools::RunCommand(command.c_str(), output, 0, true);
  531. clock_finish = clock();
  532. time(&time_finish);
  533. std::cout << output.c_str();
  534. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  535. std::cout << "Elapsed time: "
  536. << (long)(time_finish - time_start) << " s. (time)"
  537. << ", "
  538. << (double)(clock_finish - clock_start) / clocks_per_sec
  539. << " s. (clock)"
  540. << "\n";
  541. return 0;
  542. }
  543. // Clock command
  544. else if (args[1] == "chdir" && args.size() >= 4)
  545. {
  546. std::string directory = args[2];
  547. std::string command = args[3];
  548. std::string output;
  549. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  550. {
  551. command += " ";
  552. command += args[cc];
  553. }
  554. int retval = 0;
  555. if ( cmSystemTools::RunCommand(command.c_str(), output, retval,
  556. directory.c_str(), false) )
  557. {
  558. std::cout << output.c_str();
  559. return retval;
  560. }
  561. return 1;
  562. }
  563. #if defined(_WIN32) && !defined(__CYGWIN__)
  564. // Write registry value
  565. else if (args[1] == "write_regv" && args.size() > 3)
  566. {
  567. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  568. args[3].c_str()) ? 0 : 1;
  569. }
  570. // Delete registry value
  571. else if (args[1] == "delete_regv" && args.size() > 2)
  572. {
  573. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  574. }
  575. // Remove file
  576. else if (args[1] == "comspec" && args.size() > 2)
  577. {
  578. unsigned int cc;
  579. std::string command = args[2];
  580. for ( cc = 3; cc < args.size(); cc ++ )
  581. {
  582. command += " " + args[cc];
  583. }
  584. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  585. }
  586. #endif
  587. }
  588. ::CMakeCommandUsage(args[0].c_str());
  589. return 1;
  590. }
  591. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  592. {
  593. #if defined(_WIN32) && !defined(__CYGWIN__)
  594. names.push_back(cmGlobalVisualStudio6Generator::GetActualName());
  595. names.push_back(cmGlobalVisualStudio7Generator::GetActualName());
  596. names.push_back(cmGlobalBorlandMakefileGenerator::GetActualName());
  597. names.push_back(cmGlobalNMakeMakefileGenerator::GetActualName());
  598. #else
  599. names.push_back(cmGlobalUnixMakefileGenerator::GetActualName());
  600. #endif
  601. }
  602. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  603. {
  604. cmGlobalGenerator *ret = 0;
  605. #if defined(_WIN32) && !defined(__CYGWIN__)
  606. if (!strcmp(name,cmGlobalNMakeMakefileGenerator::GetActualName()))
  607. {
  608. ret = new cmGlobalNMakeMakefileGenerator;
  609. ret->SetCMakeInstance(this);
  610. }
  611. if (!strcmp(name,cmGlobalVisualStudio6Generator::GetActualName()))
  612. {
  613. ret = new cmGlobalVisualStudio6Generator;
  614. ret->SetCMakeInstance(this);
  615. }
  616. if (!strcmp(name,cmGlobalVisualStudio7Generator::GetActualName()))
  617. {
  618. ret = new cmGlobalVisualStudio7Generator;
  619. ret->SetCMakeInstance(this);
  620. }
  621. if (!strcmp(name,cmGlobalBorlandMakefileGenerator::GetActualName()))
  622. {
  623. ret = new cmGlobalBorlandMakefileGenerator;
  624. ret->SetCMakeInstance(this);
  625. }
  626. #else
  627. if (!strcmp(name,cmGlobalUnixMakefileGenerator::GetActualName()))
  628. {
  629. ret = new cmGlobalUnixMakefileGenerator;
  630. ret->SetCMakeInstance(this);
  631. }
  632. #endif
  633. return ret;
  634. }
  635. void cmake::SetHomeDirectory(const char* dir)
  636. {
  637. m_cmHomeDirectory = dir;
  638. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  639. }
  640. void cmake::SetHomeOutputDirectory(const char* lib)
  641. {
  642. m_HomeOutputDirectory = lib;
  643. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  644. }
  645. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  646. {
  647. // delete the old generator
  648. if (m_GlobalGenerator)
  649. {
  650. delete m_GlobalGenerator;
  651. // 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. // no generator specified on the command line
  720. if(!m_GlobalGenerator)
  721. {
  722. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  723. if(genName)
  724. {
  725. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  726. }
  727. else
  728. {
  729. #if defined(__BORLANDC__) && defined(_WIN32)
  730. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  731. #elif defined(_WIN32) && !defined(__CYGWIN__)
  732. this->SetGlobalGenerator(new cmGlobalVisualStudio6Generator);
  733. #else
  734. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator);
  735. #endif
  736. }
  737. if(!m_GlobalGenerator)
  738. {
  739. cmSystemTools::Error("Could not create generator");
  740. return -1;
  741. }
  742. }
  743. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  744. if(genName)
  745. {
  746. if(strcmp(m_GlobalGenerator->GetName(), genName) != 0)
  747. {
  748. std::string message = "Error: generator : ";
  749. message += m_GlobalGenerator->GetName();
  750. message += "\nDoes not match the generator used previously: ";
  751. message += genName;
  752. message +=
  753. "\nEither remove the CMakeCache.txt file or choose a different"
  754. " binary directory.";
  755. cmSystemTools::Error(message.c_str());
  756. return -2;
  757. }
  758. }
  759. if(!m_CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  760. {
  761. m_CacheManager->AddCacheEntry("CMAKE_GENERATOR", m_GlobalGenerator->GetName(),
  762. "Name of generator.",
  763. cmCacheManager::INTERNAL);
  764. }
  765. // reset any system configuration information, except for when we are
  766. // InTryCompile. With TryCompile the system info is taken from the parent's
  767. // info to save time
  768. if (!m_InTryCompile)
  769. {
  770. m_GlobalGenerator->ClearEnabledLanguages();
  771. }
  772. // actually do the configure
  773. m_GlobalGenerator->Configure();
  774. // Before saving the cache
  775. // if the project did not define one of the entries below, add them now
  776. // so users can edit the values in the cache:
  777. // LIBRARY_OUTPUT_PATH
  778. // EXECUTABLE_OUTPUT_PATH
  779. if(!m_CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  780. {
  781. m_CacheManager->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  782. "Single output directory for building all libraries.",
  783. cmCacheManager::PATH);
  784. }
  785. if(!m_CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  786. {
  787. m_CacheManager->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  788. "Single output directory for building all executables.",
  789. cmCacheManager::PATH);
  790. }
  791. this->m_CacheManager->SaveCache(this->GetHomeOutputDirectory());
  792. if(cmSystemTools::GetErrorOccuredFlag())
  793. {
  794. return -1;
  795. }
  796. return 0;
  797. }
  798. bool cmake::CacheVersionMatches()
  799. {
  800. const char* majv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  801. const char* minv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  802. const char* relv = m_CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  803. bool cacheSameCMake = false;
  804. if(majv &&
  805. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  806. && minv &&
  807. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  808. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  809. {
  810. cacheSameCMake = true;
  811. }
  812. return cacheSameCMake;
  813. }
  814. // handle a command line invocation
  815. int cmake::Run(const std::vector<std::string>& args)
  816. {
  817. // a quick check for args
  818. if(args.size() == 1 && !cmSystemTools::FileExists("CMakeLists.txt"))
  819. {
  820. this->Usage(args[0].c_str());
  821. return -1;
  822. }
  823. // look for obvious request for help
  824. for(unsigned int i=1; i < args.size(); ++i)
  825. {
  826. std::string arg = args[i];
  827. if(arg.find("-help",0) != std::string::npos ||
  828. arg.find("--help",0) != std::string::npos ||
  829. arg.find("/?",0) != std::string::npos ||
  830. arg.find("-usage",0) != std::string::npos)
  831. {
  832. this->Usage(args[0].c_str());
  833. return -1;
  834. }
  835. }
  836. // Process the arguments
  837. this->SetArgs(args);
  838. // make sure rthe Start directory contains a CMakeLists.txt file
  839. std::string srcList = this->GetHomeDirectory();
  840. srcList += "/CMakeLists.txt";
  841. if(!cmSystemTools::FileExists(srcList.c_str()))
  842. {
  843. cmSystemTools::Error("The source directory does not appear to contain CMakeLists.txt\n");
  844. this->Usage(args[0].c_str());
  845. return -1;
  846. }
  847. // set the cmake command
  848. m_CMakeCommand = args[0];
  849. // load the cache
  850. this->LoadCache();
  851. // Add any cache args
  852. this->SetCacheArgs(args);
  853. std::string systemFile = this->GetHomeOutputDirectory();
  854. systemFile += "/CMakeSystem.cmake";
  855. int ret = 0;
  856. // if not local or the cmake version has changed since the last run
  857. // of cmake, or CMakeSystem.cmake file is not in the root binary
  858. // directory, run a global generate
  859. if(!m_Local || !this->CacheVersionMatches() ||
  860. !cmSystemTools::FileExists(systemFile.c_str()) )
  861. {
  862. // If we are doing global generate, we better set start and start
  863. // output directory to the root of the project.
  864. std::string oldstartdir = this->GetStartDirectory();
  865. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  866. this->SetStartDirectory(this->GetHomeDirectory());
  867. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  868. bool saveLocalFlag = m_Local;
  869. m_Local = false;
  870. ret = this->Configure();
  871. if (ret)
  872. {
  873. return ret;
  874. }
  875. ret = this->Generate();
  876. if(ret)
  877. {
  878. return ret;
  879. }
  880. m_Local = saveLocalFlag;
  881. this->SetStartDirectory(oldstartdir.c_str());
  882. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  883. }
  884. // if we are local do the local thing
  885. if (m_Local)
  886. {
  887. ret = this->LocalGenerate();
  888. }
  889. return ret;
  890. }
  891. int cmake::Generate()
  892. {
  893. m_GlobalGenerator->Generate();
  894. if(cmSystemTools::GetErrorOccuredFlag())
  895. {
  896. return -1;
  897. }
  898. return 0;
  899. }
  900. int cmake::LocalGenerate()
  901. {
  902. // Read in the cache
  903. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  904. // create the generator based on the cache if it isn't already there
  905. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  906. if(genName)
  907. {
  908. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  909. }
  910. else
  911. {
  912. cmSystemTools::Error("Could local Generate called without the GENERATOR being specified in the CMakeCache");
  913. return -1;
  914. }
  915. // do the local generate
  916. m_GlobalGenerator->LocalGenerate();
  917. if(cmSystemTools::GetErrorOccuredFlag())
  918. {
  919. return -1;
  920. }
  921. return 0;
  922. }
  923. unsigned int cmake::GetMajorVersion()
  924. {
  925. return cmMakefile::GetMajorVersion();
  926. }
  927. unsigned int cmake::GetMinorVersion()
  928. {
  929. return cmMakefile::GetMinorVersion();
  930. }
  931. const char *cmake::GetReleaseVersion()
  932. {
  933. return cmMakefile::GetReleaseVersion();
  934. }
  935. void cmake::AddCacheEntry(const char* key, const char* value,
  936. const char* helpString,
  937. int type)
  938. {
  939. m_CacheManager->AddCacheEntry(key, value,
  940. helpString,
  941. cmCacheManager::CacheEntryType(type));
  942. }
  943. const char* cmake::GetCacheDefinition(const char* name) const
  944. {
  945. return m_CacheManager->GetCacheValue(name);
  946. }
  947. int cmake::DumpDocumentationToFile(std::ostream& f)
  948. {
  949. // Loop over all registered commands and print out documentation
  950. const char *name;
  951. const char *terse;
  952. const char *full;
  953. char tmp[1024];
  954. sprintf(tmp,"Version %d.%d", cmake::GetMajorVersion(),
  955. cmake::GetMinorVersion());
  956. f << "<html>\n";
  957. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  958. f << "<ul>\n";
  959. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  960. j != m_Commands.end(); ++j)
  961. {
  962. name = (*j).second->GetName();
  963. terse = (*j).second->GetTerseDocumentation();
  964. full = (*j).second->GetFullDocumentation();
  965. f << "<li><b>" << name << "</b> - " << terse << std::endl
  966. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  967. }
  968. f << "</ul></html>\n";
  969. return 1;
  970. }
  971. void cmake::AddDefaultCommands()
  972. {
  973. std::list<cmCommand*> commands;
  974. GetPredefinedCommands(commands);
  975. for(std::list<cmCommand*>::iterator i = commands.begin();
  976. i != commands.end(); ++i)
  977. {
  978. this->AddCommand(*i);
  979. }
  980. }
  981. int cmake::LoadCache()
  982. {
  983. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  984. if (m_CMakeCommand.size() < 2)
  985. {
  986. cmSystemTools::Error("cmake command was not specified prior to loading the cache in cmake.cxx");
  987. return -1;
  988. }
  989. // setup CMAKE_ROOT and CMAKE_COMMAND
  990. if(!this->AddCMakePaths(m_CMakeCommand.c_str()))
  991. {
  992. return -3;
  993. }
  994. // set the default BACKWARDS compatibility to the current version
  995. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  996. {
  997. char ver[256];
  998. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  999. cmMakefile::GetMinorVersion());
  1000. this->m_CacheManager->AddCacheEntry
  1001. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1002. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1003. cmCacheManager::STRING);
  1004. }
  1005. return 0;
  1006. }
  1007. void cmake::SetProgressCallback(ProgressCallback f, void *cd)
  1008. {
  1009. m_ProgressCallback = f;
  1010. m_ProgressCallbackClientData = cd;
  1011. }
  1012. void cmake::UpdateProgress(const char *msg, float prog)
  1013. {
  1014. if(m_ProgressCallback && !m_InTryCompile)
  1015. {
  1016. (*m_ProgressCallback)(msg, prog, m_ProgressCallbackClientData);
  1017. return;
  1018. }
  1019. }