cmake.cxx 44 KB

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