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 | fa00c06 | 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 | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 23 | "android/soong/java" |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 24 | |
Jaewoong Jung | fa00c06 | 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 | fa00c06 | 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 | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 43 | type prebuilt interface { |
| 44 | isForceDisabled() bool |
| 45 | InstallFilename() string |
| 46 | } |
| 47 | |
| 48 | type prebuiltCommon struct { |
| 49 | prebuilt android.Prebuilt |
| 50 | properties prebuiltCommonProperties |
| 51 | } |
| 52 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 53 | type sanitizedPrebuilt interface { |
| 54 | hasSanitizedSource(sanitizer string) bool |
| 55 | } |
| 56 | |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 57 | type prebuiltCommonProperties struct { |
| 58 | ForceDisable bool `blueprint:"mutated"` |
| 59 | } |
| 60 | |
| 61 | func (p *prebuiltCommon) Prebuilt() *android.Prebuilt { |
| 62 | return &p.prebuilt |
| 63 | } |
| 64 | |
| 65 | func (p *prebuiltCommon) isForceDisabled() bool { |
| 66 | return p.properties.ForceDisable |
| 67 | } |
| 68 | |
| 69 | func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool { |
| 70 | // If the device is configured to use flattened APEX, force disable the prebuilt because |
| 71 | // the prebuilt is a non-flattened one. |
| 72 | forceDisable := ctx.Config().FlattenApex() |
| 73 | |
| 74 | // Force disable the prebuilts when we are doing unbundled build. We do unbundled build |
| 75 | // to build the prebuilts themselves. |
| 76 | forceDisable = forceDisable || ctx.Config().UnbundledBuild() |
| 77 | |
| 78 | // Force disable the prebuilts when coverage is enabled. |
| 79 | forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled() |
| 80 | forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT") |
| 81 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 82 | // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source |
| 83 | sanitized := ctx.Module().(sanitizedPrebuilt) |
| 84 | forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address")) |
| 85 | forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress")) |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 86 | |
| 87 | if forceDisable && p.prebuilt.SourceExists() { |
| 88 | p.properties.ForceDisable = true |
| 89 | return true |
| 90 | } |
| 91 | return false |
| 92 | } |
| 93 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 94 | type Prebuilt struct { |
| 95 | android.ModuleBase |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 96 | prebuiltCommon |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 97 | |
| 98 | properties PrebuiltProperties |
| 99 | |
| 100 | inputApex android.Path |
| 101 | installDir android.InstallPath |
| 102 | installFilename string |
| 103 | outputApex android.WritablePath |
Jooyung Han | 002ab68 | 2020-01-08 01:57:58 +0900 | [diff] [blame] | 104 | |
| 105 | // list of commands to create symlinks for backward compatibility. |
| 106 | // these commands will be attached as LOCAL_POST_INSTALL_CMD |
| 107 | compatSymlinks []string |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 108 | } |
| 109 | |
Paul Duffin | 851f399 | 2021-01-13 17:03:51 +0000 | [diff] [blame] | 110 | type ApexFileProperties struct { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 111 | // the path to the prebuilt .apex file to import. |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 112 | Source string `blueprint:"mutated"` |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 113 | |
| 114 | Src *string |
| 115 | Arch struct { |
| 116 | Arm struct { |
| 117 | Src *string |
| 118 | } |
| 119 | Arm64 struct { |
| 120 | Src *string |
| 121 | } |
| 122 | X86 struct { |
| 123 | Src *string |
| 124 | } |
| 125 | X86_64 struct { |
| 126 | Src *string |
| 127 | } |
| 128 | } |
Paul Duffin | 851f399 | 2021-01-13 17:03:51 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | func (p *ApexFileProperties) selectSource(ctx android.BottomUpMutatorContext) error { |
| 132 | // This is called before prebuilt_select and prebuilt_postdeps mutators |
| 133 | // The mutators requires that src to be set correctly for each arch so that |
| 134 | // arch variants are disabled when src is not provided for the arch. |
| 135 | if len(ctx.MultiTargets()) != 1 { |
| 136 | return fmt.Errorf("compile_multilib shouldn't be \"both\" for prebuilt_apex") |
| 137 | } |
| 138 | var src string |
| 139 | switch ctx.MultiTargets()[0].Arch.ArchType { |
| 140 | case android.Arm: |
| 141 | src = String(p.Arch.Arm.Src) |
| 142 | case android.Arm64: |
| 143 | src = String(p.Arch.Arm64.Src) |
| 144 | case android.X86: |
| 145 | src = String(p.Arch.X86.Src) |
| 146 | case android.X86_64: |
| 147 | src = String(p.Arch.X86_64.Src) |
| 148 | default: |
| 149 | return fmt.Errorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String()) |
| 150 | } |
| 151 | if src == "" { |
| 152 | src = String(p.Src) |
| 153 | } |
| 154 | p.Source = src |
| 155 | |
| 156 | return nil |
| 157 | } |
| 158 | |
| 159 | type PrebuiltProperties struct { |
| 160 | ApexFileProperties |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 161 | DeapexerProperties |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 162 | |
| 163 | Installable *bool |
| 164 | // Optional name for the installed apex. If unspecified, name of the |
| 165 | // module is used as the file name |
| 166 | Filename *string |
| 167 | |
| 168 | // Names of modules to be overridden. Listed modules can only be other binaries |
| 169 | // (in Make or Soong). |
| 170 | // This does not completely prevent installation of the overridden binaries, but if both |
| 171 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 172 | // from PRODUCT_PACKAGES. |
| 173 | Overrides []string |
| 174 | } |
| 175 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 176 | func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool { |
| 177 | return false |
| 178 | } |
| 179 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 180 | func (p *Prebuilt) installable() bool { |
| 181 | return p.properties.Installable == nil || proptools.Bool(p.properties.Installable) |
| 182 | } |
| 183 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 184 | func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) { |
| 185 | switch tag { |
| 186 | case "": |
| 187 | return android.Paths{p.outputApex}, nil |
| 188 | default: |
| 189 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | func (p *Prebuilt) InstallFilename() string { |
| 194 | return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix) |
| 195 | } |
| 196 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 197 | func (p *Prebuilt) Name() string { |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 198 | return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name()) |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex. |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 202 | // |
| 203 | // If this needs to make files from within a `.apex` file available for use by other Soong modules, |
| 204 | // e.g. make dex implementation jars available for java_import modules isted in exported_java_libs, |
| 205 | // it does so as follows: |
| 206 | // |
| 207 | // 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and |
| 208 | // makes them available for use by other modules, at both Soong and ninja levels. |
| 209 | // |
| 210 | // 2. It adds a dependency onto those modules and creates an apex specific variant similar to what |
| 211 | // an `apex` module does. That ensures that code which looks for specific apex variant, e.g. |
| 212 | // dexpreopt, will work the same way from source and prebuilt. |
| 213 | // |
| 214 | // 3. The `deapexer` module adds a dependency from the modules that require the exported files onto |
| 215 | // itself so that they can retrieve the file paths to those files. |
| 216 | // |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 217 | func PrebuiltFactory() android.Module { |
| 218 | module := &Prebuilt{} |
| 219 | module.AddProperties(&module.properties) |
| 220 | android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source") |
| 221 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 222 | |
| 223 | android.AddLoadHook(module, func(ctx android.LoadHookContext) { |
| 224 | props := struct { |
| 225 | Name *string |
| 226 | }{ |
| 227 | Name: proptools.StringPtr(module.BaseModuleName() + ".deapexer"), |
| 228 | } |
| 229 | ctx.CreateModule(privateDeapexerFactory, |
| 230 | &props, |
| 231 | &module.properties.ApexFileProperties, |
| 232 | &module.properties.DeapexerProperties, |
| 233 | ) |
| 234 | }) |
| 235 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 236 | return module |
| 237 | } |
| 238 | |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 239 | func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string { |
| 240 | // The prebuilt_apex should be depending on prebuilt modules but as this runs after |
| 241 | // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So, |
| 242 | // check to see if the prefixed name is in use first, if it is then use that, otherwise assume |
| 243 | // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module |
| 244 | // and not a renamed prebuilt module then that will be detected and reported as an error when |
| 245 | // processing the dependency in ApexInfoMutator(). |
| 246 | prebuiltName := "prebuilt_" + name |
| 247 | if ctx.OtherModuleExists(prebuiltName) { |
| 248 | name = prebuiltName |
| 249 | } |
| 250 | return name |
| 251 | } |
| 252 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 253 | func prebuiltSelectSourceMutator(ctx android.BottomUpMutatorContext) { |
| 254 | p, ok := ctx.Module().(*Prebuilt) |
| 255 | if !ok { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 256 | return |
| 257 | } |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 258 | if err := p.properties.selectSource(ctx); err != nil { |
| 259 | ctx.ModuleErrorf("%s", err) |
| 260 | } |
| 261 | } |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 262 | |
Paul Duffin | a713942 | 2021-02-08 11:01:58 +0000 | [diff] [blame^] | 263 | type exportedDependencyTag struct { |
| 264 | blueprint.BaseDependencyTag |
| 265 | name string |
| 266 | } |
| 267 | |
| 268 | // Mark this tag so dependencies that use it are excluded from visibility enforcement. |
| 269 | // |
| 270 | // This does allow any prebuilt_apex to reference any module which does open up a small window for |
| 271 | // restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so |
| 272 | // avoids opening up a much bigger window by widening the visibility of modules that need files |
| 273 | // provided by the prebuilt_apex to include all the possible locations they may be defined, which |
| 274 | // could include everything below vendor/. |
| 275 | // |
| 276 | // A prebuilt_apex that references a module via this tag will have to contain the appropriate files |
| 277 | // corresponding to that module, otherwise it will fail when attempting to retrieve the files from |
| 278 | // the .apex file. It will also have to be included in the module's apex_available property too. |
| 279 | // That makes it highly unlikely that a prebuilt_apex would reference a restricted module |
| 280 | // incorrectly. |
| 281 | func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {} |
| 282 | |
| 283 | var ( |
| 284 | exportedJavaLibTag = exportedDependencyTag{name: "exported_java_lib"} |
| 285 | ) |
| 286 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 287 | func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 288 | // Add dependencies onto the java modules that represent the java libraries that are provided by |
| 289 | // and exported from this prebuilt apex. |
| 290 | for _, lib := range p.properties.Exported_java_libs { |
| 291 | dep := prebuiltApexExportedModuleName(ctx, lib) |
Paul Duffin | a713942 | 2021-02-08 11:01:58 +0000 | [diff] [blame^] | 292 | ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), exportedJavaLibTag, dep) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | var _ ApexInfoMutator = (*Prebuilt)(nil) |
| 297 | |
| 298 | // ApexInfoMutator marks any modules for which this apex exports a file as requiring an apex |
| 299 | // specific variant and checks that they are supported. |
| 300 | // |
| 301 | // The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are |
| 302 | // associated with the apex specific variant using the ApexInfoProvider for later retrieval. |
| 303 | // |
| 304 | // Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants |
| 305 | // across prebuilt_apex modules. That is because there is no way to determine whether two |
| 306 | // prebuilt_apex modules that export files for the same module are compatible. e.g. they could have |
| 307 | // been built from different source at different times or they could have been built with different |
| 308 | // build options that affect the libraries. |
| 309 | // |
| 310 | // While it may be possible to provide sufficient information to determine whether two prebuilt_apex |
| 311 | // modules were compatible it would be a lot of work and would not provide much benefit for a couple |
| 312 | // of reasons: |
| 313 | // * The number of prebuilt_apex modules that will be exporting files for the same module will be |
| 314 | // low as the prebuilt_apex only exports files for the direct dependencies that require it and |
| 315 | // very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a |
| 316 | // few com.android.art* apex files that contain the same contents and could export files for the |
| 317 | // same modules but only one of them needs to do so. Contrast that with source apex modules which |
| 318 | // need apex specific variants for every module that contributes code to the apex, whether direct |
| 319 | // or indirect. |
| 320 | // * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some |
| 321 | // extra copying of files. Contrast that with source apex modules that has to build each variant |
| 322 | // from source. |
| 323 | func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) { |
| 324 | |
| 325 | // Collect direct dependencies into contents. |
| 326 | contents := make(map[string]android.ApexMembership) |
| 327 | |
| 328 | // Collect the list of dependencies. |
| 329 | var dependencies []android.ApexModule |
| 330 | mctx.VisitDirectDeps(func(m android.Module) { |
| 331 | tag := mctx.OtherModuleDependencyTag(m) |
Paul Duffin | a713942 | 2021-02-08 11:01:58 +0000 | [diff] [blame^] | 332 | if tag == exportedJavaLibTag { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 333 | depName := mctx.OtherModuleName(m) |
| 334 | |
| 335 | // It is an error if the other module is not a prebuilt. |
| 336 | if _, ok := m.(android.PrebuiltInterface); !ok { |
| 337 | mctx.PropertyErrorf("exported_java_libs", "%q is not a prebuilt module", depName) |
| 338 | return |
| 339 | } |
| 340 | |
| 341 | // It is an error if the other module is not an ApexModule. |
| 342 | if _, ok := m.(android.ApexModule); !ok { |
| 343 | mctx.PropertyErrorf("exported_java_libs", "%q is not usable within an apex", depName) |
| 344 | return |
| 345 | } |
| 346 | |
| 347 | // Strip off the prebuilt_ prefix if present before storing content to ensure consistent |
| 348 | // behavior whether there is a corresponding source module present or not. |
| 349 | depName = android.RemoveOptionalPrebuiltPrefix(depName) |
| 350 | |
| 351 | // Remember that this module was added as a direct dependency. |
| 352 | contents[depName] = contents[depName].Add(true) |
| 353 | |
| 354 | // Add the module to the list of dependencies that need to have an APEX variant. |
| 355 | dependencies = append(dependencies, m.(android.ApexModule)) |
| 356 | } |
| 357 | }) |
| 358 | |
| 359 | // Create contents for the prebuilt_apex and store it away for later use. |
| 360 | apexContents := android.NewApexContents(contents) |
| 361 | mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{ |
| 362 | Contents: apexContents, |
| 363 | }) |
| 364 | |
| 365 | // Create an ApexInfo for the prebuilt_apex. |
| 366 | apexInfo := android.ApexInfo{ |
| 367 | ApexVariationName: mctx.ModuleName(), |
| 368 | InApexes: []string{mctx.ModuleName()}, |
| 369 | ApexContents: []*android.ApexContents{apexContents}, |
| 370 | ForPrebuiltApex: true, |
| 371 | } |
| 372 | |
| 373 | // Mark the dependencies of this module as requiring a variant for this module. |
| 374 | for _, am := range dependencies { |
| 375 | am.BuildForApex(apexInfo) |
| 376 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 380 | // TODO(jungjw): Check the key validity. |
| 381 | p.inputApex = p.Prebuilt().SingleSourcePath(ctx) |
| 382 | p.installDir = android.PathForModuleInstall(ctx, "apex") |
| 383 | p.installFilename = p.InstallFilename() |
| 384 | if !strings.HasSuffix(p.installFilename, imageApexSuffix) { |
| 385 | ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix) |
| 386 | } |
| 387 | p.outputApex = android.PathForModuleOut(ctx, p.installFilename) |
| 388 | ctx.Build(pctx, android.BuildParams{ |
| 389 | Rule: android.Cp, |
| 390 | Input: p.inputApex, |
| 391 | Output: p.outputApex, |
| 392 | }) |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 393 | |
| 394 | if p.prebuiltCommon.checkForceDisable(ctx) { |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 395 | p.HideFromMake() |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 396 | return |
| 397 | } |
| 398 | |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 399 | if p.installable() { |
| 400 | ctx.InstallFile(p.installDir, p.installFilename, p.inputApex) |
| 401 | } |
| 402 | |
Jooyung Han | 002ab68 | 2020-01-08 01:57:58 +0900 | [diff] [blame] | 403 | // in case that prebuilt_apex replaces source apex (using prefer: prop) |
| 404 | p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx) |
| 405 | // or that prebuilt_apex overrides other apexes (using overrides: prop) |
| 406 | for _, overridden := range p.properties.Overrides { |
| 407 | p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...) |
| 408 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 409 | } |
| 410 | |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 411 | func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries { |
| 412 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 413 | Class: "ETC", |
| 414 | OutputFile: android.OptionalPathForPath(p.inputApex), |
| 415 | Include: "$(BUILD_PREBUILT)", |
| 416 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 417 | func(entries *android.AndroidMkEntries) { |
| 418 | entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String()) |
| 419 | entries.SetString("LOCAL_MODULE_STEM", p.installFilename) |
| 420 | entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable()) |
| 421 | entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...) |
Jooyung Han | 002ab68 | 2020-01-08 01:57:58 +0900 | [diff] [blame] | 422 | if len(p.compatSymlinks) > 0 { |
| 423 | entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && ")) |
| 424 | } |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 425 | }, |
| 426 | }, |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 427 | }} |
Jiyong Park | 09d7752 | 2019-11-18 11:16:27 +0900 | [diff] [blame] | 428 | } |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 429 | |
| 430 | type ApexSet struct { |
| 431 | android.ModuleBase |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 432 | prebuiltCommon |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 433 | |
| 434 | properties ApexSetProperties |
| 435 | |
| 436 | installDir android.InstallPath |
| 437 | installFilename string |
| 438 | outputApex android.WritablePath |
| 439 | |
| 440 | // list of commands to create symlinks for backward compatibility. |
| 441 | // these commands will be attached as LOCAL_POST_INSTALL_CMD |
| 442 | compatSymlinks []string |
Jooyung Han | 2963716 | 2020-06-30 06:34:23 +0900 | [diff] [blame] | 443 | |
| 444 | hostRequired []string |
| 445 | postInstallCommands []string |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | type ApexSetProperties struct { |
| 449 | // the .apks file path that contains prebuilt apex files to be extracted. |
| 450 | Set *string |
| 451 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 452 | Sanitized struct { |
| 453 | None struct { |
| 454 | Set *string |
| 455 | } |
| 456 | Address struct { |
| 457 | Set *string |
| 458 | } |
| 459 | Hwaddress struct { |
| 460 | Set *string |
| 461 | } |
| 462 | } |
| 463 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 464 | // whether the extracted apex file installable. |
| 465 | Installable *bool |
| 466 | |
| 467 | // optional name for the installed apex. If unspecified, name of the |
| 468 | // module is used as the file name |
| 469 | Filename *string |
| 470 | |
| 471 | // names of modules to be overridden. Listed modules can only be other binaries |
| 472 | // (in Make or Soong). |
| 473 | // This does not completely prevent installation of the overridden binaries, but if both |
| 474 | // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed |
| 475 | // from PRODUCT_PACKAGES. |
| 476 | Overrides []string |
| 477 | |
| 478 | // apexes in this set use prerelease SDK version |
| 479 | Prerelease *bool |
| 480 | } |
| 481 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 482 | func (a *ApexSet) prebuiltSrcs(ctx android.BaseModuleContext) []string { |
| 483 | var srcs []string |
| 484 | if a.properties.Set != nil { |
| 485 | srcs = append(srcs, *a.properties.Set) |
| 486 | } |
| 487 | |
| 488 | var sanitizers []string |
| 489 | if ctx.Host() { |
| 490 | sanitizers = ctx.Config().SanitizeHost() |
| 491 | } else { |
| 492 | sanitizers = ctx.Config().SanitizeDevice() |
| 493 | } |
| 494 | |
| 495 | if android.InList("address", sanitizers) && a.properties.Sanitized.Address.Set != nil { |
| 496 | srcs = append(srcs, *a.properties.Sanitized.Address.Set) |
| 497 | } else if android.InList("hwaddress", sanitizers) && a.properties.Sanitized.Hwaddress.Set != nil { |
| 498 | srcs = append(srcs, *a.properties.Sanitized.Hwaddress.Set) |
| 499 | } else if a.properties.Sanitized.None.Set != nil { |
| 500 | srcs = append(srcs, *a.properties.Sanitized.None.Set) |
| 501 | } |
| 502 | |
| 503 | return srcs |
| 504 | } |
| 505 | |
| 506 | func (a *ApexSet) hasSanitizedSource(sanitizer string) bool { |
| 507 | if sanitizer == "address" { |
| 508 | return a.properties.Sanitized.Address.Set != nil |
| 509 | } |
| 510 | if sanitizer == "hwaddress" { |
| 511 | return a.properties.Sanitized.Hwaddress.Set != nil |
| 512 | } |
| 513 | |
| 514 | return false |
| 515 | } |
| 516 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 517 | func (a *ApexSet) installable() bool { |
| 518 | return a.properties.Installable == nil || proptools.Bool(a.properties.Installable) |
| 519 | } |
| 520 | |
| 521 | func (a *ApexSet) InstallFilename() string { |
| 522 | return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix) |
| 523 | } |
| 524 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 525 | func (a *ApexSet) Name() string { |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 526 | return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name()) |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 527 | } |
| 528 | |
Jiyong Park | 8d6c51e | 2020-06-12 17:26:31 +0900 | [diff] [blame] | 529 | func (a *ApexSet) Overrides() []string { |
| 530 | return a.properties.Overrides |
| 531 | } |
| 532 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 533 | // prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex. |
| 534 | func apexSetFactory() android.Module { |
| 535 | module := &ApexSet{} |
| 536 | module.AddProperties(&module.properties) |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 537 | |
| 538 | srcsSupplier := func(ctx android.BaseModuleContext) []string { |
| 539 | return module.prebuiltSrcs(ctx) |
| 540 | } |
| 541 | |
| 542 | android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "set") |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 543 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 544 | return module |
| 545 | } |
| 546 | |
| 547 | func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 548 | a.installFilename = a.InstallFilename() |
| 549 | if !strings.HasSuffix(a.installFilename, imageApexSuffix) { |
| 550 | ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix) |
| 551 | } |
| 552 | |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 553 | apexSet := a.prebuiltCommon.prebuilt.SingleSourcePath(ctx) |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 554 | a.outputApex = android.PathForModuleOut(ctx, a.installFilename) |
| 555 | ctx.Build(pctx, |
| 556 | android.BuildParams{ |
| 557 | Rule: extractMatchingApex, |
| 558 | Description: "Extract an apex from an apex set", |
| 559 | Inputs: android.Paths{apexSet}, |
| 560 | Output: a.outputApex, |
| 561 | Args: map[string]string{ |
| 562 | "abis": strings.Join(java.SupportedAbis(ctx), ","), |
| 563 | "allow-prereleased": strconv.FormatBool(proptools.Bool(a.properties.Prerelease)), |
Dan Albert | 4f378d7 | 2020-07-23 17:32:15 -0700 | [diff] [blame] | 564 | "sdk-version": ctx.Config().PlatformSdkVersion().String(), |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 565 | }, |
| 566 | }) |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 567 | |
| 568 | if a.prebuiltCommon.checkForceDisable(ctx) { |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 569 | a.HideFromMake() |
Jiyong Park | 10e926b | 2020-07-16 21:38:56 +0900 | [diff] [blame] | 570 | return |
| 571 | } |
| 572 | |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 573 | a.installDir = android.PathForModuleInstall(ctx, "apex") |
| 574 | if a.installable() { |
| 575 | ctx.InstallFile(a.installDir, a.installFilename, a.outputApex) |
| 576 | } |
| 577 | |
| 578 | // in case that apex_set replaces source apex (using prefer: prop) |
| 579 | a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx) |
| 580 | // or that apex_set overrides other apexes (using overrides: prop) |
| 581 | for _, overridden := range a.properties.Overrides { |
| 582 | a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...) |
| 583 | } |
Jooyung Han | 2963716 | 2020-06-30 06:34:23 +0900 | [diff] [blame] | 584 | |
| 585 | if ctx.Config().InstallExtraFlattenedApexes() { |
| 586 | // flattened apex should be in /system_ext/apex |
| 587 | flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName()) |
| 588 | a.postInstallCommands = append(a.postInstallCommands, |
| 589 | fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s", |
| 590 | a.outputApex.String(), |
| 591 | flattenedApexDir.ToMakePath().String(), |
| 592 | )) |
| 593 | a.hostRequired = []string{"deapexer", "debugfs"} |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | type systemExtContext struct { |
| 598 | android.ModuleContext |
| 599 | } |
| 600 | |
| 601 | func (*systemExtContext) SystemExtSpecific() bool { |
| 602 | return true |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries { |
| 606 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
Jooyung Han | 2963716 | 2020-06-30 06:34:23 +0900 | [diff] [blame] | 607 | Class: "ETC", |
| 608 | OutputFile: android.OptionalPathForPath(a.outputApex), |
| 609 | Include: "$(BUILD_PREBUILT)", |
| 610 | Host_required: a.hostRequired, |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 611 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 612 | func(entries *android.AndroidMkEntries) { |
| 613 | entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String()) |
| 614 | entries.SetString("LOCAL_MODULE_STEM", a.installFilename) |
| 615 | entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable()) |
| 616 | entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...) |
Jooyung Han | 2963716 | 2020-06-30 06:34:23 +0900 | [diff] [blame] | 617 | postInstallCommands := append([]string{}, a.postInstallCommands...) |
| 618 | postInstallCommands = append(postInstallCommands, a.compatSymlinks...) |
| 619 | if len(postInstallCommands) > 0 { |
| 620 | entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && ")) |
Jaewoong Jung | fa00c06 | 2020-05-14 14:15:24 -0700 | [diff] [blame] | 621 | } |
| 622 | }, |
| 623 | }, |
| 624 | }} |
| 625 | } |