resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmGetDirectoryPropertyCommand.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 "cmGetDirectoryPropertyCommand.h"
4
5 #include "cmExecutionStatus.h"
6 #include "cmGlobalGenerator.h"
7 #include "cmMakefile.h"
8 #include "cmMessageType.h"
9 #include "cmPolicies.h"
10 #include "cmSystemTools.h"
11 #include "cmValue.h"
12
13 namespace {
14 void StoreResult(cmMakefile& makefile, std::string const& variable,
15                  const char* prop);
16 void StoreResult(cmMakefile& makefile, std::string const& variable,
17                  cmValue prop);
18 }
19
20 // cmGetDirectoryPropertyCommand
21 bool cmGetDirectoryPropertyCommand(std::vector<std::string> const& args,
22                                    cmExecutionStatus& status)
23 {
24   if (args.size() < 2) {
25     status.SetError("called with incorrect number of arguments");
26     return false;
27   }
28
29   auto i = args.begin();
30   std::string const& variable = *i;
31   ++i;
32
33   // get the directory argument if there is one
34   cmMakefile* dir = &status.GetMakefile();
35   if (*i == "DIRECTORY") {
36     ++i;
37     if (i == args.end()) {
38       status.SetError(
39         "DIRECTORY argument provided without subsequent arguments");
40       return false;
41     }
42     std::string sd = cmSystemTools::CollapseFullPath(
43       *i, status.GetMakefile().GetCurrentSourceDirectory());
44
45     // lookup the makefile from the directory name
46     dir = status.GetMakefile().GetGlobalGenerator()->FindMakefile(sd);
47     if (!dir) {
48       status.SetError(
49         "DIRECTORY argument provided but requested directory not found. "
50         "This could be because the directory argument was invalid or, "
51         "it is valid but has not been processed yet.");
52       return false;
53     }
54     ++i;
55     if (i == args.end()) {
56       status.SetError("called with incorrect number of arguments");
57       return false;
58     }
59   }
60
61   // OK, now we have the directory to process, we just get the requested
62   // information out of it
63
64   if (*i == "DEFINITION") {
65     ++i;
66     if (i == args.end()) {
67       status.SetError("A request for a variable definition was made without "
68                       "providing the name of the variable to get.");
69       return false;
70     }
71     std::string const& output = dir->GetSafeDefinition(*i);
72     status.GetMakefile().AddDefinition(variable, output);
73     return true;
74   }
75
76   if (i->empty()) {
77     status.SetError("given empty string for the property name to get");
78     return false;
79   }
80
81   if (*i == "DEFINITIONS") {
82     switch (status.GetMakefile().GetPolicyStatus(cmPolicies::CMP0059)) {
83       case cmPolicies::WARN:
84         status.GetMakefile().IssueMessage(
85           MessageType::AUTHOR_WARNING,
86           cmPolicies::GetPolicyWarning(cmPolicies::CMP0059));
87         CM_FALLTHROUGH;
88       case cmPolicies::OLD:
89         StoreResult(status.GetMakefile(), variable,
90                     status.GetMakefile().GetDefineFlagsCMP0059());
91         return true;
92       case cmPolicies::NEW:
93       case cmPolicies::REQUIRED_ALWAYS:
94       case cmPolicies::REQUIRED_IF_USED:
95         break;
96     }
97   }
98   StoreResult(status.GetMakefile(), variable, dir->GetProperty(*i));
99   return true;
100 }
101
102 namespace {
103 void StoreResult(cmMakefile& makefile, std::string const& variable,
104                  const char* prop)
105 {
106   makefile.AddDefinition(variable, prop ? prop : "");
107 }
108 void StoreResult(cmMakefile& makefile, std::string const& variable,
109                  cmValue prop)
110 {
111   makefile.AddDefinition(variable, prop);
112 }
113 }