cmake.cxx 66 KB

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