Skip to content

Commit

Permalink
Migrate CreateDeployInfoAction from Native to Starlark.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 611167708
Change-Id: I15d17a4850df5647921b8f2d61eadaf7136c1b16
  • Loading branch information
Zhaoqing Xu authored and Copybara-Service committed Feb 28, 2024
1 parent 50e170b commit aeb70ea
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 8 deletions.
26 changes: 18 additions & 8 deletions rules/android_binary_internal/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -597,18 +597,27 @@ def finalize(
if apk_packaging_ctx.v4_signature_file:
files.append(apk_packaging_ctx.v4_signature_file)

providers.extend(
[
DefaultInfo(
files = depset(files),
),
OutputGroupInfo(
android_deploy_info = [
apk_packaging_ctx.deploy_info,
packaged_resources_ctx.processed_manifest,
],
_validation = depset(validation_outputs),
),
],
)
else:
providers.append(
DefaultInfo(
files = depset(files),
OutputGroupInfo(
_validation = depset(validation_outputs),
),
)

providers.append(
OutputGroupInfo(
_validation = depset(validation_outputs),
),
)

# Add the AndroidApplicationResourceInfo provider from resource shrinking if it was performed.
# TODO(ahumesky): This can be cleaned up after the rules are fully migrated to Starlark.
# Packaging will be the final step in the pipeline, and that step can be responsible for picking
Expand Down Expand Up @@ -910,6 +919,7 @@ def _process_apk_packaging(ctx, packaged_resources_ctx, native_libs_ctx, dex_ctx
deterministic_signing = False,
java_toolchain = common.get_java_toolchain(ctx),
resource_extractor = get_android_toolchain(ctx).resource_extractor.files_to_run,
deploy_info_writer = get_android_toolchain(ctx).deploy_info_writer.files_to_run,
zip_aligner = get_android_sdk(ctx).zip_align,
apk_signer = get_android_sdk(ctx).apk_signer,
toolchain_type = ANDROID_TOOLCHAIN_TYPE,
Expand Down
37 changes: 37 additions & 0 deletions rules/apk_packaging.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ load(":java.bzl", "java")

_PROVIDERS = "providers"
_V4_SIGNATURE_FILE = "v4_signature_file"
_DEPLOY_INFO = "deploy_info"

_ApkContextInfo = provider(
"Apk Context Info",
fields = {
_PROVIDERS: "The list of all providers to propagate.",
_V4_SIGNATURE_FILE: "The v4 signature file.",
_DEPLOY_INFO: "A proto providing information about how to deploy and launch the APK",
},
)

Expand All @@ -49,6 +51,7 @@ def _process(
deterministic_signing = False,
java_toolchain = None,
resource_extractor = None,
deploy_info_writer = None,
zip_aligner = None,
apk_signer = None,
toolchain_type = None):
Expand Down Expand Up @@ -77,6 +80,7 @@ def _process(
deterministic_signing: Boolean. Whether to enable deterministic DSA signing.
java_toolchain: The JavaToolchain target.
resource_extractor: FilesToRunProvider. The executable to extract resources from java_resources_zip.
deploy_info_writer: FilesToRunProvider. The executable to write the deploy info proto file.
zip_aligner: FilesToRunProvider. The executable to zipalign the APK.
apk_signer: FilesToRunProvider. The executable to sign the APK.
toolchain_type: String. The Android toolchain type.
Expand Down Expand Up @@ -129,6 +133,16 @@ def _process(
toolchain_type = toolchain_type,
)

deploy_info = ctx.actions.declare_file(ctx.label.name + "_files/deploy_info.deployinfo.pb")
_create_deploy_info(
ctx,
deploy_info,
manifest = merged_manifest,
apks_to_deploy = [signed_apk] + ([v4_signature_file] if v4_signature_file else []),
deploy_info_writer = deploy_info_writer,
toolchain_type = toolchain_type,
)

apk_packaging_ctx[_PROVIDERS].append(
ApkInfo(
signed_apk = signed_apk,
Expand All @@ -142,6 +156,7 @@ def _process(
),
)
apk_packaging_ctx[_V4_SIGNATURE_FILE] = v4_signature_file
apk_packaging_ctx[_DEPLOY_INFO] = deploy_info

return _ApkContextInfo(**apk_packaging_ctx)

Expand Down Expand Up @@ -332,6 +347,28 @@ def _sign_apk(
toolchain = toolchain_type,
)

def _create_deploy_info(
ctx,
deploy_info,
manifest = None,
apks_to_deploy = [],
deploy_info_writer = None,
toolchain_type = None):
"""Creates a deploy info proto."""
args = ctx.actions.args()
args.add("--manifest", manifest)
args.add_joined("--apk", apks_to_deploy, join_with = ",")
args.add("--deploy_info", deploy_info)

ctx.actions.run(
executable = deploy_info_writer,
arguments = [args],
outputs = [deploy_info],
mnemonic = "WriteDeployInfo",
progress_message = "Writing Deploy info proto file %s" % deploy_info.short_path,
toolchain = toolchain_type,
)

apk_packaging = struct(
process = _process,
)
21 changes: 21 additions & 0 deletions src/tools/deploy_info/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")

package(default_applicable_licenses = ["//:license"])

licenses(["notice"])

go_binary(
name = "deploy_info",
srcs = ["deploy_info.go"],
visibility = [
"//test/mobile_install:__subpackages__",
"//toolchains/android:__subpackages__",
],
deps = [
"//src/common/golang:flagfile",
"//src/common/golang:flags",
"//src/tools/deploy_info/proto:android_deploy_info_go_proto",
"@org_golang_google_protobuf//proto",
],
)
64 changes: 64 additions & 0 deletions src/tools/deploy_info/deploy_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2018 The Bazel Authors. All rights reserved.
//
// 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.

// Tool to generate the deploy_info.pb that ASwB needs in order to deploy apps.
package main

import (
"flag"
"io/ioutil"
"log"

pb "src/tools/deploy_info/proto/android_deploy_info_go_proto"
_ "src/common/golang/flagfile"
"src/common/golang/flags"
"google.golang.org/protobuf/proto"
)

var apk = flags.NewStringList("apk", "Path to the apk(s).")
var manifest = flag.String("manifest", "", "Path to the Android manifest.")
var deployInfo = flag.String("deploy_info", "", "Deploy info pb output path")

func main() {
flag.Parse()

if *apk == nil {
log.Fatalf("-apk needs to be specified.")
}

if *manifest == "" {
log.Fatalf("-manifest needs to be specified.")
}

if *deployInfo == "" {
log.Fatalf("-deploy_info needs to be specified.")
}

manifestArtifact := &pb.Artifact{ExecRootPath: *manifest}
splitArtifacts := []*pb.Artifact{}
for _, split := range *apk {
splitArtifacts = append(splitArtifacts, &pb.Artifact{ExecRootPath: split})
}

info := &pb.AndroidDeployInfo{
MergedManifest: manifestArtifact,
ApksToDeploy: splitArtifacts}
bits, err := proto.Marshal(info)
if err != nil {
log.Fatal(err)
}
if err := ioutil.WriteFile(*deployInfo, bits, 0644); err != nil {
log.Fatal(err)
}
}
13 changes: 13 additions & 0 deletions src/tools/deploy_info/proto/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library", "go_test")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")

go_proto_library(
name = "android_deploy_info_go_proto",
importpath = "src/tools/deploy_info/proto/android_deploy_info_go_proto",
protos = [
"@bazel_tools//src/main/protobuf:android_deploy_info_proto",
],
visibility = [
"//src/tools/deploy_info:__pkg__",
],
)
5 changes: 5 additions & 0 deletions toolchains/android/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ _ATTRS = dict(
default = "@bazel_tools//tools/android:resource_extractor",
executable = True,
),
deploy_info_writer = attr.label(
cfg = "exec",
default = Label("//src/tools/deploy_info"),
executable = True,
)

)

Expand Down

0 comments on commit aeb70ea

Please sign in to comment.