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") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 40 | ) |
| 41 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame^] | 42 | type builderContext struct { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 43 | config android.Config |
| 44 | } |
| 45 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame^] | 46 | func (x *builderContext) Config() android.Config { return x.config } |
| 47 | func (x *builderContext) AddNinjaFileDeps(...string) {} |
| 48 | func (x *builderContext) Build(android.PackageContext, android.BuildParams) {} |
| 49 | func (x *builderContext) Rule(android.PackageContext, string, blueprint.RuleParams, ...string) blueprint.Rule { |
| 50 | return nil |
| 51 | } |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 52 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 53 | func main() { |
| 54 | flag.Parse() |
| 55 | |
| 56 | usage := func(err string) { |
| 57 | if err != "" { |
| 58 | fmt.Println(err) |
| 59 | flag.Usage() |
| 60 | os.Exit(1) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | if flag.NArg() > 0 { |
| 65 | usage("unrecognized argument " + flag.Arg(0)) |
| 66 | } |
| 67 | |
| 68 | if *dexpreoptScriptPath == "" { |
| 69 | usage("path to output dexpreopt script is required") |
| 70 | } |
| 71 | |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 72 | if *globalSoongConfigPath == "" { |
| 73 | usage("--global_soong configuration file is required") |
| 74 | } |
| 75 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 76 | if *globalConfigPath == "" { |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 77 | usage("--global configuration file is required") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | if *moduleConfigPath == "" { |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 81 | usage("--module configuration file is required") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame^] | 84 | ctx := &builderContext{android.NullConfig(*outDir)} |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 85 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 86 | globalSoongConfigData, err := ioutil.ReadFile(*globalSoongConfigPath) |
| 87 | if err != nil { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 88 | 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] | 89 | os.Exit(2) |
| 90 | } |
| 91 | |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 92 | globalSoongConfig, err := dexpreopt.ParseGlobalSoongConfig(ctx, globalSoongConfigData) |
Martin Stjernholm | c52aaf1 | 2020-01-06 23:11:37 +0000 | [diff] [blame] | 93 | if err != nil { |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 94 | 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] | 95 | os.Exit(2) |
| 96 | } |
| 97 | |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 98 | globalConfigData, err := ioutil.ReadFile(*globalConfigPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 99 | if err != nil { |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 100 | 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] | 101 | os.Exit(2) |
| 102 | } |
| 103 | |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 104 | globalConfig, err := dexpreopt.ParseGlobalConfig(ctx, globalConfigData) |
Colin Cross | 988414c | 2020-01-11 01:11:46 +0000 | [diff] [blame] | 105 | if err != nil { |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 106 | 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] | 107 | os.Exit(2) |
| 108 | } |
| 109 | |
| 110 | moduleConfigData, err := ioutil.ReadFile(*moduleConfigPath) |
| 111 | if err != nil { |
| 112 | fmt.Fprintf(os.Stderr, "error reading module config %q: %s\n", *moduleConfigPath, err) |
| 113 | os.Exit(2) |
| 114 | } |
| 115 | |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 116 | moduleConfig, err := dexpreopt.ParseModuleConfig(ctx, moduleConfigData) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 117 | if err != nil { |
Martin Stjernholm | 40f9f3c | 2020-01-20 18:12:23 +0000 | [diff] [blame] | 118 | 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] | 119 | os.Exit(2) |
| 120 | } |
| 121 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 122 | moduleConfig.DexPath = android.PathForTesting("$1") |
| 123 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 124 | defer func() { |
| 125 | if r := recover(); r != nil { |
| 126 | switch x := r.(type) { |
| 127 | case runtime.Error: |
| 128 | panic(x) |
| 129 | case error: |
| 130 | fmt.Fprintln(os.Stderr, "error:", r) |
| 131 | os.Exit(3) |
| 132 | default: |
| 133 | panic(x) |
| 134 | } |
| 135 | } |
| 136 | }() |
| 137 | |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 138 | writeScripts(ctx, globalSoongConfig, globalConfig, moduleConfig, *dexpreoptScriptPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame^] | 141 | func writeScripts(ctx android.BuilderContext, globalSoong *dexpreopt.GlobalSoongConfig, |
Martin Stjernholm | 8d80cee | 2020-01-31 17:44:54 +0000 | [diff] [blame] | 142 | global *dexpreopt.GlobalConfig, module *dexpreopt.ModuleConfig, dexpreoptScriptPath string) { |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 143 | dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, globalSoong, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 144 | if err != nil { |
| 145 | panic(err) |
| 146 | } |
| 147 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 148 | installDir := module.BuildPath.InSameDir(ctx, "dexpreopt_install") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 149 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 150 | dexpreoptRule.Command().FlagWithArg("rm -rf ", installDir.String()) |
| 151 | dexpreoptRule.Command().FlagWithArg("mkdir -p ", installDir.String()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 152 | |
| 153 | for _, install := range dexpreoptRule.Installs() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 154 | installPath := installDir.Join(ctx, strings.TrimPrefix(install.To, "/")) |
| 155 | dexpreoptRule.Command().Text("mkdir -p").Flag(filepath.Dir(installPath.String())) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 156 | dexpreoptRule.Command().Text("cp -f").Input(install.From).Output(installPath) |
| 157 | } |
Martin Stjernholm | 75a48d8 | 2020-01-10 20:32:59 +0000 | [diff] [blame] | 158 | dexpreoptRule.Command().Tool(globalSoong.SoongZip). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 159 | FlagWithArg("-o ", "$2"). |
| 160 | FlagWithArg("-C ", installDir.String()). |
| 161 | FlagWithArg("-D ", installDir.String()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 162 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 163 | write := func(rule *android.RuleBuilder, file string) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 164 | script := &bytes.Buffer{} |
| 165 | script.WriteString(scriptHeader) |
| 166 | for _, c := range rule.Commands() { |
| 167 | script.WriteString(c) |
| 168 | script.WriteString("\n\n") |
| 169 | } |
| 170 | |
| 171 | depFile := &bytes.Buffer{} |
| 172 | |
| 173 | fmt.Fprint(depFile, `: \`+"\n") |
Colin Cross | 25397f5 | 2019-02-15 16:03:58 -0800 | [diff] [blame] | 174 | for _, tool := range rule.Tools() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 175 | fmt.Fprintf(depFile, ` %s \`+"\n", tool) |
| 176 | } |
Colin Cross | 25397f5 | 2019-02-15 16:03:58 -0800 | [diff] [blame] | 177 | for _, input := range rule.Inputs() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 178 | // Assume the rule that ran the script already has a dependency on the input file passed on the |
| 179 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 180 | if input.String() != "$1" { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 181 | fmt.Fprintf(depFile, ` %s \`+"\n", input) |
| 182 | } |
| 183 | } |
| 184 | depFile.WriteString("\n") |
| 185 | |
| 186 | fmt.Fprintln(script, "rm -f $2.d") |
| 187 | // Write the output path unescaped so the $2 gets expanded |
| 188 | fmt.Fprintln(script, `echo -n $2 > $2.d`) |
| 189 | // Write the rest of the depsfile using cat <<'EOF', which will not do any shell expansion on |
| 190 | // the contents to preserve backslashes and special characters in filenames. |
| 191 | fmt.Fprintf(script, "cat >> $2.d <<'EOF'\n%sEOF\n", depFile.String()) |
| 192 | |
| 193 | err := pathtools.WriteFileIfChanged(file, script.Bytes(), 0755) |
| 194 | if err != nil { |
| 195 | panic(err) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // 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] | 200 | if module.DexPath.String() != "$1" { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 201 | panic(fmt.Errorf("module.DexPath must be '$1', was %q", module.DexPath)) |
| 202 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 203 | |
| 204 | write(dexpreoptRule, dexpreoptScriptPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | const scriptHeader = `#!/bin/bash |
| 208 | |
| 209 | err() { |
| 210 | errno=$? |
| 211 | echo "error: $0:$1 exited with status $errno" >&2 |
| 212 | echo "error in command:" >&2 |
| 213 | sed -n -e "$1p" $0 >&2 |
| 214 | if [ "$errno" -ne 0 ]; then |
| 215 | exit $errno |
| 216 | else |
| 217 | exit 1 |
| 218 | fi |
| 219 | } |
| 220 | |
| 221 | trap 'err $LINENO' ERR |
| 222 | |
| 223 | ` |