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