cmake.cxx 50 KB

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