Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package genrule |
| 16 | |
| 17 | import ( |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 18 | "fmt" |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 19 | "io" |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 20 | "strings" |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 21 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint/bootstrap" |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 25 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 26 | "android/soong/android" |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 27 | "android/soong/shared" |
| 28 | "path/filepath" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 31 | func init() { |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 32 | android.RegisterModuleType("genrule_defaults", defaultsFactory) |
| 33 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 34 | android.RegisterModuleType("gensrcs", GenSrcsFactory) |
| 35 | android.RegisterModuleType("genrule", GenRuleFactory) |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 38 | var ( |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 39 | pctx = android.NewPackageContext("android/soong/genrule") |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 40 | ) |
| 41 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 42 | func init() { |
Dan Willemsen | ddf504c | 2019-08-09 16:21:29 -0700 | [diff] [blame^] | 43 | pctx.Import("android/soong/android") |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 44 | pctx.HostBinToolVariable("sboxCmd", "sbox") |
| 45 | } |
| 46 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 47 | type SourceFileGenerator interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 48 | GeneratedSourceFiles() android.Paths |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 49 | GeneratedHeaderDirs() android.Paths |
Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 50 | GeneratedDeps() android.Paths |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 53 | // Alias for android.HostToolProvider |
| 54 | // Deprecated: use android.HostToolProvider instead. |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 55 | type HostToolProvider interface { |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 56 | android.HostToolProvider |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 57 | } |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 58 | |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 59 | type hostToolDependencyTag struct { |
| 60 | blueprint.BaseDependencyTag |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 61 | label string |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 64 | type generatorProperties struct { |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 65 | // The command to run on one or more input files. Cmd supports substitution of a few variables |
| 66 | // (the actual substitution is implemented in GenerateAndroidBuildActions below) |
| 67 | // |
| 68 | // Available variables for substitution: |
| 69 | // |
Colin Cross | 2296f5b | 2017-10-17 21:38:14 -0700 | [diff] [blame] | 70 | // $(location): the path to the first entry in tools or tool_files |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 71 | // $(location <label>): the path to the tool, tool_file, input or output with name <label> |
Colin Cross | 2296f5b | 2017-10-17 21:38:14 -0700 | [diff] [blame] | 72 | // $(in): one or more input files |
| 73 | // $(out): a single output file |
| 74 | // $(depfile): a file to which dependencies will be written, if the depfile property is set to true |
| 75 | // $(genDir): the sandbox directory for this tool; contains $(out) |
| 76 | // $$: a literal $ |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 77 | // |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 78 | // All files used must be declared as inputs (to ensure proper up-to-date checks). |
| 79 | // Use "$(in)" directly in Cmd to ensure that all inputs used are declared. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 80 | Cmd *string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 81 | |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 82 | // Enable reading a file containing dependencies in gcc format after the command completes |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 83 | Depfile *bool |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 84 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 85 | // name of the modules (if any) that produces the host executable. Leave empty for |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 86 | // prebuilts or scripts that do not need a module to build them. |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 87 | Tools []string |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 88 | |
| 89 | // Local file that is used as the tool |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 90 | Tool_files []string `android:"path"` |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 91 | |
| 92 | // List of directories to export generated headers from |
| 93 | Export_include_dirs []string |
Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 94 | |
| 95 | // list of input files |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 96 | Srcs []string `android:"path,arch_variant"` |
Dan Willemsen | eefa026 | 2018-11-17 14:01:18 -0800 | [diff] [blame] | 97 | |
| 98 | // input files to exclude |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 99 | Exclude_srcs []string `android:"path,arch_variant"` |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 102 | type Module struct { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 103 | android.ModuleBase |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 104 | android.DefaultableModuleBase |
Jiyong Park | fc752ca | 2019-06-12 13:27:29 +0900 | [diff] [blame] | 105 | android.ApexModuleBase |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 106 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 107 | // For other packages to make their own genrules with extra |
| 108 | // properties |
| 109 | Extra interface{} |
| 110 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 111 | properties generatorProperties |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 112 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 113 | taskGenerator taskFunc |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 114 | |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 115 | deps android.Paths |
| 116 | rule blueprint.Rule |
| 117 | rawCommand string |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 118 | |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 119 | exportedIncludeDirs android.Paths |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 120 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 121 | outputFiles android.Paths |
Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 122 | outputDeps android.Paths |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 123 | |
| 124 | subName string |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 127 | type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 128 | |
| 129 | type generateTask struct { |
Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 130 | in android.Paths |
| 131 | out android.WritablePaths |
| 132 | sandboxOuts []string |
| 133 | cmd string |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 136 | func (g *Module) GeneratedSourceFiles() android.Paths { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 137 | return g.outputFiles |
| 138 | } |
| 139 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 140 | func (g *Module) Srcs() android.Paths { |
Nan Zhang | e42777a | 2018-03-27 16:19:42 -0700 | [diff] [blame] | 141 | return append(android.Paths{}, g.outputFiles...) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 144 | func (g *Module) GeneratedHeaderDirs() android.Paths { |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 145 | return g.exportedIncludeDirs |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 148 | func (g *Module) GeneratedDeps() android.Paths { |
| 149 | return g.outputDeps |
| 150 | } |
| 151 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 152 | func (g *Module) DepsMutator(ctx android.BottomUpMutatorContext) { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 153 | if g, ok := ctx.Module().(*Module); ok { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 154 | for _, tool := range g.properties.Tools { |
| 155 | tag := hostToolDependencyTag{label: tool} |
| 156 | if m := android.SrcIsModule(tool); m != "" { |
| 157 | tool = m |
| 158 | } |
Dan Willemsen | 490fd49 | 2015-11-24 17:53:15 -0800 | [diff] [blame] | 159 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
Dan Willemsen | 59339a2 | 2018-07-22 21:18:45 -0700 | [diff] [blame] | 160 | {Mutator: "arch", Variation: ctx.Config().BuildOsVariant}, |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 161 | }, tag, tool) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 162 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 163 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 166 | func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 167 | g.subName = ctx.ModuleSubDir() |
| 168 | |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 169 | if len(g.properties.Export_include_dirs) > 0 { |
| 170 | for _, dir := range g.properties.Export_include_dirs { |
| 171 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, |
| 172 | android.PathForModuleGen(ctx, ctx.ModuleDir(), dir)) |
| 173 | } |
| 174 | } else { |
| 175 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, "")) |
| 176 | } |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 177 | |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 178 | locationLabels := map[string][]string{} |
| 179 | firstLabel := "" |
| 180 | |
| 181 | addLocationLabel := func(label string, paths []string) { |
| 182 | if firstLabel == "" { |
| 183 | firstLabel = label |
| 184 | } |
| 185 | if _, exists := locationLabels[label]; !exists { |
| 186 | locationLabels[label] = paths |
| 187 | } else { |
| 188 | ctx.ModuleErrorf("multiple labels for %q, %q and %q", |
| 189 | label, strings.Join(locationLabels[label], " "), strings.Join(paths, " ")) |
| 190 | } |
| 191 | } |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 192 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 193 | if len(g.properties.Tools) > 0 { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 194 | seenTools := make(map[string]bool) |
| 195 | |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 196 | ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 197 | switch tag := ctx.OtherModuleDependencyTag(module).(type) { |
| 198 | case hostToolDependencyTag: |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 199 | tool := ctx.OtherModuleName(module) |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 200 | var path android.OptionalPath |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 201 | |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 202 | if t, ok := module.(android.HostToolProvider); ok { |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 203 | if !t.(android.Module).Enabled() { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 204 | if ctx.Config().AllowMissingDependencies() { |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 205 | ctx.AddMissingDependencies([]string{tool}) |
| 206 | } else { |
| 207 | ctx.ModuleErrorf("depends on disabled module %q", tool) |
| 208 | } |
| 209 | break |
| 210 | } |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 211 | path = t.HostToolPath() |
| 212 | } else if t, ok := module.(bootstrap.GoBinaryTool); ok { |
| 213 | if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil { |
| 214 | path = android.OptionalPathForPath(android.PathForOutput(ctx, s)) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 215 | } else { |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 216 | ctx.ModuleErrorf("cannot find path for %q: %v", tool, err) |
| 217 | break |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 218 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 219 | } else { |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 220 | ctx.ModuleErrorf("%q is not a host tool provider", tool) |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 221 | break |
| 222 | } |
| 223 | |
| 224 | if path.Valid() { |
| 225 | g.deps = append(g.deps, path.Path()) |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 226 | addLocationLabel(tag.label, []string{path.Path().String()}) |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 227 | seenTools[tag.label] = true |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 228 | } else { |
| 229 | ctx.ModuleErrorf("host tool %q missing output file", tool) |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 230 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 231 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 232 | }) |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 233 | |
| 234 | // If AllowMissingDependencies is enabled, the build will not have stopped when |
| 235 | // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical |
| 236 | // "cmd: unknown location label ..." errors later. Add a dummy file to the local label. The |
| 237 | // command that uses this dummy file will never be executed because the rule will be replaced with |
| 238 | // an android.Error rule reporting the missing dependencies. |
| 239 | if ctx.Config().AllowMissingDependencies() { |
| 240 | for _, tool := range g.properties.Tools { |
| 241 | if !seenTools[tool] { |
| 242 | addLocationLabel(tool, []string{"***missing tool " + tool + "***"}) |
| 243 | } |
| 244 | } |
| 245 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 246 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 247 | |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 248 | if ctx.Failed() { |
| 249 | return |
| 250 | } |
| 251 | |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 252 | for _, toolFile := range g.properties.Tool_files { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 253 | paths := android.PathsForModuleSrc(ctx, []string{toolFile}) |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 254 | g.deps = append(g.deps, paths...) |
| 255 | addLocationLabel(toolFile, paths.Strings()) |
| 256 | } |
| 257 | |
| 258 | var srcFiles android.Paths |
| 259 | for _, in := range g.properties.Srcs { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 260 | paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs) |
| 261 | if len(missingDeps) > 0 { |
| 262 | if !ctx.Config().AllowMissingDependencies() { |
| 263 | panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator", |
| 264 | missingDeps)) |
| 265 | } |
| 266 | |
| 267 | // If AllowMissingDependencies is enabled, the build will not have stopped when |
| 268 | // the dependency was added on a missing SourceFileProducer module, which will result in nonsensical |
| 269 | // "cmd: label ":..." has no files" errors later. Add a dummy file to the local label. The |
| 270 | // command that uses this dummy file will never be executed because the rule will be replaced with |
| 271 | // an android.Error rule reporting the missing dependencies. |
| 272 | ctx.AddMissingDependencies(missingDeps) |
| 273 | addLocationLabel(in, []string{"***missing srcs " + in + "***"}) |
| 274 | } else { |
| 275 | srcFiles = append(srcFiles, paths...) |
| 276 | addLocationLabel(in, paths.Strings()) |
| 277 | } |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | task := g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles) |
| 281 | |
| 282 | for _, out := range task.out { |
| 283 | addLocationLabel(out.Rel(), []string{filepath.Join("__SBOX_OUT_DIR__", out.Rel())}) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 286 | referencedDepfile := false |
| 287 | |
Colin Cross | 2647ced | 2019-07-11 10:58:17 -0700 | [diff] [blame] | 288 | rawCommand, err := android.ExpandNinjaEscaped(task.cmd, func(name string) (string, bool, error) { |
Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 289 | // report the error directly without returning an error to android.Expand to catch multiple errors in a |
| 290 | // single run |
Colin Cross | 2647ced | 2019-07-11 10:58:17 -0700 | [diff] [blame] | 291 | reportError := func(fmt string, args ...interface{}) (string, bool, error) { |
Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 292 | ctx.PropertyErrorf("cmd", fmt, args...) |
Colin Cross | 2647ced | 2019-07-11 10:58:17 -0700 | [diff] [blame] | 293 | return "SOONG_ERROR", false, nil |
Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 296 | switch name { |
| 297 | case "location": |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 298 | if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 { |
Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 299 | return reportError("at least one `tools` or `tool_files` is required if $(location) is used") |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 300 | } |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 301 | paths := locationLabels[firstLabel] |
| 302 | if len(paths) == 0 { |
| 303 | return reportError("default label %q has no files", firstLabel) |
| 304 | } else if len(paths) > 1 { |
| 305 | return reportError("default label %q has multiple files, use $(locations %s) to reference it", |
| 306 | firstLabel, firstLabel) |
| 307 | } |
Colin Cross | 2647ced | 2019-07-11 10:58:17 -0700 | [diff] [blame] | 308 | return locationLabels[firstLabel][0], false, nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 309 | case "in": |
Colin Cross | 2647ced | 2019-07-11 10:58:17 -0700 | [diff] [blame] | 310 | return "${in}", true, nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 311 | case "out": |
Colin Cross | 2647ced | 2019-07-11 10:58:17 -0700 | [diff] [blame] | 312 | return "__SBOX_OUT_FILES__", false, nil |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 313 | case "depfile": |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 314 | referencedDepfile = true |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 315 | if !Bool(g.properties.Depfile) { |
Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 316 | return reportError("$(depfile) used without depfile property") |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 317 | } |
Colin Cross | 2647ced | 2019-07-11 10:58:17 -0700 | [diff] [blame] | 318 | return "__SBOX_DEPFILE__", false, nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 319 | case "genDir": |
Colin Cross | 2647ced | 2019-07-11 10:58:17 -0700 | [diff] [blame] | 320 | return "__SBOX_OUT_DIR__", false, nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 321 | default: |
| 322 | if strings.HasPrefix(name, "location ") { |
| 323 | label := strings.TrimSpace(strings.TrimPrefix(name, "location ")) |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 324 | if paths, ok := locationLabels[label]; ok { |
| 325 | if len(paths) == 0 { |
| 326 | return reportError("label %q has no files", label) |
| 327 | } else if len(paths) > 1 { |
| 328 | return reportError("label %q has multiple files, use $(locations %s) to reference it", |
| 329 | label, label) |
| 330 | } |
Colin Cross | 2647ced | 2019-07-11 10:58:17 -0700 | [diff] [blame] | 331 | return paths[0], false, nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 332 | } else { |
Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 333 | return reportError("unknown location label %q", label) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 334 | } |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 335 | } else if strings.HasPrefix(name, "locations ") { |
| 336 | label := strings.TrimSpace(strings.TrimPrefix(name, "locations ")) |
| 337 | if paths, ok := locationLabels[label]; ok { |
| 338 | if len(paths) == 0 { |
| 339 | return reportError("label %q has no files", label) |
| 340 | } |
Colin Cross | 2647ced | 2019-07-11 10:58:17 -0700 | [diff] [blame] | 341 | return strings.Join(paths, " "), false, nil |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 342 | } else { |
| 343 | return reportError("unknown locations label %q", label) |
| 344 | } |
| 345 | } else { |
| 346 | return reportError("unknown variable '$(%s)'", name) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 347 | } |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 348 | } |
| 349 | }) |
| 350 | |
| 351 | if err != nil { |
| 352 | ctx.PropertyErrorf("cmd", "%s", err.Error()) |
Colin Cross | 54c5dd5 | 2017-04-19 14:23:26 -0700 | [diff] [blame] | 353 | return |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 356 | if Bool(g.properties.Depfile) && !referencedDepfile { |
| 357 | ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd") |
| 358 | } |
| 359 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 360 | // tell the sbox command which directory to use as its sandbox root |
Jeff Gaston | 193f2fb | 2017-06-12 15:00:12 -0700 | [diff] [blame] | 361 | buildDir := android.PathForOutput(ctx).String() |
| 362 | sandboxPath := shared.TempDirForOutDir(buildDir) |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 363 | |
| 364 | // recall that Sprintf replaces percent sign expressions, whereas dollar signs expressions remain as written, |
| 365 | // to be replaced later by ninja_strings.go |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 366 | depfilePlaceholder := "" |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 367 | if Bool(g.properties.Depfile) { |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 368 | depfilePlaceholder = "$depfileArgs" |
| 369 | } |
Jeff Gaston | 5acec2b | 2017-11-06 14:15:16 -0800 | [diff] [blame] | 370 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 371 | genDir := android.PathForModuleGen(ctx) |
Jeff Gaston | 3160306 | 2017-12-08 12:45:35 -0800 | [diff] [blame] | 372 | // Escape the command for the shell |
| 373 | rawCommand = "'" + strings.Replace(rawCommand, "'", `'\''`, -1) + "'" |
Colin Cross | 2a07692 | 2018-10-04 23:28:25 -0700 | [diff] [blame] | 374 | g.rawCommand = rawCommand |
Jeff Gaston | 3160306 | 2017-12-08 12:45:35 -0800 | [diff] [blame] | 375 | sandboxCommand := fmt.Sprintf("$sboxCmd --sandbox-path %s --output-root %s -c %s %s $allouts", |
| 376 | sandboxPath, genDir, rawCommand, depfilePlaceholder) |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 377 | |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 378 | ruleParams := blueprint.RuleParams{ |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 379 | Command: sandboxCommand, |
| 380 | CommandDeps: []string{"$sboxCmd"}, |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 381 | } |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 382 | args := []string{"allouts"} |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 383 | if Bool(g.properties.Depfile) { |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 384 | ruleParams.Deps = blueprint.DepsGCC |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 385 | args = append(args, "depfileArgs") |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 386 | } |
| 387 | g.rule = ctx.Rule(pctx, "generator", ruleParams, args...) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 388 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 389 | g.generateSourceFile(ctx, task) |
| 390 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 393 | func (g *Module) generateSourceFile(ctx android.ModuleContext, task generateTask) { |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 394 | desc := "generate" |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 395 | if len(task.out) == 0 { |
| 396 | ctx.ModuleErrorf("must have at least one output file") |
| 397 | return |
| 398 | } |
Colin Cross | 67a5c13 | 2017-05-09 13:45:28 -0700 | [diff] [blame] | 399 | if len(task.out) == 1 { |
| 400 | desc += " " + task.out[0].Base() |
| 401 | } |
| 402 | |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 403 | var depFile android.ModuleGenPath |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 404 | if Bool(g.properties.Depfile) { |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 405 | depFile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d") |
| 406 | } |
| 407 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 408 | params := android.BuildParams{ |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 409 | Rule: g.rule, |
| 410 | Description: "generate", |
| 411 | Output: task.out[0], |
| 412 | ImplicitOutputs: task.out[1:], |
| 413 | Inputs: task.in, |
| 414 | Implicits: g.deps, |
| 415 | Args: map[string]string{ |
Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 416 | "allouts": strings.Join(task.sandboxOuts, " "), |
Colin Cross | 15e86d9 | 2017-10-20 15:07:08 -0700 | [diff] [blame] | 417 | }, |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 418 | } |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 419 | if Bool(g.properties.Depfile) { |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 420 | params.Depfile = android.PathForModuleGen(ctx, task.out[0].Rel()+".d") |
| 421 | params.Args["depfileArgs"] = "--depfile-out " + depFile.String() |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 422 | } |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 423 | |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 424 | ctx.Build(pctx, params) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 425 | |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 426 | for _, outputFile := range task.out { |
| 427 | g.outputFiles = append(g.outputFiles, outputFile) |
| 428 | } |
Dan Willemsen | ddf504c | 2019-08-09 16:21:29 -0700 | [diff] [blame^] | 429 | |
| 430 | // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of |
| 431 | // the genrules on AOSP. That will make things simpler to look at the graph in the common |
| 432 | // case. For larger sets of outputs, inject a phony target in between to limit ninja file |
| 433 | // growth. |
| 434 | if len(task.out) <= 6 { |
| 435 | g.outputDeps = g.outputFiles |
| 436 | } else { |
| 437 | phonyFile := android.PathForModuleGen(ctx, "genrule-phony") |
| 438 | |
| 439 | ctx.Build(pctx, android.BuildParams{ |
| 440 | Rule: android.Phony, |
| 441 | Output: phonyFile, |
| 442 | Inputs: g.outputFiles, |
| 443 | }) |
| 444 | |
| 445 | g.outputDeps = android.Paths{phonyFile} |
| 446 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 449 | // Collect information for opening IDE project files in java/jdeps.go. |
| 450 | func (g *Module) IDEInfo(dpInfo *android.IdeInfo) { |
| 451 | dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...) |
| 452 | for _, src := range g.properties.Srcs { |
| 453 | if strings.HasPrefix(src, ":") { |
| 454 | src = strings.Trim(src, ":") |
| 455 | dpInfo.Deps = append(dpInfo.Deps, src) |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 460 | func (g *Module) AndroidMk() android.AndroidMkData { |
| 461 | return android.AndroidMkData{ |
| 462 | Include: "$(BUILD_PHONY_PACKAGE)", |
| 463 | Class: "FAKE", |
| 464 | OutputFile: android.OptionalPathForPath(g.outputFiles[0]), |
| 465 | SubName: g.subName, |
| 466 | Extra: []android.AndroidMkExtraFunc{ |
| 467 | func(w io.Writer, outputFile android.Path) { |
| 468 | fmt.Fprintln(w, "LOCAL_ADDITIONAL_DEPENDENCIES :=", strings.Join(g.outputFiles.Strings(), " ")) |
| 469 | }, |
| 470 | }, |
| 471 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 472 | android.WriteAndroidMkData(w, data) |
| 473 | if data.SubName != "" { |
| 474 | fmt.Fprintln(w, ".PHONY:", name) |
| 475 | fmt.Fprintln(w, name, ":", name+g.subName) |
| 476 | } |
| 477 | }, |
| 478 | } |
| 479 | } |
| 480 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 481 | func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 482 | module := &Module{ |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 483 | taskGenerator: taskGenerator, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 484 | } |
| 485 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 486 | module.AddProperties(props...) |
| 487 | module.AddProperties(&module.properties) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 488 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 489 | return module |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 492 | // replace "out" with "__SBOX_OUT_DIR__/<the value of ${out}>" |
| 493 | func pathToSandboxOut(path android.Path, genDir android.Path) string { |
| 494 | relOut, err := filepath.Rel(genDir.String(), path.String()) |
| 495 | if err != nil { |
| 496 | panic(fmt.Sprintf("Could not make ${out} relative: %v", err)) |
| 497 | } |
| 498 | return filepath.Join("__SBOX_OUT_DIR__", relOut) |
| 499 | |
| 500 | } |
| 501 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 502 | func NewGenSrcs() *Module { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 503 | properties := &genSrcsProperties{} |
| 504 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 505 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask { |
| 506 | commands := []string{} |
| 507 | outFiles := android.WritablePaths{} |
Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 508 | genDir := android.PathForModuleGen(ctx) |
| 509 | sandboxOuts := []string{} |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 510 | for _, in := range srcFiles { |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 511 | outFile := android.GenPathWithExt(ctx, "", in, String(properties.Output_extension)) |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 512 | outFiles = append(outFiles, outFile) |
| 513 | |
Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 514 | sandboxOutfile := pathToSandboxOut(outFile, genDir) |
| 515 | sandboxOuts = append(sandboxOuts, sandboxOutfile) |
| 516 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 517 | command, err := android.Expand(rawCommand, func(name string) (string, error) { |
| 518 | switch name { |
| 519 | case "in": |
| 520 | return in.String(), nil |
| 521 | case "out": |
| 522 | return sandboxOutfile, nil |
| 523 | default: |
| 524 | return "$(" + name + ")", nil |
| 525 | } |
Dan Willemsen | 34cc69e | 2015-09-23 15:26:20 -0700 | [diff] [blame] | 526 | }) |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 527 | if err != nil { |
| 528 | ctx.PropertyErrorf("cmd", err.Error()) |
| 529 | } |
| 530 | |
| 531 | // escape the command in case for example it contains '#', an odd number of '"', etc |
Colin Cross | 0b9f31f | 2019-02-28 11:00:01 -0800 | [diff] [blame] | 532 | command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command)) |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 533 | commands = append(commands, command) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 534 | } |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 535 | fullCommand := strings.Join(commands, " && ") |
| 536 | |
| 537 | return generateTask{ |
Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 538 | in: srcFiles, |
| 539 | out: outFiles, |
| 540 | sandboxOuts: sandboxOuts, |
| 541 | cmd: fullCommand, |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 542 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 543 | } |
| 544 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 545 | return generatorFactory(taskGenerator, properties) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 548 | func GenSrcsFactory() android.Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 549 | m := NewGenSrcs() |
| 550 | android.InitAndroidModule(m) |
| 551 | return m |
| 552 | } |
| 553 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 554 | type genSrcsProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 555 | // extension that will be substituted for each output file |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 556 | Output_extension *string |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 559 | func NewGenRule() *Module { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 560 | properties := &genRuleProperties{} |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 561 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 562 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) generateTask { |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 563 | outs := make(android.WritablePaths, len(properties.Out)) |
Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 564 | sandboxOuts := make([]string, len(properties.Out)) |
| 565 | genDir := android.PathForModuleGen(ctx) |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 566 | for i, out := range properties.Out { |
| 567 | outs[i] = android.PathForModuleGen(ctx, out) |
Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 568 | sandboxOuts[i] = pathToSandboxOut(outs[i], genDir) |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 569 | } |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 570 | return generateTask{ |
Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 571 | in: srcFiles, |
| 572 | out: outs, |
| 573 | sandboxOuts: sandboxOuts, |
| 574 | cmd: rawCommand, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 575 | } |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 576 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 577 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 578 | return generatorFactory(taskGenerator, properties) |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 581 | func GenRuleFactory() android.Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 582 | m := NewGenRule() |
| 583 | android.InitAndroidModule(m) |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 584 | android.InitDefaultableModule(m) |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 585 | return m |
| 586 | } |
| 587 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 588 | type genRuleProperties struct { |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 589 | // names of the output files that will be generated |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 590 | Out []string `android:"arch_variant"` |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 591 | } |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 592 | |
| 593 | var Bool = proptools.Bool |
| 594 | var String = proptools.String |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 595 | |
| 596 | // |
| 597 | // Defaults |
| 598 | // |
| 599 | type Defaults struct { |
| 600 | android.ModuleBase |
| 601 | android.DefaultsModuleBase |
| 602 | } |
| 603 | |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 604 | func defaultsFactory() android.Module { |
| 605 | return DefaultsFactory() |
| 606 | } |
| 607 | |
| 608 | func DefaultsFactory(props ...interface{}) android.Module { |
| 609 | module := &Defaults{} |
| 610 | |
| 611 | module.AddProperties(props...) |
| 612 | module.AddProperties( |
| 613 | &generatorProperties{}, |
| 614 | &genRuleProperties{}, |
| 615 | ) |
| 616 | |
| 617 | android.InitDefaultsModule(module) |
| 618 | |
| 619 | return module |
| 620 | } |