Colin Cross | 8e0c511 | 2015-01-23 14:15:10 -0800 | [diff] [blame] | 1 | // Copyright 2014 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 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 15 | package blueprint |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io" |
| 20 | "strings" |
| 21 | "unicode" |
| 22 | ) |
| 23 | |
| 24 | const ( |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 25 | indentWidth = 4 |
| 26 | maxIndentDepth = 2 |
| 27 | lineWidth = 80 |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 30 | var indentString = strings.Repeat(" ", indentWidth*maxIndentDepth) |
| 31 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 32 | type ninjaWriter struct { |
| 33 | writer io.Writer |
| 34 | |
| 35 | justDidBlankLine bool // true if the last operation was a BlankLine |
| 36 | } |
| 37 | |
| 38 | func newNinjaWriter(writer io.Writer) *ninjaWriter { |
| 39 | return &ninjaWriter{ |
| 40 | writer: writer, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | func (n *ninjaWriter) Comment(comment string) error { |
| 45 | n.justDidBlankLine = false |
| 46 | |
| 47 | const lineHeaderLen = len("# ") |
| 48 | const maxLineLen = lineWidth - lineHeaderLen |
| 49 | |
| 50 | var lineStart, lastSplitPoint int |
| 51 | for i, r := range comment { |
| 52 | if unicode.IsSpace(r) { |
| 53 | // We know we can safely split the line here. |
| 54 | lastSplitPoint = i + 1 |
| 55 | } |
| 56 | |
| 57 | var line string |
| 58 | var writeLine bool |
| 59 | switch { |
| 60 | case r == '\n': |
| 61 | // Output the line without trimming the left so as to allow comments |
| 62 | // to contain their own indentation. |
| 63 | line = strings.TrimRightFunc(comment[lineStart:i], unicode.IsSpace) |
| 64 | writeLine = true |
| 65 | |
| 66 | case (i-lineStart > maxLineLen) && (lastSplitPoint > lineStart): |
| 67 | // The line has grown too long and is splittable. Split it at the |
| 68 | // last split point. |
| 69 | line = strings.TrimSpace(comment[lineStart:lastSplitPoint]) |
| 70 | writeLine = true |
| 71 | } |
| 72 | |
| 73 | if writeLine { |
| 74 | line = strings.TrimSpace("# "+line) + "\n" |
| 75 | _, err := io.WriteString(n.writer, line) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | lineStart = lastSplitPoint |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if lineStart != len(comment) { |
| 84 | line := strings.TrimSpace(comment[lineStart:]) |
| 85 | _, err := fmt.Fprintf(n.writer, "# %s\n", line) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return nil |
| 92 | } |
| 93 | |
| 94 | func (n *ninjaWriter) Pool(name string) error { |
| 95 | n.justDidBlankLine = false |
| 96 | _, err := fmt.Fprintf(n.writer, "pool %s\n", name) |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | func (n *ninjaWriter) Rule(name string) error { |
| 101 | n.justDidBlankLine = false |
| 102 | _, err := fmt.Fprintf(n.writer, "rule %s\n", name) |
| 103 | return err |
| 104 | } |
| 105 | |
| 106 | func (n *ninjaWriter) Build(rule string, outputs, explicitDeps, implicitDeps, |
| 107 | orderOnlyDeps []string) error { |
| 108 | |
| 109 | n.justDidBlankLine = false |
| 110 | |
| 111 | const lineWrapLen = len(" $") |
| 112 | const maxLineLen = lineWidth - lineWrapLen |
| 113 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 114 | wrapper := ninjaWriterWithWrap{ |
| 115 | ninjaWriter: n, |
| 116 | maxLineLen: maxLineLen, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 119 | wrapper.WriteString("build") |
| 120 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 121 | for _, output := range outputs { |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 122 | wrapper.WriteStringWithSpace(output) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 125 | wrapper.WriteString(":") |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 126 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 127 | wrapper.WriteStringWithSpace(rule) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 128 | |
| 129 | for _, dep := range explicitDeps { |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 130 | wrapper.WriteStringWithSpace(dep) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if len(implicitDeps) > 0 { |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 134 | wrapper.WriteStringWithSpace("|") |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 135 | |
| 136 | for _, dep := range implicitDeps { |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 137 | wrapper.WriteStringWithSpace(dep) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | if len(orderOnlyDeps) > 0 { |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 142 | wrapper.WriteStringWithSpace("||") |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 143 | |
| 144 | for _, dep := range orderOnlyDeps { |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 145 | wrapper.WriteStringWithSpace(dep) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 149 | return wrapper.Flush() |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | func (n *ninjaWriter) Assign(name, value string) error { |
| 153 | n.justDidBlankLine = false |
| 154 | _, err := fmt.Fprintf(n.writer, "%s = %s\n", name, value) |
| 155 | return err |
| 156 | } |
| 157 | |
| 158 | func (n *ninjaWriter) ScopedAssign(name, value string) error { |
| 159 | n.justDidBlankLine = false |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 160 | _, err := fmt.Fprintf(n.writer, "%s%s = %s\n", indentString[:indentWidth], name, value) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 161 | return err |
| 162 | } |
| 163 | |
| 164 | func (n *ninjaWriter) Default(targets ...string) error { |
| 165 | n.justDidBlankLine = false |
| 166 | |
| 167 | const lineWrapLen = len(" $") |
| 168 | const maxLineLen = lineWidth - lineWrapLen |
| 169 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 170 | wrapper := ninjaWriterWithWrap{ |
| 171 | ninjaWriter: n, |
| 172 | maxLineLen: maxLineLen, |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 175 | wrapper.WriteString("default") |
| 176 | |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 177 | for _, target := range targets { |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 178 | wrapper.WriteString(" " + target) |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 181 | return wrapper.Flush() |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | func (n *ninjaWriter) BlankLine() (err error) { |
| 185 | // We don't output multiple blank lines in a row. |
| 186 | if !n.justDidBlankLine { |
| 187 | n.justDidBlankLine = true |
| 188 | _, err = io.WriteString(n.writer, "\n") |
| 189 | } |
| 190 | return err |
| 191 | } |
| 192 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 193 | type ninjaWriterWithWrap struct { |
| 194 | *ninjaWriter |
| 195 | maxLineLen int |
| 196 | writtenLen int |
| 197 | err error |
| 198 | } |
| 199 | |
| 200 | func (n *ninjaWriterWithWrap) writeString(s string, space bool) { |
| 201 | if n.err != nil { |
| 202 | return |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 205 | spaceLen := 0 |
| 206 | if space { |
| 207 | spaceLen = 1 |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Colin Cross | 8ac0a81 | 2015-04-14 15:38:36 -0700 | [diff] [blame^] | 210 | if n.writtenLen+len(s)+spaceLen > n.maxLineLen { |
| 211 | _, n.err = io.WriteString(n.writer, " $\n") |
| 212 | if n.err != nil { |
| 213 | return |
| 214 | } |
| 215 | _, n.err = io.WriteString(n.writer, indentString[:indentWidth*2]) |
| 216 | if n.err != nil { |
| 217 | return |
| 218 | } |
| 219 | n.writtenLen = indentWidth * 2 |
| 220 | s = strings.TrimLeftFunc(s, unicode.IsSpace) |
| 221 | } else if space { |
| 222 | io.WriteString(n.writer, " ") |
| 223 | n.writtenLen++ |
| 224 | } |
| 225 | |
| 226 | _, n.err = io.WriteString(n.writer, s) |
| 227 | n.writtenLen += len(s) |
| 228 | } |
| 229 | |
| 230 | func (n *ninjaWriterWithWrap) WriteString(s string) { |
| 231 | n.writeString(s, false) |
| 232 | } |
| 233 | |
| 234 | func (n *ninjaWriterWithWrap) WriteStringWithSpace(s string) { |
| 235 | n.writeString(s, true) |
| 236 | } |
| 237 | |
| 238 | func (n *ninjaWriterWithWrap) Flush() error { |
| 239 | if n.err != nil { |
| 240 | return n.err |
| 241 | } |
| 242 | _, err := io.WriteString(n.writer, "\n") |
| 243 | return err |
Jamie Gennis | 1bc967e | 2014-05-27 16:34:41 -0700 | [diff] [blame] | 244 | } |