blob: ba05d945d8c7b235727683c7000c0a77bf427239 [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"
Colin Cross988414c2020-01-11 01:11:46 +000021 "io/ioutil"
Colin Cross43f08db2018-11-12 10:13:39 -080022 "os"
23 "path/filepath"
24 "runtime"
Colin Cross69f59a32019-02-15 10:39:37 -080025 "strings"
Colin Cross43f08db2018-11-12 10:13:39 -080026
Colin Crossfeec25b2019-01-30 17:32:39 -080027 "android/soong/android"
Colin Cross43f08db2018-11-12 10:13:39 -080028 "android/soong/dexpreopt"
29
Colin Crossf1a035e2020-11-16 17:32:30 -080030 "github.com/google/blueprint"
Colin Cross43f08db2018-11-12 10:13:39 -080031 "github.com/google/blueprint/pathtools"
32)
33
34var (
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000035 dexpreoptScriptPath = flag.String("dexpreopt_script", "", "path to output dexpreopt script")
36 globalSoongConfigPath = flag.String("global_soong", "", "path to global configuration file for settings originating from Soong")
37 globalConfigPath = flag.String("global", "", "path to global configuration file")
38 moduleConfigPath = flag.String("module", "", "path to module configuration file")
39 outDir = flag.String("out_dir", "", "path to output directory")
Jeongik Cha4dda75e2021-04-27 23:56:44 +090040 // If uses_target_files is true, dexpreopt_gen will be running on extracted target_files.zip files.
41 // In this case, the tool replace output file path with $(basePath)/$(on-device file path).
42 // The flag is useful when running dex2oat on system image and vendor image which are built separately.
43 usesTargetFiles = flag.Bool("uses_target_files", false, "whether or not dexpreopt is running on target_files")
44 // basePath indicates the path where target_files.zip is extracted.
45 basePath = flag.String("base_path", ".", "base path where images and tools are extracted")
Colin Cross43f08db2018-11-12 10:13:39 -080046)
47
Colin Crossf1a035e2020-11-16 17:32:30 -080048type builderContext struct {
Colin Cross69f59a32019-02-15 10:39:37 -080049 config android.Config
50}
51
Colin Crossf1a035e2020-11-16 17:32:30 -080052func (x *builderContext) Config() android.Config { return x.config }
53func (x *builderContext) AddNinjaFileDeps(...string) {}
54func (x *builderContext) Build(android.PackageContext, android.BuildParams) {}
55func (x *builderContext) Rule(android.PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule {
56 return nil
57}
Colin Cross69f59a32019-02-15 10:39:37 -080058
Colin Cross43f08db2018-11-12 10:13:39 -080059func main() {
60 flag.Parse()
61
62 usage := func(err string) {
63 if err != "" {
64 fmt.Println(err)
65 flag.Usage()
66 os.Exit(1)
67 }
68 }
69
70 if flag.NArg() > 0 {
71 usage("unrecognized argument " + flag.Arg(0))
72 }
73
74 if *dexpreoptScriptPath == "" {
75 usage("path to output dexpreopt script is required")
76 }
77
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000078 if *globalSoongConfigPath == "" {
79 usage("--global_soong configuration file is required")
80 }
81
Colin Cross43f08db2018-11-12 10:13:39 -080082 if *globalConfigPath == "" {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000083 usage("--global configuration file is required")
Colin Cross43f08db2018-11-12 10:13:39 -080084 }
85
86 if *moduleConfigPath == "" {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000087 usage("--module configuration file is required")
Colin Cross43f08db2018-11-12 10:13:39 -080088 }
89
Lukacs T. Berkid6cee7e2021-09-01 16:25:51 +020090 // NOTE: duplicating --out_dir here is incorrect (one should be the another
91 // plus "/soong" but doing so apparently breaks dexpreopt
92 ctx := &builderContext{android.NullConfig(*outDir, *outDir)}
Colin Cross69f59a32019-02-15 10:39:37 -080093
Colin Cross988414c2020-01-11 01:11:46 +000094 globalSoongConfigData, err := ioutil.ReadFile(*globalSoongConfigPath)
95 if err != nil {
Martin Stjernholm75a48d82020-01-10 20:32:59 +000096 fmt.Fprintf(os.Stderr, "error reading global Soong config %q: %s\n", *globalSoongConfigPath, err)
Colin Cross988414c2020-01-11 01:11:46 +000097 os.Exit(2)
98 }
99
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000100 globalSoongConfig, err := dexpreopt.ParseGlobalSoongConfig(ctx, globalSoongConfigData)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000101 if err != nil {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000102 fmt.Fprintf(os.Stderr, "error parsing global Soong config %q: %s\n", *globalSoongConfigPath, err)
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000103 os.Exit(2)
104 }
105
Colin Cross988414c2020-01-11 01:11:46 +0000106 globalConfigData, err := ioutil.ReadFile(*globalConfigPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800107 if err != nil {
Colin Cross988414c2020-01-11 01:11:46 +0000108 fmt.Fprintf(os.Stderr, "error reading global config %q: %s\n", *globalConfigPath, err)
Colin Cross43f08db2018-11-12 10:13:39 -0800109 os.Exit(2)
110 }
111
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000112 globalConfig, err := dexpreopt.ParseGlobalConfig(ctx, globalConfigData)
Colin Cross988414c2020-01-11 01:11:46 +0000113 if err != nil {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000114 fmt.Fprintf(os.Stderr, "error parsing global config %q: %s\n", *globalConfigPath, err)
Colin Cross988414c2020-01-11 01:11:46 +0000115 os.Exit(2)
116 }
117
118 moduleConfigData, err := ioutil.ReadFile(*moduleConfigPath)
119 if err != nil {
120 fmt.Fprintf(os.Stderr, "error reading module config %q: %s\n", *moduleConfigPath, err)
121 os.Exit(2)
122 }
123
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000124 moduleConfig, err := dexpreopt.ParseModuleConfig(ctx, moduleConfigData)
Colin Cross43f08db2018-11-12 10:13:39 -0800125 if err != nil {
Martin Stjernholm40f9f3c2020-01-20 18:12:23 +0000126 fmt.Fprintf(os.Stderr, "error parsing module config %q: %s\n", *moduleConfigPath, err)
Colin Cross43f08db2018-11-12 10:13:39 -0800127 os.Exit(2)
128 }
129
Colin Cross69f59a32019-02-15 10:39:37 -0800130 moduleConfig.DexPath = android.PathForTesting("$1")
131
Colin Cross43f08db2018-11-12 10:13:39 -0800132 defer func() {
133 if r := recover(); r != nil {
134 switch x := r.(type) {
135 case runtime.Error:
136 panic(x)
137 case error:
138 fmt.Fprintln(os.Stderr, "error:", r)
139 os.Exit(3)
140 default:
141 panic(x)
142 }
143 }
144 }()
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900145 if *usesTargetFiles {
146 moduleConfig.ManifestPath = android.OptionalPath{}
147 prefix := "dex2oat_result"
148 moduleConfig.BuildPath = android.PathForOutput(ctx, filepath.Join(prefix, moduleConfig.DexLocation))
149 for i, location := range moduleConfig.PreoptBootClassPathDexLocations {
150 moduleConfig.PreoptBootClassPathDexFiles[i] = android.PathForSource(ctx, *basePath+location)
151 }
152 for i := range moduleConfig.ClassLoaderContexts {
153 for _, v := range moduleConfig.ClassLoaderContexts[i] {
154 v.Host = android.PathForSource(ctx, *basePath+v.Device)
155 }
156 }
157 moduleConfig.EnforceUsesLibraries = false
158 for i, location := range moduleConfig.DexPreoptImageLocationsOnDevice {
159 moduleConfig.DexPreoptImageLocationsOnHost[i] = *basePath + location
160 }
161 }
Martin Stjernholm75a48d82020-01-10 20:32:59 +0000162 writeScripts(ctx, globalSoongConfig, globalConfig, moduleConfig, *dexpreoptScriptPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800163}
164
Colin Crossf1a035e2020-11-16 17:32:30 -0800165func writeScripts(ctx android.BuilderContext, globalSoong *dexpreopt.GlobalSoongConfig,
Martin Stjernholm8d80cee2020-01-31 17:44:54 +0000166 global *dexpreopt.GlobalConfig, module *dexpreopt.ModuleConfig, dexpreoptScriptPath string) {
Colin Crossfeec25b2019-01-30 17:32:39 -0800167 write := func(rule *android.RuleBuilder, file string) {
Colin Cross43f08db2018-11-12 10:13:39 -0800168 script := &bytes.Buffer{}
169 script.WriteString(scriptHeader)
170 for _, c := range rule.Commands() {
171 script.WriteString(c)
172 script.WriteString("\n\n")
173 }
174
175 depFile := &bytes.Buffer{}
176
177 fmt.Fprint(depFile, `: \`+"\n")
Colin Cross25397f52019-02-15 16:03:58 -0800178 for _, tool := range rule.Tools() {
Colin Cross43f08db2018-11-12 10:13:39 -0800179 fmt.Fprintf(depFile, ` %s \`+"\n", tool)
180 }
Colin Cross25397f52019-02-15 16:03:58 -0800181 for _, input := range rule.Inputs() {
Colin Cross43f08db2018-11-12 10:13:39 -0800182 // Assume the rule that ran the script already has a dependency on the input file passed on the
183 // command line.
Colin Cross69f59a32019-02-15 10:39:37 -0800184 if input.String() != "$1" {
Colin Cross43f08db2018-11-12 10:13:39 -0800185 fmt.Fprintf(depFile, ` %s \`+"\n", input)
186 }
187 }
188 depFile.WriteString("\n")
189
190 fmt.Fprintln(script, "rm -f $2.d")
191 // Write the output path unescaped so the $2 gets expanded
192 fmt.Fprintln(script, `echo -n $2 > $2.d`)
193 // Write the rest of the depsfile using cat <<'EOF', which will not do any shell expansion on
194 // the contents to preserve backslashes and special characters in filenames.
195 fmt.Fprintf(script, "cat >> $2.d <<'EOF'\n%sEOF\n", depFile.String())
196
197 err := pathtools.WriteFileIfChanged(file, script.Bytes(), 0755)
198 if err != nil {
199 panic(err)
200 }
201 }
Jeongik Cha4dda75e2021-04-27 23:56:44 +0900202 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, module)
203 if err != nil {
204 panic(err)
205 }
206 // When usesTargetFiles is true, only odex/vdex files are necessary.
207 // So skip redunant processes(such as copying the result to the artifact path, and zipping, and so on.)
208 if *usesTargetFiles {
209 write(dexpreoptRule, dexpreoptScriptPath)
210 return
211 }
212 installDir := module.BuildPath.InSameDir(ctx, "dexpreopt_install")
213
214 dexpreoptRule.Command().FlagWithArg("rm -rf ", installDir.String())
215 dexpreoptRule.Command().FlagWithArg("mkdir -p ", installDir.String())
216
217 for _, install := range dexpreoptRule.Installs() {
218 installPath := installDir.Join(ctx, strings.TrimPrefix(install.To, "/"))
219 dexpreoptRule.Command().Text("mkdir -p").Flag(filepath.Dir(installPath.String()))
220 dexpreoptRule.Command().Text("cp -f").Input(install.From).Output(installPath)
221 }
222 dexpreoptRule.Command().Tool(globalSoong.SoongZip).
223 FlagWithArg("-o ", "$2").
224 FlagWithArg("-C ", installDir.String()).
225 FlagWithArg("-D ", installDir.String())
Colin Cross43f08db2018-11-12 10:13:39 -0800226
227 // The written scripts will assume the input is $1 and the output is $2
Colin Cross69f59a32019-02-15 10:39:37 -0800228 if module.DexPath.String() != "$1" {
Colin Cross43f08db2018-11-12 10:13:39 -0800229 panic(fmt.Errorf("module.DexPath must be '$1', was %q", module.DexPath))
230 }
Colin Cross43f08db2018-11-12 10:13:39 -0800231
232 write(dexpreoptRule, dexpreoptScriptPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800233}
234
235const scriptHeader = `#!/bin/bash
236
237err() {
238 errno=$?
239 echo "error: $0:$1 exited with status $errno" >&2
240 echo "error in command:" >&2
241 sed -n -e "$1p" $0 >&2
242 if [ "$errno" -ne 0 ]; then
243 exit $errno
244 else
245 exit 1
246 fi
247}
248
249trap 'err $LINENO' ERR
250
251`