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 experimental/fiddle | |
14 // go build fiddler.go | |
15 // # compile prerequisites | |
16 // ./fiddler "$SKIA" | |
17 // # compile and run a fiddle | |
18 // ./fiddler "$SKIA" draw.cpp | ./parse-fiddle-output | |
19 // # compile and run a different fiddle | |
20 // ./fiddler "$SKIA" ANOTHER_FIDDLE.cpp | ./parse-fiddle-output | |
21 | |
22 import ( | |
23 "bytes" | |
24 "fmt" | |
25 "io" | |
26 "io/ioutil" | |
27 "os" | |
28 "os/exec" | |
29 "path" | |
30 "path/filepath" | |
31 "sort" | |
32 "strings" | |
33 "syscall" | |
34 | |
35 "github.com/skia-dev/glog" | |
36 ) | |
37 | |
38 func setResourceLimits() { | |
39 const maximumTimeInSeconds = 5 | |
40 limit := syscall.Rlimit{maximumTimeInSeconds, maximumTimeInSeconds} | |
41 if err := syscall.Setrlimit(syscall.RLIMIT_CPU, &limit); err != nil { | |
42 glog.Fatalf("syscall.Setrlimit(RLIMIT_CPU) error: %v", err) | |
43 } | |
44 const maximumMemoryInBytes = 1 << 28 | |
45 limit = syscall.Rlimit{maximumMemoryInBytes, maximumMemoryInBytes} | |
46 if err := syscall.Setrlimit(syscall.RLIMIT_AS, &limit); err != nil { | |
47 glog.Fatalf("syscall.Setrlimit(RLIMIT_AS) error: %v", err) | |
48 } | |
49 } | |
50 | |
51 func trimmedBasePath(fpath string) string { | |
52 basename := path.Base(fpath) | |
53 return strings.TrimSuffix(basename, path.Ext(basename)) | |
54 } | |
55 | |
56 func absPath(path string) string { | |
57 abspath, err := filepath.Abs(path) | |
58 if err != nil { | |
59 glog.Fatalf("filepath.Abs failed %v", err) | |
60 } | |
61 return abspath | |
62 } | |
63 | |
64 // runs command and logs a fatal error if it fails. If there is no | |
65 // error, all output is discarded. | |
66 func execCommand(input io.Reader, dir string, name string, arg ...string) { | |
67 var buffer bytes.Buffer | |
68 cmd := exec.Command(name, arg...) | |
69 cmd.Dir = dir | |
70 cmd.Stdout = &buffer | |
71 cmd.Stderr = &buffer | |
72 cmd.Stdin = input | |
73 if err := cmd.Run(); err != nil { | |
74 glog.Fatalf("command failed:\n%v\n%s\n[%v]", cmd.Args, buffer.St ring(), err) | |
75 } | |
76 } | |
77 | |
78 func compileSkia(inputReader io.Reader, skiaSrc string, arguments []string) { | |
79 compileArguments := []string{ | |
80 "--std=c++11", | |
81 "-DSK_RELEASE", | |
82 "-DSK_MESA", | |
83 fmt.Sprintf("-I%s", path.Join(skiaSrc, "include/core")), | |
84 fmt.Sprintf("-I%s", path.Join(skiaSrc, "include/gpu")), | |
85 fmt.Sprintf("-I%s", path.Join(skiaSrc, "include/effects")), | |
86 fmt.Sprintf("-I%s", path.Join(skiaSrc, "include/pathops")), | |
87 fmt.Sprintf("-I%s", path.Join(skiaSrc, "include/c")), | |
88 fmt.Sprintf("-I%s", path.Join(skiaSrc, "include/utils")), | |
89 fmt.Sprintf("-I%s", path.Join(skiaSrc, "include/config")), | |
90 } | |
91 compileArguments = append(compileArguments, arguments...) | |
92 execCommand(inputReader, "", "c++", compileArguments...) | |
93 } | |
94 | |
95 func closeme(closer io.Closer) { | |
96 err := closer.Close() | |
97 if err != nil { | |
98 glog.Fatalf("Close() failed: %v", err) | |
99 } | |
100 } | |
101 | |
102 func fiddler(skiaSrc string, inputReader io.Reader, output io.Writer) { | |
103 tempDir, err := ioutil.TempDir("", "fiddle_") | |
104 if err != nil { | |
105 glog.Fatalf("ioutil.TempDir() failed: %v", err) | |
106 } | |
107 defer func() { | |
108 err = os.RemoveAll(tempDir) | |
109 if err != nil { | |
110 glog.Fatalf("os.RemoveAll(tempDir) failed: %v", err) | |
111 } | |
112 }() | |
113 binarypath := path.Join(tempDir, "fiddle") | |
114 | |
115 fiddle_dir := path.Join(skiaSrc, "experimental", "fiddle") | |
116 libskia_dir := absPath(path.Join(skiaSrc, "cmake")) | |
117 | |
118 compileSkia(inputReader, skiaSrc, []string{ | |
119 "-o", binarypath, | |
120 "-x", "c++", "-", "-x", "none", | |
121 path.Join(fiddle_dir, "fiddle_main.o"), | |
122 path.Join(libskia_dir, "libskia.so"), | |
123 "-lOSMesa", | |
124 fmt.Sprintf("-Wl,-rpath,%s", libskia_dir), | |
125 }) | |
126 glog.Infof("Successfully compiled %s", binarypath) | |
127 var buffer bytes.Buffer | |
128 runCmd := exec.Command(binarypath) | |
129 runCmd.Stdout = output | |
130 runCmd.Stderr = &buffer | |
131 if err := runCmd.Run(); err != nil { | |
132 glog.Fatalf("execution failed:\n\n%s\n[%v]", buffer.String(), er r) | |
133 } | |
134 } | |
135 | |
136 func fiddlerPrerequisites(skiaSrc string) { | |
137 execCommand(nil, path.Join(skiaSrc, "cmake"), "cmake", "-G", "Ninja") | |
138 execCommand(nil, path.Join(skiaSrc, "cmake"), "ninja", "skia") | |
139 | |
140 fiddle_dir := path.Join(skiaSrc, "experimental", "fiddle") | |
141 out, err := os.Create(path.Join(fiddle_dir, "skia.h")) | |
jcgregorio
2015/11/24 20:21:35
Generating skia.h should be part of cmake, that wa
| |
142 if err != nil { | |
143 glog.Fatalf("os.Create('experimental/fiddle/skia.h') failed: %v" , err) | |
144 } | |
145 defer closeme(out) | |
146 | |
147 fmt.Fprintf(out, "#ifndef skia_headers_DEFINED\n") | |
148 fmt.Fprintf(out, "#define skia_headers_DEFINED\n\n") | |
149 | |
150 for _, dir := range []string{"c", "core", "effects", "pathops", "gpu"} { | |
151 fmt.Fprintf(out, "// %s\n", dir) | |
152 matches, err := filepath.Glob(path.Join(skiaSrc, "include", dir, "*.h")) | |
153 if err != nil { | |
154 glog.Fatalf("glob failed: %v", err) | |
155 } | |
156 sortedMatches := sort.StringSlice(matches) | |
157 sortedMatches.Sort() | |
158 for _, match := range sortedMatches { | |
159 fmt.Fprintf(out, "#include \"%s\"\n", filepath.Base(matc h)) | |
160 } | |
161 fmt.Fprintf(out, "\n") | |
162 } | |
163 fmt.Fprintf(out, "#include \"gl/GrGLInterface.h\"\n") | |
164 fmt.Fprintf(out, "\n#endif // skia_headers_DEFINED\n") | |
165 | |
166 compileSkia(nil, skiaSrc, []string{path.Join(fiddle_dir, "fiddle_main.h" )}) | |
167 compileSkia(nil, skiaSrc, []string{ | |
168 "-c", "-o", path.Join(fiddle_dir, "fiddle_main.o"), | |
169 path.Join(fiddle_dir, "fiddle_main.cpp")}) | |
170 } | |
171 | |
172 func main() { | |
173 if len(os.Args) < 2 { | |
174 glog.Fatalf("usage: %s SKIA_SRC_PATH [PATH_TO_DRAW.CPP]", os.Arg s[0]) | |
175 } | |
176 skiaSrc := os.Args[1] | |
177 if len(os.Args) < 3 { | |
178 // execCommand(nil, skiaSrc, "git", "fetch") | |
179 // execCommand(nil, skiaSrc, "git", "checkout", "origin/master") | |
180 fiddlerPrerequisites(skiaSrc) | |
181 } else { | |
182 setResourceLimits() | |
183 if os.Args[2] == "-" { | |
184 fiddler(skiaSrc, os.Stdin, os.Stdout) | |
185 } else { | |
186 inputFile, err := os.Open(os.Args[2]) | |
187 if err != nil { | |
188 glog.Fatalf("unable to open \"%s\": %v", os.Args [2], err) | |
189 } | |
190 defer closeme(inputFile) | |
191 fiddler(skiaSrc, inputFile, os.Stdout) | |
192 } | |
193 } | |
194 } | |
OLD | NEW |