cmLocalGenerator.cxx 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmLocalGenerator.h"
  4. #include "cmAlgorithms.h"
  5. #include "cmComputeLinkInformation.h"
  6. #include "cmCustomCommandGenerator.h"
  7. #include "cmGeneratedFileStream.h"
  8. #include "cmGeneratorExpressionEvaluationFile.h"
  9. #include "cmGeneratorTarget.h"
  10. #include "cmGlobalGenerator.h"
  11. #include "cmInstallGenerator.h"
  12. #include "cmInstallScriptGenerator.h"
  13. #include "cmInstallTargetGenerator.h"
  14. #include "cmLinkLineComputer.h"
  15. #include "cmMakefile.h"
  16. #include "cmRulePlaceholderExpander.h"
  17. #include "cmSourceFile.h"
  18. #include "cmSourceFileLocation.h"
  19. #include "cmState.h"
  20. #include "cmStateDirectory.h"
  21. #include "cmStateTypes.h"
  22. #include "cmSystemTools.h"
  23. #include "cmTarget.h"
  24. #include "cmTestGenerator.h"
  25. #include "cmVersion.h"
  26. #include "cmake.h"
  27. #if defined(CMAKE_BUILD_WITH_CMAKE)
  28. # define CM_LG_ENCODE_OBJECT_NAMES
  29. # include "cmCryptoHash.h"
  30. #endif
  31. #include "cmsys/RegularExpression.hxx"
  32. #include <algorithm>
  33. #include <assert.h>
  34. #include <iterator>
  35. #include <sstream>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <unordered_set>
  39. #include <utility>
  40. #if defined(__HAIKU__)
  41. # include <FindDirectory.h>
  42. # include <StorageDefs.h>
  43. #endif
  44. // List of variables that are replaced when
  45. // rules are expanced. These variables are
  46. // replaced in the form <var> with GetSafeDefinition(var).
  47. // ${LANG} is replaced in the variable first with all enabled
  48. // languages.
  49. static const char* ruleReplaceVars[] = {
  50. "CMAKE_${LANG}_COMPILER",
  51. "CMAKE_SHARED_LIBRARY_CREATE_${LANG}_FLAGS",
  52. "CMAKE_SHARED_MODULE_CREATE_${LANG}_FLAGS",
  53. "CMAKE_SHARED_MODULE_${LANG}_FLAGS",
  54. "CMAKE_SHARED_LIBRARY_${LANG}_FLAGS",
  55. "CMAKE_${LANG}_LINK_FLAGS",
  56. "CMAKE_SHARED_LIBRARY_SONAME_${LANG}_FLAG",
  57. "CMAKE_${LANG}_ARCHIVE",
  58. "CMAKE_AR",
  59. "CMAKE_CURRENT_SOURCE_DIR",
  60. "CMAKE_CURRENT_BINARY_DIR",
  61. "CMAKE_RANLIB",
  62. "CMAKE_LINKER",
  63. "CMAKE_MT",
  64. "CMAKE_CUDA_HOST_COMPILER",
  65. "CMAKE_CUDA_HOST_LINK_LAUNCHER",
  66. "CMAKE_CL_SHOWINCLUDES_PREFIX"
  67. };
  68. cmLocalGenerator::cmLocalGenerator(cmGlobalGenerator* gg, cmMakefile* makefile)
  69. : cmOutputConverter(makefile->GetStateSnapshot())
  70. , StateSnapshot(makefile->GetStateSnapshot())
  71. , DirectoryBacktrace(makefile->GetBacktrace())
  72. {
  73. this->GlobalGenerator = gg;
  74. this->Makefile = makefile;
  75. this->AliasTargets = makefile->GetAliasTargets();
  76. this->EmitUniversalBinaryFlags = true;
  77. this->BackwardsCompatibility = 0;
  78. this->BackwardsCompatibilityFinal = false;
  79. this->ComputeObjectMaxPath();
  80. std::vector<std::string> enabledLanguages =
  81. this->GetState()->GetEnabledLanguages();
  82. if (const char* sysrootCompile =
  83. this->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE")) {
  84. this->CompilerSysroot = sysrootCompile;
  85. } else {
  86. this->CompilerSysroot = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  87. }
  88. if (const char* sysrootLink =
  89. this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK")) {
  90. this->LinkerSysroot = sysrootLink;
  91. } else {
  92. this->LinkerSysroot = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  93. }
  94. for (std::string const& lang : enabledLanguages) {
  95. if (lang == "NONE") {
  96. continue;
  97. }
  98. this->Compilers["CMAKE_" + lang + "_COMPILER"] = lang;
  99. this->VariableMappings["CMAKE_" + lang + "_COMPILER"] =
  100. this->Makefile->GetSafeDefinition("CMAKE_" + lang + "_COMPILER");
  101. std::string const& compilerArg1 = "CMAKE_" + lang + "_COMPILER_ARG1";
  102. std::string const& compilerTarget = "CMAKE_" + lang + "_COMPILER_TARGET";
  103. std::string const& compilerOptionTarget =
  104. "CMAKE_" + lang + "_COMPILE_OPTIONS_TARGET";
  105. std::string const& compilerExternalToolchain =
  106. "CMAKE_" + lang + "_COMPILER_EXTERNAL_TOOLCHAIN";
  107. std::string const& compilerOptionExternalToolchain =
  108. "CMAKE_" + lang + "_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN";
  109. std::string const& compilerOptionSysroot =
  110. "CMAKE_" + lang + "_COMPILE_OPTIONS_SYSROOT";
  111. this->VariableMappings[compilerArg1] =
  112. this->Makefile->GetSafeDefinition(compilerArg1);
  113. this->VariableMappings[compilerTarget] =
  114. this->Makefile->GetSafeDefinition(compilerTarget);
  115. this->VariableMappings[compilerOptionTarget] =
  116. this->Makefile->GetSafeDefinition(compilerOptionTarget);
  117. this->VariableMappings[compilerExternalToolchain] =
  118. this->Makefile->GetSafeDefinition(compilerExternalToolchain);
  119. this->VariableMappings[compilerOptionExternalToolchain] =
  120. this->Makefile->GetSafeDefinition(compilerOptionExternalToolchain);
  121. this->VariableMappings[compilerOptionSysroot] =
  122. this->Makefile->GetSafeDefinition(compilerOptionSysroot);
  123. for (const char* const* replaceIter = cm::cbegin(ruleReplaceVars);
  124. replaceIter != cm::cend(ruleReplaceVars); ++replaceIter) {
  125. std::string actualReplace = *replaceIter;
  126. if (actualReplace.find("${LANG}") != std::string::npos) {
  127. cmSystemTools::ReplaceString(actualReplace, "${LANG}", lang);
  128. }
  129. this->VariableMappings[actualReplace] =
  130. this->Makefile->GetSafeDefinition(actualReplace);
  131. }
  132. }
  133. }
  134. cmRulePlaceholderExpander* cmLocalGenerator::CreateRulePlaceholderExpander()
  135. const
  136. {
  137. return new cmRulePlaceholderExpander(this->Compilers, this->VariableMappings,
  138. this->CompilerSysroot,
  139. this->LinkerSysroot);
  140. }
  141. cmLocalGenerator::~cmLocalGenerator()
  142. {
  143. cmDeleteAll(this->GeneratorTargets);
  144. cmDeleteAll(this->OwnedImportedGeneratorTargets);
  145. }
  146. void cmLocalGenerator::IssueMessage(cmake::MessageType t,
  147. std::string const& text) const
  148. {
  149. this->GetCMakeInstance()->IssueMessage(t, text, this->DirectoryBacktrace);
  150. }
  151. void cmLocalGenerator::ComputeObjectMaxPath()
  152. {
  153. // Choose a maximum object file name length.
  154. #if defined(_WIN32) || defined(__CYGWIN__)
  155. this->ObjectPathMax = 250;
  156. #else
  157. this->ObjectPathMax = 1000;
  158. #endif
  159. const char* plen = this->Makefile->GetDefinition("CMAKE_OBJECT_PATH_MAX");
  160. if (plen && *plen) {
  161. unsigned int pmax;
  162. if (sscanf(plen, "%u", &pmax) == 1) {
  163. if (pmax >= 128) {
  164. this->ObjectPathMax = pmax;
  165. } else {
  166. std::ostringstream w;
  167. w << "CMAKE_OBJECT_PATH_MAX is set to " << pmax
  168. << ", which is less than the minimum of 128. "
  169. << "The value will be ignored.";
  170. this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  171. }
  172. } else {
  173. std::ostringstream w;
  174. w << "CMAKE_OBJECT_PATH_MAX is set to \"" << plen
  175. << "\", which fails to parse as a positive integer. "
  176. << "The value will be ignored.";
  177. this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  178. }
  179. }
  180. this->ObjectMaxPathViolations.clear();
  181. }
  182. static void MoveSystemIncludesToEnd(std::vector<std::string>& includeDirs,
  183. const std::string& config,
  184. const std::string& lang,
  185. const cmGeneratorTarget* target)
  186. {
  187. if (!target) {
  188. return;
  189. }
  190. std::stable_sort(
  191. includeDirs.begin(), includeDirs.end(),
  192. [&target, &config, &lang](std::string const& a, std::string const& b) {
  193. return !target->IsSystemIncludeDirectory(a, config, lang) &&
  194. target->IsSystemIncludeDirectory(b, config, lang);
  195. });
  196. }
  197. static void MoveSystemIncludesToEnd(std::vector<BT<std::string>>& includeDirs,
  198. const std::string& config,
  199. const std::string& lang,
  200. const cmGeneratorTarget* target)
  201. {
  202. if (!target) {
  203. return;
  204. }
  205. std::stable_sort(includeDirs.begin(), includeDirs.end(),
  206. [target, &config, &lang](BT<std::string> const& a,
  207. BT<std::string> const& b) {
  208. return !target->IsSystemIncludeDirectory(a.Value, config,
  209. lang) &&
  210. target->IsSystemIncludeDirectory(b.Value, config, lang);
  211. });
  212. }
  213. void cmLocalGenerator::TraceDependencies()
  214. {
  215. std::vector<std::string> configs;
  216. this->Makefile->GetConfigurations(configs);
  217. if (configs.empty()) {
  218. configs.emplace_back();
  219. }
  220. for (std::string const& c : configs) {
  221. this->GlobalGenerator->CreateEvaluationSourceFiles(c);
  222. }
  223. // Generate the rule files for each target.
  224. const std::vector<cmGeneratorTarget*>& targets = this->GetGeneratorTargets();
  225. for (cmGeneratorTarget* target : targets) {
  226. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  227. continue;
  228. }
  229. target->TraceDependencies();
  230. }
  231. }
  232. void cmLocalGenerator::GenerateTestFiles()
  233. {
  234. if (!this->Makefile->IsOn("CMAKE_TESTING_ENABLED")) {
  235. return;
  236. }
  237. // Compute the set of configurations.
  238. std::vector<std::string> configurationTypes;
  239. const std::string& config =
  240. this->Makefile->GetConfigurations(configurationTypes, false);
  241. std::string file = this->StateSnapshot.GetDirectory().GetCurrentBinary();
  242. file += "/";
  243. file += "CTestTestfile.cmake";
  244. cmGeneratedFileStream fout(file);
  245. fout.SetCopyIfDifferent(true);
  246. fout << "# CMake generated Testfile for " << std::endl
  247. << "# Source directory: "
  248. << this->StateSnapshot.GetDirectory().GetCurrentSource() << std::endl
  249. << "# Build directory: "
  250. << this->StateSnapshot.GetDirectory().GetCurrentBinary() << std::endl
  251. << "# " << std::endl
  252. << "# This file includes the relevant testing commands "
  253. << "required for " << std::endl
  254. << "# testing this directory and lists subdirectories to "
  255. << "be tested as well." << std::endl;
  256. const char* testIncludeFile =
  257. this->Makefile->GetProperty("TEST_INCLUDE_FILE");
  258. if (testIncludeFile) {
  259. fout << "include(\"" << testIncludeFile << "\")" << std::endl;
  260. }
  261. const char* testIncludeFiles =
  262. this->Makefile->GetProperty("TEST_INCLUDE_FILES");
  263. if (testIncludeFiles) {
  264. std::vector<std::string> includesList;
  265. cmSystemTools::ExpandListArgument(testIncludeFiles, includesList);
  266. for (std::string const& i : includesList) {
  267. fout << "include(\"" << i << "\")" << std::endl;
  268. }
  269. }
  270. // Ask each test generator to write its code.
  271. std::vector<cmTestGenerator*> const& testers =
  272. this->Makefile->GetTestGenerators();
  273. for (cmTestGenerator* tester : testers) {
  274. tester->Compute(this);
  275. tester->Generate(fout, config, configurationTypes);
  276. }
  277. typedef std::vector<cmStateSnapshot> vec_t;
  278. vec_t const& children = this->Makefile->GetStateSnapshot().GetChildren();
  279. std::string parentBinDir = this->GetCurrentBinaryDirectory();
  280. for (cmStateSnapshot const& i : children) {
  281. // TODO: Use add_subdirectory instead?
  282. std::string outP = i.GetDirectory().GetCurrentBinary();
  283. outP = this->ConvertToRelativePath(parentBinDir, outP);
  284. outP = cmOutputConverter::EscapeForCMake(outP);
  285. fout << "subdirs(" << outP << ")" << std::endl;
  286. }
  287. // Add directory labels property
  288. const char* directoryLabels =
  289. this->Makefile->GetDefinition("CMAKE_DIRECTORY_LABELS");
  290. const char* labels = this->Makefile->GetProperty("LABELS");
  291. if (labels || directoryLabels) {
  292. fout << "set_directory_properties(PROPERTIES LABELS ";
  293. if (labels) {
  294. fout << cmOutputConverter::EscapeForCMake(labels);
  295. }
  296. if (labels && directoryLabels) {
  297. fout << ";";
  298. }
  299. if (directoryLabels) {
  300. fout << cmOutputConverter::EscapeForCMake(directoryLabels);
  301. }
  302. fout << ")" << std::endl;
  303. }
  304. }
  305. void cmLocalGenerator::CreateEvaluationFileOutputs(std::string const& config)
  306. {
  307. std::vector<cmGeneratorExpressionEvaluationFile*> ef =
  308. this->Makefile->GetEvaluationFiles();
  309. for (cmGeneratorExpressionEvaluationFile* geef : ef) {
  310. geef->CreateOutputFile(this, config);
  311. }
  312. }
  313. void cmLocalGenerator::ProcessEvaluationFiles(
  314. std::vector<std::string>& generatedFiles)
  315. {
  316. std::vector<cmGeneratorExpressionEvaluationFile*> ef =
  317. this->Makefile->GetEvaluationFiles();
  318. for (cmGeneratorExpressionEvaluationFile* geef : ef) {
  319. geef->Generate(this);
  320. if (cmSystemTools::GetFatalErrorOccured()) {
  321. return;
  322. }
  323. std::vector<std::string> files = geef->GetFiles();
  324. std::sort(files.begin(), files.end());
  325. std::vector<std::string> intersection;
  326. std::set_intersection(files.begin(), files.end(), generatedFiles.begin(),
  327. generatedFiles.end(),
  328. std::back_inserter(intersection));
  329. if (!intersection.empty()) {
  330. cmSystemTools::Error("Files to be generated by multiple different "
  331. "commands: ",
  332. cmWrap('"', intersection, '"', " ").c_str());
  333. return;
  334. }
  335. generatedFiles.insert(generatedFiles.end(), files.begin(), files.end());
  336. std::vector<std::string>::iterator newIt =
  337. generatedFiles.end() - files.size();
  338. std::inplace_merge(generatedFiles.begin(), newIt, generatedFiles.end());
  339. }
  340. }
  341. void cmLocalGenerator::GenerateInstallRules()
  342. {
  343. // Compute the install prefix.
  344. const char* prefix = this->Makefile->GetDefinition("CMAKE_INSTALL_PREFIX");
  345. #if defined(_WIN32) && !defined(__CYGWIN__)
  346. std::string prefix_win32;
  347. if (!prefix) {
  348. if (!cmSystemTools::GetEnv("SystemDrive", prefix_win32)) {
  349. prefix_win32 = "C:";
  350. }
  351. const char* project_name = this->Makefile->GetDefinition("PROJECT_NAME");
  352. if (project_name && project_name[0]) {
  353. prefix_win32 += "/Program Files/";
  354. prefix_win32 += project_name;
  355. } else {
  356. prefix_win32 += "/InstalledCMakeProject";
  357. }
  358. prefix = prefix_win32.c_str();
  359. }
  360. #elif defined(__HAIKU__)
  361. char dir[B_PATH_NAME_LENGTH];
  362. if (!prefix) {
  363. if (find_directory(B_SYSTEM_DIRECTORY, -1, false, dir, sizeof(dir)) ==
  364. B_OK) {
  365. prefix = dir;
  366. } else {
  367. prefix = "/boot/system";
  368. }
  369. }
  370. #else
  371. if (!prefix) {
  372. prefix = "/usr/local";
  373. }
  374. #endif
  375. if (const char* stagingPrefix =
  376. this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX")) {
  377. prefix = stagingPrefix;
  378. }
  379. // Compute the set of configurations.
  380. std::vector<std::string> configurationTypes;
  381. const std::string& config =
  382. this->Makefile->GetConfigurations(configurationTypes, false);
  383. // Choose a default install configuration.
  384. std::string default_config = config;
  385. const char* default_order[] = { "RELEASE", "MINSIZEREL", "RELWITHDEBINFO",
  386. "DEBUG", nullptr };
  387. for (const char** c = default_order; *c && default_config.empty(); ++c) {
  388. for (std::string const& configurationType : configurationTypes) {
  389. if (cmSystemTools::UpperCase(configurationType) == *c) {
  390. default_config = configurationType;
  391. }
  392. }
  393. }
  394. if (default_config.empty() && !configurationTypes.empty()) {
  395. default_config = configurationTypes[0];
  396. }
  397. // Create the install script file.
  398. std::string file = this->StateSnapshot.GetDirectory().GetCurrentBinary();
  399. std::string homedir = this->GetState()->GetBinaryDirectory();
  400. int toplevel_install = 0;
  401. if (file == homedir) {
  402. toplevel_install = 1;
  403. }
  404. file += "/cmake_install.cmake";
  405. cmGeneratedFileStream fout(file);
  406. fout.SetCopyIfDifferent(true);
  407. // Write the header.
  408. fout << "# Install script for directory: "
  409. << this->StateSnapshot.GetDirectory().GetCurrentSource() << std::endl
  410. << std::endl;
  411. fout << "# Set the install prefix" << std::endl
  412. << "if(NOT DEFINED CMAKE_INSTALL_PREFIX)" << std::endl
  413. << " set(CMAKE_INSTALL_PREFIX \"" << prefix << "\")" << std::endl
  414. << "endif()" << std::endl
  415. << "string(REGEX REPLACE \"/$\" \"\" CMAKE_INSTALL_PREFIX "
  416. << "\"${CMAKE_INSTALL_PREFIX}\")" << std::endl
  417. << std::endl;
  418. // Write support code for generating per-configuration install rules.
  419. /* clang-format off */
  420. fout <<
  421. "# Set the install configuration name.\n"
  422. "if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)\n"
  423. " if(BUILD_TYPE)\n"
  424. " string(REGEX REPLACE \"^[^A-Za-z0-9_]+\" \"\"\n"
  425. " CMAKE_INSTALL_CONFIG_NAME \"${BUILD_TYPE}\")\n"
  426. " else()\n"
  427. " set(CMAKE_INSTALL_CONFIG_NAME \"" << default_config << "\")\n"
  428. " endif()\n"
  429. " message(STATUS \"Install configuration: "
  430. "\\\"${CMAKE_INSTALL_CONFIG_NAME}\\\"\")\n"
  431. "endif()\n"
  432. "\n";
  433. /* clang-format on */
  434. // Write support code for dealing with component-specific installs.
  435. /* clang-format off */
  436. fout <<
  437. "# Set the component getting installed.\n"
  438. "if(NOT CMAKE_INSTALL_COMPONENT)\n"
  439. " if(COMPONENT)\n"
  440. " message(STATUS \"Install component: \\\"${COMPONENT}\\\"\")\n"
  441. " set(CMAKE_INSTALL_COMPONENT \"${COMPONENT}\")\n"
  442. " else()\n"
  443. " set(CMAKE_INSTALL_COMPONENT)\n"
  444. " endif()\n"
  445. "endif()\n"
  446. "\n";
  447. /* clang-format on */
  448. // Copy user-specified install options to the install code.
  449. if (const char* so_no_exe =
  450. this->Makefile->GetDefinition("CMAKE_INSTALL_SO_NO_EXE")) {
  451. /* clang-format off */
  452. fout <<
  453. "# Install shared libraries without execute permission?\n"
  454. "if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)\n"
  455. " set(CMAKE_INSTALL_SO_NO_EXE \"" << so_no_exe << "\")\n"
  456. "endif()\n"
  457. "\n";
  458. /* clang-format on */
  459. }
  460. // Copy cmake cross compile state to install code.
  461. if (const char* crosscompiling =
  462. this->Makefile->GetDefinition("CMAKE_CROSSCOMPILING")) {
  463. /* clang-format off */
  464. fout <<
  465. "# Is this installation the result of a crosscompile?\n"
  466. "if(NOT DEFINED CMAKE_CROSSCOMPILING)\n"
  467. " set(CMAKE_CROSSCOMPILING \"" << crosscompiling << "\")\n"
  468. "endif()\n"
  469. "\n";
  470. /* clang-format on */
  471. }
  472. // Write default directory permissions.
  473. if (const char* defaultDirPermissions = this->Makefile->GetDefinition(
  474. "CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS")) {
  475. /* clang-format off */
  476. fout <<
  477. "# Set default install directory permissions.\n"
  478. "if(NOT DEFINED CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS)\n"
  479. " set(CMAKE_INSTALL_DEFAULT_DIRECTORY_PERMISSIONS \""
  480. << defaultDirPermissions << "\")\n"
  481. "endif()\n"
  482. "\n";
  483. /* clang-format on */
  484. }
  485. // Ask each install generator to write its code.
  486. cmPolicies::PolicyStatus status = this->GetPolicyStatus(cmPolicies::CMP0082);
  487. std::vector<cmInstallGenerator*> const& installers =
  488. this->Makefile->GetInstallGenerators();
  489. bool haveSubdirectoryInstall = false;
  490. bool haveInstallAfterSubdirectory = false;
  491. if (status == cmPolicies::WARN) {
  492. for (cmInstallGenerator* installer : installers) {
  493. installer->CheckCMP0082(haveSubdirectoryInstall,
  494. haveInstallAfterSubdirectory);
  495. installer->Generate(fout, config, configurationTypes);
  496. }
  497. } else {
  498. for (cmInstallGenerator* installer : installers) {
  499. installer->Generate(fout, config, configurationTypes);
  500. }
  501. }
  502. // Write rules from old-style specification stored in targets.
  503. this->GenerateTargetInstallRules(fout, config, configurationTypes);
  504. // Include install scripts from subdirectories.
  505. switch (status) {
  506. case cmPolicies::WARN:
  507. if (haveInstallAfterSubdirectory &&
  508. this->Makefile->PolicyOptionalWarningEnabled(
  509. "CMAKE_POLICY_WARNING_CMP0082")) {
  510. std::ostringstream e;
  511. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0082) << "\n";
  512. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  513. }
  514. CM_FALLTHROUGH;
  515. case cmPolicies::OLD: {
  516. std::vector<cmStateSnapshot> children =
  517. this->Makefile->GetStateSnapshot().GetChildren();
  518. if (!children.empty()) {
  519. fout << "if(NOT CMAKE_INSTALL_LOCAL_ONLY)\n";
  520. fout << " # Include the install script for each subdirectory.\n";
  521. for (cmStateSnapshot const& c : children) {
  522. if (!c.GetDirectory().GetPropertyAsBool("EXCLUDE_FROM_ALL")) {
  523. std::string odir = c.GetDirectory().GetCurrentBinary();
  524. cmSystemTools::ConvertToUnixSlashes(odir);
  525. fout << " include(\"" << odir << "/cmake_install.cmake\")"
  526. << std::endl;
  527. }
  528. }
  529. fout << "\n";
  530. fout << "endif()\n\n";
  531. }
  532. } break;
  533. case cmPolicies::REQUIRED_IF_USED:
  534. case cmPolicies::REQUIRED_ALWAYS:
  535. case cmPolicies::NEW:
  536. // NEW behavior is handled in
  537. // cmInstallSubdirectoryGenerator::GenerateScript()
  538. break;
  539. }
  540. // Record the install manifest.
  541. if (toplevel_install) {
  542. /* clang-format off */
  543. fout <<
  544. "if(CMAKE_INSTALL_COMPONENT)\n"
  545. " set(CMAKE_INSTALL_MANIFEST \"install_manifest_"
  546. "${CMAKE_INSTALL_COMPONENT}.txt\")\n"
  547. "else()\n"
  548. " set(CMAKE_INSTALL_MANIFEST \"install_manifest.txt\")\n"
  549. "endif()\n"
  550. "\n"
  551. "string(REPLACE \";\" \"\\n\" CMAKE_INSTALL_MANIFEST_CONTENT\n"
  552. " \"${CMAKE_INSTALL_MANIFEST_FILES}\")\n"
  553. "file(WRITE \"" << homedir << "/${CMAKE_INSTALL_MANIFEST}\"\n"
  554. " \"${CMAKE_INSTALL_MANIFEST_CONTENT}\")\n";
  555. /* clang-format on */
  556. }
  557. }
  558. void cmLocalGenerator::AddGeneratorTarget(cmGeneratorTarget* gt)
  559. {
  560. this->GeneratorTargets.push_back(gt);
  561. this->GeneratorTargetSearchIndex.emplace(gt->GetName(), gt);
  562. this->GlobalGenerator->IndexGeneratorTarget(gt);
  563. }
  564. void cmLocalGenerator::AddImportedGeneratorTarget(cmGeneratorTarget* gt)
  565. {
  566. this->ImportedGeneratorTargets.emplace(gt->GetName(), gt);
  567. this->GlobalGenerator->IndexGeneratorTarget(gt);
  568. }
  569. void cmLocalGenerator::AddOwnedImportedGeneratorTarget(cmGeneratorTarget* gt)
  570. {
  571. this->OwnedImportedGeneratorTargets.push_back(gt);
  572. }
  573. cmGeneratorTarget* cmLocalGenerator::FindLocalNonAliasGeneratorTarget(
  574. const std::string& name) const
  575. {
  576. GeneratorTargetMap::const_iterator ti =
  577. this->GeneratorTargetSearchIndex.find(name);
  578. if (ti != this->GeneratorTargetSearchIndex.end()) {
  579. return ti->second;
  580. }
  581. return nullptr;
  582. }
  583. void cmLocalGenerator::ComputeTargetManifest()
  584. {
  585. // Collect the set of configuration types.
  586. std::vector<std::string> configNames;
  587. this->Makefile->GetConfigurations(configNames);
  588. if (configNames.empty()) {
  589. configNames.emplace_back();
  590. }
  591. // Add our targets to the manifest for each configuration.
  592. const std::vector<cmGeneratorTarget*>& targets = this->GetGeneratorTargets();
  593. for (cmGeneratorTarget* target : targets) {
  594. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  595. continue;
  596. }
  597. for (std::string const& c : configNames) {
  598. target->ComputeTargetManifest(c);
  599. }
  600. }
  601. }
  602. bool cmLocalGenerator::ComputeTargetCompileFeatures()
  603. {
  604. // Collect the set of configuration types.
  605. std::vector<std::string> configNames;
  606. this->Makefile->GetConfigurations(configNames);
  607. if (configNames.empty()) {
  608. configNames.emplace_back();
  609. }
  610. // Process compile features of all targets.
  611. const std::vector<cmGeneratorTarget*>& targets = this->GetGeneratorTargets();
  612. for (cmGeneratorTarget* target : targets) {
  613. for (std::string const& c : configNames) {
  614. if (!target->ComputeCompileFeatures(c)) {
  615. return false;
  616. }
  617. }
  618. }
  619. return true;
  620. }
  621. bool cmLocalGenerator::IsRootMakefile() const
  622. {
  623. return !this->StateSnapshot.GetBuildsystemDirectoryParent().IsValid();
  624. }
  625. cmState* cmLocalGenerator::GetState() const
  626. {
  627. return this->GlobalGenerator->GetCMakeInstance()->GetState();
  628. }
  629. cmStateSnapshot cmLocalGenerator::GetStateSnapshot() const
  630. {
  631. return this->Makefile->GetStateSnapshot();
  632. }
  633. const char* cmLocalGenerator::GetRuleLauncher(cmGeneratorTarget* target,
  634. const std::string& prop)
  635. {
  636. if (target) {
  637. return target->GetProperty(prop);
  638. }
  639. return this->Makefile->GetProperty(prop);
  640. }
  641. std::string cmLocalGenerator::ConvertToIncludeReference(
  642. std::string const& path, OutputFormat format, bool forceFullPaths)
  643. {
  644. static_cast<void>(forceFullPaths);
  645. return this->ConvertToOutputForExisting(path, format);
  646. }
  647. std::string cmLocalGenerator::GetIncludeFlags(
  648. const std::vector<std::string>& includeDirs, cmGeneratorTarget* target,
  649. const std::string& lang, bool forceFullPaths, bool forResponseFile,
  650. const std::string& config)
  651. {
  652. if (lang.empty()) {
  653. return "";
  654. }
  655. std::vector<std::string> includes = includeDirs;
  656. MoveSystemIncludesToEnd(includes, config, lang, target);
  657. OutputFormat shellFormat = forResponseFile ? RESPONSE : SHELL;
  658. std::ostringstream includeFlags;
  659. std::string flagVar = "CMAKE_INCLUDE_FLAG_";
  660. flagVar += lang;
  661. std::string const& includeFlag = this->Makefile->GetSafeDefinition(flagVar);
  662. flagVar = "CMAKE_INCLUDE_FLAG_SEP_";
  663. flagVar += lang;
  664. const char* sep = this->Makefile->GetDefinition(flagVar);
  665. bool quotePaths = false;
  666. if (this->Makefile->GetDefinition("CMAKE_QUOTE_INCLUDE_PATHS")) {
  667. quotePaths = true;
  668. }
  669. bool repeatFlag = true;
  670. // should the include flag be repeated like ie. -IA -IB
  671. if (!sep) {
  672. sep = " ";
  673. } else {
  674. // if there is a separator then the flag is not repeated but is only
  675. // given once i.e. -classpath a:b:c
  676. repeatFlag = false;
  677. }
  678. // Support special system include flag if it is available and the
  679. // normal flag is repeated for each directory.
  680. std::string sysFlagVar = "CMAKE_INCLUDE_SYSTEM_FLAG_";
  681. sysFlagVar += lang;
  682. const char* sysIncludeFlag = nullptr;
  683. if (repeatFlag) {
  684. sysIncludeFlag = this->Makefile->GetDefinition(sysFlagVar);
  685. }
  686. std::string fwSearchFlagVar = "CMAKE_";
  687. fwSearchFlagVar += lang;
  688. fwSearchFlagVar += "_FRAMEWORK_SEARCH_FLAG";
  689. const char* fwSearchFlag = this->Makefile->GetDefinition(fwSearchFlagVar);
  690. std::string sysFwSearchFlagVar = "CMAKE_";
  691. sysFwSearchFlagVar += lang;
  692. sysFwSearchFlagVar += "_SYSTEM_FRAMEWORK_SEARCH_FLAG";
  693. const char* sysFwSearchFlag =
  694. this->Makefile->GetDefinition(sysFwSearchFlagVar);
  695. bool flagUsed = false;
  696. std::set<std::string> emitted;
  697. #ifdef __APPLE__
  698. emitted.insert("/System/Library/Frameworks");
  699. #endif
  700. for (std::string const& i : includes) {
  701. if (fwSearchFlag && *fwSearchFlag && this->Makefile->IsOn("APPLE") &&
  702. cmSystemTools::IsPathToFramework(i.c_str())) {
  703. std::string frameworkDir = i;
  704. frameworkDir += "/../";
  705. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  706. if (emitted.insert(frameworkDir).second) {
  707. if (sysFwSearchFlag && target &&
  708. target->IsSystemIncludeDirectory(i, config, lang)) {
  709. includeFlags << sysFwSearchFlag;
  710. } else {
  711. includeFlags << fwSearchFlag;
  712. }
  713. includeFlags << this->ConvertToOutputFormat(frameworkDir, shellFormat)
  714. << " ";
  715. }
  716. continue;
  717. }
  718. if (!flagUsed || repeatFlag) {
  719. if (sysIncludeFlag && target &&
  720. target->IsSystemIncludeDirectory(i, config, lang)) {
  721. includeFlags << sysIncludeFlag;
  722. } else {
  723. includeFlags << includeFlag;
  724. }
  725. flagUsed = true;
  726. }
  727. std::string includePath =
  728. this->ConvertToIncludeReference(i, shellFormat, forceFullPaths);
  729. if (quotePaths && !includePath.empty() && includePath.front() != '\"') {
  730. includeFlags << "\"";
  731. }
  732. includeFlags << includePath;
  733. if (quotePaths && !includePath.empty() && includePath.front() != '\"') {
  734. includeFlags << "\"";
  735. }
  736. includeFlags << sep;
  737. }
  738. std::string flags = includeFlags.str();
  739. // remove trailing separators
  740. if ((sep[0] != ' ') && !flags.empty() && flags.back() == sep[0]) {
  741. flags.back() = ' ';
  742. }
  743. return flags;
  744. }
  745. void cmLocalGenerator::AddCompileOptions(std::string& flags,
  746. cmGeneratorTarget* target,
  747. const std::string& lang,
  748. const std::string& config)
  749. {
  750. std::string langFlagRegexVar = std::string("CMAKE_") + lang + "_FLAG_REGEX";
  751. if (const char* langFlagRegexStr =
  752. this->Makefile->GetDefinition(langFlagRegexVar)) {
  753. // Filter flags acceptable to this language.
  754. std::vector<std::string> opts;
  755. if (const char* targetFlags = target->GetProperty("COMPILE_FLAGS")) {
  756. cmSystemTools::ParseWindowsCommandLine(targetFlags, opts);
  757. }
  758. target->GetCompileOptions(opts, config, lang);
  759. // (Re-)Escape these flags. COMPILE_FLAGS were already parsed
  760. // as a command line above, and COMPILE_OPTIONS are escaped.
  761. this->AppendCompileOptions(flags, opts, langFlagRegexStr);
  762. } else {
  763. // Use all flags.
  764. if (const char* targetFlags = target->GetProperty("COMPILE_FLAGS")) {
  765. // COMPILE_FLAGS are not escaped for historical reasons.
  766. this->AppendFlags(flags, targetFlags);
  767. }
  768. std::vector<std::string> opts;
  769. target->GetCompileOptions(opts, config, lang);
  770. // COMPILE_OPTIONS are escaped.
  771. this->AppendCompileOptions(flags, opts);
  772. }
  773. for (auto const& it : target->GetMaxLanguageStandards()) {
  774. const char* standard = target->GetProperty(it.first + "_STANDARD");
  775. if (!standard) {
  776. continue;
  777. }
  778. if (this->Makefile->IsLaterStandard(it.first, standard, it.second)) {
  779. std::ostringstream e;
  780. e << "The COMPILE_FEATURES property of target \"" << target->GetName()
  781. << "\" was evaluated when computing the link "
  782. "implementation, and the \""
  783. << it.first << "_STANDARD\" was \"" << it.second
  784. << "\" for that computation. Computing the "
  785. "COMPILE_FEATURES based on the link implementation resulted in a "
  786. "higher \""
  787. << it.first << "_STANDARD\" \"" << standard
  788. << "\". "
  789. "This is not permitted. The COMPILE_FEATURES may not both depend "
  790. "on "
  791. "and be depended on by the link implementation."
  792. << std::endl;
  793. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  794. return;
  795. }
  796. }
  797. this->AddCompilerRequirementFlag(flags, target, lang);
  798. }
  799. void cmLocalGenerator::GetIncludeDirectories(std::vector<std::string>& dirs,
  800. cmGeneratorTarget const* target,
  801. const std::string& lang,
  802. const std::string& config,
  803. bool stripImplicitDirs,
  804. bool appendAllImplicitDirs) const
  805. {
  806. std::vector<BT<std::string>> tmp = this->GetIncludeDirectories(
  807. target, lang, config, stripImplicitDirs, appendAllImplicitDirs);
  808. dirs.reserve(tmp.size());
  809. for (BT<std::string>& v : tmp) {
  810. dirs.emplace_back(std::move(v.Value));
  811. }
  812. }
  813. std::vector<BT<std::string>> cmLocalGenerator::GetIncludeDirectories(
  814. cmGeneratorTarget const* target, std::string const& lang,
  815. std::string const& config, bool stripImplicitDirs,
  816. bool appendAllImplicitDirs) const
  817. {
  818. std::vector<BT<std::string>> result;
  819. // Do not repeat an include path.
  820. std::set<std::string> emitted;
  821. // When automatic include directories are requested for a build then
  822. // include the source and binary directories at the beginning of the
  823. // include path to approximate include file behavior for an
  824. // in-source build. This does not account for the case of a source
  825. // file in a subdirectory of the current source directory but we
  826. // cannot fix this because not all native build tools support
  827. // per-source-file include paths.
  828. if (this->Makefile->IsOn("CMAKE_INCLUDE_CURRENT_DIR")) {
  829. // Current binary directory
  830. {
  831. std::string binDir =
  832. this->StateSnapshot.GetDirectory().GetCurrentBinary();
  833. if (emitted.insert(binDir).second) {
  834. result.emplace_back(std::move(binDir));
  835. }
  836. }
  837. // Current source directory
  838. {
  839. std::string srcDir =
  840. this->StateSnapshot.GetDirectory().GetCurrentSource();
  841. if (emitted.insert(srcDir).second) {
  842. result.emplace_back(std::move(srcDir));
  843. }
  844. }
  845. }
  846. if (!target) {
  847. return result;
  848. }
  849. // Implicit include directories
  850. std::vector<std::string> implicitDirs;
  851. {
  852. std::string rootPath;
  853. if (const char* sysrootCompile =
  854. this->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE")) {
  855. rootPath = sysrootCompile;
  856. } else {
  857. rootPath = this->Makefile->GetSafeDefinition("CMAKE_SYSROOT");
  858. }
  859. // Load implicit include directories for this language.
  860. std::string key = "CMAKE_";
  861. key += lang;
  862. key += "_IMPLICIT_INCLUDE_DIRECTORIES";
  863. if (const char* value = this->Makefile->GetDefinition(key)) {
  864. std::vector<std::string> impDirVec;
  865. cmSystemTools::ExpandListArgument(value, impDirVec);
  866. for (std::string const& i : impDirVec) {
  867. {
  868. std::string d = rootPath + i;
  869. cmSystemTools::ConvertToUnixSlashes(d);
  870. emitted.insert(std::move(d));
  871. }
  872. implicitDirs.push_back(i);
  873. }
  874. }
  875. }
  876. // Get the target-specific include directories.
  877. std::vector<BT<std::string>> userDirs =
  878. target->GetIncludeDirectories(config, lang);
  879. // Support putting all the in-project include directories first if
  880. // it is requested by the project.
  881. if (this->Makefile->IsOn("CMAKE_INCLUDE_DIRECTORIES_PROJECT_BEFORE")) {
  882. std::string const &topSourceDir = this->GetState()->GetSourceDirectory(),
  883. &topBinaryDir = this->GetState()->GetBinaryDirectory();
  884. for (BT<std::string> const& i : userDirs) {
  885. // Emit this directory only if it is a subdirectory of the
  886. // top-level source or binary tree.
  887. if (cmSystemTools::ComparePath(i.Value, topSourceDir) ||
  888. cmSystemTools::ComparePath(i.Value, topBinaryDir) ||
  889. cmSystemTools::IsSubDirectory(i.Value, topSourceDir) ||
  890. cmSystemTools::IsSubDirectory(i.Value, topBinaryDir)) {
  891. if (emitted.insert(i.Value).second) {
  892. result.push_back(i);
  893. }
  894. }
  895. }
  896. }
  897. // Construct the final ordered include directory list.
  898. for (BT<std::string> const& i : userDirs) {
  899. if (emitted.insert(i.Value).second) {
  900. result.push_back(i);
  901. }
  902. }
  903. MoveSystemIncludesToEnd(result, config, lang, target);
  904. // Add standard include directories for this language.
  905. {
  906. std::vector<std::string> userStandardDirs;
  907. {
  908. std::string key = "CMAKE_";
  909. key += lang;
  910. key += "_STANDARD_INCLUDE_DIRECTORIES";
  911. std::string const value = this->Makefile->GetSafeDefinition(key);
  912. cmSystemTools::ExpandListArgument(value, userStandardDirs);
  913. }
  914. userDirs.reserve(userDirs.size() + userStandardDirs.size());
  915. for (std::string& d : userStandardDirs) {
  916. cmSystemTools::ConvertToUnixSlashes(d);
  917. result.emplace_back(d);
  918. userDirs.emplace_back(std::move(d));
  919. }
  920. }
  921. if (!stripImplicitDirs) {
  922. // Append only implicit directories that were requested by the user
  923. for (std::string const& i : implicitDirs) {
  924. if (std::find(userDirs.begin(), userDirs.end(), i) != userDirs.end()) {
  925. result.emplace_back(i);
  926. }
  927. }
  928. // Append remaining implicit directories on demand
  929. if (appendAllImplicitDirs) {
  930. for (std::string const& i : implicitDirs) {
  931. if (std::find(result.begin(), result.end(), i) == result.end()) {
  932. result.emplace_back(i);
  933. }
  934. }
  935. }
  936. }
  937. return result;
  938. }
  939. void cmLocalGenerator::GetStaticLibraryFlags(std::string& flags,
  940. std::string const& config,
  941. std::string const& linkLanguage,
  942. cmGeneratorTarget* target)
  943. {
  944. this->AppendFlags(
  945. flags, this->Makefile->GetSafeDefinition("CMAKE_STATIC_LINKER_FLAGS"));
  946. if (!config.empty()) {
  947. std::string name = "CMAKE_STATIC_LINKER_FLAGS_" + config;
  948. this->AppendFlags(flags, this->Makefile->GetSafeDefinition(name));
  949. }
  950. this->AppendFlags(flags, target->GetProperty("STATIC_LIBRARY_FLAGS"));
  951. if (!config.empty()) {
  952. std::string name = "STATIC_LIBRARY_FLAGS_" + config;
  953. this->AppendFlags(flags, target->GetProperty(name));
  954. }
  955. std::vector<std::string> options;
  956. target->GetStaticLibraryLinkOptions(options, config, linkLanguage);
  957. // STATIC_LIBRARY_OPTIONS are escaped.
  958. this->AppendCompileOptions(flags, options);
  959. }
  960. void cmLocalGenerator::GetTargetFlags(
  961. cmLinkLineComputer* linkLineComputer, const std::string& config,
  962. std::string& linkLibs, std::string& flags, std::string& linkFlags,
  963. std::string& frameworkPath, std::string& linkPath, cmGeneratorTarget* target)
  964. {
  965. const std::string buildType = cmSystemTools::UpperCase(config);
  966. cmComputeLinkInformation* pcli = target->GetLinkInformation(config);
  967. const char* libraryLinkVariable =
  968. "CMAKE_SHARED_LINKER_FLAGS"; // default to shared library
  969. const std::string linkLanguage =
  970. linkLineComputer->GetLinkerLanguage(target, buildType);
  971. switch (target->GetType()) {
  972. case cmStateEnums::STATIC_LIBRARY:
  973. this->GetStaticLibraryFlags(linkFlags, buildType, linkLanguage, target);
  974. break;
  975. case cmStateEnums::MODULE_LIBRARY:
  976. libraryLinkVariable = "CMAKE_MODULE_LINKER_FLAGS";
  977. CM_FALLTHROUGH;
  978. case cmStateEnums::SHARED_LIBRARY: {
  979. linkFlags = this->Makefile->GetSafeDefinition(libraryLinkVariable);
  980. linkFlags += " ";
  981. if (!buildType.empty()) {
  982. std::string build = libraryLinkVariable;
  983. build += "_";
  984. build += buildType;
  985. linkFlags += this->Makefile->GetSafeDefinition(build);
  986. linkFlags += " ";
  987. }
  988. if (this->Makefile->IsOn("WIN32") &&
  989. !(this->Makefile->IsOn("CYGWIN") || this->Makefile->IsOn("MINGW"))) {
  990. std::vector<cmSourceFile*> sources;
  991. target->GetSourceFiles(sources, buildType);
  992. std::string defFlag =
  993. this->Makefile->GetSafeDefinition("CMAKE_LINK_DEF_FILE_FLAG");
  994. for (cmSourceFile* sf : sources) {
  995. if (sf->GetExtension() == "def") {
  996. linkFlags += defFlag;
  997. linkFlags += this->ConvertToOutputFormat(
  998. cmSystemTools::CollapseFullPath(sf->GetFullPath()), SHELL);
  999. linkFlags += " ";
  1000. }
  1001. }
  1002. }
  1003. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1004. if (targetLinkFlags) {
  1005. linkFlags += targetLinkFlags;
  1006. linkFlags += " ";
  1007. }
  1008. if (!buildType.empty()) {
  1009. std::string configLinkFlags = "LINK_FLAGS_";
  1010. configLinkFlags += buildType;
  1011. targetLinkFlags = target->GetProperty(configLinkFlags);
  1012. if (targetLinkFlags) {
  1013. linkFlags += targetLinkFlags;
  1014. linkFlags += " ";
  1015. }
  1016. }
  1017. std::vector<std::string> opts;
  1018. target->GetLinkOptions(opts, config, linkLanguage);
  1019. // LINK_OPTIONS are escaped.
  1020. this->AppendCompileOptions(linkFlags, opts);
  1021. if (pcli) {
  1022. this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
  1023. frameworkPath, linkPath);
  1024. }
  1025. } break;
  1026. case cmStateEnums::EXECUTABLE: {
  1027. linkFlags += this->Makefile->GetSafeDefinition("CMAKE_EXE_LINKER_FLAGS");
  1028. linkFlags += " ";
  1029. if (!buildType.empty()) {
  1030. std::string build = "CMAKE_EXE_LINKER_FLAGS_";
  1031. build += buildType;
  1032. linkFlags += this->Makefile->GetSafeDefinition(build);
  1033. linkFlags += " ";
  1034. }
  1035. if (linkLanguage.empty()) {
  1036. cmSystemTools::Error(
  1037. "CMake can not determine linker language for target: ",
  1038. target->GetName().c_str());
  1039. return;
  1040. }
  1041. this->AddLanguageFlagsForLinking(flags, target, linkLanguage, buildType);
  1042. if (pcli) {
  1043. this->OutputLinkLibraries(pcli, linkLineComputer, linkLibs,
  1044. frameworkPath, linkPath);
  1045. }
  1046. if (cmSystemTools::IsOn(
  1047. this->Makefile->GetDefinition("BUILD_SHARED_LIBS"))) {
  1048. std::string sFlagVar = std::string("CMAKE_SHARED_BUILD_") +
  1049. linkLanguage + std::string("_FLAGS");
  1050. linkFlags += this->Makefile->GetSafeDefinition(sFlagVar);
  1051. linkFlags += " ";
  1052. }
  1053. if (target->GetPropertyAsBool("WIN32_EXECUTABLE")) {
  1054. linkFlags +=
  1055. this->Makefile->GetSafeDefinition("CMAKE_CREATE_WIN32_EXE");
  1056. linkFlags += " ";
  1057. } else {
  1058. linkFlags +=
  1059. this->Makefile->GetSafeDefinition("CMAKE_CREATE_CONSOLE_EXE");
  1060. linkFlags += " ";
  1061. }
  1062. if (target->IsExecutableWithExports()) {
  1063. std::string exportFlagVar = "CMAKE_EXE_EXPORTS_";
  1064. exportFlagVar += linkLanguage;
  1065. exportFlagVar += "_FLAG";
  1066. linkFlags += this->Makefile->GetSafeDefinition(exportFlagVar);
  1067. linkFlags += " ";
  1068. }
  1069. std::string cmp0065Flags =
  1070. this->GetLinkLibsCMP0065(linkLanguage, *target);
  1071. if (!cmp0065Flags.empty()) {
  1072. linkFlags += cmp0065Flags;
  1073. linkFlags += " ";
  1074. }
  1075. const char* targetLinkFlags = target->GetProperty("LINK_FLAGS");
  1076. if (targetLinkFlags) {
  1077. linkFlags += targetLinkFlags;
  1078. linkFlags += " ";
  1079. }
  1080. if (!buildType.empty()) {
  1081. std::string configLinkFlags = "LINK_FLAGS_";
  1082. configLinkFlags += buildType;
  1083. targetLinkFlags = target->GetProperty(configLinkFlags);
  1084. if (targetLinkFlags) {
  1085. linkFlags += targetLinkFlags;
  1086. linkFlags += " ";
  1087. }
  1088. }
  1089. std::vector<std::string> opts;
  1090. target->GetLinkOptions(opts, config, linkLanguage);
  1091. // LINK_OPTIONS are escaped.
  1092. this->AppendCompileOptions(linkFlags, opts);
  1093. } break;
  1094. default:
  1095. break;
  1096. }
  1097. this->AppendIPOLinkerFlags(linkFlags, target, config, linkLanguage);
  1098. }
  1099. void cmLocalGenerator::GetTargetCompileFlags(cmGeneratorTarget* target,
  1100. std::string const& config,
  1101. std::string const& lang,
  1102. std::string& flags)
  1103. {
  1104. cmMakefile* mf = this->GetMakefile();
  1105. // Add language-specific flags.
  1106. this->AddLanguageFlags(flags, target, lang, config);
  1107. this->AddArchitectureFlags(flags, target, lang, config);
  1108. if (lang == "Fortran") {
  1109. this->AppendFlags(flags, this->GetTargetFortranFlags(target, config));
  1110. }
  1111. this->AddCMP0018Flags(flags, target, lang, config);
  1112. this->AddVisibilityPresetFlags(flags, target, lang);
  1113. this->AppendFlags(flags, mf->GetDefineFlags());
  1114. this->AppendFlags(flags, this->GetFrameworkFlags(lang, config, target));
  1115. this->AddCompileOptions(flags, target, lang, config);
  1116. }
  1117. static std::string GetFrameworkFlags(const std::string& lang,
  1118. const std::string& config,
  1119. cmGeneratorTarget* target)
  1120. {
  1121. cmLocalGenerator* lg = target->GetLocalGenerator();
  1122. cmMakefile* mf = lg->GetMakefile();
  1123. if (!mf->IsOn("APPLE")) {
  1124. return std::string();
  1125. }
  1126. std::string fwSearchFlagVar = "CMAKE_" + lang + "_FRAMEWORK_SEARCH_FLAG";
  1127. const char* fwSearchFlag = mf->GetDefinition(fwSearchFlagVar);
  1128. if (!(fwSearchFlag && *fwSearchFlag)) {
  1129. return std::string();
  1130. }
  1131. std::set<std::string> emitted;
  1132. #ifdef __APPLE__ /* don't insert this when crosscompiling e.g. to iphone */
  1133. emitted.insert("/System/Library/Frameworks");
  1134. #endif
  1135. std::vector<std::string> includes;
  1136. lg->GetIncludeDirectories(includes, target, "C", config);
  1137. // check all include directories for frameworks as this
  1138. // will already have added a -F for the framework
  1139. for (std::string const& include : includes) {
  1140. if (lg->GetGlobalGenerator()->NameResolvesToFramework(include)) {
  1141. std::string frameworkDir = include;
  1142. frameworkDir += "/../";
  1143. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1144. emitted.insert(frameworkDir);
  1145. }
  1146. }
  1147. std::string flags;
  1148. if (cmComputeLinkInformation* cli = target->GetLinkInformation(config)) {
  1149. std::vector<std::string> const& frameworks = cli->GetFrameworkPaths();
  1150. for (std::string const& framework : frameworks) {
  1151. if (emitted.insert(framework).second) {
  1152. flags += fwSearchFlag;
  1153. flags +=
  1154. lg->ConvertToOutputFormat(framework, cmOutputConverter::SHELL);
  1155. flags += " ";
  1156. }
  1157. }
  1158. }
  1159. return flags;
  1160. }
  1161. std::string cmLocalGenerator::GetFrameworkFlags(std::string const& l,
  1162. std::string const& config,
  1163. cmGeneratorTarget* target)
  1164. {
  1165. return ::GetFrameworkFlags(l, config, target);
  1166. }
  1167. void cmLocalGenerator::GetTargetDefines(cmGeneratorTarget const* target,
  1168. std::string const& config,
  1169. std::string const& lang,
  1170. std::set<std::string>& defines) const
  1171. {
  1172. std::set<BT<std::string>> tmp = this->GetTargetDefines(target, config, lang);
  1173. for (BT<std::string> const& v : tmp) {
  1174. defines.emplace(v.Value);
  1175. }
  1176. }
  1177. std::set<BT<std::string>> cmLocalGenerator::GetTargetDefines(
  1178. cmGeneratorTarget const* target, std::string const& config,
  1179. std::string const& lang) const
  1180. {
  1181. std::set<BT<std::string>> defines;
  1182. // Add the export symbol definition for shared library objects.
  1183. if (const std::string* exportMacro = target->GetExportMacro()) {
  1184. this->AppendDefines(defines, *exportMacro);
  1185. }
  1186. // Add preprocessor definitions for this target and configuration.
  1187. std::vector<BT<std::string>> targetDefines =
  1188. target->GetCompileDefinitions(config, lang);
  1189. this->AppendDefines(defines, targetDefines);
  1190. return defines;
  1191. }
  1192. std::string cmLocalGenerator::GetTargetFortranFlags(
  1193. cmGeneratorTarget const* /*unused*/, std::string const& /*unused*/)
  1194. {
  1195. // Implemented by specific generators that override this.
  1196. return std::string();
  1197. }
  1198. /**
  1199. * Output the linking rules on a command line. For executables,
  1200. * targetLibrary should be a NULL pointer. For libraries, it should point
  1201. * to the name of the library. This will not link a library against itself.
  1202. */
  1203. void cmLocalGenerator::OutputLinkLibraries(
  1204. cmComputeLinkInformation* pcli, cmLinkLineComputer* linkLineComputer,
  1205. std::string& linkLibraries, std::string& frameworkPath,
  1206. std::string& linkPath)
  1207. {
  1208. cmComputeLinkInformation& cli = *pcli;
  1209. std::string linkLanguage = cli.GetLinkLanguage();
  1210. std::string libPathFlag =
  1211. this->Makefile->GetRequiredDefinition("CMAKE_LIBRARY_PATH_FLAG");
  1212. std::string libPathTerminator =
  1213. this->Makefile->GetSafeDefinition("CMAKE_LIBRARY_PATH_TERMINATOR");
  1214. // Add standard libraries for this language.
  1215. std::string standardLibsVar = "CMAKE_";
  1216. standardLibsVar += cli.GetLinkLanguage();
  1217. standardLibsVar += "_STANDARD_LIBRARIES";
  1218. std::string stdLibString;
  1219. if (const char* stdLibs = this->Makefile->GetDefinition(standardLibsVar)) {
  1220. stdLibString = stdLibs;
  1221. }
  1222. // Append the framework search path flags.
  1223. std::string fwSearchFlagVar = "CMAKE_";
  1224. fwSearchFlagVar += linkLanguage;
  1225. fwSearchFlagVar += "_FRAMEWORK_SEARCH_FLAG";
  1226. std::string fwSearchFlag =
  1227. this->Makefile->GetSafeDefinition(fwSearchFlagVar);
  1228. frameworkPath = linkLineComputer->ComputeFrameworkPath(cli, fwSearchFlag);
  1229. linkPath =
  1230. linkLineComputer->ComputeLinkPath(cli, libPathFlag, libPathTerminator);
  1231. linkLibraries = linkLineComputer->ComputeLinkLibraries(cli, stdLibString);
  1232. }
  1233. std::string cmLocalGenerator::GetLinkLibsCMP0065(
  1234. std::string const& linkLanguage, cmGeneratorTarget& tgt) const
  1235. {
  1236. std::string linkFlags;
  1237. // Flags to link an executable to shared libraries.
  1238. if (tgt.GetType() == cmStateEnums::EXECUTABLE &&
  1239. this->StateSnapshot.GetState()->GetGlobalPropertyAsBool(
  1240. "TARGET_SUPPORTS_SHARED_LIBS")) {
  1241. bool add_shlib_flags = false;
  1242. switch (tgt.GetPolicyStatusCMP0065()) {
  1243. case cmPolicies::WARN:
  1244. if (!tgt.GetPropertyAsBool("ENABLE_EXPORTS") &&
  1245. this->Makefile->PolicyOptionalWarningEnabled(
  1246. "CMAKE_POLICY_WARNING_CMP0065")) {
  1247. std::ostringstream w;
  1248. /* clang-format off */
  1249. w << cmPolicies::GetPolicyWarning(cmPolicies::CMP0065) << "\n"
  1250. "For compatibility with older versions of CMake, "
  1251. "additional flags may be added to export symbols on all "
  1252. "executables regardless of their ENABLE_EXPORTS property.";
  1253. /* clang-format on */
  1254. this->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  1255. }
  1256. CM_FALLTHROUGH;
  1257. case cmPolicies::OLD:
  1258. // OLD behavior is to always add the flags
  1259. add_shlib_flags = true;
  1260. break;
  1261. case cmPolicies::REQUIRED_IF_USED:
  1262. case cmPolicies::REQUIRED_ALWAYS:
  1263. this->IssueMessage(
  1264. cmake::FATAL_ERROR,
  1265. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0065));
  1266. CM_FALLTHROUGH;
  1267. case cmPolicies::NEW:
  1268. // NEW behavior is to only add the flags if ENABLE_EXPORTS is on
  1269. add_shlib_flags = tgt.GetPropertyAsBool("ENABLE_EXPORTS");
  1270. break;
  1271. }
  1272. if (add_shlib_flags) {
  1273. std::string linkFlagsVar = "CMAKE_SHARED_LIBRARY_LINK_";
  1274. linkFlagsVar += linkLanguage;
  1275. linkFlagsVar += "_FLAGS";
  1276. linkFlags = this->Makefile->GetSafeDefinition(linkFlagsVar);
  1277. }
  1278. }
  1279. return linkFlags;
  1280. }
  1281. void cmLocalGenerator::AddArchitectureFlags(std::string& flags,
  1282. cmGeneratorTarget const* target,
  1283. const std::string& lang,
  1284. const std::string& config)
  1285. {
  1286. // Only add macOS specific flags on Darwin platforms (macOS and iOS):
  1287. if (this->Makefile->IsOn("APPLE") && this->EmitUniversalBinaryFlags) {
  1288. std::vector<std::string> archs;
  1289. target->GetAppleArchs(config, archs);
  1290. const char* sysroot = this->Makefile->GetDefinition("CMAKE_OSX_SYSROOT");
  1291. if (sysroot && sysroot[0] == '/' && !sysroot[1]) {
  1292. sysroot = nullptr;
  1293. }
  1294. std::string sysrootFlagVar =
  1295. std::string("CMAKE_") + lang + "_SYSROOT_FLAG";
  1296. const char* sysrootFlag = this->Makefile->GetDefinition(sysrootFlagVar);
  1297. const char* deploymentTarget =
  1298. this->Makefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  1299. std::string deploymentTargetFlagVar =
  1300. std::string("CMAKE_") + lang + "_OSX_DEPLOYMENT_TARGET_FLAG";
  1301. const char* deploymentTargetFlag =
  1302. this->Makefile->GetDefinition(deploymentTargetFlagVar);
  1303. if (!archs.empty() && !lang.empty() &&
  1304. (lang[0] == 'C' || lang[0] == 'F')) {
  1305. for (std::string const& arch : archs) {
  1306. flags += " -arch ";
  1307. flags += arch;
  1308. }
  1309. }
  1310. if (sysrootFlag && *sysrootFlag && sysroot && *sysroot) {
  1311. flags += " ";
  1312. flags += sysrootFlag;
  1313. flags += " ";
  1314. flags += this->ConvertToOutputFormat(sysroot, SHELL);
  1315. }
  1316. if (deploymentTargetFlag && *deploymentTargetFlag && deploymentTarget &&
  1317. *deploymentTarget) {
  1318. flags += " ";
  1319. flags += deploymentTargetFlag;
  1320. flags += deploymentTarget;
  1321. }
  1322. }
  1323. }
  1324. void cmLocalGenerator::AddLanguageFlags(std::string& flags,
  1325. cmGeneratorTarget const* target,
  1326. const std::string& lang,
  1327. const std::string& config)
  1328. {
  1329. // Add language-specific flags.
  1330. std::string flagsVar = "CMAKE_";
  1331. flagsVar += lang;
  1332. flagsVar += "_FLAGS";
  1333. this->AddConfigVariableFlags(flags, flagsVar, config);
  1334. if (target->IsIPOEnabled(lang, config)) {
  1335. this->AppendFeatureOptions(flags, lang, "IPO");
  1336. }
  1337. }
  1338. void cmLocalGenerator::AddLanguageFlagsForLinking(
  1339. std::string& flags, cmGeneratorTarget const* target, const std::string& lang,
  1340. const std::string& config)
  1341. {
  1342. if (this->Makefile->IsOn("CMAKE_" + lang +
  1343. "_LINK_WITH_STANDARD_COMPILE_OPTION")) {
  1344. // This toolchain requires use of the language standard flag
  1345. // when linking in order to use the matching standard library.
  1346. // FIXME: If CMake gains an abstraction for standard library
  1347. // selection, this will have to be reconciled with it.
  1348. this->AddCompilerRequirementFlag(flags, target, lang);
  1349. }
  1350. this->AddLanguageFlags(flags, target, lang, config);
  1351. }
  1352. cmGeneratorTarget* cmLocalGenerator::FindGeneratorTargetToUse(
  1353. const std::string& name) const
  1354. {
  1355. GeneratorTargetMap::const_iterator imported =
  1356. this->ImportedGeneratorTargets.find(name);
  1357. if (imported != this->ImportedGeneratorTargets.end()) {
  1358. return imported->second;
  1359. }
  1360. if (cmGeneratorTarget* t = this->FindLocalNonAliasGeneratorTarget(name)) {
  1361. return t;
  1362. }
  1363. return this->GetGlobalGenerator()->FindGeneratorTarget(name);
  1364. }
  1365. bool cmLocalGenerator::GetRealDependency(const std::string& inName,
  1366. const std::string& config,
  1367. std::string& dep)
  1368. {
  1369. // Older CMake code may specify the dependency using the target
  1370. // output file rather than the target name. Such code would have
  1371. // been written before there was support for target properties that
  1372. // modify the name so stripping down to just the file name should
  1373. // produce the target name in this case.
  1374. std::string name = cmSystemTools::GetFilenameName(inName);
  1375. // If the input name is the empty string, there is no real
  1376. // dependency. Short-circuit the other checks:
  1377. if (name.empty()) {
  1378. return false;
  1379. }
  1380. if (cmSystemTools::GetFilenameLastExtension(name) == ".exe") {
  1381. name = cmSystemTools::GetFilenameWithoutLastExtension(name);
  1382. }
  1383. // Look for a CMake target with the given name.
  1384. if (cmGeneratorTarget* target = this->FindGeneratorTargetToUse(name)) {
  1385. // make sure it is not just a coincidence that the target name
  1386. // found is part of the inName
  1387. if (cmSystemTools::FileIsFullPath(inName)) {
  1388. std::string tLocation;
  1389. if (target->GetType() >= cmStateEnums::EXECUTABLE &&
  1390. target->GetType() <= cmStateEnums::MODULE_LIBRARY) {
  1391. tLocation = target->GetLocation(config);
  1392. tLocation = cmSystemTools::GetFilenamePath(tLocation);
  1393. tLocation = cmSystemTools::CollapseFullPath(tLocation);
  1394. }
  1395. std::string depLocation =
  1396. cmSystemTools::GetFilenamePath(std::string(inName));
  1397. depLocation = cmSystemTools::CollapseFullPath(depLocation);
  1398. if (depLocation != tLocation) {
  1399. // it is a full path to a depend that has the same name
  1400. // as a target but is in a different location so do not use
  1401. // the target as the depend
  1402. dep = inName;
  1403. return true;
  1404. }
  1405. }
  1406. switch (target->GetType()) {
  1407. case cmStateEnums::EXECUTABLE:
  1408. case cmStateEnums::STATIC_LIBRARY:
  1409. case cmStateEnums::SHARED_LIBRARY:
  1410. case cmStateEnums::MODULE_LIBRARY:
  1411. case cmStateEnums::UNKNOWN_LIBRARY:
  1412. dep = target->GetLocation(config);
  1413. return true;
  1414. case cmStateEnums::OBJECT_LIBRARY:
  1415. // An object library has no single file on which to depend.
  1416. // This was listed to get the target-level dependency.
  1417. return false;
  1418. case cmStateEnums::INTERFACE_LIBRARY:
  1419. // An interface library has no file on which to depend.
  1420. // This was listed to get the target-level dependency.
  1421. return false;
  1422. case cmStateEnums::UTILITY:
  1423. case cmStateEnums::GLOBAL_TARGET:
  1424. // A utility target has no file on which to depend. This was listed
  1425. // only to get the target-level dependency.
  1426. return false;
  1427. }
  1428. }
  1429. // The name was not that of a CMake target. It must name a file.
  1430. if (cmSystemTools::FileIsFullPath(inName)) {
  1431. // This is a full path. Return it as given.
  1432. dep = inName;
  1433. return true;
  1434. }
  1435. // Check for a source file in this directory that matches the
  1436. // dependency.
  1437. if (cmSourceFile* sf = this->Makefile->GetSource(inName)) {
  1438. dep = sf->GetFullPath();
  1439. return true;
  1440. }
  1441. // Treat the name as relative to the source directory in which it
  1442. // was given.
  1443. dep = this->StateSnapshot.GetDirectory().GetCurrentSource();
  1444. dep += "/";
  1445. dep += inName;
  1446. return true;
  1447. }
  1448. void cmLocalGenerator::AddSharedFlags(std::string& flags,
  1449. const std::string& lang, bool shared)
  1450. {
  1451. std::string flagsVar;
  1452. // Add flags for dealing with shared libraries for this language.
  1453. if (shared) {
  1454. flagsVar = "CMAKE_SHARED_LIBRARY_";
  1455. flagsVar += lang;
  1456. flagsVar += "_FLAGS";
  1457. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1458. }
  1459. }
  1460. void cmLocalGenerator::AddCompilerRequirementFlag(
  1461. std::string& flags, cmGeneratorTarget const* target, const std::string& lang)
  1462. {
  1463. if (lang.empty()) {
  1464. return;
  1465. }
  1466. const char* defaultStd =
  1467. this->Makefile->GetDefinition("CMAKE_" + lang + "_STANDARD_DEFAULT");
  1468. if (!defaultStd || !*defaultStd) {
  1469. // This compiler has no notion of language standard levels.
  1470. return;
  1471. }
  1472. std::string extProp = lang + "_EXTENSIONS";
  1473. bool ext = true;
  1474. if (const char* extPropValue = target->GetProperty(extProp)) {
  1475. if (cmSystemTools::IsOff(extPropValue)) {
  1476. ext = false;
  1477. }
  1478. }
  1479. std::string stdProp = lang + "_STANDARD";
  1480. const char* standardProp = target->GetProperty(stdProp);
  1481. if (!standardProp) {
  1482. if (ext) {
  1483. // No language standard is specified and extensions are not disabled.
  1484. // Check if this compiler needs a flag to enable extensions.
  1485. std::string const option_flag =
  1486. "CMAKE_" + lang + "_EXTENSION_COMPILE_OPTION";
  1487. if (const char* opt =
  1488. target->Target->GetMakefile()->GetDefinition(option_flag)) {
  1489. std::vector<std::string> optVec;
  1490. cmSystemTools::ExpandListArgument(opt, optVec);
  1491. for (std::string const& i : optVec) {
  1492. this->AppendFlagEscape(flags, i);
  1493. }
  1494. }
  1495. }
  1496. return;
  1497. }
  1498. std::string const type = ext ? "EXTENSION" : "STANDARD";
  1499. if (target->GetPropertyAsBool(lang + "_STANDARD_REQUIRED")) {
  1500. std::string option_flag =
  1501. "CMAKE_" + lang + standardProp + "_" + type + "_COMPILE_OPTION";
  1502. const char* opt =
  1503. target->Target->GetMakefile()->GetDefinition(option_flag);
  1504. if (!opt) {
  1505. std::ostringstream e;
  1506. e << "Target \"" << target->GetName()
  1507. << "\" requires the language "
  1508. "dialect \""
  1509. << lang << standardProp << "\" "
  1510. << (ext ? "(with compiler extensions)" : "")
  1511. << ", but CMake "
  1512. "does not know the compile flags to use to enable it.";
  1513. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  1514. } else {
  1515. std::vector<std::string> optVec;
  1516. cmSystemTools::ExpandListArgument(opt, optVec);
  1517. for (std::string const& i : optVec) {
  1518. this->AppendFlagEscape(flags, i);
  1519. }
  1520. }
  1521. return;
  1522. }
  1523. static std::map<std::string, std::vector<std::string>> langStdMap;
  1524. if (langStdMap.empty()) {
  1525. // Maintain sorted order, most recent first.
  1526. langStdMap["CXX"].push_back("20");
  1527. langStdMap["CXX"].push_back("17");
  1528. langStdMap["CXX"].push_back("14");
  1529. langStdMap["CXX"].push_back("11");
  1530. langStdMap["CXX"].push_back("98");
  1531. langStdMap["C"].push_back("11");
  1532. langStdMap["C"].push_back("99");
  1533. langStdMap["C"].push_back("90");
  1534. langStdMap["CUDA"].push_back("14");
  1535. langStdMap["CUDA"].push_back("11");
  1536. langStdMap["CUDA"].push_back("98");
  1537. }
  1538. std::string standard(standardProp);
  1539. std::vector<std::string>& stds = langStdMap[lang];
  1540. std::vector<std::string>::const_iterator stdIt =
  1541. std::find(stds.begin(), stds.end(), standard);
  1542. if (stdIt == stds.end()) {
  1543. std::string e =
  1544. lang + "_STANDARD is set to invalid value '" + standard + "'";
  1545. this->GetGlobalGenerator()->GetCMakeInstance()->IssueMessage(
  1546. cmake::FATAL_ERROR, e, target->GetBacktrace());
  1547. return;
  1548. }
  1549. std::vector<std::string>::const_iterator defaultStdIt =
  1550. std::find(stds.begin(), stds.end(), defaultStd);
  1551. if (defaultStdIt == stds.end()) {
  1552. std::string e = "CMAKE_" + lang +
  1553. "_STANDARD_DEFAULT is set to invalid value '" + std::string(defaultStd) +
  1554. "'";
  1555. this->IssueMessage(cmake::INTERNAL_ERROR, e);
  1556. return;
  1557. }
  1558. // If the standard requested is older than the compiler's default
  1559. // then we need to use a flag to change it. The comparison is
  1560. // greater-or-equal because the standards are stored in backward
  1561. // chronological order.
  1562. if (stdIt >= defaultStdIt) {
  1563. std::string option_flag =
  1564. "CMAKE_" + lang + *stdIt + "_" + type + "_COMPILE_OPTION";
  1565. std::string const& opt =
  1566. target->Target->GetMakefile()->GetRequiredDefinition(option_flag);
  1567. std::vector<std::string> optVec;
  1568. cmSystemTools::ExpandListArgument(opt, optVec);
  1569. for (std::string const& i : optVec) {
  1570. this->AppendFlagEscape(flags, i);
  1571. }
  1572. return;
  1573. }
  1574. // The standard requested is at least as new as the compiler's default,
  1575. // and the standard request is not required. Decay to the newest standard
  1576. // for which a flag is defined.
  1577. for (; stdIt < defaultStdIt; ++stdIt) {
  1578. std::string option_flag =
  1579. "CMAKE_" + lang + *stdIt + "_" + type + "_COMPILE_OPTION";
  1580. if (const char* opt =
  1581. target->Target->GetMakefile()->GetDefinition(option_flag)) {
  1582. std::vector<std::string> optVec;
  1583. cmSystemTools::ExpandListArgument(opt, optVec);
  1584. for (std::string const& i : optVec) {
  1585. this->AppendFlagEscape(flags, i);
  1586. }
  1587. return;
  1588. }
  1589. }
  1590. }
  1591. static void AddVisibilityCompileOption(std::string& flags,
  1592. cmGeneratorTarget const* target,
  1593. cmLocalGenerator* lg,
  1594. const std::string& lang,
  1595. std::string* warnCMP0063)
  1596. {
  1597. std::string compileOption = "CMAKE_" + lang + "_COMPILE_OPTIONS_VISIBILITY";
  1598. const char* opt = lg->GetMakefile()->GetDefinition(compileOption);
  1599. if (!opt) {
  1600. return;
  1601. }
  1602. std::string flagDefine = lang + "_VISIBILITY_PRESET";
  1603. const char* prop = target->GetProperty(flagDefine);
  1604. if (!prop) {
  1605. return;
  1606. }
  1607. if (warnCMP0063) {
  1608. *warnCMP0063 += " " + flagDefine + "\n";
  1609. return;
  1610. }
  1611. if (strcmp(prop, "hidden") != 0 && strcmp(prop, "default") != 0 &&
  1612. strcmp(prop, "protected") != 0 && strcmp(prop, "internal") != 0) {
  1613. std::ostringstream e;
  1614. e << "Target " << target->GetName() << " uses unsupported value \"" << prop
  1615. << "\" for " << flagDefine << ".";
  1616. cmSystemTools::Error(e.str().c_str());
  1617. return;
  1618. }
  1619. std::string option = std::string(opt) + prop;
  1620. lg->AppendFlags(flags, option);
  1621. }
  1622. static void AddInlineVisibilityCompileOption(std::string& flags,
  1623. cmGeneratorTarget const* target,
  1624. cmLocalGenerator* lg,
  1625. std::string* warnCMP0063)
  1626. {
  1627. std::string compileOption =
  1628. "CMAKE_CXX_COMPILE_OPTIONS_VISIBILITY_INLINES_HIDDEN";
  1629. const char* opt = lg->GetMakefile()->GetDefinition(compileOption);
  1630. if (!opt) {
  1631. return;
  1632. }
  1633. bool prop = target->GetPropertyAsBool("VISIBILITY_INLINES_HIDDEN");
  1634. if (!prop) {
  1635. return;
  1636. }
  1637. if (warnCMP0063) {
  1638. *warnCMP0063 += " VISIBILITY_INLINES_HIDDEN\n";
  1639. return;
  1640. }
  1641. lg->AppendFlags(flags, opt);
  1642. }
  1643. void cmLocalGenerator::AddVisibilityPresetFlags(
  1644. std::string& flags, cmGeneratorTarget const* target, const std::string& lang)
  1645. {
  1646. if (lang.empty()) {
  1647. return;
  1648. }
  1649. std::string warnCMP0063;
  1650. std::string* pWarnCMP0063 = nullptr;
  1651. if (target->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1652. target->GetType() != cmStateEnums::MODULE_LIBRARY &&
  1653. !target->IsExecutableWithExports()) {
  1654. switch (target->GetPolicyStatusCMP0063()) {
  1655. case cmPolicies::OLD:
  1656. return;
  1657. case cmPolicies::WARN:
  1658. pWarnCMP0063 = &warnCMP0063;
  1659. break;
  1660. default:
  1661. break;
  1662. }
  1663. }
  1664. AddVisibilityCompileOption(flags, target, this, lang, pWarnCMP0063);
  1665. if (lang == "CXX") {
  1666. AddInlineVisibilityCompileOption(flags, target, this, pWarnCMP0063);
  1667. }
  1668. if (!warnCMP0063.empty() && this->WarnCMP0063.insert(target).second) {
  1669. std::ostringstream w;
  1670. /* clang-format off */
  1671. w <<
  1672. cmPolicies::GetPolicyWarning(cmPolicies::CMP0063) << "\n"
  1673. "Target \"" << target->GetName() << "\" of "
  1674. "type \"" << cmState::GetTargetTypeName(target->GetType()) << "\" "
  1675. "has the following visibility properties set for " << lang << ":\n" <<
  1676. warnCMP0063 <<
  1677. "For compatibility CMake is not honoring them for this target.";
  1678. /* clang-format on */
  1679. target->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  1680. cmake::AUTHOR_WARNING, w.str(), target->GetBacktrace());
  1681. }
  1682. }
  1683. void cmLocalGenerator::AddCMP0018Flags(std::string& flags,
  1684. cmGeneratorTarget const* target,
  1685. std::string const& lang,
  1686. const std::string& config)
  1687. {
  1688. int targetType = target->GetType();
  1689. bool shared = ((targetType == cmStateEnums::SHARED_LIBRARY) ||
  1690. (targetType == cmStateEnums::MODULE_LIBRARY));
  1691. if (this->GetShouldUseOldFlags(shared, lang)) {
  1692. this->AddSharedFlags(flags, lang, shared);
  1693. } else {
  1694. if (target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  1695. if (target->GetPropertyAsBool("POSITION_INDEPENDENT_CODE")) {
  1696. this->AddPositionIndependentFlags(flags, lang, targetType);
  1697. }
  1698. return;
  1699. }
  1700. if (target->GetLinkInterfaceDependentBoolProperty(
  1701. "POSITION_INDEPENDENT_CODE", config)) {
  1702. this->AddPositionIndependentFlags(flags, lang, targetType);
  1703. }
  1704. if (shared) {
  1705. this->AppendFeatureOptions(flags, lang, "DLL");
  1706. }
  1707. }
  1708. }
  1709. bool cmLocalGenerator::GetShouldUseOldFlags(bool shared,
  1710. const std::string& lang) const
  1711. {
  1712. std::string originalFlags =
  1713. this->GlobalGenerator->GetSharedLibFlagsForLanguage(lang);
  1714. if (shared) {
  1715. std::string flagsVar = "CMAKE_SHARED_LIBRARY_";
  1716. flagsVar += lang;
  1717. flagsVar += "_FLAGS";
  1718. std::string const& flags = this->Makefile->GetSafeDefinition(flagsVar);
  1719. if (flags != originalFlags) {
  1720. switch (this->GetPolicyStatus(cmPolicies::CMP0018)) {
  1721. case cmPolicies::WARN: {
  1722. std::ostringstream e;
  1723. e << "Variable " << flagsVar
  1724. << " has been modified. CMake "
  1725. "will ignore the POSITION_INDEPENDENT_CODE target property for "
  1726. "shared libraries and will use the "
  1727. << flagsVar
  1728. << " variable "
  1729. "instead. This may cause errors if the original content of "
  1730. << flagsVar << " was removed.\n"
  1731. << cmPolicies::GetPolicyWarning(cmPolicies::CMP0018);
  1732. this->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  1733. CM_FALLTHROUGH;
  1734. }
  1735. case cmPolicies::OLD:
  1736. return true;
  1737. case cmPolicies::REQUIRED_IF_USED:
  1738. case cmPolicies::REQUIRED_ALWAYS:
  1739. case cmPolicies::NEW:
  1740. return false;
  1741. }
  1742. }
  1743. }
  1744. return false;
  1745. }
  1746. void cmLocalGenerator::AddPositionIndependentFlags(std::string& flags,
  1747. std::string const& lang,
  1748. int targetType)
  1749. {
  1750. std::string picFlags;
  1751. if (targetType == cmStateEnums::EXECUTABLE) {
  1752. std::string flagsVar = "CMAKE_";
  1753. flagsVar += lang;
  1754. flagsVar += "_COMPILE_OPTIONS_PIE";
  1755. picFlags = this->Makefile->GetSafeDefinition(flagsVar);
  1756. }
  1757. if (picFlags.empty()) {
  1758. std::string flagsVar = "CMAKE_";
  1759. flagsVar += lang;
  1760. flagsVar += "_COMPILE_OPTIONS_PIC";
  1761. picFlags = this->Makefile->GetSafeDefinition(flagsVar);
  1762. }
  1763. if (!picFlags.empty()) {
  1764. std::vector<std::string> options;
  1765. cmSystemTools::ExpandListArgument(picFlags, options);
  1766. for (std::string const& o : options) {
  1767. this->AppendFlagEscape(flags, o);
  1768. }
  1769. }
  1770. }
  1771. void cmLocalGenerator::AddConfigVariableFlags(std::string& flags,
  1772. const std::string& var,
  1773. const std::string& config)
  1774. {
  1775. // Add the flags from the variable itself.
  1776. std::string flagsVar = var;
  1777. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1778. // Add the flags from the build-type specific variable.
  1779. if (!config.empty()) {
  1780. flagsVar += "_";
  1781. flagsVar += cmSystemTools::UpperCase(config);
  1782. this->AppendFlags(flags, this->Makefile->GetDefinition(flagsVar));
  1783. }
  1784. }
  1785. void cmLocalGenerator::AppendFlags(std::string& flags,
  1786. const std::string& newFlags) const
  1787. {
  1788. if (!newFlags.empty()) {
  1789. if (!flags.empty()) {
  1790. flags += " ";
  1791. }
  1792. flags += newFlags;
  1793. }
  1794. }
  1795. void cmLocalGenerator::AppendFlags(std::string& flags,
  1796. const char* newFlags) const
  1797. {
  1798. if (newFlags && *newFlags) {
  1799. this->AppendFlags(flags, std::string(newFlags));
  1800. }
  1801. }
  1802. void cmLocalGenerator::AppendFlagEscape(std::string& flags,
  1803. const std::string& rawFlag) const
  1804. {
  1805. this->AppendFlags(flags, this->EscapeForShell(rawFlag));
  1806. }
  1807. void cmLocalGenerator::AppendIPOLinkerFlags(std::string& flags,
  1808. cmGeneratorTarget* target,
  1809. const std::string& config,
  1810. const std::string& lang)
  1811. {
  1812. if (!target->IsIPOEnabled(lang, config)) {
  1813. return;
  1814. }
  1815. switch (target->GetType()) {
  1816. case cmStateEnums::EXECUTABLE:
  1817. case cmStateEnums::SHARED_LIBRARY:
  1818. case cmStateEnums::MODULE_LIBRARY:
  1819. break;
  1820. default:
  1821. return;
  1822. }
  1823. const std::string name = "CMAKE_" + lang + "_LINK_OPTIONS_IPO";
  1824. const char* rawFlagsList = this->Makefile->GetDefinition(name);
  1825. if (rawFlagsList == nullptr) {
  1826. return;
  1827. }
  1828. std::vector<std::string> flagsList;
  1829. cmSystemTools::ExpandListArgument(rawFlagsList, flagsList);
  1830. for (std::string const& o : flagsList) {
  1831. this->AppendFlagEscape(flags, o);
  1832. }
  1833. }
  1834. void cmLocalGenerator::AppendCompileOptions(std::string& options,
  1835. const char* options_list,
  1836. const char* regex) const
  1837. {
  1838. // Short-circuit if there are no options.
  1839. if (!options_list) {
  1840. return;
  1841. }
  1842. // Expand the list of options.
  1843. std::vector<std::string> options_vec;
  1844. cmSystemTools::ExpandListArgument(options_list, options_vec);
  1845. this->AppendCompileOptions(options, options_vec, regex);
  1846. }
  1847. void cmLocalGenerator::AppendCompileOptions(
  1848. std::string& options, const std::vector<std::string>& options_vec,
  1849. const char* regex) const
  1850. {
  1851. if (regex != nullptr) {
  1852. // Filter flags upon specified reges.
  1853. cmsys::RegularExpression r(regex);
  1854. for (std::string const& opt : options_vec) {
  1855. if (r.find(opt)) {
  1856. this->AppendFlagEscape(options, opt);
  1857. }
  1858. }
  1859. } else {
  1860. for (std::string const& opt : options_vec) {
  1861. this->AppendFlagEscape(options, opt);
  1862. }
  1863. }
  1864. }
  1865. void cmLocalGenerator::AppendIncludeDirectories(
  1866. std::vector<std::string>& includes, const char* includes_list,
  1867. const cmSourceFile& sourceFile) const
  1868. {
  1869. // Short-circuit if there are no includes.
  1870. if (!includes_list) {
  1871. return;
  1872. }
  1873. // Expand the list of includes.
  1874. std::vector<std::string> includes_vec;
  1875. cmSystemTools::ExpandListArgument(includes_list, includes_vec);
  1876. this->AppendIncludeDirectories(includes, includes_vec, sourceFile);
  1877. }
  1878. void cmLocalGenerator::AppendIncludeDirectories(
  1879. std::vector<std::string>& includes,
  1880. const std::vector<std::string>& includes_vec,
  1881. const cmSourceFile& sourceFile) const
  1882. {
  1883. std::unordered_set<std::string> uniqueIncludes;
  1884. for (const std::string& include : includes_vec) {
  1885. if (!cmSystemTools::FileIsFullPath(include)) {
  1886. std::ostringstream e;
  1887. e << "Found relative path while evaluating include directories of "
  1888. "\""
  1889. << sourceFile.GetLocation().GetName() << "\":\n \"" << include
  1890. << "\"\n";
  1891. this->IssueMessage(cmake::FATAL_ERROR, e.str());
  1892. return;
  1893. }
  1894. std::string inc = include;
  1895. if (!cmSystemTools::IsOff(inc)) {
  1896. cmSystemTools::ConvertToUnixSlashes(inc);
  1897. }
  1898. if (uniqueIncludes.insert(inc).second) {
  1899. includes.push_back(std::move(inc));
  1900. }
  1901. }
  1902. }
  1903. void cmLocalGenerator::AppendDefines(std::set<std::string>& defines,
  1904. std::string const& defines_list) const
  1905. {
  1906. std::set<BT<std::string>> tmp;
  1907. this->AppendDefines(tmp, ExpandListWithBacktrace(defines_list));
  1908. for (BT<std::string> const& i : tmp) {
  1909. defines.emplace(i.Value);
  1910. }
  1911. }
  1912. void cmLocalGenerator::AppendDefines(std::set<BT<std::string>>& defines,
  1913. std::string const& defines_list) const
  1914. {
  1915. // Short-circuit if there are no definitions.
  1916. if (defines_list.empty()) {
  1917. return;
  1918. }
  1919. // Expand the list of definitions.
  1920. this->AppendDefines(defines, ExpandListWithBacktrace(defines_list));
  1921. }
  1922. void cmLocalGenerator::AppendDefines(
  1923. std::set<BT<std::string>>& defines,
  1924. const std::vector<BT<std::string>>& defines_vec) const
  1925. {
  1926. for (BT<std::string> const& d : defines_vec) {
  1927. // Skip unsupported definitions.
  1928. if (!this->CheckDefinition(d.Value)) {
  1929. continue;
  1930. }
  1931. defines.insert(d);
  1932. }
  1933. }
  1934. void cmLocalGenerator::JoinDefines(const std::set<std::string>& defines,
  1935. std::string& definesString,
  1936. const std::string& lang)
  1937. {
  1938. // Lookup the define flag for the current language.
  1939. std::string dflag = "-D";
  1940. if (!lang.empty()) {
  1941. std::string defineFlagVar = "CMAKE_";
  1942. defineFlagVar += lang;
  1943. defineFlagVar += "_DEFINE_FLAG";
  1944. const char* df = this->Makefile->GetDefinition(defineFlagVar);
  1945. if (df && *df) {
  1946. dflag = df;
  1947. }
  1948. }
  1949. std::set<std::string>::const_iterator defineIt = defines.begin();
  1950. const std::set<std::string>::const_iterator defineEnd = defines.end();
  1951. const char* itemSeparator = definesString.empty() ? "" : " ";
  1952. for (; defineIt != defineEnd; ++defineIt) {
  1953. // Append the definition with proper escaping.
  1954. std::string def = dflag;
  1955. if (this->GetState()->UseWatcomWMake()) {
  1956. // The Watcom compiler does its own command line parsing instead
  1957. // of using the windows shell rules. Definitions are one of
  1958. // -DNAME
  1959. // -DNAME=<cpp-token>
  1960. // -DNAME="c-string with spaces and other characters(?@#$)"
  1961. //
  1962. // Watcom will properly parse each of these cases from the
  1963. // command line without any escapes. However we still have to
  1964. // get the '$' and '#' characters through WMake as '$$' and
  1965. // '$#'.
  1966. for (const char* c = defineIt->c_str(); *c; ++c) {
  1967. if (*c == '$' || *c == '#') {
  1968. def += '$';
  1969. }
  1970. def += *c;
  1971. }
  1972. } else {
  1973. // Make the definition appear properly on the command line. Use
  1974. // -DNAME="value" instead of -D"NAME=value" for historical reasons.
  1975. std::string::size_type eq = defineIt->find("=");
  1976. def += defineIt->substr(0, eq);
  1977. if (eq != std::string::npos) {
  1978. def += "=";
  1979. def += this->EscapeForShell(defineIt->c_str() + eq + 1, true);
  1980. }
  1981. }
  1982. definesString += itemSeparator;
  1983. itemSeparator = " ";
  1984. definesString += def;
  1985. }
  1986. }
  1987. void cmLocalGenerator::AppendFeatureOptions(std::string& flags,
  1988. const std::string& lang,
  1989. const char* feature)
  1990. {
  1991. std::string optVar = "CMAKE_";
  1992. optVar += lang;
  1993. optVar += "_COMPILE_OPTIONS_";
  1994. optVar += feature;
  1995. if (const char* optionList = this->Makefile->GetDefinition(optVar)) {
  1996. std::vector<std::string> options;
  1997. cmSystemTools::ExpandListArgument(optionList, options);
  1998. for (std::string const& o : options) {
  1999. this->AppendFlagEscape(flags, o);
  2000. }
  2001. }
  2002. }
  2003. const char* cmLocalGenerator::GetFeature(const std::string& feature,
  2004. const std::string& config)
  2005. {
  2006. std::string featureName = feature;
  2007. // TODO: Define accumulation policy for features (prepend, append, replace).
  2008. // Currently we always replace.
  2009. if (!config.empty()) {
  2010. featureName += "_";
  2011. featureName += cmSystemTools::UpperCase(config);
  2012. }
  2013. cmStateSnapshot snp = this->StateSnapshot;
  2014. while (snp.IsValid()) {
  2015. if (const char* value = snp.GetDirectory().GetProperty(featureName)) {
  2016. return value;
  2017. }
  2018. snp = snp.GetBuildsystemDirectoryParent();
  2019. }
  2020. return nullptr;
  2021. }
  2022. std::string cmLocalGenerator::GetProjectName() const
  2023. {
  2024. return this->StateSnapshot.GetProjectName();
  2025. }
  2026. std::string cmLocalGenerator::ConstructComment(
  2027. cmCustomCommandGenerator const& ccg, const char* default_comment)
  2028. {
  2029. // Check for a comment provided with the command.
  2030. if (ccg.GetComment()) {
  2031. return ccg.GetComment();
  2032. }
  2033. // Construct a reasonable default comment if possible.
  2034. if (!ccg.GetOutputs().empty()) {
  2035. std::string comment;
  2036. comment = "Generating ";
  2037. const char* sep = "";
  2038. std::string currentBinaryDir = this->GetCurrentBinaryDirectory();
  2039. for (std::string const& o : ccg.GetOutputs()) {
  2040. comment += sep;
  2041. comment += this->ConvertToRelativePath(currentBinaryDir, o);
  2042. sep = ", ";
  2043. }
  2044. return comment;
  2045. }
  2046. // Otherwise use the provided default.
  2047. return default_comment;
  2048. }
  2049. class cmInstallTargetGeneratorLocal : public cmInstallTargetGenerator
  2050. {
  2051. public:
  2052. cmInstallTargetGeneratorLocal(cmLocalGenerator* lg, std::string const& t,
  2053. const char* dest, bool implib)
  2054. : cmInstallTargetGenerator(
  2055. t, dest, implib, "", std::vector<std::string>(), "Unspecified",
  2056. cmInstallGenerator::SelectMessageLevel(lg->GetMakefile()), false,
  2057. false)
  2058. {
  2059. this->Compute(lg);
  2060. }
  2061. };
  2062. void cmLocalGenerator::GenerateTargetInstallRules(
  2063. std::ostream& os, const std::string& config,
  2064. std::vector<std::string> const& configurationTypes)
  2065. {
  2066. // Convert the old-style install specification from each target to
  2067. // an install generator and run it.
  2068. const std::vector<cmGeneratorTarget*>& tgts = this->GetGeneratorTargets();
  2069. for (cmGeneratorTarget* l : tgts) {
  2070. if (l->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2071. continue;
  2072. }
  2073. // Include the user-specified pre-install script for this target.
  2074. if (const char* preinstall = l->GetProperty("PRE_INSTALL_SCRIPT")) {
  2075. cmInstallScriptGenerator g(preinstall, false, nullptr, false);
  2076. g.Generate(os, config, configurationTypes);
  2077. }
  2078. // Install this target if a destination is given.
  2079. if (!l->Target->GetInstallPath().empty()) {
  2080. // Compute the full install destination. Note that converting
  2081. // to unix slashes also removes any trailing slash.
  2082. // We also skip over the leading slash given by the user.
  2083. std::string destination = l->Target->GetInstallPath().substr(1);
  2084. cmSystemTools::ConvertToUnixSlashes(destination);
  2085. if (destination.empty()) {
  2086. destination = ".";
  2087. }
  2088. // Generate the proper install generator for this target type.
  2089. switch (l->GetType()) {
  2090. case cmStateEnums::EXECUTABLE:
  2091. case cmStateEnums::STATIC_LIBRARY:
  2092. case cmStateEnums::MODULE_LIBRARY: {
  2093. // Use a target install generator.
  2094. cmInstallTargetGeneratorLocal g(this, l->GetName(),
  2095. destination.c_str(), false);
  2096. g.Generate(os, config, configurationTypes);
  2097. } break;
  2098. case cmStateEnums::SHARED_LIBRARY: {
  2099. #if defined(_WIN32) || defined(__CYGWIN__)
  2100. // Special code to handle DLL. Install the import library
  2101. // to the normal destination and the DLL to the runtime
  2102. // destination.
  2103. cmInstallTargetGeneratorLocal g1(this, l->GetName(),
  2104. destination.c_str(), true);
  2105. g1.Generate(os, config, configurationTypes);
  2106. // We also skip over the leading slash given by the user.
  2107. destination = l->Target->GetRuntimeInstallPath().substr(1);
  2108. cmSystemTools::ConvertToUnixSlashes(destination);
  2109. cmInstallTargetGeneratorLocal g2(this, l->GetName(),
  2110. destination.c_str(), false);
  2111. g2.Generate(os, config, configurationTypes);
  2112. #else
  2113. // Use a target install generator.
  2114. cmInstallTargetGeneratorLocal g(this, l->GetName(),
  2115. destination.c_str(), false);
  2116. g.Generate(os, config, configurationTypes);
  2117. #endif
  2118. } break;
  2119. default:
  2120. break;
  2121. }
  2122. }
  2123. // Include the user-specified post-install script for this target.
  2124. if (const char* postinstall = l->GetProperty("POST_INSTALL_SCRIPT")) {
  2125. cmInstallScriptGenerator g(postinstall, false, nullptr, false);
  2126. g.Generate(os, config, configurationTypes);
  2127. }
  2128. }
  2129. }
  2130. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2131. static bool cmLocalGeneratorShortenObjectName(std::string& objName,
  2132. std::string::size_type max_len)
  2133. {
  2134. // Replace the beginning of the path portion of the object name with
  2135. // its own md5 sum.
  2136. std::string::size_type pos =
  2137. objName.find('/', objName.size() - max_len + 32);
  2138. if (pos != std::string::npos) {
  2139. cmCryptoHash md5(cmCryptoHash::AlgoMD5);
  2140. std::string md5name = md5.HashString(objName.substr(0, pos));
  2141. md5name += objName.substr(pos);
  2142. objName = md5name;
  2143. // The object name is now short enough.
  2144. return true;
  2145. }
  2146. // The object name could not be shortened enough.
  2147. return false;
  2148. }
  2149. bool cmLocalGeneratorCheckObjectName(std::string& objName,
  2150. std::string::size_type dir_len,
  2151. std::string::size_type max_total_len)
  2152. {
  2153. // Enforce the maximum file name length if possible.
  2154. std::string::size_type max_obj_len = max_total_len;
  2155. if (dir_len < max_total_len) {
  2156. max_obj_len = max_total_len - dir_len;
  2157. if (objName.size() > max_obj_len) {
  2158. // The current object file name is too long. Try to shorten it.
  2159. return cmLocalGeneratorShortenObjectName(objName, max_obj_len);
  2160. }
  2161. // The object file name is short enough.
  2162. return true;
  2163. }
  2164. // The build directory in which the object will be stored is
  2165. // already too deep.
  2166. return false;
  2167. }
  2168. #endif
  2169. std::string& cmLocalGenerator::CreateSafeUniqueObjectFileName(
  2170. const std::string& sin, std::string const& dir_max)
  2171. {
  2172. // Look for an existing mapped name for this object file.
  2173. std::map<std::string, std::string>::iterator it =
  2174. this->UniqueObjectNamesMap.find(sin);
  2175. // If no entry exists create one.
  2176. if (it == this->UniqueObjectNamesMap.end()) {
  2177. // Start with the original name.
  2178. std::string ssin = sin;
  2179. // Avoid full paths by removing leading slashes.
  2180. ssin.erase(0, ssin.find_first_not_of('/'));
  2181. // Avoid full paths by removing colons.
  2182. std::replace(ssin.begin(), ssin.end(), ':', '_');
  2183. // Avoid relative paths that go up the tree.
  2184. cmSystemTools::ReplaceString(ssin, "../", "__/");
  2185. // Avoid spaces.
  2186. std::replace(ssin.begin(), ssin.end(), ' ', '_');
  2187. // Mangle the name if necessary.
  2188. if (this->Makefile->IsOn("CMAKE_MANGLE_OBJECT_FILE_NAMES")) {
  2189. bool done;
  2190. int cc = 0;
  2191. char rpstr[100];
  2192. sprintf(rpstr, "_p_");
  2193. cmSystemTools::ReplaceString(ssin, "+", rpstr);
  2194. std::string sssin = sin;
  2195. do {
  2196. done = true;
  2197. for (it = this->UniqueObjectNamesMap.begin();
  2198. it != this->UniqueObjectNamesMap.end(); ++it) {
  2199. if (it->second == ssin) {
  2200. done = false;
  2201. }
  2202. }
  2203. if (done) {
  2204. break;
  2205. }
  2206. sssin = ssin;
  2207. cmSystemTools::ReplaceString(ssin, "_p_", rpstr);
  2208. sprintf(rpstr, "_p%d_", cc++);
  2209. } while (!done);
  2210. }
  2211. #if defined(CM_LG_ENCODE_OBJECT_NAMES)
  2212. if (!cmLocalGeneratorCheckObjectName(ssin, dir_max.size(),
  2213. this->ObjectPathMax)) {
  2214. // Warn if this is the first time the path has been seen.
  2215. if (this->ObjectMaxPathViolations.insert(dir_max).second) {
  2216. std::ostringstream m;
  2217. /* clang-format off */
  2218. m << "The object file directory\n"
  2219. << " " << dir_max << "\n"
  2220. << "has " << dir_max.size() << " characters. "
  2221. << "The maximum full path to an object file is "
  2222. << this->ObjectPathMax << " characters "
  2223. << "(see CMAKE_OBJECT_PATH_MAX). "
  2224. << "Object file\n"
  2225. << " " << ssin << "\n"
  2226. << "cannot be safely placed under this directory. "
  2227. << "The build may not work correctly.";
  2228. /* clang-format on */
  2229. this->IssueMessage(cmake::WARNING, m.str());
  2230. }
  2231. }
  2232. #else
  2233. (void)dir_max;
  2234. #endif
  2235. // Insert the newly mapped object file name.
  2236. std::map<std::string, std::string>::value_type e(sin, ssin);
  2237. it = this->UniqueObjectNamesMap.insert(e).first;
  2238. }
  2239. // Return the map entry.
  2240. return it->second;
  2241. }
  2242. void cmLocalGenerator::ComputeObjectFilenames(
  2243. std::map<cmSourceFile const*, std::string>& /*unused*/,
  2244. cmGeneratorTarget const* /*unused*/)
  2245. {
  2246. }
  2247. bool cmLocalGenerator::IsWindowsShell() const
  2248. {
  2249. return this->GetState()->UseWindowsShell();
  2250. }
  2251. bool cmLocalGenerator::IsWatcomWMake() const
  2252. {
  2253. return this->GetState()->UseWatcomWMake();
  2254. }
  2255. bool cmLocalGenerator::IsMinGWMake() const
  2256. {
  2257. return this->GetState()->UseMinGWMake();
  2258. }
  2259. bool cmLocalGenerator::IsNMake() const
  2260. {
  2261. return this->GetState()->UseNMake();
  2262. }
  2263. std::string cmLocalGenerator::GetObjectFileNameWithoutTarget(
  2264. const cmSourceFile& source, std::string const& dir_max,
  2265. bool* hasSourceExtension, char const* customOutputExtension)
  2266. {
  2267. // Construct the object file name using the full path to the source
  2268. // file which is its only unique identification.
  2269. std::string const& fullPath = source.GetFullPath();
  2270. // Try referencing the source relative to the source tree.
  2271. std::string relFromSource =
  2272. this->ConvertToRelativePath(this->GetCurrentSourceDirectory(), fullPath);
  2273. assert(!relFromSource.empty());
  2274. bool relSource = !cmSystemTools::FileIsFullPath(relFromSource);
  2275. bool subSource = relSource && relFromSource[0] != '.';
  2276. // Try referencing the source relative to the binary tree.
  2277. std::string relFromBinary =
  2278. this->ConvertToRelativePath(this->GetCurrentBinaryDirectory(), fullPath);
  2279. assert(!relFromBinary.empty());
  2280. bool relBinary = !cmSystemTools::FileIsFullPath(relFromBinary);
  2281. bool subBinary = relBinary && relFromBinary[0] != '.';
  2282. // Select a nice-looking reference to the source file to construct
  2283. // the object file name.
  2284. std::string objectName;
  2285. if ((relSource && !relBinary) || (subSource && !subBinary)) {
  2286. objectName = relFromSource;
  2287. } else if ((relBinary && !relSource) || (subBinary && !subSource)) {
  2288. objectName = relFromBinary;
  2289. } else if (relFromBinary.length() < relFromSource.length()) {
  2290. objectName = relFromBinary;
  2291. } else {
  2292. objectName = relFromSource;
  2293. }
  2294. // if it is still a full path check for the try compile case
  2295. // try compile never have in source sources, and should not
  2296. // have conflicting source file names in the same target
  2297. if (cmSystemTools::FileIsFullPath(objectName)) {
  2298. if (this->GetGlobalGenerator()->GetCMakeInstance()->GetIsInTryCompile()) {
  2299. objectName = cmSystemTools::GetFilenameName(source.GetFullPath());
  2300. }
  2301. }
  2302. // Replace the original source file extension with the object file
  2303. // extension.
  2304. bool keptSourceExtension = true;
  2305. if (!source.GetPropertyAsBool("KEEP_EXTENSION")) {
  2306. // Decide whether this language wants to replace the source
  2307. // extension with the object extension. For CMake 2.4
  2308. // compatibility do this by default.
  2309. bool replaceExt = this->NeedBackwardsCompatibility_2_4();
  2310. if (!replaceExt) {
  2311. std::string lang = source.GetLanguage();
  2312. if (!lang.empty()) {
  2313. std::string repVar = "CMAKE_";
  2314. repVar += lang;
  2315. repVar += "_OUTPUT_EXTENSION_REPLACE";
  2316. replaceExt = this->Makefile->IsOn(repVar);
  2317. }
  2318. }
  2319. // Remove the source extension if it is to be replaced.
  2320. if (replaceExt || customOutputExtension) {
  2321. keptSourceExtension = false;
  2322. std::string::size_type dot_pos = objectName.rfind('.');
  2323. if (dot_pos != std::string::npos) {
  2324. objectName = objectName.substr(0, dot_pos);
  2325. }
  2326. }
  2327. // Store the new extension.
  2328. if (customOutputExtension) {
  2329. objectName += customOutputExtension;
  2330. } else {
  2331. objectName += this->GlobalGenerator->GetLanguageOutputExtension(source);
  2332. }
  2333. }
  2334. if (hasSourceExtension) {
  2335. *hasSourceExtension = keptSourceExtension;
  2336. }
  2337. // Convert to a safe name.
  2338. return this->CreateSafeUniqueObjectFileName(objectName, dir_max);
  2339. }
  2340. std::string cmLocalGenerator::GetSourceFileLanguage(const cmSourceFile& source)
  2341. {
  2342. return source.GetLanguage();
  2343. }
  2344. cmake* cmLocalGenerator::GetCMakeInstance() const
  2345. {
  2346. return this->GlobalGenerator->GetCMakeInstance();
  2347. }
  2348. std::string const& cmLocalGenerator::GetSourceDirectory() const
  2349. {
  2350. return this->GetCMakeInstance()->GetHomeDirectory();
  2351. }
  2352. std::string const& cmLocalGenerator::GetBinaryDirectory() const
  2353. {
  2354. return this->GetCMakeInstance()->GetHomeOutputDirectory();
  2355. }
  2356. std::string const& cmLocalGenerator::GetCurrentBinaryDirectory() const
  2357. {
  2358. return this->StateSnapshot.GetDirectory().GetCurrentBinary();
  2359. }
  2360. std::string const& cmLocalGenerator::GetCurrentSourceDirectory() const
  2361. {
  2362. return this->StateSnapshot.GetDirectory().GetCurrentSource();
  2363. }
  2364. std::string cmLocalGenerator::GetTargetDirectory(
  2365. const cmGeneratorTarget* /*unused*/) const
  2366. {
  2367. cmSystemTools::Error("GetTargetDirectory"
  2368. " called on cmLocalGenerator");
  2369. return "";
  2370. }
  2371. KWIML_INT_uint64_t cmLocalGenerator::GetBackwardsCompatibility()
  2372. {
  2373. // The computed version may change until the project is fully
  2374. // configured.
  2375. if (!this->BackwardsCompatibilityFinal) {
  2376. unsigned int major = 0;
  2377. unsigned int minor = 0;
  2378. unsigned int patch = 0;
  2379. if (const char* value =
  2380. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY")) {
  2381. switch (sscanf(value, "%u.%u.%u", &major, &minor, &patch)) {
  2382. case 2:
  2383. patch = 0;
  2384. break;
  2385. case 1:
  2386. minor = 0;
  2387. patch = 0;
  2388. break;
  2389. default:
  2390. break;
  2391. }
  2392. }
  2393. this->BackwardsCompatibility = CMake_VERSION_ENCODE(major, minor, patch);
  2394. this->BackwardsCompatibilityFinal = true;
  2395. }
  2396. return this->BackwardsCompatibility;
  2397. }
  2398. bool cmLocalGenerator::NeedBackwardsCompatibility_2_4()
  2399. {
  2400. // Check the policy to decide whether to pay attention to this
  2401. // variable.
  2402. switch (this->GetPolicyStatus(cmPolicies::CMP0001)) {
  2403. case cmPolicies::WARN:
  2404. // WARN is just OLD without warning because user code does not
  2405. // always affect whether this check is done.
  2406. case cmPolicies::OLD:
  2407. // Old behavior is to check the variable.
  2408. break;
  2409. case cmPolicies::NEW:
  2410. // New behavior is to ignore the variable.
  2411. return false;
  2412. case cmPolicies::REQUIRED_IF_USED:
  2413. case cmPolicies::REQUIRED_ALWAYS:
  2414. // This will never be the case because the only way to require
  2415. // the setting is to require the user to specify version policy
  2416. // 2.6 or higher. Once we add that requirement then this whole
  2417. // method can be removed anyway.
  2418. return false;
  2419. }
  2420. // Compatibility is needed if CMAKE_BACKWARDS_COMPATIBILITY is set
  2421. // equal to or lower than the given version.
  2422. KWIML_INT_uint64_t actual_compat = this->GetBackwardsCompatibility();
  2423. return (actual_compat && actual_compat <= CMake_VERSION_ENCODE(2, 4, 255));
  2424. }
  2425. cmPolicies::PolicyStatus cmLocalGenerator::GetPolicyStatus(
  2426. cmPolicies::PolicyID id) const
  2427. {
  2428. return this->Makefile->GetPolicyStatus(id);
  2429. }
  2430. bool cmLocalGenerator::CheckDefinition(std::string const& define) const
  2431. {
  2432. // Many compilers do not support -DNAME(arg)=sdf so we disable it.
  2433. std::string::size_type pos = define.find_first_of("(=");
  2434. if (pos != std::string::npos) {
  2435. if (define[pos] == '(') {
  2436. std::ostringstream e;
  2437. /* clang-format off */
  2438. e << "WARNING: Function-style preprocessor definitions may not be "
  2439. << "passed on the compiler command line because many compilers "
  2440. << "do not support it.\n"
  2441. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2442. << "Consider defining the macro in a (configured) header file.\n";
  2443. /* clang-format on */
  2444. cmSystemTools::Message(e.str().c_str());
  2445. return false;
  2446. }
  2447. }
  2448. // Many compilers do not support # in the value so we disable it.
  2449. if (define.find_first_of('#') != std::string::npos) {
  2450. std::ostringstream e;
  2451. /* clang-format off */
  2452. e << "WARNING: Preprocessor definitions containing '#' may not be "
  2453. << "passed on the compiler command line because many compilers "
  2454. << "do not support it.\n"
  2455. << "CMake is dropping a preprocessor definition: " << define << "\n"
  2456. << "Consider defining the macro in a (configured) header file.\n";
  2457. /* clang-format on */
  2458. cmSystemTools::Message(e.str().c_str());
  2459. return false;
  2460. }
  2461. // Assume it is supported.
  2462. return true;
  2463. }
  2464. static void cmLGInfoProp(cmMakefile* mf, cmGeneratorTarget* target,
  2465. const std::string& prop)
  2466. {
  2467. if (const char* val = target->GetProperty(prop)) {
  2468. mf->AddDefinition(prop, val);
  2469. }
  2470. }
  2471. void cmLocalGenerator::GenerateAppleInfoPList(cmGeneratorTarget* target,
  2472. const std::string& targetName,
  2473. const char* fname)
  2474. {
  2475. // Find the Info.plist template.
  2476. const char* in = target->GetProperty("MACOSX_BUNDLE_INFO_PLIST");
  2477. std::string inFile = (in && *in) ? in : "MacOSXBundleInfo.plist.in";
  2478. if (!cmSystemTools::FileIsFullPath(inFile)) {
  2479. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  2480. if (!inMod.empty()) {
  2481. inFile = inMod;
  2482. }
  2483. }
  2484. if (!cmSystemTools::FileExists(inFile, true)) {
  2485. std::ostringstream e;
  2486. e << "Target " << target->GetName() << " Info.plist template \"" << inFile
  2487. << "\" could not be found.";
  2488. cmSystemTools::Error(e.str().c_str());
  2489. return;
  2490. }
  2491. // Convert target properties to variables in an isolated makefile
  2492. // scope to configure the file. If properties are set they will
  2493. // override user make variables. If not the configuration will fall
  2494. // back to the directory-level values set by the user.
  2495. cmMakefile* mf = this->Makefile;
  2496. cmMakefile::ScopePushPop varScope(mf);
  2497. mf->AddDefinition("MACOSX_BUNDLE_EXECUTABLE_NAME", targetName.c_str());
  2498. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_INFO_STRING");
  2499. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_ICON_FILE");
  2500. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_GUI_IDENTIFIER");
  2501. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_LONG_VERSION_STRING");
  2502. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_NAME");
  2503. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_SHORT_VERSION_STRING");
  2504. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_BUNDLE_VERSION");
  2505. cmLGInfoProp(mf, target, "MACOSX_BUNDLE_COPYRIGHT");
  2506. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  2507. }
  2508. void cmLocalGenerator::GenerateFrameworkInfoPList(
  2509. cmGeneratorTarget* target, const std::string& targetName, const char* fname)
  2510. {
  2511. // Find the Info.plist template.
  2512. const char* in = target->GetProperty("MACOSX_FRAMEWORK_INFO_PLIST");
  2513. std::string inFile = (in && *in) ? in : "MacOSXFrameworkInfo.plist.in";
  2514. if (!cmSystemTools::FileIsFullPath(inFile)) {
  2515. std::string inMod = this->Makefile->GetModulesFile(inFile.c_str());
  2516. if (!inMod.empty()) {
  2517. inFile = inMod;
  2518. }
  2519. }
  2520. if (!cmSystemTools::FileExists(inFile, true)) {
  2521. std::ostringstream e;
  2522. e << "Target " << target->GetName() << " Info.plist template \"" << inFile
  2523. << "\" could not be found.";
  2524. cmSystemTools::Error(e.str().c_str());
  2525. return;
  2526. }
  2527. // Convert target properties to variables in an isolated makefile
  2528. // scope to configure the file. If properties are set they will
  2529. // override user make variables. If not the configuration will fall
  2530. // back to the directory-level values set by the user.
  2531. cmMakefile* mf = this->Makefile;
  2532. cmMakefile::ScopePushPop varScope(mf);
  2533. mf->AddDefinition("MACOSX_FRAMEWORK_NAME", targetName.c_str());
  2534. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_ICON_FILE");
  2535. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_IDENTIFIER");
  2536. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_SHORT_VERSION_STRING");
  2537. cmLGInfoProp(mf, target, "MACOSX_FRAMEWORK_BUNDLE_VERSION");
  2538. mf->ConfigureFile(inFile.c_str(), fname, false, false, false);
  2539. }