cmake.cxx 86 KB

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