cmTargetSourcesCommand.cxx 11 KB

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