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" |
| 21 | "os" |
| 22 | "path/filepath" |
| 23 | "runtime" |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 24 | "strings" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 25 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 26 | "android/soong/android" |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 27 | "android/soong/dexpreopt" |
| 28 | |
| 29 | "github.com/google/blueprint/pathtools" |
| 30 | ) |
| 31 | |
| 32 | var ( |
| 33 | dexpreoptScriptPath = flag.String("dexpreopt_script", "", "path to output dexpreopt script") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 34 | globalConfigPath = flag.String("global", "", "path to global configuration file") |
| 35 | moduleConfigPath = flag.String("module", "", "path to module configuration file") |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 36 | outDir = flag.String("out_dir", "", "path to output directory") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 37 | ) |
| 38 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 39 | type pathContext struct { |
| 40 | config android.Config |
| 41 | } |
| 42 | |
| 43 | func (x *pathContext) Fs() pathtools.FileSystem { return pathtools.OsFs } |
| 44 | func (x *pathContext) Config() android.Config { return x.config } |
| 45 | func (x *pathContext) AddNinjaFileDeps(...string) {} |
| 46 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 47 | func 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 Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 66 | 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 Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 74 | ctx := &pathContext{android.TestConfig(*outDir, nil)} |
| 75 | |
Colin Cross | 2d00f0d | 2019-05-09 21:50:00 -0700 | [diff] [blame] | 76 | globalConfig, _, err := dexpreopt.LoadGlobalConfig(ctx, *globalConfigPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 77 | if err != nil { |
| 78 | fmt.Fprintf(os.Stderr, "error loading global config %q: %s\n", *globalConfigPath, err) |
| 79 | os.Exit(2) |
| 80 | } |
| 81 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 82 | moduleConfig, err := dexpreopt.LoadModuleConfig(ctx, *moduleConfigPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 83 | if err != nil { |
| 84 | fmt.Fprintf(os.Stderr, "error loading module config %q: %s\n", *moduleConfigPath, err) |
| 85 | os.Exit(2) |
| 86 | } |
| 87 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 88 | moduleConfig.DexPath = android.PathForTesting("$1") |
| 89 | |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 90 | 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 Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame^] | 104 | writeScripts(ctx, globalConfig, moduleConfig, *dexpreoptScriptPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 107 | func writeScripts(ctx android.PathContext, global dexpreopt.GlobalConfig, module dexpreopt.ModuleConfig, |
Nicolas Geoffray | c1bf724 | 2019-10-18 14:51:38 +0100 | [diff] [blame^] | 108 | dexpreoptScriptPath string) { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 109 | dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, module) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 110 | if err != nil { |
| 111 | panic(err) |
| 112 | } |
| 113 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 114 | installDir := module.BuildPath.InSameDir(ctx, "dexpreopt_install") |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 115 | |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 116 | dexpreoptRule.Command().FlagWithArg("rm -rf ", installDir.String()) |
| 117 | dexpreoptRule.Command().FlagWithArg("mkdir -p ", installDir.String()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 118 | |
| 119 | for _, install := range dexpreoptRule.Installs() { |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 120 | installPath := installDir.Join(ctx, strings.TrimPrefix(install.To, "/")) |
| 121 | dexpreoptRule.Command().Text("mkdir -p").Flag(filepath.Dir(installPath.String())) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 122 | dexpreoptRule.Command().Text("cp -f").Input(install.From).Output(installPath) |
| 123 | } |
| 124 | dexpreoptRule.Command().Tool(global.Tools.SoongZip). |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 125 | FlagWithArg("-o ", "$2"). |
| 126 | FlagWithArg("-C ", installDir.String()). |
| 127 | FlagWithArg("-D ", installDir.String()) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 128 | |
Colin Cross | feec25b | 2019-01-30 17:32:39 -0800 | [diff] [blame] | 129 | write := func(rule *android.RuleBuilder, file string) { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 130 | 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 Cross | 25397f5 | 2019-02-15 16:03:58 -0800 | [diff] [blame] | 140 | for _, tool := range rule.Tools() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 141 | fmt.Fprintf(depFile, ` %s \`+"\n", tool) |
| 142 | } |
Colin Cross | 25397f5 | 2019-02-15 16:03:58 -0800 | [diff] [blame] | 143 | for _, input := range rule.Inputs() { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 144 | // Assume the rule that ran the script already has a dependency on the input file passed on the |
| 145 | // command line. |
Colin Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 146 | if input.String() != "$1" { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 147 | 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 Cross | 69f59a3 | 2019-02-15 10:39:37 -0800 | [diff] [blame] | 166 | if module.DexPath.String() != "$1" { |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 167 | panic(fmt.Errorf("module.DexPath must be '$1', was %q", module.DexPath)) |
| 168 | } |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 169 | |
| 170 | write(dexpreoptRule, dexpreoptScriptPath) |
Colin Cross | 43f08db | 2018-11-12 10:13:39 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | const scriptHeader = `#!/bin/bash |
| 174 | |
| 175 | err() { |
| 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 | |
| 187 | trap 'err $LINENO' ERR |
| 188 | |
| 189 | ` |