cmake/Source/cmGetSourceFilePropertyCommand.cxx

76 lines
2.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. */
#include "cmGetSourceFilePropertyCommand.h"
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2021-09-14 00:13:48 +02:00
#include "cmProperty.h"
2020-08-30 11:54:41 +02:00
#include "cmSetPropertyCommand.h"
#include "cmSourceFile.h"
2020-02-01 23:06:01 +01:00
bool cmGetSourceFilePropertyCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2020-08-30 11:54:41 +02:00
std::vector<std::string>::size_type args_size = args.size();
if (args_size != 3 && args_size != 5) {
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
}
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;
int property_arg_index = 2;
if (args[2] == "DIRECTORY" && args_size == 5) {
property_arg_index = 4;
source_file_directory_option_enabled = true;
source_file_directories.push_back(args[3]);
} else if (args[2] == "TARGET_DIRECTORY" && args_size == 5) {
property_arg_index = 4;
source_file_target_option_enabled = true;
source_file_target_directories.push_back(args[3]);
}
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;
}
2017-07-20 19:35:53 +02:00
std::string const& var = args[0];
2020-08-30 11:54:41 +02:00
bool source_file_paths_should_be_absolute =
source_file_directory_option_enabled || source_file_target_option_enabled;
std::string const file =
SetPropertyCommand::MakeSourceFilePathAbsoluteIfNeeded(
status, args[1], source_file_paths_should_be_absolute);
cmMakefile& mf = *source_file_directory_makefiles[0];
2020-02-01 23:06:01 +01:00
cmSourceFile* sf = mf.GetSource(file);
// for the location we must create a source file first
2020-08-30 11:54:41 +02:00
if (!sf && args[property_arg_index] == "LOCATION") {
2020-02-01 23:06:01 +01:00
sf = mf.CreateSource(file);
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
2016-07-09 11:21:54 +02:00
if (sf) {
2021-09-14 00:13:48 +02:00
cmProp prop = nullptr;
2020-08-30 11:54:41 +02:00
if (!args[property_arg_index].empty()) {
prop = sf->GetPropertyForUser(args[property_arg_index]);
2016-07-09 11:21:54 +02:00
}
if (prop) {
2020-08-30 11:54:41 +02:00
// Set the value on the original Makefile scope, not the scope of the
// requested directory.
2021-09-14 00:13:48 +02:00
status.GetMakefile().AddDefinition(var, *prop);
return true;
}
2016-07-09 11:21:54 +02:00
}
2020-08-30 11:54:41 +02:00
status.GetMakefile().AddDefinition(var, "NOTFOUND");
return true;
}