cmTargetLinkLibrariesCommand.cxx 17 KB

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