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