cmTargetCompileDefinitionsCommand.cxx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "cmTargetCompileDefinitionsCommand.h"
  4. #include "cmMakefile.h"
  5. #include "cmMessageType.h"
  6. #include "cmStringAlgorithms.h"
  7. #include "cmTarget.h"
  8. #include "cmTargetPropCommandBase.h"
  9. namespace {
  10. class TargetCompileDefinitionsImpl : public cmTargetPropCommandBase
  11. {
  12. public:
  13. using cmTargetPropCommandBase::cmTargetPropCommandBase;
  14. private:
  15. void HandleMissingTarget(const std::string& name) override
  16. {
  17. this->Makefile->IssueMessage(
  18. MessageType::FATAL_ERROR,
  19. cmStrCat("Cannot specify compile definitions for target \"", name,
  20. "\" which is not built by this project."));
  21. }
  22. bool HandleDirectContent(cmTarget* tgt,
  23. const std::vector<std::string>& content,
  24. bool /*prepend*/, bool /*system*/) override
  25. {
  26. tgt->AppendProperty("COMPILE_DEFINITIONS", this->Join(content).c_str());
  27. return true; // Successfully handled.
  28. }
  29. std::string Join(const std::vector<std::string>& content) override
  30. {
  31. std::string defs;
  32. std::string sep;
  33. for (std::string const& it : content) {
  34. if (cmHasLiteralPrefix(it, "-D")) {
  35. defs += sep + it.substr(2);
  36. } else {
  37. defs += sep + it;
  38. }
  39. sep = ";";
  40. }
  41. return defs;
  42. }
  43. };
  44. } // namespace
  45. bool cmTargetCompileDefinitionsCommand(std::vector<std::string> const& args,
  46. cmExecutionStatus& status)
  47. {
  48. return TargetCompileDefinitionsImpl(status).HandleArguments(
  49. args, "COMPILE_DEFINITIONS");
  50. }