/src/CMake/Source/cmTargetCompileDefinitionsCommand.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmTargetCompileDefinitionsCommand.h" |
4 | | |
5 | | #include <utility> |
6 | | |
7 | | #include <cm/optional> |
8 | | |
9 | | #include "cmListFileCache.h" |
10 | | #include "cmMakefile.h" |
11 | | #include "cmMessageType.h" |
12 | | #include "cmStringAlgorithms.h" |
13 | | #include "cmTarget.h" |
14 | | #include "cmTargetPropCommandBase.h" |
15 | | |
16 | | namespace { |
17 | | |
18 | | class TargetCompileDefinitionsImpl : public cmTargetPropCommandBase |
19 | | { |
20 | | public: |
21 | | using cmTargetPropCommandBase::cmTargetPropCommandBase; |
22 | | |
23 | | private: |
24 | | void HandleMissingTarget(std::string const& name) override |
25 | 0 | { |
26 | 0 | this->Makefile->IssueMessage( |
27 | 0 | MessageType::FATAL_ERROR, |
28 | 0 | cmStrCat("Cannot specify compile definitions for target \"", name, |
29 | 0 | "\" which is not built by this project.")); |
30 | 0 | } |
31 | | |
32 | | bool HandleDirectContent(cmTarget* tgt, |
33 | | std::vector<std::string> const& content, |
34 | | bool /*prepend*/, bool /*system*/) override |
35 | 5.92k | { |
36 | 5.92k | tgt->AppendProperty("COMPILE_DEFINITIONS", this->Join(content), |
37 | 5.92k | this->Makefile->GetBacktrace()); |
38 | 5.92k | return true; // Successfully handled. |
39 | 5.92k | } |
40 | | |
41 | | std::string Join(std::vector<std::string> const& content) override |
42 | 11.8k | { |
43 | 11.8k | std::string defs; |
44 | 11.8k | std::string sep; |
45 | 23.7k | for (std::string const& it : content) { |
46 | 23.7k | if (cmHasLiteralPrefix(it, "-D")) { |
47 | 0 | defs = cmStrCat(std::move(defs), sep, it.substr(2)); |
48 | 23.7k | } else { |
49 | 23.7k | defs = cmStrCat(std::move(defs), sep, it); |
50 | 23.7k | } |
51 | 23.7k | sep = ";"; |
52 | 23.7k | } |
53 | 11.8k | return defs; |
54 | 11.8k | } |
55 | | }; |
56 | | |
57 | | } // namespace |
58 | | |
59 | | bool cmTargetCompileDefinitionsCommand(std::vector<std::string> const& args, |
60 | | cmExecutionStatus& status) |
61 | 11.8k | { |
62 | 11.8k | return TargetCompileDefinitionsImpl(status).HandleArguments( |
63 | 11.8k | args, "COMPILE_DEFINITIONS"); |
64 | 11.8k | } |