Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmCustomCommandGenerator.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2010 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmCustomCommandGenerator.h"
13
14 #include "cmMakefile.h"
15 #include "cmCustomCommand.h"
16 #include "cmLocalGenerator.h"
17 #include "cmGeneratorExpression.h"
18
19 //----------------------------------------------------------------------------
20 cmCustomCommandGenerator::cmCustomCommandGenerator(
21   cmCustomCommand const& cc, const char* config, cmMakefile* mf):
22   CC(cc), Config(config), Makefile(mf), LG(mf->GetLocalGenerator()),
23   OldStyle(cc.GetEscapeOldStyle()), MakeVars(cc.GetEscapeAllowMakeVars()),
24   GE(new cmGeneratorExpression(cc.GetBacktrace()))
25 {
26 }
27
28 //----------------------------------------------------------------------------
29 cmCustomCommandGenerator::~cmCustomCommandGenerator()
30 {
31   delete this->GE;
32 }
33
34 //----------------------------------------------------------------------------
35 unsigned int cmCustomCommandGenerator::GetNumberOfCommands() const
36 {
37   return static_cast<unsigned int>(this->CC.GetCommandLines().size());
38 }
39
40 //----------------------------------------------------------------------------
41 std::string cmCustomCommandGenerator::GetCommand(unsigned int c) const
42 {
43   std::string const& argv0 = this->CC.GetCommandLines()[c][0];
44   cmTarget* target = this->Makefile->FindTargetToUse(argv0.c_str());
45   if(target && target->GetType() == cmTarget::EXECUTABLE &&
46      (target->IsImported() || !this->Makefile->IsOn("CMAKE_CROSSCOMPILING")))
47     {
48     return target->GetLocation(this->Config);
49     }
50   return this->GE->Parse(argv0)->Evaluate(this->Makefile, this->Config);
51 }
52
53 //----------------------------------------------------------------------------
54 void
55 cmCustomCommandGenerator
56 ::AppendArguments(unsigned int c, std::string& cmd) const
57 {
58   cmCustomCommandLine const& commandLine = this->CC.GetCommandLines()[c];
59   for(unsigned int j=1;j < commandLine.size(); ++j)
60     {
61     std::string arg = this->GE->Parse(commandLine[j])->Evaluate(this->Makefile,
62                                                                this->Config);
63     cmd += " ";
64     if(this->OldStyle)
65       {
66       cmd += this->LG->EscapeForShellOldStyle(arg.c_str());
67       }
68     else
69       {
70       cmd += this->LG->EscapeForShell(arg.c_str(), this->MakeVars);
71       }
72     }
73 }