2018-10-12 14:49:38 +02:00
|
|
|
// Copyright (C) 2018 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package apex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-04-23 11:00:10 +02:00
|
|
|
"sort"
|
2019-02-18 07:25:04 +01:00
|
|
|
"strings"
|
2018-10-12 14:49:38 +02:00
|
|
|
|
|
|
|
"android/soong/android"
|
2021-07-27 13:10:32 +02:00
|
|
|
"android/soong/bazel"
|
2019-02-02 01:53:07 +01:00
|
|
|
|
2018-10-12 14:49:38 +02:00
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
)
|
|
|
|
|
|
|
|
var String = proptools.String
|
|
|
|
|
|
|
|
func init() {
|
2021-03-09 23:34:13 +01:00
|
|
|
registerApexKeyBuildComponents(android.InitRegistrationContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerApexKeyBuildComponents(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("apex_key", ApexKeyFactory)
|
|
|
|
ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
|
2018-10-12 14:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type apexKey struct {
|
|
|
|
android.ModuleBase
|
2021-07-27 07:34:59 +02:00
|
|
|
android.BazelModuleBase
|
2018-10-12 14:49:38 +02:00
|
|
|
|
|
|
|
properties apexKeyProperties
|
|
|
|
|
2020-12-21 18:11:10 +01:00
|
|
|
publicKeyFile android.Path
|
|
|
|
privateKeyFile android.Path
|
2018-10-12 14:49:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type apexKeyProperties struct {
|
2019-03-20 17:11:21 +01:00
|
|
|
// Path or module to the public key file in avbpubkey format. Installed to the device.
|
2018-10-12 14:49:38 +02:00
|
|
|
// Base name of the file is used as the ID for the key.
|
2019-03-20 17:11:21 +01:00
|
|
|
Public_key *string `android:"path"`
|
|
|
|
// Path or module to the private key file in pem format. Used to sign APEXs.
|
|
|
|
Private_key *string `android:"path"`
|
2018-12-27 05:32:34 +01:00
|
|
|
|
|
|
|
// Whether this key is installable to one of the partitions. Defualt: true.
|
|
|
|
Installable *bool
|
2018-10-12 14:49:38 +02:00
|
|
|
}
|
|
|
|
|
Introduce module type 'sdk'
This change introduces a new module type named 'sdk'. It is a logical
group of prebuilt modules that together provide a context (e.g. APIs)
in which Mainline modules (such as APEXes) are built.
A prebuilt module (e.g. java_import) can join an sdk by adding it to the
sdk module as shown below:
sdk {
name: "mysdk#20",
java_libs: ["myjavalib_mysdk_20"],
}
java_import {
name: "myjavalib_mysdk_20",
srcs: ["myjavalib-v20.jar"],
sdk_member_name: "myjavalib",
}
sdk {
name: "mysdk#21",
java_libs: ["myjavalib_mysdk_21"],
}
java_import {
name: "myjavalib_mysdk_21",
srcs: ["myjavalib-v21.jar"],
sdk_member_name: "myjavalib",
}
java_library {
name: "myjavalib",
srcs: ["**/*/*.java"],
}
An APEX can specify the SDK(s) that it wants to build with via the new
'uses_sdks' property.
apex {
name: "myapex",
java_libs: ["libX", "libY"],
uses_sdks: ["mysdk#20"],
}
With this, libX, libY, and their transitive dependencies are all built
with the version 20 of myjavalib (the first java_import module) instead
of the other one (which is for version 21) and java_library having the
same name (which is for ToT).
Bug: 138182343
Test: m (sdk_test.go added)
Change-Id: I7e14c524a7d6a0d9f575fb20822080f39818c01e
2019-07-17 13:08:41 +02:00
|
|
|
func ApexKeyFactory() android.Module {
|
2018-10-12 14:49:38 +02:00
|
|
|
module := &apexKey{}
|
|
|
|
module.AddProperties(&module.properties)
|
2019-04-11 17:27:11 +02:00
|
|
|
android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon)
|
2021-07-27 07:34:59 +02:00
|
|
|
android.InitBazelModule(module)
|
2018-10-12 14:49:38 +02:00
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
2018-12-27 05:32:34 +01:00
|
|
|
func (m *apexKey) installable() bool {
|
2019-04-01 04:15:50 +02:00
|
|
|
return false
|
2018-12-27 05:32:34 +01:00
|
|
|
}
|
|
|
|
|
2018-10-12 14:49:38 +02:00
|
|
|
func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2019-03-20 17:11:21 +01:00
|
|
|
// If the keys are from other modules (i.e. :module syntax) respect it.
|
|
|
|
// Otherwise, try to locate the key files in the default cert dir or
|
|
|
|
// in the local module dir
|
|
|
|
if android.SrcIsModule(String(m.properties.Public_key)) != "" {
|
2020-12-21 18:11:10 +01:00
|
|
|
m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
|
2019-03-20 17:11:21 +01:00
|
|
|
} else {
|
2020-12-21 18:11:10 +01:00
|
|
|
m.publicKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
|
2019-03-20 17:11:21 +01:00
|
|
|
// If not found, fall back to the local key pairs
|
2020-12-21 18:11:10 +01:00
|
|
|
if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() {
|
|
|
|
m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
|
2019-03-20 17:11:21 +01:00
|
|
|
}
|
2018-12-24 03:31:58 +01:00
|
|
|
}
|
2019-03-20 17:11:21 +01:00
|
|
|
|
|
|
|
if android.SrcIsModule(String(m.properties.Private_key)) != "" {
|
2020-12-21 18:11:10 +01:00
|
|
|
m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
|
2019-03-20 17:11:21 +01:00
|
|
|
} else {
|
2020-12-21 18:11:10 +01:00
|
|
|
m.privateKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Private_key))
|
|
|
|
if !android.ExistentPathForSource(ctx, m.privateKeyFile.String()).Valid() {
|
|
|
|
m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
|
2019-03-20 17:11:21 +01:00
|
|
|
}
|
2018-12-24 03:31:58 +01:00
|
|
|
}
|
2018-10-12 14:49:38 +02:00
|
|
|
|
2020-12-21 18:11:10 +01:00
|
|
|
pubKeyName := m.publicKeyFile.Base()[0 : len(m.publicKeyFile.Base())-len(m.publicKeyFile.Ext())]
|
|
|
|
privKeyName := m.privateKeyFile.Base()[0 : len(m.privateKeyFile.Base())-len(m.privateKeyFile.Ext())]
|
2018-10-12 14:49:38 +02:00
|
|
|
|
2019-03-26 23:07:36 +01:00
|
|
|
if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName {
|
2018-10-12 14:49:38 +02:00
|
|
|
ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
|
2020-12-21 18:11:10 +01:00
|
|
|
m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName)
|
2018-10-12 14:49:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2019-02-18 07:25:04 +01:00
|
|
|
|
2022-08-16 19:27:33 +02:00
|
|
|
// //////////////////////////////////////////////////////////////////////
|
2019-02-18 07:25:04 +01:00
|
|
|
// apex_keys_text
|
2019-02-20 14:23:29 +01:00
|
|
|
type apexKeysText struct {
|
|
|
|
output android.OutputPath
|
|
|
|
}
|
2019-02-18 07:25:04 +01:00
|
|
|
|
|
|
|
func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) {
|
2019-02-20 14:23:29 +01:00
|
|
|
s.output = android.PathForOutput(ctx, "apexkeys.txt")
|
2020-06-12 10:26:31 +02:00
|
|
|
type apexKeyEntry struct {
|
2020-12-21 18:11:10 +01:00
|
|
|
name string
|
|
|
|
presigned bool
|
|
|
|
publicKey string
|
|
|
|
privateKey string
|
|
|
|
containerCertificate string
|
|
|
|
containerPrivateKey string
|
|
|
|
partition string
|
2021-10-26 20:45:31 +02:00
|
|
|
signTool string
|
2020-06-12 10:26:31 +02:00
|
|
|
}
|
|
|
|
toString := func(e apexKeyEntry) string {
|
2021-10-26 20:45:31 +02:00
|
|
|
signTool := ""
|
|
|
|
if e.signTool != "" {
|
|
|
|
signTool = fmt.Sprintf(" sign_tool=%q", e.signTool)
|
|
|
|
}
|
|
|
|
format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q%s\n"
|
2020-06-12 10:26:31 +02:00
|
|
|
if e.presigned {
|
2021-10-26 20:45:31 +02:00
|
|
|
return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition, signTool)
|
2020-06-12 10:26:31 +02:00
|
|
|
} else {
|
2021-10-26 20:45:31 +02:00
|
|
|
return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition, signTool)
|
2020-06-12 10:26:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
apexKeyMap := make(map[string]apexKeyEntry)
|
2019-02-18 07:25:04 +01:00
|
|
|
ctx.VisitAllModules(func(module android.Module) {
|
2019-04-23 11:00:10 +02:00
|
|
|
if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() {
|
2020-11-24 11:51:18 +01:00
|
|
|
pem, key := m.getCertificateAndPrivateKey(ctx)
|
2020-06-12 10:26:31 +02:00
|
|
|
apexKeyMap[m.Name()] = apexKeyEntry{
|
2020-12-21 18:11:10 +01:00
|
|
|
name: m.Name() + ".apex",
|
|
|
|
presigned: false,
|
|
|
|
publicKey: m.publicKeyFile.String(),
|
|
|
|
privateKey: m.privateKeyFile.String(),
|
|
|
|
containerCertificate: pem.String(),
|
|
|
|
containerPrivateKey: key.String(),
|
|
|
|
partition: m.PartitionTag(ctx.DeviceConfig()),
|
2021-10-26 20:45:31 +02:00
|
|
|
signTool: proptools.String(m.properties.Custom_sign_tool),
|
2020-06-12 10:26:31 +02:00
|
|
|
}
|
2019-02-18 07:25:04 +01:00
|
|
|
}
|
2019-04-23 11:00:10 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Find prebuilts and let them override apexBundle if they are preferred
|
|
|
|
ctx.VisitAllModules(func(module android.Module) {
|
|
|
|
if m, ok := module.(*Prebuilt); ok && m.Enabled() && m.installable() &&
|
|
|
|
m.Prebuilt().UsePrebuilt() {
|
2020-06-12 10:26:31 +02:00
|
|
|
apexKeyMap[m.BaseModuleName()] = apexKeyEntry{
|
|
|
|
name: m.InstallFilename(),
|
|
|
|
presigned: true,
|
|
|
|
partition: m.PartitionTag(ctx.DeviceConfig()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Find apex_set and let them override apexBundle or prebuilts. This is done in a separate pass
|
|
|
|
// so that apex_set are not overridden by prebuilts.
|
|
|
|
ctx.VisitAllModules(func(module android.Module) {
|
|
|
|
if m, ok := module.(*ApexSet); ok && m.Enabled() {
|
|
|
|
entry := apexKeyEntry{
|
|
|
|
name: m.InstallFilename(),
|
|
|
|
presigned: true,
|
|
|
|
partition: m.PartitionTag(ctx.DeviceConfig()),
|
|
|
|
}
|
|
|
|
apexKeyMap[m.BaseModuleName()] = entry
|
2019-04-23 11:00:10 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// iterating over map does not give consistent ordering in golang
|
|
|
|
var moduleNames []string
|
2020-06-12 10:26:31 +02:00
|
|
|
for key, _ := range apexKeyMap {
|
2019-04-23 11:00:10 +02:00
|
|
|
moduleNames = append(moduleNames, key)
|
|
|
|
}
|
|
|
|
sort.Strings(moduleNames)
|
2019-02-18 07:25:04 +01:00
|
|
|
|
2019-04-23 11:00:10 +02:00
|
|
|
var filecontent strings.Builder
|
2020-06-12 10:26:31 +02:00
|
|
|
for _, name := range moduleNames {
|
2020-11-13 20:48:42 +01:00
|
|
|
filecontent.WriteString(toString(apexKeyMap[name]))
|
2019-04-23 11:00:10 +02:00
|
|
|
}
|
2020-11-13 20:48:42 +01:00
|
|
|
android.WriteFileRule(ctx, s.output, filecontent.String())
|
2019-02-18 07:25:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func apexKeysTextFactory() android.Singleton {
|
|
|
|
return &apexKeysText{}
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:23:29 +01:00
|
|
|
func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) {
|
|
|
|
ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String())
|
2019-02-18 07:25:04 +01:00
|
|
|
}
|
2021-07-27 13:10:32 +02:00
|
|
|
|
|
|
|
// For Bazel / bp2build
|
|
|
|
|
|
|
|
type bazelApexKeyAttributes struct {
|
2022-10-05 08:15:15 +02:00
|
|
|
Public_key bazel.LabelAttribute
|
2022-10-14 11:56:07 +02:00
|
|
|
Public_key_name bazel.StringAttribute
|
2022-10-05 08:15:15 +02:00
|
|
|
|
|
|
|
Private_key bazel.LabelAttribute
|
2022-10-14 11:56:07 +02:00
|
|
|
Private_key_name bazel.StringAttribute
|
2021-07-27 13:10:32 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 20:32:43 +01:00
|
|
|
// ConvertWithBp2build performs conversion apexKey for bp2build
|
|
|
|
func (m *apexKey) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
|
|
|
|
apexKeyBp2BuildInternal(ctx, m)
|
2021-07-27 13:10:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func apexKeyBp2BuildInternal(ctx android.TopDownMutatorContext, module *apexKey) {
|
2022-10-14 11:56:07 +02:00
|
|
|
privateKeyLabelAttribute, privateKeyNameAttribute :=
|
|
|
|
android.BazelStringOrLabelFromProp(ctx, module.properties.Private_key)
|
2021-07-27 13:10:32 +02:00
|
|
|
|
2022-10-14 11:56:07 +02:00
|
|
|
publicKeyLabelAttribute, publicKeyNameAttribute :=
|
|
|
|
android.BazelStringOrLabelFromProp(ctx, module.properties.Public_key)
|
2021-07-27 13:10:32 +02:00
|
|
|
|
|
|
|
attrs := &bazelApexKeyAttributes{
|
2022-10-05 08:15:15 +02:00
|
|
|
Private_key: privateKeyLabelAttribute,
|
|
|
|
Private_key_name: privateKeyNameAttribute,
|
|
|
|
|
|
|
|
Public_key: publicKeyLabelAttribute,
|
|
|
|
Public_key_name: publicKeyNameAttribute,
|
2021-07-27 13:10:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
props := bazel.BazelTargetModuleProperties{
|
|
|
|
Rule_class: "apex_key",
|
2022-04-29 22:37:43 +02:00
|
|
|
Bzl_load_location: "//build/bazel/rules/apex:apex_key.bzl",
|
2021-07-27 13:10:32 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 22:30:36 +02:00
|
|
|
ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: module.Name()}, attrs)
|
2021-07-27 13:10:32 +02:00
|
|
|
}
|