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