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"
5 #include "cmExecutionStatus.h"
6 #include "cmGlobalGenerator.h"
7 #include "cmMakefile.h"
8 #include "cmMessageType.h"
9 #include "cmPolicies.h"
10 #include "cmSystemTools.h"
14 void StoreResult(cmMakefile& makefile, std::string const& variable,
16 void StoreResult(cmMakefile& makefile, std::string const& variable,
20 // cmGetDirectoryPropertyCommand
21 bool cmGetDirectoryPropertyCommand(std::vector<std::string> const& args,
22 cmExecutionStatus& status)
24 if (args.size() < 2) {
25 status.SetError("called with incorrect number of arguments");
29 auto i = args.begin();
30 std::string const& variable = *i;
33 // get the directory argument if there is one
34 cmMakefile* dir = &status.GetMakefile();
35 if (*i == "DIRECTORY") {
37 if (i == args.end()) {
39 "DIRECTORY argument provided without subsequent arguments");
42 std::string sd = cmSystemTools::CollapseFullPath(
43 *i, status.GetMakefile().GetCurrentSourceDirectory());
45 // lookup the makefile from the directory name
46 dir = status.GetMakefile().GetGlobalGenerator()->FindMakefile(sd);
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.");
55 if (i == args.end()) {
56 status.SetError("called with incorrect number of arguments");
61 // OK, now we have the directory to process, we just get the requested
62 // information out of it
64 if (*i == "DEFINITION") {
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.");
71 std::string const& output = dir->GetSafeDefinition(*i);
72 status.GetMakefile().AddDefinition(variable, output);
77 status.SetError("given empty string for the property name to get");
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));
89 StoreResult(status.GetMakefile(), variable,
90 status.GetMakefile().GetDefineFlagsCMP0059());
93 case cmPolicies::REQUIRED_ALWAYS:
94 case cmPolicies::REQUIRED_IF_USED:
98 StoreResult(status.GetMakefile(), variable, dir->GetProperty(*i));
103 void StoreResult(cmMakefile& makefile, std::string const& variable,
106 makefile.AddDefinition(variable, prop ? prop : "");
108 void StoreResult(cmMakefile& makefile, std::string const& variable,
111 makefile.AddDefinition(variable, prop);