2018-10-12 20:49:38 +08: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 17:00:10 +08:00
|
|
|
"sort"
|
2019-02-18 14:25:04 +08:00
|
|
|
"strings"
|
2018-10-12 20:49:38 +08:00
|
|
|
|
|
|
|
"android/soong/android"
|
2019-02-02 08:53:07 +08:00
|
|
|
|
2018-10-12 20:49:38 +08:00
|
|
|
"github.com/google/blueprint/proptools"
|
|
|
|
)
|
|
|
|
|
|
|
|
var String = proptools.String
|
|
|
|
|
|
|
|
func init() {
|
2021-03-10 06:34:13 +08:00
|
|
|
registerApexKeyBuildComponents(android.InitRegistrationContext)
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerApexKeyBuildComponents(ctx android.RegistrationContext) {
|
|
|
|
ctx.RegisterModuleType("apex_key", ApexKeyFactory)
|
|
|
|
ctx.RegisterSingletonType("apex_keys_text", apexKeysTextFactory)
|
2018-10-12 20:49:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type apexKey struct {
|
|
|
|
android.ModuleBase
|
|
|
|
|
|
|
|
properties apexKeyProperties
|
|
|
|
|
2020-12-22 01:11:10 +08:00
|
|
|
publicKeyFile android.Path
|
|
|
|
privateKeyFile android.Path
|
2018-10-12 20:49:38 +08:00
|
|
|
|
|
|
|
keyName string
|
|
|
|
}
|
|
|
|
|
|
|
|
type apexKeyProperties struct {
|
2019-03-21 00:11:21 +08:00
|
|
|
// Path or module to the public key file in avbpubkey format. Installed to the device.
|
2018-10-12 20:49:38 +08:00
|
|
|
// Base name of the file is used as the ID for the key.
|
2019-03-21 00:11:21 +08: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 12:32:34 +08:00
|
|
|
|
|
|
|
// Whether this key is installable to one of the partitions. Defualt: true.
|
|
|
|
Installable *bool
|
2018-10-12 20:49:38 +08: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 19:08:41 +08:00
|
|
|
func ApexKeyFactory() android.Module {
|
2018-10-12 20:49:38 +08:00
|
|
|
module := &apexKey{}
|
|
|
|
module.AddProperties(&module.properties)
|
2019-04-11 23:27:11 +08:00
|
|
|
android.InitAndroidArchModule(module, android.HostAndDeviceDefault, android.MultilibCommon)
|
2018-10-12 20:49:38 +08:00
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
2018-12-27 12:32:34 +08:00
|
|
|
func (m *apexKey) installable() bool {
|
2019-04-01 10:15:50 +08:00
|
|
|
return false
|
2018-12-27 12:32:34 +08:00
|
|
|
}
|
|
|
|
|
2018-10-12 20:49:38 +08:00
|
|
|
func (m *apexKey) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2019-03-21 00:11:21 +08: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-22 01:11:10 +08:00
|
|
|
m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
|
2019-03-21 00:11:21 +08:00
|
|
|
} else {
|
2020-12-22 01:11:10 +08:00
|
|
|
m.publicKeyFile = ctx.Config().ApexKeyDir(ctx).Join(ctx, String(m.properties.Public_key))
|
2019-03-21 00:11:21 +08:00
|
|
|
// If not found, fall back to the local key pairs
|
2020-12-22 01:11:10 +08:00
|
|
|
if !android.ExistentPathForSource(ctx, m.publicKeyFile.String()).Valid() {
|
|
|
|
m.publicKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Public_key))
|
2019-03-21 00:11:21 +08:00
|
|
|
}
|
2018-12-24 10:31:58 +08:00
|
|
|
}
|
2019-03-21 00:11:21 +08:00
|
|
|
|
|
|
|
if android.SrcIsModule(String(m.properties.Private_key)) != "" {
|
2020-12-22 01:11:10 +08:00
|
|
|
m.privateKeyFile = android.PathForModuleSrc(ctx, String(m.properties.Private_key))
|
2019-03-21 00:11:21 +08:00
|
|
|
} else {
|
2020-12-22 01:11:10 +08: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-21 00:11:21 +08:00
|
|
|
}
|
2018-12-24 10:31:58 +08:00
|
|
|
}
|
2018-10-12 20:49:38 +08:00
|
|
|
|
2020-12-22 01:11:10 +08: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 20:49:38 +08:00
|
|
|
|
2019-03-27 06:07:36 +08:00
|
|
|
if m.properties.Public_key != nil && m.properties.Private_key != nil && pubKeyName != privKeyName {
|
2018-10-12 20:49:38 +08:00
|
|
|
ctx.ModuleErrorf("public_key %q (keyname:%q) and private_key %q (keyname:%q) do not have same keyname",
|
2020-12-22 01:11:10 +08:00
|
|
|
m.publicKeyFile.String(), pubKeyName, m.privateKeyFile, privKeyName)
|
2018-10-12 20:49:38 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
m.keyName = pubKeyName
|
|
|
|
}
|
2019-02-18 14:25:04 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// apex_keys_text
|
2019-02-20 21:23:29 +08:00
|
|
|
type apexKeysText struct {
|
|
|
|
output android.OutputPath
|
|
|
|
}
|
2019-02-18 14:25:04 +08:00
|
|
|
|
|
|
|
func (s *apexKeysText) GenerateBuildActions(ctx android.SingletonContext) {
|
2019-02-20 21:23:29 +08:00
|
|
|
s.output = android.PathForOutput(ctx, "apexkeys.txt")
|
2020-06-12 16:26:31 +08:00
|
|
|
type apexKeyEntry struct {
|
2020-12-22 01:11:10 +08:00
|
|
|
name string
|
|
|
|
presigned bool
|
|
|
|
publicKey string
|
|
|
|
privateKey string
|
|
|
|
containerCertificate string
|
|
|
|
containerPrivateKey string
|
|
|
|
partition string
|
2020-06-12 16:26:31 +08:00
|
|
|
}
|
|
|
|
toString := func(e apexKeyEntry) string {
|
2020-11-14 03:48:42 +08:00
|
|
|
format := "name=%q public_key=%q private_key=%q container_certificate=%q container_private_key=%q partition=%q\n"
|
2020-06-12 16:26:31 +08:00
|
|
|
if e.presigned {
|
|
|
|
return fmt.Sprintf(format, e.name, "PRESIGNED", "PRESIGNED", "PRESIGNED", "PRESIGNED", e.partition)
|
|
|
|
} else {
|
2020-12-22 01:11:10 +08:00
|
|
|
return fmt.Sprintf(format, e.name, e.publicKey, e.privateKey, e.containerCertificate, e.containerPrivateKey, e.partition)
|
2020-06-12 16:26:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
apexKeyMap := make(map[string]apexKeyEntry)
|
2019-02-18 14:25:04 +08:00
|
|
|
ctx.VisitAllModules(func(module android.Module) {
|
2019-04-23 17:00:10 +08:00
|
|
|
if m, ok := module.(*apexBundle); ok && m.Enabled() && m.installable() {
|
2020-11-24 18:51:18 +08:00
|
|
|
pem, key := m.getCertificateAndPrivateKey(ctx)
|
2020-06-12 16:26:31 +08:00
|
|
|
apexKeyMap[m.Name()] = apexKeyEntry{
|
2020-12-22 01:11:10 +08: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()),
|
2020-06-12 16:26:31 +08:00
|
|
|
}
|
2019-02-18 14:25:04 +08:00
|
|
|
}
|
2019-04-23 17:00:10 +08: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 16:26:31 +08: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 17:00:10 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// iterating over map does not give consistent ordering in golang
|
|
|
|
var moduleNames []string
|
2020-06-12 16:26:31 +08:00
|
|
|
for key, _ := range apexKeyMap {
|
2019-04-23 17:00:10 +08:00
|
|
|
moduleNames = append(moduleNames, key)
|
|
|
|
}
|
|
|
|
sort.Strings(moduleNames)
|
2019-02-18 14:25:04 +08:00
|
|
|
|
2019-04-23 17:00:10 +08:00
|
|
|
var filecontent strings.Builder
|
2020-06-12 16:26:31 +08:00
|
|
|
for _, name := range moduleNames {
|
2020-11-14 03:48:42 +08:00
|
|
|
filecontent.WriteString(toString(apexKeyMap[name]))
|
2019-04-23 17:00:10 +08:00
|
|
|
}
|
2020-11-14 03:48:42 +08:00
|
|
|
android.WriteFileRule(ctx, s.output, filecontent.String())
|
2019-02-18 14:25:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func apexKeysTextFactory() android.Singleton {
|
|
|
|
return &apexKeysText{}
|
|
|
|
}
|
|
|
|
|
2019-02-20 21:23:29 +08:00
|
|
|
func (s *apexKeysText) MakeVars(ctx android.MakeVarsContext) {
|
|
|
|
ctx.Strict("SOONG_APEX_KEYS_FILE", s.output.String())
|
2019-02-18 14:25:04 +08:00
|
|
|
}
|