Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 1 | package main |
| 2 | |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 3 | import ( |
Fumitoshi Ukai | 89215d0 | 2015-04-30 13:09:44 +0900 | [diff] [blame] | 4 | "bytes" |
Shinichiro Hamaji | 4220be3 | 2015-05-26 13:30:18 +0900 | [diff] [blame] | 5 | "crypto/sha1" |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 6 | "flag" |
| 7 | "fmt" |
| 8 | "os" |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 9 | "path/filepath" |
Fumitoshi Ukai | 89215d0 | 2015-04-30 13:09:44 +0900 | [diff] [blame] | 10 | "runtime" |
Fumitoshi Ukai | 5d85a70 | 2015-04-08 22:44:36 +0900 | [diff] [blame] | 11 | "runtime/pprof" |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 12 | "strings" |
Fumitoshi Ukai | 89215d0 | 2015-04-30 13:09:44 +0900 | [diff] [blame] | 13 | "text/template" |
Shinichiro Hamaji | 750988e | 2015-04-12 10:14:32 +0900 | [diff] [blame] | 14 | "time" |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 15 | ) |
Shinichiro Hamaji | 07259e0 | 2015-04-02 03:14:41 +0900 | [diff] [blame] | 16 | |
Fumitoshi Ukai | 5d85a70 | 2015-04-08 22:44:36 +0900 | [diff] [blame] | 17 | var ( |
Shinichiro Hamaji | 4926aea | 2015-05-25 18:32:18 +0900 | [diff] [blame] | 18 | katiLogFlag bool |
| 19 | makefileFlag string |
| 20 | dryRunFlag bool |
| 21 | jobsFlag int |
| 22 | cpuprofile string |
| 23 | heapprofile string |
| 24 | memstats string |
| 25 | katiStatsFlag bool |
| 26 | katiPeriodicStatsFlag bool |
| 27 | katiEvalStatsFlag bool |
| 28 | loadJson string |
| 29 | saveJson string |
| 30 | loadGob string |
| 31 | saveGob string |
| 32 | syntaxCheckOnlyFlag bool |
| 33 | queryFlag string |
| 34 | eagerCmdEvalFlag bool |
| 35 | useParaFlag bool |
| 36 | useCache bool |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 37 | generateNinja bool |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 38 | |
| 39 | katiDir string |
Fumitoshi Ukai | 5d85a70 | 2015-04-08 22:44:36 +0900 | [diff] [blame] | 40 | ) |
Shinichiro Hamaji | 07259e0 | 2015-04-02 03:14:41 +0900 | [diff] [blame] | 41 | |
Shinichiro Hamaji | 750ae2e | 2015-05-20 12:59:15 +0900 | [diff] [blame] | 42 | type DepGraph struct { |
Shinichiro Hamaji | f3ea5d0 | 2015-05-20 17:13:00 +0900 | [diff] [blame] | 43 | nodes []*DepNode |
| 44 | vars Vars |
Shinichiro Hamaji | 45cde1d | 2015-05-25 18:21:23 +0900 | [diff] [blame] | 45 | readMks []*ReadMakefile |
Shinichiro Hamaji | f3ea5d0 | 2015-05-20 17:13:00 +0900 | [diff] [blame] | 46 | isCached bool |
Shinichiro Hamaji | 750ae2e | 2015-05-20 12:59:15 +0900 | [diff] [blame] | 47 | } |
| 48 | |
Shinichiro Hamaji | 07259e0 | 2015-04-02 03:14:41 +0900 | [diff] [blame] | 49 | func parseFlags() { |
| 50 | // TODO: Make this default and replace this by -d flag. |
Fumitoshi Ukai | b07bcc3 | 2015-04-07 12:03:31 +0900 | [diff] [blame] | 51 | flag.BoolVar(&katiLogFlag, "kati_log", false, "Verbose kati specific log") |
Shinichiro Hamaji | 601cbb7 | 2015-04-02 05:55:47 +0900 | [diff] [blame] | 52 | flag.StringVar(&makefileFlag, "f", "", "Use it as a makefile") |
Shinichiro Hamaji | 07259e0 | 2015-04-02 03:14:41 +0900 | [diff] [blame] | 53 | |
Shinichiro Hamaji | 07e146b | 2015-04-02 16:17:24 +0900 | [diff] [blame] | 54 | flag.BoolVar(&dryRunFlag, "n", false, "Only print the commands that would be executed") |
Shinichiro Hamaji | 69bb7e4 | 2015-04-27 17:54:42 +0900 | [diff] [blame] | 55 | |
| 56 | flag.IntVar(&jobsFlag, "j", 1, "Allow N jobs at once.") |
| 57 | |
Shinichiro Hamaji | 1833c28 | 2015-04-28 05:02:27 +0900 | [diff] [blame] | 58 | flag.StringVar(&loadGob, "load", "", "") |
| 59 | flag.StringVar(&saveGob, "save", "", "") |
Shinichiro Hamaji | 8c9d0e3 | 2015-04-28 03:30:19 +0900 | [diff] [blame] | 60 | flag.StringVar(&loadJson, "load_json", "", "") |
Shinichiro Hamaji | 0df24ec | 2015-04-27 21:29:53 +0900 | [diff] [blame] | 61 | flag.StringVar(&saveJson, "save_json", "", "") |
| 62 | |
Fumitoshi Ukai | 5d85a70 | 2015-04-08 22:44:36 +0900 | [diff] [blame] | 63 | flag.StringVar(&cpuprofile, "kati_cpuprofile", "", "write cpu profile to `file`") |
Shinichiro Hamaji | 5ff9b34 | 2015-04-12 04:17:10 +0900 | [diff] [blame] | 64 | flag.StringVar(&heapprofile, "kati_heapprofile", "", "write heap profile to `file`") |
Fumitoshi Ukai | 89215d0 | 2015-04-30 13:09:44 +0900 | [diff] [blame] | 65 | flag.StringVar(&memstats, "kati_memstats", "", "Show memstats with given templates") |
Shinichiro Hamaji | 750988e | 2015-04-12 10:14:32 +0900 | [diff] [blame] | 66 | flag.BoolVar(&katiStatsFlag, "kati_stats", false, "Show a bunch of statistics") |
Shinichiro Hamaji | 4926aea | 2015-05-25 18:32:18 +0900 | [diff] [blame] | 67 | flag.BoolVar(&katiPeriodicStatsFlag, "kati_periodic_stats", false, "Show a bunch of periodic statistics") |
Fumitoshi Ukai | 586b02a | 2015-05-08 00:23:10 +0900 | [diff] [blame] | 68 | flag.BoolVar(&katiEvalStatsFlag, "kati_eval_stats", false, "Show eval statistics") |
Shinichiro Hamaji | b41fd50 | 2015-04-29 03:34:07 +0900 | [diff] [blame] | 69 | flag.BoolVar(&eagerCmdEvalFlag, "eager_cmd_eval", false, "Eval commands first.") |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 70 | flag.BoolVar(&useParaFlag, "use_para", false, "Use para.") |
Shinichiro Hamaji | bf66cb0 | 2015-04-28 16:21:35 +0900 | [diff] [blame] | 71 | flag.BoolVar(&syntaxCheckOnlyFlag, "c", false, "Syntax check only.") |
Shinichiro Hamaji | 1eb7111 | 2015-04-29 02:08:52 +0900 | [diff] [blame] | 72 | flag.StringVar(&queryFlag, "query", "", "Show the target info") |
Shinichiro Hamaji | b0d2e2f | 2015-05-20 16:42:59 +0900 | [diff] [blame] | 73 | // TODO: Make this default. |
| 74 | flag.BoolVar(&useCache, "use_cache", false, "Use cache.") |
Shinichiro Hamaji | 6aa86ac | 2015-05-26 15:21:11 +0900 | [diff] [blame] | 75 | flag.BoolVar(&generateNinja, "ninja", false, "Generate build.ninja.") |
Shinichiro Hamaji | 07259e0 | 2015-04-02 03:14:41 +0900 | [diff] [blame] | 76 | flag.Parse() |
| 77 | } |
Shinichiro Hamaji | b13f3d5 | 2015-03-30 19:29:44 +0900 | [diff] [blame] | 78 | |
Fumitoshi Ukai | 8ae323b | 2015-04-08 17:44:38 +0900 | [diff] [blame] | 79 | func parseCommandLine() ([]string, []string) { |
| 80 | var vars []string |
| 81 | var targets []string |
| 82 | for _, arg := range flag.Args() { |
| 83 | if strings.IndexByte(arg, '=') >= 0 { |
| 84 | vars = append(vars, arg) |
| 85 | continue |
| 86 | } |
| 87 | targets = append(targets, arg) |
| 88 | } |
| 89 | return vars, targets |
| 90 | } |
| 91 | |
Shinichiro Hamaji | 3cd6396 | 2015-04-06 16:38:45 +0900 | [diff] [blame] | 92 | func getBootstrapMakefile(targets []string) Makefile { |
Shinichiro Hamaji | a5dee37 | 2015-04-03 16:41:30 +0900 | [diff] [blame] | 93 | bootstrap := ` |
| 94 | CC:=cc |
| 95 | CXX:=g++ |
Fumitoshi Ukai | b75c5f3 | 2015-04-06 17:28:53 +0900 | [diff] [blame] | 96 | AR:=ar |
Shinichiro Hamaji | a5dee37 | 2015-04-03 16:41:30 +0900 | [diff] [blame] | 97 | MAKE:=kati |
| 98 | # Pretend to be GNU make 3.81, for compatibility. |
| 99 | MAKE_VERSION:=3.81 |
Shinichiro Hamaji | 89551c9 | 2015-04-11 19:33:03 +0900 | [diff] [blame] | 100 | SHELL:=/bin/sh |
Shinichiro Hamaji | a5dee37 | 2015-04-03 16:41:30 +0900 | [diff] [blame] | 101 | # TODO: Add more builtin vars. |
| 102 | |
| 103 | # http://www.gnu.org/software/make/manual/make.html#Catalogue-of-Rules |
| 104 | # The document above is actually not correct. See default.c: |
| 105 | # http://git.savannah.gnu.org/cgit/make.git/tree/default.c?id=4.1 |
| 106 | .c.o: |
| 107 | $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $< |
| 108 | .cc.o: |
| 109 | $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $< |
| 110 | # TODO: Add more builtin rules. |
| 111 | ` |
Shinichiro Hamaji | 4c3dc40 | 2015-05-28 20:49:00 +0900 | [diff] [blame] | 112 | bootstrap += fmt.Sprintf("MAKECMDGOALS:=%s\n", strings.Join(targets, " ")) |
| 113 | cwd, err := filepath.Abs(".") |
| 114 | if err != nil { |
| 115 | panic(err) |
| 116 | } |
| 117 | bootstrap += fmt.Sprintf("CURDIR:=%s\n", cwd) |
Fumitoshi Ukai | 103a796 | 2015-04-08 15:53:53 +0900 | [diff] [blame] | 118 | mk, err := ParseMakefileString(bootstrap, BootstrapMakefile, 0) |
Shinichiro Hamaji | a5dee37 | 2015-04-03 16:41:30 +0900 | [diff] [blame] | 119 | if err != nil { |
| 120 | panic(err) |
| 121 | } |
| 122 | return mk |
| 123 | } |
| 124 | |
Shinichiro Hamaji | 5ff9b34 | 2015-04-12 04:17:10 +0900 | [diff] [blame] | 125 | func maybeWriteHeapProfile() { |
| 126 | if heapprofile != "" { |
| 127 | f, err := os.Create(heapprofile) |
| 128 | if err != nil { |
| 129 | panic(err) |
| 130 | } |
| 131 | pprof.WriteHeapProfile(f) |
| 132 | } |
| 133 | } |
| 134 | |
Shinichiro Hamaji | b0d2e2f | 2015-05-20 16:42:59 +0900 | [diff] [blame] | 135 | func getDepGraph(clvars []string, targets []string) *DepGraph { |
Shinichiro Hamaji | 750988e | 2015-04-12 10:14:32 +0900 | [diff] [blame] | 136 | startTime := time.Now() |
| 137 | |
Shinichiro Hamaji | 1833c28 | 2015-04-28 05:02:27 +0900 | [diff] [blame] | 138 | if loadGob != "" { |
Shinichiro Hamaji | 750ae2e | 2015-05-20 12:59:15 +0900 | [diff] [blame] | 139 | g := LoadDepGraph(loadGob) |
Shinichiro Hamaji | 1833c28 | 2015-04-28 05:02:27 +0900 | [diff] [blame] | 140 | LogStats("deserialize time: %q", time.Now().Sub(startTime)) |
Shinichiro Hamaji | 750ae2e | 2015-05-20 12:59:15 +0900 | [diff] [blame] | 141 | return g |
Shinichiro Hamaji | 1833c28 | 2015-04-28 05:02:27 +0900 | [diff] [blame] | 142 | } |
Shinichiro Hamaji | 8c9d0e3 | 2015-04-28 03:30:19 +0900 | [diff] [blame] | 143 | if loadJson != "" { |
Shinichiro Hamaji | 750ae2e | 2015-05-20 12:59:15 +0900 | [diff] [blame] | 144 | g := LoadDepGraphFromJson(loadJson) |
Shinichiro Hamaji | 8c9d0e3 | 2015-04-28 03:30:19 +0900 | [diff] [blame] | 145 | LogStats("deserialize time: %q", time.Now().Sub(startTime)) |
Shinichiro Hamaji | 750ae2e | 2015-05-20 12:59:15 +0900 | [diff] [blame] | 146 | return g |
Shinichiro Hamaji | 8c9d0e3 | 2015-04-28 03:30:19 +0900 | [diff] [blame] | 147 | } |
| 148 | |
Shinichiro Hamaji | b0d2e2f | 2015-05-20 16:42:59 +0900 | [diff] [blame] | 149 | makefile := makefileFlag |
| 150 | if makefile == "" { |
| 151 | makefile = GetDefaultMakefile() |
| 152 | } |
| 153 | |
| 154 | if useCache { |
Shinichiro Hamaji | f3ea5d0 | 2015-05-20 17:13:00 +0900 | [diff] [blame] | 155 | g := LoadDepGraphCache(makefile, targets) |
Shinichiro Hamaji | b0d2e2f | 2015-05-20 16:42:59 +0900 | [diff] [blame] | 156 | if g != nil { |
| 157 | return g |
| 158 | } |
| 159 | } |
| 160 | |
Shinichiro Hamaji | 3cd6396 | 2015-04-06 16:38:45 +0900 | [diff] [blame] | 161 | bmk := getBootstrapMakefile(targets) |
Shinichiro Hamaji | c384081 | 2015-04-02 02:10:20 +0900 | [diff] [blame] | 162 | |
Shinichiro Hamaji | 45cde1d | 2015-05-25 18:21:23 +0900 | [diff] [blame] | 163 | content, err := readFile(makefile) |
| 164 | if err != nil { |
| 165 | panic(err) |
| 166 | } |
| 167 | mk, err := ParseMakefile(content, makefile) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 168 | if err != nil { |
| 169 | panic(err) |
| 170 | } |
| 171 | |
| 172 | for _, stmt := range mk.stmts { |
| 173 | stmt.show() |
| 174 | } |
| 175 | |
Shinichiro Hamaji | c384081 | 2015-04-02 02:10:20 +0900 | [diff] [blame] | 176 | mk.stmts = append(bmk.stmts, mk.stmts...) |
| 177 | |
Shinichiro Hamaji | cc919ae | 2015-04-09 17:23:30 +0900 | [diff] [blame] | 178 | vars := make(Vars) |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 179 | for _, env := range os.Environ() { |
| 180 | kv := strings.SplitN(env, "=", 2) |
| 181 | Log("envvar %q", kv) |
| 182 | if len(kv) < 2 { |
| 183 | panic(fmt.Sprintf("A weird environ variable %q", kv)) |
| 184 | } |
| 185 | vars.Assign(kv[0], RecursiveVar{ |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 186 | expr: literal(kv[1]), |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 187 | origin: "environment", |
| 188 | }) |
| 189 | } |
Shinichiro Hamaji | 39728f1 | 2015-04-11 20:12:23 +0900 | [diff] [blame] | 190 | vars.Assign("MAKEFILE_LIST", SimpleVar{value: []byte{}, origin: "file"}) |
Fumitoshi Ukai | 8ae323b | 2015-04-08 17:44:38 +0900 | [diff] [blame] | 191 | for _, v := range clvars { |
| 192 | kv := strings.SplitN(v, "=", 2) |
| 193 | Log("cmdlinevar %q", kv) |
| 194 | if len(kv) < 2 { |
| 195 | panic(fmt.Sprintf("unexpected command line var %q", kv)) |
| 196 | } |
| 197 | vars.Assign(kv[0], RecursiveVar{ |
Fumitoshi Ukai | b36f387 | 2015-04-10 15:06:38 +0900 | [diff] [blame] | 198 | expr: literal(kv[1]), |
Fumitoshi Ukai | 8ae323b | 2015-04-08 17:44:38 +0900 | [diff] [blame] | 199 | origin: "command line", |
| 200 | }) |
| 201 | } |
Shinichiro Hamaji | 491e73f | 2015-04-07 12:41:59 +0900 | [diff] [blame] | 202 | |
Shinichiro Hamaji | d7bef60 | 2015-03-30 19:55:32 +0900 | [diff] [blame] | 203 | er, err := Eval(mk, vars) |
Fumitoshi Ukai | f8efa0a | 2015-03-30 18:10:11 +0900 | [diff] [blame] | 204 | if err != nil { |
| 205 | panic(err) |
| 206 | } |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 207 | |
Shinichiro Hamaji | 63a77d4 | 2015-04-12 01:04:07 +0900 | [diff] [blame] | 208 | vars.Merge(er.vars) |
Shinichiro Hamaji | 574a4ef | 2015-04-06 15:20:28 +0900 | [diff] [blame] | 209 | |
Shinichiro Hamaji | 3887c50 | 2015-04-12 22:07:56 +0900 | [diff] [blame] | 210 | LogStats("eval time: %q", time.Now().Sub(startTime)) |
Fumitoshi Ukai | 586b02a | 2015-05-08 00:23:10 +0900 | [diff] [blame] | 211 | LogStats("shell func time: %q", shellFuncTime) |
Shinichiro Hamaji | 750988e | 2015-04-12 10:14:32 +0900 | [diff] [blame] | 212 | |
| 213 | startTime = time.Now() |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 214 | db := NewDepBuilder(er, vars) |
Shinichiro Hamaji | d68b5bf | 2015-04-29 00:27:11 +0900 | [diff] [blame] | 215 | LogStats("dep build prepare time: %q", time.Now().Sub(startTime)) |
Shinichiro Hamaji | 750988e | 2015-04-12 10:14:32 +0900 | [diff] [blame] | 216 | |
Shinichiro Hamaji | 8843a05 | 2015-04-13 16:46:56 +0900 | [diff] [blame] | 217 | startTime = time.Now() |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 218 | nodes, err2 := db.Eval(targets) |
| 219 | if err2 != nil { |
| 220 | panic(err2) |
| 221 | } |
Shinichiro Hamaji | d68b5bf | 2015-04-29 00:27:11 +0900 | [diff] [blame] | 222 | LogStats("dep build time: %q", time.Now().Sub(startTime)) |
Shinichiro Hamaji | 45cde1d | 2015-05-25 18:21:23 +0900 | [diff] [blame] | 223 | var readMks []*ReadMakefile |
Shinichiro Hamaji | b0d2e2f | 2015-05-20 16:42:59 +0900 | [diff] [blame] | 224 | // Always put the root Makefile as the first element. |
Shinichiro Hamaji | 45cde1d | 2015-05-25 18:21:23 +0900 | [diff] [blame] | 225 | readMks = append(readMks, &ReadMakefile{ |
| 226 | Filename: makefile, |
Shinichiro Hamaji | 4220be3 | 2015-05-26 13:30:18 +0900 | [diff] [blame] | 227 | Hash: sha1.Sum(content), |
Shinichiro Hamaji | 45cde1d | 2015-05-25 18:21:23 +0900 | [diff] [blame] | 228 | State: FILE_EXISTS, |
Shinichiro Hamaji | f3ea5d0 | 2015-05-20 17:13:00 +0900 | [diff] [blame] | 229 | }) |
Shinichiro Hamaji | b0d2e2f | 2015-05-20 16:42:59 +0900 | [diff] [blame] | 230 | readMks = append(readMks, er.readMks...) |
| 231 | return &DepGraph{ |
Shinichiro Hamaji | f3ea5d0 | 2015-05-20 17:13:00 +0900 | [diff] [blame] | 232 | nodes: nodes, |
| 233 | vars: vars, |
| 234 | readMks: readMks, |
Shinichiro Hamaji | 750ae2e | 2015-05-20 12:59:15 +0900 | [diff] [blame] | 235 | } |
Shinichiro Hamaji | 1d95907 | 2015-04-27 22:57:44 +0900 | [diff] [blame] | 236 | } |
| 237 | |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 238 | func findKatiDir() { |
| 239 | switch runtime.GOOS { |
| 240 | case "linux": |
| 241 | kati, err := os.Readlink("/proc/self/exe") |
| 242 | if err != nil { |
| 243 | panic(err) |
| 244 | } |
| 245 | katiDir = filepath.Dir(kati) |
| 246 | default: |
| 247 | panic(fmt.Sprintf("unknown OS: %s", runtime.GOOS)) |
| 248 | } |
| 249 | } |
| 250 | |
Shinichiro Hamaji | 1d95907 | 2015-04-27 22:57:44 +0900 | [diff] [blame] | 251 | func main() { |
Shinichiro Hamaji | 5e07dd3 | 2015-05-12 15:37:37 +0900 | [diff] [blame] | 252 | runtime.GOMAXPROCS(runtime.NumCPU()) |
Shinichiro Hamaji | cedc5c8 | 2015-05-13 17:03:20 +0900 | [diff] [blame] | 253 | findKatiDir() |
Shinichiro Hamaji | 1d95907 | 2015-04-27 22:57:44 +0900 | [diff] [blame] | 254 | parseFlags() |
| 255 | if cpuprofile != "" { |
| 256 | f, err := os.Create(cpuprofile) |
| 257 | if err != nil { |
| 258 | panic(err) |
| 259 | } |
| 260 | pprof.StartCPUProfile(f) |
| 261 | defer pprof.StopCPUProfile() |
| 262 | } |
| 263 | defer maybeWriteHeapProfile() |
| 264 | defer dumpStats() |
Fumitoshi Ukai | 89215d0 | 2015-04-30 13:09:44 +0900 | [diff] [blame] | 265 | if memstats != "" { |
| 266 | t := template.Must(template.New("memstats").Parse(memstats)) |
| 267 | var ms runtime.MemStats |
| 268 | runtime.ReadMemStats(&ms) |
| 269 | var buf bytes.Buffer |
| 270 | err := t.Execute(&buf, ms) |
| 271 | fmt.Println(buf.String()) |
| 272 | if err != nil { |
| 273 | panic(err) |
| 274 | } |
| 275 | defer func() { |
| 276 | var ms runtime.MemStats |
| 277 | runtime.ReadMemStats(&ms) |
| 278 | var buf bytes.Buffer |
| 279 | t.Execute(&buf, ms) |
| 280 | fmt.Println(buf.String()) |
| 281 | }() |
| 282 | } |
Shinichiro Hamaji | 1d95907 | 2015-04-27 22:57:44 +0900 | [diff] [blame] | 283 | |
| 284 | clvars, targets := parseCommandLine() |
| 285 | |
Shinichiro Hamaji | 750ae2e | 2015-05-20 12:59:15 +0900 | [diff] [blame] | 286 | g := getDepGraph(clvars, targets) |
| 287 | nodes := g.nodes |
| 288 | vars := g.vars |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 289 | |
Shinichiro Hamaji | b41fd50 | 2015-04-29 03:34:07 +0900 | [diff] [blame] | 290 | if eagerCmdEvalFlag { |
| 291 | startTime := time.Now() |
| 292 | EvalCommands(nodes, vars) |
| 293 | LogStats("eager eval command time: %q", time.Now().Sub(startTime)) |
| 294 | } |
| 295 | |
Shinichiro Hamaji | 1833c28 | 2015-04-28 05:02:27 +0900 | [diff] [blame] | 296 | if saveGob != "" { |
| 297 | startTime := time.Now() |
Shinichiro Hamaji | 750ae2e | 2015-05-20 12:59:15 +0900 | [diff] [blame] | 298 | DumpDepGraph(g, saveGob, targets) |
Shinichiro Hamaji | 1833c28 | 2015-04-28 05:02:27 +0900 | [diff] [blame] | 299 | LogStats("serialize time: %q", time.Now().Sub(startTime)) |
| 300 | } |
Shinichiro Hamaji | 0df24ec | 2015-04-27 21:29:53 +0900 | [diff] [blame] | 301 | if saveJson != "" { |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 302 | startTime := time.Now() |
Shinichiro Hamaji | 750ae2e | 2015-05-20 12:59:15 +0900 | [diff] [blame] | 303 | DumpDepGraphAsJson(g, saveJson, targets) |
Shinichiro Hamaji | c8bc731 | 2015-04-28 02:48:03 +0900 | [diff] [blame] | 304 | LogStats("serialize time: %q", time.Now().Sub(startTime)) |
Shinichiro Hamaji | 0df24ec | 2015-04-27 21:29:53 +0900 | [diff] [blame] | 305 | } |
| 306 | |
Shinichiro Hamaji | f3ea5d0 | 2015-05-20 17:13:00 +0900 | [diff] [blame] | 307 | if useCache && !g.isCached { |
Shinichiro Hamaji | b0d2e2f | 2015-05-20 16:42:59 +0900 | [diff] [blame] | 308 | startTime := time.Now() |
| 309 | DumpDepGraphCache(g, targets) |
| 310 | LogStats("serialize time: %q", time.Now().Sub(startTime)) |
| 311 | } |
| 312 | |
Shinichiro Hamaji | e0eb031 | 2015-05-27 18:33:18 +0900 | [diff] [blame] | 313 | if generateNinja { |
| 314 | startTime := time.Now() |
| 315 | GenerateNinja(g) |
| 316 | LogStats("generate ninja time: %q", time.Now().Sub(startTime)) |
| 317 | return |
| 318 | } |
| 319 | |
Shinichiro Hamaji | bf66cb0 | 2015-04-28 16:21:35 +0900 | [diff] [blame] | 320 | if syntaxCheckOnlyFlag { |
| 321 | return |
| 322 | } |
| 323 | |
Shinichiro Hamaji | 1eb7111 | 2015-04-29 02:08:52 +0900 | [diff] [blame] | 324 | if queryFlag != "" { |
| 325 | HandleQuery(queryFlag, nodes, vars) |
| 326 | return |
| 327 | } |
| 328 | |
Shinichiro Hamaji | 1d95907 | 2015-04-27 22:57:44 +0900 | [diff] [blame] | 329 | startTime := time.Now() |
Shinichiro Hamaji | 17a8a6e | 2015-04-20 21:44:42 +0900 | [diff] [blame] | 330 | ex := NewExecutor(vars) |
Shinichiro Hamaji | 1d95907 | 2015-04-27 22:57:44 +0900 | [diff] [blame] | 331 | err := ex.Exec(nodes) |
Fumitoshi Ukai | 3e5161e | 2015-04-01 22:42:10 +0900 | [diff] [blame] | 332 | if err != nil { |
| 333 | panic(err) |
| 334 | } |
Shinichiro Hamaji | 3887c50 | 2015-04-12 22:07:56 +0900 | [diff] [blame] | 335 | LogStats("exec time: %q", time.Now().Sub(startTime)) |
Fumitoshi Ukai | 119dc91 | 2015-03-30 16:52:41 +0900 | [diff] [blame] | 336 | } |