Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 1 | // Copyright (C) 2019 The Android Open Source Project |
| 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 apex |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Jaewoong Jung | 8cf307e | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 19 | "strconv" |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 20 | "strings" |
| 21 | |
| 22 | "android/soong/android" |
Jaewoong Jung | 8cf307e | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 23 | "android/soong/java" |
Jooyung Han | 2963716 | 2020-06-30 06:34:23 +0900 | [diff] [blame^] | 24 | |
Jaewoong Jung | 8cf307e | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 25 | "github.com/google/blueprint" |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 26 | |
| 27 | "github.com/google/blueprint/proptools" |
| 28 | ) |
| 29 | |
Jaewoong Jung | 8cf307e | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 30 | var ( |
| 31 | extractMatchingApex = pctx.StaticRule( |
| 32 | "extractMatchingApex", |
| 33 | blueprint.RuleParams{ |
| 34 | Command: `rm -rf "$out" && ` + |
| 35 | `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` + |
| 36 | `-sdk-version=${sdk-version} -abis=${abis} -screen-densities=all -extract-single ` + |
| 37 | `${in}`, |
| 38 | CommandDeps: []string{"${extract_apks}"}, |
| 39 | }, |
| 40 | "abis", "allow-prereleased", "sdk-version") |
| 41 | ) |
| 42 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 43 | type Prebuilt struct { |
| 44 | android.ModuleBase |
| 45 | prebuilt android.Prebuilt |
| 46 | |
| 47 | properties PrebuiltProperties |
| 48 | |
| 49 | inputApex android.Path |
| 50 | installDir android.InstallPath |
| 51 | installFilename string |
| 52 | outputApex android.WritablePath |
Jooyung Han | 002ab68 | 2020-01-08 01:57:58 +0900 | [diff] [blame] | 53 | |
| 54 | // list of commands to create symlinks for backward compatibility. |
| 55 | // these commands will be attached as LOCAL_POST_INSTALL_CMD |
| 56 | compatSymlinks []string |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | type PrebuiltProperties struct { |
| 60 | // the path to the prebuilt .apex file to import. |
| 61 | Source string `blueprint:"mutated"` |
| 62 | ForceDisable bool `blueprint:"mutated"` |
| 63 | |
| 64 | Src *string |
| 65 | Arch struct { |
| 66 | Arm struct { |
| 67 | Src *string |
| 68 | } |
| 69 | Arm64 struct { |
| 70 | Src *string |
| 71 | } |
| 72 | X86 struct { |
| 73 | Src *string |
| 74 | } |
| 75 | X86_64 struct { |
| 76 | Src *string |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | Installable *bool |
| 81 | // Optional name for the installed apex. If unspecified, name of the |
| 82 | // module is used as the file name |
| 83 | Filename *string |
| 84 | |
| 85 | // Names of modules to be overridden. Listed modules can only be other binaries |
| 86 | // (in Make or Soong). |
| 87 | // This does not completely prevent installation of the overridden binaries, but if both |
| 88 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 89 | // from PRODUCT_PACKAGES. |
| 90 | Overrides []string |
| 91 | } |
| 92 | |
| 93 | func (p *Prebuilt) installable() bool { |
| 94 | return p.properties.Installable == nil || proptools.Bool(p.properties.Installable) |
| 95 | } |
| 96 | |
| 97 | func (p *Prebuilt) isForceDisabled() bool { |
| 98 | return p.properties.ForceDisable |
| 99 | } |
| 100 | |
| 101 | func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) { |
| 102 | switch tag { |
| 103 | case "": |
| 104 | return android.Paths{p.outputApex}, nil |
| 105 | default: |
| 106 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func (p *Prebuilt) InstallFilename() string { |
| 111 | return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix) |
| 112 | } |
| 113 | |
| 114 | func (p *Prebuilt) Prebuilt() *android.Prebuilt { |
| 115 | return &p.prebuilt |
| 116 | } |
| 117 | |
| 118 | func (p *Prebuilt) Name() string { |
| 119 | return p.prebuilt.Name(p.ModuleBase.Name()) |
| 120 | } |
| 121 | |
| 122 | // prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex. |
| 123 | func PrebuiltFactory() android.Module { |
| 124 | module := &Prebuilt{} |
| 125 | module.AddProperties(&module.properties) |
| 126 | android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source") |
| 127 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 128 | return module |
| 129 | } |
| 130 | |
| 131 | func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 132 | // If the device is configured to use flattened APEX, force disable the prebuilt because |
| 133 | // the prebuilt is a non-flattened one. |
| 134 | forceDisable := ctx.Config().FlattenApex() |
| 135 | |
| 136 | // Force disable the prebuilts when we are doing unbundled build. We do unbundled build |
| 137 | // to build the prebuilts themselves. |
| 138 | forceDisable = forceDisable || ctx.Config().UnbundledBuild() |
| 139 | |
| 140 | // Force disable the prebuilts when coverage is enabled. |
| 141 | forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled() |
| 142 | forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") |
| 143 | |
| 144 | // b/137216042 don't use prebuilts when address sanitizer is on |
| 145 | forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) || |
| 146 | android.InList("hwaddress", ctx.Config().SanitizeDevice()) |
| 147 | |
| 148 | if forceDisable && p.prebuilt.SourceExists() { |
| 149 | p.properties.ForceDisable = true |
| 150 | return |
| 151 | } |
| 152 | |
| 153 | // This is called before prebuilt_select and prebuilt_postdeps mutators |
| 154 | // The mutators requires that src to be set correctly for each arch so that |
| 155 | // arch variants are disabled when src is not provided for the arch. |
| 156 | if len(ctx.MultiTargets()) != 1 { |
| 157 | ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex") |
| 158 | return |
| 159 | } |
| 160 | var src string |
| 161 | switch ctx.MultiTargets()[0].Arch.ArchType { |
| 162 | case android.Arm: |
| 163 | src = String(p.properties.Arch.Arm.Src) |
| 164 | case android.Arm64: |
| 165 | src = String(p.properties.Arch.Arm64.Src) |
| 166 | case android.X86: |
| 167 | src = String(p.properties.Arch.X86.Src) |
| 168 | case android.X86_64: |
| 169 | src = String(p.properties.Arch.X86_64.Src) |
| 170 | default: |
| 171 | ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String()) |
| 172 | return |
| 173 | } |
| 174 | if src == "" { |
| 175 | src = String(p.properties.Src) |
| 176 | } |
| 177 | p.properties.Source = src |
| 178 | } |
| 179 | |
| 180 | func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 181 | if p.properties.ForceDisable { |
| 182 | return |
| 183 | } |
| 184 | |
| 185 | // TODO(jungjw): Check the key validity. |
| 186 | p.inputApex = p.Prebuilt().SingleSourcePath(ctx) |
| 187 | p.installDir = android.PathForModuleInstall(ctx, "apex") |
| 188 | p.installFilename = p.InstallFilename() |
| 189 | if !strings.HasSuffix(p.installFilename, imageApexSuffix) { |
| 190 | ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix) |
| 191 | } |
| 192 | p.outputApex = android.PathForModuleOut(ctx, p.installFilename) |
| 193 | ctx.Build(pctx, android.BuildParams{ |
| 194 | Rule: android.Cp, |
| 195 | Input: p.inputApex, |
| 196 | Output: p.outputApex, |
| 197 | }) |
| 198 | if p.installable() { |
| 199 | ctx.InstallFile(p.installDir, p.installFilename, p.inputApex) |
| 200 | } |
| 201 | |
Jooyung Han | 002ab68 | 2020-01-08 01:57:58 +0900 | [diff] [blame] | 202 | // in case that prebuilt_apex replaces source apex (using prefer: prop) |
| 203 | p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx) |
| 204 | // or that prebuilt_apex overrides other apexes (using overrides: prop) |
| 205 | for _, overridden := range p.properties.Overrides { |
| 206 | p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...) |
| 207 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 208 | } |
| 209 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 210 | func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries { |
| 211 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 212 | Class: "ETC", |
| 213 | OutputFile: android.OptionalPathForPath(p.inputApex), |
| 214 | Include: "$(BUILD_PREBUILT)", |
| 215 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 216 | func(entries *android.AndroidMkEntries) { |
| 217 | entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String()) |
| 218 | entries.SetString("LOCAL_MODULE_STEM", p.installFilename) |
| 219 | entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable()) |
| 220 | entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...) |
Jooyung Han | 002ab68 | 2020-01-08 01:57:58 +0900 | [diff] [blame] | 221 | if len(p.compatSymlinks) > 0 { |
| 222 | entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && ")) |
| 223 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 224 | }, |
| 225 | }, |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 226 | }} |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 227 | } |
Jaewoong Jung | 8cf307e | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 228 | |
| 229 | type ApexSet struct { |
| 230 | android.ModuleBase |
| 231 | prebuilt android.Prebuilt |
| 232 | |
| 233 | properties ApexSetProperties |
| 234 | |
| 235 | installDir android.InstallPath |
| 236 | installFilename string |
| 237 | outputApex android.WritablePath |
| 238 | |
| 239 | // list of commands to create symlinks for backward compatibility. |
| 240 | // these commands will be attached as LOCAL_POST_INSTALL_CMD |
| 241 | compatSymlinks []string |
Jooyung Han | 2963716 | 2020-06-30 06:34:23 +0900 | [diff] [blame^] | 242 | |
| 243 | hostRequired []string |
| 244 | postInstallCommands []string |
Jaewoong Jung | 8cf307e | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | type ApexSetProperties struct { |
| 248 | // the .apks file path that contains prebuilt apex files to be extracted. |
| 249 | Set *string |
| 250 | |
| 251 | // whether the extracted apex file installable. |
| 252 | Installable *bool |
| 253 | |
| 254 | // optional name for the installed apex. If unspecified, name of the |
| 255 | // module is used as the file name |
| 256 | Filename *string |
| 257 | |
| 258 | // names of modules to be overridden. Listed modules can only be other binaries |
| 259 | // (in Make or Soong). |
| 260 | // This does not completely prevent installation of the overridden binaries, but if both |
| 261 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 262 | // from PRODUCT_PACKAGES. |
| 263 | Overrides []string |
| 264 | |
| 265 | // apexes in this set use prerelease SDK version |
| 266 | Prerelease *bool |
| 267 | } |
| 268 | |
| 269 | func (a *ApexSet) installable() bool { |
| 270 | return a.properties.Installable == nil || proptools.Bool(a.properties.Installable) |
| 271 | } |
| 272 | |
| 273 | func (a *ApexSet) InstallFilename() string { |
| 274 | return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix) |
| 275 | } |
| 276 | |
| 277 | func (a *ApexSet) Prebuilt() *android.Prebuilt { |
| 278 | return &a.prebuilt |
| 279 | } |
| 280 | |
| 281 | func (a *ApexSet) Name() string { |
| 282 | return a.prebuilt.Name(a.ModuleBase.Name()) |
| 283 | } |
| 284 | |
Jiyong Park | 8d6286b | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 285 | func (a *ApexSet) Overrides() []string { |
| 286 | return a.properties.Overrides |
| 287 | } |
| 288 | |
Jaewoong Jung | 8cf307e | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 289 | // prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex. |
| 290 | func apexSetFactory() android.Module { |
| 291 | module := &ApexSet{} |
| 292 | module.AddProperties(&module.properties) |
| 293 | android.InitSingleSourcePrebuiltModule(module, &module.properties, "Set") |
| 294 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 295 | return module |
| 296 | } |
| 297 | |
| 298 | func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 299 | a.installFilename = a.InstallFilename() |
| 300 | if !strings.HasSuffix(a.installFilename, imageApexSuffix) { |
| 301 | ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix) |
| 302 | } |
| 303 | |
| 304 | apexSet := a.prebuilt.SingleSourcePath(ctx) |
| 305 | a.outputApex = android.PathForModuleOut(ctx, a.installFilename) |
| 306 | ctx.Build(pctx, |
| 307 | android.BuildParams{ |
| 308 | Rule: extractMatchingApex, |
| 309 | Description: "Extract an apex from an apex set", |
| 310 | Inputs: android.Paths{apexSet}, |
| 311 | Output: a.outputApex, |
| 312 | Args: map[string]string{ |
| 313 | "abis": strings.Join(java.SupportedAbis(ctx), ","), |
| 314 | "allow-prereleased": strconv.FormatBool(proptools.Bool(a.properties.Prerelease)), |
| 315 | "sdk-version": ctx.Config().PlatformSdkVersion(), |
| 316 | }, |
| 317 | }) |
| 318 | a.installDir = android.PathForModuleInstall(ctx, "apex") |
| 319 | if a.installable() { |
| 320 | ctx.InstallFile(a.installDir, a.installFilename, a.outputApex) |
| 321 | } |
| 322 | |
| 323 | // in case that apex_set replaces source apex (using prefer: prop) |
| 324 | a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx) |
| 325 | // or that apex_set overrides other apexes (using overrides: prop) |
| 326 | for _, overridden := range a.properties.Overrides { |
| 327 | a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...) |
| 328 | } |
Jooyung Han | 2963716 | 2020-06-30 06:34:23 +0900 | [diff] [blame^] | 329 | |
| 330 | if ctx.Config().InstallExtraFlattenedApexes() { |
| 331 | // flattened apex should be in /system_ext/apex |
| 332 | flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName()) |
| 333 | a.postInstallCommands = append(a.postInstallCommands, |
| 334 | fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s", |
| 335 | a.outputApex.String(), |
| 336 | flattenedApexDir.ToMakePath().String(), |
| 337 | )) |
| 338 | a.hostRequired = []string{"deapexer", "debugfs"} |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | type systemExtContext struct { |
| 343 | android.ModuleContext |
| 344 | } |
| 345 | |
| 346 | func (*systemExtContext) SystemExtSpecific() bool { |
| 347 | return true |
Jaewoong Jung | 8cf307e | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries { |
| 351 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
Jooyung Han | 2963716 | 2020-06-30 06:34:23 +0900 | [diff] [blame^] | 352 | Class: "ETC", |
| 353 | OutputFile: android.OptionalPathForPath(a.outputApex), |
| 354 | Include: "$(BUILD_PREBUILT)", |
| 355 | Host_required: a.hostRequired, |
Jaewoong Jung | 8cf307e | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 356 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 357 | func(entries *android.AndroidMkEntries) { |
| 358 | entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String()) |
| 359 | entries.SetString("LOCAL_MODULE_STEM", a.installFilename) |
| 360 | entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable()) |
| 361 | entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...) |
Jooyung Han | 2963716 | 2020-06-30 06:34:23 +0900 | [diff] [blame^] | 362 | postInstallCommands := append([]string{}, a.postInstallCommands...) |
| 363 | postInstallCommands = append(postInstallCommands, a.compatSymlinks...) |
| 364 | if len(postInstallCommands) > 0 { |
| 365 | entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && ")) |
Jaewoong Jung | 8cf307e | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 366 | } |
| 367 | }, |
| 368 | }, |
| 369 | }} |
| 370 | } |