cmake.cxx 52 KB

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