Merge branch 'release-3.29'
[kiteware-cmake.git] / Source / cmGetSourceFilePropertyCommand.cxx
blob209030a84ae6197585bb0aaf8939c2149b35ea16
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 <functional>
7 #include <cm/string_view>
8 #include <cmext/string_view>
10 #include "cmExecutionStatus.h"
11 #include "cmMakefile.h"
12 #include "cmPolicies.h"
13 #include "cmSetPropertyCommand.h"
14 #include "cmSourceFile.h"
15 #include "cmValue.h"
17 namespace GetPropertyCommand {
18 bool GetSourceFilePropertyGENERATED(
19 const std::string& name, cmMakefile& mf,
20 const std::function<bool(bool)>& storeResult);
23 bool cmGetSourceFilePropertyCommand(std::vector<std::string> const& args,
24 cmExecutionStatus& status)
26 std::vector<std::string>::size_type args_size = args.size();
27 if (args_size != 3 && args_size != 5) {
28 status.SetError("called with incorrect number of arguments");
29 return false;
32 std::vector<std::string> source_file_directories;
33 std::vector<std::string> source_file_target_directories;
34 bool source_file_directory_option_enabled = false;
35 bool source_file_target_option_enabled = false;
37 int property_arg_index = 2;
38 if (args[2] == "DIRECTORY"_s && args_size == 5) {
39 property_arg_index = 4;
40 source_file_directory_option_enabled = true;
41 source_file_directories.push_back(args[3]);
42 } else if (args[2] == "TARGET_DIRECTORY"_s && args_size == 5) {
43 property_arg_index = 4;
44 source_file_target_option_enabled = true;
45 source_file_target_directories.push_back(args[3]);
48 std::vector<cmMakefile*> source_file_directory_makefiles;
49 bool file_scopes_handled =
50 SetPropertyCommand::HandleAndValidateSourceFileDirectoryScopes(
51 status, source_file_directory_option_enabled,
52 source_file_target_option_enabled, source_file_directories,
53 source_file_target_directories, source_file_directory_makefiles);
54 if (!file_scopes_handled) {
55 return false;
58 std::string const& var = args[0];
59 std::string const& propName = args[property_arg_index];
60 bool source_file_paths_should_be_absolute =
61 source_file_directory_option_enabled || source_file_target_option_enabled;
62 cmMakefile& directory_makefile = *source_file_directory_makefiles[0];
64 // Special handling for GENERATED property.
65 // Note: Only, if CMP0163 is set to NEW!
66 if (propName == "GENERATED"_s) {
67 auto& mf = status.GetMakefile();
68 auto cmp0163 = directory_makefile.GetPolicyStatus(cmPolicies::CMP0163);
69 bool const cmp0163new =
70 cmp0163 != cmPolicies::OLD && cmp0163 != cmPolicies::WARN;
71 if (cmp0163new) {
72 return GetPropertyCommand::GetSourceFilePropertyGENERATED(
73 args[1], mf, [&var, &mf](bool isGenerated) -> bool {
74 // Set the value on the original Makefile scope, not the scope of the
75 // requested directory.
76 mf.AddDefinition(var, (isGenerated) ? cmValue("1") : cmValue("0"));
77 return true;
78 });
82 // Get the source file.
83 std::string const file =
84 SetPropertyCommand::MakeSourceFilePathAbsoluteIfNeeded(
85 status, args[1], source_file_paths_should_be_absolute);
86 cmSourceFile* sf = directory_makefile.GetSource(file);
88 // for the location we must create a source file first
89 if (!sf && propName == "LOCATION"_s) {
90 sf = directory_makefile.CreateSource(file);
93 if (sf) {
94 cmValue prop = nullptr;
95 if (!propName.empty()) {
96 prop = sf->GetPropertyForUser(propName);
98 if (prop) {
99 // Set the value on the original Makefile scope, not the scope of the
100 // requested directory.
101 status.GetMakefile().AddDefinition(var, *prop);
102 return true;
106 // Set the value on the original Makefile scope, not the scope of the
107 // requested directory.
108 status.GetMakefile().AddDefinition(var, "NOTFOUND");
109 return true;