cmake.cxx 47 KB

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