cmMacroCommand.cxx 3.98 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1
2
/*=========================================================================

3
  Program:   CMake - Cross-Platform Makefile Generator
Ken Martin's avatar
Ken Martin committed
4
5
6
7
8
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

9
10
  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
Ken Martin's avatar
Ken Martin committed
11
12
13
14
15
16
17
18
19

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "cmMacroCommand.h"

bool cmMacroFunctionBlocker::
20
IsFunctionBlocked(const cmListFileFunction& lff, cmMakefile &mf) 
Ken Martin's avatar
Ken Martin committed
21
22
23
{
  // record commands until we hit the ENDMACRO
  // at the ENDMACRO call we shift gears and start looking for invocations
24
  if(lff.m_Name == "ENDMACRO")
Ken Martin's avatar
Ken Martin committed
25
    {
26
27
28
29
30
31
32
    std::vector<std::string> expandedArguments;
    mf.ExpandArguments(lff.m_Arguments, expandedArguments);
    if(!expandedArguments.empty() && (expandedArguments[0] == m_Args[0]))
      {
      m_Executing = true;
      return true;
      }
Ken Martin's avatar
Ken Martin committed
33
34
35
36
37
38
    }
  
  if (!m_Executing)
    {
    // if it wasn't an endmacro and we are not executing then we must be
    // recording
39
    m_Functions.push_back(lff);
Ken Martin's avatar
Ken Martin committed
40
41
42
43
44
    return true;
    }
  
  // otherwise the macro has been recorded and we are executing
  // so we look for macro invocations
45
  if(lff.m_Name == m_Args[0])
Ken Martin's avatar
Ken Martin committed
46
    {
47
48
49
    // Expand the argument list to the macro.
    std::vector<std::string> expandedArguments;
    mf.ExpandArguments(lff.m_Arguments, expandedArguments);
Ken Martin's avatar
Ken Martin committed
50
    // make sure the number of arguments matches
51
    if (expandedArguments.size() != m_Args.size() - 1)
Ken Martin's avatar
Ken Martin committed
52
53
54
      {
      cmSystemTools::Error("A macro was invoked without the correct number of arguments. The macro name was: ", m_Args[0].c_str());
      }
55
56
57
    
    // Invoke all the functions that were collected in the block.
    for(unsigned int c = 0; c < m_Functions.size(); ++c)
Ken Martin's avatar
Ken Martin committed
58
      {
59
60
61
      // Replace the formal arguments and then invoke the command.
      cmListFileFunction newLFF;
      newLFF.m_Name = m_Functions[c].m_Name;
62
63
      newLFF.m_FilePath = m_Functions[c].m_FilePath;
      newLFF.m_Line = m_Functions[c].m_Line;
64
65
66
      for (std::vector<cmListFileArgument>::const_iterator k = 
             m_Functions[c].m_Arguments.begin();
           k != m_Functions[c].m_Arguments.end(); ++k)
Ken Martin's avatar
Ken Martin committed
67
        {
68
        std::string tmps = k->Value;
Ken Martin's avatar
Ken Martin committed
69
70
71
72
73
74
        for (unsigned int j = 1; j < m_Args.size(); ++j)
          {
          std::string variable = "${";
          variable += m_Args[j];
          variable += "}"; 
          cmSystemTools::ReplaceString(tmps, variable.c_str(),
75
                                       expandedArguments[j-1].c_str());
Ken Martin's avatar
Ken Martin committed
76
          }
77
78
        cmListFileArgument arg(tmps, k->Quoted);
        newLFF.m_Arguments.push_back(arg);
Ken Martin's avatar
Ken Martin committed
79
        }
80
81
82
83
84
85
86
87
88
      if(!mf.ExecuteCommand(newLFF))
        {
        cmOStringStream error;
        error << "Error in cmake code at\n"
              << lff.m_FilePath << ":" << lff.m_Line << ":\n"
              << "A command failed during the invocation of macro \""
              << lff.m_Name.c_str() << "\".";
        cmSystemTools::Error(error.str().c_str());
        }
Ken Martin's avatar
Ken Martin committed
89
90
91
92
93
94
95
96
97
      }
    return true;
    }

  // if not an invocation then it is just an ordinary line
  return false;
}

bool cmMacroFunctionBlocker::
98
ShouldRemove(const cmListFileFunction&, cmMakefile &) 
Ken Martin's avatar
Ken Martin committed
99
100
101
102
103
104
105
106
107
108
{
  return false;
}

void cmMacroFunctionBlocker::
ScopeEnded(cmMakefile &) 
{
  // macros never leave scope
}

109
bool cmMacroCommand::InitialPass(std::vector<std::string> const& args)
Ken Martin's avatar
Ken Martin committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
{
  if(args.size() < 1)
    {
    this->SetError("called with incorrect number of arguments");
    return false;
    }
  
  // create a function blocker
  cmMacroFunctionBlocker *f = new cmMacroFunctionBlocker();
  for(std::vector<std::string>::const_iterator j = args.begin();
      j != args.end(); ++j)
    {   
    f->m_Args.push_back(*j);
    }
  m_Makefile->AddFunctionBlocker(f);
  
  return true;
}