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 "cmTargetPropCommandBase.h"
5 #include "cmExecutionStatus.h"
6 #include "cmGlobalGenerator.h"
7 #include "cmMakefile.h"
8 #include "cmStateTypes.h"
13 cmTargetPropCommandBase::cmTargetPropCommandBase(cmExecutionStatus& status)
14 : Makefile(&status.GetMakefile())
19 void cmTargetPropCommandBase::SetError(std::string const& e)
21 this->Status.SetError(e);
24 bool cmTargetPropCommandBase::HandleArguments(
25 std::vector<std::string> const& args, const std::string& prop,
28 if (args.size() < 2) {
29 this->SetError("called with incorrect number of arguments");
33 if (this->Makefile->IsAlias(args[0])) {
34 this->SetError("can not be used on an ALIAS target.");
37 // Lookup the target for which property-values are specified.
39 this->Makefile->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(
42 this->Target = this->Makefile->FindTargetToUse(args[0]);
45 this->HandleMissingTarget(args[0]);
48 const bool isRegularTarget =
49 (this->Target->GetType() == cmStateEnums::EXECUTABLE) ||
50 (this->Target->GetType() == cmStateEnums::STATIC_LIBRARY) ||
51 (this->Target->GetType() == cmStateEnums::SHARED_LIBRARY) ||
52 (this->Target->GetType() == cmStateEnums::MODULE_LIBRARY) ||
53 (this->Target->GetType() == cmStateEnums::OBJECT_LIBRARY) ||
54 (this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY) ||
55 (this->Target->GetType() == cmStateEnums::UNKNOWN_LIBRARY);
56 const bool isCustomTarget = this->Target->GetType() == cmStateEnums::UTILITY;
58 if (prop == "SOURCES") {
59 if (!isRegularTarget && !isCustomTarget) {
60 this->SetError("called with non-compilable target type");
64 if (!isRegularTarget) {
65 this->SetError("called with non-compilable target type");
71 unsigned int argIndex = 1;
73 if ((flags & PROCESS_SYSTEM) && args[argIndex] == "SYSTEM") {
74 if (args.size() < 3) {
75 this->SetError("called with incorrect number of arguments");
83 if ((flags & PROCESS_BEFORE) && args[argIndex] == "BEFORE") {
84 if (args.size() < 3) {
85 this->SetError("called with incorrect number of arguments");
90 } else if ((flags & PROCESS_AFTER) && args[argIndex] == "AFTER") {
91 if (args.size() < 3) {
92 this->SetError("called with incorrect number of arguments");
99 if ((flags & PROCESS_REUSE_FROM) && args[argIndex] == "REUSE_FROM") {
100 if (args.size() != 3) {
101 this->SetError("called with incorrect number of arguments");
106 this->Target->SetProperty("PRECOMPILE_HEADERS_REUSE_FROM", args[argIndex]);
110 this->Property = prop;
112 while (argIndex < args.size()) {
113 if (!this->ProcessContentArgs(args, argIndex, prepend, system)) {
120 bool cmTargetPropCommandBase::ProcessContentArgs(
121 std::vector<std::string> const& args, unsigned int& argIndex, bool prepend,
124 std::string const& scope = args[argIndex];
126 if (scope != "PUBLIC" && scope != "PRIVATE" && scope != "INTERFACE") {
127 this->SetError("called with invalid arguments");
133 std::vector<std::string> content;
135 for (unsigned int i = argIndex; i < args.size(); ++i, ++argIndex) {
136 if (args[i] == "PUBLIC" || args[i] == "PRIVATE" ||
137 args[i] == "INTERFACE") {
140 content.push_back(args[i]);
142 if (!content.empty()) {
143 if (this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
144 scope != "INTERFACE" && this->Property != "SOURCES") {
145 this->SetError("may only set INTERFACE properties on INTERFACE targets");
148 if (this->Target->IsImported() && scope != "INTERFACE") {
149 this->SetError("may only set INTERFACE properties on IMPORTED targets");
152 if (this->Target->GetType() == cmStateEnums::UTILITY &&
153 scope != "PRIVATE") {
154 this->SetError("may only set PRIVATE properties on custom targets");
158 return this->PopulateTargetProperties(scope, content, prepend, system);
161 bool cmTargetPropCommandBase::PopulateTargetProperties(
162 const std::string& scope, const std::vector<std::string>& content,
163 bool prepend, bool system)
165 if (content.empty()) {
168 if (scope == "PRIVATE" || scope == "PUBLIC") {
169 if (!this->HandleDirectContent(this->Target, content, prepend, system)) {
173 if (scope == "INTERFACE" || scope == "PUBLIC") {
174 this->HandleInterfaceContent(this->Target, content, prepend, system);
179 void cmTargetPropCommandBase::HandleInterfaceContent(
180 cmTarget* tgt, const std::vector<std::string>& content, bool prepend, bool)
183 const std::string propName = std::string("INTERFACE_") + this->Property;
184 cmValue propValue = tgt->GetProperty(propName);
185 const std::string totalContent =
186 this->Join(content) + (propValue ? (";" + *propValue) : std::string());
187 tgt->SetProperty(propName, totalContent);
189 tgt->AppendProperty("INTERFACE_" + this->Property, this->Join(content));