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