cmGeneratorTarget_Options.cxx 26 KB

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