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