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" |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
Fumitoshi Ukai | 3ec25b5 | 2015-06-25 15:39:35 +0900 | [diff] [blame] | 29 | type ninjaGenerator struct { |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 30 | f *os.File |
| 31 | nodes []*DepNode |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 32 | exports map[string]bool |
Fumitoshi Ukai | a70b4ea | 2015-06-30 15:31:49 +0900 | [diff] [blame] | 33 | |
| 34 | ctx *execContext |
| 35 | |
Fumitoshi Ukai | e08e9ee | 2015-07-13 13:14:41 +0900 | [diff] [blame] | 36 | ruleID int |
| 37 | done map[string]bool |
| 38 | shortNames map[string][]string |
| 39 | gomaDir string |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 40 | } |
| 41 | |
Fumitoshi Ukai | 3ec25b5 | 2015-06-25 15:39:35 +0900 | [diff] [blame] | 42 | func newNinjaGenerator(g *DepGraph, gomaDir string) *ninjaGenerator { |
Fumitoshi Ukai | a70b4ea | 2015-06-30 15:31:49 +0900 | [diff] [blame] | 43 | ctx := newExecContext(g.vars, true) |
Fumitoshi Ukai | 3ec25b5 | 2015-06-25 15:39:35 +0900 | [diff] [blame] | 44 | return &ninjaGenerator{ |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 45 | nodes: g.nodes, |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 46 | exports: g.exports, |
Fumitoshi Ukai | a70b4ea | 2015-06-30 15:31:49 +0900 | [diff] [blame] | 47 | ctx: ctx, |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 48 | done: make(map[string]bool), |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 49 | gomaDir: gomaDir, |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 53 | func getDepfileImpl(ss string) (string, error) { |
Shinichiro Hamaji | 6dca220 | 2015-05-27 18:00:34 +0900 | [diff] [blame] | 54 | tss := ss + " " |
| 55 | if !strings.Contains(tss, " -MD ") && !strings.Contains(tss, " -MMD ") { |
| 56 | return "", nil |
| 57 | } |
| 58 | |
| 59 | mfIndex := strings.Index(ss, " -MF ") |
| 60 | if mfIndex >= 0 { |
| 61 | mf := trimLeftSpace(ss[mfIndex+4:]) |
| 62 | if strings.Index(mf, " -MF ") >= 0 { |
| 63 | return "", fmt.Errorf("Multiple output file candidates in %s", ss) |
| 64 | } |
| 65 | mfEndIndex := strings.IndexAny(mf, " \t\n") |
| 66 | if mfEndIndex >= 0 { |
| 67 | mf = mf[:mfEndIndex] |
| 68 | } |
| 69 | |
Shinichiro Hamaji | 6dca220 | 2015-05-27 18:00:34 +0900 | [diff] [blame] | 70 | return mf, nil |
| 71 | } |
| 72 | |
| 73 | outIndex := strings.Index(ss, " -o ") |
| 74 | if outIndex < 0 { |
| 75 | return "", fmt.Errorf("Cannot find the depfile in %s", ss) |
| 76 | } |
| 77 | out := trimLeftSpace(ss[outIndex+4:]) |
| 78 | if strings.Index(out, " -o ") >= 0 { |
| 79 | return "", fmt.Errorf("Multiple output file candidates in %s", ss) |
| 80 | } |
| 81 | outEndIndex := strings.IndexAny(out, " \t\n") |
| 82 | if outEndIndex >= 0 { |
| 83 | out = out[:outEndIndex] |
| 84 | } |
| 85 | return stripExt(out) + ".d", nil |
| 86 | } |
| 87 | |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 88 | // getDepfile gets depfile from cmdline, and returns cmdline and depfile. |
| 89 | func getDepfile(cmdline string) (string, string, error) { |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 90 | // 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] | 91 | if strings.Contains(cmdline, "bin/llvm-rs-cc ") { |
| 92 | return cmdline, "", nil |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 93 | } |
| 94 | |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 95 | depfile, err := getDepfileImpl(cmdline) |
| 96 | if depfile == "" || err != nil { |
| 97 | return cmdline, depfile, err |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 98 | } |
| 99 | |
Shinichiro Hamaji | 3438ae3 | 2015-06-05 11:33:20 +0900 | [diff] [blame] | 100 | // A hack for Makefiles generated by automake. |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 101 | mvCmd := "(mv -f " + depfile + " " |
| 102 | if i := strings.LastIndex(cmdline, mvCmd); i >= 0 { |
| 103 | rest := cmdline[i+len(mvCmd):] |
Shinichiro Hamaji | 3438ae3 | 2015-06-05 11:33:20 +0900 | [diff] [blame] | 104 | ei := strings.IndexByte(rest, ')') |
| 105 | if ei < 0 { |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 106 | return cmdline, "", fmt.Errorf("unbalanced parenthes? %s", cmdline) |
Shinichiro Hamaji | 3438ae3 | 2015-06-05 11:33:20 +0900 | [diff] [blame] | 107 | } |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 108 | cmdline = cmdline[:i] + "(cp -f " + depfile + " " + rest |
| 109 | return cmdline, depfile, nil |
Shinichiro Hamaji | 3438ae3 | 2015-06-05 11:33:20 +0900 | [diff] [blame] | 110 | } |
| 111 | |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 112 | // A hack for Android to get .P files instead of .d. |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 113 | p := stripExt(depfile) + ".P" |
| 114 | if strings.Contains(cmdline, p) { |
| 115 | rmfCmd := "; rm -f " + depfile |
| 116 | ncmdline := strings.Replace(cmdline, rmfCmd, "", 1) |
| 117 | if ncmdline == cmdline { |
| 118 | return cmdline, "", fmt.Errorf("cannot find removal of .d file: %s", cmdline) |
| 119 | } |
| 120 | return ncmdline, p, nil |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // A hack for Android. For .s files, GCC does not use |
| 124 | // C preprocessor, so it ignores -MF flag. |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 125 | as := "/" + stripExt(filepath.Base(depfile)) + ".s" |
| 126 | if strings.Contains(cmdline, as) { |
| 127 | return cmdline, "", nil |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 128 | } |
| 129 | |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 130 | cmdline += fmt.Sprintf(" && cp %s %s.tmp", depfile, depfile) |
| 131 | depfile += ".tmp" |
| 132 | return cmdline, depfile, nil |
Shinichiro Hamaji | 74ab4b6 | 2015-05-28 22:56:24 +0900 | [diff] [blame] | 133 | } |
| 134 | |
Shinichiro Hamaji | 246cd23 | 2015-05-27 17:08:23 +0900 | [diff] [blame] | 135 | func stripShellComment(s string) string { |
| 136 | if strings.IndexByte(s, '#') < 0 { |
| 137 | // Fast path. |
| 138 | return s |
| 139 | } |
| 140 | var escape bool |
| 141 | var quote rune |
| 142 | for i, c := range s { |
| 143 | if quote > 0 { |
| 144 | if quote == c && (quote == '\'' || !escape) { |
| 145 | quote = 0 |
| 146 | } |
| 147 | } else if !escape { |
| 148 | if c == '#' { |
| 149 | return s[:i] |
| 150 | } else if c == '\'' || c == '"' || c == '`' { |
| 151 | quote = c |
| 152 | } |
| 153 | } |
| 154 | if escape { |
| 155 | escape = false |
| 156 | } else if c == '\\' { |
| 157 | escape = true |
| 158 | } else { |
| 159 | escape = false |
| 160 | } |
| 161 | } |
| 162 | return s |
| 163 | } |
| 164 | |
Fumitoshi Ukai | 1713c6f | 2015-07-14 13:08:24 +0900 | [diff] [blame^] | 165 | var ccRE = regexp.MustCompile(`^prebuilts/(gcc|clang)/.*(gcc|g\+\+|clang|clang\+\+) .* ?-c `) |
| 166 | |
| 167 | func gomaCmdForAndroidCompileCmd(cmd string) (string, bool) { |
| 168 | i := strings.Index(cmd, " ") |
| 169 | if i < 0 { |
| 170 | return cmd, false |
| 171 | } |
| 172 | driver := cmd[:i] |
| 173 | if strings.HasSuffix(driver, "ccache") { |
| 174 | return gomaCmdForAndroidCompileCmd(cmd[i+1:]) |
| 175 | } |
| 176 | return cmd, ccRE.MatchString(cmd) |
| 177 | } |
| 178 | |
Fumitoshi Ukai | 3ec25b5 | 2015-06-25 15:39:35 +0900 | [diff] [blame] | 179 | func (n *ninjaGenerator) genShellScript(runners []runner) (string, bool) { |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 180 | useGomacc := false |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 181 | var buf bytes.Buffer |
Shinichiro Hamaji | a546779 | 2015-05-27 16:28:40 +0900 | [diff] [blame] | 182 | for i, r := range runners { |
| 183 | if i > 0 { |
| 184 | if runners[i-1].ignoreError { |
| 185 | buf.WriteString(" ; ") |
| 186 | } else { |
| 187 | buf.WriteString(" && ") |
| 188 | } |
| 189 | } |
Shinichiro Hamaji | 246cd23 | 2015-05-27 17:08:23 +0900 | [diff] [blame] | 190 | cmd := stripShellComment(r.cmd) |
| 191 | cmd = trimLeftSpace(cmd) |
Shinichiro Hamaji | a546779 | 2015-05-27 16:28:40 +0900 | [diff] [blame] | 192 | cmd = strings.Replace(cmd, "\\\n", " ", -1) |
| 193 | cmd = strings.TrimRight(cmd, " \t\n;") |
| 194 | cmd = strings.Replace(cmd, "$", "$$", -1) |
| 195 | cmd = strings.Replace(cmd, "\t", " ", -1) |
Shinichiro Hamaji | 501f68e | 2015-05-27 17:33:41 +0900 | [diff] [blame] | 196 | if cmd == "" { |
| 197 | cmd = "true" |
| 198 | } |
Fumitoshi Ukai | 1713c6f | 2015-07-14 13:08:24 +0900 | [diff] [blame^] | 199 | if n.gomaDir != "" { |
| 200 | rcmd, ok := gomaCmdForAndroidCompileCmd(cmd) |
| 201 | if ok { |
| 202 | cmd = fmt.Sprintf("%s/gomacc %s", n.gomaDir, rcmd) |
| 203 | useGomacc = true |
| 204 | } |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 205 | } |
Shinichiro Hamaji | 13653b2 | 2015-05-28 22:45:12 +0900 | [diff] [blame] | 206 | |
| 207 | needsSubShell := i > 0 || len(runners) > 1 |
| 208 | if cmd[0] == '(' { |
| 209 | needsSubShell = false |
| 210 | } |
| 211 | |
| 212 | if needsSubShell { |
| 213 | buf.WriteByte('(') |
| 214 | } |
Shinichiro Hamaji | a546779 | 2015-05-27 16:28:40 +0900 | [diff] [blame] | 215 | buf.WriteString(cmd) |
| 216 | if i == len(runners)-1 && r.ignoreError { |
| 217 | buf.WriteString(" ; true") |
| 218 | } |
Shinichiro Hamaji | 13653b2 | 2015-05-28 22:45:12 +0900 | [diff] [blame] | 219 | if needsSubShell { |
| 220 | buf.WriteByte(')') |
| 221 | } |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 222 | } |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 223 | return buf.String(), n.gomaDir != "" && !useGomacc |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 224 | } |
| 225 | |
Fumitoshi Ukai | 3ec25b5 | 2015-06-25 15:39:35 +0900 | [diff] [blame] | 226 | func (n *ninjaGenerator) genRuleName() string { |
Fumitoshi Ukai | 936de10 | 2015-06-08 11:21:16 +0900 | [diff] [blame] | 227 | ruleName := fmt.Sprintf("rule%d", n.ruleID) |
| 228 | n.ruleID++ |
Shinichiro Hamaji | fd87216 | 2015-05-26 18:06:48 +0900 | [diff] [blame] | 229 | return ruleName |
| 230 | } |
| 231 | |
Fumitoshi Ukai | 3ec25b5 | 2015-06-25 15:39:35 +0900 | [diff] [blame] | 232 | func (n *ninjaGenerator) emitBuild(output, rule, dep string) { |
Shinichiro Hamaji | ea2e0de | 2015-05-28 15:22:16 +0900 | [diff] [blame] | 233 | fmt.Fprintf(n.f, "build %s: %s%s\n", output, rule, dep) |
Shinichiro Hamaji | fd87216 | 2015-05-26 18:06:48 +0900 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | func getDepString(node *DepNode) string { |
| 237 | var deps []string |
Shinichiro Hamaji | fd87216 | 2015-05-26 18:06:48 +0900 | [diff] [blame] | 238 | for _, d := range node.Deps { |
Fumitoshi Ukai | c916ea2 | 2015-06-30 09:52:13 +0900 | [diff] [blame] | 239 | deps = append(deps, d.Output) |
| 240 | } |
| 241 | var orderOnlys []string |
| 242 | for _, d := range node.OrderOnlys { |
| 243 | orderOnlys = append(orderOnlys, d.Output) |
Shinichiro Hamaji | fd87216 | 2015-05-26 18:06:48 +0900 | [diff] [blame] | 244 | } |
| 245 | dep := "" |
| 246 | if len(deps) > 0 { |
| 247 | dep += fmt.Sprintf(" %s", strings.Join(deps, " ")) |
| 248 | } |
| 249 | if len(orderOnlys) > 0 { |
| 250 | dep += fmt.Sprintf(" || %s", strings.Join(orderOnlys, " ")) |
| 251 | } |
| 252 | return dep |
| 253 | } |
| 254 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 255 | func (n *ninjaGenerator) emitNode(node *DepNode) error { |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 256 | if n.done[node.Output] { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 257 | return nil |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 258 | } |
| 259 | n.done[node.Output] = true |
| 260 | |
Fumitoshi Ukai | c916ea2 | 2015-06-30 09:52:13 +0900 | [diff] [blame] | 261 | if len(node.Cmds) == 0 && len(node.Deps) == 0 && len(node.OrderOnlys) == 0 && !node.IsPhony { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 262 | return nil |
Shinichiro Hamaji | fd4aaa0 | 2015-05-26 15:40:53 +0900 | [diff] [blame] | 263 | } |
| 264 | |
Fumitoshi Ukai | e08e9ee | 2015-07-13 13:14:41 +0900 | [diff] [blame] | 265 | base := filepath.Base(node.Output) |
| 266 | if base != node.Output { |
| 267 | n.shortNames[base] = append(n.shortNames[base], node.Output) |
| 268 | } |
| 269 | |
Fumitoshi Ukai | a70b4ea | 2015-06-30 15:31:49 +0900 | [diff] [blame] | 270 | runners, _, err := createRunners(n.ctx, node) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 271 | if err != nil { |
| 272 | return err |
| 273 | } |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 274 | ruleName := "phony" |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 275 | useLocalPool := false |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 276 | if len(runners) > 0 { |
| 277 | ruleName = n.genRuleName() |
Fumitoshi Ukai | c916ea2 | 2015-06-30 09:52:13 +0900 | [diff] [blame] | 278 | fmt.Fprintf(n.f, "\n# rule for %s\n", node.Output) |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 279 | fmt.Fprintf(n.f, "rule %s\n", ruleName) |
| 280 | fmt.Fprintf(n.f, " description = build $out\n") |
| 281 | |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 282 | ss, ulp := n.genShellScript(runners) |
| 283 | if ulp { |
| 284 | useLocalPool = true |
| 285 | } |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 286 | cmdline, depfile, err := getDepfile(ss) |
Shinichiro Hamaji | 0ab9a2f | 2015-05-27 18:32:46 +0900 | [diff] [blame] | 287 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 288 | return err |
Shinichiro Hamaji | 0ab9a2f | 2015-05-27 18:32:46 +0900 | [diff] [blame] | 289 | } |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 290 | if depfile != "" { |
| 291 | fmt.Fprintf(n.f, " depfile = %s\n", depfile) |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 292 | fmt.Fprintf(n.f, " deps = gcc\n") |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 293 | } |
Shinichiro Hamaji | a546779 | 2015-05-27 16:28:40 +0900 | [diff] [blame] | 294 | // It seems Linux is OK with ~130kB. |
| 295 | // TODO: Find this number automatically. |
| 296 | ArgLenLimit := 100 * 1000 |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 297 | if len(ss) > ArgLenLimit { |
Shinichiro Hamaji | a546779 | 2015-05-27 16:28:40 +0900 | [diff] [blame] | 298 | fmt.Fprintf(n.f, " rspfile = $out.rsp\n") |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 299 | fmt.Fprintf(n.f, " rspfile_content = %s\n", ss) |
| 300 | ss = "sh $out.rsp" |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 301 | } |
Fumitoshi Ukai | 923b128 | 2015-07-13 12:53:30 +0900 | [diff] [blame] | 302 | fmt.Fprintf(n.f, " command = %s\n", cmdline) |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 303 | |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 304 | } |
Shinichiro Hamaji | bd59ebb | 2015-05-28 17:07:17 +0900 | [diff] [blame] | 305 | n.emitBuild(node.Output, ruleName, getDepString(node)) |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 306 | if useLocalPool { |
| 307 | fmt.Fprintf(n.f, " pool = local_pool\n") |
| 308 | } |
Fumitoshi Ukai | c916ea2 | 2015-06-30 09:52:13 +0900 | [diff] [blame] | 309 | fmt.Fprintf(n.f, "\n") |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 310 | |
| 311 | for _, d := range node.Deps { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 312 | err := n.emitNode(d) |
| 313 | if err != nil { |
| 314 | return err |
| 315 | } |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 316 | } |
Fumitoshi Ukai | c916ea2 | 2015-06-30 09:52:13 +0900 | [diff] [blame] | 317 | for _, d := range node.OrderOnlys { |
| 318 | err := n.emitNode(d) |
| 319 | if err != nil { |
| 320 | return err |
| 321 | } |
| 322 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 323 | return nil |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 324 | } |
| 325 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 326 | func (n *ninjaGenerator) generateShell() (err error) { |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 327 | f, err := os.Create("ninja.sh") |
| 328 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 329 | return err |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 330 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 331 | defer func() { |
| 332 | cerr := f.Close() |
| 333 | if err == nil { |
| 334 | err = cerr |
| 335 | } |
| 336 | }() |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 337 | |
Fumitoshi Ukai | a70b4ea | 2015-06-30 15:31:49 +0900 | [diff] [blame] | 338 | fmt.Fprintf(f, "#!%s\n", n.ctx.shell) |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 339 | for name, export := range n.exports { |
| 340 | if export { |
Fumitoshi Ukai | a70b4ea | 2015-06-30 15:31:49 +0900 | [diff] [blame] | 341 | v, err := n.ctx.ev.EvaluateVar(name) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 342 | if err != nil { |
| 343 | return err |
| 344 | } |
| 345 | fmt.Fprintf(f, "export %s=%s\n", name, v) |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 346 | } else { |
| 347 | fmt.Fprintf(f, "unset %s\n", name) |
| 348 | } |
| 349 | } |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 350 | if n.gomaDir == "" { |
Shinichiro Hamaji | 096c7e0 | 2015-06-04 17:13:26 +0900 | [diff] [blame] | 351 | fmt.Fprintln(f, `exec ninja "$@"`) |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 352 | } else { |
Shinichiro Hamaji | 096c7e0 | 2015-06-04 17:13:26 +0900 | [diff] [blame] | 353 | fmt.Fprintln(f, `exec ninja -j300 "$@"`) |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 354 | } |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 355 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 356 | return f.Chmod(0755) |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 357 | } |
| 358 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 359 | func (n *ninjaGenerator) generateNinja() (err error) { |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 360 | f, err := os.Create("build.ninja") |
| 361 | if err != nil { |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 362 | return err |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 363 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 364 | defer func() { |
| 365 | cerr := f.Close() |
| 366 | if err == nil { |
| 367 | err = cerr |
| 368 | } |
| 369 | }() |
Shinichiro Hamaji | 30a02ad | 2015-05-29 14:56:43 +0900 | [diff] [blame] | 370 | |
| 371 | n.f = f |
Shinichiro Hamaji | ea2e0de | 2015-05-28 15:22:16 +0900 | [diff] [blame] | 372 | fmt.Fprintf(n.f, "# Generated by kati\n") |
| 373 | fmt.Fprintf(n.f, "\n") |
| 374 | |
Fumitoshi Ukai | 744bb2b | 2015-06-25 00:10:52 +0900 | [diff] [blame] | 375 | if n.gomaDir != "" { |
Shinichiro Hamaji | bcecb50 | 2015-06-04 16:20:25 +0900 | [diff] [blame] | 376 | fmt.Fprintf(n.f, "pool local_pool\n") |
| 377 | fmt.Fprintf(n.f, " depth = %d\n", runtime.NumCPU()) |
| 378 | } |
| 379 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 380 | for _, node := range n.nodes { |
| 381 | err := n.emitNode(node) |
| 382 | if err != nil { |
| 383 | return err |
| 384 | } |
| 385 | } |
Fumitoshi Ukai | e08e9ee | 2015-07-13 13:14:41 +0900 | [diff] [blame] | 386 | |
| 387 | fmt.Fprintf(n.f, "\n# shortcuts:\n") |
| 388 | var names []string |
| 389 | for name := range n.shortNames { |
| 390 | names = append(names, name) |
| 391 | } |
| 392 | sort.Strings(names) |
| 393 | for _, name := range names { |
| 394 | if len(n.shortNames[name]) != 1 { |
| 395 | // we generate shortcuts only for targets whose basename are unique. |
| 396 | continue |
| 397 | } |
| 398 | fmt.Fprintf(n.f, "build %s: phony %s\n", name, n.shortNames[name][0]) |
| 399 | } |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 400 | return nil |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 401 | } |
| 402 | |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 403 | // GenerateNinja generates build.ninja from DepGraph. |
| 404 | func GenerateNinja(g *DepGraph, gomaDir string) error { |
Fumitoshi Ukai | 49599e5 | 2015-06-26 10:10:24 +0900 | [diff] [blame] | 405 | startTime := time.Now() |
Fumitoshi Ukai | 3ec25b5 | 2015-06-25 15:39:35 +0900 | [diff] [blame] | 406 | n := newNinjaGenerator(g, gomaDir) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 407 | err := n.generateShell() |
| 408 | if err != nil { |
| 409 | return err |
| 410 | } |
| 411 | err = n.generateNinja() |
| 412 | if err != nil { |
| 413 | return err |
| 414 | } |
Fumitoshi Ukai | 49599e5 | 2015-06-26 10:10:24 +0900 | [diff] [blame] | 415 | logStats("generate ninja time: %q", time.Since(startTime)) |
Fumitoshi Ukai | 65c7233 | 2015-06-26 21:32:50 +0900 | [diff] [blame] | 416 | return nil |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 417 | } |