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