cmake.cxx 47 KB

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