Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "flag" |
| 20 | "fmt" |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 21 | "io/ioutil" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 22 | "os" |
| 23 | "path/filepath" |
| 24 | "runtime" |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 25 | "strings" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 26 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 27 | "android/soong/android" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 28 | "android/soong/dexpreopt" |
| 29 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 30 | "github.com/google/blueprint" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 31 | "github.com/google/blueprint/pathtools" |
| 32 | ) |
| 33 | |
| 34 | var ( |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 35 | 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 Cha | 4dda75e | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 40 | // 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. |
Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 45 | basePath = flag.String("base_path", ".", "base path where images and tools are extracted") |
| 46 | productPackagesPath = flag.String("product_packages", "", "path to product_packages.txt") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 47 | ) |
| 48 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 49 | type builderContext struct { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 50 | config android.Config |
| 51 | } |
| 52 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 53 | func (x *builderContext) Config() android.Config { return x.config } |
| 54 | func (x *builderContext) AddNinjaFileDeps(...string) {} |
| 55 | func (x *builderContext) Build(android.PackageContext, android.BuildParams) {} |
| 56 | func (x *builderContext) Rule(android.PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule { |
| 57 | return nil |
| 58 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 59 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 60 | func main() { |
| 61 | flag.Parse() |
| 62 | |
| 63 | usage := func(err string) { |
| 64 | if err != "" { |
| 65 | fmt.Println(err) |
| 66 | flag.Usage() |
| 67 | os.Exit(1) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | if flag.NArg() > 0 { |
| 72 | usage("unrecognized argument " + flag.Arg(0)) |
| 73 | } |
| 74 | |
| 75 | if *dexpreoptScriptPath == "" { |
| 76 | usage("path to output dexpreopt script is required") |
| 77 | } |
| 78 | |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 79 | if *globalSoongConfigPath == "" { |
| 80 | usage("--global_soong configuration file is required") |
| 81 | } |
| 82 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 83 | if *globalConfigPath == "" { |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 84 | usage("--global configuration file is required") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | if *moduleConfigPath == "" { |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 88 | usage("--module configuration file is required") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 91 | if *productPackagesPath == "" { |
| 92 | usage("--product_packages configuration file is required") |
| 93 | } |
| 94 | |
Lukacs T. Berki | d6cee7e | 2021-09-01 16:25:51 +0200 | [diff] [blame] | 95 | // NOTE: duplicating --out_dir here is incorrect (one should be the another |
| 96 | // plus "/soong" but doing so apparently breaks dexpreopt |
| 97 | ctx := &builderContext{android.NullConfig(*outDir, *outDir)} |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 98 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 99 | globalSoongConfigData, err := ioutil.ReadFile(*globalSoongConfigPath) |
| 100 | if err != nil { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 101 | fmt.Fprintf(os.Stderr, "error reading global Soong config %q: %s\n", *globalSoongConfigPath, err) |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 102 | os.Exit(2) |
| 103 | } |
| 104 | |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 105 | globalSoongConfig, err := dexpreopt.ParseGlobalSoongConfig(ctx, globalSoongConfigData) |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 106 | if err != nil { |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 107 | fmt.Fprintf(os.Stderr, "error parsing global Soong config %q: %s\n", *globalSoongConfigPath, err) |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 108 | os.Exit(2) |
| 109 | } |
| 110 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 111 | globalConfigData, err := ioutil.ReadFile(*globalConfigPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 112 | if err != nil { |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 113 | fmt.Fprintf(os.Stderr, "error reading global config %q: %s\n", *globalConfigPath, err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 114 | os.Exit(2) |
| 115 | } |
| 116 | |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 117 | globalConfig, err := dexpreopt.ParseGlobalConfig(ctx, globalConfigData) |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 118 | if err != nil { |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 119 | fmt.Fprintf(os.Stderr, "error parsing global config %q: %s\n", *globalConfigPath, err) |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 120 | os.Exit(2) |
| 121 | } |
| 122 | |
| 123 | moduleConfigData, err := ioutil.ReadFile(*moduleConfigPath) |
| 124 | if err != nil { |
| 125 | fmt.Fprintf(os.Stderr, "error reading module config %q: %s\n", *moduleConfigPath, err) |
| 126 | os.Exit(2) |
| 127 | } |
| 128 | |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 129 | moduleConfig, err := dexpreopt.ParseModuleConfig(ctx, moduleConfigData) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 130 | if err != nil { |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 131 | fmt.Fprintf(os.Stderr, "error parsing module config %q: %s\n", *moduleConfigPath, err) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 132 | os.Exit(2) |
| 133 | } |
| 134 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 135 | moduleConfig.DexPath = android.PathForTesting("$1") |
| 136 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 137 | defer func() { |
| 138 | if r := recover(); r != nil { |
| 139 | switch x := r.(type) { |
| 140 | case runtime.Error: |
| 141 | panic(x) |
| 142 | case error: |
| 143 | fmt.Fprintln(os.Stderr, "error:", r) |
| 144 | os.Exit(3) |
| 145 | default: |
| 146 | panic(x) |
| 147 | } |
| 148 | } |
| 149 | }() |
Jeongik Cha | 4dda75e | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 150 | if *usesTargetFiles { |
| 151 | moduleConfig.ManifestPath = android.OptionalPath{} |
| 152 | prefix := "dex2oat_result" |
| 153 | moduleConfig.BuildPath = android.PathForOutput(ctx, filepath.Join(prefix, moduleConfig.DexLocation)) |
| 154 | for i, location := range moduleConfig.PreoptBootClassPathDexLocations { |
| 155 | moduleConfig.PreoptBootClassPathDexFiles[i] = android.PathForSource(ctx, *basePath+location) |
| 156 | } |
| 157 | for i := range moduleConfig.ClassLoaderContexts { |
| 158 | for _, v := range moduleConfig.ClassLoaderContexts[i] { |
| 159 | v.Host = android.PathForSource(ctx, *basePath+v.Device) |
| 160 | } |
| 161 | } |
| 162 | moduleConfig.EnforceUsesLibraries = false |
| 163 | for i, location := range moduleConfig.DexPreoptImageLocationsOnDevice { |
| 164 | moduleConfig.DexPreoptImageLocationsOnHost[i] = *basePath + location |
| 165 | } |
| 166 | } |
Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 167 | writeScripts(ctx, globalSoongConfig, globalConfig, moduleConfig, *dexpreoptScriptPath, *productPackagesPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 170 | func writeScripts(ctx android.BuilderContext, globalSoong *dexpreopt.GlobalSoongConfig, |
Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 171 | global *dexpreopt.GlobalConfig, module *dexpreopt.ModuleConfig, dexpreoptScriptPath string, |
| 172 | productPackagesPath string) { |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 173 | write := func(rule *android.RuleBuilder, file string) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 174 | script := &bytes.Buffer{} |
| 175 | script.WriteString(scriptHeader) |
| 176 | for _, c := range rule.Commands() { |
| 177 | script.WriteString(c) |
| 178 | script.WriteString("\n\n") |
| 179 | } |
| 180 | |
| 181 | depFile := &bytes.Buffer{} |
| 182 | |
| 183 | fmt.Fprint(depFile, `: \`+"\n") |
Colin Cross | 25397f5 | 2019-02-15 16:03:58 -0800 | [diff] [blame] | 184 | for _, tool := range rule.Tools() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 185 | fmt.Fprintf(depFile, ` %s \`+"\n", tool) |
| 186 | } |
Colin Cross | 25397f5 | 2019-02-15 16:03:58 -0800 | [diff] [blame] | 187 | for _, input := range rule.Inputs() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 188 | // Assume the rule that ran the script already has a dependency on the input file passed on the |
| 189 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 190 | if input.String() != "$1" { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 191 | fmt.Fprintf(depFile, ` %s \`+"\n", input) |
| 192 | } |
| 193 | } |
| 194 | depFile.WriteString("\n") |
| 195 | |
| 196 | fmt.Fprintln(script, "rm -f $2.d") |
| 197 | // Write the output path unescaped so the $2 gets expanded |
| 198 | fmt.Fprintln(script, `echo -n $2 > $2.d`) |
| 199 | // Write the rest of the depsfile using cat <<'EOF', which will not do any shell expansion on |
| 200 | // the contents to preserve backslashes and special characters in filenames. |
| 201 | fmt.Fprintf(script, "cat >> $2.d <<'EOF'\n%sEOF\n", depFile.String()) |
| 202 | |
| 203 | err := pathtools.WriteFileIfChanged(file, script.Bytes(), 0755) |
| 204 | if err != nil { |
| 205 | panic(err) |
| 206 | } |
| 207 | } |
Spandan Das | 5ae65ee | 2024-04-16 22:03:26 +0000 | [diff] [blame] | 208 | cpApexSscpServerJar := false // dexpreopt_gen operates on make modules, and since sscp libraries are in soong, this should be a noop |
Jiakai Zhang | a449678 | 2023-05-17 16:57:30 +0100 | [diff] [blame] | 209 | dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule( |
Spandan Das | 5ae65ee | 2024-04-16 22:03:26 +0000 | [diff] [blame] | 210 | ctx, globalSoong, global, module, android.PathForTesting(productPackagesPath), cpApexSscpServerJar) |
Jeongik Cha | 4dda75e | 2021-04-27 23:56:44 +0900 | [diff] [blame] | 211 | if err != nil { |
| 212 | panic(err) |
| 213 | } |
| 214 | // When usesTargetFiles is true, only odex/vdex files are necessary. |
| 215 | // So skip redunant processes(such as copying the result to the artifact path, and zipping, and so on.) |
| 216 | if *usesTargetFiles { |
| 217 | write(dexpreoptRule, dexpreoptScriptPath) |
| 218 | return |
| 219 | } |
| 220 | installDir := module.BuildPath.InSameDir(ctx, "dexpreopt_install") |
| 221 | |
| 222 | dexpreoptRule.Command().FlagWithArg("rm -rf ", installDir.String()) |
| 223 | dexpreoptRule.Command().FlagWithArg("mkdir -p ", installDir.String()) |
| 224 | |
| 225 | for _, install := range dexpreoptRule.Installs() { |
| 226 | installPath := installDir.Join(ctx, strings.TrimPrefix(install.To, "/")) |
| 227 | dexpreoptRule.Command().Text("mkdir -p").Flag(filepath.Dir(installPath.String())) |
| 228 | dexpreoptRule.Command().Text("cp -f").Input(install.From).Output(installPath) |
| 229 | } |
| 230 | dexpreoptRule.Command().Tool(globalSoong.SoongZip). |
| 231 | FlagWithArg("-o ", "$2"). |
| 232 | FlagWithArg("-C ", installDir.String()). |
| 233 | FlagWithArg("-D ", installDir.String()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 234 | |
| 235 | // The written scripts will assume the input is $1 and the output is $2 |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 236 | if module.DexPath.String() != "$1" { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 237 | panic(fmt.Errorf("module.DexPath must be '$1', was %q", module.DexPath)) |
| 238 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 239 | |
| 240 | write(dexpreoptRule, dexpreoptScriptPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | const scriptHeader = `#!/bin/bash |
| 244 | |
| 245 | err() { |
| 246 | errno=$? |
| 247 | echo "error: $0:$1 exited with status $errno" >&2 |
| 248 | echo "error in command:" >&2 |
| 249 | sed -n -e "$1p" $0 >&2 |
| 250 | if [ "$errno" -ne 0 ]; then |
| 251 | exit $errno |
| 252 | else |
| 253 | exit 1 |
| 254 | fi |
| 255 | } |
| 256 | |
| 257 | trap 'err $LINENO' ERR |
| 258 | |
| 259 | ` |