OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2015 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 package main | |
8 | |
9 // Example use: | |
10 // git clone https://skia.googlesource.com/skia.git | |
11 // cd skia | |
12 // SKIA=$PWD | |
13 // cd tools/fiddle | |
14 // go get github.com/skia-dev/glog | |
15 // go get go.skia.org/infra/go/util | |
16 // go build fiddler.go | |
17 // # compile prerequisites | |
18 // ./fiddler "$SKIA" | |
19 // # compile and run a fiddle | |
20 // ./fiddler "$SKIA" draw.cpp | ./parse-fiddle-output | |
21 // # compile and run a different fiddle | |
22 // ./fiddler "$SKIA" ANOTHER_FIDDLE.cpp | ./parse-fiddle-output | |
23 | |
24 import ( | |
25 "bytes" | |
26 "fmt" | |
27 "io" | |
28 "io/ioutil" | |
29 "os" | |
30 "os/exec" | |
31 "path" | |
32 "syscall" | |
33 | |
34 "github.com/skia-dev/glog" | |
35 "go.skia.org/infra/go/util" | |
36 ) | |
37 | |
38 func fiddlePath() string { | |
39 return path.Join("tools", "fiddle") | |
40 } | |
41 func setResourceLimits() error { | |
42 const maximumTimeInSeconds = 5 | |
43 limit := syscall.Rlimit{maximumTimeInSeconds, maximumTimeInSeconds} | |
44 if err := syscall.Setrlimit(syscall.RLIMIT_CPU, &limit); err != nil { | |
45 return err | |
46 } | |
47 const maximumMemoryInBytes = 1 << 28 | |
48 limit = syscall.Rlimit{maximumMemoryInBytes, maximumMemoryInBytes} | |
49 return syscall.Setrlimit(syscall.RLIMIT_AS, &limit) | |
50 } | |
51 | |
52 // execCommand runs command and returns an error if it fails. If there is no | |
53 // error, all output is discarded. | |
54 func execCommand(input io.Reader, dir string, name string, arg ...string) error
{ | |
55 var buffer bytes.Buffer | |
56 cmd := exec.Command(name, arg...) | |
57 cmd.Dir = dir | |
58 cmd.Stdout = &buffer | |
59 cmd.Stderr = &buffer | |
60 cmd.Stdin = input | |
61 if err := cmd.Run(); err != nil { | |
62 return fmt.Errorf("execution failed:\n\n%s\n[%v]", buffer.String
(), err) | |
63 } | |
64 return nil | |
65 } | |
66 | |
67 func compileArgs(skiaSrc string) string { | |
68 return "@" + path.Join(skiaSrc, "cmake", "skia_compile_arguments.txt") | |
69 } | |
70 | |
71 func linkArgs(skiaSrc string) string { | |
72 return "@" + path.Join(skiaSrc, "cmake", "skia_link_arguments.txt") | |
73 } | |
74 | |
75 // fiddler compiles the input, links against skia, and runs the executable. | |
76 // @param skiaSrc: the base directory of the Skia repository | |
77 // @param inputReader: C++ fiddle source | |
78 // @param output: stdout of executable sent here | |
79 // @param tempDir: where to place the compiled executable | |
80 func fiddler(skiaSrc string, inputReader io.Reader, output io.Writer, tempDir st
ring) error { | |
81 binarypath := path.Join(tempDir, "fiddle") | |
82 fiddle_dir := path.Join(skiaSrc, fiddlePath()) | |
83 if err := execCommand(inputReader, fiddle_dir, | |
84 "c++", | |
85 compileArgs(skiaSrc), | |
86 "-I", fiddle_dir, | |
87 "-o", binarypath, | |
88 "-x", "c++", "-", "-x", "none", | |
89 "fiddle_main.o", | |
90 "-lOSMesa", | |
91 linkArgs(skiaSrc), | |
92 ); err != nil { | |
93 return err | |
94 } | |
95 var buffer bytes.Buffer | |
96 runCmd := exec.Cmd{Path: binarypath, Stdout: output, Stderr: &buffer} | |
97 if err := runCmd.Run(); err != nil { | |
98 return fmt.Errorf("execution failed:\n\n%s\n[%v]", buffer.String
(), err) | |
99 } | |
100 return nil | |
101 } | |
102 | |
103 // Compile Skia library and fiddle_main.cpp | |
104 // @param skiaSrc: the base directory of the Skia repository. | |
105 func fiddlerPrerequisites(skiaSrc string) error { | |
106 cmakeDir := path.Join(skiaSrc, "cmake") | |
107 if err := execCommand(nil, cmakeDir, "cmake", "-G", "Ninja", "."); err !
= nil { | |
108 return err | |
109 } | |
110 if err := execCommand(nil, cmakeDir, "ninja", "skia"); err != nil { | |
111 return err | |
112 } | |
113 fiddle_dir := path.Join(skiaSrc, fiddlePath()) | |
114 if err := execCommand(nil, fiddle_dir, "c++", compileArgs(skiaSrc), | |
115 "fiddle_main.h"); err != nil { | |
116 return err | |
117 } | |
118 return execCommand(nil, fiddle_dir, "c++", compileArgs(skiaSrc), | |
119 "-c", "-o", "fiddle_main.o", "fiddle_main.cpp") | |
120 } | |
121 | |
122 func main() { | |
123 if len(os.Args) < 2 { | |
124 glog.Fatalf("usage: %s SKIA_SRC_PATH [PATH_TO_DRAW.CPP]", os.Arg
s[0]) | |
125 } | |
126 skiaSrc := os.Args[1] | |
127 if len(os.Args) < 3 { | |
128 // execCommand(nil, skiaSrc, "git", "fetch") | |
129 // execCommand(nil, skiaSrc, "git", "checkout", "origin/master") | |
130 if err := fiddlerPrerequisites(skiaSrc); err != nil { | |
131 glog.Fatal(err) | |
132 } | |
133 } else { | |
134 if err := setResourceLimits(); err != nil { | |
135 glog.Fatal(err) | |
136 } | |
137 tempDir, err := ioutil.TempDir("", "fiddle_") | |
138 if err != nil { | |
139 glog.Fatal(err) | |
140 } | |
141 defer func() { | |
142 err = os.RemoveAll(tempDir) | |
143 if err != nil { | |
144 glog.Fatalf("os.RemoveAll(tempDir) failed: %v",
err) | |
145 } | |
146 }() | |
147 if os.Args[2] == "-" { | |
148 if err := fiddler(skiaSrc, os.Stdin, os.Stdout, tempDir)
; err != nil { | |
149 glog.Fatal(err) | |
150 } | |
151 } else { | |
152 inputFile, err := os.Open(os.Args[2]) | |
153 if err != nil { | |
154 glog.Fatalf("unable to open \"%s\": %v", os.Args
[2], err) | |
155 } | |
156 defer util.Close(inputFile) | |
157 if err = fiddler(skiaSrc, inputFile, os.Stdout, tempDir)
; err != nil { | |
158 glog.Fatal(err) | |
159 } | |
160 } | |
161 } | |
162 } | |
OLD | NEW |