1
0

cmTargetLinkLibrariesCommand.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 "cmTargetLinkLibrariesCommand.h"
  4. #include <cassert>
  5. #include <cstddef>
  6. #include <memory>
  7. #include <sstream>
  8. #include <unordered_set>
  9. #include <utility>
  10. #include <cm/optional>
  11. #include <cm/string_view>
  12. #include "cmExecutionStatus.h"
  13. #include "cmGeneratorExpression.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmListFileCache.h"
  16. #include "cmMakefile.h"
  17. #include "cmMessageType.h"
  18. #include "cmPolicies.h"
  19. #include "cmState.h"
  20. #include "cmStateTypes.h"
  21. #include "cmStringAlgorithms.h"
  22. #include "cmSystemTools.h"
  23. #include "cmTarget.h"
  24. #include "cmTargetLinkLibraryType.h"
  25. namespace {
  26. enum ProcessingState
  27. {
  28. ProcessingLinkLibraries,
  29. ProcessingPlainLinkInterface,
  30. ProcessingKeywordLinkInterface,
  31. ProcessingPlainPublicInterface,
  32. ProcessingKeywordPublicInterface,
  33. ProcessingPlainPrivateInterface,
  34. ProcessingKeywordPrivateInterface
  35. };
  36. const char* LinkLibraryTypeNames[3] = { "general", "debug", "optimized" };
  37. struct TLL
  38. {
  39. cmMakefile& Makefile;
  40. cmTarget* Target;
  41. bool WarnRemoteInterface = false;
  42. bool RejectRemoteLinking = false;
  43. bool EncodeRemoteReference = false;
  44. std::string DirectoryId;
  45. std::unordered_set<std::string> Props;
  46. TLL(cmMakefile& mf, cmTarget* target);
  47. ~TLL();
  48. bool HandleLibrary(ProcessingState currentProcessingState,
  49. const std::string& lib, cmTargetLinkLibraryType llt);
  50. void AppendProperty(std::string const& prop, std::string const& value);
  51. void AffectsProperty(std::string const& prop);
  52. };
  53. } // namespace
  54. static void LinkLibraryTypeSpecifierWarning(cmMakefile& mf, int left,
  55. int right);
  56. bool cmTargetLinkLibrariesCommand(std::vector<std::string> const& args,
  57. cmExecutionStatus& status)
  58. {
  59. // Must have at least one argument.
  60. if (args.empty()) {
  61. status.SetError("called with incorrect number of arguments");
  62. return false;
  63. }
  64. cmMakefile& mf = status.GetMakefile();
  65. // Alias targets cannot be on the LHS of this command.
  66. if (mf.IsAlias(args[0])) {
  67. status.SetError("can not be used on an ALIAS target.");
  68. return false;
  69. }
  70. // Lookup the target for which libraries are specified.
  71. cmTarget* target = mf.GetGlobalGenerator()->FindTarget(args[0]);
  72. if (!target) {
  73. for (const auto& importedTarget : mf.GetOwnedImportedTargets()) {
  74. if (importedTarget->GetName() == args[0]) {
  75. target = importedTarget.get();
  76. break;
  77. }
  78. }
  79. }
  80. if (!target) {
  81. mf.IssueMessage(MessageType::FATAL_ERROR,
  82. cmStrCat("Cannot specify link libraries for target \"",
  83. args[0],
  84. "\" which is not built by this project."));
  85. cmSystemTools::SetFatalErrorOccurred();
  86. return true;
  87. }
  88. // Having a UTILITY library on the LHS is a bug.
  89. if (target->GetType() == cmStateEnums::UTILITY) {
  90. mf.IssueMessage(
  91. MessageType::FATAL_ERROR,
  92. cmStrCat(
  93. "Utility target \"", target->GetName(),
  94. "\" must not be used as the target of a target_link_libraries call."));
  95. return false;
  96. }
  97. // But we might not have any libs after variable expansion.
  98. if (args.size() < 2) {
  99. return true;
  100. }
  101. TLL tll(mf, target);
  102. // Keep track of link configuration specifiers.
  103. cmTargetLinkLibraryType llt = GENERAL_LibraryType;
  104. bool haveLLT = false;
  105. // Start with primary linking and switch to link interface
  106. // specification if the keyword is encountered as the first argument.
  107. ProcessingState currentProcessingState = ProcessingLinkLibraries;
  108. // Accumulate consecutive non-keyword arguments into one entry in
  109. // order to handle unquoted generator expressions containing ';'.
  110. std::size_t genexNesting = 0;
  111. cm::optional<std::string> currentEntry;
  112. auto processCurrentEntry = [&]() -> bool {
  113. // FIXME: Warn about partial genex if genexNesting > 0?
  114. genexNesting = 0;
  115. if (currentEntry) {
  116. assert(!haveLLT);
  117. if (!tll.HandleLibrary(currentProcessingState, *currentEntry,
  118. GENERAL_LibraryType)) {
  119. return false;
  120. }
  121. currentEntry = cm::nullopt;
  122. }
  123. return true;
  124. };
  125. auto extendCurrentEntry = [&currentEntry](std::string const& arg) {
  126. if (currentEntry) {
  127. currentEntry = cmStrCat(*currentEntry, ';', arg);
  128. } else {
  129. currentEntry = arg;
  130. }
  131. };
  132. // Keep this list in sync with the keyword dispatch below.
  133. static std::unordered_set<std::string> const keywords{
  134. "LINK_INTERFACE_LIBRARIES",
  135. "INTERFACE",
  136. "LINK_PUBLIC",
  137. "PUBLIC",
  138. "LINK_PRIVATE",
  139. "PRIVATE",
  140. "debug",
  141. "optimized",
  142. "general",
  143. };
  144. // Add libraries, note that there is an optional prefix
  145. // of debug and optimized that can be used.
  146. for (unsigned int i = 1; i < args.size(); ++i) {
  147. if (keywords.count(args[i])) {
  148. // A keyword argument terminates any accumulated partial genex.
  149. if (!processCurrentEntry()) {
  150. return false;
  151. }
  152. // Process this keyword argument.
  153. if (args[i] == "LINK_INTERFACE_LIBRARIES") {
  154. currentProcessingState = ProcessingPlainLinkInterface;
  155. if (i != 1) {
  156. mf.IssueMessage(
  157. MessageType::FATAL_ERROR,
  158. "The LINK_INTERFACE_LIBRARIES option must appear as the "
  159. "second argument, just after the target name.");
  160. return true;
  161. }
  162. } else if (args[i] == "INTERFACE") {
  163. if (i != 1 &&
  164. currentProcessingState != ProcessingKeywordPrivateInterface &&
  165. currentProcessingState != ProcessingKeywordPublicInterface &&
  166. currentProcessingState != ProcessingKeywordLinkInterface) {
  167. mf.IssueMessage(MessageType::FATAL_ERROR,
  168. "The INTERFACE, PUBLIC or PRIVATE option must "
  169. "appear as the second argument, just after the "
  170. "target name.");
  171. return true;
  172. }
  173. currentProcessingState = ProcessingKeywordLinkInterface;
  174. } else if (args[i] == "LINK_PUBLIC") {
  175. if (i != 1 &&
  176. currentProcessingState != ProcessingPlainPrivateInterface &&
  177. currentProcessingState != ProcessingPlainPublicInterface) {
  178. mf.IssueMessage(
  179. MessageType::FATAL_ERROR,
  180. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the "
  181. "second argument, just after the target name.");
  182. return true;
  183. }
  184. currentProcessingState = ProcessingPlainPublicInterface;
  185. } else if (args[i] == "PUBLIC") {
  186. if (i != 1 &&
  187. currentProcessingState != ProcessingKeywordPrivateInterface &&
  188. currentProcessingState != ProcessingKeywordPublicInterface &&
  189. currentProcessingState != ProcessingKeywordLinkInterface) {
  190. mf.IssueMessage(MessageType::FATAL_ERROR,
  191. "The INTERFACE, PUBLIC or PRIVATE option must "
  192. "appear as the second argument, just after the "
  193. "target name.");
  194. return true;
  195. }
  196. currentProcessingState = ProcessingKeywordPublicInterface;
  197. } else if (args[i] == "LINK_PRIVATE") {
  198. if (i != 1 &&
  199. currentProcessingState != ProcessingPlainPublicInterface &&
  200. currentProcessingState != ProcessingPlainPrivateInterface) {
  201. mf.IssueMessage(
  202. MessageType::FATAL_ERROR,
  203. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the "
  204. "second argument, just after the target name.");
  205. return true;
  206. }
  207. currentProcessingState = ProcessingPlainPrivateInterface;
  208. } else if (args[i] == "PRIVATE") {
  209. if (i != 1 &&
  210. currentProcessingState != ProcessingKeywordPrivateInterface &&
  211. currentProcessingState != ProcessingKeywordPublicInterface &&
  212. currentProcessingState != ProcessingKeywordLinkInterface) {
  213. mf.IssueMessage(MessageType::FATAL_ERROR,
  214. "The INTERFACE, PUBLIC or PRIVATE option must "
  215. "appear as the second argument, just after the "
  216. "target name.");
  217. return true;
  218. }
  219. currentProcessingState = ProcessingKeywordPrivateInterface;
  220. } else if (args[i] == "debug") {
  221. if (haveLLT) {
  222. LinkLibraryTypeSpecifierWarning(mf, llt, DEBUG_LibraryType);
  223. }
  224. llt = DEBUG_LibraryType;
  225. haveLLT = true;
  226. } else if (args[i] == "optimized") {
  227. if (haveLLT) {
  228. LinkLibraryTypeSpecifierWarning(mf, llt, OPTIMIZED_LibraryType);
  229. }
  230. llt = OPTIMIZED_LibraryType;
  231. haveLLT = true;
  232. } else if (args[i] == "general") {
  233. if (haveLLT) {
  234. LinkLibraryTypeSpecifierWarning(mf, llt, GENERAL_LibraryType);
  235. }
  236. llt = GENERAL_LibraryType;
  237. haveLLT = true;
  238. }
  239. } else if (haveLLT) {
  240. // The link type was specified by the previous argument.
  241. haveLLT = false;
  242. assert(!currentEntry);
  243. if (!tll.HandleLibrary(currentProcessingState, args[i], llt)) {
  244. return false;
  245. }
  246. llt = GENERAL_LibraryType;
  247. } else {
  248. // Track the genex nesting level.
  249. {
  250. cm::string_view arg = args[i];
  251. for (std::string::size_type pos = 0; pos < arg.size(); ++pos) {
  252. cm::string_view cur = arg.substr(pos);
  253. if (cmHasLiteralPrefix(cur, "$<")) {
  254. ++genexNesting;
  255. ++pos;
  256. } else if (genexNesting > 0 && cmHasLiteralPrefix(cur, ">")) {
  257. --genexNesting;
  258. }
  259. }
  260. }
  261. // Accumulate this argument in the current entry.
  262. extendCurrentEntry(args[i]);
  263. // Process this entry if it does not end inside a genex.
  264. if (genexNesting == 0) {
  265. if (!processCurrentEntry()) {
  266. return false;
  267. }
  268. }
  269. }
  270. }
  271. // Process the last accumulated partial genex, if any.
  272. if (!processCurrentEntry()) {
  273. return false;
  274. }
  275. // Make sure the last argument was not a library type specifier.
  276. if (haveLLT) {
  277. mf.IssueMessage(MessageType::FATAL_ERROR,
  278. cmStrCat("The \"", LinkLibraryTypeNames[llt],
  279. "\" argument must be followed by a library."));
  280. cmSystemTools::SetFatalErrorOccurred();
  281. }
  282. return true;
  283. }
  284. static void LinkLibraryTypeSpecifierWarning(cmMakefile& mf, int left,
  285. int right)
  286. {
  287. mf.IssueMessage(
  288. MessageType::AUTHOR_WARNING,
  289. cmStrCat(
  290. "Link library type specifier \"", LinkLibraryTypeNames[left],
  291. "\" is followed by specifier \"", LinkLibraryTypeNames[right],
  292. "\" instead of a library name. The first specifier will be ignored."));
  293. }
  294. namespace {
  295. TLL::TLL(cmMakefile& mf, cmTarget* target)
  296. : Makefile(mf)
  297. , Target(target)
  298. {
  299. if (&this->Makefile != this->Target->GetMakefile()) {
  300. // The LHS target was created in another directory.
  301. switch (this->Makefile.GetPolicyStatus(cmPolicies::CMP0079)) {
  302. case cmPolicies::WARN:
  303. this->WarnRemoteInterface = true;
  304. CM_FALLTHROUGH;
  305. case cmPolicies::OLD:
  306. this->RejectRemoteLinking = true;
  307. break;
  308. case cmPolicies::NEW:
  309. this->EncodeRemoteReference = true;
  310. break;
  311. }
  312. }
  313. if (this->EncodeRemoteReference) {
  314. cmDirectoryId const dirId = this->Makefile.GetDirectoryId();
  315. this->DirectoryId = cmStrCat(CMAKE_DIRECTORY_ID_SEP, dirId.String);
  316. }
  317. }
  318. bool TLL::HandleLibrary(ProcessingState currentProcessingState,
  319. const std::string& lib, cmTargetLinkLibraryType llt)
  320. {
  321. if (this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
  322. currentProcessingState != ProcessingKeywordLinkInterface) {
  323. this->Makefile.IssueMessage(
  324. MessageType::FATAL_ERROR,
  325. "INTERFACE library can only be used with the INTERFACE keyword of "
  326. "target_link_libraries");
  327. return false;
  328. }
  329. if (this->Target->IsImported() &&
  330. currentProcessingState != ProcessingKeywordLinkInterface) {
  331. this->Makefile.IssueMessage(
  332. MessageType::FATAL_ERROR,
  333. "IMPORTED library can only be used with the INTERFACE keyword of "
  334. "target_link_libraries");
  335. return false;
  336. }
  337. cmTarget::TLLSignature sig =
  338. (currentProcessingState == ProcessingPlainPrivateInterface ||
  339. currentProcessingState == ProcessingPlainPublicInterface ||
  340. currentProcessingState == ProcessingKeywordPrivateInterface ||
  341. currentProcessingState == ProcessingKeywordPublicInterface ||
  342. currentProcessingState == ProcessingKeywordLinkInterface)
  343. ? cmTarget::KeywordTLLSignature
  344. : cmTarget::PlainTLLSignature;
  345. if (!this->Target->PushTLLCommandTrace(
  346. sig, this->Makefile.GetBacktrace().Top())) {
  347. std::ostringstream e;
  348. // If the sig is a keyword form and there is a conflict, the existing
  349. // form must be the plain form.
  350. const char* existingSig =
  351. (sig == cmTarget::KeywordTLLSignature ? "plain" : "keyword");
  352. e << "The " << existingSig
  353. << " signature for target_link_libraries has "
  354. "already been used with the target \""
  355. << this->Target->GetName()
  356. << "\". All uses of target_link_libraries with a target must "
  357. << " be either all-keyword or all-plain.\n";
  358. this->Target->GetTllSignatureTraces(e,
  359. sig == cmTarget::KeywordTLLSignature
  360. ? cmTarget::PlainTLLSignature
  361. : cmTarget::KeywordTLLSignature);
  362. this->Makefile.IssueMessage(MessageType::FATAL_ERROR, e.str());
  363. return false;
  364. }
  365. // Handle normal case where the command was called with another keyword than
  366. // INTERFACE / LINK_INTERFACE_LIBRARIES or none at all. (The "LINK_LIBRARIES"
  367. // property of the target on the LHS shall be populated.)
  368. if (currentProcessingState != ProcessingKeywordLinkInterface &&
  369. currentProcessingState != ProcessingPlainLinkInterface) {
  370. if (this->RejectRemoteLinking) {
  371. this->Makefile.IssueMessage(
  372. MessageType::FATAL_ERROR,
  373. cmStrCat("Attempt to add link library \"", lib, "\" to target \"",
  374. this->Target->GetName(),
  375. "\" which is not built in this "
  376. "directory.\nThis is allowed only when policy CMP0079 "
  377. "is set to NEW."));
  378. return false;
  379. }
  380. cmTarget* tgt = this->Makefile.GetGlobalGenerator()->FindTarget(lib);
  381. if (tgt && (tgt->GetType() != cmStateEnums::STATIC_LIBRARY) &&
  382. (tgt->GetType() != cmStateEnums::SHARED_LIBRARY) &&
  383. (tgt->GetType() != cmStateEnums::UNKNOWN_LIBRARY) &&
  384. (tgt->GetType() != cmStateEnums::OBJECT_LIBRARY) &&
  385. (tgt->GetType() != cmStateEnums::INTERFACE_LIBRARY) &&
  386. !tgt->IsExecutableWithExports()) {
  387. this->Makefile.IssueMessage(
  388. MessageType::FATAL_ERROR,
  389. cmStrCat(
  390. "Target \"", lib, "\" of type ",
  391. cmState::GetTargetTypeName(tgt->GetType()),
  392. " may not be linked into another target. One may link only to "
  393. "INTERFACE, OBJECT, STATIC or SHARED libraries, or to ",
  394. "executables with the ENABLE_EXPORTS property set."));
  395. }
  396. this->AffectsProperty("LINK_LIBRARIES");
  397. this->Target->AddLinkLibrary(this->Makefile, lib, llt);
  398. }
  399. if (this->WarnRemoteInterface) {
  400. this->Makefile.IssueMessage(
  401. MessageType::AUTHOR_WARNING,
  402. cmStrCat(
  403. cmPolicies::GetPolicyWarning(cmPolicies::CMP0079), "\nTarget\n ",
  404. this->Target->GetName(),
  405. "\nis not created in this "
  406. "directory. For compatibility with older versions of CMake, link "
  407. "library\n ",
  408. lib,
  409. "\nwill be looked up in the directory in which "
  410. "the target was created rather than in this calling directory."));
  411. }
  412. // Handle (additional) case where the command was called with PRIVATE /
  413. // LINK_PRIVATE and stop its processing. (The "INTERFACE_LINK_LIBRARIES"
  414. // property of the target on the LHS shall only be populated if it is a
  415. // STATIC library.)
  416. if (currentProcessingState == ProcessingKeywordPrivateInterface ||
  417. currentProcessingState == ProcessingPlainPrivateInterface) {
  418. if (this->Target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  419. this->Target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  420. // TODO: Detect and no-op `$<COMPILE_ONLY>` genexes here.
  421. std::string configLib =
  422. this->Target->GetDebugGeneratorExpressions(lib, llt);
  423. if (cmGeneratorExpression::IsValidTargetName(lib) ||
  424. cmGeneratorExpression::Find(lib) != std::string::npos) {
  425. configLib = "$<LINK_ONLY:" + configLib + ">";
  426. }
  427. this->AppendProperty("INTERFACE_LINK_LIBRARIES", configLib);
  428. }
  429. return true;
  430. }
  431. // Handle general case where the command was called with another keyword than
  432. // PRIVATE / LINK_PRIVATE or none at all. (The "INTERFACE_LINK_LIBRARIES"
  433. // property of the target on the LHS shall be populated.)
  434. this->AppendProperty("INTERFACE_LINK_LIBRARIES",
  435. this->Target->GetDebugGeneratorExpressions(lib, llt));
  436. return true;
  437. }
  438. void TLL::AppendProperty(std::string const& prop, std::string const& value)
  439. {
  440. this->AffectsProperty(prop);
  441. this->Target->AppendProperty(prop, value, this->Makefile.GetBacktrace());
  442. }
  443. void TLL::AffectsProperty(std::string const& prop)
  444. {
  445. if (!this->EncodeRemoteReference) {
  446. return;
  447. }
  448. // Add a wrapper to the expression to tell LookupLinkItem to look up
  449. // names in the caller's directory.
  450. if (this->Props.insert(prop).second) {
  451. this->Target->AppendProperty(prop, this->DirectoryId,
  452. this->Makefile.GetBacktrace());
  453. }
  454. }
  455. TLL::~TLL()
  456. {
  457. for (std::string const& prop : this->Props) {
  458. this->Target->AppendProperty(prop, CMAKE_DIRECTORY_ID_SEP,
  459. this->Makefile.GetBacktrace());
  460. }
  461. }
  462. } // namespace