cmake.cxx 52 KB

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