Shinichiro Hamaji | b69bf8a | 2015-06-10 14:52:06 +0900 | [diff] [blame] | 1 | // Copyright 2015 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 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 15 | package kati |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "bytes" |
| 19 | "fmt" |
| 20 | "os" |
Shinichiro Hamaji | 11c4584 | 2015-05-27 19:02:07 +0900 | [diff] [blame] | 21 | "path/filepath" |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 22 | "regexp" |
| 23 | "runtime" |
Fumitoshi Ukai | e08e9ee | 2015-07-13 13:14:41 +0900 | [diff] [blame] | 24 | "sort" |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 25 | "strings" |
Fumitoshi Ukai | 49599e5 | 2015-06-26 10:10:24 +0900 | [diff] [blame] | 26 | "time" |
Fumitoshi Ukai | f90f73c | 2015-07-28 14:37:02 +0900 | [diff] [blame] | 27 | |
| 28 | "github.com/golang/glog" |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 29 | ) |
| 30 | |
Fumitoshi Ukai | b816ffb | 2015-07-28 17:46:04 +0900 | [diff] [blame] | 31 | type nodeState int |
| 32 | |
| 33 | const ( |
Fumitoshi Ukai | 786846d | 2015-07-28 18:08:47 +0900 | [diff] [blame] | 34 | nodeInit nodeState = iota // not visited |
| 35 | nodeVisit // visited |
| 36 | nodeFile // visited & file exists |
| 37 | nodeAlias // visited & alias for other target |
| 38 | nodeMissing // visited & no target for this output |
| 39 | nodeBuild // visited & build emitted |
Fumitoshi Ukai | b816ffb | 2015-07-28 17:46:04 +0900 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | func (s nodeState) String() string { |
| 43 | switch s { |
| 44 | case nodeInit: |
| 45 | return "node-init" |
| 46 | case nodeVisit: |
| 47 | return "node-visit" |
| 48 | case nodeFile: |
| 49 | return "node-file" |
| 50 | case nodeAlias: |
| 51 | return "node-alias" |
Fumitoshi Ukai | 786846d | 2015-07-28 18:08:47 +0900 | [diff] [blame] | 52 | case nodeMissing: |
| 53 | return "node-missing" |
Fumitoshi Ukai | b816ffb | 2015-07-28 17:46:04 +0900 | [diff] [blame] | 54 | case nodeBuild: |
| 55 | return "node-build" |
| 56 | default: |
| 57 | return fmt.Sprintf("node-unknown[%d]", int(s)) |
| 58 | } |
| 59 | } |
| 60 | |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 61 | // NinjaGenerator generates ninja build files from DepGraph. |
| 62 | type NinjaGenerator struct { |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 63 | // Args is original arguments to generate the ninja file. |
| 64 | Args []string |
| 65 | // Suffix is suffix for generated files. |
| 66 | Suffix string |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 67 | // GomaDir is goma directory. If empty, goma will not be used. |
| 68 | GomaDir string |
Fumitoshi Ukai | b92f028 | 2015-07-21 14:05:51 +0900 | [diff] [blame] | 69 | // DetectAndroidEcho detects echo as description. |
| 70 | DetectAndroidEcho bool |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 71 | |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 72 | f *os.File |
| 73 | nodes []*DepNode |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 74 | exports map[string]bool |
Fumitoshi Ukai | a70b4ea | 2015-06-30 15:31:49 +0900 | [diff] [blame] | 75 | |
| 76 | ctx *execContext |
| 77 | |
Fumitoshi Ukai | b816ffb | 2015-07-28 17:46:04 +0900 | [diff] [blame] | 78 | ruleID int |
| 79 | done map[string]nodeState |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 80 | } |
| 81 | |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 82 | func (n *NinjaGenerator) init(g *DepGraph) { |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 83 | g.resolveVPATH() |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 84 | n.nodes = g.nodes |
| 85 | n.exports = g.exports |
| 86 | n.ctx = newExecContext(g.vars, g.vpaths, true) |
Fumitoshi Ukai | b816ffb | 2015-07-28 17:46:04 +0900 | [diff] [blame] | 87 | n.done = make(map[string]nodeState) |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 88 | } |
| 89 | |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 90 | func getDepfileImpl(ss string) (string, error) { |
Shinichiro Hamaji | 6dca220 | 2015-05-27 18:00:34 +0900 | [diff] [blame] | 91 | tss := ss + " " |
Fumitoshi Ukai | b8b8050 | 2015-07-21 14:28:26 +0900 | [diff] [blame] | 92 | if (!strings.Contains(tss, " -MD ") && !strings.Contains(tss, " -MMD ")) || !strings.Contains(tss, " -c ") { |
Shinichiro Hamaji | 6dca220 | 2015-05-27 18:00:34 +0900 | [diff] [blame] | 93 | return "", nil |
| 94 | } |
| 95 | |
| 96 | mfIndex := strings.Index(ss, " -MF ") |
| 97 | if mfIndex >= 0 { |
| 98 | mf := trimLeftSpace(ss[mfIndex+4:]) |
| 99 | if strings.Index(mf, " -MF ") >= 0 { |
| 100 | return "", fmt.Errorf("Multiple output file candidates in %s", ss) |
| 101 | } |
| 102 | mfEndIndex := strings.IndexAny(mf, " \t\n") |
| 103 | if mfEndIndex >= 0 { |
| 104 | mf = mf[:mfEndIndex] |
| 105 | } |
| 106 | |
Shinichiro Hamaji | 6dca220 | 2015-05-27 18:00:34 +0900 | [diff] [blame] | 107 | return mf, nil |
| 108 | } |
| 109 | |
| 110 | outIndex := strings.Index(ss, " -o ") |
| 111 | if outIndex < 0 { |
| 112 | return "", fmt.Errorf("Cannot find the depfile in %s", ss) |
| 113 | } |
| 114 | out := trimLeftSpace(ss[outIndex+4:]) |
| 115 | if strings.Index(out, " -o ") >= 0 { |
| 116 | return "", fmt.Errorf("Multiple output file candidates in %s", ss) |
| 117 | } |
| 118 | outEndIndex := strings.IndexAny(out, " \t\n") |
| 119 | if outEndIndex >= 0 { |
| 120 | out = out[:outEndIndex] |
| 121 | } |
| 122 | return stripExt(out) + ".d", nil |
| 123 | } |
| 124 | |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 125 | // getDepfile gets depfile from cmdline, and returns cmdline and depfile. |
| 126 | func getDepfile(cmdline string) (string, string, error) { |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 127 | // A hack for Android - llvm-rs-cc seems not to emit a dep file. |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 128 | if strings.Contains(cmdline, "bin/llvm-rs-cc ") { |
| 129 | return cmdline, "", nil |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 130 | } |
| 131 | |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 132 | depfile, err := getDepfileImpl(cmdline) |
| 133 | if depfile == "" || err != nil { |
| 134 | return cmdline, depfile, err |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 135 | } |
| 136 | |
Shinichiro Hamaji | 3438ae3 | 2015-06-05 11:33:20 +0900 | [diff] [blame] | 137 | // A hack for Makefiles generated by automake. |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 138 | mvCmd := "(mv -f " + depfile + " " |
| 139 | if i := strings.LastIndex(cmdline, mvCmd); i >= 0 { |
| 140 | rest := cmdline[i+len(mvCmd):] |
Shinichiro Hamaji | 3438ae3 | 2015-06-05 11:33:20 +0900 | [diff] [blame] | 141 | ei := strings.IndexByte(rest, ')') |
| 142 | if ei < 0 { |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 143 | return cmdline, "", fmt.Errorf("unbalanced parenthes? %s", cmdline) |
Shinichiro Hamaji | 3438ae3 | 2015-06-05 11:33:20 +0900 | [diff] [blame] | 144 | } |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 145 | cmdline = cmdline[:i] + "(cp -f " + depfile + " " + rest |
| 146 | return cmdline, depfile, nil |
Shinichiro Hamaji | 3438ae3 | 2015-06-05 11:33:20 +0900 | [diff] [blame] | 147 | } |
| 148 | |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 149 | // A hack for Android to get .P files instead of .d. |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 150 | p := stripExt(depfile) + ".P" |
| 151 | if strings.Contains(cmdline, p) { |
| 152 | rmfCmd := "; rm -f " + depfile |
| 153 | ncmdline := strings.Replace(cmdline, rmfCmd, "", 1) |
| 154 | if ncmdline == cmdline { |
| 155 | return cmdline, "", fmt.Errorf("cannot find removal of .d file: %s", cmdline) |
| 156 | } |
| 157 | return ncmdline, p, nil |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // A hack for Android. For .s files, GCC does not use |
| 161 | // C preprocessor, so it ignores -MF flag. |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 162 | as := "/" + stripExt(filepath.Base(depfile)) + ".s" |
| 163 | if strings.Contains(cmdline, as) { |
| 164 | return cmdline, "", nil |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 165 | } |
| 166 | |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 167 | cmdline += fmt.Sprintf(" && cp %s %s.tmp", depfile, depfile) |
| 168 | depfile += ".tmp" |
| 169 | return cmdline, depfile, nil |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 170 | } |
| 171 | |
Fumitoshi Ukai | 60f92c0 | 2015-07-29 10:09:36 +0900 | [diff] [blame] | 172 | func trimTailingSlash(s string) string { |
| 173 | if s == "" { |
| 174 | return s |
| 175 | } |
| 176 | if s[len(s)-1] != '\\' { |
| 177 | return s |
| 178 | } |
| 179 | // drop single trailing slash - multiline_arg.mk |
| 180 | if len(s) > 2 && s[len(s)-2] != '\\' { |
| 181 | return s[:len(s)-1] |
| 182 | } |
| 183 | // preserve two trailing slash - escaped_backslash.mk |
| 184 | return s |
| 185 | } |
| 186 | |
Shinichiro Hamaji | 246cd23 | 2015-05-27 17:08:23 +0900 | [diff] [blame] | 187 | func stripShellComment(s string) string { |
| 188 | if strings.IndexByte(s, '#') < 0 { |
| 189 | // Fast path. |
| 190 | return s |
| 191 | } |
Fumitoshi Ukai | 43b5666 | 2015-07-15 10:41:47 +0900 | [diff] [blame] | 192 | // set space as an initial value so the leading comment will be |
| 193 | // stripped out. |
| 194 | lastch := rune(' ') |
Shinichiro Hamaji | 246cd23 | 2015-05-27 17:08:23 +0900 | [diff] [blame] | 195 | var escape bool |
| 196 | var quote rune |
Fumitoshi Ukai | 03e9ea6 | 2015-07-29 11:39:00 +0900 | [diff] [blame] | 197 | var skip rune |
| 198 | var cmdsubst []rune |
| 199 | var buf bytes.Buffer |
| 200 | Loop: |
| 201 | for _, c := range s { |
| 202 | if skip != 0 { |
| 203 | if skip != c { |
| 204 | continue Loop |
| 205 | } |
| 206 | if len(cmdsubst) > 0 && cmdsubst[len(cmdsubst)-1] == skip { |
| 207 | cmdsubst = cmdsubst[:len(cmdsubst)-1] |
| 208 | } |
| 209 | skip = 0 |
| 210 | } |
Fumitoshi Ukai | b92f028 | 2015-07-21 14:05:51 +0900 | [diff] [blame] | 211 | if quote != 0 { |
Shinichiro Hamaji | 246cd23 | 2015-05-27 17:08:23 +0900 | [diff] [blame] | 212 | if quote == c && (quote == '\'' || !escape) { |
| 213 | quote = 0 |
| 214 | } |
| 215 | } else if !escape { |
Fumitoshi Ukai | 43b5666 | 2015-07-15 10:41:47 +0900 | [diff] [blame] | 216 | if c == '#' && isWhitespace(lastch) { |
Fumitoshi Ukai | 03e9ea6 | 2015-07-29 11:39:00 +0900 | [diff] [blame] | 217 | if len(cmdsubst) == 0 { |
| 218 | // strip comment until the end of line. |
| 219 | skip = '\n' |
| 220 | continue Loop |
| 221 | } |
| 222 | // strip comment until the end of command subst. |
| 223 | skip = cmdsubst[len(cmdsubst)-1] |
| 224 | continue Loop |
| 225 | } else if c == '\'' || c == '"' { |
Shinichiro Hamaji | 246cd23 | 2015-05-27 17:08:23 +0900 | [diff] [blame] | 226 | quote = c |
Fumitoshi Ukai | 03e9ea6 | 2015-07-29 11:39:00 +0900 | [diff] [blame] | 227 | } else if lastch == '$' && c == '(' { |
| 228 | cmdsubst = append(cmdsubst, ')') |
| 229 | } else if c == '`' { |
| 230 | cmdsubst = append(cmdsubst, '`') |
Shinichiro Hamaji | 246cd23 | 2015-05-27 17:08:23 +0900 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | if escape { |
| 234 | escape = false |
| 235 | } else if c == '\\' { |
| 236 | escape = true |
| 237 | } else { |
| 238 | escape = false |
| 239 | } |
Fumitoshi Ukai | 43b5666 | 2015-07-15 10:41:47 +0900 | [diff] [blame] | 240 | lastch = c |
Fumitoshi Ukai | 03e9ea6 | 2015-07-29 11:39:00 +0900 | [diff] [blame] | 241 | buf.WriteRune(c) |
Shinichiro Hamaji | 246cd23 | 2015-05-27 17:08:23 +0900 | [diff] [blame] | 242 | } |
Fumitoshi Ukai | 03e9ea6 | 2015-07-29 11:39:00 +0900 | [diff] [blame] | 243 | return buf.String() |
Shinichiro Hamaji | 246cd23 | 2015-05-27 17:08:23 +0900 | [diff] [blame] | 244 | } |
| 245 | |
Fumitoshi Ukai | 1713c6f | 2015-07-14 13:08:24 +0900 | [diff] [blame] | 246 | var ccRE = regexp.MustCompile(`^prebuilts/(gcc|clang)/.*(gcc|g\+\+|clang|clang\+\+) .* ?-c `) |
| 247 | |
| 248 | func gomaCmdForAndroidCompileCmd(cmd string) (string, bool) { |
| 249 | i := strings.Index(cmd, " ") |
| 250 | if i < 0 { |
| 251 | return cmd, false |
| 252 | } |
| 253 | driver := cmd[:i] |
| 254 | if strings.HasSuffix(driver, "ccache") { |
| 255 | return gomaCmdForAndroidCompileCmd(cmd[i+1:]) |
| 256 | } |
| 257 | return cmd, ccRE.MatchString(cmd) |
| 258 | } |
| 259 | |
Fumitoshi Ukai | b92f028 | 2015-07-21 14:05:51 +0900 | [diff] [blame] | 260 | func descriptionFromCmd(cmd string) (string, bool) { |
| 261 | if !strings.HasPrefix(cmd, "echo") || !isWhitespace(rune(cmd[4])) { |
| 262 | return "", false |
| 263 | } |
| 264 | echoarg := cmd[5:] |
| 265 | |
| 266 | // strip outer quotes, and fail if it is not a single echo command. |
| 267 | var buf bytes.Buffer |
| 268 | var escape bool |
| 269 | var quote rune |
| 270 | for _, c := range echoarg { |
| 271 | if escape { |
| 272 | escape = false |
| 273 | buf.WriteRune(c) |
| 274 | continue |
| 275 | } |
| 276 | if c == '\\' { |
| 277 | escape = true |
| 278 | buf.WriteRune(c) |
| 279 | continue |
| 280 | } |
| 281 | if quote != 0 { |
| 282 | if c == quote { |
| 283 | quote = 0 |
| 284 | continue |
| 285 | } |
| 286 | buf.WriteRune(c) |
| 287 | continue |
| 288 | } |
| 289 | switch c { |
| 290 | case '\'', '"', '`': |
| 291 | quote = c |
| 292 | case '<', '>', '&', '|', ';': |
| 293 | return "", false |
| 294 | default: |
| 295 | buf.WriteRune(c) |
| 296 | } |
| 297 | } |
| 298 | return buf.String(), true |
| 299 | } |
| 300 | |
| 301 | func (n *NinjaGenerator) genShellScript(runners []runner) (cmd string, desc string, useLocalPool bool) { |
| 302 | const defaultDesc = "build $out" |
| 303 | var useGomacc bool |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 304 | var buf bytes.Buffer |
Shinichiro Hamaji | a546779 | 2015-05-27 16:28:40 +0900 | [diff] [blame] | 305 | for i, r := range runners { |
| 306 | if i > 0 { |
| 307 | if runners[i-1].ignoreError { |
| 308 | buf.WriteString(" ; ") |
| 309 | } else { |
| 310 | buf.WriteString(" && ") |
| 311 | } |
| 312 | } |
Fumitoshi Ukai | 60f92c0 | 2015-07-29 10:09:36 +0900 | [diff] [blame] | 313 | cmd := trimTailingSlash(r.cmd) |
| 314 | cmd = stripShellComment(cmd) |
Shinichiro Hamaji | 246cd23 | 2015-05-27 17:08:23 +0900 | [diff] [blame] | 315 | cmd = trimLeftSpace(cmd) |
Fumitoshi Ukai | 5af212f | 2015-07-28 17:57:17 +0900 | [diff] [blame] | 316 | cmd = strings.Replace(cmd, "\\\n\t", "", -1) |
Fumitoshi Ukai | 7466359 | 2015-07-17 17:53:11 +0900 | [diff] [blame] | 317 | cmd = strings.Replace(cmd, "\\\n", "", -1) |
Shinichiro Hamaji | a546779 | 2015-05-27 16:28:40 +0900 | [diff] [blame] | 318 | cmd = strings.TrimRight(cmd, " \t\n;") |
Fumitoshi Ukai | 701b0e0 | 2015-07-28 14:06:06 +0900 | [diff] [blame] | 319 | cmd = escapeNinja(cmd) |
Shinichiro Hamaji | 501f68e | 2015-05-27 17:33:41 +0900 | [diff] [blame] | 320 | if cmd == "" { |
| 321 | cmd = "true" |
| 322 | } |
Fumitoshi Ukai | 5af212f | 2015-07-28 17:57:17 +0900 | [diff] [blame] | 323 | glog.V(2).Infof("cmd %q=>%q", r.cmd, cmd) |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 324 | if n.GomaDir != "" { |
Fumitoshi Ukai | 1713c6f | 2015-07-14 13:08:24 +0900 | [diff] [blame] | 325 | rcmd, ok := gomaCmdForAndroidCompileCmd(cmd) |
| 326 | if ok { |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 327 | cmd = fmt.Sprintf("%s/gomacc %s", n.GomaDir, rcmd) |
Fumitoshi Ukai | 1713c6f | 2015-07-14 13:08:24 +0900 | [diff] [blame] | 328 | useGomacc = true |
| 329 | } |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 330 | } |
Fumitoshi Ukai | b92f028 | 2015-07-21 14:05:51 +0900 | [diff] [blame] | 331 | if n.DetectAndroidEcho && desc == "" { |
| 332 | d, ok := descriptionFromCmd(cmd) |
| 333 | if ok { |
| 334 | desc = d |
| 335 | cmd = "true" |
| 336 | } |
| 337 | } |
Shinichiro Hamaji | 13653b2 | 2015-05-28 22:45:12 +0900 | [diff] [blame] | 338 | needsSubShell := i > 0 || len(runners) > 1 |
| 339 | if cmd[0] == '(' { |
| 340 | needsSubShell = false |
| 341 | } |
| 342 | |
| 343 | if needsSubShell { |
| 344 | buf.WriteByte('(') |
| 345 | } |
Shinichiro Hamaji | a546779 | 2015-05-27 16:28:40 +0900 | [diff] [blame] | 346 | buf.WriteString(cmd) |
| 347 | if i == len(runners)-1 && r.ignoreError { |
| 348 | buf.WriteString(" ; true") |
| 349 | } |
Shinichiro Hamaji | 13653b2 | 2015-05-28 22:45:12 +0900 | [diff] [blame] | 350 | if needsSubShell { |
| 351 | buf.WriteByte(')') |
| 352 | } |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 353 | } |
Fumitoshi Ukai | b92f028 | 2015-07-21 14:05:51 +0900 | [diff] [blame] | 354 | if desc == "" { |
| 355 | desc = defaultDesc |
| 356 | } |
| 357 | return buf.String(), desc, n.GomaDir != "" && !useGomacc |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 358 | } |
| 359 | |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 360 | func (n *NinjaGenerator) genRuleName() string { |
Fumitoshi Ukai | 936de10 | 2015-06-08 11:21:16 +0900 | [diff] [blame] | 361 | ruleName := fmt.Sprintf("rule%d", n.ruleID) |
| 362 | n.ruleID++ |
Shinichiro Hamaji | fd87216 | 2015-05-26 18:06:48 +0900 | [diff] [blame] | 363 | return ruleName |
| 364 | } |
| 365 | |
Fumitoshi Ukai | 3dae804 | 2015-07-22 13:49:23 +0900 | [diff] [blame] | 366 | func (n *NinjaGenerator) emitBuild(output, rule, inputs, orderOnlys string) { |
| 367 | fmt.Fprintf(n.f, "build %s: %s", escapeBuildTarget(output), rule) |
| 368 | if inputs != "" { |
| 369 | fmt.Fprintf(n.f, " %s", inputs) |
| 370 | } |
| 371 | if orderOnlys != "" { |
| 372 | fmt.Fprintf(n.f, " || %s", orderOnlys) |
| 373 | } |
Fumitoshi Ukai | 26e8381 | 2015-07-21 14:38:33 +0900 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | func escapeBuildTarget(s string) string { |
Fumitoshi Ukai | 48ac8ae | 2015-07-29 11:52:28 +0900 | [diff] [blame] | 377 | i := strings.IndexAny(s, "$: \\") |
Fumitoshi Ukai | 26e8381 | 2015-07-21 14:38:33 +0900 | [diff] [blame] | 378 | if i < 0 { |
| 379 | return s |
| 380 | } |
Fumitoshi Ukai | 48ac8ae | 2015-07-29 11:52:28 +0900 | [diff] [blame] | 381 | // unescapeInput only "\ ", "\=" unescape as " ", "=". |
| 382 | // TODO(ukai): which char should unescape, which should not here? |
| 383 | var esc rune |
Fumitoshi Ukai | 26e8381 | 2015-07-21 14:38:33 +0900 | [diff] [blame] | 384 | var buf bytes.Buffer |
| 385 | for _, c := range s { |
| 386 | switch c { |
Fumitoshi Ukai | 48ac8ae | 2015-07-29 11:52:28 +0900 | [diff] [blame] | 387 | case '\\': |
| 388 | esc = c |
| 389 | continue |
Fumitoshi Ukai | 26e8381 | 2015-07-21 14:38:33 +0900 | [diff] [blame] | 390 | case '$', ':', ' ': |
Fumitoshi Ukai | 48ac8ae | 2015-07-29 11:52:28 +0900 | [diff] [blame] | 391 | esc = 0 |
Fumitoshi Ukai | 26e8381 | 2015-07-21 14:38:33 +0900 | [diff] [blame] | 392 | buf.WriteByte('$') |
| 393 | } |
Fumitoshi Ukai | 48ac8ae | 2015-07-29 11:52:28 +0900 | [diff] [blame] | 394 | if esc != 0 { |
| 395 | buf.WriteRune(esc) |
| 396 | esc = 0 |
| 397 | } |
Fumitoshi Ukai | 26e8381 | 2015-07-21 14:38:33 +0900 | [diff] [blame] | 398 | buf.WriteRune(c) |
| 399 | } |
Fumitoshi Ukai | 48ac8ae | 2015-07-29 11:52:28 +0900 | [diff] [blame] | 400 | if esc != 0 { |
| 401 | buf.WriteRune(esc) |
| 402 | } |
Fumitoshi Ukai | 26e8381 | 2015-07-21 14:38:33 +0900 | [diff] [blame] | 403 | return buf.String() |
Shinichiro Hamaji | fd87216 | 2015-05-26 18:06:48 +0900 | [diff] [blame] | 404 | } |
| 405 | |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 406 | func (n *NinjaGenerator) dependency(node *DepNode) (string, string) { |
Shinichiro Hamaji | fd87216 | 2015-05-26 18:06:48 +0900 | [diff] [blame] | 407 | var deps []string |
Fumitoshi Ukai | 3dae804 | 2015-07-22 13:49:23 +0900 | [diff] [blame] | 408 | seen := make(map[string]bool) |
Shinichiro Hamaji | fd87216 | 2015-05-26 18:06:48 +0900 | [diff] [blame] | 409 | for _, d := range node.Deps { |
Fumitoshi Ukai | 3dae804 | 2015-07-22 13:49:23 +0900 | [diff] [blame] | 410 | t := escapeBuildTarget(d.Output) |
| 411 | if seen[t] { |
| 412 | continue |
| 413 | } |
| 414 | deps = append(deps, t) |
| 415 | seen[t] = true |
Fumitoshi Ukai | c916ea2 | 2015-06-30 09:52:13 +0900 | [diff] [blame] | 416 | } |
| 417 | var orderOnlys []string |
| 418 | for _, d := range node.OrderOnlys { |
Fumitoshi Ukai | 3dae804 | 2015-07-22 13:49:23 +0900 | [diff] [blame] | 419 | t := escapeBuildTarget(d.Output) |
| 420 | if seen[t] { |
| 421 | continue |
| 422 | } |
| 423 | orderOnlys = append(orderOnlys, t) |
| 424 | seen[t] = true |
Shinichiro Hamaji | fd87216 | 2015-05-26 18:06:48 +0900 | [diff] [blame] | 425 | } |
Fumitoshi Ukai | 3dae804 | 2015-07-22 13:49:23 +0900 | [diff] [blame] | 426 | return strings.Join(deps, " "), strings.Join(orderOnlys, " ") |
Shinichiro Hamaji | fd87216 | 2015-05-26 18:06:48 +0900 | [diff] [blame] | 427 | } |
| 428 | |
Fumitoshi Ukai | 701b0e0 | 2015-07-28 14:06:06 +0900 | [diff] [blame] | 429 | func escapeNinja(s string) string { |
| 430 | return strings.Replace(s, "$", "$$", -1) |
| 431 | } |
| 432 | |
Fumitoshi Ukai | cf6c6c2 | 2015-07-21 15:24:35 +0900 | [diff] [blame] | 433 | func escapeShell(s string) string { |
| 434 | i := strings.IndexAny(s, "$`!\\\"") |
| 435 | if i < 0 { |
| 436 | return s |
| 437 | } |
| 438 | var buf bytes.Buffer |
| 439 | var lastDollar bool |
| 440 | for _, c := range s { |
| 441 | switch c { |
| 442 | case '$': |
| 443 | if lastDollar { |
| 444 | buf.WriteRune(c) |
| 445 | lastDollar = false |
| 446 | continue |
| 447 | } |
| 448 | buf.WriteString(`\$`) |
| 449 | lastDollar = true |
| 450 | continue |
| 451 | case '`', '"', '!', '\\': |
| 452 | buf.WriteByte('\\') |
| 453 | } |
| 454 | buf.WriteRune(c) |
| 455 | lastDollar = false |
| 456 | } |
| 457 | return buf.String() |
| 458 | } |
| 459 | |
Fumitoshi Ukai | b6adb39 | 2015-07-28 16:46:11 +0900 | [diff] [blame] | 460 | func (n *NinjaGenerator) ninjaVars(s string, nv [][]string, esc func(string) string) string { |
| 461 | for _, v := range nv { |
| 462 | k, v := v[0], v[1] |
| 463 | if v == "" { |
| 464 | continue |
| 465 | } |
Fumitoshi Ukai | 53c880d | 2015-07-28 17:10:15 +0900 | [diff] [blame] | 466 | if strings.Contains(v, "/./") || strings.Contains(v, "/../") || strings.Contains(v, "$") { |
| 467 | // ninja will normalize paths (/./, /../), so keep it as is |
| 468 | // ninja will emit quoted string for $ |
Fumitoshi Ukai | b6adb39 | 2015-07-28 16:46:11 +0900 | [diff] [blame] | 469 | continue |
| 470 | } |
| 471 | if esc != nil { |
| 472 | v = esc(v) |
| 473 | } |
| 474 | s = strings.Replace(s, v, k, -1) |
| 475 | } |
| 476 | return s |
| 477 | } |
| 478 | |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 479 | func (n *NinjaGenerator) emitNode(node *DepNode) error { |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 480 | output := node.Output |
| 481 | if _, found := n.done[output]; found { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 482 | return nil |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 483 | } |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 484 | n.done[output] = nodeVisit |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 485 | |
Fumitoshi Ukai | c916ea2 | 2015-06-30 09:52:13 +0900 | [diff] [blame] | 486 | if len(node.Cmds) == 0 && len(node.Deps) == 0 && len(node.OrderOnlys) == 0 && !node.IsPhony { |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 487 | if _, ok := n.ctx.vpaths.exists(output); ok { |
| 488 | n.done[output] = nodeFile |
Fumitoshi Ukai | 3857a24 | 2015-07-17 17:15:12 +0900 | [diff] [blame] | 489 | return nil |
| 490 | } |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 491 | o := filepath.Clean(output) |
| 492 | if o != output { |
Fumitoshi Ukai | b816ffb | 2015-07-28 17:46:04 +0900 | [diff] [blame] | 493 | // if normalized target has been done, it marks as alias. |
| 494 | if s, found := n.done[o]; found { |
| 495 | glog.V(1).Infof("node %s=%s => %s=alias", o, s, node.Output) |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 496 | n.done[output] = nodeAlias |
Fumitoshi Ukai | b816ffb | 2015-07-28 17:46:04 +0900 | [diff] [blame] | 497 | return nil |
| 498 | } |
| 499 | } |
Fumitoshi Ukai | 786846d | 2015-07-28 18:08:47 +0900 | [diff] [blame] | 500 | if node.Filename == "" { |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 501 | n.done[output] = nodeMissing |
Fumitoshi Ukai | 786846d | 2015-07-28 18:08:47 +0900 | [diff] [blame] | 502 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 503 | return nil |
Shinichiro Hamaji | fd4aaa0 | 2015-05-26 15:40:53 +0900 | [diff] [blame] | 504 | } |
| 505 | |
Fumitoshi Ukai | a70b4ea | 2015-06-30 15:31:49 +0900 | [diff] [blame] | 506 | runners, _, err := createRunners(n.ctx, node) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 507 | if err != nil { |
| 508 | return err |
| 509 | } |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 510 | ruleName := "phony" |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 511 | useLocalPool := false |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 512 | inputs, orderOnlys := n.dependency(node) |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 513 | if len(runners) > 0 { |
| 514 | ruleName = n.genRuleName() |
Fumitoshi Ukai | 701b0e0 | 2015-07-28 14:06:06 +0900 | [diff] [blame] | 515 | fmt.Fprintf(n.f, "\n# rule for %q\n", node.Output) |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 516 | fmt.Fprintf(n.f, "rule %s\n", ruleName) |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 517 | |
Fumitoshi Ukai | b92f028 | 2015-07-21 14:05:51 +0900 | [diff] [blame] | 518 | ss, desc, ulp := n.genShellScript(runners) |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 519 | if ulp { |
| 520 | useLocalPool = true |
| 521 | } |
Fumitoshi Ukai | b92f028 | 2015-07-21 14:05:51 +0900 | [diff] [blame] | 522 | fmt.Fprintf(n.f, " description = %s\n", desc) |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 523 | cmdline, depfile, err := getDepfile(ss) |
Shinichiro Hamaji | 0ab9a2f | 2015-05-27 18:32:46 +0900 | [diff] [blame] | 524 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 525 | return err |
Shinichiro Hamaji | 0ab9a2f | 2015-05-27 18:32:46 +0900 | [diff] [blame] | 526 | } |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 527 | if depfile != "" { |
| 528 | fmt.Fprintf(n.f, " depfile = %s\n", depfile) |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 529 | fmt.Fprintf(n.f, " deps = gcc\n") |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 530 | } |
Fumitoshi Ukai | b6adb39 | 2015-07-28 16:46:11 +0900 | [diff] [blame] | 531 | nv := [][]string{ |
| 532 | []string{"${in}", inputs}, |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 533 | []string{"${out}", escapeNinja(output)}, |
Fumitoshi Ukai | b6adb39 | 2015-07-28 16:46:11 +0900 | [diff] [blame] | 534 | } |
Shinichiro Hamaji | a546779 | 2015-05-27 16:28:40 +0900 | [diff] [blame] | 535 | // It seems Linux is OK with ~130kB. |
| 536 | // TODO: Find this number automatically. |
| 537 | ArgLenLimit := 100 * 1000 |
Fumitoshi Ukai | cf6c6c2 | 2015-07-21 15:24:35 +0900 | [diff] [blame] | 538 | if len(cmdline) > ArgLenLimit { |
Shinichiro Hamaji | a546779 | 2015-05-27 16:28:40 +0900 | [diff] [blame] | 539 | fmt.Fprintf(n.f, " rspfile = $out.rsp\n") |
Fumitoshi Ukai | b6adb39 | 2015-07-28 16:46:11 +0900 | [diff] [blame] | 540 | cmdline = n.ninjaVars(cmdline, nv, nil) |
Fumitoshi Ukai | cf6c6c2 | 2015-07-21 15:24:35 +0900 | [diff] [blame] | 541 | fmt.Fprintf(n.f, " rspfile_content = %s\n", cmdline) |
| 542 | fmt.Fprintf(n.f, " command = %s $out.rsp\n", n.ctx.shell) |
| 543 | } else { |
Fumitoshi Ukai | 3dae804 | 2015-07-22 13:49:23 +0900 | [diff] [blame] | 544 | cmdline = escapeShell(cmdline) |
Fumitoshi Ukai | b6adb39 | 2015-07-28 16:46:11 +0900 | [diff] [blame] | 545 | cmdline = n.ninjaVars(cmdline, nv, escapeShell) |
Fumitoshi Ukai | 3dae804 | 2015-07-22 13:49:23 +0900 | [diff] [blame] | 546 | fmt.Fprintf(n.f, " command = %s -c \"%s\"\n", n.ctx.shell, cmdline) |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 547 | } |
| 548 | } |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 549 | n.emitBuild(output, ruleName, inputs, orderOnlys) |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 550 | if useLocalPool { |
| 551 | fmt.Fprintf(n.f, " pool = local_pool\n") |
| 552 | } |
Fumitoshi Ukai | c916ea2 | 2015-06-30 09:52:13 +0900 | [diff] [blame] | 553 | fmt.Fprintf(n.f, "\n") |
Fumitoshi Ukai | 8a1110d | 2015-07-29 13:18:40 +0900 | [diff] [blame] | 554 | n.done[output] = nodeBuild |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 555 | |
| 556 | for _, d := range node.Deps { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 557 | err := n.emitNode(d) |
| 558 | if err != nil { |
| 559 | return err |
| 560 | } |
Fumitoshi Ukai | b816ffb | 2015-07-28 17:46:04 +0900 | [diff] [blame] | 561 | glog.V(1).Infof("node %s dep node %q %s", node.Output, d.Output, n.done[d.Output]) |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 562 | } |
Fumitoshi Ukai | c916ea2 | 2015-06-30 09:52:13 +0900 | [diff] [blame] | 563 | for _, d := range node.OrderOnlys { |
| 564 | err := n.emitNode(d) |
| 565 | if err != nil { |
| 566 | return err |
| 567 | } |
Fumitoshi Ukai | b816ffb | 2015-07-28 17:46:04 +0900 | [diff] [blame] | 568 | glog.V(1).Infof("node %s order node %q %s", node.Output, d.Output, n.done[d.Output]) |
Fumitoshi Ukai | c916ea2 | 2015-06-30 09:52:13 +0900 | [diff] [blame] | 569 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 570 | return nil |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 571 | } |
| 572 | |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 573 | func (n *NinjaGenerator) emitRegenRules() error { |
| 574 | if len(n.Args) == 0 { |
| 575 | return nil |
| 576 | } |
| 577 | mkfiles, err := n.ctx.ev.EvaluateVar("MAKEFILE_LIST") |
| 578 | if err != nil { |
| 579 | return err |
| 580 | } |
| 581 | fmt.Fprintf(n.f, ` |
| 582 | rule regen_ninja |
| 583 | description = Regenerate ninja files due to dependency |
| 584 | generator=1 |
| 585 | command=%s |
| 586 | `, strings.Join(n.Args, " ")) |
| 587 | fmt.Fprintf(n.f, "build %s: regen_ninja %s", n.ninjaName(), mkfiles) |
| 588 | // TODO: Add dependencies to directories read by $(wildcard) or |
| 589 | // $(shell find). |
| 590 | if len(usedEnvs) > 0 { |
| 591 | fmt.Fprintf(n.f, " %s", n.envlistName()) |
| 592 | } |
| 593 | fmt.Fprintf(n.f, "\n\n") |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 594 | return nil |
Fumitoshi Ukai | 861c9f2 | 2015-07-22 10:54:01 +0900 | [diff] [blame] | 595 | } |
| 596 | |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 597 | func (n *NinjaGenerator) shName() string { |
| 598 | return fmt.Sprintf("ninja%s.sh", n.Suffix) |
Fumitoshi Ukai | 861c9f2 | 2015-07-22 10:54:01 +0900 | [diff] [blame] | 599 | } |
| 600 | |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 601 | func (n *NinjaGenerator) ninjaName() string { |
| 602 | return fmt.Sprintf("build%s.ninja", n.Suffix) |
| 603 | } |
| 604 | |
| 605 | func (n *NinjaGenerator) envlistName() string { |
| 606 | return fmt.Sprintf(".kati_env%s", n.Suffix) |
| 607 | } |
| 608 | |
| 609 | func (n *NinjaGenerator) generateEnvlist() (err error) { |
| 610 | f, err := os.Create(n.envlistName()) |
| 611 | if err != nil { |
| 612 | return err |
| 613 | } |
| 614 | defer func() { |
| 615 | cerr := f.Close() |
| 616 | if err == nil { |
| 617 | err = cerr |
| 618 | } |
| 619 | }() |
| 620 | for k := range usedEnvs { |
| 621 | v, err := n.ctx.ev.EvaluateVar(k) |
| 622 | if err != nil { |
| 623 | return err |
| 624 | } |
Fumitoshi Ukai | d557026 | 2015-07-28 12:44:31 +0900 | [diff] [blame] | 625 | fmt.Fprintf(f, "%q=%q\n", k, v) |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 626 | } |
| 627 | return nil |
| 628 | } |
| 629 | |
| 630 | func (n *NinjaGenerator) generateShell() (err error) { |
| 631 | f, err := os.Create(n.shName()) |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 632 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 633 | return err |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 634 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 635 | defer func() { |
| 636 | cerr := f.Close() |
| 637 | if err == nil { |
| 638 | err = cerr |
| 639 | } |
| 640 | }() |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 641 | |
Fumitoshi Ukai | be9104d | 2015-07-28 14:20:43 +0900 | [diff] [blame] | 642 | fmt.Fprintf(f, "#!/bin/bash\n") |
Fumitoshi Ukai | 08db53a | 2015-07-17 14:45:57 +0900 | [diff] [blame] | 643 | fmt.Fprintf(f, "# Generated by kati %s\n", gitVersion) |
| 644 | fmt.Fprintln(f) |
| 645 | fmt.Fprintln(f, `cd $(dirname "$0")`) |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 646 | if n.Suffix != "" { |
| 647 | fmt.Fprintf(f, "if [ -f %s ]; then\n export $(cat %s)\nfi\n", n.envlistName(), n.envlistName()) |
| 648 | } |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 649 | for name, export := range n.exports { |
Fumitoshi Ukai | f90f73c | 2015-07-28 14:37:02 +0900 | [diff] [blame] | 650 | // export "a b"=c will error on bash |
| 651 | // bash: export `a b=c': not a valid identifier |
| 652 | if strings.ContainsAny(name, " \t\n\r") { |
| 653 | glog.V(1).Infof("ignore export %q (export:%t)", name, export) |
| 654 | continue |
| 655 | } |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 656 | if export { |
Fumitoshi Ukai | a70b4ea | 2015-06-30 15:31:49 +0900 | [diff] [blame] | 657 | v, err := n.ctx.ev.EvaluateVar(name) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 658 | if err != nil { |
| 659 | return err |
| 660 | } |
Fumitoshi Ukai | d557026 | 2015-07-28 12:44:31 +0900 | [diff] [blame] | 661 | fmt.Fprintf(f, "export %q=%q\n", name, v) |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 662 | } else { |
Fumitoshi Ukai | d557026 | 2015-07-28 12:44:31 +0900 | [diff] [blame] | 663 | fmt.Fprintf(f, "unset %q\n", name) |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 664 | } |
| 665 | } |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 666 | if n.GomaDir == "" { |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 667 | fmt.Fprintf(f, `exec ninja -f %s "$@"`+"\n", n.ninjaName()) |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 668 | } else { |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 669 | fmt.Fprintf(f, `exec ninja -f %s -j500 "$@"`+"\n", n.ninjaName()) |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 670 | } |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 671 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 672 | return f.Chmod(0755) |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 673 | } |
| 674 | |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 675 | func (n *NinjaGenerator) generateNinja(defaultTarget string) (err error) { |
| 676 | f, err := os.Create(n.ninjaName()) |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 677 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 678 | return err |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 679 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 680 | defer func() { |
| 681 | cerr := f.Close() |
| 682 | if err == nil { |
| 683 | err = cerr |
| 684 | } |
| 685 | }() |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 686 | |
| 687 | n.f = f |
Fumitoshi Ukai | 9dd48a9 | 2015-07-14 13:39:03 +0900 | [diff] [blame] | 688 | fmt.Fprintf(n.f, "# Generated by kati %s\n", gitVersion) |
Shinichiro Hamaji | ea2e0de | 2015-05-28 15:22:16 +0900 | [diff] [blame] | 689 | fmt.Fprintf(n.f, "\n") |
| 690 | |
Fumitoshi Ukai | b5612a8 | 2015-07-14 13:22:17 +0900 | [diff] [blame] | 691 | if len(usedEnvs) > 0 { |
| 692 | fmt.Fprintln(n.f, "# Environment variables used:") |
| 693 | var names []string |
| 694 | for name := range usedEnvs { |
| 695 | names = append(names, name) |
| 696 | } |
| 697 | sort.Strings(names) |
| 698 | for _, name := range names { |
| 699 | v, err := n.ctx.ev.EvaluateVar(name) |
| 700 | if err != nil { |
| 701 | return err |
| 702 | } |
Fumitoshi Ukai | d557026 | 2015-07-28 12:44:31 +0900 | [diff] [blame] | 703 | fmt.Fprintf(n.f, "# %q=%q\n", name, v) |
Fumitoshi Ukai | b5612a8 | 2015-07-14 13:22:17 +0900 | [diff] [blame] | 704 | } |
| 705 | fmt.Fprintf(n.f, "\n") |
| 706 | } |
| 707 | |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 708 | if n.GomaDir != "" { |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 709 | fmt.Fprintf(n.f, "pool local_pool\n") |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 710 | fmt.Fprintf(n.f, " depth = %d\n\n", runtime.NumCPU()) |
| 711 | } |
| 712 | |
| 713 | err = n.emitRegenRules() |
| 714 | if err != nil { |
| 715 | return err |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 716 | } |
| 717 | |
Fumitoshi Ukai | 3dae804 | 2015-07-22 13:49:23 +0900 | [diff] [blame] | 718 | // defining $out for $@ and $in for $^ here doesn't work well, |
| 719 | // because these texts will be processed in escapeShell... |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 720 | for _, node := range n.nodes { |
| 721 | err := n.emitNode(node) |
| 722 | if err != nil { |
| 723 | return err |
| 724 | } |
Fumitoshi Ukai | b816ffb | 2015-07-28 17:46:04 +0900 | [diff] [blame] | 725 | glog.V(1).Infof("node %q %s", node.Output, n.done[node.Output]) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 726 | } |
Fumitoshi Ukai | e08e9ee | 2015-07-13 13:14:41 +0900 | [diff] [blame] | 727 | |
Fumitoshi Ukai | b816ffb | 2015-07-28 17:46:04 +0900 | [diff] [blame] | 728 | // emit phony targets for visited nodes that are |
| 729 | // - not existing file |
| 730 | // - not alias for other targets. |
| 731 | var nodes []string |
| 732 | for node, state := range n.done { |
| 733 | if state != nodeVisit { |
| 734 | continue |
| 735 | } |
| 736 | nodes = append(nodes, node) |
| 737 | } |
| 738 | if len(nodes) > 0 { |
| 739 | fmt.Fprintln(n.f) |
| 740 | sort.Strings(nodes) |
| 741 | for _, node := range nodes { |
| 742 | n.emitBuild(node, "phony", "", "") |
| 743 | fmt.Fprintln(n.f) |
| 744 | n.done[node] = nodeBuild |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | // emit default if the target was emitted. |
| 749 | if defaultTarget != "" && n.done[defaultTarget] == nodeBuild { |
Fumitoshi Ukai | 701b0e0 | 2015-07-28 14:06:06 +0900 | [diff] [blame] | 750 | fmt.Fprintf(n.f, "\ndefault %s\n", escapeNinja(defaultTarget)) |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 751 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 752 | return nil |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 753 | } |
| 754 | |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 755 | // Save generates build.ninja from DepGraph. |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 756 | func (n *NinjaGenerator) Save(g *DepGraph, name string, targets []string) error { |
Fumitoshi Ukai | 49599e5 | 2015-06-26 10:10:24 +0900 | [diff] [blame] | 757 | startTime := time.Now() |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 758 | n.init(g) |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 759 | err := n.generateEnvlist() |
| 760 | if err != nil { |
| 761 | return err |
| 762 | } |
| 763 | err = n.generateShell() |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 764 | if err != nil { |
| 765 | return err |
| 766 | } |
Fumitoshi Ukai | b79b290 | 2015-07-16 16:40:09 +0900 | [diff] [blame] | 767 | var defaultTarget string |
| 768 | if len(targets) == 0 && len(g.nodes) > 0 { |
| 769 | defaultTarget = g.nodes[0].Output |
| 770 | } |
Fumitoshi Ukai | d4a016e | 2015-07-27 16:50:00 +0900 | [diff] [blame] | 771 | err = n.generateNinja(defaultTarget) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 772 | if err != nil { |
| 773 | return err |
| 774 | } |
Fumitoshi Ukai | 49599e5 | 2015-06-26 10:10:24 +0900 | [diff] [blame] | 775 | logStats("generate ninja time: %q", time.Since(startTime)) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 776 | return nil |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 777 | } |