cmFunctionCommand.cxx 5.82 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3 4
#include "cmFunctionCommand.h"

5 6 7 8 9 10
#include <sstream>

#include "cmAlgorithms.h"
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
#include "cmPolicies.h"
11
#include "cmRange.h"
12
#include "cmState.h"
13 14 15 16 17 18 19 20

// define the class for function commands
class cmFunctionHelperCommand : public cmCommand
{
public:
  /**
   * This is a virtual constructor for the command.
   */
21
  cmCommand* Clone() override
22
  {
23
    cmFunctionHelperCommand* newC = new cmFunctionHelperCommand;
24 25 26
    // we must copy when we clone
    newC->Args = this->Args;
    newC->Functions = this->Functions;
27
    newC->Policies = this->Policies;
28
    newC->FilePath = this->FilePath;
29 30 31 32 33 34 35
    return newC;
  }

  /**
   * This is called when the command is first encountered in
   * the CMakeLists.txt file.
   */
36
  bool InvokeInitialPass(const std::vector<cmListFileArgument>& args,
37
                         cmExecutionStatus&) override;
38

39
  bool InitialPass(std::vector<std::string> const&,
40
                   cmExecutionStatus&) override
41 42 43
  {
    return false;
  }
44 45 46

  std::vector<std::string> Args;
  std::vector<cmListFileFunction> Functions;
47
  cmPolicies::PolicyMap Policies;
48
  std::string FilePath;
49 50
};

51
bool cmFunctionHelperCommand::InvokeInitialPass(
52
  const std::vector<cmListFileArgument>& args, cmExecutionStatus& inStatus)
53 54 55 56 57 58 59
{
  // Expand the argument list to the function.
  std::vector<std::string> expandedArgs;
  this->Makefile->ExpandArguments(args, expandedArgs);

  // make sure the number of arguments passed is at least the number
  // required by the signature
60
  if (expandedArgs.size() < this->Args.size() - 1) {
61 62 63
    std::string errorMsg =
      "Function invoked with incorrect arguments for function named: ";
    errorMsg += this->Args[0];
Stephen Kelly's avatar
Stephen Kelly committed
64
    this->SetError(errorMsg);
65
    return false;
66
  }
67

68
  cmMakefile::FunctionPushPop functionScope(this->Makefile, this->FilePath,
69
                                            this->Policies);
70

71
  // set the value of argc
72
  std::ostringstream strStream;
73
  strStream << expandedArgs.size();
74
  this->Makefile->AddDefinition("ARGC", strStream.str().c_str());
75
  this->Makefile->MarkVariableAsUsed("ARGC");
76 77

  // set the values for ARGV0 ARGV1 ...
78
  for (unsigned int t = 0; t < expandedArgs.size(); ++t) {
79
    std::ostringstream tmpStream;
80
    tmpStream << "ARGV" << t;
81
    this->Makefile->AddDefinition(tmpStream.str(), expandedArgs[t].c_str());
Stephen Kelly's avatar
Stephen Kelly committed
82
    this->Makefile->MarkVariableAsUsed(tmpStream.str());
83
  }
84

85
  // define the formal arguments
86 87 88
  for (unsigned int j = 1; j < this->Args.size(); ++j) {
    this->Makefile->AddDefinition(this->Args[j], expandedArgs[j - 1].c_str());
  }
89 90

  // define ARGV and ARGN
91
  std::string argvDef = cmJoin(expandedArgs, ";");
92 93
  std::vector<std::string>::const_iterator eit =
    expandedArgs.begin() + (this->Args.size() - 1);
94
  std::string argnDef = cmJoin(cmMakeRange(eit, expandedArgs.end()), ";");
95
  this->Makefile->AddDefinition("ARGV", argvDef.c_str());
96
  this->Makefile->MarkVariableAsUsed("ARGV");
97
  this->Makefile->AddDefinition("ARGN", argnDef.c_str());
98
  this->Makefile->MarkVariableAsUsed("ARGN");
99 100 101

  // Invoke all the functions that were collected in the block.
  // for each function
102
  for (cmListFileFunction const& func : this->Functions) {
103
    cmExecutionStatus status;
104
    if (!this->Makefile->ExecuteCommand(func, status) ||
105
        status.GetNestedError()) {
106 107
      // The error message should have already included the call stack
      // so we do not need to report an error here.
108
      functionScope.Quiet();
109
      inStatus.SetNestedError();
110
      return false;
111 112
    }
    if (status.GetReturnInvoked()) {
113
      return true;
114
    }
115
  }
116 117 118 119 120

  // pop scope on the makefile
  return true;
}

121 122
bool cmFunctionFunctionBlocker::IsFunctionBlocked(
  const cmListFileFunction& lff, cmMakefile& mf, cmExecutionStatus&)
123 124 125
{
  // record commands until we hit the ENDFUNCTION
  // at the ENDFUNCTION call we shift gears and start looking for invocations
126
  if (lff.Name.Lower == "function") {
127
    this->Depth++;
128
  } else if (lff.Name.Lower == "endfunction") {
129
    // if this is the endfunction for this function then execute
130
    if (!this->Depth) {
131
      // create a new command and add it to cmake
132
      cmFunctionHelperCommand* f = new cmFunctionHelperCommand();
133 134
      f->Args = this->Args;
      f->Functions = this->Functions;
135
      f->FilePath = this->GetStartingContext().FilePath;
136
      mf.RecordPolicies(f->Policies);
137
      mf.GetState()->AddScriptedCommand(this->Args[0], f);
138
      // remove the function blocker now that the function is defined
139
      mf.RemoveFunctionBlocker(this, lff);
140 141
      return true;
    }
Daniel Pfeifer's avatar
Daniel Pfeifer committed
142 143
    // decrement for each nested function that ends
    this->Depth--;
144
  }
145 146 147 148 149 150 151

  // if it wasn't an endfunction and we are not executing then we must be
  // recording
  this->Functions.push_back(lff);
  return true;
}

152 153
bool cmFunctionFunctionBlocker::ShouldRemove(const cmListFileFunction& lff,
                                             cmMakefile& mf)
154
{
155
  if (lff.Name.Lower == "endfunction") {
156
    std::vector<std::string> expandedArguments;
157 158
    mf.ExpandArguments(lff.Arguments, expandedArguments,
                       this->GetStartingContext().FilePath.c_str());
159
    // if the endfunction has arguments then make sure
160
    // they match the ones in the opening function command
161
    if ((expandedArguments.empty() ||
162
         (expandedArguments[0] == this->Args[0]))) {
163 164
      return true;
    }
165
  }
166 167 168 169

  return false;
}

170 171
bool cmFunctionCommand::InitialPass(std::vector<std::string> const& args,
                                    cmExecutionStatus&)
172
{
173
  if (args.empty()) {
174 175
    this->SetError("called with incorrect number of arguments");
    return false;
176
  }
177 178

  // create a function blocker
179
  cmFunctionFunctionBlocker* f = new cmFunctionFunctionBlocker();
180
  cmAppend(f->Args, args);
181 182 183
  this->Makefile->AddFunctionBlocker(f);
  return true;
}