Debian Codegraph @

gl/dll_windows.go

raw symbols SCIP
1 // Copyright 2015 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package gl
6
7 import (
8 "archive/tar"
9 "compress/gzip"
10 "debug/pe"
11 "fmt"
12 "io"
13 "log"
14 "net/http"
15 "os"
16 "path/filepath"
17 "runtime"
18 )
19
20 var debug = log.New(io.Discard, "gl: ", log.LstdFlags)
21
22 func downloadDLLs() (path string, err error) {
23 url := "https://dl.google.com/go/mobile/angle-bd3f8780b-" + runtime.GOARCH + ".tgz"
24 debug.Printf("downloading %s", url)
25 resp, err := http.Get(url)
26 if err != nil {
27 return "", fmt.Errorf("gl: %v", err)
28 }
29 defer func() {
30 err2 := resp.Body.Close()
31 if err == nil && err2 != nil {
32 err = fmt.Errorf("gl: error reading body from %v: %v", url, err2)
33 }
34 }()
35 if resp.StatusCode != http.StatusOK {
36 err := fmt.Errorf("gl: error fetching %v, status: %v", url, resp.Status)
37 return "", err
38 }
39
40 r, err := gzip.NewReader(resp.Body)
41 if err != nil {
42 return "", fmt.Errorf("gl: error reading gzip from %v: %v", url, err)
43 }
44 tr := tar.NewReader(r)
45 var bytesGLESv2, bytesEGL, bytesD3DCompiler []byte
46 for {
47 header, err := tr.Next()
48 if err == io.EOF {
49 break
50 }
51 if err != nil {
52 return "", fmt.Errorf("gl: error reading tar from %v: %v", url, err)
53 }
54 switch header.Name {
55 case "angle-" + runtime.GOARCH + "/libglesv2.dll":
56 bytesGLESv2, err = io.ReadAll(tr)
57 case "angle-" + runtime.GOARCH + "/libegl.dll":
58 bytesEGL, err = io.ReadAll(tr)
59 case "angle-" + runtime.GOARCH + "/d3dcompiler_47.dll":
60 bytesD3DCompiler, err = io.ReadAll(tr)
61 default: // skip
62 }
63 if err != nil {
64 return "", fmt.Errorf("gl: error reading %v from %v: %v", header.Name, url, err)
65 }
66 }
67 if len(bytesGLESv2) == 0 || len(bytesEGL) == 0 || len(bytesD3DCompiler) == 0 {
68 return "", fmt.Errorf("gl: did not find all DLLs in %v", url)
69 }
70
71 writeDLLs := func(path string) error {
72 if err := os.WriteFile(filepath.Join(path, "libglesv2.dll"), bytesGLESv2, 0755); err != nil {
73 return fmt.Errorf("gl: cannot install ANGLE: %v", err)
74 }
75 if err := os.WriteFile(filepath.Join(path, "libegl.dll"), bytesEGL, 0755); err != nil {
76 return fmt.Errorf("gl: cannot install ANGLE: %v", err)
77 }
78 if err := os.WriteFile(filepath.Join(path, "d3dcompiler_47.dll"), bytesD3DCompiler, 0755); err != nil {
79 return fmt.Errorf("gl: cannot install ANGLE: %v", err)
80 }
81 return nil
82 }
83
84 // First, we attempt to install these DLLs in LOCALAPPDATA/Shiny.
85 //
86 // Traditionally we would use the system32 directory, but it is
87 // no longer writable by normal programs.
88 os.MkdirAll(appdataPath(), 0775)
89 if err := writeDLLs(appdataPath()); err == nil {
90 return appdataPath(), nil
91 }
92 debug.Printf("DLLs could not be written to %s", appdataPath())
93
94 // Second, install in GOPATH/pkg if it exists.
95 gopath := os.Getenv("GOPATH")
96 gopathpkg := filepath.Join(gopath, "pkg")
97 if _, err := os.Stat(gopathpkg); err == nil && gopath != "" {
98 if err := writeDLLs(gopathpkg); err == nil {
99 return gopathpkg, nil
100 }
101 }
102 debug.Printf("DLLs could not be written to GOPATH")
103
104 // Third, pick a temporary directory.
105 tmp := os.TempDir()
106 if err := writeDLLs(tmp); err != nil {
107 return "", fmt.Errorf("gl: unable to install ANGLE DLLs: %v", err)
108 }
109 return tmp, nil
110 }
111
112 func appdataPath() string {
113 return filepath.Join(os.Getenv("LOCALAPPDATA"), "GoGL", runtime.GOARCH)
114 }
115
116 func containsDLLs(dir string) bool {
117 compatible := func(name string) bool {
118 file, err := pe.Open(filepath.Join(dir, name))
119 if err != nil {
120 return false
121 }
122 defer file.Close()
123
124 switch file.Machine {
125 case pe.IMAGE_FILE_MACHINE_AMD64:
126 return "amd64" == runtime.GOARCH
127 case pe.IMAGE_FILE_MACHINE_ARM:
128 return "arm" == runtime.GOARCH
129 case pe.IMAGE_FILE_MACHINE_I386:
130 return "386" == runtime.GOARCH
131 }
132 return false
133 }
134
135 return compatible("libglesv2.dll") && compatible("libegl.dll") && compatible("d3dcompiler_47.dll")
136 }
137
138 func chromePath() string {
139 // dlls are stored in:
140 // <BASE>/<VERSION>/libglesv2.dll
141
142 var installdirs = []string{
143 // Chrome User
144 filepath.Join(os.Getenv("LOCALAPPDATA"), "Google", "Chrome", "Application"),
145 // Chrome System
146 filepath.Join(os.Getenv("ProgramFiles(x86)"), "Google", "Chrome", "Application"),
147 // Chromium
148 filepath.Join(os.Getenv("LOCALAPPDATA"), "Chromium", "Application"),
149 // Chrome Canary
150 filepath.Join(os.Getenv("LOCALAPPDATA"), "Google", "Chrome SxS", "Application"),
151 }
152
153 for _, installdir := range installdirs {
154 versiondirs, err := os.ReadDir(installdir)
155 if err != nil {
156 continue
157 }
158
159 for _, versiondir := range versiondirs {
160 if !versiondir.IsDir() {
161 continue
162 }
163
164 versionpath := filepath.Join(installdir, versiondir.Name())
165 if containsDLLs(versionpath) {
166 return versionpath
167 }
168 }
169 }
170
171 return ""
172 }
173
174 func findDLLs() (err error) {
175 load := func(path string) (bool, error) {
176 if path != "" {
177 // don't try to start when one of the files is missing
178 if !containsDLLs(path) {
179 return false, nil
180 }
181
182 LibD3DCompiler.Name = filepath.Join(path, filepath.Base(LibD3DCompiler.Name))
183 LibGLESv2.Name = filepath.Join(path, filepath.Base(LibGLESv2.Name))
184 LibEGL.Name = filepath.Join(path, filepath.Base(LibEGL.Name))
185 }
186
187 if err := LibGLESv2.Load(); err == nil {
188 if err := LibEGL.Load(); err != nil {
189 return false, fmt.Errorf("gl: loaded libglesv2 but not libegl: %v", err)
190 }
191 if err := LibD3DCompiler.Load(); err != nil {
192 return false, fmt.Errorf("gl: loaded libglesv2, libegl but not d3dcompiler: %v", err)
193 }
194 if path == "" {
195 debug.Printf("DLLs found")
196 } else {
197 debug.Printf("DLLs found in: %q", path)
198 }
199 return true, nil
200 }
201
202 return false, nil
203 }
204
205 // Look in the system directory.
206 if ok, err := load(""); ok || err != nil {
207 return err
208 }
209
210 // Look in the AppData directory.
211 if ok, err := load(appdataPath()); ok || err != nil {
212 return err
213 }
214
215 // Look for a Chrome installation
216 if dir := chromePath(); dir != "" {
217 if ok, err := load(dir); ok || err != nil {
218 return err
219 }
220 }
221
222 // Look in GOPATH/pkg.
223 if ok, err := load(filepath.Join(os.Getenv("GOPATH"), "pkg")); ok || err != nil {
224 return err
225 }
226
227 // Look in temporary directory.
228 if ok, err := load(os.TempDir()); ok || err != nil {
229 return err
230 }
231
232 // Download the DLL binary.
233 path, err := downloadDLLs()
234 if err != nil {
235 return err
236 }
237 debug.Printf("DLLs written to %s", path)
238 if ok, err := load(path); !ok || err != nil {
239 return fmt.Errorf("gl: unable to load ANGLE after installation: %v", err)
240 }
241 return nil
242 }