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 | |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 15 | // A genrule module takes a list of source files ("srcs" property), an optional |
| 16 | // list of tools ("tools" property), and a command line ("cmd" property), to |
| 17 | // generate output files ("out" property). |
| 18 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 19 | package genrule |
| 20 | |
| 21 | import ( |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 22 | "fmt" |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 23 | "io" |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 24 | "path/filepath" |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 25 | "strconv" |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 26 | "strings" |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 27 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint" |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 29 | "github.com/google/blueprint/bootstrap" |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 30 | "github.com/google/blueprint/proptools" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 31 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 32 | "android/soong/android" |
Jingwen Chen | 30f5aaa | 2020-11-19 05:38:02 -0500 | [diff] [blame] | 33 | "android/soong/bazel" |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 34 | ) |
| 35 | |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 36 | func init() { |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 37 | RegisterGenruleBuildComponents(android.InitRegistrationContext) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 38 | } |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 39 | |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 40 | func RegisterGenruleBuildComponents(ctx android.RegistrationContext) { |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 41 | ctx.RegisterModuleType("genrule_defaults", defaultsFactory) |
| 42 | |
| 43 | ctx.RegisterModuleType("gensrcs", GenSrcsFactory) |
| 44 | ctx.RegisterModuleType("genrule", GenRuleFactory) |
| 45 | |
| 46 | ctx.FinalDepsMutators(func(ctx android.RegisterMutatorsContext) { |
| 47 | ctx.BottomUp("genrule_tool_deps", toolDepsMutator).Parallel() |
| 48 | }) |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 49 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 50 | android.DepsBp2BuildMutators(RegisterGenruleBp2BuildDeps) |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 51 | android.RegisterBp2BuildMutator("genrule", GenruleBp2Build) |
Colin Cross | 463a90e | 2015-06-17 14:20:06 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 54 | func RegisterGenruleBp2BuildDeps(ctx android.RegisterMutatorsContext) { |
| 55 | ctx.BottomUp("genrule_tool_deps", toolDepsMutator) |
| 56 | } |
| 57 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 58 | var ( |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 59 | pctx = android.NewPackageContext("android/soong/genrule") |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 60 | |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 61 | // Used by gensrcs when there is more than 1 shard to merge the outputs |
| 62 | // of each shard into a zip file. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 63 | gensrcsMerge = pctx.AndroidStaticRule("gensrcsMerge", blueprint.RuleParams{ |
| 64 | Command: "${soongZip} -o ${tmpZip} @${tmpZip}.rsp && ${zipSync} -d ${genDir} ${tmpZip}", |
| 65 | CommandDeps: []string{"${soongZip}", "${zipSync}"}, |
| 66 | Rspfile: "${tmpZip}.rsp", |
| 67 | RspfileContent: "${zipArgs}", |
| 68 | }, "tmpZip", "genDir", "zipArgs") |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 69 | ) |
| 70 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 71 | func init() { |
Dan Willemsen | ddf504c | 2019-08-09 16:21:29 -0700 | [diff] [blame] | 72 | pctx.Import("android/soong/android") |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 73 | pctx.HostBinToolVariable("sboxCmd", "sbox") |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 74 | |
| 75 | pctx.HostBinToolVariable("soongZip", "soong_zip") |
| 76 | pctx.HostBinToolVariable("zipSync", "zipsync") |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 79 | type SourceFileGenerator interface { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 80 | GeneratedSourceFiles() android.Paths |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 81 | GeneratedHeaderDirs() android.Paths |
Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 82 | GeneratedDeps() android.Paths |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 85 | // Alias for android.HostToolProvider |
| 86 | // Deprecated: use android.HostToolProvider instead. |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 87 | type HostToolProvider interface { |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 88 | android.HostToolProvider |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 89 | } |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 90 | |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 91 | type hostToolDependencyTag struct { |
| 92 | blueprint.BaseDependencyTag |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 93 | label string |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 94 | } |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 95 | type generatorProperties struct { |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 96 | // 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] | 97 | // |
| 98 | // Available variables for substitution: |
| 99 | // |
Colin Cross | 2296f5b | 2017-10-17 21:38:14 -0700 | [diff] [blame] | 100 | // $(location): the path to the first entry in tools or tool_files |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 101 | // $(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] | 102 | // $(in): one or more input files |
| 103 | // $(out): a single output file |
| 104 | // $(depfile): a file to which dependencies will be written, if the depfile property is set to true |
| 105 | // $(genDir): the sandbox directory for this tool; contains $(out) |
| 106 | // $$: a literal $ |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 107 | Cmd *string |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 108 | |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 109 | // 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] | 110 | Depfile *bool |
Colin Cross | 33bfb0a | 2016-11-21 17:23:08 -0800 | [diff] [blame] | 111 | |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 112 | // 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] | 113 | // prebuilts or scripts that do not need a module to build them. |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 114 | Tools []string |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 115 | |
| 116 | // Local file that is used as the tool |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 117 | Tool_files []string `android:"path"` |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 118 | |
| 119 | // List of directories to export generated headers from |
| 120 | Export_include_dirs []string |
Colin Cross | 708c424 | 2017-01-13 18:05:49 -0800 | [diff] [blame] | 121 | |
| 122 | // list of input files |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 123 | Srcs []string `android:"path,arch_variant"` |
Dan Willemsen | eefa026 | 2018-11-17 14:01:18 -0800 | [diff] [blame] | 124 | |
| 125 | // input files to exclude |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 126 | Exclude_srcs []string `android:"path,arch_variant"` |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 127 | |
Jingwen Chen | 30f5aaa | 2020-11-19 05:38:02 -0500 | [diff] [blame] | 128 | // Properties for Bazel migration purposes. |
| 129 | bazel.Properties |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 130 | } |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 131 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 132 | type Module struct { |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 133 | android.ModuleBase |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 134 | android.DefaultableModuleBase |
Jiyong Park | fc752ca | 2019-06-12 13:27:29 +0900 | [diff] [blame] | 135 | android.ApexModuleBase |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 136 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 137 | // For other packages to make their own genrules with extra |
| 138 | // properties |
| 139 | Extra interface{} |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 140 | android.ImageInterface |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 141 | |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 142 | properties generatorProperties |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 143 | |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 144 | // For the different tasks that genrule and gensrc generate. genrule will |
| 145 | // generate 1 task, and gensrc will generate 1 or more tasks based on the |
| 146 | // number of shards the input files are sharded into. |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 147 | taskGenerator taskFunc |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 148 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 149 | rule blueprint.Rule |
| 150 | rawCommands []string |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 151 | |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 152 | exportedIncludeDirs android.Paths |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 153 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 154 | outputFiles android.Paths |
Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 155 | outputDeps android.Paths |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 156 | |
| 157 | subName string |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 158 | subDir string |
bralee | 1fbf440 | 2020-05-21 10:11:59 +0800 | [diff] [blame] | 159 | |
| 160 | // Collect the module directory for IDE info in java/jdeps.go. |
| 161 | modulePaths []string |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 164 | type taskFunc func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 165 | |
| 166 | type generateTask struct { |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 167 | in android.Paths |
| 168 | out android.WritablePaths |
| 169 | depFile android.WritablePath |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 170 | copyTo android.WritablePaths // For gensrcs to set on gensrcsMerge rule. |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 171 | genDir android.WritablePath |
| 172 | extraTools android.Paths // dependencies on tools used by the generator |
| 173 | |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 174 | cmd string |
| 175 | // For gensrsc sharding. |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 176 | shard int |
| 177 | shards int |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 180 | func (g *Module) GeneratedSourceFiles() android.Paths { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 181 | return g.outputFiles |
| 182 | } |
| 183 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 184 | func (g *Module) Srcs() android.Paths { |
Nan Zhang | e42777a | 2018-03-27 16:19:42 -0700 | [diff] [blame] | 185 | return append(android.Paths{}, g.outputFiles...) |
Colin Cross | 068e0fe | 2016-12-13 15:23:47 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 188 | func (g *Module) GeneratedHeaderDirs() android.Paths { |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 189 | return g.exportedIncludeDirs |
Dan Willemsen | b40aab6 | 2016-04-20 14:21:14 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Dan Willemsen | 9da9d49 | 2018-02-21 18:28:18 -0800 | [diff] [blame] | 192 | func (g *Module) GeneratedDeps() android.Paths { |
| 193 | return g.outputDeps |
| 194 | } |
| 195 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 196 | func toolDepsMutator(ctx android.BottomUpMutatorContext) { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 197 | if g, ok := ctx.Module().(*Module); ok { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 198 | for _, tool := range g.properties.Tools { |
| 199 | tag := hostToolDependencyTag{label: tool} |
| 200 | if m := android.SrcIsModule(tool); m != "" { |
| 201 | tool = m |
| 202 | } |
Colin Cross | 0f7d2ef | 2019-10-16 11:03:10 -0700 | [diff] [blame] | 203 | ctx.AddFarVariationDependencies(ctx.Config().BuildOSTarget.Variations(), tag, tool) |
Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 204 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 205 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 208 | // Returns true if information was available from Bazel, false if bazel invocation still needs to occur. |
| 209 | func (c *Module) generateBazelBuildActions(ctx android.ModuleContext, label string) bool { |
| 210 | bazelCtx := ctx.Config().BazelContext |
| 211 | filePaths, ok := bazelCtx.GetAllFiles(label) |
| 212 | if ok { |
| 213 | var bazelOutputFiles android.Paths |
| 214 | for _, bazelOutputFile := range filePaths { |
Chris Parsons | dbcb1ff | 2020-12-10 17:19:18 -0500 | [diff] [blame] | 215 | bazelOutputFiles = append(bazelOutputFiles, android.PathForBazelOut(ctx, bazelOutputFile)) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 216 | } |
| 217 | c.outputFiles = bazelOutputFiles |
| 218 | c.outputDeps = bazelOutputFiles |
| 219 | } |
| 220 | return ok |
| 221 | } |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 222 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 223 | func (g *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 224 | g.subName = ctx.ModuleSubDir() |
| 225 | |
bralee | 1fbf440 | 2020-05-21 10:11:59 +0800 | [diff] [blame] | 226 | // Collect the module directory for IDE info in java/jdeps.go. |
| 227 | g.modulePaths = append(g.modulePaths, ctx.ModuleDir()) |
| 228 | |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 229 | if len(g.properties.Export_include_dirs) > 0 { |
| 230 | for _, dir := range g.properties.Export_include_dirs { |
| 231 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 232 | android.PathForModuleGen(ctx, g.subDir, ctx.ModuleDir(), dir)) |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 233 | } |
| 234 | } else { |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 235 | g.exportedIncludeDirs = append(g.exportedIncludeDirs, android.PathForModuleGen(ctx, g.subDir)) |
Colin Cross | 5ed99c6 | 2016-11-22 12:55:55 -0800 | [diff] [blame] | 236 | } |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 237 | |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 238 | locationLabels := map[string][]string{} |
| 239 | firstLabel := "" |
| 240 | |
| 241 | addLocationLabel := func(label string, paths []string) { |
| 242 | if firstLabel == "" { |
| 243 | firstLabel = label |
| 244 | } |
| 245 | if _, exists := locationLabels[label]; !exists { |
| 246 | locationLabels[label] = paths |
| 247 | } else { |
| 248 | ctx.ModuleErrorf("multiple labels for %q, %q and %q", |
| 249 | label, strings.Join(locationLabels[label], " "), strings.Join(paths, " ")) |
| 250 | } |
| 251 | } |
Dan Willemsen | 3f4539b | 2016-09-28 16:19:10 -0700 | [diff] [blame] | 252 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 253 | var tools android.Paths |
| 254 | var packagedTools []android.PackagingSpec |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 255 | if len(g.properties.Tools) > 0 { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 256 | seenTools := make(map[string]bool) |
| 257 | |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 258 | ctx.VisitDirectDepsBlueprint(func(module blueprint.Module) { |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 259 | switch tag := ctx.OtherModuleDependencyTag(module).(type) { |
| 260 | case hostToolDependencyTag: |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 261 | tool := ctx.OtherModuleName(module) |
| 262 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 263 | switch t := module.(type) { |
| 264 | case android.HostToolProvider: |
| 265 | // A HostToolProvider provides the path to a tool, which will be copied |
| 266 | // into the sandbox. |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 267 | if !t.(android.Module).Enabled() { |
Colin Cross | 6510f91 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 268 | if ctx.Config().AllowMissingDependencies() { |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 269 | ctx.AddMissingDependencies([]string{tool}) |
| 270 | } else { |
| 271 | ctx.ModuleErrorf("depends on disabled module %q", tool) |
| 272 | } |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 273 | return |
Colin Cross | 35143d0 | 2017-11-16 00:11:20 -0800 | [diff] [blame] | 274 | } |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 275 | path := t.HostToolPath() |
| 276 | if !path.Valid() { |
| 277 | ctx.ModuleErrorf("host tool %q missing output file", tool) |
| 278 | return |
| 279 | } |
| 280 | if specs := t.TransitivePackagingSpecs(); specs != nil { |
| 281 | // If the HostToolProvider has PackgingSpecs, which are definitions of the |
| 282 | // required relative locations of the tool and its dependencies, use those |
| 283 | // instead. They will be copied to those relative locations in the sbox |
| 284 | // sandbox. |
| 285 | packagedTools = append(packagedTools, specs...) |
| 286 | // Assume that the first PackagingSpec of the module is the tool. |
| 287 | addLocationLabel(tag.label, []string{android.SboxPathForPackagedTool(specs[0])}) |
| 288 | } else { |
| 289 | tools = append(tools, path.Path()) |
| 290 | addLocationLabel(tag.label, []string{android.SboxPathForTool(ctx, path.Path())}) |
| 291 | } |
| 292 | case bootstrap.GoBinaryTool: |
| 293 | // A GoBinaryTool provides the install path to a tool, which will be copied. |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 294 | if s, err := filepath.Rel(android.PathForOutput(ctx).String(), t.InstallPath()); err == nil { |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 295 | toolPath := android.PathForOutput(ctx, s) |
| 296 | tools = append(tools, toolPath) |
| 297 | addLocationLabel(tag.label, []string{android.SboxPathForTool(ctx, toolPath)}) |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 298 | } else { |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 299 | ctx.ModuleErrorf("cannot find path for %q: %v", tool, err) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 300 | return |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 301 | } |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 302 | default: |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 303 | ctx.ModuleErrorf("%q is not a host tool provider", tool) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 304 | return |
Dan Willemsen | 8eded0a | 2017-09-13 16:07:44 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 307 | seenTools[tag.label] = true |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 308 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 309 | }) |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 310 | |
| 311 | // If AllowMissingDependencies is enabled, the build will not have stopped when |
| 312 | // AddFarVariationDependencies was called on a missing tool, which will result in nonsensical |
Liz Kammer | 20ebfb4 | 2020-07-28 11:32:07 -0700 | [diff] [blame] | 313 | // "cmd: unknown location label ..." errors later. Add a placeholder file to the local label. |
| 314 | // The command that uses this placeholder file will never be executed because the rule will be |
| 315 | // replaced with an android.Error rule reporting the missing dependencies. |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 316 | if ctx.Config().AllowMissingDependencies() { |
| 317 | for _, tool := range g.properties.Tools { |
| 318 | if !seenTools[tool] { |
| 319 | addLocationLabel(tool, []string{"***missing tool " + tool + "***"}) |
| 320 | } |
| 321 | } |
| 322 | } |
Dan Willemsen | f7f3d69 | 2016-04-20 14:54:32 -0700 | [diff] [blame] | 323 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 324 | |
Dan Willemsen | d6ba0d5 | 2017-09-13 15:46:47 -0700 | [diff] [blame] | 325 | if ctx.Failed() { |
| 326 | return |
| 327 | } |
| 328 | |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 329 | for _, toolFile := range g.properties.Tool_files { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 330 | paths := android.PathsForModuleSrc(ctx, []string{toolFile}) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 331 | tools = append(tools, paths...) |
| 332 | var sandboxPaths []string |
| 333 | for _, path := range paths { |
| 334 | sandboxPaths = append(sandboxPaths, android.SboxPathForTool(ctx, path)) |
| 335 | } |
| 336 | addLocationLabel(toolFile, sandboxPaths) |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | var srcFiles android.Paths |
| 340 | for _, in := range g.properties.Srcs { |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 341 | paths, missingDeps := android.PathsAndMissingDepsForModuleSrcExcludes(ctx, []string{in}, g.properties.Exclude_srcs) |
| 342 | if len(missingDeps) > 0 { |
| 343 | if !ctx.Config().AllowMissingDependencies() { |
| 344 | panic(fmt.Errorf("should never get here, the missing dependencies %q should have been reported in DepsMutator", |
| 345 | missingDeps)) |
| 346 | } |
| 347 | |
| 348 | // If AllowMissingDependencies is enabled, the build will not have stopped when |
| 349 | // 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] | 350 | // "cmd: label ":..." has no files" errors later. Add a placeholder file to the local label. |
| 351 | // The command that uses this placeholder file will never be executed because the rule will be |
| 352 | // replaced with an android.Error rule reporting the missing dependencies. |
Colin Cross | ba71a3f | 2019-03-18 12:12:48 -0700 | [diff] [blame] | 353 | ctx.AddMissingDependencies(missingDeps) |
| 354 | addLocationLabel(in, []string{"***missing srcs " + in + "***"}) |
| 355 | } else { |
| 356 | srcFiles = append(srcFiles, paths...) |
| 357 | addLocationLabel(in, paths.Strings()) |
| 358 | } |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 361 | var copyFrom android.Paths |
| 362 | var outputFiles android.WritablePaths |
| 363 | var zipArgs strings.Builder |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 364 | |
Alex Humesky | 29e3bbe | 2020-11-20 21:30:13 -0500 | [diff] [blame] | 365 | // Generate tasks, either from genrule or gensrcs. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 366 | for _, task := range g.taskGenerator(ctx, String(g.properties.Cmd), srcFiles) { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 367 | if len(task.out) == 0 { |
| 368 | ctx.ModuleErrorf("must have at least one output file") |
| 369 | return |
Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 372 | // Pick a unique path outside the task.genDir for the sbox manifest textproto, |
| 373 | // a unique rule name, and the user-visible description. |
| 374 | manifestName := "genrule.sbox.textproto" |
| 375 | desc := "generate" |
| 376 | name := "generator" |
| 377 | if task.shards > 0 { |
| 378 | manifestName = "genrule_" + strconv.Itoa(task.shard) + ".sbox.textproto" |
| 379 | desc += " " + strconv.Itoa(task.shard) |
| 380 | name += strconv.Itoa(task.shard) |
| 381 | } else if len(task.out) == 1 { |
| 382 | desc += " " + task.out[0].Base() |
| 383 | } |
| 384 | |
| 385 | manifestPath := android.PathForModuleOut(ctx, manifestName) |
| 386 | |
| 387 | // Use a RuleBuilder to create a rule that runs the command inside an sbox sandbox. |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 388 | rule := android.NewRuleBuilder(pctx, ctx).Sbox(task.genDir, manifestPath).SandboxTools() |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 389 | cmd := rule.Command() |
| 390 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 391 | for _, out := range task.out { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 392 | addLocationLabel(out.Rel(), []string{cmd.PathForOutput(out)}) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 395 | referencedDepfile := false |
| 396 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 397 | rawCommand, err := android.Expand(task.cmd, func(name string) (string, error) { |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 398 | // report the error directly without returning an error to android.Expand to catch multiple errors in a |
| 399 | // single run |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 400 | reportError := func(fmt string, args ...interface{}) (string, error) { |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 401 | ctx.PropertyErrorf("cmd", fmt, args...) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 402 | return "SOONG_ERROR", nil |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 403 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 404 | |
| 405 | switch name { |
| 406 | case "location": |
| 407 | if len(g.properties.Tools) == 0 && len(g.properties.Tool_files) == 0 { |
| 408 | 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] | 409 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 410 | paths := locationLabels[firstLabel] |
| 411 | if len(paths) == 0 { |
| 412 | return reportError("default label %q has no files", firstLabel) |
| 413 | } else if len(paths) > 1 { |
| 414 | return reportError("default label %q has multiple files, use $(locations %s) to reference it", |
| 415 | firstLabel, firstLabel) |
Colin Cross | 08f15ab | 2018-10-04 23:29:14 -0700 | [diff] [blame] | 416 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 417 | return locationLabels[firstLabel][0], nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 418 | case "in": |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 419 | return strings.Join(srcFiles.Strings(), " "), nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 420 | case "out": |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 421 | var sandboxOuts []string |
| 422 | for _, out := range task.out { |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 423 | sandboxOuts = append(sandboxOuts, cmd.PathForOutput(out)) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 424 | } |
| 425 | return strings.Join(sandboxOuts, " "), nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 426 | case "depfile": |
| 427 | referencedDepfile = true |
| 428 | if !Bool(g.properties.Depfile) { |
| 429 | return reportError("$(depfile) used without depfile property") |
| 430 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 431 | return "__SBOX_DEPFILE__", nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 432 | case "genDir": |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 433 | return cmd.PathForOutput(task.genDir), nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 434 | default: |
| 435 | if strings.HasPrefix(name, "location ") { |
| 436 | label := strings.TrimSpace(strings.TrimPrefix(name, "location ")) |
| 437 | if paths, ok := locationLabels[label]; ok { |
| 438 | if len(paths) == 0 { |
| 439 | return reportError("label %q has no files", label) |
| 440 | } else if len(paths) > 1 { |
| 441 | return reportError("label %q has multiple files, use $(locations %s) to reference it", |
| 442 | label, label) |
| 443 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 444 | return paths[0], nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 445 | } else { |
| 446 | return reportError("unknown location label %q", label) |
| 447 | } |
| 448 | } else if strings.HasPrefix(name, "locations ") { |
| 449 | label := strings.TrimSpace(strings.TrimPrefix(name, "locations ")) |
| 450 | if paths, ok := locationLabels[label]; ok { |
| 451 | if len(paths) == 0 { |
| 452 | return reportError("label %q has no files", label) |
| 453 | } |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 454 | return strings.Join(paths, " "), nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 455 | } else { |
| 456 | return reportError("unknown locations label %q", label) |
| 457 | } |
| 458 | } else { |
| 459 | return reportError("unknown variable '$(%s)'", name) |
| 460 | } |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 461 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 462 | }) |
| 463 | |
| 464 | if err != nil { |
| 465 | ctx.PropertyErrorf("cmd", "%s", err.Error()) |
| 466 | return |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 467 | } |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 468 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 469 | if Bool(g.properties.Depfile) && !referencedDepfile { |
| 470 | ctx.PropertyErrorf("cmd", "specified depfile=true but did not include a reference to '${depfile}' in cmd") |
| 471 | return |
| 472 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 473 | g.rawCommands = append(g.rawCommands, rawCommand) |
Bill Peckham | c087be1 | 2020-02-13 15:55:10 -0800 | [diff] [blame] | 474 | |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 475 | cmd.Text(rawCommand) |
| 476 | cmd.ImplicitOutputs(task.out) |
| 477 | cmd.Implicits(task.in) |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 478 | cmd.ImplicitTools(tools) |
| 479 | cmd.ImplicitTools(task.extraTools) |
| 480 | cmd.ImplicitPackagedTools(packagedTools) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 481 | if Bool(g.properties.Depfile) { |
| 482 | cmd.ImplicitDepFile(task.depFile) |
| 483 | } |
| 484 | |
| 485 | // Create the rule to run the genrule command inside sbox. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 486 | rule.Build(name, desc) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 487 | |
| 488 | if len(task.copyTo) > 0 { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 489 | // If copyTo is set, multiple shards need to be copied into a single directory. |
| 490 | // task.out contains the per-shard paths, and copyTo contains the corresponding |
| 491 | // final path. The files need to be copied into the final directory by a |
| 492 | // single rule so it can remove the directory before it starts to ensure no |
| 493 | // old files remain. zipsync already does this, so build up zipArgs that |
| 494 | // zip all the per-shard directories into a single zip. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 495 | outputFiles = append(outputFiles, task.copyTo...) |
| 496 | copyFrom = append(copyFrom, task.out.Paths()...) |
| 497 | zipArgs.WriteString(" -C " + task.genDir.String()) |
| 498 | zipArgs.WriteString(android.JoinWithPrefix(task.out.Strings(), " -f ")) |
| 499 | } else { |
| 500 | outputFiles = append(outputFiles, task.out...) |
| 501 | } |
Colin Cross | 6f080df | 2016-11-04 15:32:58 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 504 | if len(copyFrom) > 0 { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 505 | // Create a rule that zips all the per-shard directories into a single zip and then |
| 506 | // uses zipsync to unzip it into the final directory. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 507 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 508 | Rule: gensrcsMerge, |
| 509 | Implicits: copyFrom, |
| 510 | Outputs: outputFiles, |
| 511 | Description: "merge shards", |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 512 | Args: map[string]string{ |
| 513 | "zipArgs": zipArgs.String(), |
| 514 | "tmpZip": android.PathForModuleGen(ctx, g.subDir+".zip").String(), |
| 515 | "genDir": android.PathForModuleGen(ctx, g.subDir).String(), |
| 516 | }, |
| 517 | }) |
Colin Cross | 85a2e89 | 2018-07-09 09:45:06 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 520 | g.outputFiles = outputFiles.Paths() |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 521 | |
Chris Parsons | aa8be05 | 2020-10-14 16:22:37 -0400 | [diff] [blame] | 522 | bazelModuleLabel := g.properties.Bazel_module.Label |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 523 | bazelActionsUsed := false |
| 524 | if ctx.Config().BazelContext.BazelEnabled() && len(bazelModuleLabel) > 0 { |
| 525 | bazelActionsUsed = g.generateBazelBuildActions(ctx, bazelModuleLabel) |
Jeff Gaston | 02a684b | 2017-10-27 14:59:27 -0700 | [diff] [blame] | 526 | } |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 527 | if !bazelActionsUsed { |
| 528 | // For <= 6 outputs, just embed those directly in the users. Right now, that covers >90% of |
| 529 | // the genrules on AOSP. That will make things simpler to look at the graph in the common |
| 530 | // case. For larger sets of outputs, inject a phony target in between to limit ninja file |
| 531 | // growth. |
| 532 | if len(g.outputFiles) <= 6 { |
| 533 | g.outputDeps = g.outputFiles |
| 534 | } else { |
| 535 | phonyFile := android.PathForModuleGen(ctx, "genrule-phony") |
| 536 | ctx.Build(pctx, android.BuildParams{ |
| 537 | Rule: blueprint.Phony, |
| 538 | Output: phonyFile, |
| 539 | Inputs: g.outputFiles, |
| 540 | }) |
| 541 | g.outputDeps = android.Paths{phonyFile} |
| 542 | } |
| 543 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 544 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 545 | |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 546 | // Collect information for opening IDE project files in java/jdeps.go. |
| 547 | func (g *Module) IDEInfo(dpInfo *android.IdeInfo) { |
| 548 | dpInfo.Srcs = append(dpInfo.Srcs, g.Srcs().Strings()...) |
| 549 | for _, src := range g.properties.Srcs { |
| 550 | if strings.HasPrefix(src, ":") { |
| 551 | src = strings.Trim(src, ":") |
| 552 | dpInfo.Deps = append(dpInfo.Deps, src) |
| 553 | } |
| 554 | } |
bralee | 1fbf440 | 2020-05-21 10:11:59 +0800 | [diff] [blame] | 555 | dpInfo.Paths = append(dpInfo.Paths, g.modulePaths...) |
Brandon Lee | 5d45c6f | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 558 | func (g *Module) AndroidMk() android.AndroidMkData { |
| 559 | return android.AndroidMkData{ |
Anton Hansson | 72f1849 | 2020-10-30 16:34:45 +0000 | [diff] [blame] | 560 | Class: "ETC", |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 561 | OutputFile: android.OptionalPathForPath(g.outputFiles[0]), |
| 562 | SubName: g.subName, |
| 563 | Extra: []android.AndroidMkExtraFunc{ |
| 564 | func(w io.Writer, outputFile android.Path) { |
Anton Hansson | 72f1849 | 2020-10-30 16:34:45 +0000 | [diff] [blame] | 565 | fmt.Fprintln(w, "LOCAL_UNINSTALLABLE_MODULE := true") |
Colin Cross | a4ad2b0 | 2019-03-18 22:15:32 -0700 | [diff] [blame] | 566 | }, |
| 567 | }, |
| 568 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 569 | android.WriteAndroidMkData(w, data) |
| 570 | if data.SubName != "" { |
| 571 | fmt.Fprintln(w, ".PHONY:", name) |
| 572 | fmt.Fprintln(w, name, ":", name+g.subName) |
| 573 | } |
| 574 | }, |
| 575 | } |
| 576 | } |
| 577 | |
Jiyong Park | 45bf82e | 2020-12-15 22:29:02 +0900 | [diff] [blame] | 578 | var _ android.ApexModule = (*Module)(nil) |
| 579 | |
| 580 | // Implements android.ApexModule |
Dan Albert | c806053 | 2020-07-22 22:32:17 -0700 | [diff] [blame] | 581 | func (g *Module) ShouldSupportSdkVersion(ctx android.BaseModuleContext, |
| 582 | sdkVersion android.ApiLevel) error { |
Jooyung Han | 749dc69 | 2020-04-15 11:03:39 +0900 | [diff] [blame] | 583 | // Because generated outputs are checked by client modules(e.g. cc_library, ...) |
| 584 | // we can safely ignore the check here. |
| 585 | return nil |
| 586 | } |
| 587 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 588 | func generatorFactory(taskGenerator taskFunc, props ...interface{}) *Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 589 | module := &Module{ |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 590 | taskGenerator: taskGenerator, |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 593 | module.AddProperties(props...) |
| 594 | module.AddProperties(&module.properties) |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 595 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 596 | module.ImageInterface = noopImageInterface{} |
| 597 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 598 | return module |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 601 | type noopImageInterface struct{} |
| 602 | |
| 603 | func (x noopImageInterface) ImageMutatorBegin(android.BaseModuleContext) {} |
| 604 | func (x noopImageInterface) CoreVariantNeeded(android.BaseModuleContext) bool { return false } |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 605 | func (x noopImageInterface) RamdiskVariantNeeded(android.BaseModuleContext) bool { return false } |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 606 | func (x noopImageInterface) VendorRamdiskVariantNeeded(android.BaseModuleContext) bool { return false } |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 607 | func (x noopImageInterface) RecoveryVariantNeeded(android.BaseModuleContext) bool { return false } |
| 608 | func (x noopImageInterface) ExtraImageVariations(ctx android.BaseModuleContext) []string { return nil } |
| 609 | func (x noopImageInterface) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { |
| 610 | } |
| 611 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 612 | func NewGenSrcs() *Module { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 613 | properties := &genSrcsProperties{} |
| 614 | |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 615 | // finalSubDir is the name of the subdirectory that output files will be generated into. |
| 616 | // It is used so that per-shard directories can be placed alongside it an then finally |
| 617 | // merged into it. |
| 618 | const finalSubDir = "gensrcs" |
| 619 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 620 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask { |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 621 | shardSize := defaultShardSize |
| 622 | if s := properties.Shard_size; s != nil { |
| 623 | shardSize = int(*s) |
| 624 | } |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 625 | |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 626 | // gensrcs rules can easily hit command line limits by repeating the command for |
| 627 | // every input file. Shard the input files into groups. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 628 | shards := android.ShardPaths(srcFiles, shardSize) |
| 629 | var generateTasks []generateTask |
Colin Cross | baccf5b | 2018-02-21 14:07:48 -0800 | [diff] [blame] | 630 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 631 | for i, shard := range shards { |
| 632 | var commands []string |
| 633 | var outFiles android.WritablePaths |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 634 | var commandDepFiles []string |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 635 | var copyTo android.WritablePaths |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 636 | |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 637 | // When sharding is enabled (i.e. len(shards) > 1), the sbox rules for each |
| 638 | // shard will be write to their own directories and then be merged together |
| 639 | // into finalSubDir. If sharding is not enabled (i.e. len(shards) == 1), |
| 640 | // the sbox rule will write directly to finalSubDir. |
| 641 | genSubDir := finalSubDir |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 642 | if len(shards) > 1 { |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 643 | genSubDir = strconv.Itoa(i) |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 644 | } |
| 645 | |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 646 | genDir := android.PathForModuleGen(ctx, genSubDir) |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 647 | // TODO(ccross): this RuleBuilder is a hack to be able to call |
| 648 | // rule.Command().PathForOutput. Replace this with passing the rule into the |
| 649 | // generator. |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 650 | rule := android.NewRuleBuilder(pctx, ctx).Sbox(genDir, nil).SandboxTools() |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 651 | |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 652 | for _, in := range shard { |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 653 | outFile := android.GenPathWithExt(ctx, finalSubDir, in, String(properties.Output_extension)) |
| 654 | |
| 655 | // If sharding is enabled, then outFile is the path to the output file in |
| 656 | // the shard directory, and copyTo is the path to the output file in the |
| 657 | // final directory. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 658 | if len(shards) > 1 { |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 659 | shardFile := android.GenPathWithExt(ctx, genSubDir, in, String(properties.Output_extension)) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 660 | copyTo = append(copyTo, outFile) |
| 661 | outFile = shardFile |
| 662 | } |
| 663 | |
| 664 | outFiles = append(outFiles, outFile) |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 665 | |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 666 | // pre-expand the command line to replace $in and $out with references to |
| 667 | // a single input and output file. |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 668 | command, err := android.Expand(rawCommand, func(name string) (string, error) { |
| 669 | switch name { |
| 670 | case "in": |
| 671 | return in.String(), nil |
| 672 | case "out": |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 673 | return rule.Command().PathForOutput(outFile), nil |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 674 | case "depfile": |
| 675 | // Generate a depfile for each output file. Store the list for |
| 676 | // later in order to combine them all into a single depfile. |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 677 | depFile := rule.Command().PathForOutput(outFile.ReplaceExtension(ctx, "d")) |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 678 | commandDepFiles = append(commandDepFiles, depFile) |
| 679 | return depFile, nil |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 680 | default: |
| 681 | return "$(" + name + ")", nil |
| 682 | } |
| 683 | }) |
| 684 | if err != nil { |
| 685 | ctx.PropertyErrorf("cmd", err.Error()) |
| 686 | } |
| 687 | |
| 688 | // escape the command in case for example it contains '#', an odd number of '"', etc |
| 689 | command = fmt.Sprintf("bash -c %v", proptools.ShellEscape(command)) |
| 690 | commands = append(commands, command) |
| 691 | } |
| 692 | fullCommand := strings.Join(commands, " && ") |
| 693 | |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 694 | var outputDepfile android.WritablePath |
| 695 | var extraTools android.Paths |
| 696 | if len(commandDepFiles) > 0 { |
| 697 | // Each command wrote to a depfile, but ninja can only handle one |
| 698 | // depfile per rule. Use the dep_fixer tool at the end of the |
| 699 | // command to combine all the depfiles into a single output depfile. |
| 700 | outputDepfile = android.PathForModuleGen(ctx, genSubDir, "gensrcs.d") |
| 701 | depFixerTool := ctx.Config().HostToolPath(ctx, "dep_fixer") |
| 702 | fullCommand += fmt.Sprintf(" && %s -o $(depfile) %s", |
Colin Cross | ba9e403 | 2020-11-24 16:32:22 -0800 | [diff] [blame] | 703 | android.SboxPathForTool(ctx, depFixerTool), |
| 704 | strings.Join(commandDepFiles, " ")) |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 705 | extraTools = append(extraTools, depFixerTool) |
| 706 | } |
| 707 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 708 | generateTasks = append(generateTasks, generateTask{ |
Colin Cross | 3ea4eb8 | 2020-11-24 13:07:27 -0800 | [diff] [blame] | 709 | in: shard, |
| 710 | out: outFiles, |
| 711 | depFile: outputDepfile, |
| 712 | copyTo: copyTo, |
| 713 | genDir: genDir, |
| 714 | cmd: fullCommand, |
| 715 | shard: i, |
| 716 | shards: len(shards), |
| 717 | extraTools: extraTools, |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 718 | }) |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 719 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 720 | |
| 721 | return generateTasks |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 724 | g := generatorFactory(taskGenerator, properties) |
Colin Cross | f188596 | 2020-11-20 15:28:30 -0800 | [diff] [blame] | 725 | g.subDir = finalSubDir |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 726 | return g |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 729 | func GenSrcsFactory() android.Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 730 | m := NewGenSrcs() |
| 731 | android.InitAndroidModule(m) |
| 732 | return m |
| 733 | } |
| 734 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 735 | type genSrcsProperties struct { |
Colin Cross | 7d5136f | 2015-05-11 13:39:40 -0700 | [diff] [blame] | 736 | // extension that will be substituted for each output file |
Nan Zhang | a5e7cb4 | 2017-11-09 22:42:32 -0800 | [diff] [blame] | 737 | Output_extension *string |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 738 | |
| 739 | // maximum number of files that will be passed on a single command line. |
| 740 | Shard_size *int64 |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Evgenii Stepanov | f47c90d | 2020-12-02 18:55:09 -0800 | [diff] [blame] | 743 | const defaultShardSize = 50 |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 744 | |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 745 | func NewGenRule() *Module { |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 746 | properties := &genRuleProperties{} |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 747 | |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 748 | taskGenerator := func(ctx android.ModuleContext, rawCommand string, srcFiles android.Paths) []generateTask { |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 749 | outs := make(android.WritablePaths, len(properties.Out)) |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 750 | var depFile android.WritablePath |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 751 | for i, out := range properties.Out { |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 752 | outPath := android.PathForModuleGen(ctx, out) |
| 753 | if i == 0 { |
| 754 | depFile = outPath.ReplaceExtension(ctx, "d") |
| 755 | } |
| 756 | outs[i] = outPath |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 757 | } |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 758 | return []generateTask{{ |
Colin Cross | 3d68051 | 2020-11-13 16:23:53 -0800 | [diff] [blame] | 759 | in: srcFiles, |
| 760 | out: outs, |
| 761 | depFile: depFile, |
| 762 | genDir: android.PathForModuleGen(ctx), |
| 763 | cmd: rawCommand, |
Colin Cross | 1a52768 | 2019-09-23 15:55:30 -0700 | [diff] [blame] | 764 | }} |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 765 | } |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 766 | |
Jeff Gaston | 437d23c | 2017-11-08 12:38:00 -0800 | [diff] [blame] | 767 | return generatorFactory(taskGenerator, properties) |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Colin Cross | 54190b3 | 2017-10-09 15:34:10 -0700 | [diff] [blame] | 770 | func GenRuleFactory() android.Module { |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 771 | m := NewGenRule() |
| 772 | android.InitAndroidModule(m) |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 773 | android.InitDefaultableModule(m) |
Dan Willemsen | 3e5bdf2 | 2017-09-13 18:37:08 -0700 | [diff] [blame] | 774 | return m |
| 775 | } |
| 776 | |
Colin Cross | d350ecd | 2015-04-28 13:25:36 -0700 | [diff] [blame] | 777 | type genRuleProperties struct { |
Dan Willemsen | 9c8681f | 2016-09-28 16:21:00 -0700 | [diff] [blame] | 778 | // names of the output files that will be generated |
Colin Cross | ef35448 | 2018-10-23 11:27:50 -0700 | [diff] [blame] | 779 | Out []string `android:"arch_variant"` |
Colin Cross | 5049f02 | 2015-03-18 13:28:46 -0700 | [diff] [blame] | 780 | } |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 781 | |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 782 | type bazelGenruleAttributes struct { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 783 | Srcs bazel.LabelList |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 784 | Outs []string |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 785 | Tools bazel.LabelList |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 786 | Cmd string |
| 787 | } |
| 788 | |
| 789 | type bazelGenrule struct { |
| 790 | android.BazelTargetModuleBase |
| 791 | bazelGenruleAttributes |
| 792 | } |
| 793 | |
| 794 | func BazelGenruleFactory() android.Module { |
| 795 | module := &bazelGenrule{} |
| 796 | module.AddProperties(&module.bazelGenruleAttributes) |
| 797 | android.InitBazelTargetModule(module) |
| 798 | return module |
| 799 | } |
| 800 | |
Jingwen Chen | a42d641 | 2021-01-26 21:57:27 -0500 | [diff] [blame] | 801 | func GenruleBp2Build(ctx android.TopDownMutatorContext) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 802 | m, ok := ctx.Module().(*Module) |
| 803 | if !ok { |
| 804 | return |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 805 | } |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame^] | 806 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 807 | // Bazel only has the "tools" attribute. |
| 808 | tools := android.BazelLabelForModuleDeps(ctx, m.properties.Tools) |
| 809 | tool_files := android.BazelLabelForModuleSrc(ctx, m.properties.Tool_files) |
| 810 | tools.Append(tool_files) |
| 811 | |
| 812 | srcs := android.BazelLabelForModuleSrc(ctx, m.properties.Srcs) |
| 813 | |
| 814 | var allReplacements bazel.LabelList |
| 815 | allReplacements.Append(tools) |
| 816 | allReplacements.Append(srcs) |
| 817 | |
| 818 | // Replace in and out variables with $< and $@ |
| 819 | var cmd string |
| 820 | if m.properties.Cmd != nil { |
| 821 | cmd = strings.Replace(*m.properties.Cmd, "$(in)", "$(SRCS)", -1) |
| 822 | cmd = strings.Replace(cmd, "$(out)", "$(OUTS)", -1) |
| 823 | cmd = strings.Replace(cmd, "$(genDir)", "$(GENDIR)", -1) |
| 824 | if len(tools.Includes) > 0 { |
| 825 | cmd = strings.Replace(cmd, "$(location)", fmt.Sprintf("$(location %s)", tools.Includes[0].Label), -1) |
| 826 | cmd = strings.Replace(cmd, "$(locations)", fmt.Sprintf("$(locations %s)", tools.Includes[0].Label), -1) |
| 827 | } |
| 828 | for _, l := range allReplacements.Includes { |
| 829 | bpLoc := fmt.Sprintf("$(location %s)", l.Bp_text) |
| 830 | bpLocs := fmt.Sprintf("$(locations %s)", l.Bp_text) |
| 831 | bazelLoc := fmt.Sprintf("$(location %s)", l.Label) |
| 832 | bazelLocs := fmt.Sprintf("$(locations %s)", l.Label) |
| 833 | cmd = strings.Replace(cmd, bpLoc, bazelLoc, -1) |
| 834 | cmd = strings.Replace(cmd, bpLocs, bazelLocs, -1) |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | // The Out prop is not in an immediately accessible field |
| 839 | // in the Module struct, so use GetProperties and cast it |
| 840 | // to the known struct prop. |
| 841 | var outs []string |
| 842 | for _, propIntf := range m.GetProperties() { |
| 843 | if props, ok := propIntf.(*genRuleProperties); ok { |
| 844 | outs = props.Out |
| 845 | break |
| 846 | } |
| 847 | } |
| 848 | |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame^] | 849 | attrs := &bazelGenruleAttributes{ |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 850 | Srcs: srcs, |
| 851 | Outs: outs, |
| 852 | Cmd: cmd, |
| 853 | Tools: tools, |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame^] | 854 | } |
| 855 | |
| 856 | // Can we automate this? |
| 857 | name := "__bp2build__" + m.Name() |
| 858 | props := bazel.BazelTargetModuleProperties{ |
| 859 | Name: &name, |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 860 | Rule_class: "genrule", |
Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame^] | 861 | } |
| 862 | |
| 863 | // Create the BazelTargetModule. |
| 864 | ctx.CreateBazelTargetModule(BazelGenruleFactory, props, attrs) |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | func (m *bazelGenrule) Name() string { |
| 868 | return m.BaseModuleName() |
| 869 | } |
| 870 | |
| 871 | func (m *bazelGenrule) GenerateAndroidBuildActions(ctx android.ModuleContext) {} |
| 872 | |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 873 | var Bool = proptools.Bool |
| 874 | var String = proptools.String |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 875 | |
| 876 | // |
| 877 | // Defaults |
| 878 | // |
| 879 | type Defaults struct { |
| 880 | android.ModuleBase |
| 881 | android.DefaultsModuleBase |
| 882 | } |
| 883 | |
Jaewoong Jung | 98716bd | 2018-12-10 08:13:18 -0800 | [diff] [blame] | 884 | func defaultsFactory() android.Module { |
| 885 | return DefaultsFactory() |
| 886 | } |
| 887 | |
| 888 | func DefaultsFactory(props ...interface{}) android.Module { |
| 889 | module := &Defaults{} |
| 890 | |
| 891 | module.AddProperties(props...) |
| 892 | module.AddProperties( |
| 893 | &generatorProperties{}, |
| 894 | &genRuleProperties{}, |
| 895 | ) |
| 896 | |
| 897 | android.InitDefaultsModule(module) |
| 898 | |
| 899 | return module |
| 900 | } |