cmTargetLinkLibrariesCommand.cxx 17 KB

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