TZIVI-254: IVI needs a newer version of cmake
[profile/ivi/cmake.git] / Source / cmNinjaUtilityTargetGenerator.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2011 Peter Collingbourne <peter@pcc.me.uk>
4   Copyright 2011 Nicolas Despres <nicolas.despres@gmail.com>
5
6   Distributed under the OSI-approved BSD License (the "License");
7   see accompanying file Copyright.txt for details.
8
9   This software is distributed WITHOUT ANY WARRANTY; without even the
10   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11   See the License for more information.
12 ============================================================================*/
13 #include "cmNinjaUtilityTargetGenerator.h"
14 #include "cmCustomCommand.h"
15 #include "cmGeneratedFileStream.h"
16 #include "cmGlobalNinjaGenerator.h"
17 #include "cmMakefile.h"
18 #include "cmSourceFile.h"
19 #include "cmTarget.h"
20
21 cmNinjaUtilityTargetGenerator::cmNinjaUtilityTargetGenerator(cmTarget *target)
22   : cmNinjaTargetGenerator(target) {}
23
24 cmNinjaUtilityTargetGenerator::~cmNinjaUtilityTargetGenerator() {}
25
26 void cmNinjaUtilityTargetGenerator::Generate()
27 {
28   std::vector<std::string> commands;
29   cmNinjaDeps deps, outputs;
30
31   const std::vector<cmCustomCommand> *cmdLists[2] = {
32     &this->GetTarget()->GetPreBuildCommands(),
33     &this->GetTarget()->GetPostBuildCommands()
34   };
35
36   for (unsigned i = 0; i != 2; ++i) {
37     for (std::vector<cmCustomCommand>::const_iterator
38          ci = cmdLists[i]->begin(); ci != cmdLists[i]->end(); ++ci) {
39       this->GetLocalGenerator()->AppendCustomCommandDeps(&*ci, deps);
40       this->GetLocalGenerator()->AppendCustomCommandLines(&*ci, commands);
41     }
42   }
43
44   const std::vector<cmSourceFile*>& sources =
45     this->GetTarget()->GetSourceFiles();
46   for(std::vector<cmSourceFile*>::const_iterator source = sources.begin();
47       source != sources.end(); ++source)
48     {
49     if(cmCustomCommand* cc = (*source)->GetCustomCommand())
50       {
51       this->GetLocalGenerator()->AddCustomCommandTarget(cc, this->GetTarget());
52
53       // Depend on all custom command outputs.
54       const std::vector<std::string>& ccOutputs = cc->GetOutputs();
55       std::transform(ccOutputs.begin(), ccOutputs.end(),
56                      std::back_inserter(deps), MapToNinjaPath());
57       }
58     }
59
60   this->GetLocalGenerator()->AppendTargetOutputs(this->GetTarget(), outputs);
61   this->GetLocalGenerator()->AppendTargetDepends(this->GetTarget(), deps);
62
63   if (commands.empty()) {
64     cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
65                                             "Utility command for "
66                                             + this->GetTargetName(),
67                                             outputs,
68                                             deps);
69   } else {
70     std::string command =
71       this->GetLocalGenerator()->BuildCommandLine(commands);
72     const char *echoStr = this->GetTarget()->GetProperty("EchoString");
73     std::string desc;
74     if (echoStr)
75       desc = echoStr;
76     else
77       desc = "Running utility command for " + this->GetTargetName();
78
79     // TODO: fix problematic global targets.  For now, search and replace the
80     // makefile vars.
81     cmSystemTools::ReplaceString(
82       command,
83       "$(CMAKE_SOURCE_DIR)",
84       this->GetLocalGenerator()->ConvertToOutputFormat(
85         this->GetTarget()->GetMakefile()->GetHomeDirectory(),
86         cmLocalGenerator::SHELL).c_str());
87     cmSystemTools::ReplaceString(
88       command,
89       "$(CMAKE_BINARY_DIR)",
90       this->GetLocalGenerator()->ConvertToOutputFormat(
91         this->GetTarget()->GetMakefile()->GetHomeOutputDirectory(),
92         cmLocalGenerator::SHELL).c_str());
93     cmSystemTools::ReplaceString(command, "$(ARGS)", "");
94
95     if (command.find('$') != std::string::npos)
96       return;
97
98     std::string utilCommandName = cmake::GetCMakeFilesDirectoryPostSlash();
99     utilCommandName += this->GetTargetName() + ".util";
100
101     this->GetGlobalGenerator()->WriteCustomCommandBuild(
102       command,
103       desc,
104       "Utility command for " + this->GetTargetName(),
105       cmNinjaDeps(1, utilCommandName),
106       deps);
107
108     cmGlobalNinjaGenerator::WritePhonyBuild(this->GetBuildFileStream(),
109                                             "",
110                                             outputs,
111                                             cmNinjaDeps(1, utilCommandName));
112   }
113
114   this->GetGlobalGenerator()->AddTargetAlias(this->GetTargetName(),
115                                              this->GetTarget());
116 }