Move dexpreopt.Script to android.RuleBuilder

Move dexpreopt.Script to android.RuleBuilder so that the builder
style can be used in more places.  Also add tests for it.

Test: rule_builder_test.go
Change-Id: I92a963bd112bf033b08899e930094b908acfcdfd
This commit is contained in:
Colin Cross
2019-01-30 17:32:39 -08:00
parent a55b12bec2
commit feec25b084
10 changed files with 413 additions and 251 deletions

View File

@@ -4,12 +4,12 @@ bootstrap_go_package {
srcs: [
"config.go",
"dexpreopt.go",
"script.go",
],
testSrcs: [
"dexpreopt_test.go",
],
deps: [
"blueprint-pathtools",
"soong-android",
],
}
}

View File

@@ -39,6 +39,8 @@ import (
"path/filepath"
"strings"
"android/soong/android"
"github.com/google/blueprint/pathtools"
)
@@ -47,7 +49,7 @@ const SystemOtherPartition = "/system_other/"
// GenerateStripRule generates a set of commands that will take an APK or JAR as an input and strip the dex files if
// they are no longer necessary after preopting.
func GenerateStripRule(global GlobalConfig, module ModuleConfig) (rule *Rule, err error) {
func GenerateStripRule(global GlobalConfig, module ModuleConfig) (rule *android.RuleBuilder, err error) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
@@ -61,7 +63,7 @@ func GenerateStripRule(global GlobalConfig, module ModuleConfig) (rule *Rule, er
tools := global.Tools
rule = &Rule{}
rule = &android.RuleBuilder{}
strip := shouldStripDex(module, global)
@@ -81,7 +83,7 @@ func GenerateStripRule(global GlobalConfig, module ModuleConfig) (rule *Rule, er
// GenerateDexpreoptRule generates a set of commands that will preopt a module based on a GlobalConfig and a
// ModuleConfig. The produced files and their install locations will be available through rule.Installs().
func GenerateDexpreoptRule(global GlobalConfig, module ModuleConfig) (rule *Rule, err error) {
func GenerateDexpreoptRule(global GlobalConfig, module ModuleConfig) (rule *android.RuleBuilder, err error) {
defer func() {
if r := recover(); r != nil {
if e, ok := r.(error); ok {
@@ -93,7 +95,7 @@ func GenerateDexpreoptRule(global GlobalConfig, module ModuleConfig) (rule *Rule
}
}()
rule = &Rule{}
rule = &android.RuleBuilder{}
generateProfile := module.ProfileClassListing != "" && !global.DisableGenerateProfile
@@ -141,7 +143,7 @@ func dexpreoptDisabled(global GlobalConfig, module ModuleConfig) bool {
return false
}
func profileCommand(global GlobalConfig, module ModuleConfig, rule *Rule) string {
func profileCommand(global GlobalConfig, module ModuleConfig, rule *android.RuleBuilder) string {
profilePath := filepath.Join(filepath.Dir(module.BuildPath), "profile.prof")
profileInstalledPath := module.DexLocation + ".prof"
@@ -178,8 +180,8 @@ func profileCommand(global GlobalConfig, module ModuleConfig, rule *Rule) string
return profilePath
}
func dexpreoptCommand(global GlobalConfig, module ModuleConfig, rule *Rule, profile, arch, bootImageLocation string,
appImage, generateDM bool) {
func dexpreoptCommand(global GlobalConfig, module ModuleConfig, rule *android.RuleBuilder,
profile, arch, bootImageLocation string, appImage, generateDM bool) {
// HACK: make soname in Soong-generated .odex files match Make.
base := filepath.Base(module.DexLocation)

View File

@@ -22,6 +22,7 @@ import (
"path/filepath"
"runtime"
"android/soong/android"
"android/soong/dexpreopt"
"github.com/google/blueprint/pathtools"
@@ -121,7 +122,7 @@ func writeScripts(global dexpreopt.GlobalConfig, module dexpreopt.ModuleConfig,
panic(err)
}
write := func(rule *dexpreopt.Rule, file string) {
write := func(rule *android.RuleBuilder, file string) {
script := &bytes.Buffer{}
script.WriteString(scriptHeader)
for _, c := range rule.Commands() {

View File

@@ -15,6 +15,7 @@
package dexpreopt
import (
"android/soong/android"
"reflect"
"strings"
"testing"
@@ -100,7 +101,7 @@ func TestDexPreopt(t *testing.T) {
t.Error(err)
}
wantInstalls := []Install{
wantInstalls := []android.RuleBuilderInstall{
{"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},
{"out/test/oat/arm/package.vdex", "/system/app/test/oat/arm/test.vdex"},
}
@@ -126,7 +127,7 @@ func TestDexPreoptSystemOther(t *testing.T) {
t.Error(err)
}
wantInstalls := []Install{
wantInstalls := []android.RuleBuilderInstall{
{"out/test/oat/arm/package.odex", "/system_other/app/test/oat/arm/test.odex"},
{"out/test/oat/arm/package.vdex", "/system_other/app/test/oat/arm/test.vdex"},
}
@@ -150,7 +151,7 @@ func TestDexPreoptProfile(t *testing.T) {
t.Error(err)
}
wantInstalls := []Install{
wantInstalls := []android.RuleBuilderInstall{
{"out/test/profile.prof", "/system/app/test/test.apk.prof"},
{"out/test/oat/arm/package.art", "/system/app/test/oat/arm/test.art"},
{"out/test/oat/arm/package.odex", "/system/app/test/oat/arm/test.odex"},

View File

@@ -1,178 +0,0 @@
// Copyright 2018 Google Inc. 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.
package dexpreopt
import (
"fmt"
"sort"
"strings"
)
type Install struct {
From, To string
}
type Rule struct {
commands []*Command
installs []Install
}
func (r *Rule) Install(from, to string) {
r.installs = append(r.installs, Install{from, to})
}
func (r *Rule) Command() *Command {
command := &Command{}
r.commands = append(r.commands, command)
return command
}
func (r *Rule) Inputs() []string {
outputs := r.outputSet()
inputs := make(map[string]bool)
for _, c := range r.commands {
for _, input := range c.inputs {
if !outputs[input] {
inputs[input] = true
}
}
}
var inputList []string
for input := range inputs {
inputList = append(inputList, input)
}
sort.Strings(inputList)
return inputList
}
func (r *Rule) outputSet() map[string]bool {
outputs := make(map[string]bool)
for _, c := range r.commands {
for _, output := range c.outputs {
outputs[output] = true
}
}
return outputs
}
func (r *Rule) Outputs() []string {
outputs := r.outputSet()
var outputList []string
for output := range outputs {
outputList = append(outputList, output)
}
sort.Strings(outputList)
return outputList
}
func (r *Rule) Installs() []Install {
return append([]Install(nil), r.installs...)
}
func (r *Rule) Tools() []string {
var tools []string
for _, c := range r.commands {
tools = append(tools, c.tools...)
}
return tools
}
func (r *Rule) Commands() []string {
var commands []string
for _, c := range r.commands {
commands = append(commands, string(c.buf))
}
return commands
}
type Command struct {
buf []byte
inputs []string
outputs []string
tools []string
}
func (c *Command) Text(text string) *Command {
if len(c.buf) > 0 {
c.buf = append(c.buf, ' ')
}
c.buf = append(c.buf, text...)
return c
}
func (c *Command) Textf(format string, a ...interface{}) *Command {
return c.Text(fmt.Sprintf(format, a...))
}
func (c *Command) Flag(flag string) *Command {
return c.Text(flag)
}
func (c *Command) FlagWithArg(flag, arg string) *Command {
return c.Text(flag + arg)
}
func (c *Command) FlagWithList(flag string, list []string, sep string) *Command {
return c.Text(flag + strings.Join(list, sep))
}
func (c *Command) Tool(path string) *Command {
c.tools = append(c.tools, path)
return c.Text(path)
}
func (c *Command) Input(path string) *Command {
c.inputs = append(c.inputs, path)
return c.Text(path)
}
func (c *Command) Implicit(path string) *Command {
c.inputs = append(c.inputs, path)
return c
}
func (c *Command) Implicits(paths []string) *Command {
c.inputs = append(c.inputs, paths...)
return c
}
func (c *Command) Output(path string) *Command {
c.outputs = append(c.outputs, path)
return c.Text(path)
}
func (c *Command) ImplicitOutput(path string) *Command {
c.outputs = append(c.outputs, path)
return c
}
func (c *Command) FlagWithInput(flag, path string) *Command {
c.inputs = append(c.inputs, path)
return c.Text(flag + path)
}
func (c *Command) FlagWithInputList(flag string, paths []string, sep string) *Command {
c.inputs = append(c.inputs, paths...)
return c.FlagWithList(flag, paths, sep)
}
func (c *Command) FlagWithOutput(flag, path string) *Command {
c.outputs = append(c.outputs, path)
return c.Text(flag + path)
}