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