blob: 6f510801089544f2e0d7775ef95920194e8722b7 [file] [log] [blame]
Colin Cross43f08db2018-11-12 10:13:39 -08001// Copyright 2018 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18 "bytes"
19 "flag"
20 "fmt"
21 "os"
22 "path/filepath"
23 "runtime"
Colin Cross69f59a32019-02-15 10:39:37 -080024 "strings"
Colin Cross43f08db2018-11-12 10:13:39 -080025
Colin Crossfeec25b2019-01-30 17:32:39 -080026 "android/soong/android"
Colin Cross43f08db2018-11-12 10:13:39 -080027 "android/soong/dexpreopt"
28
29 "github.com/google/blueprint/pathtools"
30)
31
32var (
33 dexpreoptScriptPath = flag.String("dexpreopt_script", "", "path to output dexpreopt script")
Colin Cross43f08db2018-11-12 10:13:39 -080034 globalConfigPath = flag.String("global", "", "path to global configuration file")
35 moduleConfigPath = flag.String("module", "", "path to module configuration file")
Colin Cross69f59a32019-02-15 10:39:37 -080036 outDir = flag.String("out_dir", "", "path to output directory")
Colin Cross43f08db2018-11-12 10:13:39 -080037)
38
Colin Cross69f59a32019-02-15 10:39:37 -080039type pathContext struct {
40 config android.Config
41}
42
43func (x *pathContext) Fs() pathtools.FileSystem { return pathtools.OsFs }
44func (x *pathContext) Config() android.Config { return x.config }
45func (x *pathContext) AddNinjaFileDeps(...string) {}
46
Colin Cross43f08db2018-11-12 10:13:39 -080047func main() {
48 flag.Parse()
49
50 usage := func(err string) {
51 if err != "" {
52 fmt.Println(err)
53 flag.Usage()
54 os.Exit(1)
55 }
56 }
57
58 if flag.NArg() > 0 {
59 usage("unrecognized argument " + flag.Arg(0))
60 }
61
62 if *dexpreoptScriptPath == "" {
63 usage("path to output dexpreopt script is required")
64 }
65
Colin Cross43f08db2018-11-12 10:13:39 -080066 if *globalConfigPath == "" {
67 usage("path to global configuration file is required")
68 }
69
70 if *moduleConfigPath == "" {
71 usage("path to module configuration file is required")
72 }
73
Colin Cross98be1bb2019-12-13 20:41:13 -080074 ctx := &pathContext{android.TestConfig(*outDir, nil, "", nil)}
Colin Cross69f59a32019-02-15 10:39:37 -080075
Colin Cross2d00f0d2019-05-09 21:50:00 -070076 globalConfig, _, err := dexpreopt.LoadGlobalConfig(ctx, *globalConfigPath)
Colin Cross43f08db2018-11-12 10:13:39 -080077 if err != nil {
78 fmt.Fprintf(os.Stderr, "error loading global config %q: %s\n", *globalConfigPath, err)
79 os.Exit(2)
80 }
81
Colin Cross69f59a32019-02-15 10:39:37 -080082 moduleConfig, err := dexpreopt.LoadModuleConfig(ctx, *moduleConfigPath)
Colin Cross43f08db2018-11-12 10:13:39 -080083 if err != nil {
84 fmt.Fprintf(os.Stderr, "error loading module config %q: %s\n", *moduleConfigPath, err)
85 os.Exit(2)
86 }
87
Colin Cross69f59a32019-02-15 10:39:37 -080088 moduleConfig.DexPath = android.PathForTesting("$1")
89
Colin Cross43f08db2018-11-12 10:13:39 -080090 defer func() {
91 if r := recover(); r != nil {
92 switch x := r.(type) {
93 case runtime.Error:
94 panic(x)
95 case error:
96 fmt.Fprintln(os.Stderr, "error:", r)
97 os.Exit(3)
98 default:
99 panic(x)
100 }
101 }
102 }()
103
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100104 writeScripts(ctx, globalConfig, moduleConfig, *dexpreoptScriptPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800105}
106
Colin Cross69f59a32019-02-15 10:39:37 -0800107func writeScripts(ctx android.PathContext, global dexpreopt.GlobalConfig, module dexpreopt.ModuleConfig,
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100108 dexpreoptScriptPath string) {
Colin Cross69f59a32019-02-15 10:39:37 -0800109 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800110 if err != nil {
111 panic(err)
112 }
113
Colin Cross69f59a32019-02-15 10:39:37 -0800114 installDir := module.BuildPath.InSameDir(ctx, "dexpreopt_install")
Colin Cross43f08db2018-11-12 10:13:39 -0800115
Colin Cross69f59a32019-02-15 10:39:37 -0800116 dexpreoptRule.Command().FlagWithArg("rm -rf ", installDir.String())
117 dexpreoptRule.Command().FlagWithArg("mkdir -p ", installDir.String())
Colin Cross43f08db2018-11-12 10:13:39 -0800118
119 for _, install := range dexpreoptRule.Installs() {
Colin Cross69f59a32019-02-15 10:39:37 -0800120 installPath := installDir.Join(ctx, strings.TrimPrefix(install.To, "/"))
121 dexpreoptRule.Command().Text("mkdir -p").Flag(filepath.Dir(installPath.String()))
Colin Cross43f08db2018-11-12 10:13:39 -0800122 dexpreoptRule.Command().Text("cp -f").Input(install.From).Output(installPath)
123 }
124 dexpreoptRule.Command().Tool(global.Tools.SoongZip).
Colin Cross69f59a32019-02-15 10:39:37 -0800125 FlagWithArg("-o ", "$2").
126 FlagWithArg("-C ", installDir.String()).
127 FlagWithArg("-D ", installDir.String())
Colin Cross43f08db2018-11-12 10:13:39 -0800128
Colin Crossfeec25b2019-01-30 17:32:39 -0800129 write := func(rule *android.RuleBuilder, file string) {
Colin Cross43f08db2018-11-12 10:13:39 -0800130 script := &bytes.Buffer{}
131 script.WriteString(scriptHeader)
132 for _, c := range rule.Commands() {
133 script.WriteString(c)
134 script.WriteString("\n\n")
135 }
136
137 depFile := &bytes.Buffer{}
138
139 fmt.Fprint(depFile, `: \`+"\n")
Colin Cross25397f52019-02-15 16:03:58 -0800140 for _, tool := range rule.Tools() {
Colin Cross43f08db2018-11-12 10:13:39 -0800141 fmt.Fprintf(depFile, ` %s \`+"\n", tool)
142 }
Colin Cross25397f52019-02-15 16:03:58 -0800143 for _, input := range rule.Inputs() {
Colin Cross43f08db2018-11-12 10:13:39 -0800144 // Assume the rule that ran the script already has a dependency on the input file passed on the
145 // command line.
Colin Cross69f59a32019-02-15 10:39:37 -0800146 if input.String() != "$1" {
Colin Cross43f08db2018-11-12 10:13:39 -0800147 fmt.Fprintf(depFile, ` %s \`+"\n", input)
148 }
149 }
150 depFile.WriteString("\n")
151
152 fmt.Fprintln(script, "rm -f $2.d")
153 // Write the output path unescaped so the $2 gets expanded
154 fmt.Fprintln(script, `echo -n $2 > $2.d`)
155 // Write the rest of the depsfile using cat <<'EOF', which will not do any shell expansion on
156 // the contents to preserve backslashes and special characters in filenames.
157 fmt.Fprintf(script, "cat >> $2.d <<'EOF'\n%sEOF\n", depFile.String())
158
159 err := pathtools.WriteFileIfChanged(file, script.Bytes(), 0755)
160 if err != nil {
161 panic(err)
162 }
163 }
164
165 // The written scripts will assume the input is $1 and the output is $2
Colin Cross69f59a32019-02-15 10:39:37 -0800166 if module.DexPath.String() != "$1" {
Colin Cross43f08db2018-11-12 10:13:39 -0800167 panic(fmt.Errorf("module.DexPath must be '$1', was %q", module.DexPath))
168 }
Colin Cross43f08db2018-11-12 10:13:39 -0800169
170 write(dexpreoptRule, dexpreoptScriptPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800171}
172
173const scriptHeader = `#!/bin/bash
174
175err() {
176 errno=$?
177 echo "error: $0:$1 exited with status $errno" >&2
178 echo "error in command:" >&2
179 sed -n -e "$1p" $0 >&2
180 if [ "$errno" -ne 0 ]; then
181 exit $errno
182 else
183 exit 1
184 fi
185}
186
187trap 'err $LINENO' ERR
188
189`