cmTargetLinkLibrariesCommand.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmTargetLinkLibrariesCommand.h"
  11. #include "cmGeneratorExpression.h"
  12. const char* cmTargetLinkLibrariesCommand::LinkLibraryTypeNames[3] =
  13. {
  14. "general",
  15. "debug",
  16. "optimized"
  17. };
  18. // cmTargetLinkLibrariesCommand
  19. bool cmTargetLinkLibrariesCommand
  20. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  21. {
  22. // must have one argument
  23. if(args.size() < 1)
  24. {
  25. this->SetError("called with incorrect number of arguments");
  26. return false;
  27. }
  28. if (this->Makefile->IsAlias(args[0]))
  29. {
  30. this->SetError("can not be used on an ALIAS target.");
  31. return false;
  32. }
  33. // Lookup the target for which libraries are specified.
  34. this->Target =
  35. this->Makefile->GetCMakeInstance()
  36. ->GetGlobalGenerator()->FindTarget(args[0]);
  37. if(!this->Target)
  38. {
  39. cmake::MessageType t = cmake::FATAL_ERROR; // fail by default
  40. std::ostringstream e;
  41. e << "Cannot specify link libraries for target \"" << args[0] << "\" "
  42. << "which is not built by this project.";
  43. // The bad target is the only argument. Check how policy CMP0016 is set,
  44. // and accept, warn or fail respectively:
  45. if (args.size() < 2)
  46. {
  47. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0016))
  48. {
  49. case cmPolicies::WARN:
  50. t = cmake::AUTHOR_WARNING;
  51. // Print the warning.
  52. e << "\n"
  53. << "CMake does not support this but it used to work accidentally "
  54. << "and is being allowed for compatibility."
  55. << "\n" << cmPolicies::GetPolicyWarning(cmPolicies::CMP0016);
  56. break;
  57. case cmPolicies::OLD: // OLD behavior does not warn.
  58. t = cmake::MESSAGE;
  59. break;
  60. case cmPolicies::REQUIRED_IF_USED:
  61. case cmPolicies::REQUIRED_ALWAYS:
  62. e << "\n" << cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0016);
  63. break;
  64. case cmPolicies::NEW: // NEW behavior prints the error.
  65. break;
  66. }
  67. }
  68. // now actually print the message
  69. switch(t)
  70. {
  71. case cmake::AUTHOR_WARNING:
  72. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, e.str());
  73. break;
  74. case cmake::FATAL_ERROR:
  75. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  76. cmSystemTools::SetFatalErrorOccured();
  77. break;
  78. default:
  79. break;
  80. }
  81. return true;
  82. }
  83. if(this->Target->GetType() == cmState::OBJECT_LIBRARY)
  84. {
  85. std::ostringstream e;
  86. e << "Object library target \"" << args[0] << "\" "
  87. << "may not link to anything.";
  88. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  89. cmSystemTools::SetFatalErrorOccured();
  90. return true;
  91. }
  92. if (this->Target->GetType() == cmState::UTILITY)
  93. {
  94. std::ostringstream e;
  95. const char *modal = 0;
  96. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  97. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0039))
  98. {
  99. case cmPolicies::WARN:
  100. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0039) << "\n";
  101. modal = "should";
  102. case cmPolicies::OLD:
  103. break;
  104. case cmPolicies::REQUIRED_ALWAYS:
  105. case cmPolicies::REQUIRED_IF_USED:
  106. case cmPolicies::NEW:
  107. modal = "must";
  108. messageType = cmake::FATAL_ERROR;
  109. }
  110. if (modal)
  111. {
  112. e <<
  113. "Utility target \"" << this->Target->GetName() << "\" " << modal
  114. << " not be used as the target of a target_link_libraries call.";
  115. this->Makefile->IssueMessage(messageType, e.str());
  116. if(messageType == cmake::FATAL_ERROR)
  117. {
  118. return false;
  119. }
  120. }
  121. }
  122. // but we might not have any libs after variable expansion
  123. if(args.size() < 2)
  124. {
  125. return true;
  126. }
  127. // Keep track of link configuration specifiers.
  128. cmTargetLinkLibraryType llt = GENERAL_LibraryType;
  129. bool haveLLT = false;
  130. // Start with primary linking and switch to link interface
  131. // specification if the keyword is encountered as the first argument.
  132. this->CurrentProcessingState = ProcessingLinkLibraries;
  133. // add libraries, note that there is an optional prefix
  134. // of debug and optimized that can be used
  135. for(unsigned int i=1; i < args.size(); ++i)
  136. {
  137. if(args[i] == "LINK_INTERFACE_LIBRARIES")
  138. {
  139. this->CurrentProcessingState = ProcessingPlainLinkInterface;
  140. if(i != 1)
  141. {
  142. this->Makefile->IssueMessage(
  143. cmake::FATAL_ERROR,
  144. "The LINK_INTERFACE_LIBRARIES option must appear as the second "
  145. "argument, just after the target name."
  146. );
  147. return true;
  148. }
  149. }
  150. else if(args[i] == "INTERFACE")
  151. {
  152. if(i != 1
  153. && this->CurrentProcessingState != ProcessingKeywordPrivateInterface
  154. && this->CurrentProcessingState != ProcessingKeywordPublicInterface
  155. && this->CurrentProcessingState != ProcessingKeywordLinkInterface)
  156. {
  157. this->Makefile->IssueMessage(
  158. cmake::FATAL_ERROR,
  159. "The INTERFACE option must appear as the second "
  160. "argument, just after the target name."
  161. );
  162. return true;
  163. }
  164. this->CurrentProcessingState = ProcessingKeywordLinkInterface;
  165. }
  166. else if(args[i] == "LINK_PUBLIC")
  167. {
  168. if(i != 1
  169. && this->CurrentProcessingState != ProcessingPlainPrivateInterface
  170. && this->CurrentProcessingState != ProcessingPlainPublicInterface)
  171. {
  172. this->Makefile->IssueMessage(
  173. cmake::FATAL_ERROR,
  174. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the second "
  175. "argument, just after the target name."
  176. );
  177. return true;
  178. }
  179. this->CurrentProcessingState = ProcessingPlainPublicInterface;
  180. }
  181. else if(args[i] == "PUBLIC")
  182. {
  183. if(i != 1
  184. && this->CurrentProcessingState != ProcessingKeywordPrivateInterface
  185. && this->CurrentProcessingState != ProcessingKeywordPublicInterface
  186. && this->CurrentProcessingState != ProcessingKeywordLinkInterface)
  187. {
  188. this->Makefile->IssueMessage(
  189. cmake::FATAL_ERROR,
  190. "The PUBLIC or PRIVATE option must appear as the second "
  191. "argument, just after the target name."
  192. );
  193. return true;
  194. }
  195. this->CurrentProcessingState = ProcessingKeywordPublicInterface;
  196. }
  197. else if(args[i] == "LINK_PRIVATE")
  198. {
  199. if(i != 1
  200. && this->CurrentProcessingState != ProcessingPlainPublicInterface
  201. && this->CurrentProcessingState != ProcessingPlainPrivateInterface)
  202. {
  203. this->Makefile->IssueMessage(
  204. cmake::FATAL_ERROR,
  205. "The LINK_PUBLIC or LINK_PRIVATE option must appear as the second "
  206. "argument, just after the target name."
  207. );
  208. return true;
  209. }
  210. this->CurrentProcessingState = ProcessingPlainPrivateInterface;
  211. }
  212. else if(args[i] == "PRIVATE")
  213. {
  214. if(i != 1
  215. && this->CurrentProcessingState != ProcessingKeywordPrivateInterface
  216. && this->CurrentProcessingState != ProcessingKeywordPublicInterface
  217. && this->CurrentProcessingState != ProcessingKeywordLinkInterface)
  218. {
  219. this->Makefile->IssueMessage(
  220. cmake::FATAL_ERROR,
  221. "The PUBLIC or PRIVATE option must appear as the second "
  222. "argument, just after the target name."
  223. );
  224. return true;
  225. }
  226. this->CurrentProcessingState = ProcessingKeywordPrivateInterface;
  227. }
  228. else if(args[i] == "debug")
  229. {
  230. if(haveLLT)
  231. {
  232. this->LinkLibraryTypeSpecifierWarning(llt, DEBUG_LibraryType);
  233. }
  234. llt = DEBUG_LibraryType;
  235. haveLLT = true;
  236. }
  237. else if(args[i] == "optimized")
  238. {
  239. if(haveLLT)
  240. {
  241. this->LinkLibraryTypeSpecifierWarning(llt, OPTIMIZED_LibraryType);
  242. }
  243. llt = OPTIMIZED_LibraryType;
  244. haveLLT = true;
  245. }
  246. else if(args[i] == "general")
  247. {
  248. if(haveLLT)
  249. {
  250. this->LinkLibraryTypeSpecifierWarning(llt, GENERAL_LibraryType);
  251. }
  252. llt = GENERAL_LibraryType;
  253. haveLLT = true;
  254. }
  255. else if(haveLLT)
  256. {
  257. // The link type was specified by the previous argument.
  258. haveLLT = false;
  259. if (!this->HandleLibrary(args[i], llt))
  260. {
  261. return false;
  262. }
  263. }
  264. else
  265. {
  266. // Lookup old-style cache entry if type is unspecified. So if you
  267. // do a target_link_libraries(foo optimized bar) it will stay optimized
  268. // and not use the lookup. As there maybe the case where someone has
  269. // specifed that a library is both debug and optimized. (this check is
  270. // only there for backwards compatibility when mixing projects built
  271. // with old versions of CMake and new)
  272. llt = GENERAL_LibraryType;
  273. std::string linkType = args[0];
  274. linkType += "_LINK_TYPE";
  275. const char* linkTypeString =
  276. this->Makefile->GetDefinition( linkType );
  277. if(linkTypeString)
  278. {
  279. if(strcmp(linkTypeString, "debug") == 0)
  280. {
  281. llt = DEBUG_LibraryType;
  282. }
  283. if(strcmp(linkTypeString, "optimized") == 0)
  284. {
  285. llt = OPTIMIZED_LibraryType;
  286. }
  287. }
  288. if (!this->HandleLibrary(args[i], llt))
  289. {
  290. return false;
  291. }
  292. }
  293. }
  294. // Make sure the last argument was not a library type specifier.
  295. if(haveLLT)
  296. {
  297. std::ostringstream e;
  298. e << "The \"" << this->LinkLibraryTypeNames[llt]
  299. << "\" argument must be followed by a library.";
  300. this->Makefile->IssueMessage(cmake::FATAL_ERROR, e.str());
  301. cmSystemTools::SetFatalErrorOccured();
  302. }
  303. const cmPolicies::PolicyStatus policy22Status
  304. = this->Target->GetPolicyStatusCMP0022();
  305. // If any of the LINK_ options were given, make sure the
  306. // LINK_INTERFACE_LIBRARIES target property exists.
  307. // Use of any of the new keywords implies awareness of
  308. // this property. And if no libraries are named, it should
  309. // result in an empty link interface.
  310. if((policy22Status == cmPolicies::OLD ||
  311. policy22Status == cmPolicies::WARN) &&
  312. this->CurrentProcessingState != ProcessingLinkLibraries &&
  313. !this->Target->GetProperty("LINK_INTERFACE_LIBRARIES"))
  314. {
  315. this->Target->SetProperty("LINK_INTERFACE_LIBRARIES", "");
  316. }
  317. return true;
  318. }
  319. //----------------------------------------------------------------------------
  320. void
  321. cmTargetLinkLibrariesCommand
  322. ::LinkLibraryTypeSpecifierWarning(int left, int right)
  323. {
  324. std::ostringstream w;
  325. w << "Link library type specifier \""
  326. << this->LinkLibraryTypeNames[left] << "\" is followed by specifier \""
  327. << this->LinkLibraryTypeNames[right] << "\" instead of a library name. "
  328. << "The first specifier will be ignored.";
  329. this->Makefile->IssueMessage(cmake::AUTHOR_WARNING, w.str());
  330. }
  331. //----------------------------------------------------------------------------
  332. bool
  333. cmTargetLinkLibrariesCommand::HandleLibrary(const std::string& lib,
  334. cmTargetLinkLibraryType llt)
  335. {
  336. if(this->Target->GetType() == cmState::INTERFACE_LIBRARY
  337. && this->CurrentProcessingState != ProcessingKeywordLinkInterface)
  338. {
  339. this->Makefile->IssueMessage(cmake::FATAL_ERROR,
  340. "INTERFACE library can only be used with the INTERFACE keyword of "
  341. "target_link_libraries");
  342. return false;
  343. }
  344. cmTarget::TLLSignature sig =
  345. (this->CurrentProcessingState == ProcessingPlainPrivateInterface
  346. || this->CurrentProcessingState == ProcessingPlainPublicInterface
  347. || this->CurrentProcessingState == ProcessingKeywordPrivateInterface
  348. || this->CurrentProcessingState == ProcessingKeywordPublicInterface
  349. || this->CurrentProcessingState == ProcessingKeywordLinkInterface)
  350. ? cmTarget::KeywordTLLSignature : cmTarget::PlainTLLSignature;
  351. if (!this->Target->PushTLLCommandTrace(
  352. sig, this->Makefile->GetExecutionContext()))
  353. {
  354. std::ostringstream e;
  355. const char *modal = 0;
  356. cmake::MessageType messageType = cmake::AUTHOR_WARNING;
  357. switch(this->Makefile->GetPolicyStatus(cmPolicies::CMP0023))
  358. {
  359. case cmPolicies::WARN:
  360. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0023) << "\n";
  361. modal = "should";
  362. case cmPolicies::OLD:
  363. break;
  364. case cmPolicies::REQUIRED_ALWAYS:
  365. case cmPolicies::REQUIRED_IF_USED:
  366. case cmPolicies::NEW:
  367. modal = "must";
  368. messageType = cmake::FATAL_ERROR;
  369. }
  370. if(modal)
  371. {
  372. // If the sig is a keyword form and there is a conflict, the existing
  373. // form must be the plain form.
  374. const char *existingSig
  375. = (sig == cmTarget::KeywordTLLSignature ? "plain"
  376. : "keyword");
  377. e <<
  378. "The " << existingSig << " signature for target_link_libraries "
  379. "has already been used with the target \""
  380. << this->Target->GetName() << "\". All uses of "
  381. "target_link_libraries with a target " << modal << " be either "
  382. "all-keyword or all-plain.\n";
  383. this->Target->GetTllSignatureTraces(e,
  384. sig == cmTarget::KeywordTLLSignature
  385. ? cmTarget::PlainTLLSignature
  386. : cmTarget::KeywordTLLSignature);
  387. this->Makefile->IssueMessage(messageType, e.str());
  388. if(messageType == cmake::FATAL_ERROR)
  389. {
  390. return false;
  391. }
  392. }
  393. }
  394. // Handle normal case first.
  395. if(this->CurrentProcessingState != ProcessingKeywordLinkInterface
  396. && this->CurrentProcessingState != ProcessingPlainLinkInterface)
  397. {
  398. this->Makefile
  399. ->AddLinkLibraryForTarget(this->Target->GetName(), lib, llt);
  400. if(this->CurrentProcessingState == ProcessingLinkLibraries)
  401. {
  402. this->Target->AppendProperty("INTERFACE_LINK_LIBRARIES",
  403. this->Target->GetDebugGeneratorExpressions(lib, llt).c_str());
  404. return true;
  405. }
  406. else if(this->CurrentProcessingState != ProcessingKeywordPublicInterface
  407. && this->CurrentProcessingState != ProcessingPlainPublicInterface)
  408. {
  409. if (this->Target->GetType() == cmState::STATIC_LIBRARY)
  410. {
  411. std::string configLib = this->Target
  412. ->GetDebugGeneratorExpressions(lib, llt);
  413. if (cmGeneratorExpression::IsValidTargetName(lib)
  414. || cmGeneratorExpression::Find(lib) != std::string::npos)
  415. {
  416. configLib = "$<LINK_ONLY:" + configLib + ">";
  417. }
  418. this->Target->AppendProperty("INTERFACE_LINK_LIBRARIES",
  419. configLib.c_str());
  420. }
  421. // Not a 'public' or 'interface' library. Do not add to interface
  422. // property.
  423. return true;
  424. }
  425. }
  426. this->Target->AppendProperty("INTERFACE_LINK_LIBRARIES",
  427. this->Target->GetDebugGeneratorExpressions(lib, llt).c_str());
  428. const cmPolicies::PolicyStatus policy22Status
  429. = this->Target->GetPolicyStatusCMP0022();
  430. if (policy22Status != cmPolicies::OLD
  431. && policy22Status != cmPolicies::WARN)
  432. {
  433. return true;
  434. }
  435. if (this->Target->GetType() == cmState::INTERFACE_LIBRARY)
  436. {
  437. return true;
  438. }
  439. // Get the list of configurations considered to be DEBUG.
  440. std::vector<std::string> debugConfigs =
  441. this->Makefile->GetCMakeInstance()->GetDebugConfigs();
  442. std::string prop;
  443. // Include this library in the link interface for the target.
  444. if(llt == DEBUG_LibraryType || llt == GENERAL_LibraryType)
  445. {
  446. // Put in the DEBUG configuration interfaces.
  447. for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
  448. i != debugConfigs.end(); ++i)
  449. {
  450. prop = "LINK_INTERFACE_LIBRARIES_";
  451. prop += *i;
  452. this->Target->AppendProperty(prop, lib.c_str());
  453. }
  454. }
  455. if(llt == OPTIMIZED_LibraryType || llt == GENERAL_LibraryType)
  456. {
  457. // Put in the non-DEBUG configuration interfaces.
  458. this->Target->AppendProperty("LINK_INTERFACE_LIBRARIES", lib.c_str());
  459. // Make sure the DEBUG configuration interfaces exist so that the
  460. // general one will not be used as a fall-back.
  461. for(std::vector<std::string>::const_iterator i = debugConfigs.begin();
  462. i != debugConfigs.end(); ++i)
  463. {
  464. prop = "LINK_INTERFACE_LIBRARIES_";
  465. prop += *i;
  466. if(!this->Target->GetProperty(prop))
  467. {
  468. this->Target->SetProperty(prop, "");
  469. }
  470. }
  471. }
  472. return true;
  473. }