cmFunctionCommand.cxx 5.83 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 "cmState.h"
12 13 14 15 16 17 18 19

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

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

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

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

50
bool cmFunctionHelperCommand::InvokeInitialPass(
51
  const std::vector<cmListFileArgument>& args, cmExecutionStatus& inStatus)
52 53 54 55 56 57 58
{
  // 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
59
  if (expandedArgs.size() < this->Args.size() - 1) {
60 61 62
    std::string errorMsg =
      "Function invoked with incorrect arguments for function named: ";
    errorMsg += this->Args[0];
63
    this->SetError(errorMsg);
64
    return false;
65
  }
66

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

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

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

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

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

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

  // pop scope on the makefile
  return true;
}

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

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

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

  return false;
}

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

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