/src/CMake/Source/cmNinjaUtilityTargetGenerator.cxx
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst or https://cmake.org/licensing for details. */ |
3 | | #include "cmNinjaUtilityTargetGenerator.h" |
4 | | |
5 | | #include <algorithm> |
6 | | #include <array> |
7 | | #include <iterator> |
8 | | #include <memory> |
9 | | #include <set> |
10 | | #include <string> |
11 | | #include <utility> |
12 | | #include <vector> |
13 | | |
14 | | #include "cmCustomCommand.h" |
15 | | #include "cmCustomCommandGenerator.h" |
16 | | #include "cmGeneratedFileStream.h" |
17 | | #include "cmGeneratorExpression.h" |
18 | | #include "cmGeneratorTarget.h" |
19 | | #include "cmGlobalNinjaGenerator.h" |
20 | | #include "cmLocalNinjaGenerator.h" |
21 | | #include "cmNinjaTypes.h" |
22 | | #include "cmOutputConverter.h" |
23 | | #include "cmSourceFile.h" |
24 | | #include "cmStateTypes.h" |
25 | | #include "cmStringAlgorithms.h" |
26 | | #include "cmSystemTools.h" |
27 | | #include "cmTarget.h" |
28 | | #include "cmValue.h" |
29 | | |
30 | | cmNinjaUtilityTargetGenerator::cmNinjaUtilityTargetGenerator( |
31 | | cmGeneratorTarget* target) |
32 | 0 | : cmNinjaTargetGenerator(target) |
33 | 0 | { |
34 | 0 | } |
35 | | |
36 | 0 | cmNinjaUtilityTargetGenerator::~cmNinjaUtilityTargetGenerator() = default; |
37 | | |
38 | | void cmNinjaUtilityTargetGenerator::Generate(std::string const& config) |
39 | 0 | { |
40 | 0 | if (!this->GetGeneratorTarget()->Target->IsPerConfig()) { |
41 | 0 | this->WriteUtilBuildStatements(config, config); |
42 | 0 | return; |
43 | 0 | } |
44 | | |
45 | 0 | for (auto const& fileConfig : this->GetConfigNames()) { |
46 | 0 | if (!this->GetGlobalGenerator() |
47 | 0 | ->GetCrossConfigs(fileConfig) |
48 | 0 | .count(config)) { |
49 | 0 | continue; |
50 | 0 | } |
51 | 0 | if (fileConfig != config && |
52 | 0 | this->GetGeneratorTarget()->GetType() == cmStateEnums::GLOBAL_TARGET) { |
53 | 0 | continue; |
54 | 0 | } |
55 | 0 | this->WriteUtilBuildStatements(config, fileConfig); |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | | void cmNinjaUtilityTargetGenerator::WriteUtilBuildStatements( |
60 | | std::string const& config, std::string const& fileConfig) |
61 | 0 | { |
62 | 0 | cmGlobalNinjaGenerator* gg = this->GetGlobalGenerator(); |
63 | 0 | cmLocalNinjaGenerator* lg = this->GetLocalGenerator(); |
64 | 0 | cmGeneratorTarget* genTarget = this->GetGeneratorTarget(); |
65 | |
|
66 | 0 | std::string configDir; |
67 | 0 | if (genTarget->Target->IsPerConfig()) { |
68 | 0 | configDir = gg->ConfigDirectory(fileConfig); |
69 | 0 | } |
70 | 0 | std::string utilCommandName = |
71 | 0 | cmStrCat(lg->GetCurrentBinaryDirectory(), "/CMakeFiles", configDir, '/', |
72 | 0 | this->GetTargetName(), ".util"); |
73 | 0 | utilCommandName = this->ConvertToNinjaPath(utilCommandName); |
74 | |
|
75 | 0 | cmNinjaBuild phonyBuild("phony"); |
76 | 0 | std::vector<std::string> commands; |
77 | 0 | cmNinjaDeps deps; |
78 | 0 | cmGlobalNinjaGenerator::CCOutputs util_outputs(gg); |
79 | 0 | util_outputs.ExplicitOuts.emplace_back(utilCommandName); |
80 | |
|
81 | 0 | std::string commandDesc; |
82 | 0 | cmGeneratorExpression ge(*this->GetLocalGenerator()->GetCMakeInstance()); |
83 | 0 | bool uses_terminal = false; |
84 | 0 | { |
85 | 0 | std::array<std::vector<cmCustomCommand> const*, 2> const cmdLists = { |
86 | 0 | { &genTarget->GetPreBuildCommands(), &genTarget->GetPostBuildCommands() } |
87 | 0 | }; |
88 | |
|
89 | 0 | for (std::vector<cmCustomCommand> const* cmdList : cmdLists) { |
90 | 0 | for (cmCustomCommand const& ci : *cmdList) { |
91 | 0 | cmCustomCommandGenerator ccg(ci, fileConfig, lg); |
92 | 0 | lg->AppendCustomCommandDeps(ccg, deps, fileConfig); |
93 | 0 | lg->AppendCustomCommandLines(ccg, commands); |
94 | 0 | if (ci.GetComment()) { |
95 | 0 | if (!commandDesc.empty()) { |
96 | 0 | commandDesc += "; "; |
97 | 0 | } |
98 | 0 | auto cge = ge.Parse(ci.GetComment()); |
99 | 0 | commandDesc += cge->Evaluate(this->GetLocalGenerator(), config); |
100 | 0 | } |
101 | 0 | util_outputs.Add(ccg.GetByproducts()); |
102 | 0 | if (ci.GetUsesTerminal()) { |
103 | 0 | uses_terminal = true; |
104 | 0 | } |
105 | 0 | } |
106 | 0 | } |
107 | 0 | } |
108 | |
|
109 | 0 | { |
110 | 0 | std::vector<cmSourceFile*> sources; |
111 | 0 | genTarget->GetSourceFiles(sources, config); |
112 | 0 | for (cmSourceFile const* source : sources) { |
113 | 0 | if (cmCustomCommand const* cc = source->GetCustomCommand()) { |
114 | 0 | cmCustomCommandGenerator ccg(*cc, config, lg); |
115 | 0 | lg->AddCustomCommandTarget(cc, genTarget); |
116 | | |
117 | | // Depend on all custom command outputs. |
118 | 0 | std::vector<std::string> const& ccOutputs = ccg.GetOutputs(); |
119 | 0 | std::vector<std::string> const& ccByproducts = ccg.GetByproducts(); |
120 | 0 | std::transform(ccOutputs.begin(), ccOutputs.end(), |
121 | 0 | std::back_inserter(deps), this->MapToNinjaPath()); |
122 | 0 | std::transform(ccByproducts.begin(), ccByproducts.end(), |
123 | 0 | std::back_inserter(deps), this->MapToNinjaPath()); |
124 | 0 | } |
125 | 0 | } |
126 | 0 | } |
127 | |
|
128 | 0 | std::string outputConfig; |
129 | 0 | if (genTarget->Target->IsPerConfig()) { |
130 | 0 | outputConfig = config; |
131 | 0 | } |
132 | 0 | lg->AppendTargetOutputs(genTarget, phonyBuild.Outputs, outputConfig); |
133 | 0 | if (genTarget->Target->GetType() != cmStateEnums::GLOBAL_TARGET) { |
134 | 0 | lg->AppendTargetOutputs(genTarget, gg->GetByproductsForCleanTarget(), |
135 | 0 | config); |
136 | 0 | std::copy(util_outputs.ExplicitOuts.begin(), |
137 | 0 | util_outputs.ExplicitOuts.end(), |
138 | 0 | std::back_inserter(gg->GetByproductsForCleanTarget())); |
139 | 0 | } |
140 | 0 | lg->AppendTargetDepends(genTarget, deps, config, fileConfig, |
141 | 0 | DependOnTargetArtifact); |
142 | |
|
143 | 0 | if (commands.empty()) { |
144 | 0 | phonyBuild.Comment = "Utility command for " + this->GetTargetName(); |
145 | 0 | phonyBuild.ExplicitDeps = std::move(deps); |
146 | 0 | if (genTarget->GetType() != cmStateEnums::GLOBAL_TARGET) { |
147 | 0 | gg->WriteBuild(this->GetImplFileStream(fileConfig), phonyBuild); |
148 | 0 | } else { |
149 | 0 | gg->WriteBuild(this->GetCommonFileStream(), phonyBuild); |
150 | 0 | } |
151 | 0 | } else { |
152 | 0 | std::string command = lg->BuildCommandLine( |
153 | 0 | commands, config, fileConfig, "utility", this->GeneratorTarget); |
154 | 0 | std::string desc; |
155 | 0 | cmValue echoStr = genTarget->GetProperty("EchoString"); |
156 | 0 | if (echoStr) { |
157 | 0 | desc = *echoStr; |
158 | 0 | } else if (!commandDesc.empty()) { |
159 | 0 | desc = commandDesc; |
160 | 0 | } else { |
161 | 0 | desc = "Running utility command for " + this->GetTargetName(); |
162 | 0 | } |
163 | | |
164 | | // TODO: fix problematic global targets. For now, search and replace the |
165 | | // makefile vars. |
166 | 0 | cmSystemTools::ReplaceString( |
167 | 0 | command, "$(CMAKE_SOURCE_DIR)", |
168 | 0 | lg->ConvertToOutputFormat(lg->GetSourceDirectory(), |
169 | 0 | cmOutputConverter::SHELL)); |
170 | 0 | cmSystemTools::ReplaceString( |
171 | 0 | command, "$(CMAKE_BINARY_DIR)", |
172 | 0 | lg->ConvertToOutputFormat(lg->GetBinaryDirectory(), |
173 | 0 | cmOutputConverter::SHELL)); |
174 | 0 | cmSystemTools::ReplaceString(command, "$(ARGS)", ""); |
175 | 0 | command = gg->ExpandCFGIntDir(command, config); |
176 | |
|
177 | 0 | std::string ccConfig; |
178 | 0 | if (genTarget->Target->IsPerConfig() && |
179 | 0 | genTarget->GetType() != cmStateEnums::GLOBAL_TARGET) { |
180 | 0 | ccConfig = config; |
181 | 0 | } |
182 | 0 | if (config == fileConfig || |
183 | 0 | gg->GetPerConfigUtilityTargets().count(genTarget->GetName())) { |
184 | 0 | gg->WriteCustomCommandBuild( |
185 | 0 | command, desc, "Utility command for " + this->GetTargetName(), |
186 | 0 | /*depfile*/ "", /*job_pool*/ "", uses_terminal, |
187 | 0 | /*restat*/ true, ccConfig, std::move(util_outputs), std::move(deps)); |
188 | 0 | } |
189 | |
|
190 | 0 | phonyBuild.ExplicitDeps.push_back(utilCommandName); |
191 | 0 | if (genTarget->GetType() != cmStateEnums::GLOBAL_TARGET) { |
192 | 0 | gg->WriteBuild(this->GetImplFileStream(fileConfig), phonyBuild); |
193 | 0 | } else { |
194 | 0 | gg->WriteBuild(this->GetCommonFileStream(), phonyBuild); |
195 | 0 | } |
196 | 0 | } |
197 | | |
198 | | // Find ADDITIONAL_CLEAN_FILES |
199 | 0 | this->AdditionalCleanFiles(config); |
200 | | |
201 | | // Add an alias for the logical target name regardless of what directory |
202 | | // contains it. Skip this for GLOBAL_TARGET because they are meant to |
203 | | // be per-directory and have one at the top-level anyway. |
204 | 0 | if (genTarget->GetType() != cmStateEnums::GLOBAL_TARGET) { |
205 | 0 | gg->AddTargetAlias(this->GetTargetName(), genTarget, config); |
206 | 0 | } |
207 | 0 | } |