cmGeneratorTarget_Options.cxx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. /* clang-format off */
  4. #include "cmGeneratorTarget.h"
  5. /* clang-format on */
  6. #include "cmConfigure.h"
  7. #include <algorithm>
  8. #include <iterator>
  9. #include <map>
  10. #include <memory>
  11. #include <string>
  12. #include <unordered_set>
  13. #include <utility>
  14. #include <vector>
  15. #include <cm/string_view>
  16. #include <cmext/algorithm>
  17. #include <cmext/string_view>
  18. #include "cmEvaluatedTargetProperty.h"
  19. #include "cmGeneratorExpressionDAGChecker.h"
  20. #include "cmGlobalGenerator.h"
  21. #include "cmList.h"
  22. #include "cmListFileCache.h"
  23. #include "cmLocalGenerator.h"
  24. #include "cmMakefile.h"
  25. #include "cmMessageType.h"
  26. #include "cmPolicies.h"
  27. #include "cmRange.h"
  28. #include "cmStringAlgorithms.h"
  29. #include "cmSystemTools.h"
  30. #include "cmValue.h"
  31. #include "cmake.h"
  32. enum class OptionsParse
  33. {
  34. None,
  35. Shell
  36. };
  37. namespace {
  38. const auto DL_BEGIN = "<DEVICE_LINK>"_s;
  39. const auto DL_END = "</DEVICE_LINK>"_s;
  40. void processOptions(cmGeneratorTarget const* tgt,
  41. EvaluatedTargetPropertyEntries const& entries,
  42. std::vector<BT<std::string>>& options,
  43. std::unordered_set<std::string>& uniqueOptions,
  44. bool debugOptions, const char* logName, OptionsParse parse,
  45. bool processDeviceOptions = false)
  46. {
  47. bool splitOption = !processDeviceOptions;
  48. for (EvaluatedTargetPropertyEntry const& entry : entries.Entries) {
  49. std::string usedOptions;
  50. for (std::string const& opt : entry.Values) {
  51. if (processDeviceOptions && (opt == DL_BEGIN || opt == DL_END)) {
  52. options.emplace_back(opt, entry.Backtrace);
  53. splitOption = opt == DL_BEGIN;
  54. continue;
  55. }
  56. if (uniqueOptions.insert(opt).second) {
  57. if (parse == OptionsParse::Shell &&
  58. cmHasLiteralPrefix(opt, "SHELL:")) {
  59. if (splitOption) {
  60. std::vector<std::string> tmp;
  61. cmSystemTools::ParseUnixCommandLine(opt.c_str() + 6, tmp);
  62. for (std::string& o : tmp) {
  63. options.emplace_back(std::move(o), entry.Backtrace);
  64. }
  65. } else {
  66. options.emplace_back(std::string(opt.c_str() + 6),
  67. entry.Backtrace);
  68. }
  69. } else {
  70. options.emplace_back(opt, entry.Backtrace);
  71. }
  72. if (debugOptions) {
  73. usedOptions += " * " + opt + "\n";
  74. }
  75. }
  76. }
  77. if (!usedOptions.empty()) {
  78. tgt->GetLocalGenerator()->GetCMakeInstance()->IssueMessage(
  79. MessageType::LOG,
  80. std::string("Used ") + logName + std::string(" for target ") +
  81. tgt->GetName() + ":\n" + usedOptions,
  82. entry.Backtrace);
  83. }
  84. }
  85. }
  86. enum class NestedLinkerFlags
  87. {
  88. PreserveAsSpelled,
  89. Normalize,
  90. };
  91. std::vector<BT<std::string>> wrapOptions(
  92. std::vector<std::string>& options, const cmListFileBacktrace& bt,
  93. const std::vector<std::string>& wrapperFlag, const std::string& wrapperSep,
  94. bool concatFlagAndArgs, NestedLinkerFlags nestedLinkerFlags)
  95. {
  96. std::vector<BT<std::string>> result;
  97. if (options.empty()) {
  98. return result;
  99. }
  100. if (wrapperFlag.empty()) {
  101. // nothing specified, insert elements as is
  102. result.reserve(options.size());
  103. for (std::string& o : options) {
  104. result.emplace_back(std::move(o), bt);
  105. }
  106. return result;
  107. }
  108. auto insertWrapped = [&](std::vector<std::string>& opts) {
  109. if (!wrapperSep.empty()) {
  110. if (concatFlagAndArgs) {
  111. // insert flag elements except last one
  112. for (auto i = wrapperFlag.begin(); i != wrapperFlag.end() - 1; ++i) {
  113. result.emplace_back(*i, bt);
  114. }
  115. // concatenate last flag element and all list values
  116. // in one option
  117. result.emplace_back(wrapperFlag.back() + cmJoin(opts, wrapperSep), bt);
  118. } else {
  119. for (std::string const& i : wrapperFlag) {
  120. result.emplace_back(i, bt);
  121. }
  122. // concatenate all list values in one option
  123. result.emplace_back(cmJoin(opts, wrapperSep), bt);
  124. }
  125. } else {
  126. // prefix each element of list with wrapper
  127. if (concatFlagAndArgs) {
  128. std::transform(opts.begin(), opts.end(), opts.begin(),
  129. [&wrapperFlag](std::string const& o) -> std::string {
  130. return wrapperFlag.back() + o;
  131. });
  132. }
  133. for (std::string& o : opts) {
  134. for (auto i = wrapperFlag.begin(),
  135. e = concatFlagAndArgs ? wrapperFlag.end() - 1
  136. : wrapperFlag.end();
  137. i != e; ++i) {
  138. result.emplace_back(*i, bt);
  139. }
  140. result.emplace_back(std::move(o), bt);
  141. }
  142. }
  143. };
  144. if (nestedLinkerFlags == NestedLinkerFlags::PreserveAsSpelled) {
  145. insertWrapped(options);
  146. return result;
  147. }
  148. for (std::vector<std::string>::size_type index = 0; index < options.size();
  149. index++) {
  150. if (cmHasLiteralPrefix(options[index], "LINKER:")) {
  151. // LINKER wrapper specified, insert elements as is
  152. result.emplace_back(std::move(options[index]), bt);
  153. continue;
  154. }
  155. if (cmHasLiteralPrefix(options[index], "-Wl,")) {
  156. // replace option by LINKER wrapper
  157. result.emplace_back(options[index].replace(0, 4, "LINKER:"), bt);
  158. continue;
  159. }
  160. if (cmHasLiteralPrefix(options[index], "-Xlinker=")) {
  161. // replace option by LINKER wrapper
  162. result.emplace_back(options[index].replace(0, 9, "LINKER:"), bt);
  163. continue;
  164. }
  165. if (options[index] == "-Xlinker") {
  166. // replace option by LINKER wrapper
  167. if (index + 1 < options.size()) {
  168. result.emplace_back("LINKER:" + options[++index], bt);
  169. } else {
  170. result.emplace_back(std::move(options[index]), bt);
  171. }
  172. continue;
  173. }
  174. // collect all options which must be transformed
  175. std::vector<std::string> opts;
  176. while (index < options.size()) {
  177. if (!cmHasLiteralPrefix(options[index], "LINKER:") &&
  178. !cmHasLiteralPrefix(options[index], "-Wl,") &&
  179. !cmHasLiteralPrefix(options[index], "-Xlinker")) {
  180. opts.emplace_back(std::move(options[index++]));
  181. } else {
  182. --index;
  183. break;
  184. }
  185. }
  186. if (opts.empty()) {
  187. continue;
  188. }
  189. insertWrapped(opts);
  190. }
  191. return result;
  192. }
  193. }
  194. void cmGeneratorTarget::GetCompileOptions(std::vector<std::string>& result,
  195. const std::string& config,
  196. const std::string& language) const
  197. {
  198. std::vector<BT<std::string>> tmp = this->GetCompileOptions(config, language);
  199. result.reserve(tmp.size());
  200. for (BT<std::string>& v : tmp) {
  201. result.emplace_back(std::move(v.Value));
  202. }
  203. }
  204. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileOptions(
  205. std::string const& config, std::string const& language) const
  206. {
  207. ConfigAndLanguage cacheKey(config, language);
  208. {
  209. auto it = this->CompileOptionsCache.find(cacheKey);
  210. if (it != this->CompileOptionsCache.end()) {
  211. return it->second;
  212. }
  213. }
  214. std::vector<BT<std::string>> result;
  215. std::unordered_set<std::string> uniqueOptions;
  216. cmGeneratorExpressionDAGChecker dagChecker(
  217. this, "COMPILE_OPTIONS", nullptr, nullptr, this->LocalGenerator, config);
  218. cmList debugProperties{ this->Makefile->GetDefinition(
  219. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  220. bool debugOptions = !this->DebugCompileOptionsDone &&
  221. cm::contains(debugProperties, "COMPILE_OPTIONS");
  222. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  223. this->DebugCompileOptionsDone = true;
  224. }
  225. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  226. this, config, language, &dagChecker, this->CompileOptionsEntries);
  227. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_OPTIONS", language,
  228. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  229. processOptions(this, entries, result, uniqueOptions, debugOptions,
  230. "compile options", OptionsParse::Shell);
  231. CompileOptionsCache.emplace(cacheKey, result);
  232. return result;
  233. }
  234. void cmGeneratorTarget::GetCompileFeatures(std::vector<std::string>& result,
  235. const std::string& config) const
  236. {
  237. std::vector<BT<std::string>> tmp = this->GetCompileFeatures(config);
  238. result.reserve(tmp.size());
  239. for (BT<std::string>& v : tmp) {
  240. result.emplace_back(std::move(v.Value));
  241. }
  242. }
  243. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileFeatures(
  244. std::string const& config) const
  245. {
  246. std::vector<BT<std::string>> result;
  247. std::unordered_set<std::string> uniqueFeatures;
  248. cmGeneratorExpressionDAGChecker dagChecker(
  249. this, "COMPILE_FEATURES", nullptr, nullptr, this->LocalGenerator, config);
  250. cmList debugProperties{ this->Makefile->GetDefinition(
  251. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  252. bool debugFeatures = !this->DebugCompileFeaturesDone &&
  253. cm::contains(debugProperties, "COMPILE_FEATURES");
  254. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  255. this->DebugCompileFeaturesDone = true;
  256. }
  257. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  258. this, config, std::string(), &dagChecker, this->CompileFeaturesEntries);
  259. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_FEATURES",
  260. std::string(), &dagChecker, entries,
  261. IncludeRuntimeInterface::Yes);
  262. processOptions(this, entries, result, uniqueFeatures, debugFeatures,
  263. "compile features", OptionsParse::None);
  264. return result;
  265. }
  266. void cmGeneratorTarget::GetCompileDefinitions(
  267. std::vector<std::string>& result, const std::string& config,
  268. const std::string& language) const
  269. {
  270. std::vector<BT<std::string>> tmp =
  271. this->GetCompileDefinitions(config, language);
  272. result.reserve(tmp.size());
  273. for (BT<std::string>& v : tmp) {
  274. result.emplace_back(std::move(v.Value));
  275. }
  276. }
  277. std::vector<BT<std::string>> cmGeneratorTarget::GetCompileDefinitions(
  278. std::string const& config, std::string const& language) const
  279. {
  280. ConfigAndLanguage cacheKey(config, language);
  281. {
  282. auto it = this->CompileDefinitionsCache.find(cacheKey);
  283. if (it != this->CompileDefinitionsCache.end()) {
  284. return it->second;
  285. }
  286. }
  287. std::vector<BT<std::string>> list;
  288. std::unordered_set<std::string> uniqueOptions;
  289. cmGeneratorExpressionDAGChecker dagChecker(this, "COMPILE_DEFINITIONS",
  290. nullptr, nullptr,
  291. this->LocalGenerator, config);
  292. cmList debugProperties{ this->Makefile->GetDefinition(
  293. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  294. bool debugDefines = !this->DebugCompileDefinitionsDone &&
  295. cm::contains(debugProperties, "COMPILE_DEFINITIONS");
  296. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  297. this->DebugCompileDefinitionsDone = true;
  298. }
  299. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  300. this, config, language, &dagChecker, this->CompileDefinitionsEntries);
  301. AddInterfaceEntries(this, config, "INTERFACE_COMPILE_DEFINITIONS", language,
  302. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  303. if (!config.empty()) {
  304. std::string configPropName =
  305. "COMPILE_DEFINITIONS_" + cmSystemTools::UpperCase(config);
  306. cmValue configProp = this->GetProperty(configPropName);
  307. if (configProp) {
  308. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0043)) {
  309. case cmPolicies::WARN: {
  310. this->LocalGenerator->IssueMessage(
  311. MessageType::AUTHOR_WARNING,
  312. cmPolicies::GetPolicyWarning(cmPolicies::CMP0043));
  313. CM_FALLTHROUGH;
  314. }
  315. case cmPolicies::OLD: {
  316. std::unique_ptr<TargetPropertyEntry> entry =
  317. TargetPropertyEntry::Create(
  318. *this->LocalGenerator->GetCMakeInstance(), *configProp);
  319. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  320. this, config, language, &dagChecker, *entry));
  321. } break;
  322. case cmPolicies::NEW:
  323. case cmPolicies::REQUIRED_ALWAYS:
  324. case cmPolicies::REQUIRED_IF_USED:
  325. break;
  326. }
  327. }
  328. }
  329. processOptions(this, entries, list, uniqueOptions, debugDefines,
  330. "compile definitions", OptionsParse::None);
  331. this->CompileDefinitionsCache.emplace(cacheKey, list);
  332. return list;
  333. }
  334. std::vector<BT<std::string>> cmGeneratorTarget::GetPrecompileHeaders(
  335. const std::string& config, const std::string& language) const
  336. {
  337. ConfigAndLanguage cacheKey(config, language);
  338. {
  339. auto it = this->PrecompileHeadersCache.find(cacheKey);
  340. if (it != this->PrecompileHeadersCache.end()) {
  341. return it->second;
  342. }
  343. }
  344. std::unordered_set<std::string> uniqueOptions;
  345. cmGeneratorExpressionDAGChecker dagChecker(this, "PRECOMPILE_HEADERS",
  346. nullptr, nullptr,
  347. this->LocalGenerator, config);
  348. cmList debugProperties{ this->Makefile->GetDefinition(
  349. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  350. bool debugDefines = !this->DebugPrecompileHeadersDone &&
  351. std::find(debugProperties.begin(), debugProperties.end(),
  352. "PRECOMPILE_HEADERS") != debugProperties.end();
  353. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  354. this->DebugPrecompileHeadersDone = true;
  355. }
  356. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  357. this, config, language, &dagChecker, this->PrecompileHeadersEntries);
  358. AddInterfaceEntries(this, config, "INTERFACE_PRECOMPILE_HEADERS", language,
  359. &dagChecker, entries, IncludeRuntimeInterface::Yes);
  360. std::vector<BT<std::string>> list;
  361. processOptions(this, entries, list, uniqueOptions, debugDefines,
  362. "precompile headers", OptionsParse::None);
  363. this->PrecompileHeadersCache.emplace(cacheKey, list);
  364. return list;
  365. }
  366. void cmGeneratorTarget::GetLinkOptions(std::vector<std::string>& result,
  367. const std::string& config,
  368. const std::string& language) const
  369. {
  370. if (this->IsDeviceLink() &&
  371. this->GetPolicyStatusCMP0105() != cmPolicies::NEW) {
  372. // link options are not propagated to the device link step
  373. return;
  374. }
  375. std::vector<BT<std::string>> tmp = this->GetLinkOptions(config, language);
  376. result.reserve(tmp.size());
  377. for (BT<std::string>& v : tmp) {
  378. result.emplace_back(std::move(v.Value));
  379. }
  380. }
  381. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkOptions(
  382. std::string const& config, std::string const& language) const
  383. {
  384. ConfigAndLanguage cacheKey(
  385. config, cmStrCat(language, this->IsDeviceLink() ? "-device" : ""));
  386. {
  387. auto it = this->LinkOptionsCache.find(cacheKey);
  388. if (it != this->LinkOptionsCache.end()) {
  389. return it->second;
  390. }
  391. }
  392. std::vector<BT<std::string>> result;
  393. std::unordered_set<std::string> uniqueOptions;
  394. cmGeneratorExpressionDAGChecker dagChecker(
  395. this, "LINK_OPTIONS", nullptr, nullptr, this->LocalGenerator, config);
  396. cmList debugProperties{ this->Makefile->GetDefinition(
  397. "CMAKE_DEBUG_TARGET_PROPERTIES") };
  398. bool debugOptions = !this->DebugLinkOptionsDone &&
  399. cm::contains(debugProperties, "LINK_OPTIONS");
  400. if (this->GlobalGenerator->GetConfigureDoneCMP0026()) {
  401. this->DebugLinkOptionsDone = true;
  402. }
  403. EvaluatedTargetPropertyEntries entries = EvaluateTargetPropertyEntries(
  404. this, config, language, &dagChecker, this->LinkOptionsEntries);
  405. AddInterfaceEntries(this, config, "INTERFACE_LINK_OPTIONS", language,
  406. &dagChecker, entries, IncludeRuntimeInterface::Yes,
  407. this->GetPolicyStatusCMP0099() == cmPolicies::NEW
  408. ? UseTo::Link
  409. : UseTo::Compile);
  410. processOptions(this, entries, result, uniqueOptions, debugOptions,
  411. "link options", OptionsParse::Shell, this->IsDeviceLink());
  412. if (this->IsDeviceLink()) {
  413. // wrap host link options
  414. const std::string wrapper(this->Makefile->GetSafeDefinition(
  415. "CMAKE_" + language + "_DEVICE_COMPILER_WRAPPER_FLAG"));
  416. cmList wrapperFlag{ wrapper };
  417. const std::string wrapperSep(this->Makefile->GetSafeDefinition(
  418. "CMAKE_" + language + "_DEVICE_COMPILER_WRAPPER_FLAG_SEP"));
  419. bool concatFlagAndArgs = true;
  420. if (!wrapperFlag.empty() && wrapperFlag.back() == " ") {
  421. concatFlagAndArgs = false;
  422. wrapperFlag.pop_back();
  423. }
  424. auto it = result.begin();
  425. while (it != result.end()) {
  426. if (it->Value == DL_BEGIN) {
  427. // device link options, no treatment
  428. it = result.erase(it);
  429. it = std::find_if(it, result.end(), [](const BT<std::string>& item) {
  430. return item.Value == DL_END;
  431. });
  432. if (it != result.end()) {
  433. it = result.erase(it);
  434. }
  435. } else {
  436. // host link options must be wrapped
  437. std::vector<std::string> options;
  438. cmSystemTools::ParseUnixCommandLine(it->Value.c_str(), options);
  439. auto hostOptions =
  440. wrapOptions(options, it->Backtrace, wrapperFlag, wrapperSep,
  441. concatFlagAndArgs, NestedLinkerFlags::Normalize);
  442. it = result.erase(it);
  443. // some compilers (like gcc 4.8 or Intel 19.0 or XLC 16) do not respect
  444. // C++11 standard: 'std::vector::insert()' do not returns an iterator,
  445. // so need to recompute the iterator after insertion.
  446. if (it == result.end()) {
  447. cm::append(result, hostOptions);
  448. it = result.end();
  449. } else {
  450. auto index = it - result.begin();
  451. result.insert(it, hostOptions.begin(), hostOptions.end());
  452. it = result.begin() + index + hostOptions.size();
  453. }
  454. }
  455. }
  456. }
  457. // Last step: replace "LINKER:" prefixed elements by
  458. // actual linker wrapper
  459. result = this->ResolveLinkerWrapper(result, language);
  460. this->LinkOptionsCache.emplace(cacheKey, result);
  461. return result;
  462. }
  463. std::vector<BT<std::string>>& cmGeneratorTarget::ResolveLinkerWrapper(
  464. std::vector<BT<std::string>>& result, const std::string& language,
  465. bool joinItems) const
  466. {
  467. // replace "LINKER:" prefixed elements by actual linker wrapper
  468. const std::string wrapper(this->Makefile->GetSafeDefinition(
  469. "CMAKE_" + language +
  470. (this->IsDeviceLink() ? "_DEVICE_LINKER_WRAPPER_FLAG"
  471. : "_LINKER_WRAPPER_FLAG")));
  472. cmList wrapperFlag{ wrapper };
  473. const std::string wrapperSep(this->Makefile->GetSafeDefinition(
  474. "CMAKE_" + language +
  475. (this->IsDeviceLink() ? "_DEVICE_LINKER_WRAPPER_FLAG_SEP"
  476. : "_LINKER_WRAPPER_FLAG_SEP")));
  477. bool concatFlagAndArgs = true;
  478. if (!wrapperFlag.empty() && wrapperFlag.back() == " ") {
  479. concatFlagAndArgs = false;
  480. wrapperFlag.pop_back();
  481. }
  482. const std::string LINKER{ "LINKER:" };
  483. const std::string SHELL{ "SHELL:" };
  484. const std::string LINKER_SHELL = LINKER + SHELL;
  485. for (auto entry = result.begin(); entry != result.end(); ++entry) {
  486. if (entry->Value.compare(0, LINKER.length(), LINKER) != 0) {
  487. continue;
  488. }
  489. std::string value = std::move(entry->Value);
  490. cmListFileBacktrace bt = std::move(entry->Backtrace);
  491. entry = result.erase(entry);
  492. std::vector<std::string> linkerOptions;
  493. if (value.compare(0, LINKER_SHELL.length(), LINKER_SHELL) == 0) {
  494. cmSystemTools::ParseUnixCommandLine(
  495. value.c_str() + LINKER_SHELL.length(), linkerOptions);
  496. } else {
  497. linkerOptions =
  498. cmTokenize(value.substr(LINKER.length()), ',', cmTokenizerMode::New);
  499. }
  500. if (linkerOptions.empty()) {
  501. continue;
  502. }
  503. // for now, raise an error if prefix SHELL: is part of arguments
  504. if (std::find_if(linkerOptions.begin(), linkerOptions.end(),
  505. [&SHELL](const std::string& item) -> bool {
  506. return item.find(SHELL) != std::string::npos;
  507. }) != linkerOptions.end()) {
  508. this->LocalGenerator->GetCMakeInstance()->IssueMessage(
  509. MessageType::FATAL_ERROR,
  510. "'SHELL:' prefix is not supported as part of 'LINKER:' arguments.",
  511. this->GetBacktrace());
  512. return result;
  513. }
  514. // Very old versions of the C++ standard library return void for insert, so
  515. // can't use it to get the new iterator
  516. const auto index = entry - result.begin();
  517. std::vector<BT<std::string>> options =
  518. wrapOptions(linkerOptions, bt, wrapperFlag, wrapperSep,
  519. concatFlagAndArgs, NestedLinkerFlags::PreserveAsSpelled);
  520. if (joinItems) {
  521. result.insert(
  522. entry, cmJoin(cmMakeRange(options.begin(), options.end()), " "_s));
  523. entry = std::next(result.begin(), index);
  524. } else {
  525. result.insert(entry, options.begin(), options.end());
  526. entry = std::next(result.begin(), index + options.size() - 1);
  527. }
  528. }
  529. return result;
  530. }
  531. void cmGeneratorTarget::GetStaticLibraryLinkOptions(
  532. std::vector<std::string>& result, const std::string& config,
  533. const std::string& language) const
  534. {
  535. std::vector<BT<std::string>> tmp =
  536. this->GetStaticLibraryLinkOptions(config, language);
  537. result.reserve(tmp.size());
  538. for (BT<std::string>& v : tmp) {
  539. result.emplace_back(std::move(v.Value));
  540. }
  541. }
  542. std::vector<BT<std::string>> cmGeneratorTarget::GetStaticLibraryLinkOptions(
  543. std::string const& config, std::string const& language) const
  544. {
  545. std::vector<BT<std::string>> result;
  546. std::unordered_set<std::string> uniqueOptions;
  547. cmGeneratorExpressionDAGChecker dagChecker(this, "STATIC_LIBRARY_OPTIONS",
  548. nullptr, nullptr,
  549. this->LocalGenerator, config);
  550. EvaluatedTargetPropertyEntries entries;
  551. if (cmValue linkOptions = this->GetProperty("STATIC_LIBRARY_OPTIONS")) {
  552. std::unique_ptr<TargetPropertyEntry> entry = TargetPropertyEntry::Create(
  553. *this->LocalGenerator->GetCMakeInstance(), *linkOptions);
  554. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  555. this, config, language, &dagChecker, *entry));
  556. }
  557. processOptions(this, entries, result, uniqueOptions, false,
  558. "static library link options", OptionsParse::Shell);
  559. return result;
  560. }
  561. void cmGeneratorTarget::GetLinkDepends(std::vector<std::string>& result,
  562. const std::string& config,
  563. const std::string& language) const
  564. {
  565. std::vector<BT<std::string>> tmp = this->GetLinkDepends(config, language);
  566. result.reserve(tmp.size());
  567. for (BT<std::string>& v : tmp) {
  568. result.emplace_back(std::move(v.Value));
  569. }
  570. }
  571. std::vector<BT<std::string>> cmGeneratorTarget::GetLinkDepends(
  572. std::string const& config, std::string const& language) const
  573. {
  574. std::vector<BT<std::string>> result;
  575. std::unordered_set<std::string> uniqueOptions;
  576. cmGeneratorExpressionDAGChecker dagChecker(
  577. this, "LINK_DEPENDS", nullptr, nullptr, this->LocalGenerator, config);
  578. EvaluatedTargetPropertyEntries entries;
  579. if (cmValue linkDepends = this->GetProperty("LINK_DEPENDS")) {
  580. cmList depends{ *linkDepends };
  581. for (const auto& depend : depends) {
  582. std::unique_ptr<TargetPropertyEntry> entry = TargetPropertyEntry::Create(
  583. *this->LocalGenerator->GetCMakeInstance(), depend);
  584. entries.Entries.emplace_back(EvaluateTargetPropertyEntry(
  585. this, config, language, &dagChecker, *entry));
  586. }
  587. }
  588. AddInterfaceEntries(this, config, "INTERFACE_LINK_DEPENDS", language,
  589. &dagChecker, entries, IncludeRuntimeInterface::Yes,
  590. this->GetPolicyStatusCMP0099() == cmPolicies::NEW
  591. ? UseTo::Link
  592. : UseTo::Compile);
  593. processOptions(this, entries, result, uniqueOptions, false, "link depends",
  594. OptionsParse::None);
  595. return result;
  596. }