cmake/Source/cmGetPropertyCommand.cxx

481 lines
16 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. */
#include "cmGetPropertyCommand.h"
2021-11-20 13:41:27 +01:00
#include <cstddef>
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2013-11-03 12:27:13 +02:00
#include "cmGlobalGenerator.h"
2017-04-14 19:02:05 +02:00
#include "cmInstalledFile.h"
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2017-04-14 19:02:05 +02:00
#include "cmPolicies.h"
#include "cmProperty.h"
#include "cmPropertyDefinition.h"
2020-08-30 11:54:41 +02:00
#include "cmSetPropertyCommand.h"
2016-07-09 11:21:54 +02:00
#include "cmSourceFile.h"
#include "cmState.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
#include "cmTarget.h"
2016-07-09 11:21:54 +02:00
#include "cmTest.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2016-07-09 11:21:54 +02:00
#include "cmake.h"
2020-02-01 23:06:01 +01:00
namespace {
enum OutType
{
2020-02-01 23:06:01 +01:00
OutValue,
OutDefined,
OutBriefDoc,
OutFullDoc,
OutSet
};
// Implementation of each property type.
bool HandleGlobalMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName);
bool HandleDirectoryMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName);
bool HandleTargetMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName);
bool HandleSourceMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
2020-08-30 11:54:41 +02:00
const std::string& propertyName,
cmMakefile& directory_makefile,
bool source_file_paths_should_be_absolute);
2020-02-01 23:06:01 +01:00
bool HandleTestMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName);
bool HandleVariableMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName);
bool HandleCacheMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName);
bool HandleInstallMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName);
}
2020-02-01 23:06:01 +01:00
bool cmGetPropertyCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2020-02-01 23:06:01 +01:00
OutType infoType = OutValue;
2016-07-09 11:21:54 +02:00
if (args.size() < 3) {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
// The cmake variable in which to store the result.
2022-08-04 22:12:04 +02:00
std::string const& variable = args[0];
2020-02-01 23:06:01 +01:00
std::string name;
std::string propertyName;
2020-08-30 11:54:41 +02:00
std::vector<std::string> source_file_directories;
std::vector<std::string> source_file_target_directories;
bool source_file_directory_option_enabled = false;
bool source_file_target_option_enabled = false;
// Get the scope from which to get the property.
cmProperty::ScopeType scope;
2016-07-09 11:21:54 +02:00
if (args[1] == "GLOBAL") {
scope = cmProperty::GLOBAL;
2016-07-09 11:21:54 +02:00
} else if (args[1] == "DIRECTORY") {
scope = cmProperty::DIRECTORY;
2016-07-09 11:21:54 +02:00
} else if (args[1] == "TARGET") {
scope = cmProperty::TARGET;
2016-07-09 11:21:54 +02:00
} else if (args[1] == "SOURCE") {
scope = cmProperty::SOURCE_FILE;
2016-07-09 11:21:54 +02:00
} else if (args[1] == "TEST") {
scope = cmProperty::TEST;
2016-07-09 11:21:54 +02:00
} else if (args[1] == "VARIABLE") {
scope = cmProperty::VARIABLE;
2016-07-09 11:21:54 +02:00
} else if (args[1] == "CACHE") {
2009-10-04 10:30:41 +03:00
scope = cmProperty::CACHE;
2016-07-09 11:21:54 +02:00
} else if (args[1] == "INSTALL") {
2015-04-27 22:25:09 +02:00
scope = cmProperty::INSTALL;
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
status.SetError(cmStrCat(
"given invalid scope ", args[1],
". "
"Valid scopes are "
"GLOBAL, DIRECTORY, TARGET, SOURCE, TEST, VARIABLE, CACHE, INSTALL."));
return false;
2016-07-09 11:21:54 +02:00
}
// Parse remaining arguments.
2016-07-09 11:21:54 +02:00
enum Doing
{
DoingNone,
DoingName,
DoingProperty,
2020-08-30 11:54:41 +02:00
DoingType,
DoingSourceDirectory,
DoingSourceTargetDirectory
2016-07-09 11:21:54 +02:00
};
Doing doing = DoingName;
2016-07-09 11:21:54 +02:00
for (unsigned int i = 2; i < args.size(); ++i) {
if (args[i] == "PROPERTY") {
doing = DoingProperty;
2016-07-09 11:21:54 +02:00
} else if (args[i] == "BRIEF_DOCS") {
doing = DoingNone;
2020-02-01 23:06:01 +01:00
infoType = OutBriefDoc;
2016-07-09 11:21:54 +02:00
} else if (args[i] == "FULL_DOCS") {
doing = DoingNone;
2020-02-01 23:06:01 +01:00
infoType = OutFullDoc;
2016-07-09 11:21:54 +02:00
} else if (args[i] == "SET") {
doing = DoingNone;
2020-02-01 23:06:01 +01:00
infoType = OutSet;
2016-07-09 11:21:54 +02:00
} else if (args[i] == "DEFINED") {
doing = DoingNone;
2020-02-01 23:06:01 +01:00
infoType = OutDefined;
2016-07-09 11:21:54 +02:00
} else if (doing == DoingName) {
doing = DoingNone;
2020-02-01 23:06:01 +01:00
name = args[i];
2020-08-30 11:54:41 +02:00
} else if (doing == DoingNone && scope == cmProperty::SOURCE_FILE &&
args[i] == "DIRECTORY") {
doing = DoingSourceDirectory;
source_file_directory_option_enabled = true;
} else if (doing == DoingNone && scope == cmProperty::SOURCE_FILE &&
args[i] == "TARGET_DIRECTORY") {
doing = DoingSourceTargetDirectory;
source_file_target_option_enabled = true;
} else if (doing == DoingSourceDirectory) {
source_file_directories.push_back(args[i]);
doing = DoingNone;
} else if (doing == DoingSourceTargetDirectory) {
source_file_target_directories.push_back(args[i]);
doing = DoingNone;
2016-07-09 11:21:54 +02:00
} else if (doing == DoingProperty) {
doing = DoingNone;
2020-02-01 23:06:01 +01:00
propertyName = args[i];
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
status.SetError(cmStrCat("given invalid argument \"", args[i], "\"."));
return false;
}
2016-07-09 11:21:54 +02:00
}
// Make sure a property name was found.
2020-02-01 23:06:01 +01:00
if (propertyName.empty()) {
status.SetError("not given a PROPERTY <name> argument.");
return false;
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
std::vector<cmMakefile*> source_file_directory_makefiles;
bool file_scopes_handled =
2021-09-14 00:13:48 +02:00
SetPropertyCommand::HandleAndValidateSourceFileDirectoryScopes(
2020-08-30 11:54:41 +02:00
status, source_file_directory_option_enabled,
source_file_target_option_enabled, source_file_directories,
source_file_target_directories, source_file_directory_makefiles);
if (!file_scopes_handled) {
return false;
}
// Compute requested output.
2020-02-01 23:06:01 +01:00
if (infoType == OutBriefDoc) {
// Lookup brief documentation.
std::string output;
2016-07-09 11:21:54 +02:00
if (cmPropertyDefinition const* def =
2020-02-01 23:06:01 +01:00
status.GetMakefile().GetState()->GetPropertyDefinition(propertyName,
scope)) {
output = def->GetShortDescription();
2022-03-29 21:10:50 +02:00
}
if (output.empty()) {
output = "NOTFOUND";
}
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition(variable, output);
} else if (infoType == OutFullDoc) {
// Lookup full documentation.
std::string output;
2016-07-09 11:21:54 +02:00
if (cmPropertyDefinition const* def =
2020-02-01 23:06:01 +01:00
status.GetMakefile().GetState()->GetPropertyDefinition(propertyName,
scope)) {
output = def->GetFullDescription();
2022-03-29 21:10:50 +02:00
}
if (output.empty()) {
output = "NOTFOUND";
}
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition(variable, output);
} else if (infoType == OutDefined) {
// Lookup if the property is defined
2020-02-01 23:06:01 +01:00
if (status.GetMakefile().GetState()->GetPropertyDefinition(propertyName,
scope)) {
status.GetMakefile().AddDefinition(variable, "1");
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
status.GetMakefile().AddDefinition(variable, "0");
}
2016-07-09 11:21:54 +02:00
} else {
// Dispatch property getting.
2020-08-30 11:54:41 +02:00
cmMakefile& directory_scope_mf = *(source_file_directory_makefiles[0]);
bool source_file_paths_should_be_absolute =
source_file_directory_option_enabled ||
source_file_target_option_enabled;
2016-07-09 11:21:54 +02:00
switch (scope) {
case cmProperty::GLOBAL:
2020-02-01 23:06:01 +01:00
return HandleGlobalMode(status, name, infoType, variable,
propertyName);
2016-07-09 11:21:54 +02:00
case cmProperty::DIRECTORY:
2020-02-01 23:06:01 +01:00
return HandleDirectoryMode(status, name, infoType, variable,
propertyName);
2016-07-09 11:21:54 +02:00
case cmProperty::TARGET:
2020-02-01 23:06:01 +01:00
return HandleTargetMode(status, name, infoType, variable,
propertyName);
2016-07-09 11:21:54 +02:00
case cmProperty::SOURCE_FILE:
2020-08-30 11:54:41 +02:00
return HandleSourceMode(status, name, infoType, variable, propertyName,
directory_scope_mf,
source_file_paths_should_be_absolute);
2016-07-09 11:21:54 +02:00
case cmProperty::TEST:
2020-02-01 23:06:01 +01:00
return HandleTestMode(status, name, infoType, variable, propertyName);
2016-07-09 11:21:54 +02:00
case cmProperty::VARIABLE:
2020-02-01 23:06:01 +01:00
return HandleVariableMode(status, name, infoType, variable,
propertyName);
2016-07-09 11:21:54 +02:00
case cmProperty::CACHE:
2020-02-01 23:06:01 +01:00
return HandleCacheMode(status, name, infoType, variable, propertyName);
2016-07-09 11:21:54 +02:00
case cmProperty::INSTALL:
2020-02-01 23:06:01 +01:00
return HandleInstallMode(status, name, infoType, variable,
propertyName);
case cmProperty::CACHED_VARIABLE:
break; // should never happen
}
2016-07-09 11:21:54 +02:00
}
return true;
}
2020-02-01 23:06:01 +01:00
namespace {
2021-11-20 13:41:27 +01:00
// Implementation of result storage.
template <typename ValueType>
2020-02-01 23:06:01 +01:00
bool StoreResult(OutType infoType, cmMakefile& makefile,
2021-11-20 13:41:27 +01:00
const std::string& variable, ValueType value)
{
2020-02-01 23:06:01 +01:00
if (infoType == OutSet) {
makefile.AddDefinition(variable, value ? "1" : "0");
} else // if(infoType == OutValue)
2016-07-09 11:21:54 +02:00
{
if (value) {
2020-02-01 23:06:01 +01:00
makefile.AddDefinition(variable, value);
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
makefile.RemoveDefinition(variable);
}
2016-07-09 11:21:54 +02:00
}
return true;
}
2021-11-20 13:41:27 +01:00
template <>
bool StoreResult(OutType infoType, cmMakefile& makefile,
const std::string& variable, std::nullptr_t value)
{
return StoreResult(infoType, makefile, variable, cmValue(value));
}
2020-02-01 23:06:01 +01:00
bool HandleGlobalMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName)
{
2020-02-01 23:06:01 +01:00
if (!name.empty()) {
status.SetError("given name for GLOBAL scope.");
return false;
2016-07-09 11:21:54 +02:00
}
// Get the property.
2020-02-01 23:06:01 +01:00
cmake* cm = status.GetMakefile().GetCMakeInstance();
2021-11-20 13:41:27 +01:00
return StoreResult(infoType, status.GetMakefile(), variable,
cm->GetState()->GetGlobalProperty(propertyName));
}
2020-02-01 23:06:01 +01:00
bool HandleDirectoryMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName)
{
// Default to the current directory.
2020-02-01 23:06:01 +01:00
cmMakefile* mf = &status.GetMakefile();
// Lookup the directory if given.
2020-02-01 23:06:01 +01:00
if (!name.empty()) {
// Construct the directory name. Interpret relative paths with
// respect to the current directory.
2020-08-30 11:54:41 +02:00
std::string dir = cmSystemTools::CollapseFullPath(
name, status.GetMakefile().GetCurrentSourceDirectory());
// Lookup the generator.
2020-02-01 23:06:01 +01:00
mf = status.GetMakefile().GetGlobalGenerator()->FindMakefile(dir);
2016-07-09 11:21:54 +02:00
if (!mf) {
// Could not find the directory.
2020-02-01 23:06:01 +01:00
status.SetError(
2016-07-09 11:21:54 +02:00
"DIRECTORY scope provided but requested directory was not found. "
"This could be because the directory argument was invalid or, "
"it is valid but has not been processed yet.");
return false;
}
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
if (propertyName == "DEFINITIONS") {
2016-07-09 11:21:54 +02:00
switch (mf->GetPolicyStatus(cmPolicies::CMP0059)) {
2015-11-17 17:22:37 +01:00
case cmPolicies::WARN:
2019-11-11 23:01:05 +01:00
mf->IssueMessage(MessageType::AUTHOR_WARNING,
2015-11-17 17:22:37 +01:00
cmPolicies::GetPolicyWarning(cmPolicies::CMP0059));
2017-07-20 19:35:53 +02:00
CM_FALLTHROUGH;
2015-11-17 17:22:37 +01:00
case cmPolicies::OLD:
2020-02-01 23:06:01 +01:00
return StoreResult(infoType, status.GetMakefile(), variable,
mf->GetDefineFlagsCMP0059());
2015-11-17 17:22:37 +01:00
case cmPolicies::NEW:
case cmPolicies::REQUIRED_ALWAYS:
case cmPolicies::REQUIRED_IF_USED:
break;
}
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// Get the property.
2020-02-01 23:06:01 +01:00
return StoreResult(infoType, status.GetMakefile(), variable,
2021-11-20 13:41:27 +01:00
mf->GetProperty(propertyName));
}
2020-02-01 23:06:01 +01:00
bool HandleTargetMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName)
{
2020-02-01 23:06:01 +01:00
if (name.empty()) {
status.SetError("not given name for TARGET scope.");
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
if (cmTarget* target = status.GetMakefile().FindTargetToUse(name)) {
2020-08-30 11:54:41 +02:00
if (propertyName == "ALIASED_TARGET" || propertyName == "ALIAS_GLOBAL") {
2020-02-01 23:06:01 +01:00
if (status.GetMakefile().IsAlias(name)) {
2020-08-30 11:54:41 +02:00
if (propertyName == "ALIASED_TARGET") {
return StoreResult(infoType, status.GetMakefile(), variable,
target->GetName().c_str());
}
if (propertyName == "ALIAS_GLOBAL") {
return StoreResult(
infoType, status.GetMakefile(), variable,
status.GetMakefile().GetGlobalGenerator()->IsAlias(name)
? "TRUE"
: "FALSE");
}
2013-11-03 12:27:13 +02:00
}
2020-02-01 23:06:01 +01:00
return StoreResult(infoType, status.GetMakefile(), variable, nullptr);
}
2022-03-29 21:10:50 +02:00
cmValue prop =
target->GetComputedProperty(propertyName, status.GetMakefile());
2021-09-14 00:13:48 +02:00
if (!prop) {
prop = target->GetProperty(propertyName);
2017-04-14 19:02:05 +02:00
}
2021-11-20 13:41:27 +01:00
return StoreResult(infoType, status.GetMakefile(), variable, prop);
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
status.SetError(cmStrCat("could not find TARGET ", name,
". Perhaps it has not yet been created."));
2016-10-30 18:24:19 +01:00
return false;
}
2020-02-01 23:06:01 +01:00
bool HandleSourceMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
2020-08-30 11:54:41 +02:00
const std::string& propertyName,
cmMakefile& directory_makefile,
const bool source_file_paths_should_be_absolute)
{
2020-02-01 23:06:01 +01:00
if (name.empty()) {
status.SetError("not given name for SOURCE scope.");
return false;
2016-07-09 11:21:54 +02:00
}
// Get the source file.
2020-08-30 11:54:41 +02:00
const std::string source_file_absolute_path =
SetPropertyCommand::MakeSourceFilePathAbsoluteIfNeeded(
status, name, source_file_paths_should_be_absolute);
if (cmSourceFile* sf =
directory_makefile.GetOrCreateSource(source_file_absolute_path)) {
2020-02-01 23:06:01 +01:00
return StoreResult(infoType, status.GetMakefile(), variable,
2021-11-20 13:41:27 +01:00
sf->GetPropertyForUser(propertyName));
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
status.SetError(
2020-08-30 11:54:41 +02:00
cmStrCat("given SOURCE name that could not be found or created: ",
source_file_absolute_path));
2016-10-30 18:24:19 +01:00
return false;
}
2020-02-01 23:06:01 +01:00
bool HandleTestMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName)
{
2020-02-01 23:06:01 +01:00
if (name.empty()) {
status.SetError("not given name for TEST scope.");
return false;
2016-07-09 11:21:54 +02:00
}
// Loop over all tests looking for matching names.
2020-02-01 23:06:01 +01:00
if (cmTest* test = status.GetMakefile().GetTest(name)) {
return StoreResult(infoType, status.GetMakefile(), variable,
test->GetProperty(propertyName));
2016-07-09 11:21:54 +02:00
}
// If not found it is an error.
2020-02-01 23:06:01 +01:00
status.SetError(cmStrCat("given TEST name that does not exist: ", name));
return false;
}
2020-02-01 23:06:01 +01:00
bool HandleVariableMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName)
{
2020-02-01 23:06:01 +01:00
if (!name.empty()) {
status.SetError("given name for VARIABLE scope.");
return false;
2016-07-09 11:21:54 +02:00
}
2021-11-20 13:41:27 +01:00
return StoreResult(infoType, status.GetMakefile(), variable,
status.GetMakefile().GetDefinition(propertyName));
}
2009-10-04 10:30:41 +03:00
2020-02-01 23:06:01 +01:00
bool HandleCacheMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName)
2009-10-04 10:30:41 +03:00
{
2020-02-01 23:06:01 +01:00
if (name.empty()) {
status.SetError("not given name for CACHE scope.");
2009-10-04 10:30:41 +03:00
return false;
2016-07-09 11:21:54 +02:00
}
2009-10-04 10:30:41 +03:00
2021-11-20 13:41:27 +01:00
cmValue value = nullptr;
2020-02-01 23:06:01 +01:00
if (status.GetMakefile().GetState()->GetCacheEntryValue(name)) {
value = status.GetMakefile().GetState()->GetCacheEntryProperty(
name, propertyName);
2016-07-09 11:21:54 +02:00
}
2021-11-20 13:41:27 +01:00
StoreResult(infoType, status.GetMakefile(), variable, value);
2009-10-04 10:30:41 +03:00
return true;
}
2015-04-27 22:25:09 +02:00
2020-02-01 23:06:01 +01:00
bool HandleInstallMode(cmExecutionStatus& status, const std::string& name,
OutType infoType, const std::string& variable,
const std::string& propertyName)
2015-04-27 22:25:09 +02:00
{
2020-02-01 23:06:01 +01:00
if (name.empty()) {
status.SetError("not given name for INSTALL scope.");
2015-04-27 22:25:09 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
// Get the installed file.
2020-02-01 23:06:01 +01:00
cmake* cm = status.GetMakefile().GetCMakeInstance();
2015-04-27 22:25:09 +02:00
2016-07-09 11:21:54 +02:00
if (cmInstalledFile* file =
2020-02-01 23:06:01 +01:00
cm->GetOrCreateInstalledFile(&status.GetMakefile(), name)) {
2015-04-27 22:25:09 +02:00
std::string value;
2020-02-01 23:06:01 +01:00
bool isSet = file->GetProperty(propertyName, value);
2015-04-27 22:25:09 +02:00
2020-02-01 23:06:01 +01:00
return StoreResult(infoType, status.GetMakefile(), variable,
isSet ? value.c_str() : nullptr);
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
status.SetError(
cmStrCat("given INSTALL name that could not be found or created: ", name));
2016-10-30 18:24:19 +01:00
return false;
2015-04-27 22:25:09 +02:00
}
2020-02-01 23:06:01 +01:00
}