cmake.cxx 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmake.h"
  11. #include "cmCacheManager.h"
  12. #include "cmMakefile.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmExternalMakefileProjectGenerator.h"
  15. #include "cmCommands.h"
  16. #include "cmCommand.h"
  17. #include "cmFileTimeComparison.h"
  18. #include "cmSourceFile.h"
  19. #include "cmTest.h"
  20. #include "cmDocumentationFormatter.h"
  21. #if defined(CMAKE_BUILD_WITH_CMAKE)
  22. # include "cmGraphVizWriter.h"
  23. # include "cmVariableWatch.h"
  24. # include <cmsys/SystemInformation.hxx>
  25. #endif
  26. #include <cmsys/Glob.hxx>
  27. #include <cmsys/RegularExpression.hxx>
  28. #include <cmsys/FStream.hxx>
  29. // only build kdevelop generator on non-windows platforms
  30. // when not bootstrapping cmake
  31. #if !defined(_WIN32)
  32. # if defined(CMAKE_BUILD_WITH_CMAKE)
  33. # define CMAKE_USE_KDEVELOP
  34. # endif
  35. #endif
  36. #if defined(CMAKE_BUILD_WITH_CMAKE)
  37. # define CMAKE_USE_ECLIPSE
  38. #endif
  39. #if defined(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
  40. # define CMAKE_BOOT_MINGW
  41. #endif
  42. // include the generator
  43. #if defined(_WIN32) && !defined(__CYGWIN__)
  44. # if !defined(CMAKE_BOOT_MINGW)
  45. # include "cmGlobalVisualStudio6Generator.h"
  46. # include "cmGlobalVisualStudio7Generator.h"
  47. # include "cmGlobalVisualStudio71Generator.h"
  48. # include "cmGlobalVisualStudio8Generator.h"
  49. # include "cmGlobalVisualStudio9Generator.h"
  50. # include "cmGlobalVisualStudio10Generator.h"
  51. # include "cmGlobalVisualStudio11Generator.h"
  52. # include "cmGlobalVisualStudio12Generator.h"
  53. # include "cmGlobalBorlandMakefileGenerator.h"
  54. # include "cmGlobalNMakeMakefileGenerator.h"
  55. # include "cmGlobalJOMMakefileGenerator.h"
  56. # define CMAKE_HAVE_VS_GENERATORS
  57. # endif
  58. # include "cmGlobalMSYSMakefileGenerator.h"
  59. # include "cmGlobalMinGWMakefileGenerator.h"
  60. #else
  61. #endif
  62. #if defined(CMAKE_USE_WMAKE)
  63. # include "cmGlobalWatcomWMakeGenerator.h"
  64. #endif
  65. #include "cmGlobalUnixMakefileGenerator3.h"
  66. #include "cmGlobalNinjaGenerator.h"
  67. #include "cmExtraCodeLiteGenerator.h"
  68. #if !defined(CMAKE_BOOT_MINGW)
  69. # include "cmExtraCodeBlocksGenerator.h"
  70. #endif
  71. #include "cmExtraSublimeTextGenerator.h"
  72. #include "cmExtraKateGenerator.h"
  73. #ifdef CMAKE_USE_KDEVELOP
  74. # include "cmGlobalKdevelopGenerator.h"
  75. #endif
  76. #ifdef CMAKE_USE_ECLIPSE
  77. # include "cmExtraEclipseCDT4Generator.h"
  78. #endif
  79. #include <stdlib.h> // required for atoi
  80. #if defined( __APPLE__ )
  81. # if defined(CMAKE_BUILD_WITH_CMAKE)
  82. # include "cmGlobalXCodeGenerator.h"
  83. # define CMAKE_USE_XCODE 1
  84. # endif
  85. # include <sys/types.h>
  86. # include <sys/time.h>
  87. # include <sys/resource.h>
  88. #endif
  89. #include <sys/stat.h> // struct stat
  90. static bool cmakeCheckStampFile(const char* stampName);
  91. static bool cmakeCheckStampList(const char* stampName);
  92. void cmWarnUnusedCliWarning(const std::string& variable,
  93. int, void* ctx, const char*, const cmMakefile*)
  94. {
  95. cmake* cm = reinterpret_cast<cmake*>(ctx);
  96. cm->MarkCliAsUsed(variable);
  97. }
  98. cmake::cmake()
  99. {
  100. this->Trace = false;
  101. this->WarnUninitialized = false;
  102. this->WarnUnused = false;
  103. this->WarnUnusedCli = true;
  104. this->CheckSystemVars = false;
  105. this->SuppressDevWarnings = false;
  106. this->DoSuppressDevWarnings = false;
  107. this->DebugOutput = false;
  108. this->DebugTryCompile = false;
  109. this->ClearBuildSystem = false;
  110. this->FileComparison = new cmFileTimeComparison;
  111. this->Policies = new cmPolicies();
  112. this->InitializeProperties();
  113. #ifdef __APPLE__
  114. struct rlimit rlp;
  115. if(!getrlimit(RLIMIT_STACK, &rlp))
  116. {
  117. if(rlp.rlim_cur != rlp.rlim_max)
  118. {
  119. rlp.rlim_cur = rlp.rlim_max;
  120. setrlimit(RLIMIT_STACK, &rlp);
  121. }
  122. }
  123. #endif
  124. this->Verbose = false;
  125. this->InTryCompile = false;
  126. this->CacheManager = new cmCacheManager(this);
  127. this->GlobalGenerator = 0;
  128. this->ProgressCallback = 0;
  129. this->ProgressCallbackClientData = 0;
  130. this->CurrentWorkingMode = NORMAL_MODE;
  131. #ifdef CMAKE_BUILD_WITH_CMAKE
  132. this->VariableWatch = new cmVariableWatch;
  133. #endif
  134. this->AddDefaultGenerators();
  135. this->AddDefaultExtraGenerators();
  136. this->AddDefaultCommands();
  137. // Make sure we can capture the build tool output.
  138. cmSystemTools::EnableVSConsoleOutput();
  139. }
  140. cmake::~cmake()
  141. {
  142. delete this->CacheManager;
  143. delete this->Policies;
  144. if (this->GlobalGenerator)
  145. {
  146. delete this->GlobalGenerator;
  147. this->GlobalGenerator = 0;
  148. }
  149. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  150. j != this->Commands.end(); ++j)
  151. {
  152. delete (*j).second;
  153. }
  154. for(RegisteredGeneratorsVector::iterator j = this->Generators.begin();
  155. j != this->Generators.end(); ++j)
  156. {
  157. delete *j;
  158. }
  159. #ifdef CMAKE_BUILD_WITH_CMAKE
  160. delete this->VariableWatch;
  161. #endif
  162. delete this->FileComparison;
  163. }
  164. void cmake::InitializeProperties()
  165. {
  166. this->Properties.clear();
  167. this->Properties.SetCMakeInstance(this);
  168. this->AccessedProperties.clear();
  169. this->PropertyDefinitions.clear();
  170. // initialize properties
  171. cmTarget::DefineProperties(this);
  172. cmMakefile::DefineProperties(this);
  173. }
  174. void cmake::CleanupCommandsAndMacros()
  175. {
  176. this->InitializeProperties();
  177. std::vector<cmCommand*> commands;
  178. for(RegisteredCommandsMap::iterator j = this->Commands.begin();
  179. j != this->Commands.end(); ++j)
  180. {
  181. if ( !j->second->IsA("cmMacroHelperCommand") &&
  182. !j->second->IsA("cmFunctionHelperCommand"))
  183. {
  184. commands.push_back(j->second);
  185. }
  186. else
  187. {
  188. delete j->second;
  189. }
  190. }
  191. this->Commands.erase(this->Commands.begin(), this->Commands.end());
  192. std::vector<cmCommand*>::iterator it;
  193. for ( it = commands.begin(); it != commands.end();
  194. ++ it )
  195. {
  196. this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
  197. }
  198. }
  199. bool cmake::CommandExists(const std::string& name) const
  200. {
  201. std::string sName = cmSystemTools::LowerCase(name);
  202. return (this->Commands.find(sName) != this->Commands.end());
  203. }
  204. cmCommand *cmake::GetCommand(const std::string& name)
  205. {
  206. cmCommand* rm = 0;
  207. std::string sName = cmSystemTools::LowerCase(name);
  208. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  209. if (pos != this->Commands.end())
  210. {
  211. rm = (*pos).second;
  212. }
  213. return rm;
  214. }
  215. void cmake::RenameCommand(const std::string& oldName,
  216. const std::string& newName)
  217. {
  218. // if the command already exists, free the old one
  219. std::string sOldName = cmSystemTools::LowerCase(oldName);
  220. std::string sNewName = cmSystemTools::LowerCase(newName);
  221. RegisteredCommandsMap::iterator pos = this->Commands.find(sOldName);
  222. if ( pos == this->Commands.end() )
  223. {
  224. return;
  225. }
  226. cmCommand* cmd = pos->second;
  227. pos = this->Commands.find(sNewName);
  228. if (pos != this->Commands.end())
  229. {
  230. delete pos->second;
  231. this->Commands.erase(pos);
  232. }
  233. this->Commands.insert(RegisteredCommandsMap::value_type(sNewName, cmd));
  234. pos = this->Commands.find(sOldName);
  235. this->Commands.erase(pos);
  236. }
  237. void cmake::RemoveCommand(const std::string& name)
  238. {
  239. std::string sName = cmSystemTools::LowerCase(name);
  240. RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  241. if ( pos != this->Commands.end() )
  242. {
  243. delete pos->second;
  244. this->Commands.erase(pos);
  245. }
  246. }
  247. void cmake::AddCommand(cmCommand* wg)
  248. {
  249. std::string name = cmSystemTools::LowerCase(wg->GetName());
  250. // if the command already exists, free the old one
  251. RegisteredCommandsMap::iterator pos = this->Commands.find(name);
  252. if (pos != this->Commands.end())
  253. {
  254. delete pos->second;
  255. this->Commands.erase(pos);
  256. }
  257. this->Commands.insert( RegisteredCommandsMap::value_type(name, wg));
  258. }
  259. void cmake::RemoveUnscriptableCommands()
  260. {
  261. std::vector<std::string> unscriptableCommands;
  262. cmake::RegisteredCommandsMap* commands = this->GetCommands();
  263. for (cmake::RegisteredCommandsMap::const_iterator pos = commands->begin();
  264. pos != commands->end();
  265. ++pos)
  266. {
  267. if (!pos->second->IsScriptable())
  268. {
  269. unscriptableCommands.push_back(pos->first);
  270. }
  271. }
  272. for(std::vector<std::string>::const_iterator it=unscriptableCommands.begin();
  273. it != unscriptableCommands.end();
  274. ++it)
  275. {
  276. this->RemoveCommand(*it);
  277. }
  278. }
  279. // Parse the args
  280. bool cmake::SetCacheArgs(const std::vector<std::string>& args)
  281. {
  282. bool findPackageMode = false;
  283. for(unsigned int i=1; i < args.size(); ++i)
  284. {
  285. std::string arg = args[i];
  286. if(arg.find("-D",0) == 0)
  287. {
  288. std::string entry = arg.substr(2);
  289. if(entry.size() == 0)
  290. {
  291. ++i;
  292. if(i < args.size())
  293. {
  294. entry = args[i];
  295. }
  296. else
  297. {
  298. cmSystemTools::Error("-D must be followed with VAR=VALUE.");
  299. return false;
  300. }
  301. }
  302. std::string var, value;
  303. cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
  304. if(cmCacheManager::ParseEntry(entry, var, value, type))
  305. {
  306. // The value is transformed if it is a filepath for example, so
  307. // we can't compare whether the value is already in the cache until
  308. // after we call AddCacheEntry.
  309. const char *cachedValue =
  310. this->CacheManager->GetCacheValue(var);
  311. this->CacheManager->AddCacheEntry(var, value.c_str(),
  312. "No help, variable specified on the command line.", type);
  313. if(this->WarnUnusedCli)
  314. {
  315. if (!cachedValue
  316. || strcmp(this->CacheManager->GetCacheValue(var),
  317. cachedValue) != 0)
  318. {
  319. this->WatchUnusedCli(var);
  320. }
  321. }
  322. }
  323. else
  324. {
  325. std::cerr << "Parse error in command line argument: " << arg << "\n"
  326. << "Should be: VAR:type=value\n";
  327. cmSystemTools::Error("No cmake script provided.");
  328. return false;
  329. }
  330. }
  331. else if(arg.find("-Wno-dev",0) == 0)
  332. {
  333. this->SuppressDevWarnings = true;
  334. this->DoSuppressDevWarnings = true;
  335. }
  336. else if(arg.find("-Wdev",0) == 0)
  337. {
  338. this->SuppressDevWarnings = false;
  339. this->DoSuppressDevWarnings = true;
  340. }
  341. else if(arg.find("-U",0) == 0)
  342. {
  343. std::string entryPattern = arg.substr(2);
  344. if(entryPattern.size() == 0)
  345. {
  346. ++i;
  347. if(i < args.size())
  348. {
  349. entryPattern = args[i];
  350. }
  351. else
  352. {
  353. cmSystemTools::Error("-U must be followed with VAR.");
  354. return false;
  355. }
  356. }
  357. cmsys::RegularExpression regex(
  358. cmsys::Glob::PatternToRegex(entryPattern, true, true).c_str());
  359. //go through all cache entries and collect the vars which will be removed
  360. std::vector<std::string> entriesToDelete;
  361. cmCacheManager::CacheIterator it =
  362. this->CacheManager->GetCacheIterator();
  363. for ( it.Begin(); !it.IsAtEnd(); it.Next() )
  364. {
  365. cmCacheManager::CacheEntryType t = it.GetType();
  366. if(t != cmCacheManager::STATIC)
  367. {
  368. std::string entryName = it.GetName();
  369. if (regex.find(entryName.c_str()))
  370. {
  371. entriesToDelete.push_back(entryName);
  372. }
  373. }
  374. }
  375. // now remove them from the cache
  376. for(std::vector<std::string>::const_iterator currentEntry =
  377. entriesToDelete.begin();
  378. currentEntry != entriesToDelete.end();
  379. ++currentEntry)
  380. {
  381. this->CacheManager->RemoveCacheEntry(*currentEntry);
  382. }
  383. }
  384. else if(arg.find("-C",0) == 0)
  385. {
  386. std::string path = arg.substr(2);
  387. if ( path.size() == 0 )
  388. {
  389. ++i;
  390. if(i < args.size())
  391. {
  392. path = args[i];
  393. }
  394. else
  395. {
  396. cmSystemTools::Error("-C must be followed by a file name.");
  397. return false;
  398. }
  399. }
  400. std::cerr << "loading initial cache file " << path << "\n";
  401. this->ReadListFile(args, path.c_str());
  402. }
  403. else if(arg.find("-P",0) == 0)
  404. {
  405. i++;
  406. if(i >= args.size())
  407. {
  408. cmSystemTools::Error("-P must be followed by a file name.");
  409. return false;
  410. }
  411. std::string path = args[i];
  412. if ( path.size() == 0 )
  413. {
  414. cmSystemTools::Error("No cmake script provided.");
  415. return false;
  416. }
  417. this->ReadListFile(args, path.c_str());
  418. }
  419. else if (arg.find("--find-package",0) == 0)
  420. {
  421. findPackageMode = true;
  422. }
  423. }
  424. if (findPackageMode)
  425. {
  426. return this->FindPackage(args);
  427. }
  428. return true;
  429. }
  430. void cmake::ReadListFile(const std::vector<std::string>& args,
  431. const char *path)
  432. {
  433. // if a generator was not yet created, temporarily create one
  434. cmGlobalGenerator *gg = this->GetGlobalGenerator();
  435. bool created = false;
  436. // if a generator was not specified use a generic one
  437. if (!gg)
  438. {
  439. gg = new cmGlobalGenerator;
  440. gg->SetCMakeInstance(this);
  441. created = true;
  442. }
  443. // read in the list file to fill the cache
  444. if(path)
  445. {
  446. cmsys::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  447. lg->GetMakefile()->SetHomeOutputDirectory
  448. (cmSystemTools::GetCurrentWorkingDirectory());
  449. lg->GetMakefile()->SetStartOutputDirectory
  450. (cmSystemTools::GetCurrentWorkingDirectory());
  451. lg->GetMakefile()->SetHomeDirectory
  452. (cmSystemTools::GetCurrentWorkingDirectory());
  453. lg->GetMakefile()->SetStartDirectory
  454. (cmSystemTools::GetCurrentWorkingDirectory());
  455. if (this->GetWorkingMode() != NORMAL_MODE)
  456. {
  457. std::string file(cmSystemTools::CollapseFullPath(path));
  458. cmSystemTools::ConvertToUnixSlashes(file);
  459. lg->GetMakefile()->SetScriptModeFile(file.c_str());
  460. lg->GetMakefile()->SetArgcArgv(args);
  461. }
  462. if (!lg->GetMakefile()->ReadListFile(0, path))
  463. {
  464. cmSystemTools::Error("Error processing file: ", path);
  465. }
  466. }
  467. // free generic one if generated
  468. if (created)
  469. {
  470. delete gg;
  471. }
  472. }
  473. bool cmake::FindPackage(const std::vector<std::string>& args)
  474. {
  475. // if a generator was not yet created, temporarily create one
  476. cmGlobalGenerator *gg = new cmGlobalGenerator;
  477. gg->SetCMakeInstance(this);
  478. this->SetGlobalGenerator(gg);
  479. // read in the list file to fill the cache
  480. cmsys::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
  481. cmMakefile* mf = lg->GetMakefile();
  482. mf->SetHomeOutputDirectory
  483. (cmSystemTools::GetCurrentWorkingDirectory());
  484. mf->SetStartOutputDirectory
  485. (cmSystemTools::GetCurrentWorkingDirectory());
  486. mf->SetHomeDirectory
  487. (cmSystemTools::GetCurrentWorkingDirectory());
  488. mf->SetStartDirectory
  489. (cmSystemTools::GetCurrentWorkingDirectory());
  490. mf->SetArgcArgv(args);
  491. std::string systemFile = mf->GetModulesFile("CMakeFindPackageMode.cmake");
  492. mf->ReadListFile(0, systemFile.c_str());
  493. std::string language = mf->GetSafeDefinition("LANGUAGE");
  494. std::string mode = mf->GetSafeDefinition("MODE");
  495. std::string packageName = mf->GetSafeDefinition("NAME");
  496. bool packageFound = mf->IsOn("PACKAGE_FOUND");
  497. bool quiet = mf->IsOn("PACKAGE_QUIET");
  498. if (!packageFound)
  499. {
  500. if (!quiet)
  501. {
  502. printf("%s not found.\n", packageName.c_str());
  503. }
  504. }
  505. else if (mode == "EXIST")
  506. {
  507. if (!quiet)
  508. {
  509. printf("%s found.\n", packageName.c_str());
  510. }
  511. }
  512. else if (mode == "COMPILE")
  513. {
  514. std::string includes = mf->GetSafeDefinition("PACKAGE_INCLUDE_DIRS");
  515. std::vector<std::string> includeDirs;
  516. cmSystemTools::ExpandListArgument(includes, includeDirs);
  517. std::string includeFlags = lg->GetIncludeFlags(includeDirs, 0,
  518. language, false);
  519. std::string definitions = mf->GetSafeDefinition("PACKAGE_DEFINITIONS");
  520. printf("%s %s\n", includeFlags.c_str(), definitions.c_str());
  521. }
  522. else if (mode == "LINK")
  523. {
  524. const char* targetName = "dummy";
  525. std::vector<std::string> srcs;
  526. cmTarget* tgt = mf->AddExecutable(targetName, srcs, true);
  527. tgt->SetProperty("LINKER_LANGUAGE", language.c_str());
  528. std::string libs = mf->GetSafeDefinition("PACKAGE_LIBRARIES");
  529. std::vector<std::string> libList;
  530. cmSystemTools::ExpandListArgument(libs, libList);
  531. for(std::vector<std::string>::const_iterator libIt=libList.begin();
  532. libIt != libList.end();
  533. ++libIt)
  534. {
  535. mf->AddLinkLibraryForTarget(targetName, *libIt,
  536. cmTarget::GENERAL);
  537. }
  538. std::string linkLibs;
  539. std::string frameworkPath;
  540. std::string linkPath;
  541. std::string flags;
  542. std::string linkFlags;
  543. gg->CreateGeneratorTargets(mf);
  544. cmGeneratorTarget *gtgt = gg->GetGeneratorTarget(tgt);
  545. lg->GetTargetFlags(linkLibs, frameworkPath, linkPath, flags, linkFlags,
  546. gtgt, false);
  547. linkLibs = frameworkPath + linkPath + linkLibs;
  548. printf("%s\n", linkLibs.c_str() );
  549. /* if ( use_win32 )
  550. {
  551. tgt->SetProperty("WIN32_EXECUTABLE", "ON");
  552. }
  553. if ( use_macbundle)
  554. {
  555. tgt->SetProperty("MACOSX_BUNDLE", "ON");
  556. }*/
  557. }
  558. // free generic one if generated
  559. // this->SetGlobalGenerator(0); // setting 0-pointer is not possible
  560. // delete gg; // this crashes inside the cmake instance
  561. return packageFound;
  562. }
  563. // Parse the args
  564. void cmake::SetArgs(const std::vector<std::string>& args,
  565. bool directoriesSetBefore)
  566. {
  567. bool directoriesSet = directoriesSetBefore;
  568. bool haveToolset = false;
  569. for(unsigned int i=1; i < args.size(); ++i)
  570. {
  571. std::string arg = args[i];
  572. if(arg.find("-H",0) == 0)
  573. {
  574. directoriesSet = true;
  575. std::string path = arg.substr(2);
  576. path = cmSystemTools::CollapseFullPath(path.c_str());
  577. cmSystemTools::ConvertToUnixSlashes(path);
  578. this->SetHomeDirectory(path);
  579. }
  580. else if(arg.find("-S",0) == 0)
  581. {
  582. // There is no local generate anymore. Ignore -S option.
  583. }
  584. else if(arg.find("-O",0) == 0)
  585. {
  586. // There is no local generate anymore. Ignore -O option.
  587. }
  588. else if(arg.find("-B",0) == 0)
  589. {
  590. directoriesSet = true;
  591. std::string path = arg.substr(2);
  592. path = cmSystemTools::CollapseFullPath(path.c_str());
  593. cmSystemTools::ConvertToUnixSlashes(path);
  594. this->SetHomeOutputDirectory(path);
  595. }
  596. else if((i < args.size()-2) && (arg.find("--check-build-system",0) == 0))
  597. {
  598. this->CheckBuildSystemArgument = args[++i];
  599. this->ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
  600. }
  601. else if((i < args.size()-1) && (arg.find("--check-stamp-file",0) == 0))
  602. {
  603. this->CheckStampFile = args[++i];
  604. }
  605. else if((i < args.size()-1) && (arg.find("--check-stamp-list",0) == 0))
  606. {
  607. this->CheckStampList = args[++i];
  608. }
  609. #if defined(CMAKE_HAVE_VS_GENERATORS)
  610. else if((i < args.size()-1) && (arg.find("--vs-solution-file",0) == 0))
  611. {
  612. this->VSSolutionFile = args[++i];
  613. }
  614. #endif
  615. else if(arg.find("-V",0) == 0)
  616. {
  617. this->Verbose = true;
  618. }
  619. else if(arg.find("-D",0) == 0)
  620. {
  621. // skip for now
  622. }
  623. else if(arg.find("-U",0) == 0)
  624. {
  625. // skip for now
  626. }
  627. else if(arg.find("-C",0) == 0)
  628. {
  629. // skip for now
  630. }
  631. else if(arg.find("-P",0) == 0)
  632. {
  633. // skip for now
  634. i++;
  635. }
  636. else if(arg.find("--find-package",0) == 0)
  637. {
  638. // skip for now
  639. i++;
  640. }
  641. else if(arg.find("-Wno-dev",0) == 0)
  642. {
  643. // skip for now
  644. }
  645. else if(arg.find("-Wdev",0) == 0)
  646. {
  647. // skip for now
  648. }
  649. else if(arg.find("--graphviz=",0) == 0)
  650. {
  651. std::string path = arg.substr(strlen("--graphviz="));
  652. path = cmSystemTools::CollapseFullPath(path.c_str());
  653. cmSystemTools::ConvertToUnixSlashes(path);
  654. this->GraphVizFile = path;
  655. if ( this->GraphVizFile.empty() )
  656. {
  657. cmSystemTools::Error("No file specified for --graphviz");
  658. }
  659. }
  660. else if(arg.find("--debug-trycompile",0) == 0)
  661. {
  662. std::cout << "debug trycompile on\n";
  663. this->DebugTryCompileOn();
  664. }
  665. else if(arg.find("--debug-output",0) == 0)
  666. {
  667. std::cout << "Running with debug output on.\n";
  668. this->SetDebugOutputOn(true);
  669. }
  670. else if(arg.find("--trace",0) == 0)
  671. {
  672. std::cout << "Running with trace output on.\n";
  673. this->SetTrace(true);
  674. }
  675. else if(arg.find("--warn-uninitialized",0) == 0)
  676. {
  677. std::cout << "Warn about uninitialized values.\n";
  678. this->SetWarnUninitialized(true);
  679. }
  680. else if(arg.find("--warn-unused-vars",0) == 0)
  681. {
  682. std::cout << "Finding unused variables.\n";
  683. this->SetWarnUnused(true);
  684. }
  685. else if(arg.find("--no-warn-unused-cli",0) == 0)
  686. {
  687. std::cout << "Not searching for unused variables given on the " <<
  688. "command line.\n";
  689. this->SetWarnUnusedCli(false);
  690. }
  691. else if(arg.find("--check-system-vars",0) == 0)
  692. {
  693. std::cout << "Also check system files when warning about unused and " <<
  694. "uninitialized variables.\n";
  695. this->SetCheckSystemVars(true);
  696. }
  697. else if(arg.find("-T",0) == 0)
  698. {
  699. std::string value = arg.substr(2);
  700. if(value.size() == 0)
  701. {
  702. ++i;
  703. if(i >= args.size())
  704. {
  705. cmSystemTools::Error("No toolset specified for -T");
  706. return;
  707. }
  708. value = args[i];
  709. }
  710. if(haveToolset)
  711. {
  712. cmSystemTools::Error("Multiple -T options not allowed");
  713. return;
  714. }
  715. this->GeneratorToolset = value;
  716. haveToolset = true;
  717. }
  718. else if(arg.find("-G",0) == 0)
  719. {
  720. std::string value = arg.substr(2);
  721. if(value.size() == 0)
  722. {
  723. ++i;
  724. if(i >= args.size())
  725. {
  726. cmSystemTools::Error("No generator specified for -G");
  727. return;
  728. }
  729. value = args[i];
  730. }
  731. cmGlobalGenerator* gen =
  732. this->CreateGlobalGenerator(value);
  733. if(!gen)
  734. {
  735. cmSystemTools::Error("Could not create named generator ",
  736. value.c_str());
  737. }
  738. else
  739. {
  740. this->SetGlobalGenerator(gen);
  741. }
  742. }
  743. // no option assume it is the path to the source
  744. else
  745. {
  746. directoriesSet = true;
  747. this->SetDirectoriesFromFile(arg.c_str());
  748. }
  749. }
  750. if(!directoriesSet)
  751. {
  752. this->SetHomeOutputDirectory
  753. (cmSystemTools::GetCurrentWorkingDirectory());
  754. this->SetStartOutputDirectory
  755. (cmSystemTools::GetCurrentWorkingDirectory());
  756. this->SetHomeDirectory
  757. (cmSystemTools::GetCurrentWorkingDirectory());
  758. this->SetStartDirectory
  759. (cmSystemTools::GetCurrentWorkingDirectory());
  760. }
  761. this->SetStartDirectory(this->GetHomeDirectory());
  762. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  763. }
  764. //----------------------------------------------------------------------------
  765. void cmake::SetDirectoriesFromFile(const char* arg)
  766. {
  767. // Check if the argument refers to a CMakeCache.txt or
  768. // CMakeLists.txt file.
  769. std::string listPath;
  770. std::string cachePath;
  771. bool argIsFile = false;
  772. if(cmSystemTools::FileIsDirectory(arg))
  773. {
  774. std::string path = cmSystemTools::CollapseFullPath(arg);
  775. cmSystemTools::ConvertToUnixSlashes(path);
  776. std::string cacheFile = path;
  777. cacheFile += "/CMakeCache.txt";
  778. std::string listFile = path;
  779. listFile += "/CMakeLists.txt";
  780. if(cmSystemTools::FileExists(cacheFile.c_str()))
  781. {
  782. cachePath = path;
  783. }
  784. if(cmSystemTools::FileExists(listFile.c_str()))
  785. {
  786. listPath = path;
  787. }
  788. }
  789. else if(cmSystemTools::FileExists(arg))
  790. {
  791. argIsFile = true;
  792. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  793. std::string name = cmSystemTools::GetFilenameName(fullPath);
  794. name = cmSystemTools::LowerCase(name);
  795. if(name == "cmakecache.txt")
  796. {
  797. cachePath = cmSystemTools::GetFilenamePath(fullPath);
  798. }
  799. else if(name == "cmakelists.txt")
  800. {
  801. listPath = cmSystemTools::GetFilenamePath(fullPath);
  802. }
  803. }
  804. else
  805. {
  806. // Specified file or directory does not exist. Try to set things
  807. // up to produce a meaningful error message.
  808. std::string fullPath = cmSystemTools::CollapseFullPath(arg);
  809. std::string name = cmSystemTools::GetFilenameName(fullPath);
  810. name = cmSystemTools::LowerCase(name);
  811. if(name == "cmakecache.txt" || name == "cmakelists.txt")
  812. {
  813. argIsFile = true;
  814. listPath = cmSystemTools::GetFilenamePath(fullPath);
  815. }
  816. else
  817. {
  818. listPath = fullPath;
  819. }
  820. }
  821. // If there is a CMakeCache.txt file, use its settings.
  822. if(cachePath.length() > 0)
  823. {
  824. cmCacheManager* cachem = this->GetCacheManager();
  825. cmCacheManager::CacheIterator it = cachem->NewIterator();
  826. if(cachem->LoadCache(cachePath) &&
  827. it.Find("CMAKE_HOME_DIRECTORY"))
  828. {
  829. this->SetHomeOutputDirectory(cachePath);
  830. this->SetStartOutputDirectory(cachePath);
  831. this->SetHomeDirectory(it.GetValue());
  832. this->SetStartDirectory(it.GetValue());
  833. return;
  834. }
  835. }
  836. // If there is a CMakeLists.txt file, use it as the source tree.
  837. if(listPath.length() > 0)
  838. {
  839. this->SetHomeDirectory(listPath);
  840. this->SetStartDirectory(listPath);
  841. if(argIsFile)
  842. {
  843. // Source CMakeLists.txt file given. It was probably dropped
  844. // onto the executable in a GUI. Default to an in-source build.
  845. this->SetHomeOutputDirectory(listPath);
  846. this->SetStartOutputDirectory(listPath);
  847. }
  848. else
  849. {
  850. // Source directory given on command line. Use current working
  851. // directory as build tree.
  852. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  853. this->SetHomeOutputDirectory(cwd);
  854. this->SetStartOutputDirectory(cwd);
  855. }
  856. return;
  857. }
  858. // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  859. // argument. Assume it is the path to the source tree, and use the
  860. // current working directory as the build tree.
  861. std::string full = cmSystemTools::CollapseFullPath(arg);
  862. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  863. this->SetHomeDirectory(full);
  864. this->SetStartDirectory(full);
  865. this->SetHomeOutputDirectory(cwd);
  866. this->SetStartOutputDirectory(cwd);
  867. }
  868. // at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
  869. // cache
  870. int cmake::AddCMakePaths()
  871. {
  872. // Save the value in the cache
  873. this->CacheManager->AddCacheEntry
  874. ("CMAKE_COMMAND", cmSystemTools::GetCMakeCommand().c_str(),
  875. "Path to CMake executable.", cmCacheManager::INTERNAL);
  876. #ifdef CMAKE_BUILD_WITH_CMAKE
  877. this->CacheManager->AddCacheEntry
  878. ("CMAKE_CTEST_COMMAND", cmSystemTools::GetCTestCommand().c_str(),
  879. "Path to ctest program executable.", cmCacheManager::INTERNAL);
  880. this->CacheManager->AddCacheEntry
  881. ("CMAKE_CPACK_COMMAND", cmSystemTools::GetCPackCommand().c_str(),
  882. "Path to cpack program executable.", cmCacheManager::INTERNAL);
  883. #endif
  884. if(!cmSystemTools::FileExists(
  885. (cmSystemTools::GetCMakeRoot()+"/Modules/CMake.cmake").c_str()))
  886. {
  887. // couldn't find modules
  888. cmSystemTools::Error("Could not find CMAKE_ROOT !!!\n"
  889. "CMake has most likely not been installed correctly.\n"
  890. "Modules directory not found in\n",
  891. cmSystemTools::GetCMakeRoot().c_str());
  892. return 0;
  893. }
  894. this->CacheManager->AddCacheEntry
  895. ("CMAKE_ROOT", cmSystemTools::GetCMakeRoot().c_str(),
  896. "Path to CMake installation.", cmCacheManager::INTERNAL);
  897. return 1;
  898. }
  899. void cmake::AddExtraGenerator(const std::string& name,
  900. CreateExtraGeneratorFunctionType newFunction)
  901. {
  902. cmExternalMakefileProjectGenerator* extraGenerator = newFunction();
  903. const std::vector<std::string>& supportedGlobalGenerators =
  904. extraGenerator->GetSupportedGlobalGenerators();
  905. for(std::vector<std::string>::const_iterator
  906. it = supportedGlobalGenerators.begin();
  907. it != supportedGlobalGenerators.end();
  908. ++it )
  909. {
  910. std::string fullName = cmExternalMakefileProjectGenerator::
  911. CreateFullGeneratorName(*it, name);
  912. this->ExtraGenerators[fullName] = newFunction;
  913. }
  914. delete extraGenerator;
  915. }
  916. void cmake::AddDefaultExtraGenerators()
  917. {
  918. #if defined(CMAKE_BUILD_WITH_CMAKE)
  919. #if defined(_WIN32) && !defined(__CYGWIN__)
  920. // e.g. kdevelop4 ?
  921. #endif
  922. this->AddExtraGenerator(cmExtraCodeBlocksGenerator::GetActualName(),
  923. &cmExtraCodeBlocksGenerator::New);
  924. this->AddExtraGenerator(cmExtraCodeLiteGenerator::GetActualName(),
  925. &cmExtraCodeLiteGenerator::New);
  926. this->AddExtraGenerator(cmExtraSublimeTextGenerator::GetActualName(),
  927. &cmExtraSublimeTextGenerator::New);
  928. this->AddExtraGenerator(cmExtraKateGenerator::GetActualName(),
  929. &cmExtraKateGenerator::New);
  930. #ifdef CMAKE_USE_ECLIPSE
  931. this->AddExtraGenerator(cmExtraEclipseCDT4Generator::GetActualName(),
  932. &cmExtraEclipseCDT4Generator::New);
  933. #endif
  934. #ifdef CMAKE_USE_KDEVELOP
  935. this->AddExtraGenerator(cmGlobalKdevelopGenerator::GetActualName(),
  936. &cmGlobalKdevelopGenerator::New);
  937. // for kdevelop also add the generator with just the name of the
  938. // extra generator, since it was this way since cmake 2.2
  939. this->ExtraGenerators[cmGlobalKdevelopGenerator::GetActualName()]
  940. = &cmGlobalKdevelopGenerator::New;
  941. #endif
  942. #endif
  943. }
  944. //----------------------------------------------------------------------------
  945. void cmake::GetRegisteredGenerators(std::vector<std::string>& names)
  946. {
  947. for(RegisteredGeneratorsVector::const_iterator i = this->Generators.begin();
  948. i != this->Generators.end(); ++i)
  949. {
  950. (*i)->GetGenerators(names);
  951. }
  952. for(RegisteredExtraGeneratorsMap::const_iterator
  953. i = this->ExtraGenerators.begin();
  954. i != this->ExtraGenerators.end(); ++i)
  955. {
  956. names.push_back(i->first);
  957. }
  958. }
  959. cmGlobalGenerator* cmake::CreateGlobalGenerator(const std::string& gname)
  960. {
  961. cmExternalMakefileProjectGenerator* extraGenerator = 0;
  962. std::string name = gname;
  963. RegisteredExtraGeneratorsMap::const_iterator extraGenIt =
  964. this->ExtraGenerators.find(name);
  965. if (extraGenIt != this->ExtraGenerators.end())
  966. {
  967. extraGenerator = (extraGenIt->second)();
  968. name = extraGenerator->GetGlobalGeneratorName(name);
  969. }
  970. cmGlobalGenerator* generator = 0;
  971. for (RegisteredGeneratorsVector::const_iterator i =
  972. this->Generators.begin(); i != this->Generators.end(); ++i)
  973. {
  974. generator = (*i)->CreateGlobalGenerator(name);
  975. if (generator)
  976. {
  977. break;
  978. }
  979. }
  980. if (generator)
  981. {
  982. generator->SetCMakeInstance(this);
  983. generator->SetExternalMakefileProjectGenerator(extraGenerator);
  984. }
  985. else
  986. {
  987. delete extraGenerator;
  988. }
  989. return generator;
  990. }
  991. void cmake::SetHomeDirectory(const std::string& dir)
  992. {
  993. this->cmHomeDirectory = dir;
  994. cmSystemTools::ConvertToUnixSlashes(this->cmHomeDirectory);
  995. }
  996. void cmake::SetHomeOutputDirectory(const std::string& lib)
  997. {
  998. this->HomeOutputDirectory = lib;
  999. cmSystemTools::ConvertToUnixSlashes(this->HomeOutputDirectory);
  1000. }
  1001. void cmake::SetGlobalGenerator(cmGlobalGenerator *gg)
  1002. {
  1003. if(!gg)
  1004. {
  1005. cmSystemTools::Error("Error SetGlobalGenerator called with null");
  1006. return;
  1007. }
  1008. // delete the old generator
  1009. if (this->GlobalGenerator)
  1010. {
  1011. delete this->GlobalGenerator;
  1012. // restore the original environment variables CXX and CC
  1013. // Restore CC
  1014. std::string env = "CC=";
  1015. if(this->CCEnvironment.size())
  1016. {
  1017. env += this->CCEnvironment;
  1018. }
  1019. cmSystemTools::PutEnv(env.c_str());
  1020. env = "CXX=";
  1021. if(this->CXXEnvironment.size())
  1022. {
  1023. env += this->CXXEnvironment;
  1024. }
  1025. cmSystemTools::PutEnv(env.c_str());
  1026. }
  1027. // set the new
  1028. this->GlobalGenerator = gg;
  1029. // set the global flag for unix style paths on cmSystemTools as soon as
  1030. // the generator is set. This allows gmake to be used on windows.
  1031. cmSystemTools::SetForceUnixPaths
  1032. (this->GlobalGenerator->GetForceUnixPaths());
  1033. // Save the environment variables CXX and CC
  1034. const char* cxx = getenv("CXX");
  1035. const char* cc = getenv("CC");
  1036. if(cxx)
  1037. {
  1038. this->CXXEnvironment = cxx;
  1039. }
  1040. else
  1041. {
  1042. this->CXXEnvironment = "";
  1043. }
  1044. if(cc)
  1045. {
  1046. this->CCEnvironment = cc;
  1047. }
  1048. else
  1049. {
  1050. this->CCEnvironment = "";
  1051. }
  1052. // set the cmake instance just to be sure
  1053. gg->SetCMakeInstance(this);
  1054. }
  1055. int cmake::DoPreConfigureChecks()
  1056. {
  1057. // Make sure the Start directory contains a CMakeLists.txt file.
  1058. std::string srcList = this->GetHomeDirectory();
  1059. srcList += "/CMakeLists.txt";
  1060. if(!cmSystemTools::FileExists(srcList.c_str()))
  1061. {
  1062. cmOStringStream err;
  1063. if(cmSystemTools::FileIsDirectory(this->GetHomeDirectory()))
  1064. {
  1065. err << "The source directory \"" << this->GetHomeDirectory()
  1066. << "\" does not appear to contain CMakeLists.txt.\n";
  1067. }
  1068. else if(cmSystemTools::FileExists(this->GetHomeDirectory()))
  1069. {
  1070. err << "The source directory \"" << this->GetHomeDirectory()
  1071. << "\" is a file, not a directory.\n";
  1072. }
  1073. else
  1074. {
  1075. err << "The source directory \"" << this->GetHomeDirectory()
  1076. << "\" does not exist.\n";
  1077. }
  1078. err << "Specify --help for usage, or press the help button on the CMake "
  1079. "GUI.";
  1080. cmSystemTools::Error(err.str().c_str());
  1081. return -2;
  1082. }
  1083. // do a sanity check on some values
  1084. if(this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY"))
  1085. {
  1086. std::string cacheStart =
  1087. this->CacheManager->GetCacheValue("CMAKE_HOME_DIRECTORY");
  1088. cacheStart += "/CMakeLists.txt";
  1089. std::string currentStart = this->GetHomeDirectory();
  1090. currentStart += "/CMakeLists.txt";
  1091. if(!cmSystemTools::SameFile(cacheStart.c_str(), currentStart.c_str()))
  1092. {
  1093. std::string message = "The source \"";
  1094. message += currentStart;
  1095. message += "\" does not match the source \"";
  1096. message += cacheStart;
  1097. message += "\" used to generate cache. ";
  1098. message += "Re-run cmake with a different source directory.";
  1099. cmSystemTools::Error(message.c_str());
  1100. return -2;
  1101. }
  1102. }
  1103. else
  1104. {
  1105. return 0;
  1106. }
  1107. return 1;
  1108. }
  1109. struct SaveCacheEntry
  1110. {
  1111. std::string key;
  1112. std::string value;
  1113. std::string help;
  1114. cmCacheManager::CacheEntryType type;
  1115. };
  1116. int cmake::HandleDeleteCacheVariables(const std::string& var)
  1117. {
  1118. std::vector<std::string> argsSplit;
  1119. cmSystemTools::ExpandListArgument(std::string(var), argsSplit, true);
  1120. // erase the property to avoid infinite recursion
  1121. this->SetProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_", "");
  1122. if(this->GetIsInTryCompile())
  1123. {
  1124. return 0;
  1125. }
  1126. cmCacheManager::CacheIterator ci = this->CacheManager->NewIterator();
  1127. std::vector<SaveCacheEntry> saved;
  1128. cmOStringStream warning;
  1129. warning
  1130. << "You have changed variables that require your cache to be deleted.\n"
  1131. << "Configure will be re-run and you may have to reset some variables.\n"
  1132. << "The following variables have changed:\n";
  1133. for(std::vector<std::string>::iterator i = argsSplit.begin();
  1134. i != argsSplit.end(); ++i)
  1135. {
  1136. SaveCacheEntry save;
  1137. save.key = *i;
  1138. warning << *i << "= ";
  1139. i++;
  1140. save.value = *i;
  1141. warning << *i << "\n";
  1142. if(ci.Find(save.key))
  1143. {
  1144. save.type = ci.GetType();
  1145. save.help = ci.GetProperty("HELPSTRING");
  1146. }
  1147. saved.push_back(save);
  1148. }
  1149. // remove the cache
  1150. this->CacheManager->DeleteCache(this->GetStartOutputDirectory());
  1151. // load the empty cache
  1152. this->LoadCache();
  1153. // restore the changed compilers
  1154. for(std::vector<SaveCacheEntry>::iterator i = saved.begin();
  1155. i != saved.end(); ++i)
  1156. {
  1157. this->AddCacheEntry(i->key, i->value.c_str(),
  1158. i->help.c_str(), i->type);
  1159. }
  1160. cmSystemTools::Message(warning.str().c_str());
  1161. // avoid reconfigure if there were errors
  1162. if(!cmSystemTools::GetErrorOccuredFlag())
  1163. {
  1164. // re-run configure
  1165. return this->Configure();
  1166. }
  1167. return 0;
  1168. }
  1169. int cmake::Configure()
  1170. {
  1171. if(this->DoSuppressDevWarnings)
  1172. {
  1173. if(this->SuppressDevWarnings)
  1174. {
  1175. this->CacheManager->
  1176. AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "TRUE",
  1177. "Suppress Warnings that are meant for"
  1178. " the author of the CMakeLists.txt files.",
  1179. cmCacheManager::INTERNAL);
  1180. }
  1181. else
  1182. {
  1183. this->CacheManager->
  1184. AddCacheEntry("CMAKE_SUPPRESS_DEVELOPER_WARNINGS", "FALSE",
  1185. "Suppress Warnings that are meant for"
  1186. " the author of the CMakeLists.txt files.",
  1187. cmCacheManager::INTERNAL);
  1188. }
  1189. }
  1190. int ret = this->ActualConfigure();
  1191. const char* delCacheVars =
  1192. this->GetProperty("__CMAKE_DELETE_CACHE_CHANGE_VARS_");
  1193. if(delCacheVars && delCacheVars[0] != 0)
  1194. {
  1195. return this->HandleDeleteCacheVariables(delCacheVars);
  1196. }
  1197. return ret;
  1198. }
  1199. int cmake::ActualConfigure()
  1200. {
  1201. // Construct right now our path conversion table before it's too late:
  1202. this->UpdateConversionPathTable();
  1203. this->CleanupCommandsAndMacros();
  1204. int res = 0;
  1205. if ( this->GetWorkingMode() == NORMAL_MODE )
  1206. {
  1207. res = this->DoPreConfigureChecks();
  1208. }
  1209. if ( res < 0 )
  1210. {
  1211. return -2;
  1212. }
  1213. if ( !res )
  1214. {
  1215. this->CacheManager->AddCacheEntry
  1216. ("CMAKE_HOME_DIRECTORY",
  1217. this->GetHomeDirectory(),
  1218. "Start directory with the top level CMakeLists.txt file for this "
  1219. "project",
  1220. cmCacheManager::INTERNAL);
  1221. }
  1222. // no generator specified on the command line
  1223. if(!this->GlobalGenerator)
  1224. {
  1225. const char* genName =
  1226. this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1227. const char* extraGenName =
  1228. this->CacheManager->GetCacheValue("CMAKE_EXTRA_GENERATOR");
  1229. if(genName)
  1230. {
  1231. std::string fullName = cmExternalMakefileProjectGenerator::
  1232. CreateFullGeneratorName(genName,
  1233. extraGenName ? extraGenName : "");
  1234. this->GlobalGenerator = this->CreateGlobalGenerator(fullName);
  1235. }
  1236. if(this->GlobalGenerator)
  1237. {
  1238. // set the global flag for unix style paths on cmSystemTools as
  1239. // soon as the generator is set. This allows gmake to be used
  1240. // on windows.
  1241. cmSystemTools::SetForceUnixPaths
  1242. (this->GlobalGenerator->GetForceUnixPaths());
  1243. }
  1244. else
  1245. {
  1246. #if defined(__BORLANDC__) && defined(_WIN32)
  1247. this->SetGlobalGenerator(new cmGlobalBorlandMakefileGenerator);
  1248. #elif defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW)
  1249. std::string installedCompiler;
  1250. // Try to find the newest VS installed on the computer and
  1251. // use that as a default if -G is not specified
  1252. const std::string vsregBase =
  1253. "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\";
  1254. std::vector<std::string> vsVerions;
  1255. vsVerions.push_back("VisualStudio\\");
  1256. vsVerions.push_back("VCExpress\\");
  1257. vsVerions.push_back("WDExpress\\");
  1258. struct VSRegistryEntryName
  1259. {
  1260. const char* MSVersion;
  1261. const char* GeneratorName;
  1262. };
  1263. VSRegistryEntryName version[] = {
  1264. {"6.0", "Visual Studio 6"},
  1265. {"7.0", "Visual Studio 7"},
  1266. {"7.1", "Visual Studio 7 .NET 2003"},
  1267. {"8.0", "Visual Studio 8 2005"},
  1268. {"9.0", "Visual Studio 9 2008"},
  1269. {"10.0", "Visual Studio 10 2010"},
  1270. {"11.0", "Visual Studio 11 2012"},
  1271. {"12.0", "Visual Studio 12 2013"},
  1272. {0, 0}};
  1273. for(int i=0; version[i].MSVersion != 0; i++)
  1274. {
  1275. for(size_t b=0; b < vsVerions.size(); b++)
  1276. {
  1277. std::string reg = vsregBase + vsVerions[b] + version[i].MSVersion;
  1278. reg += ";InstallDir]";
  1279. cmSystemTools::ExpandRegistryValues(reg,
  1280. cmSystemTools::KeyWOW64_32);
  1281. if (!(reg == "/registry"))
  1282. {
  1283. installedCompiler = version[i].GeneratorName;
  1284. break;
  1285. }
  1286. }
  1287. }
  1288. cmGlobalGenerator* gen
  1289. = this->CreateGlobalGenerator(installedCompiler.c_str());
  1290. if(!gen)
  1291. {
  1292. gen = new cmGlobalNMakeMakefileGenerator;
  1293. }
  1294. this->SetGlobalGenerator(gen);
  1295. std::cout << "-- Building for: " << gen->GetName() << "\n";
  1296. #else
  1297. this->SetGlobalGenerator(new cmGlobalUnixMakefileGenerator3);
  1298. #endif
  1299. }
  1300. if(!this->GlobalGenerator)
  1301. {
  1302. cmSystemTools::Error("Could not create generator");
  1303. return -1;
  1304. }
  1305. }
  1306. const char* genName = this->CacheManager->GetCacheValue("CMAKE_GENERATOR");
  1307. if(genName)
  1308. {
  1309. if(!this->GlobalGenerator->MatchesGeneratorName(genName))
  1310. {
  1311. std::string message = "Error: generator : ";
  1312. message += this->GlobalGenerator->GetName();
  1313. message += "\nDoes not match the generator used previously: ";
  1314. message += genName;
  1315. message +=
  1316. "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1317. "directory or choose a different binary directory.";
  1318. cmSystemTools::Error(message.c_str());
  1319. return -2;
  1320. }
  1321. }
  1322. if(!this->CacheManager->GetCacheValue("CMAKE_GENERATOR"))
  1323. {
  1324. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR",
  1325. this->GlobalGenerator->GetName().c_str(),
  1326. "Name of generator.",
  1327. cmCacheManager::INTERNAL);
  1328. this->CacheManager->AddCacheEntry("CMAKE_EXTRA_GENERATOR",
  1329. this->GlobalGenerator->GetExtraGeneratorName().c_str(),
  1330. "Name of external makefile project generator.",
  1331. cmCacheManager::INTERNAL);
  1332. }
  1333. if(const char* tsName =
  1334. this->CacheManager->GetCacheValue("CMAKE_GENERATOR_TOOLSET"))
  1335. {
  1336. if(this->GeneratorToolset.empty())
  1337. {
  1338. this->GeneratorToolset = tsName;
  1339. }
  1340. else if(this->GeneratorToolset != tsName)
  1341. {
  1342. std::string message = "Error: generator toolset: ";
  1343. message += this->GeneratorToolset;
  1344. message += "\nDoes not match the toolset used previously: ";
  1345. message += tsName;
  1346. message +=
  1347. "\nEither remove the CMakeCache.txt file and CMakeFiles "
  1348. "directory or choose a different binary directory.";
  1349. cmSystemTools::Error(message.c_str());
  1350. return -2;
  1351. }
  1352. }
  1353. else
  1354. {
  1355. this->CacheManager->AddCacheEntry("CMAKE_GENERATOR_TOOLSET",
  1356. this->GeneratorToolset.c_str(),
  1357. "Name of generator toolset.",
  1358. cmCacheManager::INTERNAL);
  1359. }
  1360. if(!this->GeneratorToolset.empty() &&
  1361. !this->GlobalGenerator->SetGeneratorToolset(this->GeneratorToolset))
  1362. {
  1363. return -2;
  1364. }
  1365. // reset any system configuration information, except for when we are
  1366. // InTryCompile. With TryCompile the system info is taken from the parent's
  1367. // info to save time
  1368. if (!this->InTryCompile)
  1369. {
  1370. this->GlobalGenerator->ClearEnabledLanguages();
  1371. }
  1372. // Truncate log files
  1373. if (!this->InTryCompile)
  1374. {
  1375. this->TruncateOutputLog("CMakeOutput.log");
  1376. this->TruncateOutputLog("CMakeError.log");
  1377. }
  1378. // actually do the configure
  1379. this->GlobalGenerator->Configure();
  1380. // Before saving the cache
  1381. // if the project did not define one of the entries below, add them now
  1382. // so users can edit the values in the cache:
  1383. // We used to always present LIBRARY_OUTPUT_PATH and
  1384. // EXECUTABLE_OUTPUT_PATH. They are now documented as old-style and
  1385. // should no longer be used. Therefore we present them only if the
  1386. // project requires compatibility with CMake 2.4. We detect this
  1387. // here by looking for the old CMAKE_BACKWARDS_COMPATIBILITY
  1388. // variable created when CMP0001 is not set to NEW.
  1389. if(this->GetCacheManager()->GetCacheValue("CMAKE_BACKWARDS_COMPATIBILITY"))
  1390. {
  1391. if(!this->CacheManager->GetCacheValue("LIBRARY_OUTPUT_PATH"))
  1392. {
  1393. this->CacheManager->AddCacheEntry
  1394. ("LIBRARY_OUTPUT_PATH", "",
  1395. "Single output directory for building all libraries.",
  1396. cmCacheManager::PATH);
  1397. }
  1398. if(!this->CacheManager->GetCacheValue("EXECUTABLE_OUTPUT_PATH"))
  1399. {
  1400. this->CacheManager->AddCacheEntry
  1401. ("EXECUTABLE_OUTPUT_PATH", "",
  1402. "Single output directory for building all executables.",
  1403. cmCacheManager::PATH);
  1404. }
  1405. }
  1406. if(!this->CacheManager->GetCacheValue("CMAKE_USE_RELATIVE_PATHS"))
  1407. {
  1408. this->CacheManager->AddCacheEntry
  1409. ("CMAKE_USE_RELATIVE_PATHS", "OFF",
  1410. "If true, cmake will use relative paths in makefiles and projects.",
  1411. cmCacheManager::BOOL);
  1412. cmCacheManager::CacheIterator it =
  1413. this->CacheManager->GetCacheIterator("CMAKE_USE_RELATIVE_PATHS");
  1414. if ( !it.PropertyExists("ADVANCED") )
  1415. {
  1416. it.SetProperty("ADVANCED", "1");
  1417. }
  1418. }
  1419. if(cmSystemTools::GetFatalErrorOccured() &&
  1420. (!this->CacheManager->GetCacheValue("CMAKE_MAKE_PROGRAM") ||
  1421. cmSystemTools::IsOff(this->CacheManager->
  1422. GetCacheValue("CMAKE_MAKE_PROGRAM"))))
  1423. {
  1424. // We must have a bad generator selection. Wipe the cache entry so the
  1425. // user can select another.
  1426. this->CacheManager->RemoveCacheEntry("CMAKE_GENERATOR");
  1427. this->CacheManager->RemoveCacheEntry("CMAKE_EXTRA_GENERATOR");
  1428. }
  1429. cmMakefile* mf=this->GlobalGenerator->GetLocalGenerators()[0]->GetMakefile();
  1430. if (mf->IsOn("CTEST_USE_LAUNCHERS")
  1431. && !this->GetProperty("RULE_LAUNCH_COMPILE", cmProperty::GLOBAL))
  1432. {
  1433. cmSystemTools::Error("CTEST_USE_LAUNCHERS is enabled, but the "
  1434. "RULE_LAUNCH_COMPILE global property is not defined.\n"
  1435. "Did you forget to include(CTest) in the toplevel "
  1436. "CMakeLists.txt ?");
  1437. }
  1438. // only save the cache if there were no fatal errors
  1439. if ( this->GetWorkingMode() == NORMAL_MODE )
  1440. {
  1441. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1442. }
  1443. if(cmSystemTools::GetErrorOccuredFlag())
  1444. {
  1445. return -1;
  1446. }
  1447. return 0;
  1448. }
  1449. void cmake::PreLoadCMakeFiles()
  1450. {
  1451. std::vector<std::string> args;
  1452. std::string pre_load = this->GetHomeDirectory();
  1453. if ( pre_load.size() > 0 )
  1454. {
  1455. pre_load += "/PreLoad.cmake";
  1456. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1457. {
  1458. this->ReadListFile(args, pre_load.c_str());
  1459. }
  1460. }
  1461. pre_load = this->GetHomeOutputDirectory();
  1462. if ( pre_load.size() > 0 )
  1463. {
  1464. pre_load += "/PreLoad.cmake";
  1465. if ( cmSystemTools::FileExists(pre_load.c_str()) )
  1466. {
  1467. this->ReadListFile(args, pre_load.c_str());
  1468. }
  1469. }
  1470. }
  1471. // handle a command line invocation
  1472. int cmake::Run(const std::vector<std::string>& args, bool noconfigure)
  1473. {
  1474. // Process the arguments
  1475. this->SetArgs(args);
  1476. if(cmSystemTools::GetErrorOccuredFlag())
  1477. {
  1478. return -1;
  1479. }
  1480. // If we are given a stamp list file check if it is really out of date.
  1481. if(!this->CheckStampList.empty() &&
  1482. cmakeCheckStampList(this->CheckStampList.c_str()))
  1483. {
  1484. return 0;
  1485. }
  1486. // If we are given a stamp file check if it is really out of date.
  1487. if(!this->CheckStampFile.empty() &&
  1488. cmakeCheckStampFile(this->CheckStampFile.c_str()))
  1489. {
  1490. return 0;
  1491. }
  1492. if ( this->GetWorkingMode() == NORMAL_MODE )
  1493. {
  1494. // load the cache
  1495. if(this->LoadCache() < 0)
  1496. {
  1497. cmSystemTools::Error("Error executing cmake::LoadCache(). Aborting.\n");
  1498. return -1;
  1499. }
  1500. }
  1501. else
  1502. {
  1503. this->AddCMakePaths();
  1504. }
  1505. // Add any cache args
  1506. if ( !this->SetCacheArgs(args) )
  1507. {
  1508. cmSystemTools::Error("Problem processing arguments. Aborting.\n");
  1509. return -1;
  1510. }
  1511. // In script mode we terminate after running the script.
  1512. if(this->GetWorkingMode() != NORMAL_MODE)
  1513. {
  1514. if(cmSystemTools::GetErrorOccuredFlag())
  1515. {
  1516. return -1;
  1517. }
  1518. else
  1519. {
  1520. return 0;
  1521. }
  1522. }
  1523. // If MAKEFLAGS are given in the environment, remove the environment
  1524. // variable. This will prevent try-compile from succeeding when it
  1525. // should fail (if "-i" is an option). We cannot simply test
  1526. // whether "-i" is given and remove it because some make programs
  1527. // encode the MAKEFLAGS variable in a strange way.
  1528. if(getenv("MAKEFLAGS"))
  1529. {
  1530. cmSystemTools::PutEnv("MAKEFLAGS=");
  1531. }
  1532. this->PreLoadCMakeFiles();
  1533. if ( noconfigure )
  1534. {
  1535. return 0;
  1536. }
  1537. // now run the global generate
  1538. // Check the state of the build system to see if we need to regenerate.
  1539. if(!this->CheckBuildSystem())
  1540. {
  1541. return 0;
  1542. }
  1543. // If we are doing global generate, we better set start and start
  1544. // output directory to the root of the project.
  1545. std::string oldstartdir = this->GetStartDirectory();
  1546. std::string oldstartoutputdir = this->GetStartOutputDirectory();
  1547. this->SetStartDirectory(this->GetHomeDirectory());
  1548. this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
  1549. int ret = this->Configure();
  1550. if (ret || this->GetWorkingMode() != NORMAL_MODE)
  1551. {
  1552. #if defined(CMAKE_HAVE_VS_GENERATORS)
  1553. if(!this->VSSolutionFile.empty() && this->GlobalGenerator)
  1554. {
  1555. // CMake is running to regenerate a Visual Studio build tree
  1556. // during a build from the VS IDE. The build files cannot be
  1557. // regenerated, so we should stop the build.
  1558. cmSystemTools::Message(
  1559. "CMake Configure step failed. "
  1560. "Build files cannot be regenerated correctly. "
  1561. "Attempting to stop IDE build.");
  1562. cmGlobalVisualStudioGenerator* gg =
  1563. static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator);
  1564. gg->CallVisualStudioMacro(cmGlobalVisualStudioGenerator::MacroStop,
  1565. this->VSSolutionFile.c_str());
  1566. }
  1567. #endif
  1568. return ret;
  1569. }
  1570. ret = this->Generate();
  1571. std::string message = "Build files have been written to: ";
  1572. message += this->GetHomeOutputDirectory();
  1573. this->UpdateProgress(message.c_str(), -1);
  1574. if(ret)
  1575. {
  1576. return ret;
  1577. }
  1578. this->SetStartDirectory(oldstartdir);
  1579. this->SetStartOutputDirectory(oldstartoutputdir);
  1580. return ret;
  1581. }
  1582. int cmake::Generate()
  1583. {
  1584. if(!this->GlobalGenerator)
  1585. {
  1586. return -1;
  1587. }
  1588. this->GlobalGenerator->Generate();
  1589. if ( !this->GraphVizFile.empty() )
  1590. {
  1591. std::cout << "Generate graphviz: " << this->GraphVizFile << std::endl;
  1592. this->GenerateGraphViz(this->GraphVizFile.c_str());
  1593. }
  1594. if(this->WarnUnusedCli)
  1595. {
  1596. this->RunCheckForUnusedVariables();
  1597. }
  1598. if(cmSystemTools::GetErrorOccuredFlag())
  1599. {
  1600. return -1;
  1601. }
  1602. // Save the cache again after a successful Generate so that any internal
  1603. // variables created during Generate are saved. (Specifically target GUIDs
  1604. // for the Visual Studio and Xcode generators.)
  1605. if ( this->GetWorkingMode() == NORMAL_MODE )
  1606. {
  1607. this->CacheManager->SaveCache(this->GetHomeOutputDirectory());
  1608. }
  1609. return 0;
  1610. }
  1611. void cmake::AddCacheEntry(const std::string& key, const char* value,
  1612. const char* helpString,
  1613. int type)
  1614. {
  1615. this->CacheManager->AddCacheEntry(key, value,
  1616. helpString,
  1617. cmCacheManager::CacheEntryType(type));
  1618. }
  1619. const char* cmake::GetCacheDefinition(const std::string& name) const
  1620. {
  1621. return this->CacheManager->GetCacheValue(name);
  1622. }
  1623. void cmake::AddDefaultCommands()
  1624. {
  1625. std::list<cmCommand*> commands;
  1626. GetBootstrapCommands1(commands);
  1627. GetBootstrapCommands2(commands);
  1628. GetPredefinedCommands(commands);
  1629. for(std::list<cmCommand*>::iterator i = commands.begin();
  1630. i != commands.end(); ++i)
  1631. {
  1632. this->AddCommand(*i);
  1633. }
  1634. }
  1635. void cmake::AddDefaultGenerators()
  1636. {
  1637. #if defined(_WIN32) && !defined(__CYGWIN__)
  1638. # if !defined(CMAKE_BOOT_MINGW)
  1639. this->Generators.push_back(
  1640. cmGlobalVisualStudio6Generator::NewFactory());
  1641. this->Generators.push_back(
  1642. cmGlobalVisualStudio7Generator::NewFactory());
  1643. this->Generators.push_back(
  1644. cmGlobalVisualStudio10Generator::NewFactory());
  1645. this->Generators.push_back(
  1646. cmGlobalVisualStudio11Generator::NewFactory());
  1647. this->Generators.push_back(
  1648. cmGlobalVisualStudio12Generator::NewFactory());
  1649. this->Generators.push_back(
  1650. cmGlobalVisualStudio71Generator::NewFactory());
  1651. this->Generators.push_back(
  1652. cmGlobalVisualStudio8Generator::NewFactory());
  1653. this->Generators.push_back(
  1654. cmGlobalVisualStudio9Generator::NewFactory());
  1655. this->Generators.push_back(
  1656. cmGlobalBorlandMakefileGenerator::NewFactory());
  1657. this->Generators.push_back(
  1658. cmGlobalNMakeMakefileGenerator::NewFactory());
  1659. this->Generators.push_back(
  1660. cmGlobalJOMMakefileGenerator::NewFactory());
  1661. # endif
  1662. this->Generators.push_back(
  1663. cmGlobalMSYSMakefileGenerator::NewFactory());
  1664. this->Generators.push_back(
  1665. cmGlobalMinGWMakefileGenerator::NewFactory());
  1666. #endif
  1667. this->Generators.push_back(
  1668. cmGlobalUnixMakefileGenerator3::NewFactory());
  1669. this->Generators.push_back(
  1670. cmGlobalNinjaGenerator::NewFactory());
  1671. #if defined(CMAKE_USE_WMAKE)
  1672. this->Generators.push_back(
  1673. cmGlobalWatcomWMakeGenerator::NewFactory());
  1674. #endif
  1675. #ifdef CMAKE_USE_XCODE
  1676. this->Generators.push_back(
  1677. cmGlobalXCodeGenerator::NewFactory());
  1678. #endif
  1679. }
  1680. int cmake::LoadCache()
  1681. {
  1682. // could we not read the cache
  1683. if (!this->CacheManager->LoadCache(this->GetHomeOutputDirectory()))
  1684. {
  1685. // if it does exist, but isn't readable then warn the user
  1686. std::string cacheFile = this->GetHomeOutputDirectory();
  1687. cacheFile += "/CMakeCache.txt";
  1688. if(cmSystemTools::FileExists(cacheFile.c_str()))
  1689. {
  1690. cmSystemTools::Error(
  1691. "There is a CMakeCache.txt file for the current binary tree but "
  1692. "cmake does not have permission to read it. Please check the "
  1693. "permissions of the directory you are trying to run CMake on.");
  1694. return -1;
  1695. }
  1696. }
  1697. // setup CMAKE_ROOT and CMAKE_COMMAND
  1698. if(!this->AddCMakePaths())
  1699. {
  1700. return -3;
  1701. }
  1702. return 0;
  1703. }
  1704. void cmake::SetProgressCallback(ProgressCallbackType f, void *cd)
  1705. {
  1706. this->ProgressCallback = f;
  1707. this->ProgressCallbackClientData = cd;
  1708. }
  1709. void cmake::UpdateProgress(const char *msg, float prog)
  1710. {
  1711. if(this->ProgressCallback && !this->InTryCompile)
  1712. {
  1713. (*this->ProgressCallback)(msg, prog, this->ProgressCallbackClientData);
  1714. return;
  1715. }
  1716. }
  1717. void cmake::GetGeneratorDocumentation(std::vector<cmDocumentationEntry>& v)
  1718. {
  1719. for(RegisteredGeneratorsVector::const_iterator i =
  1720. this->Generators.begin(); i != this->Generators.end(); ++i)
  1721. {
  1722. cmDocumentationEntry e;
  1723. (*i)->GetDocumentation(e);
  1724. v.push_back(e);
  1725. }
  1726. for(RegisteredExtraGeneratorsMap::const_iterator i =
  1727. this->ExtraGenerators.begin(); i != this->ExtraGenerators.end(); ++i)
  1728. {
  1729. cmDocumentationEntry e;
  1730. cmExternalMakefileProjectGenerator* generator = (i->second)();
  1731. generator->GetDocumentation(e, i->first);
  1732. e.Name = i->first;
  1733. delete generator;
  1734. v.push_back(e);
  1735. }
  1736. }
  1737. void cmake::UpdateConversionPathTable()
  1738. {
  1739. // Update the path conversion table with any specified file:
  1740. const char* tablepath =
  1741. this->CacheManager->GetCacheValue("CMAKE_PATH_TRANSLATION_FILE");
  1742. if(tablepath)
  1743. {
  1744. cmsys::ifstream table( tablepath );
  1745. if(!table)
  1746. {
  1747. cmSystemTools::Error("CMAKE_PATH_TRANSLATION_FILE set to ", tablepath,
  1748. ". CMake can not open file.");
  1749. cmSystemTools::ReportLastSystemError("CMake can not open file.");
  1750. }
  1751. else
  1752. {
  1753. std::string a, b;
  1754. while(!table.eof())
  1755. {
  1756. // two entries per line
  1757. table >> a; table >> b;
  1758. cmSystemTools::AddTranslationPath( a.c_str(), b.c_str());
  1759. }
  1760. }
  1761. }
  1762. }
  1763. //----------------------------------------------------------------------------
  1764. int cmake::CheckBuildSystem()
  1765. {
  1766. // We do not need to rerun CMake. Check dependency integrity. Use
  1767. // the make system's VERBOSE environment variable to enable verbose
  1768. // output. This can be skipped by setting CMAKE_NO_VERBOSE (which is set
  1769. // by the Eclipse and KDevelop generators).
  1770. bool verbose = ((cmSystemTools::GetEnv("VERBOSE") != 0)
  1771. && (cmSystemTools::GetEnv("CMAKE_NO_VERBOSE") == 0));
  1772. // This method will check the integrity of the build system if the
  1773. // option was given on the command line. It reads the given file to
  1774. // determine whether CMake should rerun.
  1775. // If no file is provided for the check, we have to rerun.
  1776. if(this->CheckBuildSystemArgument.size() == 0)
  1777. {
  1778. if(verbose)
  1779. {
  1780. cmOStringStream msg;
  1781. msg << "Re-run cmake no build system arguments\n";
  1782. cmSystemTools::Stdout(msg.str().c_str());
  1783. }
  1784. return 1;
  1785. }
  1786. // If the file provided does not exist, we have to rerun.
  1787. if(!cmSystemTools::FileExists(this->CheckBuildSystemArgument.c_str()))
  1788. {
  1789. if(verbose)
  1790. {
  1791. cmOStringStream msg;
  1792. msg << "Re-run cmake missing file: "
  1793. << this->CheckBuildSystemArgument << "\n";
  1794. cmSystemTools::Stdout(msg.str().c_str());
  1795. }
  1796. return 1;
  1797. }
  1798. // Read the rerun check file and use it to decide whether to do the
  1799. // global generate.
  1800. cmake cm;
  1801. cmGlobalGenerator gg;
  1802. gg.SetCMakeInstance(&cm);
  1803. cmsys::auto_ptr<cmLocalGenerator> lg(gg.CreateLocalGenerator());
  1804. cmMakefile* mf = lg->GetMakefile();
  1805. if(!mf->ReadListFile(0, this->CheckBuildSystemArgument.c_str()) ||
  1806. cmSystemTools::GetErrorOccuredFlag())
  1807. {
  1808. if(verbose)
  1809. {
  1810. cmOStringStream msg;
  1811. msg << "Re-run cmake error reading : "
  1812. << this->CheckBuildSystemArgument << "\n";
  1813. cmSystemTools::Stdout(msg.str().c_str());
  1814. }
  1815. // There was an error reading the file. Just rerun.
  1816. return 1;
  1817. }
  1818. if(this->ClearBuildSystem)
  1819. {
  1820. // Get the generator used for this build system.
  1821. const char* genName = mf->GetDefinition("CMAKE_DEPENDS_GENERATOR");
  1822. if(!genName || genName[0] == '\0')
  1823. {
  1824. genName = "Unix Makefiles";
  1825. }
  1826. // Create the generator and use it to clear the dependencies.
  1827. cmsys::auto_ptr<cmGlobalGenerator>
  1828. ggd(this->CreateGlobalGenerator(genName));
  1829. if(ggd.get())
  1830. {
  1831. cmsys::auto_ptr<cmLocalGenerator> lgd(ggd->CreateLocalGenerator());
  1832. lgd->ClearDependencies(mf, verbose);
  1833. }
  1834. }
  1835. // If any byproduct of makefile generation is missing we must re-run.
  1836. std::vector<std::string> products;
  1837. if(const char* productStr = mf->GetDefinition("CMAKE_MAKEFILE_PRODUCTS"))
  1838. {
  1839. cmSystemTools::ExpandListArgument(productStr, products);
  1840. }
  1841. for(std::vector<std::string>::const_iterator pi = products.begin();
  1842. pi != products.end(); ++pi)
  1843. {
  1844. if(!(cmSystemTools::FileExists(pi->c_str()) ||
  1845. cmSystemTools::FileIsSymlink(pi->c_str())))
  1846. {
  1847. if(verbose)
  1848. {
  1849. cmOStringStream msg;
  1850. msg << "Re-run cmake, missing byproduct: " << *pi << "\n";
  1851. cmSystemTools::Stdout(msg.str().c_str());
  1852. }
  1853. return 1;
  1854. }
  1855. }
  1856. // Get the set of dependencies and outputs.
  1857. std::vector<std::string> depends;
  1858. std::vector<std::string> outputs;
  1859. const char* dependsStr = mf->GetDefinition("CMAKE_MAKEFILE_DEPENDS");
  1860. const char* outputsStr = mf->GetDefinition("CMAKE_MAKEFILE_OUTPUTS");
  1861. if(dependsStr && outputsStr)
  1862. {
  1863. cmSystemTools::ExpandListArgument(dependsStr, depends);
  1864. cmSystemTools::ExpandListArgument(outputsStr, outputs);
  1865. }
  1866. if(depends.empty() || outputs.empty())
  1867. {
  1868. // Not enough information was provided to do the test. Just rerun.
  1869. if(verbose)
  1870. {
  1871. cmOStringStream msg;
  1872. msg << "Re-run cmake no CMAKE_MAKEFILE_DEPENDS "
  1873. "or CMAKE_MAKEFILE_OUTPUTS :\n";
  1874. cmSystemTools::Stdout(msg.str().c_str());
  1875. }
  1876. return 1;
  1877. }
  1878. // Find the newest dependency.
  1879. std::vector<std::string>::iterator dep = depends.begin();
  1880. std::string dep_newest = *dep++;
  1881. for(;dep != depends.end(); ++dep)
  1882. {
  1883. int result = 0;
  1884. if(this->FileComparison->FileTimeCompare(dep_newest.c_str(),
  1885. dep->c_str(), &result))
  1886. {
  1887. if(result < 0)
  1888. {
  1889. dep_newest = *dep;
  1890. }
  1891. }
  1892. else
  1893. {
  1894. if(verbose)
  1895. {
  1896. cmOStringStream msg;
  1897. msg << "Re-run cmake: build system dependency is missing\n";
  1898. cmSystemTools::Stdout(msg.str().c_str());
  1899. }
  1900. return 1;
  1901. }
  1902. }
  1903. // Find the oldest output.
  1904. std::vector<std::string>::iterator out = outputs.begin();
  1905. std::string out_oldest = *out++;
  1906. for(;out != outputs.end(); ++out)
  1907. {
  1908. int result = 0;
  1909. if(this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  1910. out->c_str(), &result))
  1911. {
  1912. if(result > 0)
  1913. {
  1914. out_oldest = *out;
  1915. }
  1916. }
  1917. else
  1918. {
  1919. if(verbose)
  1920. {
  1921. cmOStringStream msg;
  1922. msg << "Re-run cmake: build system output is missing\n";
  1923. cmSystemTools::Stdout(msg.str().c_str());
  1924. }
  1925. return 1;
  1926. }
  1927. }
  1928. // If any output is older than any dependency then rerun.
  1929. {
  1930. int result = 0;
  1931. if(!this->FileComparison->FileTimeCompare(out_oldest.c_str(),
  1932. dep_newest.c_str(),
  1933. &result) ||
  1934. result < 0)
  1935. {
  1936. if(verbose)
  1937. {
  1938. cmOStringStream msg;
  1939. msg << "Re-run cmake file: " << out_oldest
  1940. << " older than: " << dep_newest << "\n";
  1941. cmSystemTools::Stdout(msg.str().c_str());
  1942. }
  1943. return 1;
  1944. }
  1945. }
  1946. // No need to rerun.
  1947. return 0;
  1948. }
  1949. //----------------------------------------------------------------------------
  1950. void cmake::TruncateOutputLog(const char* fname)
  1951. {
  1952. std::string fullPath = this->GetHomeOutputDirectory();
  1953. fullPath += "/";
  1954. fullPath += fname;
  1955. struct stat st;
  1956. if ( ::stat(fullPath.c_str(), &st) )
  1957. {
  1958. return;
  1959. }
  1960. if ( !this->CacheManager->GetCacheValue("CMAKE_CACHEFILE_DIR") )
  1961. {
  1962. cmSystemTools::RemoveFile(fullPath.c_str());
  1963. return;
  1964. }
  1965. off_t fsize = st.st_size;
  1966. const off_t maxFileSize = 50 * 1024;
  1967. if ( fsize < maxFileSize )
  1968. {
  1969. //TODO: truncate file
  1970. return;
  1971. }
  1972. }
  1973. inline std::string removeQuotes(const std::string& s)
  1974. {
  1975. if(s[0] == '\"' && s[s.size()-1] == '\"')
  1976. {
  1977. return s.substr(1, s.size()-2);
  1978. }
  1979. return s;
  1980. }
  1981. void cmake::MarkCliAsUsed(const std::string& variable)
  1982. {
  1983. this->UsedCliVariables[variable] = true;
  1984. }
  1985. void cmake::GenerateGraphViz(const char* fileName) const
  1986. {
  1987. #ifdef CMAKE_BUILD_WITH_CMAKE
  1988. cmsys::auto_ptr<cmGraphVizWriter> gvWriter(
  1989. new cmGraphVizWriter(this->GetGlobalGenerator()->GetLocalGenerators()));
  1990. std::string settingsFile = this->GetHomeOutputDirectory();
  1991. settingsFile += "/CMakeGraphVizOptions.cmake";
  1992. std::string fallbackSettingsFile = this->GetHomeDirectory();
  1993. fallbackSettingsFile += "/CMakeGraphVizOptions.cmake";
  1994. gvWriter->ReadSettings(settingsFile.c_str(), fallbackSettingsFile.c_str());
  1995. gvWriter->WritePerTargetFiles(fileName);
  1996. gvWriter->WriteTargetDependersFiles(fileName);
  1997. gvWriter->WriteGlobalFile(fileName);
  1998. #endif
  1999. }
  2000. void cmake::DefineProperty(const std::string& name,
  2001. cmProperty::ScopeType scope,
  2002. const char *ShortDescription,
  2003. const char *FullDescription,
  2004. bool chained)
  2005. {
  2006. this->PropertyDefinitions[scope].DefineProperty(name,scope,ShortDescription,
  2007. FullDescription,
  2008. chained);
  2009. }
  2010. cmPropertyDefinition *cmake
  2011. ::GetPropertyDefinition(const std::string& name,
  2012. cmProperty::ScopeType scope)
  2013. {
  2014. if (this->IsPropertyDefined(name,scope))
  2015. {
  2016. return &(this->PropertyDefinitions[scope][name]);
  2017. }
  2018. return 0;
  2019. }
  2020. bool cmake::IsPropertyDefined(const std::string& name,
  2021. cmProperty::ScopeType scope)
  2022. {
  2023. return this->PropertyDefinitions[scope].IsPropertyDefined(name);
  2024. }
  2025. bool cmake::IsPropertyChained(const std::string& name,
  2026. cmProperty::ScopeType scope)
  2027. {
  2028. return this->PropertyDefinitions[scope].IsPropertyChained(name);
  2029. }
  2030. void cmake::SetProperty(const std::string& prop, const char* value)
  2031. {
  2032. // Special hook to invalidate cached value.
  2033. if(prop == "DEBUG_CONFIGURATIONS")
  2034. {
  2035. this->DebugConfigs.clear();
  2036. }
  2037. this->Properties.SetProperty(prop, value, cmProperty::GLOBAL);
  2038. }
  2039. void cmake::AppendProperty(const std::string& prop,
  2040. const char* value, bool asString)
  2041. {
  2042. // Special hook to invalidate cached value.
  2043. if(prop == "DEBUG_CONFIGURATIONS")
  2044. {
  2045. this->DebugConfigs.clear();
  2046. }
  2047. this->Properties.AppendProperty(prop, value, cmProperty::GLOBAL, asString);
  2048. }
  2049. const char *cmake::GetProperty(const std::string& prop)
  2050. {
  2051. return this->GetProperty(prop, cmProperty::GLOBAL);
  2052. }
  2053. const char *cmake::GetProperty(const std::string& prop,
  2054. cmProperty::ScopeType scope)
  2055. {
  2056. bool chain = false;
  2057. // watch for special properties
  2058. std::string output = "";
  2059. if ( prop == "CACHE_VARIABLES" )
  2060. {
  2061. cmCacheManager::CacheIterator cit =
  2062. this->GetCacheManager()->GetCacheIterator();
  2063. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  2064. {
  2065. if ( output.size() )
  2066. {
  2067. output += ";";
  2068. }
  2069. output += cit.GetName();
  2070. }
  2071. this->SetProperty("CACHE_VARIABLES", output.c_str());
  2072. }
  2073. else if ( prop == "COMMANDS" )
  2074. {
  2075. cmake::RegisteredCommandsMap::iterator cmds
  2076. = this->GetCommands()->begin();
  2077. for (unsigned int cc=0 ; cmds != this->GetCommands()->end(); ++ cmds )
  2078. {
  2079. if ( cc > 0 )
  2080. {
  2081. output += ";";
  2082. }
  2083. output += cmds->first.c_str();
  2084. cc++;
  2085. }
  2086. this->SetProperty("COMMANDS",output.c_str());
  2087. }
  2088. else if ( prop == "IN_TRY_COMPILE" )
  2089. {
  2090. this->SetProperty("IN_TRY_COMPILE",
  2091. this->GetIsInTryCompile()? "1":"0");
  2092. }
  2093. else if ( prop == "ENABLED_LANGUAGES" )
  2094. {
  2095. std::string lang;
  2096. if(this->GlobalGenerator)
  2097. {
  2098. std::vector<std::string> enLangs;
  2099. this->GlobalGenerator->GetEnabledLanguages(enLangs);
  2100. const char* sep = "";
  2101. for(std::vector<std::string>::iterator i = enLangs.begin();
  2102. i != enLangs.end(); ++i)
  2103. {
  2104. lang += sep;
  2105. sep = ";";
  2106. lang += *i;
  2107. }
  2108. }
  2109. this->SetProperty("ENABLED_LANGUAGES", lang.c_str());
  2110. }
  2111. if (prop == "CMAKE_CXX_KNOWN_FEATURES")
  2112. {
  2113. #define STRING_LIST_ELEMENT(F) ";" #F
  2114. return FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT) + 1;
  2115. #undef STRING_LIST_ELEMENT
  2116. }
  2117. return this->Properties.GetPropertyValue(prop, scope, chain);
  2118. }
  2119. bool cmake::GetPropertyAsBool(const std::string& prop)
  2120. {
  2121. return cmSystemTools::IsOn(this->GetProperty(prop));
  2122. }
  2123. int cmake::GetSystemInformation(std::vector<std::string>& args)
  2124. {
  2125. // so create the directory
  2126. std::string resultFile;
  2127. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  2128. std::string destPath = cwd + "/__cmake_systeminformation";
  2129. cmSystemTools::RemoveADirectory(destPath.c_str());
  2130. if (!cmSystemTools::MakeDirectory(destPath.c_str()))
  2131. {
  2132. std::cerr << "Error: --system-information must be run from a "
  2133. "writable directory!\n";
  2134. return 1;
  2135. }
  2136. // process the arguments
  2137. bool writeToStdout = true;
  2138. for(unsigned int i=1; i < args.size(); ++i)
  2139. {
  2140. std::string arg = args[i];
  2141. if(arg.find("-V",0) == 0)
  2142. {
  2143. this->Verbose = true;
  2144. }
  2145. else if(arg.find("-G",0) == 0)
  2146. {
  2147. std::string value = arg.substr(2);
  2148. if(value.size() == 0)
  2149. {
  2150. ++i;
  2151. if(i >= args.size())
  2152. {
  2153. cmSystemTools::Error("No generator specified for -G");
  2154. return -1;
  2155. }
  2156. value = args[i];
  2157. }
  2158. cmGlobalGenerator* gen =
  2159. this->CreateGlobalGenerator(value);
  2160. if(!gen)
  2161. {
  2162. cmSystemTools::Error("Could not create named generator ",
  2163. value.c_str());
  2164. }
  2165. else
  2166. {
  2167. this->SetGlobalGenerator(gen);
  2168. }
  2169. }
  2170. // no option assume it is the output file
  2171. else
  2172. {
  2173. if (!cmSystemTools::FileIsFullPath(arg.c_str()))
  2174. {
  2175. resultFile = cwd;
  2176. resultFile += "/";
  2177. }
  2178. resultFile += arg;
  2179. writeToStdout = false;
  2180. }
  2181. }
  2182. // we have to find the module directory, so we can copy the files
  2183. this->AddCMakePaths();
  2184. std::string modulesPath =
  2185. this->CacheManager->GetCacheValue("CMAKE_ROOT");
  2186. modulesPath += "/Modules";
  2187. std::string inFile = modulesPath;
  2188. inFile += "/SystemInformation.cmake";
  2189. std::string outFile = destPath;
  2190. outFile += "/CMakeLists.txt";
  2191. // Copy file
  2192. if(!cmSystemTools::cmCopyFile(inFile.c_str(), outFile.c_str()))
  2193. {
  2194. std::cerr << "Error copying file \"" << inFile
  2195. << "\" to \"" << outFile << "\".\n";
  2196. return 1;
  2197. }
  2198. // do we write to a file or to stdout?
  2199. if (resultFile.size() == 0)
  2200. {
  2201. resultFile = cwd;
  2202. resultFile += "/__cmake_systeminformation/results.txt";
  2203. }
  2204. // now run cmake on the CMakeLists file
  2205. cmSystemTools::ChangeDirectory(destPath.c_str());
  2206. std::vector<std::string> args2;
  2207. args2.push_back(args[0]);
  2208. args2.push_back(destPath);
  2209. std::string resultArg = "-DRESULT_FILE=";
  2210. resultArg += resultFile;
  2211. args2.push_back(resultArg);
  2212. int res = this->Run(args2, false);
  2213. if (res != 0)
  2214. {
  2215. std::cerr << "Error: --system-information failed on internal CMake!\n";
  2216. return res;
  2217. }
  2218. // change back to the original directory
  2219. cmSystemTools::ChangeDirectory(cwd.c_str());
  2220. // echo results to stdout if needed
  2221. if (writeToStdout)
  2222. {
  2223. FILE* fin = cmsys::SystemTools::Fopen(resultFile.c_str(), "r");
  2224. if(fin)
  2225. {
  2226. const int bufferSize = 4096;
  2227. char buffer[bufferSize];
  2228. size_t n;
  2229. while((n = fread(buffer, 1, bufferSize, fin)) > 0)
  2230. {
  2231. for(char* c = buffer; c < buffer+n; ++c)
  2232. {
  2233. putc(*c, stdout);
  2234. }
  2235. fflush(stdout);
  2236. }
  2237. fclose(fin);
  2238. }
  2239. }
  2240. // clean up the directory
  2241. cmSystemTools::RemoveADirectory(destPath.c_str());
  2242. return 0;
  2243. }
  2244. //----------------------------------------------------------------------------
  2245. static bool cmakeCheckStampFile(const char* stampName)
  2246. {
  2247. // The stamp file does not exist. Use the stamp dependencies to
  2248. // determine whether it is really out of date. This works in
  2249. // conjunction with cmLocalVisualStudio7Generator to avoid
  2250. // repeatedly re-running CMake when the user rebuilds the entire
  2251. // solution.
  2252. std::string stampDepends = stampName;
  2253. stampDepends += ".depend";
  2254. #if defined(_WIN32) || defined(__CYGWIN__)
  2255. cmsys::ifstream fin(stampDepends.c_str(), std::ios::in | std::ios::binary);
  2256. #else
  2257. cmsys::ifstream fin(stampDepends.c_str(), std::ios::in);
  2258. #endif
  2259. if(!fin)
  2260. {
  2261. // The stamp dependencies file cannot be read. Just assume the
  2262. // build system is really out of date.
  2263. std::cout << "CMake is re-running because " << stampName
  2264. << " dependency file is missing.\n";
  2265. return false;
  2266. }
  2267. // Compare the stamp dependencies against the dependency file itself.
  2268. cmFileTimeComparison ftc;
  2269. std::string dep;
  2270. while(cmSystemTools::GetLineFromStream(fin, dep))
  2271. {
  2272. int result;
  2273. if(dep.length() >= 1 && dep[0] != '#' &&
  2274. (!ftc.FileTimeCompare(stampDepends.c_str(), dep.c_str(), &result)
  2275. || result < 0))
  2276. {
  2277. // The stamp depends file is older than this dependency. The
  2278. // build system is really out of date.
  2279. std::cout << "CMake is re-running because " << stampName
  2280. << " is out-of-date.\n";
  2281. std::cout << " the file '" << dep << "'\n";
  2282. std::cout << " is newer than '" << stampDepends << "'\n";
  2283. std::cout << " result='" << result << "'\n";
  2284. return false;
  2285. }
  2286. }
  2287. // The build system is up to date. The stamp file has been removed
  2288. // by the VS IDE due to a "rebuild" request. Restore it atomically.
  2289. cmOStringStream stampTempStream;
  2290. stampTempStream << stampName << ".tmp" << cmSystemTools::RandomSeed();
  2291. std::string stampTempString = stampTempStream.str();
  2292. const char* stampTemp = stampTempString.c_str();
  2293. {
  2294. // TODO: Teach cmGeneratedFileStream to use a random temp file (with
  2295. // multiple tries in unlikely case of conflict) and use that here.
  2296. cmsys::ofstream stamp(stampTemp);
  2297. stamp << "# CMake generation timestamp file for this directory.\n";
  2298. }
  2299. if(cmSystemTools::RenameFile(stampTemp, stampName))
  2300. {
  2301. // Notify the user why CMake is not re-running. It is safe to
  2302. // just print to stdout here because this code is only reachable
  2303. // through an undocumented flag used by the VS generator.
  2304. std::cout << "CMake does not need to re-run because "
  2305. << stampName << " is up-to-date.\n";
  2306. return true;
  2307. }
  2308. else
  2309. {
  2310. cmSystemTools::RemoveFile(stampTemp);
  2311. cmSystemTools::Error("Cannot restore timestamp ", stampName);
  2312. return false;
  2313. }
  2314. }
  2315. //----------------------------------------------------------------------------
  2316. static bool cmakeCheckStampList(const char* stampList)
  2317. {
  2318. // If the stamp list does not exist CMake must rerun to generate it.
  2319. if(!cmSystemTools::FileExists(stampList))
  2320. {
  2321. std::cout << "CMake is re-running because generate.stamp.list "
  2322. << "is missing.\n";
  2323. return false;
  2324. }
  2325. cmsys::ifstream fin(stampList);
  2326. if(!fin)
  2327. {
  2328. std::cout << "CMake is re-running because generate.stamp.list "
  2329. << "could not be read.\n";
  2330. return false;
  2331. }
  2332. // Check each stamp.
  2333. std::string stampName;
  2334. while(cmSystemTools::GetLineFromStream(fin, stampName))
  2335. {
  2336. if(!cmakeCheckStampFile(stampName.c_str()))
  2337. {
  2338. return false;
  2339. }
  2340. }
  2341. return true;
  2342. }
  2343. //----------------------------------------------------------------------------
  2344. void cmake::IssueMessage(cmake::MessageType t, std::string const& text,
  2345. cmListFileBacktrace const& backtrace)
  2346. {
  2347. cmOStringStream msg;
  2348. bool isError = false;
  2349. // Construct the message header.
  2350. if(t == cmake::FATAL_ERROR)
  2351. {
  2352. isError = true;
  2353. msg << "CMake Error";
  2354. }
  2355. else if(t == cmake::INTERNAL_ERROR)
  2356. {
  2357. isError = true;
  2358. msg << "CMake Internal Error (please report a bug)";
  2359. }
  2360. else if(t == cmake::LOG)
  2361. {
  2362. msg << "CMake Debug Log";
  2363. }
  2364. else if(t == cmake::DEPRECATION_ERROR)
  2365. {
  2366. msg << "CMake Deprecation Error";
  2367. isError = true;
  2368. }
  2369. else if (t == cmake::DEPRECATION_WARNING)
  2370. {
  2371. msg << "CMake Deprecation Warning";
  2372. }
  2373. else
  2374. {
  2375. msg << "CMake Warning";
  2376. if(t == cmake::AUTHOR_WARNING)
  2377. {
  2378. // Allow suppression of these warnings.
  2379. cmCacheManager::CacheIterator it = this->CacheManager
  2380. ->GetCacheIterator("CMAKE_SUPPRESS_DEVELOPER_WARNINGS");
  2381. if(!it.IsAtEnd() && it.GetValueAsBool())
  2382. {
  2383. return;
  2384. }
  2385. msg << " (dev)";
  2386. }
  2387. }
  2388. // Add the immediate context.
  2389. cmListFileBacktrace::const_iterator i = backtrace.begin();
  2390. if(i != backtrace.end())
  2391. {
  2392. cmListFileContext const& lfc = *i;
  2393. msg << (lfc.Line? " at ": " in ") << lfc;
  2394. ++i;
  2395. }
  2396. // Add the message text.
  2397. {
  2398. msg << ":\n";
  2399. cmDocumentationFormatter formatter;
  2400. formatter.SetIndent(" ");
  2401. formatter.PrintFormatted(msg, text.c_str());
  2402. }
  2403. // Add the rest of the context.
  2404. if(i != backtrace.end())
  2405. {
  2406. msg << "Call Stack (most recent call first):\n";
  2407. while(i != backtrace.end())
  2408. {
  2409. cmListFileContext const& lfc = *i;
  2410. msg << " " << lfc << "\n";
  2411. ++i;
  2412. }
  2413. }
  2414. // Add a note about warning suppression.
  2415. if(t == cmake::AUTHOR_WARNING)
  2416. {
  2417. msg <<
  2418. "This warning is for project developers. Use -Wno-dev to suppress it.";
  2419. }
  2420. // Add a terminating blank line.
  2421. msg << "\n";
  2422. #if defined(CMAKE_BUILD_WITH_CMAKE)
  2423. // Add a C++ stack trace to internal errors.
  2424. if(t == cmake::INTERNAL_ERROR)
  2425. {
  2426. std::string stack = cmsys::SystemInformation::GetProgramStack(0,0);
  2427. if(!stack.empty())
  2428. {
  2429. if(cmHasLiteralPrefix(stack, "WARNING:"))
  2430. {
  2431. stack = "Note:" + stack.substr(8);
  2432. }
  2433. msg << stack << "\n";
  2434. }
  2435. }
  2436. #endif
  2437. // Output the message.
  2438. if(isError)
  2439. {
  2440. cmSystemTools::SetErrorOccured();
  2441. cmSystemTools::Message(msg.str().c_str(), "Error");
  2442. }
  2443. else
  2444. {
  2445. cmSystemTools::Message(msg.str().c_str(), "Warning");
  2446. }
  2447. }
  2448. //----------------------------------------------------------------------------
  2449. std::vector<std::string> const& cmake::GetDebugConfigs()
  2450. {
  2451. // Compute on-demand.
  2452. if(this->DebugConfigs.empty())
  2453. {
  2454. if(const char* config_list = this->GetProperty("DEBUG_CONFIGURATIONS"))
  2455. {
  2456. // Expand the specified list and convert to upper-case.
  2457. cmSystemTools::ExpandListArgument(config_list, this->DebugConfigs);
  2458. for(std::vector<std::string>::iterator i = this->DebugConfigs.begin();
  2459. i != this->DebugConfigs.end(); ++i)
  2460. {
  2461. *i = cmSystemTools::UpperCase(*i);
  2462. }
  2463. }
  2464. // If no configurations were specified, use a default list.
  2465. if(this->DebugConfigs.empty())
  2466. {
  2467. this->DebugConfigs.push_back("DEBUG");
  2468. }
  2469. }
  2470. return this->DebugConfigs;
  2471. }
  2472. int cmake::Build(const std::string& dir,
  2473. const std::string& target,
  2474. const std::string& config,
  2475. const std::vector<std::string>& nativeOptions,
  2476. bool clean)
  2477. {
  2478. if(!cmSystemTools::FileIsDirectory(dir.c_str()))
  2479. {
  2480. std::cerr << "Error: " << dir << " is not a directory\n";
  2481. return 1;
  2482. }
  2483. std::string cachePath = dir;
  2484. cmSystemTools::ConvertToUnixSlashes(cachePath);
  2485. cmCacheManager* cachem = this->GetCacheManager();
  2486. cmCacheManager::CacheIterator it = cachem->NewIterator();
  2487. if(!cachem->LoadCache(cachePath))
  2488. {
  2489. std::cerr << "Error: could not load cache\n";
  2490. return 1;
  2491. }
  2492. if(!it.Find("CMAKE_GENERATOR"))
  2493. {
  2494. std::cerr << "Error: could find generator in Cache\n";
  2495. return 1;
  2496. }
  2497. cmsys::auto_ptr<cmGlobalGenerator> gen(
  2498. this->CreateGlobalGenerator(it.GetValue()));
  2499. std::string output;
  2500. std::string projName;
  2501. if(!it.Find("CMAKE_PROJECT_NAME"))
  2502. {
  2503. std::cerr << "Error: could not find CMAKE_PROJECT_NAME in Cache\n";
  2504. return 1;
  2505. }
  2506. projName = it.GetValue();
  2507. return gen->Build("", dir,
  2508. projName, target,
  2509. &output,
  2510. "",
  2511. config, clean, false, 0,
  2512. cmSystemTools::OUTPUT_PASSTHROUGH,
  2513. nativeOptions);
  2514. }
  2515. void cmake::WatchUnusedCli(const std::string& var)
  2516. {
  2517. #ifdef CMAKE_BUILD_WITH_CMAKE
  2518. this->VariableWatch->AddWatch(var, cmWarnUnusedCliWarning, this);
  2519. if(this->UsedCliVariables.find(var) == this->UsedCliVariables.end())
  2520. {
  2521. this->UsedCliVariables[var] = false;
  2522. }
  2523. #endif
  2524. }
  2525. void cmake::UnwatchUnusedCli(const std::string& var)
  2526. {
  2527. #ifdef CMAKE_BUILD_WITH_CMAKE
  2528. this->VariableWatch->RemoveWatch(var, cmWarnUnusedCliWarning);
  2529. this->UsedCliVariables.erase(var);
  2530. #endif
  2531. }
  2532. void cmake::RunCheckForUnusedVariables()
  2533. {
  2534. #ifdef CMAKE_BUILD_WITH_CMAKE
  2535. bool haveUnused = false;
  2536. cmOStringStream msg;
  2537. msg << "Manually-specified variables were not used by the project:";
  2538. for(std::map<std::string, bool>::const_iterator
  2539. it = this->UsedCliVariables.begin();
  2540. it != this->UsedCliVariables.end(); ++it)
  2541. {
  2542. if(!it->second)
  2543. {
  2544. haveUnused = true;
  2545. msg << "\n " << it->first;
  2546. }
  2547. }
  2548. if(haveUnused)
  2549. {
  2550. this->IssueMessage(cmake::WARNING, msg.str(), cmListFileBacktrace());
  2551. }
  2552. #endif
  2553. }