blob: fd4cf823e18d8309ef7a3cc1d0bf946408205410 [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 dexpreopt
16
17import (
18 "fmt"
19 "sort"
20 "strings"
21)
22
23type Install struct {
24 From, To string
25}
26
27type Rule struct {
28 commands []*Command
29 installs []Install
30}
31
32func (r *Rule) Install(from, to string) {
33 r.installs = append(r.installs, Install{from, to})
34}
35
36func (r *Rule) Command() *Command {
37 command := &Command{}
38 r.commands = append(r.commands, command)
39 return command
40}
41
42func (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
63func (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
73func (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
84func (r *Rule) Installs() []Install {
85 return append([]Install(nil), r.installs...)
86}
87
88func (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
96func (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
104type Command struct {
105 buf []byte
106 inputs []string
107 outputs []string
108 tools []string
109}
110
111func (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
119func (c *Command) Textf(format string, a ...interface{}) *Command {
120 return c.Text(fmt.Sprintf(format, a...))
121}
122
123func (c *Command) Flag(flag string) *Command {
124 return c.Text(flag)
125}
126
127func (c *Command) FlagWithArg(flag, arg string) *Command {
128 return c.Text(flag + arg)
129}
130
131func (c *Command) FlagWithList(flag string, list []string, sep string) *Command {
132 return c.Text(flag + strings.Join(list, sep))
133}
134
135func (c *Command) Tool(path string) *Command {
136 c.tools = append(c.tools, path)
137 return c.Text(path)
138}
139
140func (c *Command) Input(path string) *Command {
141 c.inputs = append(c.inputs, path)
142 return c.Text(path)
143}
144
145func (c *Command) Implicit(path string) *Command {
146 c.inputs = append(c.inputs, path)
147 return c
148}
149
150func (c *Command) Output(path string) *Command {
151 c.outputs = append(c.outputs, path)
152 return c.Text(path)
153}
154
155func (c *Command) ImplicitOutput(path string) *Command {
156 c.outputs = append(c.outputs, path)
157 return c
158}
159
160func (c *Command) FlagWithInput(flag, path string) *Command {
161 c.inputs = append(c.inputs, path)
162 return c.Text(flag + path)
163}
164
165func (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
170func (c *Command) FlagWithOutput(flag, path string) *Command {
171 c.outputs = append(c.outputs, path)
172 return c.Text(flag + path)
173}