cmake.cxx 30 KB

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