cmake.cxx 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  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. #include "cmGeneratedFileStream.h"
  22. #if defined(CMAKE_BUILD_WITH_CMAKE)
  23. # include "cmDependsFortran.h" // For -E cmake_copy_f90_mod callback.
  24. # include "cmVariableWatch.h"
  25. # include "cmVersion.h"
  26. #endif
  27. // only build kdevelop generator on non-windows platforms
  28. // when not bootstrapping cmake
  29. #if !defined(_WIN32)
  30. # if defined(CMAKE_BUILD_WITH_CMAKE)
  31. # define CMAKE_USE_KDEVELOP
  32. # endif
  33. #endif
  34. #if defined(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
  35. # define CMAKE_BOOT_MINGW
  36. #endif
  37. // include the generator
  38. #if defined(_WIN32) && !defined(__CYGWIN__)
  39. # if !defined(CMAKE_BOOT_MINGW)
  40. # include "cmGlobalVisualStudio6Generator.h"
  41. # include "cmGlobalVisualStudio7Generator.h"
  42. # include "cmGlobalVisualStudio71Generator.h"
  43. # include "cmGlobalVisualStudio8Generator.h"
  44. # include "cmGlobalVisualStudio8Win64Generator.h"
  45. # include "cmGlobalBorlandMakefileGenerator.h"
  46. # include "cmGlobalNMakeMakefileGenerator.h"
  47. # include "cmGlobalWatcomWMakeGenerator.h"
  48. # endif
  49. # include "cmGlobalMSYSMakefileGenerator.h"
  50. # include "cmGlobalMinGWMakefileGenerator.h"
  51. # include "cmWin32ProcessExecution.h"
  52. #else
  53. #endif
  54. #include "cmGlobalUnixMakefileGenerator3.h"
  55. #ifdef CMAKE_USE_KDEVELOP
  56. # include "cmGlobalKdevelopGenerator.h"
  57. #endif
  58. #include <stdlib.h> // required for atoi
  59. #if defined( __APPLE__ )
  60. # if defined(CMAKE_BUILD_WITH_CMAKE)
  61. # include "cmGlobalXCodeGenerator.h"
  62. # define CMAKE_USE_XCODE 1
  63. # endif
  64. # include <sys/types.h>
  65. # include <sys/time.h>
  66. # include <sys/resource.h>
  67. #endif
  68. #include <sys/stat.h> // struct stat
  69. #include <memory> // auto_ptr
  70. void cmNeedBackwardsCompatibility(const std::string& variable,
  71. int access_type, void* )
  72. {
  73. #ifdef CMAKE_BUILD_WITH_CMAKE
  74. if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
  75. {
  76. std::string message = "An attempt was made to access a variable: ";
  77. message += variable;
  78. message +=
  79. " that has not been defined. Some variables were always defined "
  80. "by CMake in versions prior to 1.6. To fix this you might need to set "
  81. "the cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If "
  82. "you are writing a CMakeList file, (or have already set "
  83. "CMAKE_BACKWARDS_COMPATABILITY to 1.4 or less) then you probably need "
  84. "to include a CMake module to test for the feature this variable "
  85. "defines.";
  86. cmSystemTools::Error(message.c_str());
  87. }
  88. #else
  89. (void)variable;
  90. (void)access_type;
  91. #endif
  92. }
  93. cmake::cmake()
  94. {
  95. this->DebugTryCompile = false;
  96. this->ClearBuildSystem = false;
  97. this->FileComparison = new cmFileTimeComparison;
  98. #ifdef __APPLE__
  99. struct rlimit rlp;
  100. if(!getrlimit(RLIMIT_STACK, &rlp))
  101. {
  102. if(rlp.rlim_cur != rlp.rlim_max)
  103. {
  104. rlp.rlim_cur = rlp.rlim_max;
  105. setrlimit(RLIMIT_STACK, &rlp);
  106. }
  107. }
  108. #endif
  109. // If MAKEFLAGS are given in the environment, remove the environment
  110. // variable. This will prevent try-compile from succeeding when it
  111. // should fail (if "-i" is an option). We cannot simply test
  112. // whether "-i" is given and remove it because some make programs
  113. // encode the MAKEFLAGS variable in a strange way.
  114. if(getenv("MAKEFLAGS"))
  115. {
  116. cmSystemTools::PutEnv("MAKEFLAGS=");
  117. }
  118. this->Verbose = false;
  119. this->InTryCompile = false;
  120. this->CacheManager = new cmCacheManager;
  121. this->GlobalGenerator = 0;
  122. this->ProgressCallback = 0;
  123. this->ProgressCallbackClientData = 0;
  124. this->ScriptMode = false;
  125. #ifdef CMAKE_BUILD_WITH_CMAKE
  126. this->VariableWatch = new cmVariableWatch;
  127. this->VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
  128. cmNeedBackwardsCompatibility);
  129. this->VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
  130. cmNeedBackwardsCompatibility);
  131. this->VariableWatch->AddWatch("CMAKE_X_LIBS",
  132. cmNeedBackwardsCompatibility);
  133. #endif
  134. this->AddDefaultGenerators();
  135. this->AddDefaultCommands();
  136. // Make sure we can capture the build tool output.
  137. cmSystemTools::EnableVSConsoleOutput();
  138. }
  139. cmake::~cmake()
  140. {
  141. delete this->CacheManager;
  142. if (this->GlobalGenerator)
  143. {
  144. delete this->GlobalGenerator;
  145. this->GlobalGenerator = 0;
  146. }
  147. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  148. j != this->Commands.end(); ++j)
  149. {
  150. delete (*j).second;
  151. }
  152. #ifdef CMAKE_BUILD_WITH_CMAKE
  153. delete this->VariableWatch;
  154. #endif
  155. delete this->FileComparison;
  156. }
  157. void cmake::CleanupCommandsAndMacros()
  158. {
  159. std::vector<cmCommand*> commands;
  160. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  161. j != this->Commands.end(); ++j)
  162. {
  163. if ( !j->second->IsA("cmMacroHelperCommand") )
  164. {
  165. commands.push_back(j->second);
  166. }
  167. else
  168. {
  169. delete j->second;
  170. }
  171. }
  172. this->Commands.erase(this->Commands.begin(), this->Commands.end());
  173. std::vector<cmCommand*>::iterator it;
  174. for ( it = commands.begin(); it != commands.end();
  175. ++ it )
  176. {
  177. this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  178. }
  179. }
  180. bool cmake::CommandExists(const char* name) const
  181. {
  182. std::string sName = cmSystemTools::LowerCase(name);
  183. return (this->Commands.find(sName) != this->Commands.end());
  184. }
  185. cmCommand *cmake::GetCommand(const char *name)
  186. {
  187. cmCommand* rm = 0;
  188. std::string sName = cmSystemTools::LowerCase(name);
  189. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  190. if (pos != this->Commands.end())
  191. {
  192. rm = (*pos).second;
  193. }
  194. return rm;
  195. }
  196. void cmake::RenameCommand(const char*oldName, const char* newName)
  197. {
  198. // if the command already exists, free the old one
  199. std::string sOldName = cmSystemTools::LowerCase(oldName);
  200. std::string sNewName = cmSystemTools::LowerCase(newName);
  201. RegisteredCommandsMap::iterator pos = this->Commands.find(sOldName);
  202. if ( pos == this->Commands.end() )
  203. {
  204. return;
  205. }
  206. cmCommand* cmd = pos->second;
  207. pos = this->Commands.find(sNewName);
  208. if (pos != this->Commands.end())
  209. {
  210. delete pos->second;
  211. this->Commands.erase(pos);
  212. }
  213. this->Commands.insert(RegisteredCommandsMap::value_type(sNewName, cmd));
  214. pos = this->Commands.find(sOldName);
  215. this->Commands.erase(pos);
  216. }
  217. void cmake::AddCommand(cmCommand* wg)
  218. {
  219. std::string name = cmSystemTools::LowerCase(wg->GetName());
  220. // if the command already exists, free the old one
  221. RegisteredCommandsMap::iterator pos = this->Commands.find(name);
  222. if (pos != this->Commands.end())
  223. {
  224. delete pos->second;
  225. this->Commands.erase(pos);
  226. }
  227. this->Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  228. }
  229. // Parse the args
  230. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  231. {
  232. for(unsigned int i=1; i < args.size(); ++i)
  233. {
  234. std::string arg = args[i];
  235. if(arg.find("-D",0) == 0)
  236. {
  237. std::string entry = arg.substr(2);
  238. if(entry.size() == 0)
  239. {
  240. entry = args[++i];
  241. }
  242. std::string var, value;
  243. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  244. if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type) ||
  245. cmCacheManager::ParseEntry(entry.c_str(), var, value))
  246. {
  247. this->CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
  248. "No help, variable specified on the command line.",
  249. type);
  250. }
  251. else
  252. {
  253. std::cerr << "Parse error in command line argument: " << arg << "\n"
  254. << "Should be: VAR:type=value\n";
  255. cmSystemTools::Error("No cmake scrpt provided.");
  256. return false;
  257. }
  258. }
  259. else if(arg.find("-C",0) == 0)
  260. {
  261. std::string path = arg.substr(2);
  262. if ( path.size() == 0 )
  263. {
  264. path = args[++i];
  265. }
  266. std::cerr << "loading initial cache file " << path.c_str() << "\n";
  267. this->ReadListFile(path.c_str());
  268. }
  269. else if(arg.find("-P",0) == 0)
  270. {
  271. i++;
  272. std::string path = args[i];
  273. if ( path.size() == 0 )
  274. {
  275. cmSystemTools::Error("No cmake script provided.");
  276. return false;
  277. }
  278. this->ReadListFile(path.c_str());
  279. }
  280. }
  281. return true;
  282. }
  283. void cmake::ReadListFile(const char *path)
  284. {
  285. // if a generator was not yet created, temporarily create one
  286. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  287. bool created = false;
  288. // if a generator was not specified use a generic one
  289. if (!gg)
  290. {
  291. gg = new cmGlobalGenerator;
  292. gg->SetCMakeInstance(this);
  293. created = true;
  294. }
  295. // read in the list file to fill the cache
  296. if(path)
  297. {
  298. std::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  299. lg->SetGlobalGenerator(gg);
  300. lg->GetMakefile()->SetHomeOutputDirectory
  301. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  302. lg->GetMakefile()->SetStartOutputDirectory
  303. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  304. lg->GetMakefile()->SetHomeDirectory
  305. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  306. lg->GetMakefile()->SetStartDirectory
  307. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  308. if (!lg->GetMakefile()->ReadListFile(0, path))
  309. {
  310. std::cerr << "Error processing file:" << path << "\n";
  311. }
  312. }
  313. // free generic one if generated
  314. if (created)
  315. {
  316. delete gg;
  317. }
  318. }
  319. // Parse the args
  320. void cmake::SetArgs(const std::vector<std::string>& args)
  321. {
  322. bool directoriesSet = false;
  323. for(unsigned int i=1; i < args.size(); ++i)
  324. {
  325. std::string arg = args[i];
  326. if(arg.find("-H",0) == 0)
  327. {
  328. directoriesSet = true;
  329. std::string path = arg.substr(2);
  330. path = cmSystemTools::CollapseFullPath(path.c_str());
  331. cmSystemTools::ConvertToUnixSlashes(path);
  332. this->SetHomeDirectory(path.c_str());
  333. }
  334. else if(arg.find("-S",0) == 0)
  335. {
  336. // There is no local generate anymore. Ignore -S option.
  337. }
  338. else if(arg.find("-O",0) == 0)
  339. {
  340. // There is no local generate anymore. Ignore -O option.
  341. }
  342. else if(arg.find("-B",0) == 0)
  343. {
  344. directoriesSet = true;
  345. std::string path = arg.substr(2);
  346. path = cmSystemTools::CollapseFullPath(path.c_str());
  347. cmSystemTools::ConvertToUnixSlashes(path);
  348. this->SetHomeOutputDirectory(path.c_str());
  349. }
  350. else if((i < args.size()-1) && (arg.find("--check-build-system",0) == 0))
  351. {
  352. this->CheckBuildSystemArgument = args[++i];
  353. this->ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  354. }
  355. else if(arg.find("-V",0) == 0)
  356. {
  357. this->Verbose = true;
  358. }
  359. else if(arg.find("-D",0) == 0)
  360. {
  361. // skip for now
  362. }
  363. else if(arg.find("-C",0) == 0)
  364. {
  365. // skip for now
  366. }
  367. else if(arg.find("-P",0) == 0)
  368. {
  369. // skip for now
  370. i++;
  371. }
  372. else if(arg.find("--graphviz=",0) == 0)
  373. {
  374. std::string path = arg.substr(strlen("--graphviz="));
  375. path = cmSystemTools::CollapseFullPath(path.c_str());
  376. cmSystemTools::ConvertToUnixSlashes(path);
  377. this->GraphVizFile = path;
  378. if ( this->GraphVizFile.empty() )
  379. {
  380. cmSystemTools::Error("No file specified for --graphviz");
  381. }
  382. }
  383. else if(arg.find("--debug-trycompile",0) == 0)
  384. {
  385. std::cout << "debug trycompile on\n";
  386. this->DebugTryCompileOn();
  387. }
  388. else if(arg.find("-G",0) == 0)
  389. {
  390. std::string value = arg.substr(2);
  391. if(value.size() == 0)
  392. {
  393. value = args[++i];
  394. }
  395. cmGlobalGenerator* gen =
  396. this->CreateGlobalGenerator(value.c_str());
  397. if(!gen)
  398. {
  399. cmSystemTools::Error("Could not create named generator ",
  400. value.c_str());
  401. }
  402. else
  403. {
  404. this->SetGlobalGenerator(gen);
  405. }
  406. }
  407. // no option assume it is the path to the source
  408. else
  409. {
  410. directoriesSet = true;
  411. this->SetDirectoriesFromFile(arg.c_str());
  412. }
  413. }
  414. if(!directoriesSet)
  415. {
  416. this->SetHomeOutputDirectory
  417. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  418. this->SetStartOutputDirectory
  419. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  420. this->SetHomeDirectory
  421. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  422. this->SetStartDirectory
  423. (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  424. }
  425. this->SetStartDirectory(this->GetHomeDirectory());
  426. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  427. }
  428. //----------------------------------------------------------------------------
  429. void cmake::SetDirectoriesFromFile(const char* arg)
  430. {
  431. // Check if the argument refers to a CMakeCache.txt or
  432. // CMakeLists.txt file.
  433. std::string listPath;
  434. std::string cachePath;
  435. bool argIsFile = false;
  436. if(cmSystemTools::FileIsDirectory(arg))
  437. {
  438. std::string path = cmSystemTools::CollapseFullPath(arg);
  439. cmSystemTools::ConvertToUnixSlashes(path);
  440. std::string cacheFile = path;
  441. cacheFile += "/CMakeCache.txt";
  442. std::string listFile = path;
  443. listFile += "/CMakeLists.txt";
  444. if(cmSystemTools::FileExists(cacheFile.c_str()))
  445. {
  446. cachePath = path;
  447. }
  448. if(cmSystemTools::FileExists(listFile.c_str()))
  449. {
  450. listPath = path;
  451. }
  452. }
  453. else if(cmSystemTools::FileExists(arg))
  454. {
  455. argIsFile = true;
  456. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  457. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  458. name = cmSystemTools::LowerCase(name);
  459. if(name == "cmakecache.txt")
  460. {
  461. cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  462. }
  463. else if(name == "cmakelists.txt")
  464. {
  465. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  466. }
  467. }
  468. else
  469. {
  470. // Specified file or directory does not exist. Try to set things
  471. // up to produce a meaningful error message.
  472. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  473. std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
  474. name = cmSystemTools::LowerCase(name);
  475. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  476. {
  477. argIsFile = true;
  478. listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
  479. }
  480. else
  481. {
  482. listPath = fullPath;
  483. }
  484. }
  485. // If there is a CMakeCache.txt file, use its settings.
  486. if(cachePath.length() > 0)
  487. {
  488. cmCacheManager* cachem = this->GetCacheManager();
  489. cmCacheManager::CacheIterator it = cachem->NewIterator();
  490. if(cachem->LoadCache(cachePath.c_str()) &&
  491. it.Find("CMAKE_HOME_DIRECTORY"))
  492. {
  493. this->SetHomeOutputDirectory(cachePath.c_str());
  494. this->SetStartOutputDirectory(cachePath.c_str());
  495. this->SetHomeDirectory(it.GetValue());
  496. this->SetStartDirectory(it.GetValue());
  497. return;
  498. }
  499. }
  500. // If there is a CMakeLists.txt file, use it as the source tree.
  501. if(listPath.length() > 0)
  502. {
  503. this->SetHomeDirectory(listPath.c_str());
  504. this->SetStartDirectory(listPath.c_str());
  505. if(argIsFile)
  506. {
  507. // Source CMakeLists.txt file given. It was probably dropped
  508. // onto the executable in a GUI. Default to an in-source build.
  509. this->SetHomeOutputDirectory(listPath.c_str());
  510. this->SetStartOutputDirectory(listPath.c_str());
  511. }
  512. else
  513. {
  514. // Source directory given on command line. Use current working
  515. // directory as build tree.
  516. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  517. this->SetHomeOutputDirectory(cwd.c_str());
  518. this->SetStartOutputDirectory(cwd.c_str());
  519. }
  520. return;
  521. }
  522. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  523. // argument. Assume it is the path to the source tree, and use the
  524. // current working directory as the build tree.
  525. std::string full = cmSystemTools::CollapseFullPath(arg);
  526. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  527. this->SetHomeDirectory(full.c_str());
  528. this->SetStartDirectory(full.c_str());
  529. this->SetHomeOutputDirectory(cwd.c_str());
  530. this->SetStartOutputDirectory(cwd.c_str());
  531. }
  532. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
  533. // cache
  534. int cmake::AddCMakePaths(const char *arg0)
  535. {
  536. // Find our own executable.
  537. std::vector<cmStdString> failures;
  538. std::string cMakeSelf = arg0;
  539. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  540. failures.push_back(cMakeSelf);
  541. cMakeSelf = cmSystemTools::FindProgram(cMakeSelf.c_str());
  542. cmSystemTools::ConvertToUnixSlashes(cMakeSelf);
  543. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  544. {
  545. #ifdef CMAKE_BUILD_DIR
  546. std::string intdir = ".";
  547. #ifdef CMAKE_INTDIR
  548. intdir = CMAKE_INTDIR;
  549. #endif
  550. cMakeSelf = CMAKE_BUILD_DIR;
  551. cMakeSelf += "/bin/";
  552. cMakeSelf += intdir;
  553. cMakeSelf += "/cmake";
  554. cMakeSelf += cmSystemTools::GetExecutableExtension();
  555. #endif
  556. }
  557. #ifdef CMAKE_PREFIX
  558. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  559. {
  560. failures.push_back(cMakeSelf);
  561. cMakeSelf = CMAKE_PREFIX "/bin/cmake";
  562. }
  563. #endif
  564. if(!cmSystemTools::FileExists(cMakeSelf.c_str()))
  565. {
  566. failures.push_back(cMakeSelf);
  567. cmOStringStream msg;
  568. msg << "CMAKE can not find the command line program cmake.\n";
  569. msg << " argv[0] = \"" << arg0 << "\"\n";
  570. msg << " Attempted paths:\n";
  571. std::vector<cmStdString>::iterator i;
  572. for(i=failures.begin(); i != failures.end(); ++i)
  573. {
  574. msg << " \"" << i->c_str() << "\"\n";
  575. }
  576. cmSystemTools::Error(msg.str().c_str());
  577. return 0;
  578. }
  579. // Save the value in the cache
  580. this->CacheManager->AddCacheEntry
  581. ("CMAKE_COMMAND",cMakeSelf.c_str(), "Path to CMake executable.",
  582. cmCacheManager::INTERNAL);
  583. // Find and save the command to edit the cache
  584. std::string editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  585. "/ccmake" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  586. if( !cmSystemTools::FileExists(editCacheCommand.c_str()))
  587. {
  588. editCacheCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  589. "/CMakeSetup" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  590. }
  591. std::string ctestCommand = cmSystemTools::GetFilenamePath(cMakeSelf) +
  592. "/ctest" + cmSystemTools::GetFilenameExtension(cMakeSelf);
  593. if(cmSystemTools::FileExists(ctestCommand.c_str()))
  594. {
  595. this->CacheManager->AddCacheEntry
  596. ("CMAKE_CTEST_COMMAND", ctestCommand.c_str(),
  597. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  598. }
  599. if(cmSystemTools::FileExists(editCacheCommand.c_str()))
  600. {
  601. this->CacheManager->AddCacheEntry
  602. ("CMAKE_EDIT_COMMAND", editCacheCommand.c_str(),
  603. "Path to cache edit program executable.", cmCacheManager::INTERNAL);
  604. }
  605. // do CMAKE_ROOT, look for the environment variable first
  606. std::string cMakeRoot;
  607. std::string modules;
  608. if (getenv("CMAKE_ROOT"))
  609. {
  610. cMakeRoot = getenv("CMAKE_ROOT");
  611. modules = cMakeRoot + "/Modules/CMake.cmake";
  612. }
  613. if(!cmSystemTools::FileExists(modules.c_str()))
  614. {
  615. // next try exe/..
  616. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  617. std::string::size_type slashPos = cMakeRoot.rfind("/");
  618. if(slashPos != std::string::npos)
  619. {
  620. cMakeRoot = cMakeRoot.substr(0, slashPos);
  621. }
  622. // is there no Modules direcory there?
  623. modules = cMakeRoot + "/Modules/CMake.cmake";
  624. }
  625. if (!cmSystemTools::FileExists(modules.c_str()))
  626. {
  627. // try exe/../share/cmake
  628. cMakeRoot += CMAKE_DATA_DIR;
  629. modules = cMakeRoot + "/Modules/CMake.cmake";
  630. }
  631. #ifdef CMAKE_ROOT_DIR
  632. if (!cmSystemTools::FileExists(modules.c_str()))
  633. {
  634. // try compiled in root directory
  635. cMakeRoot = CMAKE_ROOT_DIR;
  636. modules = cMakeRoot + "/Modules/CMake.cmake";
  637. }
  638. #endif
  639. #ifdef CMAKE_PREFIX
  640. if (!cmSystemTools::FileExists(modules.c_str()))
  641. {
  642. // try compiled in install prefix
  643. cMakeRoot = CMAKE_PREFIX CMAKE_DATA_DIR;
  644. modules = cMakeRoot + "/Modules/CMake.cmake";
  645. }
  646. #endif
  647. if (!cmSystemTools::FileExists(modules.c_str()))
  648. {
  649. // try
  650. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  651. cMakeRoot += CMAKE_DATA_DIR;
  652. modules = cMakeRoot + "/Modules/CMake.cmake";
  653. }
  654. if(!cmSystemTools::FileExists(modules.c_str()))
  655. {
  656. // next try exe
  657. cMakeRoot = cmSystemTools::GetProgramPath(cMakeSelf.c_str());
  658. // is there no Modules direcory there?
  659. modules = cMakeRoot + "/Modules/CMake.cmake";
  660. }
  661. if (!cmSystemTools::FileExists(modules.c_str()))
  662. {
  663. // couldn't find modules
  664. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  665. "CMake has most likely not been installed correctly.\n"
  666. "Modules directory not found in\n",
  667. cMakeRoot.c_str());
  668. return 0;
  669. }
  670. this->CacheManager->AddCacheEntry
  671. ("CMAKE_ROOT", cMakeRoot.c_str(),
  672. "Path to CMake installation.", cmCacheManager::INTERNAL);
  673. #ifdef _WIN32
  674. std::string comspec = "cmw9xcom.exe";
  675. cmSystemTools::SetWindows9xComspecSubstitute(comspec.c_str());
  676. #endif
  677. return 1;
  678. }
  679. void CMakeCommandUsage(const char* program)
  680. {
  681. cmOStringStream errorStream;
  682. #ifdef CMAKE_BUILD_WITH_CMAKE
  683. errorStream
  684. << "cmake version " << cmVersion::GetCMakeVersion() << "\n";
  685. #else
  686. errorStream
  687. << "cmake bootstrap\n";
  688. #endif
  689. errorStream
  690. << "Usage: " << program << " -E [command] [arguments ...]\n"
  691. << "Available commands: \n"
  692. << " chdir dir cmd [args]... - run command in a given directory\n"
  693. << " copy file destination - copy file to destination (either file or "
  694. "directory)\n"
  695. << " copy_if_different in-file out-file - copy file if input has "
  696. "changed\n"
  697. << " copy_directory source destination - copy directory 'source' "
  698. "content to directory 'destination'\n"
  699. << " compare_files file1 file2 - check if file1 is same as file2\n"
  700. << " echo [string]... - displays arguments as text\n"
  701. << " environment - display the current enviroment\n"
  702. << " remove file1 file2 ... - remove the file(s)\n"
  703. << " tar [cxt][vfz] file.tar file/dir1 file/dir2 ... - create a tar.\n"
  704. << " time command [args] ... - run command and return elapsed time\n"
  705. #if defined(_WIN32) && !defined(__CYGWIN__)
  706. << " write_regv key value - write registry value\n"
  707. << " delete_regv key - delete registry value\n"
  708. << " comspec - on windows 9x use this for RunCommand\n"
  709. #else
  710. << " create_symlink old new - create a symbolic link new -> old\n"
  711. #endif
  712. ;
  713. cmSystemTools::Error(errorStream.str().c_str());
  714. }
  715. int cmake::ExecuteCMakeCommand(std::vector<std::string>& args)
  716. {
  717. if (args.size() > 1)
  718. {
  719. // Copy file
  720. if (args[1] == "copy" && args.size() == 4)
  721. {
  722. if(!cmSystemTools::cmCopyFile(args[2].c_str(), args[3].c_str()))
  723. {
  724. std::cerr << "Error copying file \"" << args[2].c_str()
  725. << "\" to \"" << args[3].c_str() << "\".\n";
  726. return 1;
  727. }
  728. return 0;
  729. }
  730. // Copy file if different.
  731. if (args[1] == "copy_if_different" && args.size() == 4)
  732. {
  733. if(!cmSystemTools::CopyFileIfDifferent(args[2].c_str(),
  734. args[3].c_str()))
  735. {
  736. std::cerr << "Error copying file (if different) from \""
  737. << args[2].c_str() << "\" to \"" << args[3].c_str()
  738. << "\".\n";
  739. return 1;
  740. }
  741. return 0;
  742. }
  743. // Copy directory content
  744. if (args[1] == "copy_directory" && args.size() == 4)
  745. {
  746. if(!cmSystemTools::CopyADirectory(args[2].c_str(), args[3].c_str()))
  747. {
  748. std::cerr << "Error copying directory from \""
  749. << args[2].c_str() << "\" to \"" << args[3].c_str()
  750. << "\".\n";
  751. return 1;
  752. }
  753. return 0;
  754. }
  755. // Compare files
  756. if (args[1] == "compare_files" && args.size() == 4)
  757. {
  758. if(cmSystemTools::FilesDiffer(args[2].c_str(), args[3].c_str()))
  759. {
  760. std::cerr << "Files \""
  761. << args[2].c_str() << "\" to \"" << args[3].c_str()
  762. << "\" are different.\n";
  763. return 1;
  764. }
  765. return 0;
  766. }
  767. // Echo string
  768. else if (args[1] == "echo" )
  769. {
  770. unsigned int cc;
  771. const char* space = "";
  772. for ( cc = 2; cc < args.size(); cc ++ )
  773. {
  774. std::cout << space << args[cc];
  775. space = " ";
  776. }
  777. std::cout << std::endl;
  778. return 0;
  779. }
  780. #if defined(CMAKE_BUILD_WITH_CMAKE)
  781. // Command to create a symbolic link. Fails on platforms not
  782. // supporting them.
  783. else if (args[1] == "environment" )
  784. {
  785. std::vector<std::string> env = cmSystemTools::GetEnvironmentVariables();
  786. std::vector<std::string>::iterator it;
  787. for ( it = env.begin(); it != env.end(); ++ it )
  788. {
  789. std::cout << it->c_str() << std::endl;
  790. }
  791. return 0;
  792. }
  793. #endif
  794. // Remove file
  795. else if (args[1] == "remove" && args.size() > 2)
  796. {
  797. for (std::string::size_type cc = 2; cc < args.size(); cc ++)
  798. {
  799. if(args[cc] != "-f")
  800. {
  801. if(args[cc] == "\\-f")
  802. {
  803. args[cc] = "-f";
  804. }
  805. cmSystemTools::RemoveFile(args[cc].c_str());
  806. }
  807. }
  808. return 0;
  809. }
  810. // Clock command
  811. else if (args[1] == "time" && args.size() > 2)
  812. {
  813. std::string command = args[2];
  814. for (std::string::size_type cc = 3; cc < args.size(); cc ++)
  815. {
  816. command += " ";
  817. command += args[cc];
  818. }
  819. clock_t clock_start, clock_finish;
  820. time_t time_start, time_finish;
  821. time(&time_start);
  822. clock_start = clock();
  823. cmSystemTools::RunSingleCommand(command.c_str());
  824. clock_finish = clock();
  825. time(&time_finish);
  826. double clocks_per_sec = (double)CLOCKS_PER_SEC;
  827. std::cout << "Elapsed time: "
  828. << (long)(time_finish - time_start) << " s. (time)"
  829. << ", "
  830. << (double)(clock_finish - clock_start) / clocks_per_sec
  831. << " s. (clock)"
  832. << "\n";
  833. return 0;
  834. }
  835. // Command to change directory and run a program.
  836. else if (args[1] == "chdir" && args.size() >= 4)
  837. {
  838. std::string directory = args[2];
  839. if(!cmSystemTools::FileExists(directory.c_str()))
  840. {
  841. cmSystemTools::Error("Directory does not exist for chdir command: ",
  842. args[2].c_str());
  843. return 0;
  844. }
  845. std::string command = "\"";
  846. command += args[3];
  847. command += "\"";
  848. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  849. {
  850. command += " \"";
  851. command += args[cc];
  852. command += "\"";
  853. }
  854. int retval = 0;
  855. int timeout = 0;
  856. if ( cmSystemTools::RunSingleCommand(command.c_str(), 0, &retval,
  857. directory.c_str(), true, timeout) )
  858. {
  859. return retval;
  860. }
  861. return 1;
  862. }
  863. // Command to create a symbolic link. Fails on platforms not
  864. // supporting them.
  865. else if (args[1] == "create_symlink" && args.size() == 4)
  866. {
  867. return cmSystemTools::CreateSymlink(args[2].c_str(),
  868. args[3].c_str())? 0:1;
  869. }
  870. // Internal CMake shared library support.
  871. else if (args[1] == "cmake_symlink_library" && args.size() == 5)
  872. {
  873. int result = 0;
  874. std::string realName = args[2];
  875. std::string soName = args[3];
  876. std::string name = args[4];
  877. if(soName != realName)
  878. {
  879. std::string fname = cmSystemTools::GetFilenameName(realName);
  880. if(cmSystemTools::FileExists(soName.c_str()))
  881. {
  882. cmSystemTools::RemoveFile(soName.c_str());
  883. }
  884. if(!cmSystemTools::CreateSymlink(fname.c_str(), soName.c_str()))
  885. {
  886. result = 1;
  887. }
  888. }
  889. if(name != soName)
  890. {
  891. std::string fname = cmSystemTools::GetFilenameName(soName);
  892. if(cmSystemTools::FileExists(soName.c_str()))
  893. {
  894. cmSystemTools::RemoveFile(name.c_str());
  895. }
  896. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  897. {
  898. result = 1;
  899. }
  900. }
  901. return result;
  902. }
  903. // Internal CMake versioned executable support.
  904. else if (args[1] == "cmake_symlink_executable" && args.size() == 4)
  905. {
  906. int result = 0;
  907. std::string realName = args[2];
  908. std::string name = args[3];
  909. if(name != realName)
  910. {
  911. std::string fname = cmSystemTools::GetFilenameName(realName);
  912. if(cmSystemTools::FileExists(realName.c_str()))
  913. {
  914. cmSystemTools::RemoveFile(name.c_str());
  915. }
  916. if(!cmSystemTools::CreateSymlink(fname.c_str(), name.c_str()))
  917. {
  918. result = 1;
  919. }
  920. }
  921. return result;
  922. }
  923. // Internal CMake dependency scanning support.
  924. else if (args[1] == "cmake_depends" && args.size() >= 6)
  925. {
  926. cmake cm;
  927. cmGlobalGenerator *ggd = cm.CreateGlobalGenerator(args[2].c_str());
  928. if (ggd)
  929. {
  930. ggd->SetCMakeInstance(&cm);
  931. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  932. lgd->SetGlobalGenerator(ggd);
  933. return lgd->ScanDependencies(args)? 0 : 2;
  934. }
  935. return 1;
  936. }
  937. // Tar files
  938. else if (args[1] == "tar" && args.size() > 3)
  939. {
  940. std::string flags = args[2];
  941. std::string outFile = args[3];
  942. std::vector<cmStdString> files;
  943. for (std::string::size_type cc = 4; cc < args.size(); cc ++)
  944. {
  945. files.push_back(args[cc]);
  946. }
  947. bool gzip = false;
  948. bool verbose = false;
  949. if ( flags.find_first_of('z') != flags.npos )
  950. {
  951. gzip = true;
  952. }
  953. if ( flags.find_first_of('v') != flags.npos )
  954. {
  955. verbose = true;
  956. }
  957. if ( flags.find_first_of('t') != flags.npos )
  958. {
  959. if ( !cmSystemTools::ListTar(outFile.c_str(), files, gzip, verbose) )
  960. {
  961. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  962. return 1;
  963. }
  964. }
  965. else if ( flags.find_first_of('c') != flags.npos )
  966. {
  967. if ( !cmSystemTools::CreateTar(
  968. outFile.c_str(), files, gzip, verbose) )
  969. {
  970. cmSystemTools::Error("Problem creating tar: ", outFile.c_str());
  971. return 1;
  972. }
  973. }
  974. else if ( flags.find_first_of('x') != flags.npos )
  975. {
  976. if ( !cmSystemTools::ExtractTar(
  977. outFile.c_str(), files, gzip, verbose) )
  978. {
  979. cmSystemTools::Error("Problem extracting tar: ", outFile.c_str());
  980. return 1;
  981. }
  982. }
  983. return 0;
  984. }
  985. #if defined(CMAKE_BUILD_WITH_CMAKE)
  986. // Internal CMake Fortran module support.
  987. else if (args[1] == "cmake_copy_f90_mod" && args.size() >= 4)
  988. {
  989. return cmDependsFortran::CopyModule(args)? 0 : 1;
  990. }
  991. #endif
  992. #if defined(_WIN32) && !defined(__CYGWIN__)
  993. // Write registry value
  994. else if (args[1] == "write_regv" && args.size() > 3)
  995. {
  996. return cmSystemTools::WriteRegistryValue(args[2].c_str(),
  997. args[3].c_str()) ? 0 : 1;
  998. }
  999. // Delete registry value
  1000. else if (args[1] == "delete_regv" && args.size() > 2)
  1001. {
  1002. return cmSystemTools::DeleteRegistryValue(args[2].c_str()) ? 0 : 1;
  1003. }
  1004. // Remove file
  1005. else if (args[1] == "comspec" && args.size() > 2)
  1006. {
  1007. unsigned int cc;
  1008. std::string command = args[2];
  1009. for ( cc = 3; cc < args.size(); cc ++ )
  1010. {
  1011. command += " " + args[cc];
  1012. }
  1013. return cmWin32ProcessExecution::Windows9xHack(command.c_str());
  1014. }
  1015. #endif
  1016. }
  1017. ::CMakeCommandUsage(args[0].c_str());
  1018. return 1;
  1019. }
  1020. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  1021. {
  1022. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1023. i != this->Generators.end(); ++i)
  1024. {
  1025. names.push_back(i->first);
  1026. }
  1027. }
  1028. cmGlobalGenerator* cmake::CreateGlobalGenerator(const char* name)
  1029. {
  1030. RegisteredGeneratorsMap::const_iterator i = this->Generators.find(name);
  1031. if(i != this->Generators.end())
  1032. {
  1033. cmGlobalGenerator* generator = (i->second)();
  1034. generator->SetCMakeInstance(this);
  1035. return generator;
  1036. }
  1037. else
  1038. {
  1039. return 0;
  1040. }
  1041. }
  1042. void cmake::SetHomeDirectory(const char* dir)
  1043. {
  1044. this->cmHomeDirectory = dir;
  1045. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  1046. }
  1047. void cmake::SetHomeOutputDirectory(const char* lib)
  1048. {
  1049. this->HomeOutputDirectory = lib;
  1050. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1051. }
  1052. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  1053. {
  1054. // delete the old generator
  1055. if (this->GlobalGenerator)
  1056. {
  1057. delete this->GlobalGenerator;
  1058. // restore the original environment variables CXX and CC
  1059. // Restor CC
  1060. std::string env = "CC=";
  1061. if(this->CCEnvironment.size())
  1062. {
  1063. env += this->CCEnvironment;
  1064. }
  1065. cmSystemTools::PutEnv(env.c_str());
  1066. env = "CXX=";
  1067. if(this->CXXEnvironment.size())
  1068. {
  1069. env += this->CXXEnvironment;
  1070. }
  1071. cmSystemTools::PutEnv(env.c_str());
  1072. }
  1073. // set the new
  1074. this->GlobalGenerator = gg;
  1075. // set the global flag for unix style paths on cmSystemTools as
  1076. // soon as the generator is set. This allows gmake to be used
  1077. // on windows.
  1078. cmSystemTools::SetForceUnixPaths(this->GlobalGenerator->GetForceUnixPaths());
  1079. // Save the environment variables CXX and CC
  1080. const char* cxx = getenv("CXX");
  1081. const char* cc = getenv("CC");
  1082. if(cxx)
  1083. {
  1084. this->CXXEnvironment = cxx;
  1085. }
  1086. else
  1087. {
  1088. this->CXXEnvironment = "";
  1089. }
  1090. if(cc)
  1091. {
  1092. this->CCEnvironment = cc;
  1093. }
  1094. else
  1095. {
  1096. this->CCEnvironment = "";
  1097. }
  1098. // set the cmake instance just to be sure
  1099. gg->SetCMakeInstance(this);
  1100. }
  1101. int cmake::DoPreConfigureChecks()
  1102. {
  1103. // Make sure the Start directory contains a CMakeLists.txt file.
  1104. std::string srcList = this->GetHomeDirectory();
  1105. srcList += "/CMakeLists.txt";
  1106. if(!cmSystemTools::FileExists(srcList.c_str()))
  1107. {
  1108. cmOStringStream err;
  1109. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1110. {
  1111. err << "The source directory \"" << this->GetHomeDirectory()
  1112. << "\" does not appear to contain CMakeLists.txt.\n";
  1113. }
  1114. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1115. {
  1116. err << "The source directory \"" << this->GetHomeDirectory()
  1117. << "\" is a file, not a directory.\n";
  1118. }
  1119. else
  1120. {
  1121. err << "The source directory \"" << this->GetHomeDirectory()
  1122. << "\" does not exist.\n";
  1123. }
  1124. err << "Specify --help for usage, or press the help button on the CMake "
  1125. "GUI.";
  1126. cmSystemTools::Error(err.str().c_str());
  1127. return -2;
  1128. }
  1129. // do a sanity check on some values
  1130. if(this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1131. {
  1132. std::string cacheStart =
  1133. this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1134. cacheStart += "/CMakeLists.txt";
  1135. std::string currentStart = this->GetHomeDirectory();
  1136. currentStart += "/CMakeLists.txt";
  1137. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1138. {
  1139. std::string message = "The source \"";
  1140. message += currentStart;
  1141. message += "\" does not match the source \"";
  1142. message += cacheStart;
  1143. message += "\" used to generate cache. ";
  1144. message += "Re-run cmake with a different source directory.";
  1145. cmSystemTools::Error(message.c_str());
  1146. return -2;
  1147. }
  1148. }
  1149. else
  1150. {
  1151. return 0;
  1152. }
  1153. return 1;
  1154. }
  1155. int cmake::Configure()
  1156. {
  1157. // Construct right now our path conversion table before it's too late:
  1158. this->UpdateConversionPathTable();
  1159. this->CleanupCommandsAndMacros();
  1160. int res = 0;
  1161. if ( !this->ScriptMode )
  1162. {
  1163. res = this->DoPreConfigureChecks();
  1164. }
  1165. if ( res < 0 )
  1166. {
  1167. return -2;
  1168. }
  1169. if ( !res )
  1170. {
  1171. this->CacheManager->AddCacheEntry
  1172. ("CMAKE_HOME_DIRECTORY",
  1173. this->GetHomeDirectory(),
  1174. "Start directory with the top level CMakeLists.txt file for this "
  1175. "project",
  1176. cmCacheManager::INTERNAL);
  1177. }
  1178. // set the default BACKWARDS compatibility to the current version
  1179. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1180. {
  1181. char ver[256];
  1182. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1183. cmMakefile::GetMinorVersion());
  1184. this->CacheManager->AddCacheEntry
  1185. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1186. "For backwards compatibility, what version of CMake commands and "
  1187. "syntax should this version of CMake allow.",
  1188. cmCacheManager::STRING);
  1189. }
  1190. // no generator specified on the command line
  1191. if(!this->GlobalGenerator)
  1192. {
  1193. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1194. if(genName)
  1195. {
  1196. this->GlobalGenerator = this->CreateGlobalGenerator(genName);
  1197. }
  1198. if(this->GlobalGenerator)
  1199. {
  1200. // set the global flag for unix style paths on cmSystemTools as
  1201. // soon as the generator is set. This allows gmake to be used
  1202. // on windows.
  1203. cmSystemTools::SetForceUnixPaths
  1204. (this->GlobalGenerator->GetForceUnixPaths());
  1205. }
  1206. else
  1207. {
  1208. #if defined(__BORLANDC__) && defined(_WIN32)
  1209. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1210. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1211. std::string installedCompiler;
  1212. std::string mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1213. "\\VisualStudio\\8.0\\Setup;Dbghelp_path]";
  1214. cmSystemTools::ExpandRegistryValues(mp);
  1215. if (!(mp == "/registry"))
  1216. {
  1217. installedCompiler = "Visual Studio 8 2005";
  1218. }
  1219. else
  1220. {
  1221. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1222. "\\VisualStudio\\7.1;InstallDir]";
  1223. cmSystemTools::ExpandRegistryValues(mp);
  1224. if (!(mp == "/registry"))
  1225. {
  1226. installedCompiler = "Visual Studio 7 .NET 2003";
  1227. }
  1228. else
  1229. {
  1230. mp = "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft"
  1231. "\\VisualStudio\\7.0;InstallDir]";
  1232. cmSystemTools::ExpandRegistryValues(mp);
  1233. if (!(mp == "/registry"))
  1234. {
  1235. installedCompiler = "Visual Studio 7";
  1236. }
  1237. else
  1238. {
  1239. installedCompiler = "Visual Studio 6";
  1240. }
  1241. }
  1242. }
  1243. cmGlobalGenerator* gen
  1244. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1245. if(!gen)
  1246. {
  1247. gen = new cmGlobalNMakeMakefileGenerator;
  1248. }
  1249. this->SetGlobalGenerator(gen);
  1250. #else
  1251. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1252. #endif
  1253. }
  1254. if(!this->GlobalGenerator)
  1255. {
  1256. cmSystemTools::Error("Could not create generator");
  1257. return -1;
  1258. }
  1259. }
  1260. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1261. if(genName)
  1262. {
  1263. if(strcmp(this->GlobalGenerator->GetName(), genName) != 0)
  1264. {
  1265. std::string message = "Error: generator : ";
  1266. message += this->GlobalGenerator->GetName();
  1267. message += "\nDoes not match the generator used previously: ";
  1268. message += genName;
  1269. message +=
  1270. "\nEither remove the CMakeCache.txt file or choose a different"
  1271. " binary directory.";
  1272. cmSystemTools::Error(message.c_str());
  1273. return -2;
  1274. }
  1275. }
  1276. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1277. {
  1278. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1279. this->GlobalGenerator->GetName(),
  1280. "Name of generator.",
  1281. cmCacheManager::INTERNAL);
  1282. }
  1283. // reset any system configuration information, except for when we are
  1284. // InTryCompile. With TryCompile the system info is taken from the parent's
  1285. // info to save time
  1286. if (!this->InTryCompile)
  1287. {
  1288. this->GlobalGenerator->ClearEnabledLanguages();
  1289. }
  1290. this->CleanupWrittenFiles();
  1291. // Truncate log files
  1292. if (!this->InTryCompile)
  1293. {
  1294. this->TruncateOutputLog("CMakeOutput.log");
  1295. this->TruncateOutputLog("CMakeError.log");
  1296. }
  1297. // actually do the configure
  1298. this->GlobalGenerator->Configure();
  1299. // Before saving the cache
  1300. // if the project did not define one of the entries below, add them now
  1301. // so users can edit the values in the cache:
  1302. // LIBRARY_OUTPUT_PATH
  1303. // EXECUTABLE_OUTPUT_PATH
  1304. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1305. {
  1306. this->CacheManager->AddCacheEntry
  1307. ("LIBRARY_OUTPUT_PATH", "",
  1308. "Single output directory for building all libraries.",
  1309. cmCacheManager::PATH);
  1310. }
  1311. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1312. {
  1313. this->CacheManager->AddCacheEntry
  1314. ("EXECUTABLE_OUTPUT_PATH", "",
  1315. "Single output directory for building all executables.",
  1316. cmCacheManager::PATH);
  1317. }
  1318. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1319. {
  1320. this->CacheManager->AddCacheEntry
  1321. ("CMAKE_USE_RELATIVE_PATHS", false,
  1322. "If true, cmake will use relative paths in makefiles and projects.");
  1323. cmCacheManager::CacheIterator it =
  1324. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1325. if ( !it.PropertyExists("ADVANCED") )
  1326. {
  1327. it.SetProperty("ADVANCED", "1");
  1328. }
  1329. }
  1330. if(cmSystemTools::GetFatalErrorOccured() &&
  1331. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1332. cmSystemTools::IsOff(this->CacheManager->
  1333. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1334. {
  1335. // We must have a bad generator selection. Wipe the cache entry so the
  1336. // user can select another.
  1337. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1338. }
  1339. // only save the cache if there were no fatal errors
  1340. if ( !this->ScriptMode && !cmSystemTools::GetFatalErrorOccured() )
  1341. {
  1342. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1343. }
  1344. if ( !this->GraphVizFile.empty() )
  1345. {
  1346. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1347. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1348. }
  1349. if(cmSystemTools::GetErrorOccuredFlag())
  1350. {
  1351. return -1;
  1352. }
  1353. return 0;
  1354. }
  1355. bool cmake::CacheVersionMatches()
  1356. {
  1357. const char* majv =
  1358. this->CacheManager->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION");
  1359. const char* minv =
  1360. this->CacheManager->GetCacheValue("CMAKE_CACHE_MINOR_VERSION");
  1361. const char* relv =
  1362. this->CacheManager->GetCacheValue("CMAKE_CACHE_RELEASE_VERSION");
  1363. bool cacheSameCMake = false;
  1364. if(majv &&
  1365. atoi(majv) == static_cast<int>(cmMakefile::GetMajorVersion())
  1366. && minv &&
  1367. atoi(minv) == static_cast<int>(cmMakefile::GetMinorVersion())
  1368. && relv && (strcmp(relv, cmMakefile::GetReleaseVersion()) == 0))
  1369. {
  1370. cacheSameCMake = true;
  1371. }
  1372. return cacheSameCMake;
  1373. }
  1374. void cmake::PreLoadCMakeFiles()
  1375. {
  1376. std::string pre_load = this->GetHomeDirectory();
  1377. if ( pre_load.size() > 0 )
  1378. {
  1379. pre_load += "/PreLoad.cmake";
  1380. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1381. {
  1382. this->ReadListFile(pre_load.c_str());
  1383. }
  1384. }
  1385. pre_load = this->GetHomeOutputDirectory();
  1386. if ( pre_load.size() > 0 )
  1387. {
  1388. pre_load += "/PreLoad.cmake";
  1389. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1390. {
  1391. this->ReadListFile(pre_load.c_str());
  1392. }
  1393. }
  1394. }
  1395. // handle a command line invocation
  1396. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1397. {
  1398. // Process the arguments
  1399. this->SetArgs(args);
  1400. if(cmSystemTools::GetErrorOccuredFlag())
  1401. {
  1402. CMakeCommandUsage(args[0].c_str());
  1403. return -1;
  1404. }
  1405. // set the cmake command
  1406. this->CMakeCommand = args[0];
  1407. if ( !this->ScriptMode )
  1408. {
  1409. // load the cache
  1410. if(this->LoadCache() < 0)
  1411. {
  1412. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1413. return -1;
  1414. }
  1415. }
  1416. else
  1417. {
  1418. this->AddCMakePaths(this->CMakeCommand.c_str());
  1419. }
  1420. // Add any cache args
  1421. if ( !this->SetCacheArgs(args) )
  1422. {
  1423. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1424. return -1;
  1425. }
  1426. // In script mode we terminate after running the script.
  1427. if(this->ScriptMode)
  1428. {
  1429. if(cmSystemTools::GetErrorOccuredFlag())
  1430. {
  1431. return -1;
  1432. }
  1433. else
  1434. {
  1435. return 0;
  1436. }
  1437. }
  1438. this->PreLoadCMakeFiles();
  1439. std::string systemFile = this->GetHomeOutputDirectory();
  1440. systemFile += "/CMakeSystem.cmake";
  1441. if ( noconfigure )
  1442. {
  1443. return 0;
  1444. }
  1445. // now run the global generate
  1446. // Check the state of the build system to see if we need to regenerate.
  1447. if(!this->CheckBuildSystem())
  1448. {
  1449. return 0;
  1450. }
  1451. // If we are doing global generate, we better set start and start
  1452. // output directory to the root of the project.
  1453. std::string oldstartdir = this->GetStartDirectory();
  1454. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1455. this->SetStartDirectory(this->GetHomeDirectory());
  1456. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1457. int ret = this->Configure();
  1458. if (ret || this->ScriptMode)
  1459. {
  1460. return ret;
  1461. }
  1462. ret = this->Generate();
  1463. std::string message = "Build files have been written to: ";
  1464. message += this->GetHomeOutputDirectory();
  1465. this->UpdateProgress(message.c_str(), -1);
  1466. if(ret)
  1467. {
  1468. return ret;
  1469. }
  1470. this->SetStartDirectory(oldstartdir.c_str());
  1471. this->SetStartOutputDirectory(oldstartoutputdir.c_str());
  1472. return ret;
  1473. }
  1474. int cmake::Generate()
  1475. {
  1476. if(!this->GlobalGenerator)
  1477. {
  1478. return -1;
  1479. }
  1480. this->GlobalGenerator->Generate();
  1481. if(cmSystemTools::GetErrorOccuredFlag())
  1482. {
  1483. return -1;
  1484. }
  1485. return 0;
  1486. }
  1487. unsigned int cmake::GetMajorVersion()
  1488. {
  1489. return cmMakefile::GetMajorVersion();
  1490. }
  1491. unsigned int cmake::GetMinorVersion()
  1492. {
  1493. return cmMakefile::GetMinorVersion();
  1494. }
  1495. const char *cmake::GetReleaseVersion()
  1496. {
  1497. return cmMakefile::GetReleaseVersion();
  1498. }
  1499. void cmake::AddCacheEntry(const char* key, const char* value,
  1500. const char* helpString,
  1501. int type)
  1502. {
  1503. this->CacheManager->AddCacheEntry(key, value,
  1504. helpString,
  1505. cmCacheManager::CacheEntryType(type));
  1506. }
  1507. const char* cmake::GetCacheDefinition(const char* name) const
  1508. {
  1509. return this->CacheManager->GetCacheValue(name);
  1510. }
  1511. int cmake::DumpDocumentationToFile(std::ostream& f)
  1512. {
  1513. #ifdef CMAKE_BUILD_WITH_CMAKE
  1514. // Loop over all registered commands and print out documentation
  1515. const char *name;
  1516. const char *terse;
  1517. const char *full;
  1518. char tmp[1024];
  1519. sprintf(tmp,"Version %d.%d (%s)", cmake::GetMajorVersion(),
  1520. cmake::GetMinorVersion(), cmVersion::GetReleaseVersion().c_str());
  1521. f << "<html>\n";
  1522. f << "<h1>Documentation for commands of CMake " << tmp << "</h1>\n";
  1523. f << "<ul>\n";
  1524. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  1525. j != this->Commands.end(); ++j)
  1526. {
  1527. name = (*j).second->GetName();
  1528. terse = (*j).second->GetTerseDocumentation();
  1529. full = (*j).second->GetFullDocumentation();
  1530. f << "<li><b>" << name << "</b> - " << terse << std::endl
  1531. << "<br><i>Usage:</i> " << full << "</li>" << std::endl << std::endl;
  1532. }
  1533. f << "</ul></html>\n";
  1534. #else
  1535. (void)f;
  1536. #endif
  1537. return 1;
  1538. }
  1539. void cmake::AddDefaultCommands()
  1540. {
  1541. std::list<cmCommand*> commands;
  1542. GetBootstrapCommands(commands);
  1543. GetPredefinedCommands(commands);
  1544. for(std::list<cmCommand*>::iterator i = commands.begin();
  1545. i != commands.end(); ++i)
  1546. {
  1547. this->AddCommand(*i);
  1548. }
  1549. }
  1550. void cmake::AddDefaultGenerators()
  1551. {
  1552. #if defined(_WIN32) && !defined(__CYGWIN__)
  1553. # if !defined(CMAKE_BOOT_MINGW)
  1554. this->Generators[cmGlobalVisualStudio6Generator::GetActualName()] =
  1555. &cmGlobalVisualStudio6Generator::New;
  1556. this->Generators[cmGlobalVisualStudio7Generator::GetActualName()] =
  1557. &cmGlobalVisualStudio7Generator::New;
  1558. this->Generators[cmGlobalVisualStudio71Generator::GetActualName()] =
  1559. &cmGlobalVisualStudio71Generator::New;
  1560. this->Generators[cmGlobalVisualStudio8Generator::GetActualName()] =
  1561. &cmGlobalVisualStudio8Generator::New;
  1562. this->Generators[cmGlobalVisualStudio8Win64Generator::GetActualName()] =
  1563. &cmGlobalVisualStudio8Win64Generator::New;
  1564. this->Generators[cmGlobalBorlandMakefileGenerator::GetActualName()] =
  1565. &cmGlobalBorlandMakefileGenerator::New;
  1566. this->Generators[cmGlobalNMakeMakefileGenerator::GetActualName()] =
  1567. &cmGlobalNMakeMakefileGenerator::New;
  1568. this->Generators[cmGlobalWatcomWMakeGenerator::GetActualName()] =
  1569. &cmGlobalWatcomWMakeGenerator::New;
  1570. # endif
  1571. this->Generators[cmGlobalMSYSMakefileGenerator::GetActualName()] =
  1572. &cmGlobalMSYSMakefileGenerator::New;
  1573. this->Generators[cmGlobalMinGWMakefileGenerator::GetActualName()] =
  1574. &cmGlobalMinGWMakefileGenerator::New;
  1575. #endif
  1576. this->Generators[cmGlobalUnixMakefileGenerator3::GetActualName()] =
  1577. &cmGlobalUnixMakefileGenerator3::New;
  1578. #ifdef CMAKE_USE_XCODE
  1579. this->Generators[cmGlobalXCodeGenerator::GetActualName()] =
  1580. &cmGlobalXCodeGenerator::New;
  1581. #endif
  1582. #ifdef CMAKE_USE_KDEVELOP
  1583. this->Generators[cmGlobalKdevelopGenerator::GetActualName()] =
  1584. &cmGlobalKdevelopGenerator::New;
  1585. #endif
  1586. }
  1587. int cmake::LoadCache()
  1588. {
  1589. // could we not read the cache
  1590. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  1591. {
  1592. // if it does exist, but isn;t readable then warn the user
  1593. std::string cacheFile = this->GetHomeOutputDirectory();
  1594. cacheFile += "/CMakeCache.txt";
  1595. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1596. {
  1597. cmSystemTools::Error(
  1598. "There is a CMakeCache.txt file for the current binary tree but "
  1599. "cmake does not have permission to read it. Please check the "
  1600. "permissions of the directory you are trying to run CMake on.");
  1601. return -1;
  1602. }
  1603. }
  1604. if (this->CMakeCommand.size() < 2)
  1605. {
  1606. cmSystemTools::Error(
  1607. "cmake command was not specified prior to loading the cache in "
  1608. "cmake.cxx");
  1609. return -1;
  1610. }
  1611. // setup CMAKE_ROOT and CMAKE_COMMAND
  1612. if(!this->AddCMakePaths(this->CMakeCommand.c_str()))
  1613. {
  1614. return -3;
  1615. }
  1616. // set the default BACKWARDS compatibility to the current version
  1617. if(!this->CacheManager->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1618. {
  1619. char ver[256];
  1620. sprintf(ver,"%i.%i",cmMakefile::GetMajorVersion(),
  1621. cmMakefile::GetMinorVersion());
  1622. this->CacheManager->AddCacheEntry
  1623. ("CMAKE_BACKWARDS_COMPATIBILITY",ver,
  1624. "For backwards compatibility, what version of CMake commands and "
  1625. "syntax should this version of CMake allow.",
  1626. cmCacheManager::STRING);
  1627. }
  1628. return 0;
  1629. }
  1630. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  1631. {
  1632. this->ProgressCallback = f;
  1633. this->ProgressCallbackClientData = cd;
  1634. }
  1635. void cmake::UpdateProgress(const char *msg, float prog)
  1636. {
  1637. if(this->ProgressCallback && !this->InTryCompile)
  1638. {
  1639. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  1640. return;
  1641. }
  1642. }
  1643. void cmake::GetCommandDocumentation(
  1644. std::vector<cmDocumentationEntry>& v) const
  1645. {
  1646. for(RegisteredCommandsMap::const_iterator j = this->Commands.begin();
  1647. j != this->Commands.end(); ++j)
  1648. {
  1649. cmDocumentationEntry e =
  1650. {
  1651. (*j).second->GetName(),
  1652. (*j).second->GetTerseDocumentation(),
  1653. (*j).second->GetFullDocumentation()
  1654. };
  1655. v.push_back(e);
  1656. }
  1657. cmDocumentationEntry empty = {0,0,0};
  1658. v.push_back(empty);
  1659. }
  1660. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1661. {
  1662. for(RegisteredGeneratorsMap::const_iterator i = this->Generators.begin();
  1663. i != this->Generators.end(); ++i)
  1664. {
  1665. cmDocumentationEntry e;
  1666. cmGlobalGenerator* generator = (i->second)();
  1667. generator->GetDocumentation(e);
  1668. delete generator;
  1669. v.push_back(e);
  1670. }
  1671. cmDocumentationEntry empty = {0,0,0};
  1672. v.push_back(empty);
  1673. }
  1674. void cmake::AddWrittenFile(const char* file)
  1675. {
  1676. this->WrittenFiles.insert(file);
  1677. }
  1678. bool cmake::HasWrittenFile(const char* file)
  1679. {
  1680. return this->WrittenFiles.find(file) != this->WrittenFiles.end();
  1681. }
  1682. void cmake::CleanupWrittenFiles()
  1683. {
  1684. this->WrittenFiles.clear();
  1685. }
  1686. void cmake::UpdateConversionPathTable()
  1687. {
  1688. // Update the path conversion table with any specified file:
  1689. const char* tablepath =
  1690. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1691. if(tablepath)
  1692. {
  1693. std::ifstream table( tablepath );
  1694. if(!table)
  1695. {
  1696. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  1697. ". CMake can not open file.");
  1698. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1699. }
  1700. else
  1701. {
  1702. std::string a, b;
  1703. while(!table.eof())
  1704. {
  1705. // two entries per line
  1706. table >> a; table >> b;
  1707. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1708. }
  1709. }
  1710. }
  1711. }
  1712. //----------------------------------------------------------------------------
  1713. int cmake::CheckBuildSystem()
  1714. {
  1715. // This method will check the integrity of the build system if the
  1716. // option was given on the command line. It reads the given file to
  1717. // determine whether CMake should rerun. If it does rerun then the
  1718. // generation step will check the integrity of dependencies. If it
  1719. // does not then we need to check the integrity here.
  1720. // If no file is provided for the check, we have to rerun.
  1721. if(this->CheckBuildSystemArgument.size() == 0)
  1722. {
  1723. return 1;
  1724. }
  1725. // If the file provided does not exist, we have to rerun.
  1726. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  1727. {
  1728. return 1;
  1729. }
  1730. // Read the rerun check file and use it to decide whether to do the
  1731. // global generate.
  1732. cmake cm;
  1733. cmGlobalGenerator gg;
  1734. gg.SetCMakeInstance(&cm);
  1735. std::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1736. lg->SetGlobalGenerator(&gg);
  1737. cmMakefile* mf = lg->GetMakefile();
  1738. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  1739. cmSystemTools::GetErrorOccuredFlag())
  1740. {
  1741. // There was an error reading the file. Just rerun.
  1742. return 1;
  1743. }
  1744. // Get the set of dependencies and outputs.
  1745. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1746. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1747. if(!dependsStr || !outputsStr)
  1748. {
  1749. // Not enough information was provided to do the test. Just rerun.
  1750. return 1;
  1751. }
  1752. std::vector<std::string> depends;
  1753. std::vector<std::string> outputs;
  1754. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1755. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1756. // If any output is older than any dependency then rerun.
  1757. for(std::vector<std::string>::iterator dep = depends.begin();
  1758. dep != depends.end(); ++dep)
  1759. {
  1760. for(std::vector<std::string>::iterator out = outputs.begin();
  1761. out != outputs.end(); ++out)
  1762. {
  1763. int result = 0;
  1764. if(!this->FileComparison->FileTimeCompare(out->c_str(),
  1765. dep->c_str(), &result) ||
  1766. result < 0)
  1767. {
  1768. return 1;
  1769. }
  1770. }
  1771. }
  1772. // We do not need to rerun CMake. Check dependency integrity. Use
  1773. // the make system's VERBOSE environment variable to enable verbose
  1774. // output.
  1775. bool verbose = cmSystemTools::GetEnv("VERBOSE") != 0;
  1776. // compute depends based on the generator specified
  1777. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1778. if (!genName || genName[0] == '\0')
  1779. {
  1780. genName = "Unix Makefiles";
  1781. }
  1782. cmGlobalGenerator *ggd = this->CreateGlobalGenerator(genName);
  1783. if (ggd)
  1784. {
  1785. std::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1786. lgd->SetGlobalGenerator(ggd);
  1787. lgd->CheckDependencies(mf, verbose, this->ClearBuildSystem);
  1788. }
  1789. // No need to rerun.
  1790. return 0;
  1791. }
  1792. //----------------------------------------------------------------------------
  1793. void cmake::TruncateOutputLog(const char* fname)
  1794. {
  1795. std::string fullPath = this->GetHomeOutputDirectory();
  1796. fullPath += "/";
  1797. fullPath += fname;
  1798. struct stat st;
  1799. if ( ::stat(fullPath.c_str(), &st) )
  1800. {
  1801. return;
  1802. }
  1803. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  1804. {
  1805. cmSystemTools::RemoveFile(fullPath.c_str());
  1806. return;
  1807. }
  1808. size_t fsize = st.st_size;
  1809. const size_t maxFileSize = 50 * 1024;
  1810. if ( fsize < maxFileSize )
  1811. {
  1812. //TODO: truncate file
  1813. return;
  1814. }
  1815. }
  1816. inline std::string removeQuotes(const std::string& s)
  1817. {
  1818. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1819. {
  1820. return s.substr(1, s.size()-2);
  1821. }
  1822. return s;
  1823. }
  1824. const char* cmake::GetCTestCommand()
  1825. {
  1826. if ( !this->CTestCommand.empty() )
  1827. {
  1828. return this->CTestCommand.c_str();
  1829. }
  1830. cmMakefile* mf
  1831. = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  1832. #ifdef CMAKE_BUILD_WITH_CMAKE
  1833. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1834. this->CTestCommand = removeQuotes(this->CTestCommand);
  1835. this->CTestCommand = cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  1836. this->CTestCommand += "/";
  1837. this->CTestCommand += "ctest";
  1838. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1839. if(!cmSystemTools::FileExists(this->CTestCommand.c_str()))
  1840. {
  1841. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1842. this->CTestCommand = cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  1843. this->CTestCommand += "/Debug/";
  1844. this->CTestCommand += "ctest";
  1845. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1846. }
  1847. if(!cmSystemTools::FileExists(this->CTestCommand.c_str()))
  1848. {
  1849. this->CTestCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1850. this->CTestCommand = cmSystemTools::GetFilenamePath(this->CTestCommand.c_str());
  1851. this->CTestCommand += "/Release/";
  1852. this->CTestCommand += "ctest";
  1853. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1854. }
  1855. #else
  1856. // Only for bootstrap
  1857. this->CTestCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  1858. this->CTestCommand += "/ctest";
  1859. this->CTestCommand += cmSystemTools::GetExecutableExtension();
  1860. #endif
  1861. if ( this->CTestCommand.empty() )
  1862. {
  1863. cmSystemTools::Error("Cannot find the CTest executable");
  1864. this->CTestCommand = "CTEST-COMMAND-NOT-FOUND";
  1865. }
  1866. return this->CTestCommand.c_str();
  1867. }
  1868. const char* cmake::GetCPackCommand()
  1869. {
  1870. if ( !this->CPackCommand.empty() )
  1871. {
  1872. return this->CPackCommand.c_str();
  1873. }
  1874. cmMakefile* mf
  1875. = this->GetGlobalGenerator()->GetLocalGenerator(0)->GetMakefile();
  1876. #ifdef CMAKE_BUILD_WITH_CMAKE
  1877. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1878. this->CPackCommand = removeQuotes(this->CPackCommand);
  1879. this->CPackCommand = cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  1880. this->CPackCommand += "/";
  1881. this->CPackCommand += "cpack";
  1882. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1883. if(!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  1884. {
  1885. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1886. this->CPackCommand = cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  1887. this->CPackCommand += "/Debug/";
  1888. this->CPackCommand += "cpack";
  1889. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1890. }
  1891. if(!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  1892. {
  1893. this->CPackCommand = mf->GetRequiredDefinition("CMAKE_COMMAND");
  1894. this->CPackCommand = cmSystemTools::GetFilenamePath(this->CPackCommand.c_str());
  1895. this->CPackCommand += "/Release/";
  1896. this->CPackCommand += "cpack";
  1897. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1898. }
  1899. if (!cmSystemTools::FileExists(this->CPackCommand.c_str()))
  1900. {
  1901. cmSystemTools::Error("Cannot find the CPack executable");
  1902. this->CPackCommand = "CPACK-COMMAND-NOT-FOUND";
  1903. }
  1904. #else
  1905. // Only for bootstrap
  1906. this->CPackCommand += mf->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  1907. this->CPackCommand += "/cpack";
  1908. this->CPackCommand += cmSystemTools::GetExecutableExtension();
  1909. #endif
  1910. return this->CPackCommand.c_str();
  1911. }
  1912. void cmake::GenerateGraphViz(const char* fileName)
  1913. {
  1914. cmGeneratedFileStream str(fileName);
  1915. if ( !str )
  1916. {
  1917. return;
  1918. }
  1919. cmake cm;
  1920. cmGlobalGenerator ggi;
  1921. ggi.SetCMakeInstance(&cm);
  1922. std::auto_ptr<cmLocalGenerator> lg(ggi.CreateLocalGenerator());
  1923. lg->SetGlobalGenerator(&ggi);
  1924. cmMakefile *mf = lg->GetMakefile();
  1925. std::string infile = this->GetHomeOutputDirectory();
  1926. infile += "/CMakeGraphVizOptions.cmake";
  1927. if ( !cmSystemTools::FileExists(infile.c_str()) )
  1928. {
  1929. infile = this->GetHomeDirectory();
  1930. infile += "/CMakeGraphVizOptions.cmake";
  1931. if ( !cmSystemTools::FileExists(infile.c_str()) )
  1932. {
  1933. infile = "";
  1934. }
  1935. }
  1936. if ( !infile.empty() )
  1937. {
  1938. if ( !mf->ReadListFile(0, infile.c_str()) )
  1939. {
  1940. cmSystemTools::Error("Problem opening GraphViz options file: ",
  1941. infile.c_str());
  1942. return;
  1943. }
  1944. std::cout << "Read GraphViz options file: " << infile.c_str()
  1945. << std::endl;
  1946. }
  1947. #define __set_if_not_set(var, value, cmakeDefinition) \
  1948. const char* var = mf->GetDefinition(cmakeDefinition); \
  1949. if ( !var ) \
  1950. { \
  1951. var = value; \
  1952. }
  1953. __set_if_not_set(graphType, "digraph", "GRAPHVIZ_GRAPH_TYPE");
  1954. __set_if_not_set(graphName, "GG", "GRAPHVIZ_GRAPH_NAME");
  1955. __set_if_not_set(graphHeader, "node [\n fontsize = \"12\"\n];",
  1956. "GRAPHVIZ_GRAPH_HEADER");
  1957. __set_if_not_set(graphNodePrefix, "node", "GRAPHVIZ_NODE_PREFIX");
  1958. const char* ignoreTargets = mf->GetDefinition("GRAPHVIZ_IGNORE_TARGETS");
  1959. std::set<cmStdString> ignoreTargetsSet;
  1960. if ( ignoreTargets )
  1961. {
  1962. std::vector<std::string> ignoreTargetsVector;
  1963. cmSystemTools::ExpandListArgument(ignoreTargets,ignoreTargetsVector);
  1964. std::vector<std::string>::iterator itvIt;
  1965. for ( itvIt = ignoreTargetsVector.begin();
  1966. itvIt != ignoreTargetsVector.end();
  1967. ++ itvIt )
  1968. {
  1969. ignoreTargetsSet.insert(itvIt->c_str());
  1970. }
  1971. }
  1972. str << graphType << " " << graphName << " {" << std::endl;
  1973. str << graphHeader << std::endl;
  1974. cmGlobalGenerator* gg = this->GetGlobalGenerator();
  1975. std::vector<cmLocalGenerator*> localGenerators;
  1976. gg->GetLocalGenerators(localGenerators);
  1977. std::vector<cmLocalGenerator*>::iterator lit;
  1978. // for target deps
  1979. // 1 - cmake target
  1980. // 2 - external target
  1981. // 0 - no deps
  1982. std::map<cmStdString, int> targetDeps;
  1983. std::map<cmStdString, cmTarget*> targetPtrs;
  1984. std::map<cmStdString, cmStdString> targetNamesNodes;
  1985. char tgtName[100];
  1986. int cnt = 0;
  1987. // First pass get the list of all cmake targets
  1988. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  1989. {
  1990. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  1991. cmTargets::iterator tit;
  1992. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  1993. {
  1994. const char* realTargetName = tit->first.c_str();
  1995. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  1996. {
  1997. // Skip ignored targets
  1998. continue;
  1999. }
  2000. //std::cout << "Found target: " << tit->first.c_str() << std::endl;
  2001. sprintf(tgtName, "%s%d", graphNodePrefix, cnt++);
  2002. targetNamesNodes[realTargetName] = tgtName;
  2003. targetPtrs[realTargetName] = &tit->second;
  2004. //str << " \"" << tgtName << "\" [ label=\"" << tit->first.c_str()
  2005. //<< "\" shape=\"box\"];" << std::endl;
  2006. }
  2007. }
  2008. // Ok, now find all the stuff we link to that is not in cmake
  2009. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2010. {
  2011. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2012. cmTargets::iterator tit;
  2013. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2014. {
  2015. const cmTarget::LinkLibraryVectorType* ll
  2016. = &(tit->second.GetOriginalLinkLibraries());
  2017. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2018. const char* realTargetName = tit->first.c_str();
  2019. if ( ignoreTargetsSet.find(realTargetName) != ignoreTargetsSet.end() )
  2020. {
  2021. // Skip ignored targets
  2022. continue;
  2023. }
  2024. if ( ll->size() > 0 )
  2025. {
  2026. targetDeps[realTargetName] = 1;
  2027. }
  2028. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2029. {
  2030. const char* libName = llit->first.c_str();
  2031. std::map<cmStdString, cmStdString>::iterator tarIt
  2032. = targetNamesNodes.find(libName);
  2033. if ( ignoreTargetsSet.find(libName) != ignoreTargetsSet.end() )
  2034. {
  2035. // Skip ignored targets
  2036. continue;
  2037. }
  2038. if ( tarIt == targetNamesNodes.end() )
  2039. {
  2040. sprintf(tgtName, "%s%d", graphNodePrefix, cnt++);
  2041. targetDeps[libName] = 2;
  2042. targetNamesNodes[libName] = tgtName;
  2043. //str << " \"" << tgtName << "\" [ label=\"" << libName
  2044. //<< "\" shape=\"ellipse\"];" << std::endl;
  2045. }
  2046. else
  2047. {
  2048. std::map<cmStdString, int>::iterator depIt
  2049. = targetDeps.find(libName);
  2050. if ( depIt == targetDeps.end() )
  2051. {
  2052. targetDeps[libName] = 1;
  2053. }
  2054. }
  2055. }
  2056. }
  2057. }
  2058. // Write out nodes
  2059. std::map<cmStdString, int>::iterator depIt;
  2060. for ( depIt = targetDeps.begin(); depIt != targetDeps.end(); ++ depIt )
  2061. {
  2062. const char* newTargetName = depIt->first.c_str();
  2063. std::map<cmStdString, cmStdString>::iterator tarIt
  2064. = targetNamesNodes.find(newTargetName);
  2065. if ( tarIt == targetNamesNodes.end() )
  2066. {
  2067. // We should not be here.
  2068. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2069. << " even though it was added in the previous pass" << std::endl;
  2070. abort();
  2071. }
  2072. str << " \"" << tarIt->second.c_str() << "\" [ label=\""
  2073. << newTargetName << "\" shape=\"";
  2074. if ( depIt->second == 1 )
  2075. {
  2076. std::map<cmStdString, cmTarget*>::iterator tarTypeIt= targetPtrs.find(
  2077. newTargetName);
  2078. if ( tarTypeIt == targetPtrs.end() )
  2079. {
  2080. // We should not be here.
  2081. std::cout << __LINE__ << " Cannot find library: " << newTargetName
  2082. << " even though it was added in the previous pass" << std::endl;
  2083. abort();
  2084. }
  2085. cmTarget* tg = tarTypeIt->second;
  2086. switch ( tg->GetType() )
  2087. {
  2088. case cmTarget::EXECUTABLE:
  2089. str << "house";
  2090. break;
  2091. case cmTarget::STATIC_LIBRARY:
  2092. str << "diamond";
  2093. break;
  2094. case cmTarget::SHARED_LIBRARY:
  2095. str << "polygon";
  2096. break;
  2097. case cmTarget::MODULE_LIBRARY:
  2098. str << "octagon";
  2099. break;
  2100. default:
  2101. str << "box";
  2102. }
  2103. }
  2104. else
  2105. {
  2106. str << "ellipse";
  2107. }
  2108. str << "\"];" << std::endl;
  2109. }
  2110. // Now generate the connectivity
  2111. for ( lit = localGenerators.begin(); lit != localGenerators.end(); ++ lit )
  2112. {
  2113. cmTargets* targets = &((*lit)->GetMakefile()->GetTargets());
  2114. cmTargets::iterator tit;
  2115. for ( tit = targets->begin(); tit != targets->end(); ++ tit )
  2116. {
  2117. std::map<cmStdString, int>::iterator dependIt
  2118. = targetDeps.find(tit->first.c_str());
  2119. if ( dependIt == targetDeps.end() )
  2120. {
  2121. continue;
  2122. }
  2123. std::map<cmStdString, cmStdString>::iterator cmakeTarIt
  2124. = targetNamesNodes.find(tit->first.c_str());
  2125. const cmTarget::LinkLibraryVectorType* ll
  2126. = &(tit->second.GetOriginalLinkLibraries());
  2127. cmTarget::LinkLibraryVectorType::const_iterator llit;
  2128. for ( llit = ll->begin(); llit != ll->end(); ++ llit )
  2129. {
  2130. const char* libName = llit->first.c_str();
  2131. std::map<cmStdString, cmStdString>::iterator tarIt
  2132. = targetNamesNodes.find(libName);
  2133. if ( tarIt == targetNamesNodes.end() )
  2134. {
  2135. // We should not be here.
  2136. std::cout << __LINE__ << " Cannot find library: " << libName
  2137. << " even though it was added in the previous pass" << std::endl;
  2138. abort();
  2139. }
  2140. str << " \"" << cmakeTarIt->second.c_str() << "\" -> \""
  2141. << tarIt->second.c_str() << "\"" << std::endl;
  2142. }
  2143. }
  2144. }
  2145. // TODO: Use dotted or something for external libraries
  2146. //str << " \"node0\":f4 -> \"node12\"[color=\"#0000ff\" style=dotted]"
  2147. //<< std::endl;
  2148. //
  2149. str << "}" << std::endl;
  2150. }