blob: d2faa00dea6197792d766fcd6b9e119e29465d91 [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 (
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000033 dexpreoptScriptPath = flag.String("dexpreopt_script", "", "path to output dexpreopt script")
34 globalSoongConfigPath = flag.String("global_soong", "", "path to global configuration file for settings originating from Soong")
35 globalConfigPath = flag.String("global", "", "path to global configuration file")
36 moduleConfigPath = flag.String("module", "", "path to module configuration file")
37 outDir = flag.String("out_dir", "", "path to output directory")
Colin Cross43f08db2018-11-12 10:13:39 -080038)
39
Colin Cross69f59a32019-02-15 10:39:37 -080040type pathContext struct {
41 config android.Config
42}
43
44func (x *pathContext) Fs() pathtools.FileSystem { return pathtools.OsFs }
45func (x *pathContext) Config() android.Config { return x.config }
46func (x *pathContext) AddNinjaFileDeps(...string) {}
47
Colin Cross43f08db2018-11-12 10:13:39 -080048func main() {
49 flag.Parse()
50
51 usage := func(err string) {
52 if err != "" {
53 fmt.Println(err)
54 flag.Usage()
55 os.Exit(1)
56 }
57 }
58
59 if flag.NArg() > 0 {
60 usage("unrecognized argument " + flag.Arg(0))
61 }
62
63 if *dexpreoptScriptPath == "" {
64 usage("path to output dexpreopt script is required")
65 }
66
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000067 if *globalSoongConfigPath == "" {
68 usage("--global_soong configuration file is required")
69 }
70
Colin Cross43f08db2018-11-12 10:13:39 -080071 if *globalConfigPath == "" {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000072 usage("--global configuration file is required")
Colin Cross43f08db2018-11-12 10:13:39 -080073 }
74
75 if *moduleConfigPath == "" {
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000076 usage("--module configuration file is required")
Colin Cross43f08db2018-11-12 10:13:39 -080077 }
78
Colin Cross98be1bb2019-12-13 20:41:13 -080079 ctx := &pathContext{android.TestConfig(*outDir, nil, "", nil)}
Colin Cross69f59a32019-02-15 10:39:37 -080080
Martin Stjernholmc52aaf12020-01-06 23:11:37 +000081 globalSoongConfig, err := dexpreopt.LoadGlobalSoongConfig(ctx, *globalSoongConfigPath)
82 if err != nil {
83 fmt.Fprintf(os.Stderr, "error loading global config %q: %s\n", *globalSoongConfigPath, err)
84 os.Exit(2)
85 }
86
87 globalConfig, _, err := dexpreopt.LoadGlobalConfig(ctx, *globalConfigPath, globalSoongConfig)
Colin Cross43f08db2018-11-12 10:13:39 -080088 if err != nil {
89 fmt.Fprintf(os.Stderr, "error loading global config %q: %s\n", *globalConfigPath, err)
90 os.Exit(2)
91 }
92
Colin Cross69f59a32019-02-15 10:39:37 -080093 moduleConfig, err := dexpreopt.LoadModuleConfig(ctx, *moduleConfigPath)
Colin Cross43f08db2018-11-12 10:13:39 -080094 if err != nil {
95 fmt.Fprintf(os.Stderr, "error loading module config %q: %s\n", *moduleConfigPath, err)
96 os.Exit(2)
97 }
98
Colin Cross69f59a32019-02-15 10:39:37 -080099 moduleConfig.DexPath = android.PathForTesting("$1")
100
Colin Cross43f08db2018-11-12 10:13:39 -0800101 defer func() {
102 if r := recover(); r != nil {
103 switch x := r.(type) {
104 case runtime.Error:
105 panic(x)
106 case error:
107 fmt.Fprintln(os.Stderr, "error:", r)
108 os.Exit(3)
109 default:
110 panic(x)
111 }
112 }
113 }()
114
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100115 writeScripts(ctx, globalConfig, moduleConfig, *dexpreoptScriptPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800116}
117
Colin Cross69f59a32019-02-15 10:39:37 -0800118func writeScripts(ctx android.PathContext, global dexpreopt.GlobalConfig, module dexpreopt.ModuleConfig,
Nicolas Geoffrayc1bf7242019-10-18 14:51:38 +0100119 dexpreoptScriptPath string) {
Colin Cross69f59a32019-02-15 10:39:37 -0800120 dexpreoptRule, err := dexpreopt.GenerateDexpreoptRule(ctx, global, module)
Colin Cross43f08db2018-11-12 10:13:39 -0800121 if err != nil {
122 panic(err)
123 }
124
Colin Cross69f59a32019-02-15 10:39:37 -0800125 installDir := module.BuildPath.InSameDir(ctx, "dexpreopt_install")
Colin Cross43f08db2018-11-12 10:13:39 -0800126
Colin Cross69f59a32019-02-15 10:39:37 -0800127 dexpreoptRule.Command().FlagWithArg("rm -rf ", installDir.String())
128 dexpreoptRule.Command().FlagWithArg("mkdir -p ", installDir.String())
Colin Cross43f08db2018-11-12 10:13:39 -0800129
130 for _, install := range dexpreoptRule.Installs() {
Colin Cross69f59a32019-02-15 10:39:37 -0800131 installPath := installDir.Join(ctx, strings.TrimPrefix(install.To, "/"))
132 dexpreoptRule.Command().Text("mkdir -p").Flag(filepath.Dir(installPath.String()))
Colin Cross43f08db2018-11-12 10:13:39 -0800133 dexpreoptRule.Command().Text("cp -f").Input(install.From).Output(installPath)
134 }
Martin Stjernholmc52aaf12020-01-06 23:11:37 +0000135 dexpreoptRule.Command().Tool(global.SoongConfig.SoongZip).
Colin Cross69f59a32019-02-15 10:39:37 -0800136 FlagWithArg("-o ", "$2").
137 FlagWithArg("-C ", installDir.String()).
138 FlagWithArg("-D ", installDir.String())
Colin Cross43f08db2018-11-12 10:13:39 -0800139
Colin Crossfeec25b2019-01-30 17:32:39 -0800140 write := func(rule *android.RuleBuilder, file string) {
Colin Cross43f08db2018-11-12 10:13:39 -0800141 script := &bytes.Buffer{}
142 script.WriteString(scriptHeader)
143 for _, c := range rule.Commands() {
144 script.WriteString(c)
145 script.WriteString("\n\n")
146 }
147
148 depFile := &bytes.Buffer{}
149
150 fmt.Fprint(depFile, `: \`+"\n")
Colin Cross25397f52019-02-15 16:03:58 -0800151 for _, tool := range rule.Tools() {
Colin Cross43f08db2018-11-12 10:13:39 -0800152 fmt.Fprintf(depFile, ` %s \`+"\n", tool)
153 }
Colin Cross25397f52019-02-15 16:03:58 -0800154 for _, input := range rule.Inputs() {
Colin Cross43f08db2018-11-12 10:13:39 -0800155 // Assume the rule that ran the script already has a dependency on the input file passed on the
156 // command line.
Colin Cross69f59a32019-02-15 10:39:37 -0800157 if input.String() != "$1" {
Colin Cross43f08db2018-11-12 10:13:39 -0800158 fmt.Fprintf(depFile, ` %s \`+"\n", input)
159 }
160 }
161 depFile.WriteString("\n")
162
163 fmt.Fprintln(script, "rm -f $2.d")
164 // Write the output path unescaped so the $2 gets expanded
165 fmt.Fprintln(script, `echo -n $2 > $2.d`)
166 // Write the rest of the depsfile using cat <<'EOF', which will not do any shell expansion on
167 // the contents to preserve backslashes and special characters in filenames.
168 fmt.Fprintf(script, "cat >> $2.d <<'EOF'\n%sEOF\n", depFile.String())
169
170 err := pathtools.WriteFileIfChanged(file, script.Bytes(), 0755)
171 if err != nil {
172 panic(err)
173 }
174 }
175
176 // The written scripts will assume the input is $1 and the output is $2
Colin Cross69f59a32019-02-15 10:39:37 -0800177 if module.DexPath.String() != "$1" {
Colin Cross43f08db2018-11-12 10:13:39 -0800178 panic(fmt.Errorf("module.DexPath must be '$1', was %q", module.DexPath))
179 }
Colin Cross43f08db2018-11-12 10:13:39 -0800180
181 write(dexpreoptRule, dexpreoptScriptPath)
Colin Cross43f08db2018-11-12 10:13:39 -0800182}
183
184const scriptHeader = `#!/bin/bash
185
186err() {
187 errno=$?
188 echo "error: $0:$1 exited with status $errno" >&2
189 echo "error in command:" >&2
190 sed -n -e "$1p" $0 >&2
191 if [ "$errno" -ne 0 ]; then
192 exit $errno
193 else
194 exit 1
195 fi
196}
197
198trap 'err $LINENO' ERR
199
200`