cmake/Source/cmTargetPropCommandBase.cxx

192 lines
5.6 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2013-03-16 19:13:01 +02:00
#include "cmTargetPropCommandBase.h"
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2013-03-16 19:13:01 +02:00
#include "cmGlobalGenerator.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmStateTypes.h"
#include "cmTarget.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2017-04-14 19:02:05 +02:00
#include "cmake.h"
2013-03-16 19:13:01 +02:00
2020-02-01 23:06:01 +01:00
cmTargetPropCommandBase::cmTargetPropCommandBase(cmExecutionStatus& status)
: Makefile(&status.GetMakefile())
, Status(status)
{
}
void cmTargetPropCommandBase::SetError(std::string const& e)
{
this->Status.SetError(e);
}
2016-07-09 11:21:54 +02:00
bool cmTargetPropCommandBase::HandleArguments(
std::vector<std::string> const& args, const std::string& prop,
2023-05-23 16:38:00 +02:00
unsigned int flags)
2013-03-16 19:13:01 +02:00
{
2016-07-09 11:21:54 +02:00
if (args.size() < 2) {
2013-03-16 19:13:01 +02:00
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (this->Makefile->IsAlias(args[0])) {
2013-11-03 12:27:13 +02:00
this->SetError("can not be used on an ALIAS target.");
return false;
2016-07-09 11:21:54 +02:00
}
2018-04-23 21:13:27 +02:00
// Lookup the target for which property-values are specified.
2013-03-16 19:13:01 +02:00
this->Target =
2016-07-09 11:21:54 +02:00
this->Makefile->GetCMakeInstance()->GetGlobalGenerator()->FindTarget(
args[0]);
if (!this->Target) {
2014-08-03 19:52:23 +02:00
this->Target = this->Makefile->FindTargetToUse(args[0]);
2016-07-09 11:21:54 +02:00
}
if (!this->Target) {
2013-03-16 19:13:01 +02:00
this->HandleMissingTarget(args[0]);
return false;
2016-07-09 11:21:54 +02:00
}
2021-09-14 00:13:48 +02:00
const bool isRegularTarget =
(this->Target->GetType() == cmStateEnums::EXECUTABLE) ||
(this->Target->GetType() == cmStateEnums::STATIC_LIBRARY) ||
(this->Target->GetType() == cmStateEnums::SHARED_LIBRARY) ||
(this->Target->GetType() == cmStateEnums::MODULE_LIBRARY) ||
(this->Target->GetType() == cmStateEnums::OBJECT_LIBRARY) ||
(this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY) ||
(this->Target->GetType() == cmStateEnums::UNKNOWN_LIBRARY);
const bool isCustomTarget = this->Target->GetType() == cmStateEnums::UTILITY;
if (prop == "SOURCES") {
if (!isRegularTarget && !isCustomTarget) {
this->SetError("called with non-compilable target type");
return false;
}
} else {
if (!isRegularTarget) {
this->SetError("called with non-compilable target type");
return false;
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2013-11-03 12:27:13 +02:00
bool system = false;
2013-03-16 19:13:01 +02:00
unsigned int argIndex = 1;
2016-07-09 11:21:54 +02:00
if ((flags & PROCESS_SYSTEM) && args[argIndex] == "SYSTEM") {
if (args.size() < 3) {
2013-11-03 12:27:13 +02:00
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
system = true;
++argIndex;
2016-07-09 11:21:54 +02:00
}
2013-11-03 12:27:13 +02:00
2013-03-16 19:13:01 +02:00
bool prepend = false;
2016-07-09 11:21:54 +02:00
if ((flags & PROCESS_BEFORE) && args[argIndex] == "BEFORE") {
if (args.size() < 3) {
2013-03-16 19:13:01 +02:00
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
prepend = true;
++argIndex;
2021-09-14 00:13:48 +02:00
} else if ((flags & PROCESS_AFTER) && args[argIndex] == "AFTER") {
if (args.size() < 3) {
this->SetError("called with incorrect number of arguments");
return false;
}
prepend = false;
++argIndex;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
2020-02-01 23:06:01 +01:00
if ((flags & PROCESS_REUSE_FROM) && args[argIndex] == "REUSE_FROM") {
if (args.size() != 3) {
this->SetError("called with incorrect number of arguments");
return false;
}
++argIndex;
2020-08-30 11:54:41 +02:00
this->Target->SetProperty("PRECOMPILE_HEADERS_REUSE_FROM", args[argIndex]);
2020-02-01 23:06:01 +01:00
++argIndex;
}
2013-03-16 19:13:01 +02:00
this->Property = prop;
2016-07-09 11:21:54 +02:00
while (argIndex < args.size()) {
if (!this->ProcessContentArgs(args, argIndex, prepend, system)) {
2013-03-16 19:13:01 +02:00
return false;
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
return true;
}
2016-07-09 11:21:54 +02:00
bool cmTargetPropCommandBase::ProcessContentArgs(
std::vector<std::string> const& args, unsigned int& argIndex, bool prepend,
bool system)
2013-03-16 19:13:01 +02:00
{
2017-07-20 19:35:53 +02:00
std::string const& scope = args[argIndex];
2013-03-16 19:13:01 +02:00
2016-07-09 11:21:54 +02:00
if (scope != "PUBLIC" && scope != "PRIVATE" && scope != "INTERFACE") {
2013-03-16 19:13:01 +02:00
this->SetError("called with invalid arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2013-03-16 19:13:01 +02:00
++argIndex;
std::vector<std::string> content;
2016-07-09 11:21:54 +02:00
for (unsigned int i = argIndex; i < args.size(); ++i, ++argIndex) {
if (args[i] == "PUBLIC" || args[i] == "PRIVATE" ||
args[i] == "INTERFACE") {
2019-11-11 23:01:05 +01:00
break;
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
content.push_back(args[i]);
}
2019-11-11 23:01:05 +01:00
if (!content.empty()) {
if (this->Target->GetType() == cmStateEnums::INTERFACE_LIBRARY &&
2021-09-14 00:13:48 +02:00
scope != "INTERFACE" && this->Property != "SOURCES") {
2019-11-11 23:01:05 +01:00
this->SetError("may only set INTERFACE properties on INTERFACE targets");
return false;
}
if (this->Target->IsImported() && scope != "INTERFACE") {
this->SetError("may only set INTERFACE properties on IMPORTED targets");
return false;
}
2021-09-14 00:13:48 +02:00
if (this->Target->GetType() == cmStateEnums::UTILITY &&
scope != "PRIVATE") {
this->SetError("may only set PRIVATE properties on custom targets");
return false;
}
2019-11-11 23:01:05 +01:00
}
2022-03-29 21:10:50 +02:00
return this->PopulateTargetProperties(scope, content, prepend, system);
2013-03-16 19:13:01 +02:00
}
2022-03-29 21:10:50 +02:00
bool cmTargetPropCommandBase::PopulateTargetProperties(
2016-07-09 11:21:54 +02:00
const std::string& scope, const std::vector<std::string>& content,
bool prepend, bool system)
2013-03-16 19:13:01 +02:00
{
2019-11-11 23:01:05 +01:00
if (content.empty()) {
return true;
}
2016-07-09 11:21:54 +02:00
if (scope == "PRIVATE" || scope == "PUBLIC") {
if (!this->HandleDirectContent(this->Target, content, prepend, system)) {
2015-04-27 22:25:09 +02:00
return false;
2013-03-16 19:13:01 +02:00
}
2016-07-09 11:21:54 +02:00
}
if (scope == "INTERFACE" || scope == "PUBLIC") {
2013-11-03 12:27:13 +02:00
this->HandleInterfaceContent(this->Target, content, prepend, system);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
return true;
2013-11-03 12:27:13 +02:00
}
2016-07-09 11:21:54 +02:00
void cmTargetPropCommandBase::HandleInterfaceContent(
cmTarget* tgt, const std::vector<std::string>& content, bool prepend, bool)
2013-11-03 12:27:13 +02:00
{
2016-07-09 11:21:54 +02:00
if (prepend) {
2013-11-03 12:27:13 +02:00
const std::string propName = std::string("INTERFACE_") + this->Property;
2021-11-20 13:41:27 +01:00
cmValue propValue = tgt->GetProperty(propName);
2020-08-30 11:54:41 +02:00
const std::string totalContent =
this->Join(content) + (propValue ? (";" + *propValue) : std::string());
tgt->SetProperty(propName, totalContent);
2016-07-09 11:21:54 +02:00
} else {
2020-08-30 11:54:41 +02:00
tgt->AppendProperty("INTERFACE_" + this->Property, this->Join(content));
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
}