cmTargetSourcesCommand.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 "cmTargetSourcesCommand.h"
  4. #include <algorithm>
  5. #include <sstream>
  6. #include <utility>
  7. #include <cm/string_view>
  8. #include <cmext/string_view>
  9. #include "cmArgumentParser.h"
  10. #include "cmFileSet.h"
  11. #include "cmGeneratorExpression.h"
  12. #include "cmListFileCache.h"
  13. #include "cmMakefile.h"
  14. #include "cmMessageType.h"
  15. #include "cmPolicies.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. #include "cmTarget.h"
  19. #include "cmTargetPropCommandBase.h"
  20. namespace {
  21. struct FileSetArgs
  22. {
  23. std::string Type;
  24. std::string FileSet;
  25. std::vector<std::string> BaseDirs;
  26. std::vector<std::string> Files;
  27. };
  28. auto const FileSetArgsParser = cmArgumentParser<FileSetArgs>()
  29. .Bind("TYPE"_s, &FileSetArgs::Type)
  30. .Bind("FILE_SET"_s, &FileSetArgs::FileSet)
  31. .Bind("BASE_DIRS"_s, &FileSetArgs::BaseDirs)
  32. .Bind("FILES"_s, &FileSetArgs::Files);
  33. class TargetSourcesImpl : public cmTargetPropCommandBase
  34. {
  35. public:
  36. using cmTargetPropCommandBase::cmTargetPropCommandBase;
  37. protected:
  38. void HandleInterfaceContent(cmTarget* tgt,
  39. const std::vector<std::string>& content,
  40. bool prepend, bool system) override
  41. {
  42. this->cmTargetPropCommandBase::HandleInterfaceContent(
  43. tgt,
  44. this->ConvertToAbsoluteContent(tgt, content, IsInterface::Yes,
  45. CheckCMP0076::Yes),
  46. prepend, system);
  47. }
  48. private:
  49. void HandleMissingTarget(const std::string& name) override
  50. {
  51. this->Makefile->IssueMessage(
  52. MessageType::FATAL_ERROR,
  53. cmStrCat("Cannot specify sources for target \"", name,
  54. "\" which is not built by this project."));
  55. }
  56. bool HandleDirectContent(cmTarget* tgt,
  57. const std::vector<std::string>& content,
  58. bool /*prepend*/, bool /*system*/) override
  59. {
  60. tgt->AppendProperty("SOURCES",
  61. this->Join(this->ConvertToAbsoluteContent(
  62. tgt, content, IsInterface::No, CheckCMP0076::Yes)));
  63. return true; // Successfully handled.
  64. }
  65. bool PopulateTargetProperties(const std::string& scope,
  66. const std::vector<std::string>& content,
  67. bool prepend, bool system) override
  68. {
  69. if (!content.empty() && content.front() == "FILE_SET"_s) {
  70. return this->HandleFileSetMode(scope, content, prepend, system);
  71. }
  72. return this->cmTargetPropCommandBase::PopulateTargetProperties(
  73. scope, content, prepend, system);
  74. }
  75. std::string Join(const std::vector<std::string>& content) override
  76. {
  77. return cmJoin(content, ";");
  78. }
  79. enum class IsInterface
  80. {
  81. Yes,
  82. No,
  83. };
  84. enum class CheckCMP0076
  85. {
  86. Yes,
  87. No,
  88. };
  89. std::vector<std::string> ConvertToAbsoluteContent(
  90. cmTarget* tgt, const std::vector<std::string>& content,
  91. IsInterface isInterfaceContent, CheckCMP0076 checkCmp0076);
  92. bool HandleFileSetMode(const std::string& scope,
  93. const std::vector<std::string>& content, bool prepend,
  94. bool system);
  95. };
  96. std::vector<std::string> TargetSourcesImpl::ConvertToAbsoluteContent(
  97. cmTarget* tgt, const std::vector<std::string>& content,
  98. IsInterface isInterfaceContent, CheckCMP0076 checkCmp0076)
  99. {
  100. // Skip conversion in case old behavior has been explicitly requested
  101. if (checkCmp0076 == CheckCMP0076::Yes &&
  102. this->Makefile->GetPolicyStatus(cmPolicies::CMP0076) ==
  103. cmPolicies::OLD) {
  104. return content;
  105. }
  106. bool changedPath = false;
  107. std::vector<std::string> absoluteContent;
  108. absoluteContent.reserve(content.size());
  109. for (std::string const& src : content) {
  110. std::string absoluteSrc;
  111. if (cmSystemTools::FileIsFullPath(src) ||
  112. cmGeneratorExpression::Find(src) == 0 ||
  113. (isInterfaceContent == IsInterface::No &&
  114. (this->Makefile->GetCurrentSourceDirectory() ==
  115. tgt->GetMakefile()->GetCurrentSourceDirectory()))) {
  116. absoluteSrc = src;
  117. } else {
  118. changedPath = true;
  119. absoluteSrc =
  120. cmStrCat(this->Makefile->GetCurrentSourceDirectory(), '/', src);
  121. }
  122. absoluteContent.push_back(absoluteSrc);
  123. }
  124. if (!changedPath) {
  125. return content;
  126. }
  127. bool issueMessage = true;
  128. bool useAbsoluteContent = false;
  129. std::ostringstream e;
  130. if (checkCmp0076 == CheckCMP0076::Yes) {
  131. switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0076)) {
  132. case cmPolicies::WARN:
  133. e << cmPolicies::GetPolicyWarning(cmPolicies::CMP0076) << "\n";
  134. break;
  135. case cmPolicies::OLD:
  136. issueMessage = false;
  137. break;
  138. case cmPolicies::REQUIRED_ALWAYS:
  139. case cmPolicies::REQUIRED_IF_USED:
  140. this->Makefile->IssueMessage(
  141. MessageType::FATAL_ERROR,
  142. cmPolicies::GetRequiredPolicyError(cmPolicies::CMP0076));
  143. break;
  144. case cmPolicies::NEW: {
  145. issueMessage = false;
  146. useAbsoluteContent = true;
  147. break;
  148. }
  149. }
  150. } else {
  151. issueMessage = false;
  152. useAbsoluteContent = true;
  153. }
  154. if (issueMessage) {
  155. if (isInterfaceContent == IsInterface::Yes) {
  156. e << "An interface source of target \"" << tgt->GetName()
  157. << "\" has a relative path.";
  158. } else {
  159. e << "A private source from a directory other than that of target \""
  160. << tgt->GetName() << "\" has a relative path.";
  161. }
  162. this->Makefile->IssueMessage(MessageType::AUTHOR_WARNING, e.str());
  163. }
  164. return useAbsoluteContent ? absoluteContent : content;
  165. }
  166. bool TargetSourcesImpl::HandleFileSetMode(
  167. const std::string& scope, const std::vector<std::string>& content,
  168. bool /*prepend*/, bool /*system*/)
  169. {
  170. std::vector<std::string> unparsed;
  171. auto args = FileSetArgsParser.Parse(content, &unparsed);
  172. if (!unparsed.empty()) {
  173. this->SetError(
  174. cmStrCat("Unrecognized keyword: \"", unparsed.front(), "\""));
  175. return false;
  176. }
  177. if (args.FileSet.empty()) {
  178. this->SetError("FILE_SET must not be empty");
  179. return false;
  180. }
  181. bool const isDefault = args.Type == args.FileSet ||
  182. (args.Type.empty() && args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z');
  183. std::string type = isDefault ? args.FileSet : args.Type;
  184. auto fileSet = this->Target->GetOrCreateFileSet(args.FileSet, type);
  185. if (fileSet.second) {
  186. if (!isDefault) {
  187. if (args.FileSet[0] >= 'A' && args.FileSet[0] <= 'Z') {
  188. this->SetError(
  189. "Non-default file set name must not start with a capital letter");
  190. return false;
  191. }
  192. }
  193. if (type.empty()) {
  194. this->SetError("Must specify a TYPE when creating file set");
  195. return false;
  196. }
  197. if (type != "HEADERS"_s) {
  198. this->SetError("File set TYPE may only be \"HEADERS\"");
  199. return false;
  200. }
  201. if (args.BaseDirs.empty()) {
  202. args.BaseDirs.emplace_back(this->Makefile->GetCurrentSourceDirectory());
  203. }
  204. if (scope == "PRIVATE"_s || scope == "PUBLIC"_s) {
  205. this->Target->AppendProperty(cmTarget::GetFileSetsPropertyName(type),
  206. args.FileSet);
  207. }
  208. if (scope == "INTERFACE"_s || scope == "PUBLIC"_s) {
  209. this->Target->AppendProperty(
  210. cmTarget::GetInterfaceFileSetsPropertyName(type), args.FileSet);
  211. }
  212. } else {
  213. type = fileSet.first->GetType();
  214. if (!args.Type.empty() && args.Type != type) {
  215. this->SetError(cmStrCat(
  216. "Type \"", args.Type, "\" for file set \"", fileSet.first->GetName(),
  217. "\" does not match original type \"", type, "\""));
  218. return false;
  219. }
  220. std::string existingScope = "PRIVATE";
  221. auto const fileSetsProperty = cmTarget::GetFileSetsPropertyName(type);
  222. auto const interfaceFileSetsProperty =
  223. cmTarget::GetInterfaceFileSetsPropertyName(type);
  224. std::vector<std::string> fileSets;
  225. std::vector<std::string> interfaceFileSets;
  226. cmExpandList(this->Target->GetSafeProperty(fileSetsProperty), fileSets);
  227. cmExpandList(this->Target->GetSafeProperty(interfaceFileSetsProperty),
  228. interfaceFileSets);
  229. if (std::find(interfaceFileSets.begin(), interfaceFileSets.end(),
  230. args.FileSet) != interfaceFileSets.end()) {
  231. existingScope = "INTERFACE";
  232. }
  233. if (std::find(fileSets.begin(), fileSets.end(), args.FileSet) !=
  234. fileSets.end()) {
  235. if (existingScope == "INTERFACE"_s) {
  236. existingScope = "PUBLIC";
  237. }
  238. } else if (existingScope != "INTERFACE"_s) {
  239. this->SetError(cmStrCat("File set \"", args.FileSet, "\" is not in ",
  240. fileSetsProperty, " or ",
  241. interfaceFileSetsProperty));
  242. return false;
  243. }
  244. if (scope != existingScope) {
  245. this->SetError(
  246. cmStrCat("Scope ", scope, " for file set \"", args.FileSet,
  247. "\" does not match original scope ", existingScope));
  248. return false;
  249. }
  250. }
  251. auto files = this->Join(this->ConvertToAbsoluteContent(
  252. this->Target, args.Files, IsInterface::Yes, CheckCMP0076::No));
  253. if (!files.empty()) {
  254. fileSet.first->AddFileEntry(
  255. BT<std::string>(files, this->Makefile->GetBacktrace()));
  256. }
  257. auto baseDirectories = this->Join(this->ConvertToAbsoluteContent(
  258. this->Target, args.BaseDirs, IsInterface::Yes, CheckCMP0076::No));
  259. if (!baseDirectories.empty()) {
  260. fileSet.first->AddDirectoryEntry(
  261. BT<std::string>(baseDirectories, this->Makefile->GetBacktrace()));
  262. if (type == "HEADERS"_s) {
  263. for (auto const& dir : cmExpandedList(baseDirectories)) {
  264. auto interfaceDirectoriesGenex =
  265. cmStrCat("$<BUILD_INTERFACE:", dir, ">");
  266. if (scope == "PRIVATE"_s || scope == "PUBLIC"_s) {
  267. this->Target->AppendProperty("INCLUDE_DIRECTORIES",
  268. interfaceDirectoriesGenex);
  269. }
  270. if (scope == "INTERFACE"_s || scope == "PUBLIC"_s) {
  271. this->Target->AppendProperty("INTERFACE_INCLUDE_DIRECTORIES",
  272. interfaceDirectoriesGenex);
  273. }
  274. }
  275. }
  276. }
  277. return true;
  278. }
  279. } // namespace
  280. bool cmTargetSourcesCommand(std::vector<std::string> const& args,
  281. cmExecutionStatus& status)
  282. {
  283. return TargetSourcesImpl(status).HandleArguments(args, "SOURCES");
  284. }