2018-11-12 19:13:39 +01:00
|
|
|
// 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 main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2019-02-15 19:39:37 +01:00
|
|
|
"strings"
|
2018-11-12 19:13:39 +01:00
|
|
|
|
2019-01-31 02:32:39 +01:00
|
|
|
"android/soong/android"
|
2018-11-12 19:13:39 +01:00
|
|
|
"android/soong/dexpreopt"
|
|
|
|
|
|
|
|
"github.com/google/blueprint/pathtools"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-01-07 00:11:37 +01:00
|
|
|
dexpreoptScriptPath = flag.String("dexpreopt_script", "", "path to output dexpreopt script")
|
|
|
|
globalSoongConfigPath = flag.String("global_soong", "", "path to global configuration file for settings originating from Soong")
|
|
|
|
globalConfigPath = flag.String("global", "", "path to global configuration file")
|
|
|
|
moduleConfigPath = flag.String("module", "", "path to module configuration file")
|
|
|
|
outDir = flag.String("out_dir", "", "path to output directory")
|
2018-11-12 19:13:39 +01:00
|
|
|
)
|
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
type pathContext struct {
|
|
|
|
config android.Config
|
|
|
|
}
|
|
|
|
|
2020-01-10 19:51:04 +01:00
|
|
|
func (x *pathContext) Fs() pathtools.FileSystem { return pathtools.OsFs }
|
2019-02-15 19:39:37 +01:00
|
|
|
func (x *pathContext) Config() android.Config { return x.config }
|
|
|
|
func (x *pathContext) AddNinjaFileDeps(...string) {}
|
|
|
|
|
2018-11-12 19:13:39 +01:00
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
usage := func(err string) {
|
|
|
|
if err != "" {
|
|
|
|
fmt.Println(err)
|
|
|
|
flag.Usage()
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if flag.NArg() > 0 {
|
|
|
|
usage("unrecognized argument " + flag.Arg(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
if *dexpreoptScriptPath == "" {
|
|
|
|
usage("path to output dexpreopt script is required")
|
|
|
|
}
|
|
|
|
|
2020-01-07 00:11:37 +01:00
|
|
|
if *globalSoongConfigPath == "" {
|
|
|
|
usage("--global_soong configuration file is required")
|
|
|
|
}
|
|
|
|
|
2018-11-12 19:13:39 +01:00
|
|
|
if *globalConfigPath == "" {
|
2020-01-07 00:11:37 +01:00
|
|
|
usage("--global configuration file is required")
|
2018-11-12 19:13:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if *moduleConfigPath == "" {
|
2020-01-07 00:11:37 +01:00
|
|
|
usage("--module configuration file is required")
|
2018-11-12 19:13:39 +01:00
|
|
|
}
|
|
|
|
|
2020-01-10 19:51:04 +01:00
|
|
|
ctx := &pathContext{android.TestConfig(*outDir, nil, "", nil)}
|
2019-02-15 19:39:37 +01:00
|
|
|
|
2020-01-10 19:51:04 +01:00
|
|
|
globalSoongConfig, err := dexpreopt.LoadGlobalSoongConfig(ctx, *globalSoongConfigPath)
|
2020-01-07 00:11:37 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error loading global config %q: %s\n", *globalSoongConfigPath, err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2020-01-10 19:51:04 +01:00
|
|
|
globalConfig, _, err := dexpreopt.LoadGlobalConfig(ctx, *globalConfigPath, globalSoongConfig)
|
2018-11-12 19:13:39 +01:00
|
|
|
if err != nil {
|
2020-01-10 19:51:04 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "error loading global config %q: %s\n", *globalConfigPath, err)
|
2018-11-12 19:13:39 +01:00
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2020-01-10 19:51:04 +01:00
|
|
|
moduleConfig, err := dexpreopt.LoadModuleConfig(ctx, *moduleConfigPath)
|
2018-11-12 19:13:39 +01:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "error loading module config %q: %s\n", *moduleConfigPath, err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
moduleConfig.DexPath = android.PathForTesting("$1")
|
|
|
|
|
2018-11-12 19:13:39 +01:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
switch x := r.(type) {
|
|
|
|
case runtime.Error:
|
|
|
|
panic(x)
|
|
|
|
case error:
|
|
|
|
fmt.Fprintln(os.Stderr, "error:", r)
|
|
|
|
os.Exit(3)
|
|
|
|
default:
|
|
|
|
panic(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-10-18 15:51:38 +02:00
|
|
|
writeScripts(ctx, globalConfig, moduleConfig, *dexpreoptScriptPath)
|
2018-11-12 19:13:39 +01:00
|
|
|
}
|
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
func writeScripts(ctx android.PathContext, global dexpreopt.GlobalConfig, module dexpreopt.ModuleConfig,
|
2019-10-18 15:51:38 +02:00
|
|
|
dexpreoptScriptPath string) {
|
2019-02-15 19:39:37 +01:00
|
|
|
dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, module)
|
2018-11-12 19:13:39 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
installDir := module.BuildPath.InSameDir(ctx, "dexpreopt_install")
|
2018-11-12 19:13:39 +01:00
|
|
|
|
2019-02-15 19:39:37 +01:00
|
|
|
dexpreoptRule.Command().FlagWithArg("rm -rf ", installDir.String())
|
|
|
|
dexpreoptRule.Command().FlagWithArg("mkdir -p ", installDir.String())
|
2018-11-12 19:13:39 +01:00
|
|
|
|
|
|
|
for _, install := range dexpreoptRule.Installs() {
|
2019-02-15 19:39:37 +01:00
|
|
|
installPath := installDir.Join(ctx, strings.TrimPrefix(install.To, "/"))
|
|
|
|
dexpreoptRule.Command().Text("mkdir -p").Flag(filepath.Dir(installPath.String()))
|
2018-11-12 19:13:39 +01:00
|
|
|
dexpreoptRule.Command().Text("cp -f").Input(install.From).Output(installPath)
|
|
|
|
}
|
2020-01-07 00:11:37 +01:00
|
|
|
dexpreoptRule.Command().Tool(global.SoongConfig.SoongZip).
|
2019-02-15 19:39:37 +01:00
|
|
|
FlagWithArg("-o ", "$2").
|
|
|
|
FlagWithArg("-C ", installDir.String()).
|
|
|
|
FlagWithArg("-D ", installDir.String())
|
2018-11-12 19:13:39 +01:00
|
|
|
|
2019-01-31 02:32:39 +01:00
|
|
|
write := func(rule *android.RuleBuilder, file string) {
|
2018-11-12 19:13:39 +01:00
|
|
|
script := &bytes.Buffer{}
|
|
|
|
script.WriteString(scriptHeader)
|
|
|
|
for _, c := range rule.Commands() {
|
|
|
|
script.WriteString(c)
|
|
|
|
script.WriteString("\n\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
depFile := &bytes.Buffer{}
|
|
|
|
|
|
|
|
fmt.Fprint(depFile, `: \`+"\n")
|
2019-02-16 01:03:58 +01:00
|
|
|
for _, tool := range rule.Tools() {
|
2018-11-12 19:13:39 +01:00
|
|
|
fmt.Fprintf(depFile, ` %s \`+"\n", tool)
|
|
|
|
}
|
2019-02-16 01:03:58 +01:00
|
|
|
for _, input := range rule.Inputs() {
|
2018-11-12 19:13:39 +01:00
|
|
|
// Assume the rule that ran the script already has a dependency on the input file passed on the
|
|
|
|
// command line.
|
2019-02-15 19:39:37 +01:00
|
|
|
if input.String() != "$1" {
|
2018-11-12 19:13:39 +01:00
|
|
|
fmt.Fprintf(depFile, ` %s \`+"\n", input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
depFile.WriteString("\n")
|
|
|
|
|
|
|
|
fmt.Fprintln(script, "rm -f $2.d")
|
|
|
|
// Write the output path unescaped so the $2 gets expanded
|
|
|
|
fmt.Fprintln(script, `echo -n $2 > $2.d`)
|
|
|
|
// Write the rest of the depsfile using cat <<'EOF', which will not do any shell expansion on
|
|
|
|
// the contents to preserve backslashes and special characters in filenames.
|
|
|
|
fmt.Fprintf(script, "cat >> $2.d <<'EOF'\n%sEOF\n", depFile.String())
|
|
|
|
|
|
|
|
err := pathtools.WriteFileIfChanged(file, script.Bytes(), 0755)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The written scripts will assume the input is $1 and the output is $2
|
2019-02-15 19:39:37 +01:00
|
|
|
if module.DexPath.String() != "$1" {
|
2018-11-12 19:13:39 +01:00
|
|
|
panic(fmt.Errorf("module.DexPath must be '$1', was %q", module.DexPath))
|
|
|
|
}
|
|
|
|
|
|
|
|
write(dexpreoptRule, dexpreoptScriptPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
const scriptHeader = `#!/bin/bash
|
|
|
|
|
|
|
|
err() {
|
|
|
|
errno=$?
|
|
|
|
echo "error: $0:$1 exited with status $errno" >&2
|
|
|
|
echo "error in command:" >&2
|
|
|
|
sed -n -e "$1p" $0 >&2
|
|
|
|
if [ "$errno" -ne 0 ]; then
|
|
|
|
exit $errno
|
|
|
|
else
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
trap 'err $LINENO' ERR
|
|
|
|
|
|
|
|
`
|