resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmGetSourceFilePropertyCommand.cxx
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"
4
5 #include "cmExecutionStatus.h"
6 #include "cmMakefile.h"
7 #include "cmSetPropertyCommand.h"
8 #include "cmSourceFile.h"
9 #include "cmValue.h"
10
11 bool cmGetSourceFilePropertyCommand(std::vector<std::string> const& args,
12                                     cmExecutionStatus& status)
13 {
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");
17     return false;
18   }
19
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;
24
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]);
34   }
35
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) {
43     return false;
44   }
45
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);
54
55   // for the location we must create a source file first
56   if (!sf && args[property_arg_index] == "LOCATION") {
57     sf = mf.CreateSource(file);
58   }
59
60   if (sf) {
61     cmValue prop = nullptr;
62     if (!args[property_arg_index].empty()) {
63       prop = sf->GetPropertyForUser(args[property_arg_index]);
64     }
65     if (prop) {
66       // Set the value on the original Makefile scope, not the scope of the
67       // requested directory.
68       status.GetMakefile().AddDefinition(var, *prop);
69       return true;
70     }
71   }
72
73   status.GetMakefile().AddDefinition(var, "NOTFOUND");
74   return true;
75 }