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