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