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 dexpreopt |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "sort" |
| 20 | "strings" |
| 21 | ) |
| 22 | |
| 23 | type Install struct { |
| 24 | From, To string |
| 25 | } |
| 26 | |
| 27 | type Rule struct { |
| 28 | commands []*Command |
| 29 | installs []Install |
| 30 | } |
| 31 | |
| 32 | func (r *Rule) Install(from, to string) { |
| 33 | r.installs = append(r.installs, Install{from, to}) |
| 34 | } |
| 35 | |
| 36 | func (r *Rule) Command() *Command { |
| 37 | command := &Command{} |
| 38 | r.commands = append(r.commands, command) |
| 39 | return command |
| 40 | } |
| 41 | |
| 42 | func (r *Rule) Inputs() []string { |
| 43 | outputs := r.outputSet() |
| 44 | |
| 45 | inputs := make(map[string]bool) |
| 46 | for _, c := range r.commands { |
| 47 | for _, input := range c.inputs { |
| 48 | if !outputs[input] { |
| 49 | inputs[input] = true |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | var inputList []string |
| 55 | for input := range inputs { |
| 56 | inputList = append(inputList, input) |
| 57 | } |
| 58 | sort.Strings(inputList) |
| 59 | |
| 60 | return inputList |
| 61 | } |
| 62 | |
| 63 | func (r *Rule) outputSet() map[string]bool { |
| 64 | outputs := make(map[string]bool) |
| 65 | for _, c := range r.commands { |
| 66 | for _, output := range c.outputs { |
| 67 | outputs[output] = true |
| 68 | } |
| 69 | } |
| 70 | return outputs |
| 71 | } |
| 72 | |
| 73 | func (r *Rule) Outputs() []string { |
| 74 | outputs := r.outputSet() |
| 75 | |
| 76 | var outputList []string |
| 77 | for output := range outputs { |
| 78 | outputList = append(outputList, output) |
| 79 | } |
| 80 | sort.Strings(outputList) |
| 81 | return outputList |
| 82 | } |
| 83 | |
| 84 | func (r *Rule) Installs() []Install { |
| 85 | return append([]Install(nil), r.installs...) |
| 86 | } |
| 87 | |
| 88 | func (r *Rule) Tools() []string { |
| 89 | var tools []string |
| 90 | for _, c := range r.commands { |
| 91 | tools = append(tools, c.tools...) |
| 92 | } |
| 93 | return tools |
| 94 | } |
| 95 | |
| 96 | func (r *Rule) Commands() []string { |
| 97 | var commands []string |
| 98 | for _, c := range r.commands { |
| 99 | commands = append(commands, string(c.buf)) |
| 100 | } |
| 101 | return commands |
| 102 | } |
| 103 | |
| 104 | type Command struct { |
| 105 | buf []byte |
| 106 | inputs []string |
| 107 | outputs []string |
| 108 | tools []string |
| 109 | } |
| 110 | |
| 111 | func (c *Command) Text(text string) *Command { |
| 112 | if len(c.buf) > 0 { |
| 113 | c.buf = append(c.buf, ' ') |
| 114 | } |
| 115 | c.buf = append(c.buf, text...) |
| 116 | return c |
| 117 | } |
| 118 | |
| 119 | func (c *Command) Textf(format string, a ...interface{}) *Command { |
| 120 | return c.Text(fmt.Sprintf(format, a...)) |
| 121 | } |
| 122 | |
| 123 | func (c *Command) Flag(flag string) *Command { |
| 124 | return c.Text(flag) |
| 125 | } |
| 126 | |
| 127 | func (c *Command) FlagWithArg(flag, arg string) *Command { |
| 128 | return c.Text(flag + arg) |
| 129 | } |
| 130 | |
| 131 | func (c *Command) FlagWithList(flag string, list []string, sep string) *Command { |
| 132 | return c.Text(flag + strings.Join(list, sep)) |
| 133 | } |
| 134 | |
| 135 | func (c *Command) Tool(path string) *Command { |
| 136 | c.tools = append(c.tools, path) |
| 137 | return c.Text(path) |
| 138 | } |
| 139 | |
| 140 | func (c *Command) Input(path string) *Command { |
| 141 | c.inputs = append(c.inputs, path) |
| 142 | return c.Text(path) |
| 143 | } |
| 144 | |
| 145 | func (c *Command) Implicit(path string) *Command { |
| 146 | c.inputs = append(c.inputs, path) |
| 147 | return c |
| 148 | } |
| 149 | |
| 150 | func (c *Command) Output(path string) *Command { |
| 151 | c.outputs = append(c.outputs, path) |
| 152 | return c.Text(path) |
| 153 | } |
| 154 | |
| 155 | func (c *Command) ImplicitOutput(path string) *Command { |
| 156 | c.outputs = append(c.outputs, path) |
| 157 | return c |
| 158 | } |
| 159 | |
| 160 | func (c *Command) FlagWithInput(flag, path string) *Command { |
| 161 | c.inputs = append(c.inputs, path) |
| 162 | return c.Text(flag + path) |
| 163 | } |
| 164 | |
| 165 | func (c *Command) FlagWithInputList(flag string, paths []string, sep string) *Command { |
| 166 | c.inputs = append(c.inputs, paths...) |
| 167 | return c.FlagWithList(flag, paths, sep) |
| 168 | } |
| 169 | |
| 170 | func (c *Command) FlagWithOutput(flag, path string) *Command { |
| 171 | c.outputs = append(c.outputs, path) |
| 172 | return c.Text(flag + path) |
| 173 | } |