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 "cmGetSourceFilePropertyCommand.h"
5 #include "cmExecutionStatus.h"
6 #include "cmMakefile.h"
7 #include "cmSetPropertyCommand.h"
8 #include "cmSourceFile.h"
11 bool cmGetSourceFilePropertyCommand(std::vector<std::string> const& args,
12 cmExecutionStatus& status)
14 std::vector<std::string>::size_type args_size = args.size();
15 if (args_size != 3 && args_size != 5) {
16 status.SetError("called with incorrect number of arguments");
20 std::vector<std::string> source_file_directories;
21 std::vector<std::string> source_file_target_directories;
22 bool source_file_directory_option_enabled = false;
23 bool source_file_target_option_enabled = false;
25 int property_arg_index = 2;
26 if (args[2] == "DIRECTORY" && args_size == 5) {
27 property_arg_index = 4;
28 source_file_directory_option_enabled = true;
29 source_file_directories.push_back(args[3]);
30 } else if (args[2] == "TARGET_DIRECTORY" && args_size == 5) {
31 property_arg_index = 4;
32 source_file_target_option_enabled = true;
33 source_file_target_directories.push_back(args[3]);
36 std::vector<cmMakefile*> source_file_directory_makefiles;
37 bool file_scopes_handled =
38 SetPropertyCommand::HandleAndValidateSourceFileDirectoryScopes(
39 status, source_file_directory_option_enabled,
40 source_file_target_option_enabled, source_file_directories,
41 source_file_target_directories, source_file_directory_makefiles);
42 if (!file_scopes_handled) {
46 std::string const& var = args[0];
47 bool source_file_paths_should_be_absolute =
48 source_file_directory_option_enabled || source_file_target_option_enabled;
49 std::string const file =
50 SetPropertyCommand::MakeSourceFilePathAbsoluteIfNeeded(
51 status, args[1], source_file_paths_should_be_absolute);
52 cmMakefile& mf = *source_file_directory_makefiles[0];
53 cmSourceFile* sf = mf.GetSource(file);
55 // for the location we must create a source file first
56 if (!sf && args[property_arg_index] == "LOCATION") {
57 sf = mf.CreateSource(file);
61 cmValue prop = nullptr;
62 if (!args[property_arg_index].empty()) {
63 prop = sf->GetPropertyForUser(args[property_arg_index]);
66 // Set the value on the original Makefile scope, not the scope of the
67 // requested directory.
68 status.GetMakefile().AddDefinition(var, *prop);
73 status.GetMakefile().AddDefinition(var, "NOTFOUND");