cmake.cxx 77 KB

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