/src/CMake/Source/cmGetSourceFilePropertyCommand.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmGetSourceFilePropertyCommand.h" |
4 | | |
5 | | #include <functional> |
6 | | |
7 | | #include <cm/string_view> |
8 | | #include <cmext/string_view> |
9 | | |
10 | | #include "cmExecutionStatus.h" |
11 | | #include "cmMakefile.h" |
12 | | #include "cmPolicies.h" |
13 | | #include "cmSetPropertyCommand.h" |
14 | | #include "cmSourceFile.h" |
15 | | #include "cmValue.h" |
16 | | |
17 | | namespace GetPropertyCommand { |
18 | | bool GetSourceFilePropertyGENERATED( |
19 | | std::string const& name, cmMakefile& mf, |
20 | | std::function<bool(bool)> const& storeResult); |
21 | | } |
22 | | |
23 | | bool cmGetSourceFilePropertyCommand(std::vector<std::string> const& args, |
24 | | cmExecutionStatus& status) |
25 | 0 | { |
26 | 0 | std::vector<std::string>::size_type args_size = args.size(); |
27 | 0 | if (args_size != 3 && args_size != 5) { |
28 | 0 | status.SetError("called with incorrect number of arguments"); |
29 | 0 | return false; |
30 | 0 | } |
31 | | |
32 | 0 | std::vector<std::string> source_file_directories; |
33 | 0 | std::vector<std::string> source_file_target_directories; |
34 | 0 | bool source_file_directory_option_enabled = false; |
35 | 0 | bool source_file_target_option_enabled = false; |
36 | |
|
37 | 0 | int property_arg_index = 2; |
38 | 0 | if (args[2] == "DIRECTORY"_s && args_size == 5) { |
39 | 0 | property_arg_index = 4; |
40 | 0 | source_file_directory_option_enabled = true; |
41 | 0 | source_file_directories.push_back(args[3]); |
42 | 0 | } else if (args[2] == "TARGET_DIRECTORY"_s && args_size == 5) { |
43 | 0 | property_arg_index = 4; |
44 | 0 | source_file_target_option_enabled = true; |
45 | 0 | source_file_target_directories.push_back(args[3]); |
46 | 0 | } |
47 | |
|
48 | 0 | std::vector<cmMakefile*> source_file_directory_makefiles; |
49 | 0 | bool file_scopes_handled = |
50 | 0 | SetPropertyCommand::HandleAndValidateSourceFileDirectoryScopes( |
51 | 0 | status, source_file_directory_option_enabled, |
52 | 0 | source_file_target_option_enabled, source_file_directories, |
53 | 0 | source_file_target_directories, source_file_directory_makefiles); |
54 | 0 | if (!file_scopes_handled) { |
55 | 0 | return false; |
56 | 0 | } |
57 | | |
58 | 0 | std::string const& var = args[0]; |
59 | 0 | std::string const& propName = args[property_arg_index]; |
60 | 0 | bool source_file_paths_should_be_absolute = |
61 | 0 | source_file_directory_option_enabled || source_file_target_option_enabled; |
62 | 0 | cmMakefile& directory_makefile = *source_file_directory_makefiles[0]; |
63 | | |
64 | | // Special handling for GENERATED property. |
65 | | // Note: Only, if CMP0163 is set to NEW! |
66 | 0 | if (propName == "GENERATED"_s) { |
67 | 0 | auto& mf = status.GetMakefile(); |
68 | 0 | auto cmp0163 = directory_makefile.GetPolicyStatus(cmPolicies::CMP0163); |
69 | 0 | bool const cmp0163new = |
70 | 0 | cmp0163 != cmPolicies::OLD && cmp0163 != cmPolicies::WARN; |
71 | 0 | if (cmp0163new) { |
72 | 0 | return GetPropertyCommand::GetSourceFilePropertyGENERATED( |
73 | 0 | 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 | 0 | mf.AddDefinition(var, (isGenerated) ? cmValue("1") : cmValue("0")); |
77 | 0 | return true; |
78 | 0 | }); |
79 | 0 | } |
80 | 0 | } |
81 | | |
82 | | // Get the source file. |
83 | 0 | std::string const file = |
84 | 0 | SetPropertyCommand::MakeSourceFilePathAbsoluteIfNeeded( |
85 | 0 | status, args[1], source_file_paths_should_be_absolute); |
86 | 0 | cmSourceFile* sf = directory_makefile.GetSource(file); |
87 | | |
88 | | // for the location we must create a source file first |
89 | 0 | if (!sf && propName == "LOCATION"_s) { |
90 | 0 | sf = directory_makefile.CreateSource(file); |
91 | 0 | } |
92 | |
|
93 | 0 | if (sf) { |
94 | 0 | cmValue prop = nullptr; |
95 | 0 | if (!propName.empty()) { |
96 | 0 | prop = sf->GetPropertyForUser(propName); |
97 | 0 | } |
98 | 0 | if (prop) { |
99 | | // Set the value on the original Makefile scope, not the scope of the |
100 | | // requested directory. |
101 | 0 | status.GetMakefile().AddDefinition(var, *prop); |
102 | 0 | return true; |
103 | 0 | } |
104 | 0 | } |
105 | | |
106 | | // Set the value on the original Makefile scope, not the scope of the |
107 | | // requested directory. |
108 | 0 | status.GetMakefile().AddDefinition(var, "NOTFOUND"); |
109 | 0 | return true; |
110 | 0 | } |