cmake.cxx 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  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. #if !defined(__MINGW32__)
  25. #include "cmGlobalVisualStudio7Generator.h"
  26. #include "cmGlobalVisualStudio71Generator.h"
  27. #endif
  28. #include "cmGlobalBorlandMakefileGenerator.h"
  29. #include "cmGlobalNMakeMakefileGenerator.h"
  30. #include "cmGlobalUnixMakefileGenerator.h"
  31. #include "cmWin32ProcessExecution.h"
  32. #else
  33. #include "cmGlobalUnixMakefileGenerator.h"
  34. #endif
  35. #include <stdlib.h> // required for atoi
  36. #ifdef __APPLE__
  37. #include <sys/types.h>
  38. #include <sys/time.h>
  39. #include <sys/resource.h>
  40. #if defined(CMAKE_BUILD_WITH_CMAKE)
  41. #include "cmGlobalCodeWarriorGenerator.h"
  42. #endif
  43. #endif
  44. void cmNeedBackwardsCompatibility(const std::string& variable,
  45. int access_type, void* )
  46. {
  47. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  48. {
  49. std::string message = "An attempt was made to access a variable: ";
  50. message += variable;
  51. 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.";
  52. cmSystemTools::Error(message.c_str());
  53. }
  54. }
  55. cmake::cmake()
  56. {
  57. m_DebugTryCompile = false;
  58. #ifdef __APPLE__
  59. struct rlimit rlp;
  60. if(!getrlimit(RLIMIT_STACK, &rlp))
  61. {
  62. if(rlp.rlim_cur != rlp.rlim_max)
  63. {
  64. rlp.rlim_cur = rlp.rlim_max;
  65. setrlimit(RLIMIT_STACK, &rlp);
  66. }
  67. }
  68. #endif
  69. // If MAKEFLAGS are given in the environment, remove the environment
  70. // variable. This will prevent try-compile from succeeding when it
  71. // should fail (if "-i" is an option). We cannot simply test
  72. // whether "-i" is given and remove it because some make programs
  73. // encode the MAKEFLAGS variable in a strange way.
  74. if(getenv("MAKEFLAGS"))
  75. {
  76. cmSystemTools::PutEnv("MAKEFLAGS=");
  77. }
  78. m_Local = false;
  79. m_Verbose = false;
  80. m_InTryCompile = false;
  81. m_CacheManager = new cmCacheManager;
  82. m_GlobalGenerator = 0;
  83. m_ProgressCallback = 0;
  84. m_ProgressCallbackClientData = 0;
  85. m_VariableWatch = new cmVariableWatch;
  86. m_ScriptMode = false;
  87. this->AddDefaultGenerators();
  88. this->AddDefaultCommands();
  89. m_VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  90. cmNeedBackwardsCompatibility);
  91. m_VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  92. cmNeedBackwardsCompatibility);
  93. m_VariableWatch->AddWatch("CMAKE_X_LIBS",
  94. cmNeedBackwardsCompatibility);
  95. }
  96. cmake::~cmake()
  97. {
  98. delete m_CacheManager;
  99. if (m_GlobalGenerator)
  100. {
  101. delete m_GlobalGenerator;
  102. m_GlobalGenerator = 0;
  103. }
  104. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  105. j != m_Commands.end(); ++j)
  106. {
  107. delete (*j).second;
  108. }
  109. delete m_VariableWatch;
  110. }
  111. bool cmake::CommandExists(const char* name) const
  112. {
  113. return (m_Commands.find(name) != m_Commands.end());
  114. }
  115. cmCommand *cmake::GetCommand(const char *name)
  116. {
  117. cmCommand* rm = 0;
  118. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  119. if (pos != m_Commands.end())
  120. {
  121. rm = (*pos).second;
  122. }
  123. return rm;
  124. }
  125. void cmake::AddCommand(cmCommand* wg)
  126. {
  127. std::string name = wg->GetName();
  128. // if the command already exists, free the old one
  129. RegisteredCommandsMap::iterator pos = m_Commands.find(name);
  130. if (pos != m_Commands.end())
  131. {
  132. delete pos->second;
  133. m_Commands.erase(pos);
  134. }
  135. m_Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  136. }
  137. void cmake::Usage(const char* program)
  138. {
  139. cmOStringStream errorStream;
  140. errorStream << "cmake version " << cmMakefile::GetMajorVersion()
  141. << "." << cmMakefile::GetMinorVersion() << "\n";
  142. errorStream << "Usage: " << program << " [srcdir] [options]\n"
  143. << "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";
  144. errorStream << "Options are:\n";
  145. errorStream << "\n-i (puts cmake in wizard mode, not available for ccmake)\n";
  146. errorStream << "\n-DVAR:TYPE=VALUE (create a cache file entry)\n";
  147. errorStream << "\n-Cpath_to_initial_cache (a cmake list file that is used to pre-load the cache with values.)\n";
  148. errorStream << "\n[-GgeneratorName] (where generator name can be one of these: ";
  149. std::vector<std::string> names;
  150. this->GetRegisteredGenerators(names);
  151. for(std::vector<std::string>::iterator i =names.begin();
  152. i != names.end(); ++i)
  153. {
  154. errorStream << "\"" << i->c_str() << "\" ";
  155. }
  156. errorStream << ")\n";
  157. cmSystemTools::Error(errorStream.str().c_str());
  158. }
  159. // Parse the args
  160. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  161. {
  162. for(unsigned int i=1; i < args.size(); ++i)
  163. {
  164. std::string arg = args[i];
  165. if(arg.find("-D",0) == 0)
  166. {
  167. std::string entry = arg.substr(2);
  168. std::string var, value;
  169. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  170. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  171. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  172. {
  173. this->m_CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  174. "No help, variable specified on the command line.",
  175. type);
  176. }
  177. else
  178. {
  179. std::cerr << "Parse error in command line argument: " << arg << "\n"
  180. << "Should be: VAR:type=value\n";
  181. cmSystemTools::Error("No cmake scrpt provided.");
  182. return false;
  183. }
  184. }
  185. else if(arg.find("-C",0) == 0)
  186. {
  187. std::string path = arg.substr(2);
  188. if ( path.size() == 0 )
  189. {
  190. cmSystemTools::Error("No initial cache file provided.");
  191. return false;
  192. }
  193. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  194. this->ReadListFile(path.c_str());
  195. }
  196. else if(arg.find("-P",0) == 0)
  197. {
  198. i++;
  199. std::string path = args[i];
  200. if ( path.size() == 0 )
  201. {
  202. cmSystemTools::Error("No cmake scrpt provided.");
  203. return false;
  204. }
  205. std::cerr << "Running cmake script file " << path.c_str() << "\n";
  206. this->ReadListFile(path.c_str());
  207. }
  208. }
  209. return true;
  210. }
  211. void cmake::ReadListFile(const char *path)
  212. {
  213. // if a generator was not yet created, temporarily create one
  214. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  215. bool created = false;
  216. // if a generator was not specified use a generic one
  217. if (!gg)
  218. {
  219. gg = new cmGlobalGenerator;
  220. gg->SetCMakeInstance(this);
  221. created = true;
  222. }
  223. // read in the list file to fill the cache
  224. if(path)
  225. {
  226. cmLocalGenerator *lg = gg->CreateLocalGenerator();
  227. lg->SetGlobalGenerator(gg);
  228. if (!lg->GetMakefile()->ReadListFile(0, path))
  229. {
  230. std::cerr << "Error in reading cmake initial cache file:"
  231. << path << "\n";
  232. }
  233. }
  234. // free generic one if generated
  235. if (created)
  236. {
  237. delete gg;
  238. }
  239. }
  240. // Parse the args
  241. void cmake::SetArgs(const std::vector<std::string>& args)
  242. {
  243. m_Local = false;
  244. bool directoriesSet = false;
  245. for(unsigned int i=1; i < args.size(); ++i)
  246. {
  247. std::string arg = args[i];
  248. if(arg.find("-H",0) == 0)
  249. {
  250. directoriesSet = true;
  251. std::string path = arg.substr(2);
  252. path = cmSystemTools::CollapseFullPath(path.c_str());
  253. cmSystemTools::ConvertToUnixSlashes(path);
  254. this->SetHomeDirectory(path.c_str());
  255. }
  256. else if(arg.find("-S",0) == 0)
  257. {
  258. directoriesSet = true;
  259. m_Local = true;
  260. std::string path = arg.substr(2);
  261. path = cmSystemTools::CollapseFullPath(path.c_str());
  262. cmSystemTools::ConvertToUnixSlashes(path);
  263. this->SetStartDirectory(path.c_str());
  264. }
  265. else if(arg.find("-O",0) == 0)
  266. {
  267. directoriesSet = true;
  268. std::string path = arg.substr(2);
  269. path = cmSystemTools::CollapseFullPath(path.c_str());
  270. cmSystemTools::ConvertToUnixSlashes(path);
  271. this->SetStartOutputDirectory(path.c_str());
  272. }
  273. else if(arg.find("-B",0) == 0)
  274. {
  275. directoriesSet = true;
  276. std::string path = arg.substr(2);
  277. path = cmSystemTools::CollapseFullPath(path.c_str());
  278. cmSystemTools::ConvertToUnixSlashes(path);
  279. this->SetHomeOutputDirectory(path.c_str());
  280. }
  281. else if(arg.find("-V",0) == 0)
  282. {
  283. m_Verbose = true;
  284. }
  285. else if(arg.find("-D",0) == 0)
  286. {
  287. // skip for now
  288. }
  289. else if(arg.find("-C",0) == 0)
  290. {
  291. // skip for now
  292. }
  293. else if(arg.find("--script",0) == 0)
  294. {
  295. // skip for now
  296. }
  297. else if(arg.find("--debug-trycompile",0) == 0)
  298. {
  299. std::cout << "debug trycompile on\n";
  300. this->DebugTryCompileOn();
  301. }
  302. else if(arg.find("-G",0) == 0)
  303. {
  304. std::string value = arg.substr(2);
  305. cmGlobalGenerator* gen =
  306. this->CreateGlobalGenerator(value.c_str());
  307. if(!gen)
  308. {
  309. cmSystemTools::Error("Could not create named generator ",
  310. value.c_str());
  311. }
  312. else
  313. {
  314. this->SetGlobalGenerator(gen);
  315. }
  316. }
  317. // no option assume it is the path to the source
  318. else
  319. {
  320. directoriesSet = true;
  321. this->SetDirectoriesFromFile(arg.c_str());
  322. }
  323. }
  324. if(!directoriesSet)
  325. {
  326. this->SetHomeOutputDirectory
  327. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  328. this->SetStartOutputDirectory
  329. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  330. this->SetHomeDirectory
  331. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  332. this->SetStartDirectory
  333. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  334. }
  335. if (!m_Local)
  336. {
  337. this->SetStartDirectory(this->GetHomeDirectory());
  338. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  339. }
  340. }
  341. //----------------------------------------------------------------------------
  342. void cmake::SetDirectoriesFromFile(const char* arg)
  343. {
  344. // Check if the argument refers to a CMakeCache.txt or
  345. // CMakeLists.txt file.
  346. std::string listPath;
  347. std::string cachePath;
  348. bool argIsFile = false;
  349. if(cmSystemTools::FileIsDirectory(arg))
  350. {
  351. std::string path = cmSystemTools::CollapseFullPath(arg);
  352. cmSystemTools::ConvertToUnixSlashes(path);
  353. std::string cacheFile = path;
  354. cacheFile += "/CMakeCache.txt";
  355. std::string listFile = path;
  356. listFile += "/CMakeLists.txt";
  357. if(cmSystemTools::FileExists(cacheFile.c_str()))
  358. {
  359. cachePath = path;
  360. }
  361. if(cmSystemTools::FileExists(listFile.c_str()))
  362. {
  363. listPath = path;
  364. }
  365. }
  366. else if(cmSystemTools::FileExists(arg))
  367. {
  368. argIsFile = true;
  369. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  370. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  371. name = cmSystemTools::LowerCase(name);
  372. if(name == "cmakecache.txt")
  373. {
  374. cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  375. }
  376. else if(name == "cmakelists.txt")
  377. {
  378. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  379. }
  380. }
  381. else
  382. {
  383. // Specified file or directory does not exist. Try to set things
  384. // up to produce a meaningful error message.
  385. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  386. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  387. name = cmSystemTools::LowerCase(name);
  388. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  389. {
  390. argIsFile = true;
  391. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  392. }
  393. else
  394. {
  395. listPath = fullPath;
  396. }
  397. }
  398. // If there is a CMakeCache.txt file, use its settings.
  399. if(cachePath.length() > 0)
  400. {
  401. cmCacheManager* cachem = this->GetCacheManager();
  402. cmCacheManager::CacheIterator it = cachem->NewIterator();
  403. if(cachem->LoadCache(cachePath.c_str()) && it.Find("CMAKE_HOME_DIRECTORY"))
  404. {
  405. this->SetHomeOutputDirectory(cachePath.c_str());
  406. this->SetStartOutputDirectory(cachePath.c_str());
  407. this->SetHomeDirectory(it.GetValue());
  408. this->SetStartDirectory(it.GetValue());
  409. return;
  410. }
  411. }
  412. // If there is a CMakeLists.txt file, use it as the source tree.
  413. if(listPath.length() > 0)
  414. {
  415. this->SetHomeDirectory(listPath.c_str());
  416. this->SetStartDirectory(listPath.c_str());
  417. if(argIsFile)
  418. {
  419. // Source CMakeLists.txt file given. It was probably dropped
  420. // onto the executable in a GUI. Default to an in-source build.
  421. this->SetHomeOutputDirectory(listPath.c_str());
  422. this->SetStartOutputDirectory(listPath.c_str());
  423. }
  424. else
  425. {
  426. // Source directory given on command line. Use current working
  427. // directory as build tree.
  428. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  429. this->SetHomeOutputDirectory(cwd.c_str());
  430. this->SetStartOutputDirectory(cwd.c_str());
  431. }
  432. return;
  433. }
  434. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  435. // argument. Assume it is the path to the source tree, and use the
  436. // current working directory as the build tree.
  437. std::string full = cmSystemTools::CollapseFullPath(arg);
  438. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  439. this->SetHomeDirectory(full.c_str());
  440. this->SetStartDirectory(full.c_str());
  441. this->SetHomeOutputDirectory(cwd.c_str());
  442. this->SetStartOutputDirectory(cwd.c_str());
  443. }
  444. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the cache
  445. int cmake::AddCMakePaths(const char *arg0)
  446. {
  447. // Find our own executable.
  448. std::vector<cmStdString> failures;
  449. std::string cMakeSelf = arg0;
  450. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  451. failures.push_back(cMakeSelf);
  452. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  453. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  454. {
  455. #ifdef CMAKE_BUILD_DIR
  456. std::string intdir = ".";
  457. #ifdef CMAKE_INTDIR
  458. intdir = CMAKE_INTDIR;
  459. #endif
  460. cMakeSelf = CMAKE_BUILD_DIR;
  461. cMakeSelf += "/bin/";
  462. cMakeSelf += intdir;
  463. cMakeSelf += "/cmake";
  464. cMakeSelf += cmSystemTools::GetExecutableExtension();
  465. #endif
  466. }
  467. #ifdef CMAKE_PREFIX
  468. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  469. {
  470. failures.push_back(cMakeSelf);
  471. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  472. }
  473. #endif
  474. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  475. {
  476. failures.push_back(cMakeSelf);
  477. cmOStringStream msg;
  478. msg << "CMAKE can not find the command line program cmake.\n";
  479. msg << " argv[0] = \"" << arg0 << "\"\n";
  480. msg << " Attempted paths:\n";
  481. std::vector<cmStdString>::iterator i;
  482. for(i=failures.begin(); i != failures.end(); ++i)
  483. {
  484. msg << " \"" << i->c_str() << "\"\n";
  485. }
  486. cmSystemTools::Error(msg.str().c_str());
  487. return 0;
  488. }
  489. // Save the value in the cache
  490. this->m_CacheManager->AddCacheEntry
  491. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  492. cmCacheManager::INTERNAL);
  493. // Find and save the command to edit the cache
  494. std::string editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  495. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  496. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  497. {
  498. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  499. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  500. }
  501. std::string ctestCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  502. "/ctest" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  503. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  504. {
  505. this->m_CacheManager->AddCacheEntry
  506. ("CMAKE_CTEST_COMMAND", ctestCommand.c_str(),
  507. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  508. }
  509. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  510. {
  511. this->m_CacheManager->AddCacheEntry
  512. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  513. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  514. }
  515. // do CMAKE_ROOT, look for the environment variable first
  516. std::string cMakeRoot;
  517. std::string modules;
  518. if (getenv("CMAKE_ROOT"))
  519. {
  520. cMakeRoot = getenv("CMAKE_ROOT");
  521. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  522. }
  523. if(!cmSystemTools::FileExists(modules.c_str()))
  524. {
  525. // next try exe/..
  526. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  527. std::string::size_type slashPos = cMakeRoot.rfind("/");
  528. if(slashPos != std::string::npos)
  529. {
  530. cMakeRoot = cMakeRoot.substr(0, slashPos);
  531. }
  532. // is there no Modules direcory there?
  533. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  534. }
  535. if (!cmSystemTools::FileExists(modules.c_str()))
  536. {
  537. // try exe/../share/cmake
  538. cMakeRoot += CMAKE_DATA_DIR;
  539. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  540. }
  541. #ifdef CMAKE_ROOT_DIR
  542. if (!cmSystemTools::FileExists(modules.c_str()))
  543. {
  544. // try compiled in root directory
  545. cMakeRoot = CMAKE_ROOT_DIR;
  546. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  547. }
  548. #endif
  549. #ifdef CMAKE_PREFIX
  550. if (!cmSystemTools::FileExists(modules.c_str()))
  551. {
  552. // try compiled in install prefix
  553. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  554. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  555. }
  556. #endif
  557. if (!cmSystemTools::FileExists(modules.c_str()))
  558. {
  559. // try
  560. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  561. cMakeRoot += CMAKE_DATA_DIR;
  562. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  563. }
  564. if(!cmSystemTools::FileExists(modules.c_str()))
  565. {
  566. // next try exe
  567. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  568. // is there no Modules direcory there?
  569. modules = cMakeRoot + "/Modules/CMakeDefaultMakeRuleVariables.cmake";
  570. }
  571. if (!cmSystemTools::FileExists(modules.c_str()))
  572. {
  573. // couldn't find modules
  574. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  575. "CMake has most likely not been installed correctly.\n"
  576. "Modules directory not found in\n",
  577. cMakeRoot.c_str());
  578. return 0;
  579. }
  580. this->m_CacheManager->AddCacheEntry
  581. ("CMAKE_ROOT", cMakeRoot.c_str(),
  582. "Path to CMake installation.", cmCacheManager::INTERNAL);
  583. #ifdef _WIN32
  584. std::string comspec = "cmw9xcom.exe";
  585. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  586. #endif
  587. return 1;
  588. }
  589. void CMakeCommandUsage(const char* program)
  590. {
  591. cmOStringStream errorStream;
  592. errorStream
  593. << "cmake version " << cmMakefile::GetMajorVersion()
  594. << "." << cmMakefile::GetMinorVersion() << "\n";
  595. errorStream
  596. << "Usage: " << program << " -E [command] [arguments ...]\n"
  597. << "Available commands: \n"
  598. << " chdir dir cmd [args]... - run command in a given directory\n"
  599. << " copy file destination - copy file to destination (either file or directory)\n"
  600. << " copy_if_different in-file out-file - copy file if input has changed\n"
  601. << " echo [string]... - displays arguments as text\n"
  602. << " remove file1 file2 ... - remove the file(s)\n"
  603. << " time command [args] ... - run command and return elapsed time\n";
  604. #if defined(_WIN32) && !defined(__CYGWIN__)
  605. errorStream
  606. << " write_regv key value - write registry value\n"
  607. << " delete_regv key - delete registry value\n"
  608. << " comspec - on windows 9x use this for RunCommand\n";
  609. #endif
  610. cmSystemTools::Error(errorStream.str().c_str());
  611. }
  612. int cmake::CMakeCommand(std::vector<std::string>& args)
  613. {
  614. if (args.size() > 1)
  615. {
  616. // Copy file
  617. if (args[1] == "copy" && args.size() == 4)
  618. {
  619. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  620. {
  621. std::cerr << "Error copying file \"" << args[2].c_str()
  622. << "\" to \"" << args[3].c_str() << "\".\n";
  623. return 1;
  624. }
  625. return 0;
  626. }
  627. // Copy file if different.
  628. if (args[1] == "copy_if_different" && args.size() == 4)
  629. {
  630. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(), args[3].c_str()))
  631. {
  632. std::cerr << "Error copying file (if different) from \""
  633. << args[2].c_str() << "\" to \"" << args[3].c_str()
  634. << "\".\n";
  635. return 1;
  636. }
  637. return 0;
  638. }
  639. // Echo string
  640. else if (args[1] == "echo" )
  641. {
  642. unsigned int cc;
  643. for ( cc = 2; cc < args.size(); cc ++ )
  644. {
  645. std::cout << args[cc] << " ";
  646. }
  647. std::cout << std::endl;
  648. return 0;
  649. }
  650. // Remove file
  651. else if (args[1] == "remove" && args.size() > 2)
  652. {
  653. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  654. {
  655. if(args[cc] != "-f")
  656. {
  657. if(args[cc] == "\\-f")
  658. {
  659. args[cc] = "-f";
  660. }
  661. cmSystemTools::RemoveFile(args[cc].c_str());
  662. }
  663. }
  664. return 0;
  665. }
  666. // Clock command
  667. else if (args[1] == "time" && args.size() > 2)
  668. {
  669. std::string command = args[2];
  670. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  671. {
  672. command += " ";
  673. command += args[cc];
  674. }
  675. clock_t clock_start, clock_finish;
  676. time_t time_start, time_finish;
  677. time(&time_start);
  678. clock_start = clock();
  679. cmSystemTools::RunSingleCommand(command.c_str());
  680. clock_finish = clock();
  681. time(&time_finish);
  682. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  683. std::cout << "Elapsed time: "
  684. << (long)(time_finish - time_start) << " s. (time)"
  685. << ", "
  686. << (double)(clock_finish - clock_start) / clocks_per_sec
  687. << " s. (clock)"
  688. << "\n";
  689. return 0;
  690. }
  691. // Clock command
  692. else if (args[1] == "chdir" && args.size() >= 4)
  693. {
  694. std::string directory = args[2];
  695. std::string command = "\"";
  696. command += args[3];
  697. command += "\"";
  698. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  699. {
  700. command += " \"";
  701. command += args[cc];
  702. command += "\"";
  703. }
  704. int retval = 0;
  705. int timeout = 0;
  706. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  707. directory.c_str(), true, timeout) )
  708. {
  709. return retval;
  710. }
  711. return 1;
  712. }
  713. // Internal CMake shared library support.
  714. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  715. {
  716. int result = 0;
  717. std::string realName = args[2];
  718. std::string soName = args[3];
  719. std::string name = args[4];
  720. if(soName != realName)
  721. {
  722. std::string fname = cmSystemTools::GetFilenameName(realName);
  723. if(cmSystemTools::FileExists(soName.c_str()))
  724. {
  725. cmSystemTools::RemoveFile(soName.c_str());
  726. }
  727. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  728. {
  729. result = 1;
  730. }
  731. }
  732. if(name != soName)
  733. {
  734. std::string fname = cmSystemTools::GetFilenameName(soName);
  735. if(cmSystemTools::FileExists(soName.c_str()))
  736. {
  737. cmSystemTools::RemoveFile(name.c_str());
  738. }
  739. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  740. {
  741. result = 1;
  742. }
  743. }
  744. return result;
  745. }
  746. #if defined(_WIN32) && !defined(__CYGWIN__)
  747. // Write registry value
  748. else if (args[1] == "write_regv" && args.size() > 3)
  749. {
  750. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  751. args[3].c_str()) ? 0 : 1;
  752. }
  753. // Delete registry value
  754. else if (args[1] == "delete_regv" && args.size() > 2)
  755. {
  756. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  757. }
  758. // Remove file
  759. else if (args[1] == "comspec" && args.size() > 2)
  760. {
  761. unsigned int cc;
  762. std::string command = args[2];
  763. for ( cc = 3; cc < args.size(); cc ++ )
  764. {
  765. command += " " + args[cc];
  766. }
  767. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  768. }
  769. #endif
  770. }
  771. ::CMakeCommandUsage(args[0].c_str());
  772. return 1;
  773. }
  774. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  775. {
  776. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  777. i != m_Generators.end(); ++i)
  778. {
  779. names.push_back(i->first);
  780. }
  781. }
  782. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  783. {
  784. RegisteredGeneratorsMap::const_iterator i = m_Generators.find(name);
  785. if(i != m_Generators.end())
  786. {
  787. cmGlobalGenerator* generator = (i->second)();
  788. generator->SetCMakeInstance(this);
  789. return generator;
  790. }
  791. else
  792. {
  793. return 0;
  794. }
  795. }
  796. void cmake::SetHomeDirectory(const char* dir)
  797. {
  798. m_cmHomeDirectory = dir;
  799. cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
  800. }
  801. void cmake::SetHomeOutputDirectory(const char* lib)
  802. {
  803. m_HomeOutputDirectory = lib;
  804. cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
  805. }
  806. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  807. {
  808. // delete the old generator
  809. if (m_GlobalGenerator)
  810. {
  811. delete m_GlobalGenerator;
  812. // restore the original environment variables CXX and CC
  813. // Restor CC
  814. std::string env = "CC=";
  815. if(m_CCEnvironment.size())
  816. {
  817. env += m_CCEnvironment;
  818. }
  819. cmSystemTools::PutEnv(env.c_str());
  820. env = "CXX=";
  821. if(m_CXXEnvironment.size())
  822. {
  823. env += m_CXXEnvironment;
  824. }
  825. cmSystemTools::PutEnv(env.c_str());
  826. }
  827. // set the new
  828. m_GlobalGenerator = gg;
  829. // set the global flag for unix style paths on cmSystemTools as
  830. // soon as the generator is set. This allows gmake to be used
  831. // on windows.
  832. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  833. // Save the environment variables CXX and CC
  834. const char* cxx = getenv("CXX");
  835. const char* cc = getenv("CC");
  836. if(cxx)
  837. {
  838. m_CXXEnvironment = cxx;
  839. }
  840. else
  841. {
  842. m_CXXEnvironment = "";
  843. }
  844. if(cc)
  845. {
  846. m_CCEnvironment = cc;
  847. }
  848. else
  849. {
  850. m_CCEnvironment = "";
  851. }
  852. // set the cmake instance just to be sure
  853. gg->SetCMakeInstance(this);
  854. }
  855. int cmake::DoPreConfigureChecks()
  856. {
  857. // Make sure the Start directory contains a CMakeLists.txt file.
  858. std::string srcList = this->GetHomeDirectory();
  859. srcList += "/CMakeLists.txt";
  860. if(!cmSystemTools::FileExists(srcList.c_str()))
  861. {
  862. cmOStringStream err;
  863. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  864. {
  865. err << "The source directory \"" << this->GetHomeDirectory()
  866. << "\" does not appear to contain CMakeLists.txt.\n";
  867. }
  868. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  869. {
  870. err << "The source directory \"" << this->GetHomeDirectory()
  871. << "\" is a file, not a directory.\n";
  872. }
  873. else
  874. {
  875. err << "The source directory \"" << this->GetHomeDirectory()
  876. << "\" does not exist.\n";
  877. }
  878. err << "Specify --help for usage, or press the help button on the CMake GUI.";
  879. cmSystemTools::Error(err.str().c_str());
  880. return -2;
  881. }
  882. // do a sanity check on some values
  883. if(m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  884. {
  885. std::string cacheStart =
  886. m_CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  887. cacheStart += "/CMakeLists.txt";
  888. std::string currentStart = this->GetHomeDirectory();
  889. currentStart += "/CMakeLists.txt";
  890. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  891. {
  892. std::string message = "The source \"";
  893. message += currentStart;
  894. message += "\" does not match the source \"";
  895. message += cacheStart;
  896. message += "\" used to generate cache. ";
  897. message += "Re-run cmake with a different source directory.";
  898. cmSystemTools::Error(message.c_str());
  899. return -2;
  900. }
  901. }
  902. else
  903. {
  904. return 0;
  905. }
  906. return 1;
  907. }
  908. int cmake::Configure()
  909. {
  910. int res = 0;
  911. if ( !m_ScriptMode )
  912. {
  913. res = this->DoPreConfigureChecks();
  914. }
  915. if ( res < 0 )
  916. {
  917. return -2;
  918. }
  919. if ( !res )
  920. {
  921. m_CacheManager->AddCacheEntry("CMAKE_HOME_DIRECTORY",
  922. this->GetHomeDirectory(),
  923. "Start directory with the top level CMakeLists.txt file for this project",
  924. cmCacheManager::INTERNAL);
  925. }
  926. // set the default BACKWARDS compatibility to the current version
  927. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  928. {
  929. char ver[256];
  930. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  931. cmMakefile::GetMinorVersion());
  932. this->m_CacheManager->AddCacheEntry
  933. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  934. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  935. cmCacheManager::STRING);
  936. }
  937. // no generator specified on the command line
  938. if(!m_GlobalGenerator)
  939. {
  940. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  941. if(genName)
  942. {
  943. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  944. // set the global flag for unix style paths on cmSystemTools as
  945. // soon as the generator is set. This allows gmake to be used
  946. // on windows.
  947. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  948. }
  949. else
  950. {
  951. #if defined(__BORLANDC__) && defined(_WIN32)
  952. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  953. #elif defined(_WIN32) && !defined(__CYGWIN__)
  954. this->SetGlobalGenerator(new cmGlobalVisualStudio6Generator);
  955. #else
  956. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator);
  957. #endif
  958. }
  959. if(!m_GlobalGenerator)
  960. {
  961. cmSystemTools::Error("Could not create generator");
  962. return -1;
  963. }
  964. }
  965. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  966. if(genName)
  967. {
  968. if(strcmp(m_GlobalGenerator->GetName(), genName) != 0)
  969. {
  970. std::string message = "Error: generator : ";
  971. message += m_GlobalGenerator->GetName();
  972. message += "\nDoes not match the generator used previously: ";
  973. message += genName;
  974. message +=
  975. "\nEither remove the CMakeCache.txt file or choose a different"
  976. " binary directory.";
  977. cmSystemTools::Error(message.c_str());
  978. return -2;
  979. }
  980. }
  981. if(!m_CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  982. {
  983. m_CacheManager->AddCacheEntry("CMAKE_GENERATOR", m_GlobalGenerator->GetName(),
  984. "Name of generator.",
  985. cmCacheManager::INTERNAL);
  986. }
  987. // reset any system configuration information, except for when we are
  988. // InTryCompile. With TryCompile the system info is taken from the parent's
  989. // info to save time
  990. if (!m_InTryCompile)
  991. {
  992. m_GlobalGenerator->ClearEnabledLanguages();
  993. }
  994. this->CleanupWrittenFiles();
  995. // actually do the configure
  996. m_GlobalGenerator->Configure();
  997. // Before saving the cache
  998. // if the project did not define one of the entries below, add them now
  999. // so users can edit the values in the cache:
  1000. // LIBRARY_OUTPUT_PATH
  1001. // EXECUTABLE_OUTPUT_PATH
  1002. if(!m_CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1003. {
  1004. m_CacheManager->AddCacheEntry("LIBRARY_OUTPUT_PATH", "",
  1005. "Single output directory for building all libraries.",
  1006. cmCacheManager::PATH);
  1007. }
  1008. if(!m_CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1009. {
  1010. m_CacheManager->AddCacheEntry("EXECUTABLE_OUTPUT_PATH", "",
  1011. "Single output directory for building all executables.",
  1012. cmCacheManager::PATH);
  1013. }
  1014. if(!m_CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1015. {
  1016. m_CacheManager->AddCacheEntry("CMAKE_USE_RELATIVE_PATHS", false,
  1017. "If true, cmake will use relative paths in makefiles and projects.");
  1018. cmCacheManager::CacheIterator it =
  1019. m_CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1020. if ( !it.PropertyExists("ADVANCED") )
  1021. {
  1022. it.SetProperty("ADVANCED", "1");
  1023. }
  1024. }
  1025. if(cmSystemTools::GetFatalErrorOccured() &&
  1026. (!this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1027. cmSystemTools::IsOff(this->m_CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1028. {
  1029. // We must have a bad generator selection. Wipe the cache entry so the
  1030. // user can select another.
  1031. m_CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1032. }
  1033. if ( !m_ScriptMode )
  1034. {
  1035. this->m_CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1036. }
  1037. if(cmSystemTools::GetErrorOccuredFlag())
  1038. {
  1039. return -1;
  1040. }
  1041. return 0;
  1042. }
  1043. bool cmake::CacheVersionMatches()
  1044. {
  1045. const char* majv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1046. const char* minv = m_CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1047. const char* relv = m_CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1048. bool cacheSameCMake = false;
  1049. if(majv &&
  1050. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  1051. && minv &&
  1052. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  1053. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  1054. {
  1055. cacheSameCMake = true;
  1056. }
  1057. return cacheSameCMake;
  1058. }
  1059. // handle a command line invocation
  1060. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1061. {
  1062. // Process the arguments
  1063. this->SetArgs(args);
  1064. // set the cmake command
  1065. m_CMakeCommand = args[0];
  1066. if ( !m_ScriptMode )
  1067. {
  1068. // load the cache
  1069. if(this->LoadCache() < 0)
  1070. {
  1071. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1072. return -1;
  1073. }
  1074. }
  1075. // Add any cache args
  1076. if ( !this->SetCacheArgs(args) )
  1077. {
  1078. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1079. return -1;
  1080. }
  1081. std::string systemFile = this->GetHomeOutputDirectory();
  1082. systemFile += "/CMakeSystem.cmake";
  1083. if ( noconfigure )
  1084. {
  1085. return 0;
  1086. }
  1087. int ret = 0;
  1088. // if not local or the cmake version has changed since the last run
  1089. // of cmake, or CMakeSystem.cmake file is not in the root binary
  1090. // directory, run a global generate
  1091. if(m_ScriptMode || !m_Local || !this->CacheVersionMatches() ||
  1092. !cmSystemTools::FileExists(systemFile.c_str()) )
  1093. {
  1094. // If we are doing global generate, we better set start and start
  1095. // output directory to the root of the project.
  1096. std::string oldstartdir = this->GetStartDirectory();
  1097. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1098. this->SetStartDirectory(this->GetHomeDirectory());
  1099. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1100. bool saveLocalFlag = m_Local;
  1101. m_Local = false;
  1102. ret = this->Configure();
  1103. if (ret || m_ScriptMode)
  1104. {
  1105. return ret;
  1106. }
  1107. ret = this->Generate();
  1108. if(ret)
  1109. {
  1110. return ret;
  1111. }
  1112. m_Local = saveLocalFlag;
  1113. this->SetStartDirectory(oldstartdir.c_str());
  1114. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1115. }
  1116. // if we are local do the local thing
  1117. if (m_Local)
  1118. {
  1119. ret = this->LocalGenerate();
  1120. }
  1121. return ret;
  1122. }
  1123. int cmake::Generate()
  1124. {
  1125. if(!m_GlobalGenerator)
  1126. {
  1127. return -1;
  1128. }
  1129. m_GlobalGenerator->Generate();
  1130. if(cmSystemTools::GetErrorOccuredFlag())
  1131. {
  1132. return -1;
  1133. }
  1134. return 0;
  1135. }
  1136. int cmake::LocalGenerate()
  1137. {
  1138. // Read in the cache
  1139. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1140. // create the generator based on the cache if it isn't already there
  1141. const char* genName = m_CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1142. if(genName)
  1143. {
  1144. m_GlobalGenerator = this->CreateGlobalGenerator(genName);
  1145. // set the global flag for unix style paths on cmSystemTools as
  1146. // soon as the generator is set. This allows gmake to be used
  1147. // on windows.
  1148. cmSystemTools::SetForceUnixPaths(m_GlobalGenerator->GetForceUnixPaths());
  1149. }
  1150. else
  1151. {
  1152. cmSystemTools::Error("Could local Generate called without the GENERATOR being specified in the CMakeCache");
  1153. return -1;
  1154. }
  1155. // do the local generate
  1156. m_GlobalGenerator->LocalGenerate();
  1157. if(cmSystemTools::GetErrorOccuredFlag())
  1158. {
  1159. return -1;
  1160. }
  1161. return 0;
  1162. }
  1163. unsigned int cmake::GetMajorVersion()
  1164. {
  1165. return cmMakefile::GetMajorVersion();
  1166. }
  1167. unsigned int cmake::GetMinorVersion()
  1168. {
  1169. return cmMakefile::GetMinorVersion();
  1170. }
  1171. const char *cmake::GetReleaseVersion()
  1172. {
  1173. return cmMakefile::GetReleaseVersion();
  1174. }
  1175. void cmake::AddCacheEntry(const char* key, const char* value,
  1176. const char* helpString,
  1177. int type)
  1178. {
  1179. m_CacheManager->AddCacheEntry(key, value,
  1180. helpString,
  1181. cmCacheManager::CacheEntryType(type));
  1182. }
  1183. const char* cmake::GetCacheDefinition(const char* name) const
  1184. {
  1185. return m_CacheManager->GetCacheValue(name);
  1186. }
  1187. int cmake::DumpDocumentationToFile(std::ostream& f)
  1188. {
  1189. // Loop over all registered commands and print out documentation
  1190. const char *name;
  1191. const char *terse;
  1192. const char *full;
  1193. char tmp[1024];
  1194. sprintf(tmp,"Version %d.%d", cmake::GetMajorVersion(),
  1195. cmake::GetMinorVersion());
  1196. f << "<html>\n";
  1197. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1198. f << "<ul>\n";
  1199. for(RegisteredCommandsMap::iterator j = m_Commands.begin();
  1200. j != m_Commands.end(); ++j)
  1201. {
  1202. name = (*j).second->GetName();
  1203. terse = (*j).second->GetTerseDocumentation();
  1204. full = (*j).second->GetFullDocumentation();
  1205. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1206. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1207. }
  1208. f << "</ul></html>\n";
  1209. return 1;
  1210. }
  1211. void cmake::AddDefaultCommands()
  1212. {
  1213. std::list<cmCommand*> commands;
  1214. GetPredefinedCommands(commands);
  1215. for(std::list<cmCommand*>::iterator i = commands.begin();
  1216. i != commands.end(); ++i)
  1217. {
  1218. this->AddCommand(*i);
  1219. }
  1220. }
  1221. void cmake::AddDefaultGenerators()
  1222. {
  1223. #if defined(_WIN32) && !defined(__CYGWIN__)
  1224. m_Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1225. &cmGlobalVisualStudio6Generator::New;
  1226. #if !defined(__MINGW32__)
  1227. m_Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1228. &cmGlobalVisualStudio7Generator::New;
  1229. m_Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1230. &cmGlobalVisualStudio71Generator::New;
  1231. #endif
  1232. m_Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1233. &cmGlobalBorlandMakefileGenerator::New;
  1234. m_Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1235. &cmGlobalNMakeMakefileGenerator::New;
  1236. #else
  1237. # if defined(__APPLE__) && defined(CMAKE_BUILD_WITH_CMAKE)
  1238. m_Generators[cmGlobalCodeWarriorGenerator::GetActualName()] =
  1239. &cmGlobalCodeWarriorGenerator::New;
  1240. # endif
  1241. #endif
  1242. m_Generators[cmGlobalUnixMakefileGenerator::GetActualName()] =
  1243. &cmGlobalUnixMakefileGenerator::New;
  1244. }
  1245. int cmake::LoadCache()
  1246. {
  1247. m_CacheManager->LoadCache(this->GetHomeOutputDirectory());
  1248. if (m_CMakeCommand.size() < 2)
  1249. {
  1250. cmSystemTools::Error("cmake command was not specified prior to loading the cache in cmake.cxx");
  1251. return -1;
  1252. }
  1253. // setup CMAKE_ROOT and CMAKE_COMMAND
  1254. if(!this->AddCMakePaths(m_CMakeCommand.c_str()))
  1255. {
  1256. return -3;
  1257. }
  1258. // set the default BACKWARDS compatibility to the current version
  1259. if(!m_CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1260. {
  1261. char ver[256];
  1262. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1263. cmMakefile::GetMinorVersion());
  1264. this->m_CacheManager->AddCacheEntry
  1265. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1266. "For backwards compatibility, what version of CMake commands and syntax should this version of CMake allow.",
  1267. cmCacheManager::STRING);
  1268. }
  1269. return 0;
  1270. }
  1271. void cmake::SetProgressCallback(ProgressCallback f, void *cd)
  1272. {
  1273. m_ProgressCallback = f;
  1274. m_ProgressCallbackClientData = cd;
  1275. }
  1276. void cmake::UpdateProgress(const char *msg, float prog)
  1277. {
  1278. if(m_ProgressCallback && !m_InTryCompile)
  1279. {
  1280. (*m_ProgressCallback)(msg, prog, m_ProgressCallbackClientData);
  1281. return;
  1282. }
  1283. }
  1284. void cmake::GetCommandDocumentation(std::vector<cmDocumentationEntry>& v) const
  1285. {
  1286. for(RegisteredCommandsMap::const_iterator j = m_Commands.begin();
  1287. j != m_Commands.end(); ++j)
  1288. {
  1289. cmDocumentationEntry e =
  1290. {
  1291. (*j).second->GetName(),
  1292. (*j).second->GetTerseDocumentation(),
  1293. (*j).second->GetFullDocumentation()
  1294. };
  1295. v.push_back(e);
  1296. }
  1297. cmDocumentationEntry empty = {0,0,0};
  1298. v.push_back(empty);
  1299. }
  1300. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1301. {
  1302. for(RegisteredGeneratorsMap::const_iterator i = m_Generators.begin();
  1303. i != m_Generators.end(); ++i)
  1304. {
  1305. cmDocumentationEntry e;
  1306. cmGlobalGenerator* generator = (i->second)();
  1307. generator->GetDocumentation(e);
  1308. delete generator;
  1309. v.push_back(e);
  1310. }
  1311. cmDocumentationEntry empty = {0,0,0};
  1312. v.push_back(empty);
  1313. }
  1314. void cmake::AddWrittenFile(const char* file)
  1315. {
  1316. m_WrittenFiles.insert(file);
  1317. }
  1318. bool cmake::HasWrittenFile(const char* file)
  1319. {
  1320. return m_WrittenFiles.find(file) != m_WrittenFiles.end();
  1321. }
  1322. void cmake::CleanupWrittenFiles()
  1323. {
  1324. m_WrittenFiles.clear();
  1325. }