cmake.cxx 47 KB

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