1use std::collections::HashMap;
4use std::sync::LazyLock;
5
6use crate::Target;
7
8static GO_STDLIB_COMMON: LazyLock<HashMap<&str, &str>> = LazyLock::new(|| {
9 HashMap::from([
10 ("archive/tar", include_str!("../typedefs/archive/tar.d.lis")),
11 ("archive/zip", include_str!("../typedefs/archive/zip.d.lis")),
12 ("bufio", include_str!("../typedefs/bufio.d.lis")),
13 ("bytes", include_str!("../typedefs/bytes.d.lis")),
14 ("cmp", include_str!("../typedefs/cmp.d.lis")),
15 (
16 "compress/bzip2",
17 include_str!("../typedefs/compress/bzip2.d.lis"),
18 ),
19 (
20 "compress/flate",
21 include_str!("../typedefs/compress/flate.d.lis"),
22 ),
23 (
24 "compress/gzip",
25 include_str!("../typedefs/compress/gzip.d.lis"),
26 ),
27 (
28 "compress/lzw",
29 include_str!("../typedefs/compress/lzw.d.lis"),
30 ),
31 (
32 "compress/zlib",
33 include_str!("../typedefs/compress/zlib.d.lis"),
34 ),
35 (
36 "container/heap",
37 include_str!("../typedefs/container/heap.d.lis"),
38 ),
39 (
40 "container/list",
41 include_str!("../typedefs/container/list.d.lis"),
42 ),
43 (
44 "container/ring",
45 include_str!("../typedefs/container/ring.d.lis"),
46 ),
47 ("context", include_str!("../typedefs/context.d.lis")),
48 ("crypto", include_str!("../typedefs/crypto.d.lis")),
49 ("crypto/aes", include_str!("../typedefs/crypto/aes.d.lis")),
50 (
51 "crypto/cipher",
52 include_str!("../typedefs/crypto/cipher.d.lis"),
53 ),
54 ("crypto/des", include_str!("../typedefs/crypto/des.d.lis")),
55 ("crypto/dsa", include_str!("../typedefs/crypto/dsa.d.lis")),
56 ("crypto/ecdh", include_str!("../typedefs/crypto/ecdh.d.lis")),
57 (
58 "crypto/ecdsa",
59 include_str!("../typedefs/crypto/ecdsa.d.lis"),
60 ),
61 (
62 "crypto/ed25519",
63 include_str!("../typedefs/crypto/ed25519.d.lis"),
64 ),
65 (
66 "crypto/elliptic",
67 include_str!("../typedefs/crypto/elliptic.d.lis"),
68 ),
69 (
70 "crypto/fips140",
71 include_str!("../typedefs/crypto/fips140.d.lis"),
72 ),
73 ("crypto/hkdf", include_str!("../typedefs/crypto/hkdf.d.lis")),
74 ("crypto/hmac", include_str!("../typedefs/crypto/hmac.d.lis")),
75 ("crypto/md5", include_str!("../typedefs/crypto/md5.d.lis")),
76 (
77 "crypto/mlkem",
78 include_str!("../typedefs/crypto/mlkem.d.lis"),
79 ),
80 (
81 "crypto/pbkdf2",
82 include_str!("../typedefs/crypto/pbkdf2.d.lis"),
83 ),
84 ("crypto/rand", include_str!("../typedefs/crypto/rand.d.lis")),
85 ("crypto/rc4", include_str!("../typedefs/crypto/rc4.d.lis")),
86 ("crypto/rsa", include_str!("../typedefs/crypto/rsa.d.lis")),
87 ("crypto/sha1", include_str!("../typedefs/crypto/sha1.d.lis")),
88 (
89 "crypto/sha256",
90 include_str!("../typedefs/crypto/sha256.d.lis"),
91 ),
92 ("crypto/sha3", include_str!("../typedefs/crypto/sha3.d.lis")),
93 (
94 "crypto/sha512",
95 include_str!("../typedefs/crypto/sha512.d.lis"),
96 ),
97 (
98 "crypto/subtle",
99 include_str!("../typedefs/crypto/subtle.d.lis"),
100 ),
101 ("crypto/tls", include_str!("../typedefs/crypto/tls.d.lis")),
102 ("crypto/x509", include_str!("../typedefs/crypto/x509.d.lis")),
103 (
104 "crypto/x509/pkix",
105 include_str!("../typedefs/crypto/x509/pkix.d.lis"),
106 ),
107 (
108 "database/sql",
109 include_str!("../typedefs/database/sql.d.lis"),
110 ),
111 (
112 "database/sql/driver",
113 include_str!("../typedefs/database/sql/driver.d.lis"),
114 ),
115 (
116 "debug/buildinfo",
117 include_str!("../typedefs/debug/buildinfo.d.lis"),
118 ),
119 ("debug/dwarf", include_str!("../typedefs/debug/dwarf.d.lis")),
120 ("debug/elf", include_str!("../typedefs/debug/elf.d.lis")),
121 ("debug/gosym", include_str!("../typedefs/debug/gosym.d.lis")),
122 ("debug/macho", include_str!("../typedefs/debug/macho.d.lis")),
123 ("debug/pe", include_str!("../typedefs/debug/pe.d.lis")),
124 (
125 "debug/plan9obj",
126 include_str!("../typedefs/debug/plan9obj.d.lis"),
127 ),
128 ("embed", include_str!("../typedefs/embed.d.lis")),
129 ("encoding", include_str!("../typedefs/encoding.d.lis")),
130 (
131 "encoding/ascii85",
132 include_str!("../typedefs/encoding/ascii85.d.lis"),
133 ),
134 (
135 "encoding/asn1",
136 include_str!("../typedefs/encoding/asn1.d.lis"),
137 ),
138 (
139 "encoding/base32",
140 include_str!("../typedefs/encoding/base32.d.lis"),
141 ),
142 (
143 "encoding/base64",
144 include_str!("../typedefs/encoding/base64.d.lis"),
145 ),
146 (
147 "encoding/binary",
148 include_str!("../typedefs/encoding/binary.d.lis"),
149 ),
150 (
151 "encoding/csv",
152 include_str!("../typedefs/encoding/csv.d.lis"),
153 ),
154 (
155 "encoding/gob",
156 include_str!("../typedefs/encoding/gob.d.lis"),
157 ),
158 (
159 "encoding/hex",
160 include_str!("../typedefs/encoding/hex.d.lis"),
161 ),
162 (
163 "encoding/json",
164 include_str!("../typedefs/encoding/json.d.lis"),
165 ),
166 (
167 "encoding/pem",
168 include_str!("../typedefs/encoding/pem.d.lis"),
169 ),
170 (
171 "encoding/xml",
172 include_str!("../typedefs/encoding/xml.d.lis"),
173 ),
174 ("errors", include_str!("../typedefs/errors.d.lis")),
175 ("expvar", include_str!("../typedefs/expvar.d.lis")),
176 ("flag", include_str!("../typedefs/flag.d.lis")),
177 ("fmt", include_str!("../typedefs/fmt.d.lis")),
178 ("go/ast", include_str!("../typedefs/go/ast.d.lis")),
179 ("go/build", include_str!("../typedefs/go/build.d.lis")),
180 (
181 "go/build/constraint",
182 include_str!("../typedefs/go/build/constraint.d.lis"),
183 ),
184 ("go/constant", include_str!("../typedefs/go/constant.d.lis")),
185 ("go/doc", include_str!("../typedefs/go/doc.d.lis")),
186 (
187 "go/doc/comment",
188 include_str!("../typedefs/go/doc/comment.d.lis"),
189 ),
190 ("go/format", include_str!("../typedefs/go/format.d.lis")),
191 ("go/importer", include_str!("../typedefs/go/importer.d.lis")),
192 ("go/parser", include_str!("../typedefs/go/parser.d.lis")),
193 ("go/printer", include_str!("../typedefs/go/printer.d.lis")),
194 ("go/scanner", include_str!("../typedefs/go/scanner.d.lis")),
195 ("go/token", include_str!("../typedefs/go/token.d.lis")),
196 ("go/types", include_str!("../typedefs/go/types.d.lis")),
197 ("go/version", include_str!("../typedefs/go/version.d.lis")),
198 ("hash", include_str!("../typedefs/hash.d.lis")),
199 (
200 "hash/adler32",
201 include_str!("../typedefs/hash/adler32.d.lis"),
202 ),
203 ("hash/crc32", include_str!("../typedefs/hash/crc32.d.lis")),
204 ("hash/crc64", include_str!("../typedefs/hash/crc64.d.lis")),
205 ("hash/fnv", include_str!("../typedefs/hash/fnv.d.lis")),
206 (
207 "hash/maphash",
208 include_str!("../typedefs/hash/maphash.d.lis"),
209 ),
210 ("html", include_str!("../typedefs/html.d.lis")),
211 (
212 "html/template",
213 include_str!("../typedefs/html/template.d.lis"),
214 ),
215 ("image", include_str!("../typedefs/image.d.lis")),
216 ("image/color", include_str!("../typedefs/image/color.d.lis")),
217 (
218 "image/color/palette",
219 include_str!("../typedefs/image/color/palette.d.lis"),
220 ),
221 ("image/draw", include_str!("../typedefs/image/draw.d.lis")),
222 ("image/gif", include_str!("../typedefs/image/gif.d.lis")),
223 ("image/jpeg", include_str!("../typedefs/image/jpeg.d.lis")),
224 ("image/png", include_str!("../typedefs/image/png.d.lis")),
225 (
226 "index/suffixarray",
227 include_str!("../typedefs/index/suffixarray.d.lis"),
228 ),
229 ("io", include_str!("../typedefs/io.d.lis")),
230 ("io/fs", include_str!("../typedefs/io/fs.d.lis")),
231 ("io/ioutil", include_str!("../typedefs/io/ioutil.d.lis")),
232 ("iter", include_str!("../typedefs/iter.d.lis")),
233 ("log", include_str!("../typedefs/log.d.lis")),
234 ("log/slog", include_str!("../typedefs/log/slog.d.lis")),
235 ("log/syslog", include_str!("../typedefs/log/syslog.d.lis")),
236 ("maps", include_str!("../typedefs/maps.d.lis")),
237 ("math", include_str!("../typedefs/math.d.lis")),
238 ("math/big", include_str!("../typedefs/math/big.d.lis")),
239 ("math/bits", include_str!("../typedefs/math/bits.d.lis")),
240 ("math/cmplx", include_str!("../typedefs/math/cmplx.d.lis")),
241 ("math/rand", include_str!("../typedefs/math/rand.d.lis")),
242 (
243 "math/rand/v2",
244 include_str!("../typedefs/math/rand/v2.d.lis"),
245 ),
246 ("mime", include_str!("../typedefs/mime.d.lis")),
247 (
248 "mime/multipart",
249 include_str!("../typedefs/mime/multipart.d.lis"),
250 ),
251 (
252 "mime/quotedprintable",
253 include_str!("../typedefs/mime/quotedprintable.d.lis"),
254 ),
255 ("net", include_str!("../typedefs/net.d.lis")),
256 ("net/http", include_str!("../typedefs/net/http.d.lis")),
257 (
258 "net/http/cgi",
259 include_str!("../typedefs/net/http/cgi.d.lis"),
260 ),
261 (
262 "net/http/cookiejar",
263 include_str!("../typedefs/net/http/cookiejar.d.lis"),
264 ),
265 (
266 "net/http/fcgi",
267 include_str!("../typedefs/net/http/fcgi.d.lis"),
268 ),
269 (
270 "net/http/httptest",
271 include_str!("../typedefs/net/http/httptest.d.lis"),
272 ),
273 (
274 "net/http/httptrace",
275 include_str!("../typedefs/net/http/httptrace.d.lis"),
276 ),
277 (
278 "net/http/httputil",
279 include_str!("../typedefs/net/http/httputil.d.lis"),
280 ),
281 (
282 "net/http/pprof",
283 include_str!("../typedefs/net/http/pprof.d.lis"),
284 ),
285 ("net/mail", include_str!("../typedefs/net/mail.d.lis")),
286 ("net/netip", include_str!("../typedefs/net/netip.d.lis")),
287 ("net/rpc", include_str!("../typedefs/net/rpc.d.lis")),
288 (
289 "net/rpc/jsonrpc",
290 include_str!("../typedefs/net/rpc/jsonrpc.d.lis"),
291 ),
292 ("net/smtp", include_str!("../typedefs/net/smtp.d.lis")),
293 (
294 "net/textproto",
295 include_str!("../typedefs/net/textproto.d.lis"),
296 ),
297 ("net/url", include_str!("../typedefs/net/url.d.lis")),
298 ("os/signal", include_str!("../typedefs/os/signal.d.lis")),
299 ("os/user", include_str!("../typedefs/os/user.d.lis")),
300 ("path", include_str!("../typedefs/path.d.lis")),
301 ("plugin", include_str!("../typedefs/plugin.d.lis")),
302 ("reflect", include_str!("../typedefs/reflect.d.lis")),
303 ("regexp", include_str!("../typedefs/regexp.d.lis")),
304 (
305 "regexp/syntax",
306 include_str!("../typedefs/regexp/syntax.d.lis"),
307 ),
308 (
309 "runtime/coverage",
310 include_str!("../typedefs/runtime/coverage.d.lis"),
311 ),
312 (
313 "runtime/debug",
314 include_str!("../typedefs/runtime/debug.d.lis"),
315 ),
316 (
317 "runtime/metrics",
318 include_str!("../typedefs/runtime/metrics.d.lis"),
319 ),
320 (
321 "runtime/pprof",
322 include_str!("../typedefs/runtime/pprof.d.lis"),
323 ),
324 (
325 "runtime/race",
326 include_str!("../typedefs/runtime/race.d.lis"),
327 ),
328 (
329 "runtime/trace",
330 include_str!("../typedefs/runtime/trace.d.lis"),
331 ),
332 ("slices", include_str!("../typedefs/slices.d.lis")),
333 ("sort", include_str!("../typedefs/sort.d.lis")),
334 ("strconv", include_str!("../typedefs/strconv.d.lis")),
335 ("strings", include_str!("../typedefs/strings.d.lis")),
336 ("structs", include_str!("../typedefs/structs.d.lis")),
337 ("sync", include_str!("../typedefs/sync.d.lis")),
338 ("sync/atomic", include_str!("../typedefs/sync/atomic.d.lis")),
339 ("testing", include_str!("../typedefs/testing.d.lis")),
340 (
341 "testing/fstest",
342 include_str!("../typedefs/testing/fstest.d.lis"),
343 ),
344 (
345 "testing/iotest",
346 include_str!("../typedefs/testing/iotest.d.lis"),
347 ),
348 (
349 "testing/quick",
350 include_str!("../typedefs/testing/quick.d.lis"),
351 ),
352 (
353 "testing/slogtest",
354 include_str!("../typedefs/testing/slogtest.d.lis"),
355 ),
356 (
357 "testing/synctest",
358 include_str!("../typedefs/testing/synctest.d.lis"),
359 ),
360 (
361 "text/scanner",
362 include_str!("../typedefs/text/scanner.d.lis"),
363 ),
364 (
365 "text/tabwriter",
366 include_str!("../typedefs/text/tabwriter.d.lis"),
367 ),
368 (
369 "text/template",
370 include_str!("../typedefs/text/template.d.lis"),
371 ),
372 (
373 "text/template/parse",
374 include_str!("../typedefs/text/template/parse.d.lis"),
375 ),
376 ("time", include_str!("../typedefs/time.d.lis")),
377 ("time/tzdata", include_str!("../typedefs/time/tzdata.d.lis")),
378 ("unicode", include_str!("../typedefs/unicode.d.lis")),
379 (
380 "unicode/utf16",
381 include_str!("../typedefs/unicode/utf16.d.lis"),
382 ),
383 (
384 "unicode/utf8",
385 include_str!("../typedefs/unicode/utf8.d.lis"),
386 ),
387 ("unique", include_str!("../typedefs/unique.d.lis")),
388 ("unsafe", include_str!("../typedefs/unsafe.d.lis")),
389 ("weak", include_str!("../typedefs/weak.d.lis")),
390 ])
391});
392
393static GO_STDLIB_LINUX_AMD64: LazyLock<HashMap<&str, &str>> = LazyLock::new(|| {
394 HashMap::from([
395 ("os", include_str!("../typedefs/os_linux_amd64.d.lis")),
396 (
397 "os/exec",
398 include_str!("../typedefs/os/exec_linux_amd64.d.lis"),
399 ),
400 (
401 "path/filepath",
402 include_str!("../typedefs/path/filepath_linux_amd64.d.lis"),
403 ),
404 (
405 "runtime",
406 include_str!("../typedefs/runtime_linux_amd64.d.lis"),
407 ),
408 (
409 "syscall",
410 include_str!("../typedefs/syscall_linux_amd64.d.lis"),
411 ),
412 ])
413});
414
415static GO_STDLIB_LINUX_ARM64: LazyLock<HashMap<&str, &str>> = LazyLock::new(|| {
416 HashMap::from([
417 ("os", include_str!("../typedefs/os_linux_amd64.d.lis")),
418 (
419 "os/exec",
420 include_str!("../typedefs/os/exec_linux_amd64.d.lis"),
421 ),
422 (
423 "path/filepath",
424 include_str!("../typedefs/path/filepath_linux_amd64.d.lis"),
425 ),
426 (
427 "runtime",
428 include_str!("../typedefs/runtime_linux_arm64.d.lis"),
429 ),
430 (
431 "syscall",
432 include_str!("../typedefs/syscall_linux_arm64.d.lis"),
433 ),
434 ])
435});
436
437static GO_STDLIB_DARWIN_AMD64: LazyLock<HashMap<&str, &str>> = LazyLock::new(|| {
438 HashMap::from([
439 ("os", include_str!("../typedefs/os_darwin_amd64.d.lis")),
440 (
441 "os/exec",
442 include_str!("../typedefs/os/exec_linux_amd64.d.lis"),
443 ),
444 (
445 "path/filepath",
446 include_str!("../typedefs/path/filepath_linux_amd64.d.lis"),
447 ),
448 (
449 "runtime",
450 include_str!("../typedefs/runtime_darwin_amd64.d.lis"),
451 ),
452 (
453 "syscall",
454 include_str!("../typedefs/syscall_darwin_amd64.d.lis"),
455 ),
456 ])
457});
458
459static GO_STDLIB_DARWIN_ARM64: LazyLock<HashMap<&str, &str>> = LazyLock::new(|| {
460 HashMap::from([
461 ("os", include_str!("../typedefs/os_darwin_amd64.d.lis")),
462 (
463 "os/exec",
464 include_str!("../typedefs/os/exec_linux_amd64.d.lis"),
465 ),
466 (
467 "path/filepath",
468 include_str!("../typedefs/path/filepath_linux_amd64.d.lis"),
469 ),
470 (
471 "runtime",
472 include_str!("../typedefs/runtime_darwin_arm64.d.lis"),
473 ),
474 (
475 "syscall",
476 include_str!("../typedefs/syscall_darwin_arm64.d.lis"),
477 ),
478 ])
479});
480
481static GO_STDLIB_WINDOWS_AMD64: LazyLock<HashMap<&str, &str>> = LazyLock::new(|| {
482 HashMap::from([
483 ("os", include_str!("../typedefs/os_windows_amd64.d.lis")),
484 (
485 "os/exec",
486 include_str!("../typedefs/os/exec_windows_amd64.d.lis"),
487 ),
488 (
489 "path/filepath",
490 include_str!("../typedefs/path/filepath_windows_amd64.d.lis"),
491 ),
492 (
493 "runtime",
494 include_str!("../typedefs/runtime_windows_amd64.d.lis"),
495 ),
496 (
497 "syscall",
498 include_str!("../typedefs/syscall_windows_amd64.d.lis"),
499 ),
500 ])
501});
502
503static GO_STDLIB_PACKAGE_TARGETS: LazyLock<HashMap<&str, &[(&str, &str)]>> = LazyLock::new(|| {
504 HashMap::from([(
505 "log/syslog",
506 &[
507 ("linux", "amd64"),
508 ("linux", "arm64"),
509 ("darwin", "amd64"),
510 ("darwin", "arm64"),
511 ][..],
512 )])
513});
514
515pub fn get_go_stdlib_typedef(package: &str, target: Target) -> Option<&'static str> {
516 if !is_available_on(package, target) {
517 return None;
518 }
519 if let Some(source) = lookup_in_overlay(target, package) {
520 return Some(source);
521 }
522 GO_STDLIB_COMMON.get(package).copied()
523}
524
525pub fn get_go_stdlib_packages(target: Target) -> Vec<&'static str> {
526 let mut packages: Vec<&'static str> = GO_STDLIB_COMMON
527 .keys()
528 .copied()
529 .filter(|pkg| is_available_on(pkg, target))
530 .collect();
531 if let Some(overlay) = overlay_for(target) {
532 packages.extend(overlay.keys().copied());
533 }
534 packages.sort();
535 packages
536}
537
538pub fn get_go_stdlib_package_targets(
539 package: &str,
540) -> Option<&'static [(&'static str, &'static str)]> {
541 GO_STDLIB_PACKAGE_TARGETS.get(package).copied()
542}
543
544fn is_available_on(package: &str, target: Target) -> bool {
545 match GO_STDLIB_PACKAGE_TARGETS.get(package) {
546 Some(targets) => targets
547 .iter()
548 .any(|(goos, goarch)| *goos == target.goos && *goarch == target.goarch),
549 None => true,
550 }
551}
552
553fn lookup_in_overlay(target: Target, package: &str) -> Option<&'static str> {
554 overlay_for(target).and_then(|overlay| overlay.get(package).copied())
555}
556
557fn overlay_for(target: Target) -> Option<&'static HashMap<&'static str, &'static str>> {
558 match (target.goos, target.goarch) {
559 ("linux", "amd64") => Some(&GO_STDLIB_LINUX_AMD64),
560 ("linux", "arm64") => Some(&GO_STDLIB_LINUX_ARM64),
561 ("darwin", "amd64") => Some(&GO_STDLIB_DARWIN_AMD64),
562 ("darwin", "arm64") => Some(&GO_STDLIB_DARWIN_ARM64),
563 ("windows", "amd64") => Some(&GO_STDLIB_WINDOWS_AMD64),
564 _ => None,
565 }
566}