Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +17 -0
- platform/dbops/binaries/go/go/lib/time/README +10 -0
- platform/dbops/binaries/go/go/lib/time/mkzip.go +94 -0
- platform/dbops/binaries/go/go/lib/time/update.bash +86 -0
- platform/dbops/binaries/go/go/lib/time/zoneinfo.zip +3 -0
- platform/dbops/binaries/go/go/misc/cgo/gmp/fib.go +45 -0
- platform/dbops/binaries/go/go/misc/cgo/gmp/gmp.go +379 -0
- platform/dbops/binaries/go/go/misc/cgo/gmp/pi.go +73 -0
- platform/dbops/binaries/go/go/misc/chrome/gophertool/README.txt +8 -0
- platform/dbops/binaries/go/go/misc/chrome/gophertool/background.html +12 -0
- platform/dbops/binaries/go/go/misc/chrome/gophertool/background.js +9 -0
- platform/dbops/binaries/go/go/misc/chrome/gophertool/gopher.js +41 -0
- platform/dbops/binaries/go/go/misc/chrome/gophertool/gopher.png +3 -0
- platform/dbops/binaries/go/go/misc/chrome/gophertool/manifest.json +20 -0
- platform/dbops/binaries/go/go/misc/chrome/gophertool/popup.html +21 -0
- platform/dbops/binaries/go/go/misc/chrome/gophertool/popup.js +46 -0
- platform/dbops/binaries/go/go/misc/go_android_exec/README +25 -0
- platform/dbops/binaries/go/go/misc/go_android_exec/exitcode_test.go +76 -0
- platform/dbops/binaries/go/go/misc/go_android_exec/main.go +527 -0
- platform/dbops/binaries/go/go/misc/ios/README +57 -0
- platform/dbops/binaries/go/go/misc/ios/clangwrap.sh +20 -0
- platform/dbops/binaries/go/go/misc/ios/detect.go +133 -0
- platform/dbops/binaries/go/go/misc/ios/go_ios_exec.go +911 -0
- platform/dbops/binaries/go/go/misc/linkcheck/linkcheck.go +191 -0
- platform/dbops/binaries/go/go/misc/wasm/go_js_wasm_exec +17 -0
- platform/dbops/binaries/go/go/misc/wasm/go_wasip1_wasm_exec +31 -0
- platform/dbops/binaries/go/go/misc/wasm/wasm_exec.html +49 -0
- platform/dbops/binaries/go/go/misc/wasm/wasm_exec.js +561 -0
- platform/dbops/binaries/go/go/misc/wasm/wasm_exec_node.js +39 -0
- platform/dbops/binaries/go/go/pkg/include/asm_amd64.h +28 -0
- platform/dbops/binaries/go/go/pkg/include/asm_ppc64x.h +55 -0
- platform/dbops/binaries/go/go/pkg/include/funcdata.h +56 -0
- platform/dbops/binaries/go/go/pkg/include/textflag.h +38 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/addr2line +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/asm +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/buildid +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/cgo +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/compile +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/covdata +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/cover +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/doc +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/fix +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/link +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/nm +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/objdump +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/pack +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/pprof +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/test2json +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/trace +3 -0
- platform/dbops/binaries/go/go/pkg/tool/linux_amd64/vet +3 -0
.gitattributes
CHANGED
|
@@ -3135,3 +3135,20 @@ platform/dbops/binaries/build/bin/bison filter=lfs diff=lfs merge=lfs -text
|
|
| 3135 |
platform/dbops/binaries/build/bin/flex filter=lfs diff=lfs merge=lfs -text
|
| 3136 |
platform/dbops/binaries/build/bin/flex++ filter=lfs diff=lfs merge=lfs -text
|
| 3137 |
platform/dbops/binaries/weaviate-src/adapters/repos/db/vector/hnsw/compression_tests/fixtures/restart-from-zero-segments/1234567 filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3135 |
platform/dbops/binaries/build/bin/flex filter=lfs diff=lfs merge=lfs -text
|
| 3136 |
platform/dbops/binaries/build/bin/flex++ filter=lfs diff=lfs merge=lfs -text
|
| 3137 |
platform/dbops/binaries/weaviate-src/adapters/repos/db/vector/hnsw/compression_tests/fixtures/restart-from-zero-segments/1234567 filter=lfs diff=lfs merge=lfs -text
|
| 3138 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/addr2line filter=lfs diff=lfs merge=lfs -text
|
| 3139 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/asm filter=lfs diff=lfs merge=lfs -text
|
| 3140 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/buildid filter=lfs diff=lfs merge=lfs -text
|
| 3141 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/cgo filter=lfs diff=lfs merge=lfs -text
|
| 3142 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/compile filter=lfs diff=lfs merge=lfs -text
|
| 3143 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/covdata filter=lfs diff=lfs merge=lfs -text
|
| 3144 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/cover filter=lfs diff=lfs merge=lfs -text
|
| 3145 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/doc filter=lfs diff=lfs merge=lfs -text
|
| 3146 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/fix filter=lfs diff=lfs merge=lfs -text
|
| 3147 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/link filter=lfs diff=lfs merge=lfs -text
|
| 3148 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/nm filter=lfs diff=lfs merge=lfs -text
|
| 3149 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/objdump filter=lfs diff=lfs merge=lfs -text
|
| 3150 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/pack filter=lfs diff=lfs merge=lfs -text
|
| 3151 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/pprof filter=lfs diff=lfs merge=lfs -text
|
| 3152 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/test2json filter=lfs diff=lfs merge=lfs -text
|
| 3153 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/trace filter=lfs diff=lfs merge=lfs -text
|
| 3154 |
+
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/vet filter=lfs diff=lfs merge=lfs -text
|
platform/dbops/binaries/go/go/lib/time/README
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
The zoneinfo.zip archive contains time zone files compiled using
|
| 2 |
+
the code and data maintained as part of the IANA Time Zone Database.
|
| 3 |
+
The IANA asserts that the database is in the public domain.
|
| 4 |
+
|
| 5 |
+
For more information, see
|
| 6 |
+
https://www.iana.org/time-zones
|
| 7 |
+
ftp://ftp.iana.org/tz/code/tz-link.html
|
| 8 |
+
https://datatracker.ietf.org/doc/html/rfc6557
|
| 9 |
+
|
| 10 |
+
To rebuild the archive, read and run update.bash.
|
platform/dbops/binaries/go/go/lib/time/mkzip.go
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2022 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 |
+
//go:build ignore
|
| 6 |
+
|
| 7 |
+
// Mkzip writes a zoneinfo.zip with the content of the current directory
|
| 8 |
+
// and its subdirectories, with no compression, suitable for package time.
|
| 9 |
+
//
|
| 10 |
+
// Usage:
|
| 11 |
+
//
|
| 12 |
+
// go run ../../mkzip.go ../../zoneinfo.zip
|
| 13 |
+
//
|
| 14 |
+
// We use this program instead of 'zip -0 -r ../../zoneinfo.zip *' to get
|
| 15 |
+
// a reproducible generator that does not depend on which version of the
|
| 16 |
+
// external zip tool is used or the ordering of file names in a directory
|
| 17 |
+
// or the current time.
|
| 18 |
+
package main
|
| 19 |
+
|
| 20 |
+
import (
|
| 21 |
+
"archive/zip"
|
| 22 |
+
"bytes"
|
| 23 |
+
"flag"
|
| 24 |
+
"fmt"
|
| 25 |
+
"hash/crc32"
|
| 26 |
+
"io/fs"
|
| 27 |
+
"log"
|
| 28 |
+
"os"
|
| 29 |
+
"path/filepath"
|
| 30 |
+
"strings"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
func usage() {
|
| 34 |
+
fmt.Fprintf(os.Stderr, "usage: go run mkzip.go ../../zoneinfo.zip\n")
|
| 35 |
+
os.Exit(2)
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
func main() {
|
| 39 |
+
log.SetPrefix("mkzip: ")
|
| 40 |
+
log.SetFlags(0)
|
| 41 |
+
flag.Usage = usage
|
| 42 |
+
flag.Parse()
|
| 43 |
+
args := flag.Args()
|
| 44 |
+
if len(args) != 1 || !strings.HasSuffix(args[0], ".zip") {
|
| 45 |
+
usage()
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
var zb bytes.Buffer
|
| 49 |
+
zw := zip.NewWriter(&zb)
|
| 50 |
+
seen := make(map[string]bool)
|
| 51 |
+
err := filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
|
| 52 |
+
if d.IsDir() {
|
| 53 |
+
return nil
|
| 54 |
+
}
|
| 55 |
+
data, err := os.ReadFile(path)
|
| 56 |
+
if err != nil {
|
| 57 |
+
log.Fatal(err)
|
| 58 |
+
}
|
| 59 |
+
if strings.HasSuffix(path, ".zip") {
|
| 60 |
+
log.Fatalf("unexpected file during walk: %s", path)
|
| 61 |
+
}
|
| 62 |
+
name := filepath.ToSlash(path)
|
| 63 |
+
w, err := zw.CreateRaw(&zip.FileHeader{
|
| 64 |
+
Name: name,
|
| 65 |
+
Method: zip.Store,
|
| 66 |
+
CompressedSize64: uint64(len(data)),
|
| 67 |
+
UncompressedSize64: uint64(len(data)),
|
| 68 |
+
CRC32: crc32.ChecksumIEEE(data),
|
| 69 |
+
})
|
| 70 |
+
if err != nil {
|
| 71 |
+
log.Fatal(err)
|
| 72 |
+
}
|
| 73 |
+
if _, err := w.Write(data); err != nil {
|
| 74 |
+
log.Fatal(err)
|
| 75 |
+
}
|
| 76 |
+
seen[name] = true
|
| 77 |
+
return nil
|
| 78 |
+
})
|
| 79 |
+
if err != nil {
|
| 80 |
+
log.Fatal(err)
|
| 81 |
+
}
|
| 82 |
+
if err := zw.Close(); err != nil {
|
| 83 |
+
log.Fatal(err)
|
| 84 |
+
}
|
| 85 |
+
if len(seen) == 0 {
|
| 86 |
+
log.Fatalf("did not find any files to add")
|
| 87 |
+
}
|
| 88 |
+
if !seen["US/Eastern"] {
|
| 89 |
+
log.Fatalf("did not find US/Eastern to add")
|
| 90 |
+
}
|
| 91 |
+
if err := os.WriteFile(args[0], zb.Bytes(), 0666); err != nil {
|
| 92 |
+
log.Fatal(err)
|
| 93 |
+
}
|
| 94 |
+
}
|
platform/dbops/binaries/go/go/lib/time/update.bash
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
# Copyright 2012 The Go Authors. All rights reserved.
|
| 3 |
+
# Use of this source code is governed by a BSD-style
|
| 4 |
+
# license that can be found in the LICENSE file.
|
| 5 |
+
|
| 6 |
+
# This script rebuilds the time zone files using files
|
| 7 |
+
# downloaded from the ICANN/IANA distribution.
|
| 8 |
+
#
|
| 9 |
+
# To prepare an update for a new Go release,
|
| 10 |
+
# consult https://www.iana.org/time-zones for the latest versions,
|
| 11 |
+
# update CODE and DATA below, and then run
|
| 12 |
+
#
|
| 13 |
+
# ./update.bash -commit
|
| 14 |
+
#
|
| 15 |
+
# That will prepare the files and create the commit.
|
| 16 |
+
#
|
| 17 |
+
# To review such a commit (as the reviewer), use:
|
| 18 |
+
#
|
| 19 |
+
# git codereview change NNNNNN # CL number
|
| 20 |
+
# cd lib/time
|
| 21 |
+
# ./update.bash
|
| 22 |
+
#
|
| 23 |
+
# If it prints "No updates needed.", then the generated files
|
| 24 |
+
# in the CL match the update.bash in the CL.
|
| 25 |
+
|
| 26 |
+
# Versions to use.
|
| 27 |
+
CODE=2023d
|
| 28 |
+
DATA=2023d
|
| 29 |
+
|
| 30 |
+
set -e
|
| 31 |
+
|
| 32 |
+
cd $(dirname $0)
|
| 33 |
+
rm -rf work
|
| 34 |
+
mkdir work
|
| 35 |
+
go build -o work/mkzip mkzip.go # build now for correct paths in build errors
|
| 36 |
+
cd work
|
| 37 |
+
mkdir zoneinfo
|
| 38 |
+
curl -sS -L -O https://www.iana.org/time-zones/repository/releases/tzcode$CODE.tar.gz
|
| 39 |
+
curl -sS -L -O https://www.iana.org/time-zones/repository/releases/tzdata$DATA.tar.gz
|
| 40 |
+
tar xzf tzcode$CODE.tar.gz
|
| 41 |
+
tar xzf tzdata$DATA.tar.gz
|
| 42 |
+
|
| 43 |
+
if ! make CFLAGS=-DSTD_INSPIRED AWK=awk TZDIR=zoneinfo posix_only >make.out 2>&1; then
|
| 44 |
+
cat make.out
|
| 45 |
+
exit 2
|
| 46 |
+
fi
|
| 47 |
+
|
| 48 |
+
cd zoneinfo
|
| 49 |
+
../mkzip ../../zoneinfo.zip
|
| 50 |
+
cd ../..
|
| 51 |
+
|
| 52 |
+
files="update.bash zoneinfo.zip"
|
| 53 |
+
modified=true
|
| 54 |
+
if git diff --quiet $files; then
|
| 55 |
+
modified=false
|
| 56 |
+
fi
|
| 57 |
+
|
| 58 |
+
if [ "$1" = "-work" ]; then
|
| 59 |
+
echo Left workspace behind in work/.
|
| 60 |
+
shift
|
| 61 |
+
else
|
| 62 |
+
rm -rf work
|
| 63 |
+
fi
|
| 64 |
+
|
| 65 |
+
if ! $modified; then
|
| 66 |
+
echo No updates needed.
|
| 67 |
+
exit 0
|
| 68 |
+
fi
|
| 69 |
+
|
| 70 |
+
echo Updated for $CODE/$DATA: $files
|
| 71 |
+
|
| 72 |
+
commitmsg="lib/time: update to $CODE/$DATA
|
| 73 |
+
|
| 74 |
+
Commit generated by update.bash.
|
| 75 |
+
|
| 76 |
+
For #22487.
|
| 77 |
+
"
|
| 78 |
+
|
| 79 |
+
if [ "$1" = "-commit" ]; then
|
| 80 |
+
echo "Creating commit. Run 'git reset HEAD^' to undo commit."
|
| 81 |
+
echo
|
| 82 |
+
git commit -m "$commitmsg" $files
|
| 83 |
+
echo
|
| 84 |
+
git log -n1 --stat
|
| 85 |
+
echo
|
| 86 |
+
fi
|
platform/dbops/binaries/go/go/lib/time/zoneinfo.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:19ffc039dc3fd174849b8d18b1420c48a7b0d97a1ddaaa6d14e9216da6c11597
|
| 3 |
+
size 401728
|
platform/dbops/binaries/go/go/misc/cgo/gmp/fib.go
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2009 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 |
+
//go:build ignore
|
| 6 |
+
|
| 7 |
+
// Compute Fibonacci numbers with two goroutines
|
| 8 |
+
// that pass integers back and forth. No actual
|
| 9 |
+
// concurrency, just threads and synchronization
|
| 10 |
+
// and foreign code on multiple pthreads.
|
| 11 |
+
|
| 12 |
+
package main
|
| 13 |
+
|
| 14 |
+
import (
|
| 15 |
+
big "."
|
| 16 |
+
"runtime"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
func fibber(c chan *big.Int, out chan string, n int64) {
|
| 20 |
+
// Keep the fibbers in dedicated operating system
|
| 21 |
+
// threads, so that this program tests coordination
|
| 22 |
+
// between pthreads and not just goroutines.
|
| 23 |
+
runtime.LockOSThread()
|
| 24 |
+
|
| 25 |
+
i := big.NewInt(n)
|
| 26 |
+
if n == 0 {
|
| 27 |
+
c <- i
|
| 28 |
+
}
|
| 29 |
+
for {
|
| 30 |
+
j := <-c
|
| 31 |
+
out <- j.String()
|
| 32 |
+
i.Add(i, j)
|
| 33 |
+
c <- i
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
func main() {
|
| 38 |
+
c := make(chan *big.Int)
|
| 39 |
+
out := make(chan string)
|
| 40 |
+
go fibber(c, out, 0)
|
| 41 |
+
go fibber(c, out, 1)
|
| 42 |
+
for i := 0; i < 200; i++ {
|
| 43 |
+
println(<-out)
|
| 44 |
+
}
|
| 45 |
+
}
|
platform/dbops/binaries/go/go/misc/cgo/gmp/gmp.go
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2009 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 |
+
/*
|
| 6 |
+
An example of wrapping a C library in Go. This is the GNU
|
| 7 |
+
multiprecision library gmp's integer type mpz_t wrapped to look like
|
| 8 |
+
the Go package big's integer type Int.
|
| 9 |
+
|
| 10 |
+
This is a syntactically valid Go program—it can be parsed with the Go
|
| 11 |
+
parser and processed by godoc—but it is not compiled directly by gc.
|
| 12 |
+
Instead, a separate tool, cgo, processes it to produce three output
|
| 13 |
+
files. The first two, 6g.go and 6c.c, are a Go source file for 6g and
|
| 14 |
+
a C source file for 6c; both compile as part of the named package
|
| 15 |
+
(gmp, in this example). The third, gcc.c, is a C source file for gcc;
|
| 16 |
+
it compiles into a shared object (.so) that is dynamically linked into
|
| 17 |
+
any 6.out that imports the first two files.
|
| 18 |
+
|
| 19 |
+
The stanza
|
| 20 |
+
|
| 21 |
+
// #include <gmp.h>
|
| 22 |
+
import "C"
|
| 23 |
+
|
| 24 |
+
is a signal to cgo. The doc comment on the import of "C" provides
|
| 25 |
+
additional context for the C file. Here it is just a single #include
|
| 26 |
+
but it could contain arbitrary C definitions to be imported and used.
|
| 27 |
+
|
| 28 |
+
Cgo recognizes any use of a qualified identifier C.xxx and uses gcc to
|
| 29 |
+
find the definition of xxx. If xxx is a type, cgo replaces C.xxx with
|
| 30 |
+
a Go translation. C arithmetic types translate to precisely-sized Go
|
| 31 |
+
arithmetic types. A C struct translates to a Go struct, field by
|
| 32 |
+
field; unrepresentable fields are replaced with opaque byte arrays. A
|
| 33 |
+
C union translates into a struct containing the first union member and
|
| 34 |
+
perhaps additional padding. C arrays become Go arrays. C pointers
|
| 35 |
+
become Go pointers. C function pointers become Go's uintptr.
|
| 36 |
+
C void pointers become Go's unsafe.Pointer.
|
| 37 |
+
|
| 38 |
+
For example, mpz_t is defined in <gmp.h> as:
|
| 39 |
+
|
| 40 |
+
typedef unsigned long int mp_limb_t;
|
| 41 |
+
|
| 42 |
+
typedef struct
|
| 43 |
+
{
|
| 44 |
+
int _mp_alloc;
|
| 45 |
+
int _mp_size;
|
| 46 |
+
mp_limb_t *_mp_d;
|
| 47 |
+
} __mpz_struct;
|
| 48 |
+
|
| 49 |
+
typedef __mpz_struct mpz_t[1];
|
| 50 |
+
|
| 51 |
+
Cgo generates:
|
| 52 |
+
|
| 53 |
+
type _C_int int32
|
| 54 |
+
type _C_mp_limb_t uint64
|
| 55 |
+
type _C___mpz_struct struct {
|
| 56 |
+
_mp_alloc _C_int;
|
| 57 |
+
_mp_size _C_int;
|
| 58 |
+
_mp_d *_C_mp_limb_t;
|
| 59 |
+
}
|
| 60 |
+
type _C_mpz_t [1]_C___mpz_struct
|
| 61 |
+
|
| 62 |
+
and then replaces each occurrence of a type C.xxx with _C_xxx.
|
| 63 |
+
|
| 64 |
+
If xxx is data, cgo arranges for C.xxx to refer to the C variable,
|
| 65 |
+
with the type translated as described above. To do this, cgo must
|
| 66 |
+
introduce a Go variable that points at the C variable (the linker can
|
| 67 |
+
be told to initialize this pointer). For example, if the gmp library
|
| 68 |
+
provided
|
| 69 |
+
|
| 70 |
+
mpz_t zero;
|
| 71 |
+
|
| 72 |
+
then cgo would rewrite a reference to C.zero by introducing
|
| 73 |
+
|
| 74 |
+
var _C_zero *C.mpz_t
|
| 75 |
+
|
| 76 |
+
and then replacing all instances of C.zero with (*_C_zero).
|
| 77 |
+
|
| 78 |
+
Cgo's most interesting translation is for functions. If xxx is a C
|
| 79 |
+
function, then cgo rewrites C.xxx into a new function _C_xxx that
|
| 80 |
+
calls the C xxx in a standard pthread. The new function translates
|
| 81 |
+
its arguments, calls xxx, and translates the return value.
|
| 82 |
+
|
| 83 |
+
Translation of parameters and the return value follows the type
|
| 84 |
+
translation above except that arrays passed as parameters translate
|
| 85 |
+
explicitly in Go to pointers to arrays, as they do (implicitly) in C.
|
| 86 |
+
|
| 87 |
+
Garbage collection is the big problem. It is fine for the Go world to
|
| 88 |
+
have pointers into the C world and to free those pointers when they
|
| 89 |
+
are no longer needed. To help, the Go code can define Go objects
|
| 90 |
+
holding the C pointers and use runtime.SetFinalizer on those Go objects.
|
| 91 |
+
|
| 92 |
+
It is much more difficult for the C world to have pointers into the Go
|
| 93 |
+
world, because the Go garbage collector is unaware of the memory
|
| 94 |
+
allocated by C. The most important consideration is not to
|
| 95 |
+
constrain future implementations, so the rule is that Go code can
|
| 96 |
+
hand a Go pointer to C code but must separately arrange for
|
| 97 |
+
Go to hang on to a reference to the pointer until C is done with it.
|
| 98 |
+
*/
|
| 99 |
+
package gmp
|
| 100 |
+
|
| 101 |
+
/*
|
| 102 |
+
#cgo LDFLAGS: -lgmp
|
| 103 |
+
#include <gmp.h>
|
| 104 |
+
#include <stdlib.h>
|
| 105 |
+
|
| 106 |
+
// gmp 5.0.0+ changed the type of the 3rd argument to mp_bitcnt_t,
|
| 107 |
+
// so, to support older versions, we wrap these two functions.
|
| 108 |
+
void _mpz_mul_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) {
|
| 109 |
+
mpz_mul_2exp(a, b, n);
|
| 110 |
+
}
|
| 111 |
+
void _mpz_div_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) {
|
| 112 |
+
mpz_div_2exp(a, b, n);
|
| 113 |
+
}
|
| 114 |
+
*/
|
| 115 |
+
import "C"
|
| 116 |
+
|
| 117 |
+
import (
|
| 118 |
+
"os"
|
| 119 |
+
"unsafe"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
/*
|
| 123 |
+
* one of a kind
|
| 124 |
+
*/
|
| 125 |
+
|
| 126 |
+
// An Int represents a signed multi-precision integer.
|
| 127 |
+
// The zero value for an Int represents the value 0.
|
| 128 |
+
type Int struct {
|
| 129 |
+
i C.mpz_t
|
| 130 |
+
init bool
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
// NewInt returns a new Int initialized to x.
|
| 134 |
+
func NewInt(x int64) *Int { return new(Int).SetInt64(x) }
|
| 135 |
+
|
| 136 |
+
// Int promises that the zero value is a 0, but in gmp
|
| 137 |
+
// the zero value is a crash. To bridge the gap, the
|
| 138 |
+
// init bool says whether this is a valid gmp value.
|
| 139 |
+
// doinit initializes z.i if it needs it. This is not inherent
|
| 140 |
+
// to FFI, just a mismatch between Go's convention of
|
| 141 |
+
// making zero values useful and gmp's decision not to.
|
| 142 |
+
func (z *Int) doinit() {
|
| 143 |
+
if z.init {
|
| 144 |
+
return
|
| 145 |
+
}
|
| 146 |
+
z.init = true
|
| 147 |
+
C.mpz_init(&z.i[0])
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
// Bytes returns z's representation as a big-endian byte array.
|
| 151 |
+
func (z *Int) Bytes() []byte {
|
| 152 |
+
b := make([]byte, (z.Len()+7)/8)
|
| 153 |
+
n := C.size_t(len(b))
|
| 154 |
+
C.mpz_export(unsafe.Pointer(&b[0]), &n, 1, 1, 1, 0, &z.i[0])
|
| 155 |
+
return b[0:n]
|
| 156 |
+
}
|
| 157 |
+
|
| 158 |
+
// Len returns the length of z in bits. 0 is considered to have length 1.
|
| 159 |
+
func (z *Int) Len() int {
|
| 160 |
+
z.doinit()
|
| 161 |
+
return int(C.mpz_sizeinbase(&z.i[0], 2))
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
// Set sets z = x and returns z.
|
| 165 |
+
func (z *Int) Set(x *Int) *Int {
|
| 166 |
+
z.doinit()
|
| 167 |
+
C.mpz_set(&z.i[0], &x.i[0])
|
| 168 |
+
return z
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
// SetBytes interprets b as the bytes of a big-endian integer
|
| 172 |
+
// and sets z to that value.
|
| 173 |
+
func (z *Int) SetBytes(b []byte) *Int {
|
| 174 |
+
z.doinit()
|
| 175 |
+
if len(b) == 0 {
|
| 176 |
+
z.SetInt64(0)
|
| 177 |
+
} else {
|
| 178 |
+
C.mpz_import(&z.i[0], C.size_t(len(b)), 1, 1, 1, 0, unsafe.Pointer(&b[0]))
|
| 179 |
+
}
|
| 180 |
+
return z
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
// SetInt64 sets z = x and returns z.
|
| 184 |
+
func (z *Int) SetInt64(x int64) *Int {
|
| 185 |
+
z.doinit()
|
| 186 |
+
// TODO(rsc): more work on 32-bit platforms
|
| 187 |
+
C.mpz_set_si(&z.i[0], C.long(x))
|
| 188 |
+
return z
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
// SetString interprets s as a number in the given base
|
| 192 |
+
// and sets z to that value. The base must be in the range [2,36].
|
| 193 |
+
// SetString returns an error if s cannot be parsed or the base is invalid.
|
| 194 |
+
func (z *Int) SetString(s string, base int) error {
|
| 195 |
+
z.doinit()
|
| 196 |
+
if base < 2 || base > 36 {
|
| 197 |
+
return os.ErrInvalid
|
| 198 |
+
}
|
| 199 |
+
p := C.CString(s)
|
| 200 |
+
defer C.free(unsafe.Pointer(p))
|
| 201 |
+
if C.mpz_set_str(&z.i[0], p, C.int(base)) < 0 {
|
| 202 |
+
return os.ErrInvalid
|
| 203 |
+
}
|
| 204 |
+
return nil
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
// String returns the decimal representation of z.
|
| 208 |
+
func (z *Int) String() string {
|
| 209 |
+
if z == nil {
|
| 210 |
+
return "nil"
|
| 211 |
+
}
|
| 212 |
+
z.doinit()
|
| 213 |
+
p := C.mpz_get_str(nil, 10, &z.i[0])
|
| 214 |
+
s := C.GoString(p)
|
| 215 |
+
C.free(unsafe.Pointer(p))
|
| 216 |
+
return s
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
func (z *Int) destroy() {
|
| 220 |
+
if z.init {
|
| 221 |
+
C.mpz_clear(&z.i[0])
|
| 222 |
+
}
|
| 223 |
+
z.init = false
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
/*
|
| 227 |
+
* arithmetic
|
| 228 |
+
*/
|
| 229 |
+
|
| 230 |
+
// Add sets z = x + y and returns z.
|
| 231 |
+
func (z *Int) Add(x, y *Int) *Int {
|
| 232 |
+
x.doinit()
|
| 233 |
+
y.doinit()
|
| 234 |
+
z.doinit()
|
| 235 |
+
C.mpz_add(&z.i[0], &x.i[0], &y.i[0])
|
| 236 |
+
return z
|
| 237 |
+
}
|
| 238 |
+
|
| 239 |
+
// Sub sets z = x - y and returns z.
|
| 240 |
+
func (z *Int) Sub(x, y *Int) *Int {
|
| 241 |
+
x.doinit()
|
| 242 |
+
y.doinit()
|
| 243 |
+
z.doinit()
|
| 244 |
+
C.mpz_sub(&z.i[0], &x.i[0], &y.i[0])
|
| 245 |
+
return z
|
| 246 |
+
}
|
| 247 |
+
|
| 248 |
+
// Mul sets z = x * y and returns z.
|
| 249 |
+
func (z *Int) Mul(x, y *Int) *Int {
|
| 250 |
+
x.doinit()
|
| 251 |
+
y.doinit()
|
| 252 |
+
z.doinit()
|
| 253 |
+
C.mpz_mul(&z.i[0], &x.i[0], &y.i[0])
|
| 254 |
+
return z
|
| 255 |
+
}
|
| 256 |
+
|
| 257 |
+
// Div sets z = x / y, rounding toward zero, and returns z.
|
| 258 |
+
func (z *Int) Div(x, y *Int) *Int {
|
| 259 |
+
x.doinit()
|
| 260 |
+
y.doinit()
|
| 261 |
+
z.doinit()
|
| 262 |
+
C.mpz_tdiv_q(&z.i[0], &x.i[0], &y.i[0])
|
| 263 |
+
return z
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
// Mod sets z = x % y and returns z.
|
| 267 |
+
// Like the result of the Go % operator, z has the same sign as x.
|
| 268 |
+
func (z *Int) Mod(x, y *Int) *Int {
|
| 269 |
+
x.doinit()
|
| 270 |
+
y.doinit()
|
| 271 |
+
z.doinit()
|
| 272 |
+
C.mpz_tdiv_r(&z.i[0], &x.i[0], &y.i[0])
|
| 273 |
+
return z
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
// Lsh sets z = x << s and returns z.
|
| 277 |
+
func (z *Int) Lsh(x *Int, s uint) *Int {
|
| 278 |
+
x.doinit()
|
| 279 |
+
z.doinit()
|
| 280 |
+
C._mpz_mul_2exp(&z.i[0], &x.i[0], C.ulong(s))
|
| 281 |
+
return z
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
// Rsh sets z = x >> s and returns z.
|
| 285 |
+
func (z *Int) Rsh(x *Int, s uint) *Int {
|
| 286 |
+
x.doinit()
|
| 287 |
+
z.doinit()
|
| 288 |
+
C._mpz_div_2exp(&z.i[0], &x.i[0], C.ulong(s))
|
| 289 |
+
return z
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
// Exp sets z = x^y % m and returns z.
|
| 293 |
+
// If m == nil, Exp sets z = x^y.
|
| 294 |
+
func (z *Int) Exp(x, y, m *Int) *Int {
|
| 295 |
+
m.doinit()
|
| 296 |
+
x.doinit()
|
| 297 |
+
y.doinit()
|
| 298 |
+
z.doinit()
|
| 299 |
+
if m == nil {
|
| 300 |
+
C.mpz_pow_ui(&z.i[0], &x.i[0], C.mpz_get_ui(&y.i[0]))
|
| 301 |
+
} else {
|
| 302 |
+
C.mpz_powm(&z.i[0], &x.i[0], &y.i[0], &m.i[0])
|
| 303 |
+
}
|
| 304 |
+
return z
|
| 305 |
+
}
|
| 306 |
+
|
| 307 |
+
func (z *Int) Int64() int64 {
|
| 308 |
+
if !z.init {
|
| 309 |
+
return 0
|
| 310 |
+
}
|
| 311 |
+
return int64(C.mpz_get_si(&z.i[0]))
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
// Neg sets z = -x and returns z.
|
| 315 |
+
func (z *Int) Neg(x *Int) *Int {
|
| 316 |
+
x.doinit()
|
| 317 |
+
z.doinit()
|
| 318 |
+
C.mpz_neg(&z.i[0], &x.i[0])
|
| 319 |
+
return z
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
// Abs sets z to the absolute value of x and returns z.
|
| 323 |
+
func (z *Int) Abs(x *Int) *Int {
|
| 324 |
+
x.doinit()
|
| 325 |
+
z.doinit()
|
| 326 |
+
C.mpz_abs(&z.i[0], &x.i[0])
|
| 327 |
+
return z
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
/*
|
| 331 |
+
* functions without a clear receiver
|
| 332 |
+
*/
|
| 333 |
+
|
| 334 |
+
// CmpInt compares x and y. The result is
|
| 335 |
+
//
|
| 336 |
+
// -1 if x < y
|
| 337 |
+
// 0 if x == y
|
| 338 |
+
// +1 if x > y
|
| 339 |
+
func CmpInt(x, y *Int) int {
|
| 340 |
+
x.doinit()
|
| 341 |
+
y.doinit()
|
| 342 |
+
switch cmp := C.mpz_cmp(&x.i[0], &y.i[0]); {
|
| 343 |
+
case cmp < 0:
|
| 344 |
+
return -1
|
| 345 |
+
case cmp == 0:
|
| 346 |
+
return 0
|
| 347 |
+
}
|
| 348 |
+
return +1
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
// DivModInt sets q = x / y and r = x % y.
|
| 352 |
+
func DivModInt(q, r, x, y *Int) {
|
| 353 |
+
q.doinit()
|
| 354 |
+
r.doinit()
|
| 355 |
+
x.doinit()
|
| 356 |
+
y.doinit()
|
| 357 |
+
C.mpz_tdiv_qr(&q.i[0], &r.i[0], &x.i[0], &y.i[0])
|
| 358 |
+
}
|
| 359 |
+
|
| 360 |
+
// GcdInt sets d to the greatest common divisor of a and b,
|
| 361 |
+
// which must be positive numbers.
|
| 362 |
+
// If x and y are not nil, GcdInt sets x and y such that d = a*x + b*y.
|
| 363 |
+
// If either a or b is not positive, GcdInt sets d = x = y = 0.
|
| 364 |
+
func GcdInt(d, x, y, a, b *Int) {
|
| 365 |
+
d.doinit()
|
| 366 |
+
x.doinit()
|
| 367 |
+
y.doinit()
|
| 368 |
+
a.doinit()
|
| 369 |
+
b.doinit()
|
| 370 |
+
C.mpz_gcdext(&d.i[0], &x.i[0], &y.i[0], &a.i[0], &b.i[0])
|
| 371 |
+
}
|
| 372 |
+
|
| 373 |
+
// ProbablyPrime performs n Miller-Rabin tests to check whether z is prime.
|
| 374 |
+
// If it returns true, z is prime with probability 1 - 1/4^n.
|
| 375 |
+
// If it returns false, z is not prime.
|
| 376 |
+
func (z *Int) ProbablyPrime(n int) bool {
|
| 377 |
+
z.doinit()
|
| 378 |
+
return int(C.mpz_probab_prime_p(&z.i[0], C.int(n))) > 0
|
| 379 |
+
}
|
platform/dbops/binaries/go/go/misc/cgo/gmp/pi.go
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2009 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 |
+
//go:build ignore
|
| 6 |
+
|
| 7 |
+
package main
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
big "."
|
| 11 |
+
"fmt"
|
| 12 |
+
"runtime"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
var (
|
| 16 |
+
tmp1 = big.NewInt(0)
|
| 17 |
+
tmp2 = big.NewInt(0)
|
| 18 |
+
numer = big.NewInt(1)
|
| 19 |
+
accum = big.NewInt(0)
|
| 20 |
+
denom = big.NewInt(1)
|
| 21 |
+
ten = big.NewInt(10)
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
func extractDigit() int64 {
|
| 25 |
+
if big.CmpInt(numer, accum) > 0 {
|
| 26 |
+
return -1
|
| 27 |
+
}
|
| 28 |
+
tmp1.Lsh(numer, 1).Add(tmp1, numer).Add(tmp1, accum)
|
| 29 |
+
big.DivModInt(tmp1, tmp2, tmp1, denom)
|
| 30 |
+
tmp2.Add(tmp2, numer)
|
| 31 |
+
if big.CmpInt(tmp2, denom) >= 0 {
|
| 32 |
+
return -1
|
| 33 |
+
}
|
| 34 |
+
return tmp1.Int64()
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
func nextTerm(k int64) {
|
| 38 |
+
y2 := k*2 + 1
|
| 39 |
+
accum.Add(accum, tmp1.Lsh(numer, 1))
|
| 40 |
+
accum.Mul(accum, tmp1.SetInt64(y2))
|
| 41 |
+
numer.Mul(numer, tmp1.SetInt64(k))
|
| 42 |
+
denom.Mul(denom, tmp1.SetInt64(y2))
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
func eliminateDigit(d int64) {
|
| 46 |
+
accum.Sub(accum, tmp1.Mul(denom, tmp1.SetInt64(d)))
|
| 47 |
+
accum.Mul(accum, ten)
|
| 48 |
+
numer.Mul(numer, ten)
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
func main() {
|
| 52 |
+
i := 0
|
| 53 |
+
k := int64(0)
|
| 54 |
+
for {
|
| 55 |
+
d := int64(-1)
|
| 56 |
+
for d < 0 {
|
| 57 |
+
k++
|
| 58 |
+
nextTerm(k)
|
| 59 |
+
d = extractDigit()
|
| 60 |
+
}
|
| 61 |
+
eliminateDigit(d)
|
| 62 |
+
fmt.Printf("%c", d+'0')
|
| 63 |
+
|
| 64 |
+
if i++; i%50 == 0 {
|
| 65 |
+
fmt.Printf("\n")
|
| 66 |
+
if i >= 1000 {
|
| 67 |
+
break
|
| 68 |
+
}
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
fmt.Printf("\n%d calls; bit sizes: %d %d %d\n", runtime.NumCgoCall(), numer.Len(), accum.Len(), denom.Len())
|
| 73 |
+
}
|
platform/dbops/binaries/go/go/misc/chrome/gophertool/README.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
To install:
|
| 2 |
+
|
| 3 |
+
1) chrome://extensions/
|
| 4 |
+
2) click "[+] Developer Mode" in top right
|
| 5 |
+
3) "Load unpacked extension..."
|
| 6 |
+
4) pick $GOROOT/misc/chrome/gophertool
|
| 7 |
+
|
| 8 |
+
Done. It'll now auto-reload from source.
|
platform/dbops/binaries/go/go/misc/chrome/gophertool/background.html
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<html>
|
| 2 |
+
<!--
|
| 3 |
+
Copyright 2011 The Go Authors. All rights reserved.
|
| 4 |
+
Use of this source code is governed by a BSD-style
|
| 5 |
+
license that can be found in the LICENSE file.
|
| 6 |
+
-->
|
| 7 |
+
<head>
|
| 8 |
+
<script src="gopher.js"></script>
|
| 9 |
+
<script src="background.js"></script>
|
| 10 |
+
</head>
|
| 11 |
+
</html>
|
| 12 |
+
|
platform/dbops/binaries/go/go/misc/chrome/gophertool/background.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
chrome.omnibox.onInputEntered.addListener(function(t) {
|
| 2 |
+
var url = urlForInput(t);
|
| 3 |
+
if (url) {
|
| 4 |
+
chrome.tabs.query({ "active": true, "currentWindow": true }, function(tab) {
|
| 5 |
+
if (!tab) return;
|
| 6 |
+
chrome.tabs.update(tab.id, { "url": url, "selected": true });
|
| 7 |
+
});
|
| 8 |
+
}
|
| 9 |
+
});
|
platform/dbops/binaries/go/go/misc/chrome/gophertool/gopher.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2011 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 |
+
var numericRE = /^\d+$/;
|
| 6 |
+
var commitRE = /^(?:\d+:)?([0-9a-f]{6,40})$/; // e.g "8486:ab29d2698a47" or "ab29d2698a47"
|
| 7 |
+
var gerritChangeIdRE = /^I[0-9a-f]{4,40}$/; // e.g. Id69c00d908d18151486007ec03da5495b34b05f5
|
| 8 |
+
var pkgRE = /^[a-z0-9_\/]+$/;
|
| 9 |
+
|
| 10 |
+
function urlForInput(t) {
|
| 11 |
+
if (!t) {
|
| 12 |
+
return null;
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
if (numericRE.test(t)) {
|
| 16 |
+
if (t < 150000) {
|
| 17 |
+
// We could use the golang.org/cl/ handler here, but
|
| 18 |
+
// avoid some redirect latency and go right there, since
|
| 19 |
+
// one is easy. (no server-side mapping)
|
| 20 |
+
return "https://github.com/golang/go/issues/" + t;
|
| 21 |
+
}
|
| 22 |
+
return "https://golang.org/cl/" + t;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
if (gerritChangeIdRE.test(t)) {
|
| 26 |
+
return "https://golang.org/cl/" + t;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
var match = commitRE.exec(t);
|
| 30 |
+
if (match) {
|
| 31 |
+
return "https://golang.org/change/" + match[1];
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
if (pkgRE.test(t)) {
|
| 35 |
+
// TODO: make this smarter, using a list of packages + substring matches.
|
| 36 |
+
// Get the list from godoc itself in JSON format?
|
| 37 |
+
return "https://golang.org/pkg/" + t;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
return null;
|
| 41 |
+
}
|
platform/dbops/binaries/go/go/misc/chrome/gophertool/gopher.png
ADDED
|
Git LFS Details
|
platform/dbops/binaries/go/go/misc/chrome/gophertool/manifest.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "Hacking Gopher",
|
| 3 |
+
"version": "1.0",
|
| 4 |
+
"manifest_version": 2,
|
| 5 |
+
"description": "Go Hacking utility",
|
| 6 |
+
"background": {
|
| 7 |
+
"page": "background.html"
|
| 8 |
+
},
|
| 9 |
+
"browser_action": {
|
| 10 |
+
"default_icon": "gopher.png",
|
| 11 |
+
"default_popup": "popup.html"
|
| 12 |
+
},
|
| 13 |
+
"omnibox": { "keyword": "golang" },
|
| 14 |
+
"icons": {
|
| 15 |
+
"16": "gopher.png"
|
| 16 |
+
},
|
| 17 |
+
"permissions": [
|
| 18 |
+
"tabs"
|
| 19 |
+
]
|
| 20 |
+
}
|
platform/dbops/binaries/go/go/misc/chrome/gophertool/popup.html
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<html>
|
| 2 |
+
<!--
|
| 3 |
+
Copyright 2011 The Go Authors. All rights reserved.
|
| 4 |
+
Use of this source code is governed by a BSD-style
|
| 5 |
+
license that can be found in the LICENSE file.
|
| 6 |
+
-->
|
| 7 |
+
<head>
|
| 8 |
+
<script src="gopher.js"></script>
|
| 9 |
+
<script src="popup.js"></script>
|
| 10 |
+
</head>
|
| 11 |
+
<body style='margin: 0.5em; font-family: sans;'>
|
| 12 |
+
<small><a href="#" url="https://golang.org/issue">issue</a>,
|
| 13 |
+
<a href="#" url="https://golang.org/cl">codereview</a>,
|
| 14 |
+
<a href="#" url="https://golang.org/change">commit</a>, or
|
| 15 |
+
<a href="#" url="https://golang.org/pkg/">pkg</a> id/name:</small>
|
| 16 |
+
<form style='margin: 0' id='navform'><nobr><input id="inputbox" size=10 tabindex=1 /><input type="submit" value="go" /></nobr></form>
|
| 17 |
+
<small>Also: <a href="#" url="https://build.golang.org">buildbots</a>
|
| 18 |
+
<a href="#" url="https://github.com/golang/go">GitHub</a>
|
| 19 |
+
</small>
|
| 20 |
+
</body>
|
| 21 |
+
</html>
|
platform/dbops/binaries/go/go/misc/chrome/gophertool/popup.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
function openURL(url) {
|
| 2 |
+
chrome.tabs.create({ "url": url })
|
| 3 |
+
}
|
| 4 |
+
|
| 5 |
+
function addLinks() {
|
| 6 |
+
var links = document.getElementsByTagName("a");
|
| 7 |
+
for (var i = 0; i < links.length; i++) {
|
| 8 |
+
var url = links[i].getAttribute("url");
|
| 9 |
+
if (url)
|
| 10 |
+
links[i].addEventListener("click", function () {
|
| 11 |
+
openURL(this.getAttribute("url"));
|
| 12 |
+
});
|
| 13 |
+
}
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
window.addEventListener("load", function () {
|
| 17 |
+
addLinks();
|
| 18 |
+
console.log("hacking gopher pop-up loaded.");
|
| 19 |
+
document.getElementById("inputbox").focus();
|
| 20 |
+
});
|
| 21 |
+
|
| 22 |
+
window.addEventListener("submit", function () {
|
| 23 |
+
console.log("submitting form");
|
| 24 |
+
var box = document.getElementById("inputbox");
|
| 25 |
+
box.focus();
|
| 26 |
+
|
| 27 |
+
var t = box.value;
|
| 28 |
+
if (t == "") {
|
| 29 |
+
return false;
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
var success = function(url) {
|
| 33 |
+
console.log("matched " + t + " to: " + url)
|
| 34 |
+
box.value = "";
|
| 35 |
+
openURL(url);
|
| 36 |
+
return false; // cancel form submission
|
| 37 |
+
};
|
| 38 |
+
|
| 39 |
+
var url = urlForInput(t);
|
| 40 |
+
if (url) {
|
| 41 |
+
return success(url);
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
console.log("no match for text: " + t)
|
| 45 |
+
return false;
|
| 46 |
+
});
|
platform/dbops/binaries/go/go/misc/go_android_exec/README
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Android
|
| 2 |
+
=======
|
| 3 |
+
|
| 4 |
+
For details on developing Go for Android, see the documentation in the
|
| 5 |
+
mobile subrepository:
|
| 6 |
+
|
| 7 |
+
https://github.com/golang/mobile
|
| 8 |
+
|
| 9 |
+
To run the standard library tests, enable Cgo and use an appropriate
|
| 10 |
+
C compiler from the Android NDK. For example,
|
| 11 |
+
|
| 12 |
+
CGO_ENABLED=1 \
|
| 13 |
+
GOOS=android \
|
| 14 |
+
GOARCH=arm64 \
|
| 15 |
+
CC_FOR_TARGET=$NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android21-clang \
|
| 16 |
+
./all.bash
|
| 17 |
+
|
| 18 |
+
To run tests on the Android device, add the bin directory to PATH so the
|
| 19 |
+
go tool can find the go_android_$GOARCH_exec wrapper generated by
|
| 20 |
+
make.bash. For example, to run the go1 benchmarks
|
| 21 |
+
|
| 22 |
+
export PATH=$GOROOT/bin:$PATH
|
| 23 |
+
cd $GOROOT/test/bench/go1/
|
| 24 |
+
GOOS=android GOARCH=arm64 go test -bench=. -count=N -timeout=T
|
| 25 |
+
|
platform/dbops/binaries/go/go/misc/go_android_exec/exitcode_test.go
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2023 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 |
+
//go:build !(windows || js || wasip1)
|
| 6 |
+
|
| 7 |
+
package main
|
| 8 |
+
|
| 9 |
+
import (
|
| 10 |
+
"regexp"
|
| 11 |
+
"strings"
|
| 12 |
+
"testing"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
func TestExitCodeFilter(t *testing.T) {
|
| 16 |
+
// Write text to the filter one character at a time.
|
| 17 |
+
var out strings.Builder
|
| 18 |
+
f, exitStr := newExitCodeFilter(&out)
|
| 19 |
+
// Embed a "fake" exit code in the middle to check that we don't get caught on it.
|
| 20 |
+
pre := "abc" + exitStr + "123def"
|
| 21 |
+
text := pre + exitStr + `1`
|
| 22 |
+
for i := 0; i < len(text); i++ {
|
| 23 |
+
_, err := f.Write([]byte{text[i]})
|
| 24 |
+
if err != nil {
|
| 25 |
+
t.Fatal(err)
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
// The "pre" output should all have been flushed already.
|
| 30 |
+
if want, got := pre, out.String(); want != got {
|
| 31 |
+
t.Errorf("filter should have already flushed %q, but flushed %q", want, got)
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
code, err := f.Finish()
|
| 35 |
+
if err != nil {
|
| 36 |
+
t.Fatal(err)
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
// Nothing more should have been written to out.
|
| 40 |
+
if want, got := pre, out.String(); want != got {
|
| 41 |
+
t.Errorf("want output %q, got %q", want, got)
|
| 42 |
+
}
|
| 43 |
+
if want := 1; want != code {
|
| 44 |
+
t.Errorf("want exit code %d, got %d", want, code)
|
| 45 |
+
}
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
func TestExitCodeMissing(t *testing.T) {
|
| 49 |
+
var wantErr *regexp.Regexp
|
| 50 |
+
check := func(text string) {
|
| 51 |
+
t.Helper()
|
| 52 |
+
var out strings.Builder
|
| 53 |
+
f, exitStr := newExitCodeFilter(&out)
|
| 54 |
+
if want := "exitcode="; want != exitStr {
|
| 55 |
+
t.Fatalf("test assumes exitStr will be %q, but got %q", want, exitStr)
|
| 56 |
+
}
|
| 57 |
+
f.Write([]byte(text))
|
| 58 |
+
_, err := f.Finish()
|
| 59 |
+
// We should get a no exit code error
|
| 60 |
+
if err == nil || !wantErr.MatchString(err.Error()) {
|
| 61 |
+
t.Errorf("want error matching %s, got %s", wantErr, err)
|
| 62 |
+
}
|
| 63 |
+
// And it should flush all output (even if it looks
|
| 64 |
+
// like we may be getting an exit code)
|
| 65 |
+
if got := out.String(); text != got {
|
| 66 |
+
t.Errorf("want full output %q, got %q", text, got)
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
wantErr = regexp.MustCompile("^no exit code")
|
| 70 |
+
check("abc")
|
| 71 |
+
check("exitcode")
|
| 72 |
+
check("exitcode=")
|
| 73 |
+
check("exitcode=123\n")
|
| 74 |
+
wantErr = regexp.MustCompile("^bad exit code: .* value out of range")
|
| 75 |
+
check("exitcode=999999999999999999999999")
|
| 76 |
+
}
|
platform/dbops/binaries/go/go/misc/go_android_exec/main.go
ADDED
|
@@ -0,0 +1,527 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2014 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 |
+
// This wrapper uses syscall.Flock to prevent concurrent adb commands,
|
| 6 |
+
// so for now it only builds on platforms that support that system call.
|
| 7 |
+
// TODO(#33974): use a more portable library for file locking.
|
| 8 |
+
|
| 9 |
+
//go:build darwin || dragonfly || freebsd || illumos || linux || netbsd || openbsd
|
| 10 |
+
|
| 11 |
+
// This program can be used as go_android_GOARCH_exec by the Go tool.
|
| 12 |
+
// It executes binaries on an android device using adb.
|
| 13 |
+
package main
|
| 14 |
+
|
| 15 |
+
import (
|
| 16 |
+
"bytes"
|
| 17 |
+
"errors"
|
| 18 |
+
"fmt"
|
| 19 |
+
"io"
|
| 20 |
+
"log"
|
| 21 |
+
"os"
|
| 22 |
+
"os/exec"
|
| 23 |
+
"os/signal"
|
| 24 |
+
"path"
|
| 25 |
+
"path/filepath"
|
| 26 |
+
"regexp"
|
| 27 |
+
"runtime"
|
| 28 |
+
"strconv"
|
| 29 |
+
"strings"
|
| 30 |
+
"sync"
|
| 31 |
+
"syscall"
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
func adbRun(args string) (int, error) {
|
| 35 |
+
// The exit code of adb is often wrong. In theory it was fixed in 2016
|
| 36 |
+
// (https://code.google.com/p/android/issues/detail?id=3254), but it's
|
| 37 |
+
// still broken on our builders in 2023. Instead, append the exitcode to
|
| 38 |
+
// the output and parse it from there.
|
| 39 |
+
filter, exitStr := newExitCodeFilter(os.Stdout)
|
| 40 |
+
args += "; echo -n " + exitStr + "$?"
|
| 41 |
+
|
| 42 |
+
cmd := adbCmd("exec-out", args)
|
| 43 |
+
cmd.Stdout = filter
|
| 44 |
+
// If the adb subprocess somehow hangs, go test will kill this wrapper
|
| 45 |
+
// and wait for our os.Stderr (and os.Stdout) to close as a result.
|
| 46 |
+
// However, if the os.Stderr (or os.Stdout) file descriptors are
|
| 47 |
+
// passed on, the hanging adb subprocess will hold them open and
|
| 48 |
+
// go test will hang forever.
|
| 49 |
+
//
|
| 50 |
+
// Avoid that by wrapping stderr, breaking the short circuit and
|
| 51 |
+
// forcing cmd.Run to use another pipe and goroutine to pass
|
| 52 |
+
// along stderr from adb.
|
| 53 |
+
cmd.Stderr = struct{ io.Writer }{os.Stderr}
|
| 54 |
+
err := cmd.Run()
|
| 55 |
+
|
| 56 |
+
// Before we process err, flush any further output and get the exit code.
|
| 57 |
+
exitCode, err2 := filter.Finish()
|
| 58 |
+
|
| 59 |
+
if err != nil {
|
| 60 |
+
return 0, fmt.Errorf("adb exec-out %s: %v", args, err)
|
| 61 |
+
}
|
| 62 |
+
return exitCode, err2
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
func adb(args ...string) error {
|
| 66 |
+
if out, err := adbCmd(args...).CombinedOutput(); err != nil {
|
| 67 |
+
fmt.Fprintf(os.Stderr, "adb %s\n%s", strings.Join(args, " "), out)
|
| 68 |
+
return err
|
| 69 |
+
}
|
| 70 |
+
return nil
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
func adbCmd(args ...string) *exec.Cmd {
|
| 74 |
+
if flags := os.Getenv("GOANDROID_ADB_FLAGS"); flags != "" {
|
| 75 |
+
args = append(strings.Split(flags, " "), args...)
|
| 76 |
+
}
|
| 77 |
+
return exec.Command("adb", args...)
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
const (
|
| 81 |
+
deviceRoot = "/data/local/tmp/go_android_exec"
|
| 82 |
+
deviceGoroot = deviceRoot + "/goroot"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
func main() {
|
| 86 |
+
log.SetFlags(0)
|
| 87 |
+
log.SetPrefix("go_android_exec: ")
|
| 88 |
+
exitCode, err := runMain()
|
| 89 |
+
if err != nil {
|
| 90 |
+
log.Fatal(err)
|
| 91 |
+
}
|
| 92 |
+
os.Exit(exitCode)
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
func runMain() (int, error) {
|
| 96 |
+
// Concurrent use of adb is flaky, so serialize adb commands.
|
| 97 |
+
// See https://github.com/golang/go/issues/23795 or
|
| 98 |
+
// https://issuetracker.google.com/issues/73230216.
|
| 99 |
+
lockPath := filepath.Join(os.TempDir(), "go_android_exec-adb-lock")
|
| 100 |
+
lock, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0666)
|
| 101 |
+
if err != nil {
|
| 102 |
+
return 0, err
|
| 103 |
+
}
|
| 104 |
+
defer lock.Close()
|
| 105 |
+
if err := syscall.Flock(int(lock.Fd()), syscall.LOCK_EX); err != nil {
|
| 106 |
+
return 0, err
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
// In case we're booting a device or emulator alongside all.bash, wait for
|
| 110 |
+
// it to be ready. adb wait-for-device is not enough, we have to
|
| 111 |
+
// wait for sys.boot_completed.
|
| 112 |
+
if err := adb("wait-for-device", "exec-out", "while [[ -z $(getprop sys.boot_completed) ]]; do sleep 1; done;"); err != nil {
|
| 113 |
+
return 0, err
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
// Done once per make.bash.
|
| 117 |
+
if err := adbCopyGoroot(); err != nil {
|
| 118 |
+
return 0, err
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
// Prepare a temporary directory that will be cleaned up at the end.
|
| 122 |
+
// Binary names can conflict.
|
| 123 |
+
// E.g. template.test from the {html,text}/template packages.
|
| 124 |
+
binName := filepath.Base(os.Args[1])
|
| 125 |
+
deviceGotmp := fmt.Sprintf(deviceRoot+"/%s-%d", binName, os.Getpid())
|
| 126 |
+
deviceGopath := deviceGotmp + "/gopath"
|
| 127 |
+
defer adb("exec-out", "rm", "-rf", deviceGotmp) // Clean up.
|
| 128 |
+
|
| 129 |
+
// Determine the package by examining the current working
|
| 130 |
+
// directory, which will look something like
|
| 131 |
+
// "$GOROOT/src/mime/multipart" or "$GOPATH/src/golang.org/x/mobile".
|
| 132 |
+
// We extract everything after the $GOROOT or $GOPATH to run on the
|
| 133 |
+
// same relative directory on the target device.
|
| 134 |
+
importPath, isStd, modPath, modDir, err := pkgPath()
|
| 135 |
+
if err != nil {
|
| 136 |
+
return 0, err
|
| 137 |
+
}
|
| 138 |
+
var deviceCwd string
|
| 139 |
+
if isStd {
|
| 140 |
+
// Note that we use path.Join here instead of filepath.Join:
|
| 141 |
+
// The device paths should be slash-separated even if the go_android_exec
|
| 142 |
+
// wrapper itself is compiled for Windows.
|
| 143 |
+
deviceCwd = path.Join(deviceGoroot, "src", importPath)
|
| 144 |
+
} else {
|
| 145 |
+
deviceCwd = path.Join(deviceGopath, "src", importPath)
|
| 146 |
+
if modDir != "" {
|
| 147 |
+
// In module mode, the user may reasonably expect the entire module
|
| 148 |
+
// to be present. Copy it over.
|
| 149 |
+
deviceModDir := path.Join(deviceGopath, "src", modPath)
|
| 150 |
+
if err := adb("exec-out", "mkdir", "-p", path.Dir(deviceModDir)); err != nil {
|
| 151 |
+
return 0, err
|
| 152 |
+
}
|
| 153 |
+
// We use a single recursive 'adb push' of the module root instead of
|
| 154 |
+
// walking the tree and copying it piecewise. If the directory tree
|
| 155 |
+
// contains nested modules this could push a lot of unnecessary contents,
|
| 156 |
+
// but for the golang.org/x repos it seems to be significantly (~2x)
|
| 157 |
+
// faster than copying one file at a time (via filepath.WalkDir),
|
| 158 |
+
// apparently due to high latency in 'adb' commands.
|
| 159 |
+
if err := adb("push", modDir, deviceModDir); err != nil {
|
| 160 |
+
return 0, err
|
| 161 |
+
}
|
| 162 |
+
} else {
|
| 163 |
+
if err := adb("exec-out", "mkdir", "-p", deviceCwd); err != nil {
|
| 164 |
+
return 0, err
|
| 165 |
+
}
|
| 166 |
+
if err := adbCopyTree(deviceCwd, importPath); err != nil {
|
| 167 |
+
return 0, err
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
// Copy .go files from the package.
|
| 171 |
+
goFiles, err := filepath.Glob("*.go")
|
| 172 |
+
if err != nil {
|
| 173 |
+
return 0, err
|
| 174 |
+
}
|
| 175 |
+
if len(goFiles) > 0 {
|
| 176 |
+
args := append(append([]string{"push"}, goFiles...), deviceCwd)
|
| 177 |
+
if err := adb(args...); err != nil {
|
| 178 |
+
return 0, err
|
| 179 |
+
}
|
| 180 |
+
}
|
| 181 |
+
}
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
deviceBin := fmt.Sprintf("%s/%s", deviceGotmp, binName)
|
| 185 |
+
if err := adb("push", os.Args[1], deviceBin); err != nil {
|
| 186 |
+
return 0, err
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
// Forward SIGQUIT from the go command to show backtraces from
|
| 190 |
+
// the binary instead of from this wrapper.
|
| 191 |
+
quit := make(chan os.Signal, 1)
|
| 192 |
+
signal.Notify(quit, syscall.SIGQUIT)
|
| 193 |
+
go func() {
|
| 194 |
+
for range quit {
|
| 195 |
+
// We don't have the PID of the running process; use the
|
| 196 |
+
// binary name instead.
|
| 197 |
+
adb("exec-out", "killall -QUIT "+binName)
|
| 198 |
+
}
|
| 199 |
+
}()
|
| 200 |
+
cmd := `export TMPDIR="` + deviceGotmp + `"` +
|
| 201 |
+
`; export GOROOT="` + deviceGoroot + `"` +
|
| 202 |
+
`; export GOPATH="` + deviceGopath + `"` +
|
| 203 |
+
`; export CGO_ENABLED=0` +
|
| 204 |
+
`; export GOPROXY=` + os.Getenv("GOPROXY") +
|
| 205 |
+
`; export GOCACHE="` + deviceRoot + `/gocache"` +
|
| 206 |
+
`; export PATH="` + deviceGoroot + `/bin":$PATH` +
|
| 207 |
+
`; export HOME="` + deviceRoot + `/home"` +
|
| 208 |
+
`; cd "` + deviceCwd + `"` +
|
| 209 |
+
"; '" + deviceBin + "' " + strings.Join(os.Args[2:], " ")
|
| 210 |
+
code, err := adbRun(cmd)
|
| 211 |
+
signal.Reset(syscall.SIGQUIT)
|
| 212 |
+
close(quit)
|
| 213 |
+
return code, err
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
type exitCodeFilter struct {
|
| 217 |
+
w io.Writer // Pass through to w
|
| 218 |
+
exitRe *regexp.Regexp
|
| 219 |
+
buf bytes.Buffer
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
func newExitCodeFilter(w io.Writer) (*exitCodeFilter, string) {
|
| 223 |
+
const exitStr = "exitcode="
|
| 224 |
+
|
| 225 |
+
// Build a regexp that matches any prefix of the exit string at the end of
|
| 226 |
+
// the input. We do it this way to avoid assuming anything about the
|
| 227 |
+
// subcommand output (e.g., it might not be \n-terminated).
|
| 228 |
+
var exitReStr strings.Builder
|
| 229 |
+
for i := 1; i <= len(exitStr); i++ {
|
| 230 |
+
fmt.Fprintf(&exitReStr, "%s$|", exitStr[:i])
|
| 231 |
+
}
|
| 232 |
+
// Finally, match the exit string along with an exit code.
|
| 233 |
+
// This is the only case we use a group, and we'll use this
|
| 234 |
+
// group to extract the numeric code.
|
| 235 |
+
fmt.Fprintf(&exitReStr, "%s([0-9]+)$", exitStr)
|
| 236 |
+
exitRe := regexp.MustCompile(exitReStr.String())
|
| 237 |
+
|
| 238 |
+
return &exitCodeFilter{w: w, exitRe: exitRe}, exitStr
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
func (f *exitCodeFilter) Write(data []byte) (int, error) {
|
| 242 |
+
n := len(data)
|
| 243 |
+
f.buf.Write(data)
|
| 244 |
+
// Flush to w until a potential match of exitRe
|
| 245 |
+
b := f.buf.Bytes()
|
| 246 |
+
match := f.exitRe.FindIndex(b)
|
| 247 |
+
if match == nil {
|
| 248 |
+
// Flush all of the buffer.
|
| 249 |
+
_, err := f.w.Write(b)
|
| 250 |
+
f.buf.Reset()
|
| 251 |
+
if err != nil {
|
| 252 |
+
return n, err
|
| 253 |
+
}
|
| 254 |
+
} else {
|
| 255 |
+
// Flush up to the beginning of the (potential) match.
|
| 256 |
+
_, err := f.w.Write(b[:match[0]])
|
| 257 |
+
f.buf.Next(match[0])
|
| 258 |
+
if err != nil {
|
| 259 |
+
return n, err
|
| 260 |
+
}
|
| 261 |
+
}
|
| 262 |
+
return n, nil
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
func (f *exitCodeFilter) Finish() (int, error) {
|
| 266 |
+
// f.buf could be empty, contain a partial match of exitRe, or
|
| 267 |
+
// contain a full match.
|
| 268 |
+
b := f.buf.Bytes()
|
| 269 |
+
defer f.buf.Reset()
|
| 270 |
+
match := f.exitRe.FindSubmatch(b)
|
| 271 |
+
if len(match) < 2 || match[1] == nil {
|
| 272 |
+
// Not a full match. Flush.
|
| 273 |
+
if _, err := f.w.Write(b); err != nil {
|
| 274 |
+
return 0, err
|
| 275 |
+
}
|
| 276 |
+
return 0, fmt.Errorf("no exit code (in %q)", string(b))
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
// Parse the exit code.
|
| 280 |
+
code, err := strconv.Atoi(string(match[1]))
|
| 281 |
+
if err != nil {
|
| 282 |
+
// Something is malformed. Flush.
|
| 283 |
+
if _, err := f.w.Write(b); err != nil {
|
| 284 |
+
return 0, err
|
| 285 |
+
}
|
| 286 |
+
return 0, fmt.Errorf("bad exit code: %v (in %q)", err, string(b))
|
| 287 |
+
}
|
| 288 |
+
return code, nil
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
// pkgPath determines the package import path of the current working directory,
|
| 292 |
+
// and indicates whether it is
|
| 293 |
+
// and returns the path to the package source relative to $GOROOT (or $GOPATH).
|
| 294 |
+
func pkgPath() (importPath string, isStd bool, modPath, modDir string, err error) {
|
| 295 |
+
errorf := func(format string, args ...any) (string, bool, string, string, error) {
|
| 296 |
+
return "", false, "", "", fmt.Errorf(format, args...)
|
| 297 |
+
}
|
| 298 |
+
goTool, err := goTool()
|
| 299 |
+
if err != nil {
|
| 300 |
+
return errorf("%w", err)
|
| 301 |
+
}
|
| 302 |
+
cmd := exec.Command(goTool, "list", "-e", "-f", "{{.ImportPath}}:{{.Standard}}{{with .Module}}:{{.Path}}:{{.Dir}}{{end}}", ".")
|
| 303 |
+
out, err := cmd.Output()
|
| 304 |
+
if err != nil {
|
| 305 |
+
if ee, ok := err.(*exec.ExitError); ok && len(ee.Stderr) > 0 {
|
| 306 |
+
return errorf("%v: %s", cmd, ee.Stderr)
|
| 307 |
+
}
|
| 308 |
+
return errorf("%v: %w", cmd, err)
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
parts := strings.SplitN(string(bytes.TrimSpace(out)), ":", 4)
|
| 312 |
+
if len(parts) < 2 {
|
| 313 |
+
return errorf("%v: missing ':' in output: %q", cmd, out)
|
| 314 |
+
}
|
| 315 |
+
importPath = parts[0]
|
| 316 |
+
if importPath == "" || importPath == "." {
|
| 317 |
+
return errorf("current directory does not have a Go import path")
|
| 318 |
+
}
|
| 319 |
+
isStd, err = strconv.ParseBool(parts[1])
|
| 320 |
+
if err != nil {
|
| 321 |
+
return errorf("%v: non-boolean .Standard in output: %q", cmd, out)
|
| 322 |
+
}
|
| 323 |
+
if len(parts) >= 4 {
|
| 324 |
+
modPath = parts[2]
|
| 325 |
+
modDir = parts[3]
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
return importPath, isStd, modPath, modDir, nil
|
| 329 |
+
}
|
| 330 |
+
|
| 331 |
+
// adbCopyTree copies testdata, go.mod, go.sum files from subdir
|
| 332 |
+
// and from parent directories all the way up to the root of subdir.
|
| 333 |
+
// go.mod and go.sum files are needed for the go tool modules queries,
|
| 334 |
+
// and the testdata directories for tests. It is common for tests to
|
| 335 |
+
// reach out into testdata from parent packages.
|
| 336 |
+
func adbCopyTree(deviceCwd, subdir string) error {
|
| 337 |
+
dir := ""
|
| 338 |
+
for {
|
| 339 |
+
for _, name := range []string{"testdata", "go.mod", "go.sum"} {
|
| 340 |
+
hostPath := filepath.Join(dir, name)
|
| 341 |
+
if _, err := os.Stat(hostPath); err != nil {
|
| 342 |
+
continue
|
| 343 |
+
}
|
| 344 |
+
devicePath := path.Join(deviceCwd, dir)
|
| 345 |
+
if err := adb("exec-out", "mkdir", "-p", devicePath); err != nil {
|
| 346 |
+
return err
|
| 347 |
+
}
|
| 348 |
+
if err := adb("push", hostPath, devicePath); err != nil {
|
| 349 |
+
return err
|
| 350 |
+
}
|
| 351 |
+
}
|
| 352 |
+
if subdir == "." {
|
| 353 |
+
break
|
| 354 |
+
}
|
| 355 |
+
subdir = filepath.Dir(subdir)
|
| 356 |
+
dir = path.Join(dir, "..")
|
| 357 |
+
}
|
| 358 |
+
return nil
|
| 359 |
+
}
|
| 360 |
+
|
| 361 |
+
// adbCopyGoroot clears deviceRoot for previous versions of GOROOT, GOPATH
|
| 362 |
+
// and temporary data. Then, it copies relevant parts of GOROOT to the device,
|
| 363 |
+
// including the go tool built for android.
|
| 364 |
+
// A lock file ensures this only happens once, even with concurrent exec
|
| 365 |
+
// wrappers.
|
| 366 |
+
func adbCopyGoroot() error {
|
| 367 |
+
goTool, err := goTool()
|
| 368 |
+
if err != nil {
|
| 369 |
+
return err
|
| 370 |
+
}
|
| 371 |
+
cmd := exec.Command(goTool, "version")
|
| 372 |
+
cmd.Stderr = os.Stderr
|
| 373 |
+
out, err := cmd.Output()
|
| 374 |
+
if err != nil {
|
| 375 |
+
return fmt.Errorf("%v: %w", cmd, err)
|
| 376 |
+
}
|
| 377 |
+
goVersion := string(out)
|
| 378 |
+
|
| 379 |
+
// Also known by cmd/dist. The bootstrap command deletes the file.
|
| 380 |
+
statPath := filepath.Join(os.TempDir(), "go_android_exec-adb-sync-status")
|
| 381 |
+
stat, err := os.OpenFile(statPath, os.O_CREATE|os.O_RDWR, 0666)
|
| 382 |
+
if err != nil {
|
| 383 |
+
return err
|
| 384 |
+
}
|
| 385 |
+
defer stat.Close()
|
| 386 |
+
// Serialize check and copying.
|
| 387 |
+
if err := syscall.Flock(int(stat.Fd()), syscall.LOCK_EX); err != nil {
|
| 388 |
+
return err
|
| 389 |
+
}
|
| 390 |
+
s, err := io.ReadAll(stat)
|
| 391 |
+
if err != nil {
|
| 392 |
+
return err
|
| 393 |
+
}
|
| 394 |
+
if string(s) == goVersion {
|
| 395 |
+
return nil
|
| 396 |
+
}
|
| 397 |
+
|
| 398 |
+
goroot, err := findGoroot()
|
| 399 |
+
if err != nil {
|
| 400 |
+
return err
|
| 401 |
+
}
|
| 402 |
+
|
| 403 |
+
// Delete the device's GOROOT, GOPATH and any leftover test data,
|
| 404 |
+
// and recreate GOROOT.
|
| 405 |
+
if err := adb("exec-out", "rm", "-rf", deviceRoot); err != nil {
|
| 406 |
+
return err
|
| 407 |
+
}
|
| 408 |
+
|
| 409 |
+
// Build Go for Android.
|
| 410 |
+
cmd = exec.Command(goTool, "install", "cmd")
|
| 411 |
+
out, err = cmd.CombinedOutput()
|
| 412 |
+
if err != nil {
|
| 413 |
+
if len(bytes.TrimSpace(out)) > 0 {
|
| 414 |
+
log.Printf("\n%s", out)
|
| 415 |
+
}
|
| 416 |
+
return fmt.Errorf("%v: %w", cmd, err)
|
| 417 |
+
}
|
| 418 |
+
if err := adb("exec-out", "mkdir", "-p", deviceGoroot); err != nil {
|
| 419 |
+
return err
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
// Copy the Android tools from the relevant bin subdirectory to GOROOT/bin.
|
| 423 |
+
cmd = exec.Command(goTool, "list", "-f", "{{.Target}}", "cmd/go")
|
| 424 |
+
cmd.Stderr = os.Stderr
|
| 425 |
+
out, err = cmd.Output()
|
| 426 |
+
if err != nil {
|
| 427 |
+
return fmt.Errorf("%v: %w", cmd, err)
|
| 428 |
+
}
|
| 429 |
+
platformBin := filepath.Dir(string(bytes.TrimSpace(out)))
|
| 430 |
+
if platformBin == "." {
|
| 431 |
+
return errors.New("failed to locate cmd/go for target platform")
|
| 432 |
+
}
|
| 433 |
+
if err := adb("push", platformBin, path.Join(deviceGoroot, "bin")); err != nil {
|
| 434 |
+
return err
|
| 435 |
+
}
|
| 436 |
+
|
| 437 |
+
// Copy only the relevant subdirectories from pkg: pkg/include and the
|
| 438 |
+
// platform-native binaries in pkg/tool.
|
| 439 |
+
if err := adb("exec-out", "mkdir", "-p", path.Join(deviceGoroot, "pkg", "tool")); err != nil {
|
| 440 |
+
return err
|
| 441 |
+
}
|
| 442 |
+
if err := adb("push", filepath.Join(goroot, "pkg", "include"), path.Join(deviceGoroot, "pkg", "include")); err != nil {
|
| 443 |
+
return err
|
| 444 |
+
}
|
| 445 |
+
|
| 446 |
+
cmd = exec.Command(goTool, "list", "-f", "{{.Target}}", "cmd/compile")
|
| 447 |
+
cmd.Stderr = os.Stderr
|
| 448 |
+
out, err = cmd.Output()
|
| 449 |
+
if err != nil {
|
| 450 |
+
return fmt.Errorf("%v: %w", cmd, err)
|
| 451 |
+
}
|
| 452 |
+
platformToolDir := filepath.Dir(string(bytes.TrimSpace(out)))
|
| 453 |
+
if platformToolDir == "." {
|
| 454 |
+
return errors.New("failed to locate cmd/compile for target platform")
|
| 455 |
+
}
|
| 456 |
+
relToolDir, err := filepath.Rel(filepath.Join(goroot), platformToolDir)
|
| 457 |
+
if err != nil {
|
| 458 |
+
return err
|
| 459 |
+
}
|
| 460 |
+
if err := adb("push", platformToolDir, path.Join(deviceGoroot, relToolDir)); err != nil {
|
| 461 |
+
return err
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
// Copy all other files from GOROOT.
|
| 465 |
+
dirents, err := os.ReadDir(goroot)
|
| 466 |
+
if err != nil {
|
| 467 |
+
return err
|
| 468 |
+
}
|
| 469 |
+
for _, de := range dirents {
|
| 470 |
+
switch de.Name() {
|
| 471 |
+
case "bin", "pkg":
|
| 472 |
+
// We already created GOROOT/bin and GOROOT/pkg above; skip those.
|
| 473 |
+
continue
|
| 474 |
+
}
|
| 475 |
+
if err := adb("push", filepath.Join(goroot, de.Name()), path.Join(deviceGoroot, de.Name())); err != nil {
|
| 476 |
+
return err
|
| 477 |
+
}
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
if _, err := stat.WriteString(goVersion); err != nil {
|
| 481 |
+
return err
|
| 482 |
+
}
|
| 483 |
+
return nil
|
| 484 |
+
}
|
| 485 |
+
|
| 486 |
+
func findGoroot() (string, error) {
|
| 487 |
+
gorootOnce.Do(func() {
|
| 488 |
+
// If runtime.GOROOT reports a non-empty path, assume that it is valid.
|
| 489 |
+
// (It may be empty if this binary was built with -trimpath.)
|
| 490 |
+
gorootPath = runtime.GOROOT()
|
| 491 |
+
if gorootPath != "" {
|
| 492 |
+
return
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
+
// runtime.GOROOT is empty — perhaps go_android_exec was built with
|
| 496 |
+
// -trimpath and GOROOT is unset. Try 'go env GOROOT' as a fallback,
|
| 497 |
+
// assuming that the 'go' command in $PATH is the correct one.
|
| 498 |
+
|
| 499 |
+
cmd := exec.Command("go", "env", "GOROOT")
|
| 500 |
+
cmd.Stderr = os.Stderr
|
| 501 |
+
out, err := cmd.Output()
|
| 502 |
+
if err != nil {
|
| 503 |
+
gorootErr = fmt.Errorf("%v: %w", cmd, err)
|
| 504 |
+
}
|
| 505 |
+
|
| 506 |
+
gorootPath = string(bytes.TrimSpace(out))
|
| 507 |
+
if gorootPath == "" {
|
| 508 |
+
gorootErr = errors.New("GOROOT not found")
|
| 509 |
+
}
|
| 510 |
+
})
|
| 511 |
+
|
| 512 |
+
return gorootPath, gorootErr
|
| 513 |
+
}
|
| 514 |
+
|
| 515 |
+
func goTool() (string, error) {
|
| 516 |
+
goroot, err := findGoroot()
|
| 517 |
+
if err != nil {
|
| 518 |
+
return "", err
|
| 519 |
+
}
|
| 520 |
+
return filepath.Join(goroot, "bin", "go"), nil
|
| 521 |
+
}
|
| 522 |
+
|
| 523 |
+
var (
|
| 524 |
+
gorootOnce sync.Once
|
| 525 |
+
gorootPath string
|
| 526 |
+
gorootErr error
|
| 527 |
+
)
|
platform/dbops/binaries/go/go/misc/ios/README
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Go on iOS
|
| 2 |
+
=========
|
| 3 |
+
|
| 4 |
+
To run the standard library tests, run all.bash as usual, but with the compiler
|
| 5 |
+
set to the clang wrapper that invokes clang for iOS. For example, this command runs
|
| 6 |
+
all.bash on the iOS emulator:
|
| 7 |
+
|
| 8 |
+
GOOS=ios GOARCH=amd64 CGO_ENABLED=1 CC_FOR_TARGET=$(pwd)/../misc/ios/clangwrap.sh ./all.bash
|
| 9 |
+
|
| 10 |
+
If CC_FOR_TARGET is not set when the toolchain is built (make.bash or all.bash), CC
|
| 11 |
+
can be set on the command line. For example,
|
| 12 |
+
|
| 13 |
+
GOOS=ios GOARCH=amd64 CGO_ENABLED=1 CC=$(go env GOROOT)/misc/ios/clangwrap.sh go build
|
| 14 |
+
|
| 15 |
+
Setting CC is not necessary if the toolchain is built with CC_FOR_TARGET set.
|
| 16 |
+
|
| 17 |
+
To use the go tool to run individual programs and tests, put $GOROOT/bin into PATH to ensure
|
| 18 |
+
the go_ios_$GOARCH_exec wrapper is found. For example, to run the archive/tar tests:
|
| 19 |
+
|
| 20 |
+
export PATH=$GOROOT/bin:$PATH
|
| 21 |
+
GOOS=ios GOARCH=amd64 CGO_ENABLED=1 go test archive/tar
|
| 22 |
+
|
| 23 |
+
The go_ios_exec wrapper uses GOARCH to select the emulator (amd64) or the device (arm64).
|
| 24 |
+
However, further setup is required to run tests or programs directly on a device.
|
| 25 |
+
|
| 26 |
+
First make sure you have a valid developer certificate and have setup your device properly
|
| 27 |
+
to run apps signed by your developer certificate. Then install the libimobiledevice and
|
| 28 |
+
ideviceinstaller tools from https://www.libimobiledevice.org/. Use the HEAD versions from
|
| 29 |
+
source; the stable versions have bugs that prevents the Go exec wrapper to install and run
|
| 30 |
+
apps.
|
| 31 |
+
|
| 32 |
+
Second, the Go exec wrapper must be told the developer account signing identity, the team
|
| 33 |
+
id and a provisioned bundle id to use. They're specified with the environment variables
|
| 34 |
+
GOIOS_DEV_ID, GOIOS_TEAM_ID and GOIOS_APP_ID. The detect.go program in this directory will
|
| 35 |
+
attempt to auto-detect suitable values. Run it as
|
| 36 |
+
|
| 37 |
+
go run detect.go
|
| 38 |
+
|
| 39 |
+
which will output something similar to
|
| 40 |
+
|
| 41 |
+
export GOIOS_DEV_ID="iPhone Developer: xxx@yyy.zzz (XXXXXXXX)"
|
| 42 |
+
export GOIOS_APP_ID=YYYYYYYY.some.bundle.id
|
| 43 |
+
export GOIOS_TEAM_ID=ZZZZZZZZ
|
| 44 |
+
|
| 45 |
+
If you have multiple devices connected, specify the device UDID with the GOIOS_DEVICE_ID
|
| 46 |
+
variable. Use `idevice_id -l` to list all available UDIDs. Then, setting GOARCH to arm64
|
| 47 |
+
will select the device:
|
| 48 |
+
|
| 49 |
+
GOOS=ios GOARCH=arm64 CGO_ENABLED=1 CC_FOR_TARGET=$(pwd)/../misc/ios/clangwrap.sh ./all.bash
|
| 50 |
+
|
| 51 |
+
Note that the go_darwin_$GOARCH_exec wrapper uninstalls any existing app identified by
|
| 52 |
+
the bundle id before installing a new app. If the uninstalled app is the last app by
|
| 53 |
+
the developer identity, the device might also remove the permission to run apps from
|
| 54 |
+
that developer, and the exec wrapper will fail to install the new app. To avoid that,
|
| 55 |
+
install another app with the same developer identity but with a different bundle id.
|
| 56 |
+
That way, the permission to install apps is held on to while the primary app is
|
| 57 |
+
uninstalled.
|
platform/dbops/binaries/go/go/misc/ios/clangwrap.sh
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/sh
|
| 2 |
+
# This uses the latest available iOS SDK, which is recommended.
|
| 3 |
+
# To select a specific SDK, run 'xcodebuild -showsdks'
|
| 4 |
+
# to see the available SDKs and replace iphoneos with one of them.
|
| 5 |
+
if [ "$GOARCH" == "arm64" ]; then
|
| 6 |
+
SDK=iphoneos
|
| 7 |
+
PLATFORM=ios
|
| 8 |
+
CLANGARCH="arm64"
|
| 9 |
+
else
|
| 10 |
+
SDK=iphonesimulator
|
| 11 |
+
PLATFORM=ios-simulator
|
| 12 |
+
CLANGARCH="x86_64"
|
| 13 |
+
fi
|
| 14 |
+
|
| 15 |
+
SDK_PATH=`xcrun --sdk $SDK --show-sdk-path`
|
| 16 |
+
export IPHONEOS_DEPLOYMENT_TARGET=5.1
|
| 17 |
+
# cmd/cgo doesn't support llvm-gcc-4.2, so we have to use clang.
|
| 18 |
+
CLANG=`xcrun --sdk $SDK --find clang`
|
| 19 |
+
|
| 20 |
+
exec "$CLANG" -arch $CLANGARCH -isysroot "$SDK_PATH" -m${PLATFORM}-version-min=12.0 "$@"
|
platform/dbops/binaries/go/go/misc/ios/detect.go
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
//go:build ignore
|
| 6 |
+
|
| 7 |
+
// detect attempts to autodetect the correct
|
| 8 |
+
// values of the environment variables
|
| 9 |
+
// used by go_ios_exec.
|
| 10 |
+
// detect shells out to ideviceinfo, a third party program that can
|
| 11 |
+
// be obtained by following the instructions at
|
| 12 |
+
// https://github.com/libimobiledevice/libimobiledevice.
|
| 13 |
+
package main
|
| 14 |
+
|
| 15 |
+
import (
|
| 16 |
+
"bytes"
|
| 17 |
+
"crypto/x509"
|
| 18 |
+
"fmt"
|
| 19 |
+
"os"
|
| 20 |
+
"os/exec"
|
| 21 |
+
"strings"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
func main() {
|
| 25 |
+
udids := getLines(exec.Command("idevice_id", "-l"))
|
| 26 |
+
if len(udids) == 0 {
|
| 27 |
+
fail("no udid found; is a device connected?")
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
mps := detectMobileProvisionFiles(udids)
|
| 31 |
+
if len(mps) == 0 {
|
| 32 |
+
fail("did not find mobile provision matching device udids %q", udids)
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
fmt.Println("# Available provisioning profiles below.")
|
| 36 |
+
fmt.Println("# NOTE: Any existing app on the device with the app id specified by GOIOS_APP_ID")
|
| 37 |
+
fmt.Println("# will be overwritten when running Go programs.")
|
| 38 |
+
for _, mp := range mps {
|
| 39 |
+
fmt.Println()
|
| 40 |
+
f, err := os.CreateTemp("", "go_ios_detect_")
|
| 41 |
+
check(err)
|
| 42 |
+
fname := f.Name()
|
| 43 |
+
defer os.Remove(fname)
|
| 44 |
+
|
| 45 |
+
out := output(parseMobileProvision(mp))
|
| 46 |
+
_, err = f.Write(out)
|
| 47 |
+
check(err)
|
| 48 |
+
check(f.Close())
|
| 49 |
+
|
| 50 |
+
cert, err := plistExtract(fname, "DeveloperCertificates:0")
|
| 51 |
+
check(err)
|
| 52 |
+
pcert, err := x509.ParseCertificate(cert)
|
| 53 |
+
check(err)
|
| 54 |
+
fmt.Printf("export GOIOS_DEV_ID=\"%s\"\n", pcert.Subject.CommonName)
|
| 55 |
+
|
| 56 |
+
appID, err := plistExtract(fname, "Entitlements:application-identifier")
|
| 57 |
+
check(err)
|
| 58 |
+
fmt.Printf("export GOIOS_APP_ID=%s\n", appID)
|
| 59 |
+
|
| 60 |
+
teamID, err := plistExtract(fname, "Entitlements:com.apple.developer.team-identifier")
|
| 61 |
+
check(err)
|
| 62 |
+
fmt.Printf("export GOIOS_TEAM_ID=%s\n", teamID)
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
+
func detectMobileProvisionFiles(udids [][]byte) []string {
|
| 67 |
+
cmd := exec.Command("mdfind", "-name", ".mobileprovision")
|
| 68 |
+
lines := getLines(cmd)
|
| 69 |
+
|
| 70 |
+
var files []string
|
| 71 |
+
for _, line := range lines {
|
| 72 |
+
if len(line) == 0 {
|
| 73 |
+
continue
|
| 74 |
+
}
|
| 75 |
+
xmlLines := getLines(parseMobileProvision(string(line)))
|
| 76 |
+
matches := 0
|
| 77 |
+
for _, udid := range udids {
|
| 78 |
+
for _, xmlLine := range xmlLines {
|
| 79 |
+
if bytes.Contains(xmlLine, udid) {
|
| 80 |
+
matches++
|
| 81 |
+
}
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
if matches == len(udids) {
|
| 85 |
+
files = append(files, string(line))
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
return files
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
func parseMobileProvision(fname string) *exec.Cmd {
|
| 92 |
+
return exec.Command("security", "cms", "-D", "-i", string(fname))
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
func plistExtract(fname string, path string) ([]byte, error) {
|
| 96 |
+
out, err := exec.Command("/usr/libexec/PlistBuddy", "-c", "Print "+path, fname).CombinedOutput()
|
| 97 |
+
if err != nil {
|
| 98 |
+
return nil, err
|
| 99 |
+
}
|
| 100 |
+
return bytes.TrimSpace(out), nil
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
func getLines(cmd *exec.Cmd) [][]byte {
|
| 104 |
+
out := output(cmd)
|
| 105 |
+
lines := bytes.Split(out, []byte("\n"))
|
| 106 |
+
// Skip the empty line at the end.
|
| 107 |
+
if len(lines[len(lines)-1]) == 0 {
|
| 108 |
+
lines = lines[:len(lines)-1]
|
| 109 |
+
}
|
| 110 |
+
return lines
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
func output(cmd *exec.Cmd) []byte {
|
| 114 |
+
out, err := cmd.Output()
|
| 115 |
+
if err != nil {
|
| 116 |
+
fmt.Println(strings.Join(cmd.Args, "\n"))
|
| 117 |
+
fmt.Fprintln(os.Stderr, err)
|
| 118 |
+
os.Exit(1)
|
| 119 |
+
}
|
| 120 |
+
return out
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
func check(err error) {
|
| 124 |
+
if err != nil {
|
| 125 |
+
fail(err.Error())
|
| 126 |
+
}
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
func fail(msg string, v ...interface{}) {
|
| 130 |
+
fmt.Fprintf(os.Stderr, msg, v...)
|
| 131 |
+
fmt.Fprintln(os.Stderr)
|
| 132 |
+
os.Exit(1)
|
| 133 |
+
}
|
platform/dbops/binaries/go/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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
// This program can be used as go_ios_$GOARCH_exec by the Go tool.
|
| 6 |
+
// It executes binaries on an iOS device using the XCode toolchain
|
| 7 |
+
// and the ios-deploy program: https://github.com/phonegap/ios-deploy
|
| 8 |
+
//
|
| 9 |
+
// This script supports an extra flag, -lldb, that pauses execution
|
| 10 |
+
// just before the main program begins and allows the user to control
|
| 11 |
+
// the remote lldb session. This flag is appended to the end of the
|
| 12 |
+
// script's arguments and is not passed through to the underlying
|
| 13 |
+
// binary.
|
| 14 |
+
//
|
| 15 |
+
// This script requires that three environment variables be set:
|
| 16 |
+
//
|
| 17 |
+
// GOIOS_DEV_ID: The codesigning developer id or certificate identifier
|
| 18 |
+
// GOIOS_APP_ID: The provisioning app id prefix. Must support wildcard app ids.
|
| 19 |
+
// GOIOS_TEAM_ID: The team id that owns the app id prefix.
|
| 20 |
+
//
|
| 21 |
+
// $GOROOT/misc/ios contains a script, detect.go, that attempts to autodetect these.
|
| 22 |
+
package main
|
| 23 |
+
|
| 24 |
+
import (
|
| 25 |
+
"bytes"
|
| 26 |
+
"encoding/xml"
|
| 27 |
+
"errors"
|
| 28 |
+
"fmt"
|
| 29 |
+
"go/build"
|
| 30 |
+
"io"
|
| 31 |
+
"log"
|
| 32 |
+
"net"
|
| 33 |
+
"os"
|
| 34 |
+
"os/exec"
|
| 35 |
+
"os/signal"
|
| 36 |
+
"path/filepath"
|
| 37 |
+
"runtime"
|
| 38 |
+
"strconv"
|
| 39 |
+
"strings"
|
| 40 |
+
"syscall"
|
| 41 |
+
"time"
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
const debug = false
|
| 45 |
+
|
| 46 |
+
var tmpdir string
|
| 47 |
+
|
| 48 |
+
var (
|
| 49 |
+
devID string
|
| 50 |
+
appID string
|
| 51 |
+
teamID string
|
| 52 |
+
bundleID string
|
| 53 |
+
deviceID string
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
// lock is a file lock to serialize iOS runs. It is global to avoid the
|
| 57 |
+
// garbage collector finalizing it, closing the file and releasing the
|
| 58 |
+
// lock prematurely.
|
| 59 |
+
var lock *os.File
|
| 60 |
+
|
| 61 |
+
func main() {
|
| 62 |
+
log.SetFlags(0)
|
| 63 |
+
log.SetPrefix("go_ios_exec: ")
|
| 64 |
+
if debug {
|
| 65 |
+
log.Println(strings.Join(os.Args, " "))
|
| 66 |
+
}
|
| 67 |
+
if len(os.Args) < 2 {
|
| 68 |
+
log.Fatal("usage: go_ios_exec a.out")
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// For compatibility with the old builders, use a fallback bundle ID
|
| 72 |
+
bundleID = "golang.gotest"
|
| 73 |
+
|
| 74 |
+
exitCode, err := runMain()
|
| 75 |
+
if err != nil {
|
| 76 |
+
log.Fatalf("%v\n", err)
|
| 77 |
+
}
|
| 78 |
+
os.Exit(exitCode)
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
func runMain() (int, error) {
|
| 82 |
+
var err error
|
| 83 |
+
tmpdir, err = os.MkdirTemp("", "go_ios_exec_")
|
| 84 |
+
if err != nil {
|
| 85 |
+
return 1, err
|
| 86 |
+
}
|
| 87 |
+
if !debug {
|
| 88 |
+
defer os.RemoveAll(tmpdir)
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
appdir := filepath.Join(tmpdir, "gotest.app")
|
| 92 |
+
os.RemoveAll(appdir)
|
| 93 |
+
|
| 94 |
+
if err := assembleApp(appdir, os.Args[1]); err != nil {
|
| 95 |
+
return 1, err
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
// This wrapper uses complicated machinery to run iOS binaries. It
|
| 99 |
+
// works, but only when running one binary at a time.
|
| 100 |
+
// Use a file lock to make sure only one wrapper is running at a time.
|
| 101 |
+
//
|
| 102 |
+
// The lock file is never deleted, to avoid concurrent locks on distinct
|
| 103 |
+
// files with the same path.
|
| 104 |
+
lockName := filepath.Join(os.TempDir(), "go_ios_exec-"+deviceID+".lock")
|
| 105 |
+
lock, err = os.OpenFile(lockName, os.O_CREATE|os.O_RDONLY, 0666)
|
| 106 |
+
if err != nil {
|
| 107 |
+
return 1, err
|
| 108 |
+
}
|
| 109 |
+
if err := syscall.Flock(int(lock.Fd()), syscall.LOCK_EX); err != nil {
|
| 110 |
+
return 1, err
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
if goarch := os.Getenv("GOARCH"); goarch == "arm64" {
|
| 114 |
+
err = runOnDevice(appdir)
|
| 115 |
+
} else {
|
| 116 |
+
err = runOnSimulator(appdir)
|
| 117 |
+
}
|
| 118 |
+
if err != nil {
|
| 119 |
+
// If the lldb driver completed with an exit code, use that.
|
| 120 |
+
if err, ok := err.(*exec.ExitError); ok {
|
| 121 |
+
if ws, ok := err.Sys().(interface{ ExitStatus() int }); ok {
|
| 122 |
+
return ws.ExitStatus(), nil
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
return 1, err
|
| 126 |
+
}
|
| 127 |
+
return 0, nil
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
func runOnSimulator(appdir string) error {
|
| 131 |
+
if err := installSimulator(appdir); err != nil {
|
| 132 |
+
return err
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
return runSimulator(appdir, bundleID, os.Args[2:])
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
func runOnDevice(appdir string) error {
|
| 139 |
+
// e.g. B393DDEB490947F5A463FD074299B6C0AXXXXXXX
|
| 140 |
+
devID = getenv("GOIOS_DEV_ID")
|
| 141 |
+
|
| 142 |
+
// e.g. Z8B3JBXXXX.org.golang.sample, Z8B3JBXXXX prefix is available at
|
| 143 |
+
// https://developer.apple.com/membercenter/index.action#accountSummary as Team ID.
|
| 144 |
+
appID = getenv("GOIOS_APP_ID")
|
| 145 |
+
|
| 146 |
+
// e.g. Z8B3JBXXXX, available at
|
| 147 |
+
// https://developer.apple.com/membercenter/index.action#accountSummary as Team ID.
|
| 148 |
+
teamID = getenv("GOIOS_TEAM_ID")
|
| 149 |
+
|
| 150 |
+
// Device IDs as listed with ios-deploy -c.
|
| 151 |
+
deviceID = os.Getenv("GOIOS_DEVICE_ID")
|
| 152 |
+
|
| 153 |
+
if _, id, ok := strings.Cut(appID, "."); ok {
|
| 154 |
+
bundleID = id
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
if err := signApp(appdir); err != nil {
|
| 158 |
+
return err
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
if err := uninstallDevice(bundleID); err != nil {
|
| 162 |
+
return err
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
if err := installDevice(appdir); err != nil {
|
| 166 |
+
return err
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
if err := mountDevImage(); err != nil {
|
| 170 |
+
return err
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
// Kill any hanging debug bridges that might take up port 3222.
|
| 174 |
+
exec.Command("killall", "idevicedebugserverproxy").Run()
|
| 175 |
+
|
| 176 |
+
closer, err := startDebugBridge()
|
| 177 |
+
if err != nil {
|
| 178 |
+
return err
|
| 179 |
+
}
|
| 180 |
+
defer closer()
|
| 181 |
+
|
| 182 |
+
return runDevice(appdir, bundleID, os.Args[2:])
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
func getenv(envvar string) string {
|
| 186 |
+
s := os.Getenv(envvar)
|
| 187 |
+
if s == "" {
|
| 188 |
+
log.Fatalf("%s not set\nrun $GOROOT/misc/ios/detect.go to attempt to autodetect", envvar)
|
| 189 |
+
}
|
| 190 |
+
return s
|
| 191 |
+
}
|
| 192 |
+
|
| 193 |
+
func assembleApp(appdir, bin string) error {
|
| 194 |
+
if err := os.MkdirAll(appdir, 0755); err != nil {
|
| 195 |
+
return err
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
if err := cp(filepath.Join(appdir, "gotest"), bin); err != nil {
|
| 199 |
+
return err
|
| 200 |
+
}
|
| 201 |
+
|
| 202 |
+
pkgpath, err := copyLocalData(appdir)
|
| 203 |
+
if err != nil {
|
| 204 |
+
return err
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
entitlementsPath := filepath.Join(tmpdir, "Entitlements.plist")
|
| 208 |
+
if err := os.WriteFile(entitlementsPath, []byte(entitlementsPlist()), 0744); err != nil {
|
| 209 |
+
return err
|
| 210 |
+
}
|
| 211 |
+
if err := os.WriteFile(filepath.Join(appdir, "Info.plist"), []byte(infoPlist(pkgpath)), 0744); err != nil {
|
| 212 |
+
return err
|
| 213 |
+
}
|
| 214 |
+
if err := os.WriteFile(filepath.Join(appdir, "ResourceRules.plist"), []byte(resourceRules), 0744); err != nil {
|
| 215 |
+
return err
|
| 216 |
+
}
|
| 217 |
+
return nil
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
func signApp(appdir string) error {
|
| 221 |
+
entitlementsPath := filepath.Join(tmpdir, "Entitlements.plist")
|
| 222 |
+
cmd := exec.Command(
|
| 223 |
+
"codesign",
|
| 224 |
+
"-f",
|
| 225 |
+
"-s", devID,
|
| 226 |
+
"--entitlements", entitlementsPath,
|
| 227 |
+
appdir,
|
| 228 |
+
)
|
| 229 |
+
if debug {
|
| 230 |
+
log.Println(strings.Join(cmd.Args, " "))
|
| 231 |
+
}
|
| 232 |
+
cmd.Stdout = os.Stdout
|
| 233 |
+
cmd.Stderr = os.Stderr
|
| 234 |
+
if err := cmd.Run(); err != nil {
|
| 235 |
+
return fmt.Errorf("codesign: %v", err)
|
| 236 |
+
}
|
| 237 |
+
return nil
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
// mountDevImage ensures a developer image is mounted on the device.
|
| 241 |
+
// The image contains the device lldb server for idevicedebugserverproxy
|
| 242 |
+
// to connect to.
|
| 243 |
+
func mountDevImage() error {
|
| 244 |
+
// Check for existing mount.
|
| 245 |
+
cmd := idevCmd(exec.Command("ideviceimagemounter", "-l", "-x"))
|
| 246 |
+
out, err := cmd.CombinedOutput()
|
| 247 |
+
if err != nil {
|
| 248 |
+
os.Stderr.Write(out)
|
| 249 |
+
return fmt.Errorf("ideviceimagemounter: %v", err)
|
| 250 |
+
}
|
| 251 |
+
var info struct {
|
| 252 |
+
Dict struct {
|
| 253 |
+
Data []byte `xml:",innerxml"`
|
| 254 |
+
} `xml:"dict"`
|
| 255 |
+
}
|
| 256 |
+
if err := xml.Unmarshal(out, &info); err != nil {
|
| 257 |
+
return fmt.Errorf("mountDevImage: failed to decode mount information: %v", err)
|
| 258 |
+
}
|
| 259 |
+
dict, err := parsePlistDict(info.Dict.Data)
|
| 260 |
+
if err != nil {
|
| 261 |
+
return fmt.Errorf("mountDevImage: failed to parse mount information: %v", err)
|
| 262 |
+
}
|
| 263 |
+
if dict["ImagePresent"] == "true" && dict["Status"] == "Complete" {
|
| 264 |
+
return nil
|
| 265 |
+
}
|
| 266 |
+
// Some devices only give us an ImageSignature key.
|
| 267 |
+
if _, exists := dict["ImageSignature"]; exists {
|
| 268 |
+
return nil
|
| 269 |
+
}
|
| 270 |
+
// No image is mounted. Find a suitable image.
|
| 271 |
+
imgPath, err := findDevImage()
|
| 272 |
+
if err != nil {
|
| 273 |
+
return err
|
| 274 |
+
}
|
| 275 |
+
sigPath := imgPath + ".signature"
|
| 276 |
+
cmd = idevCmd(exec.Command("ideviceimagemounter", imgPath, sigPath))
|
| 277 |
+
if out, err := cmd.CombinedOutput(); err != nil {
|
| 278 |
+
os.Stderr.Write(out)
|
| 279 |
+
return fmt.Errorf("ideviceimagemounter: %v", err)
|
| 280 |
+
}
|
| 281 |
+
return nil
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
// findDevImage use the device iOS version and build to locate a suitable
|
| 285 |
+
// developer image.
|
| 286 |
+
func findDevImage() (string, error) {
|
| 287 |
+
cmd := idevCmd(exec.Command("ideviceinfo"))
|
| 288 |
+
out, err := cmd.Output()
|
| 289 |
+
if err != nil {
|
| 290 |
+
return "", fmt.Errorf("ideviceinfo: %v", err)
|
| 291 |
+
}
|
| 292 |
+
var iosVer, buildVer string
|
| 293 |
+
lines := bytes.Split(out, []byte("\n"))
|
| 294 |
+
for _, line := range lines {
|
| 295 |
+
key, val, ok := strings.Cut(string(line), ": ")
|
| 296 |
+
if !ok {
|
| 297 |
+
continue
|
| 298 |
+
}
|
| 299 |
+
switch key {
|
| 300 |
+
case "ProductVersion":
|
| 301 |
+
iosVer = val
|
| 302 |
+
case "BuildVersion":
|
| 303 |
+
buildVer = val
|
| 304 |
+
}
|
| 305 |
+
}
|
| 306 |
+
if iosVer == "" || buildVer == "" {
|
| 307 |
+
return "", errors.New("failed to parse ideviceinfo output")
|
| 308 |
+
}
|
| 309 |
+
verSplit := strings.Split(iosVer, ".")
|
| 310 |
+
if len(verSplit) > 2 {
|
| 311 |
+
// Developer images are specific to major.minor ios version.
|
| 312 |
+
// Cut off the patch version.
|
| 313 |
+
iosVer = strings.Join(verSplit[:2], ".")
|
| 314 |
+
}
|
| 315 |
+
sdkBase := "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/DeviceSupport"
|
| 316 |
+
patterns := []string{fmt.Sprintf("%s (%s)", iosVer, buildVer), fmt.Sprintf("%s (*)", iosVer), fmt.Sprintf("%s*", iosVer)}
|
| 317 |
+
for _, pattern := range patterns {
|
| 318 |
+
matches, err := filepath.Glob(filepath.Join(sdkBase, pattern, "DeveloperDiskImage.dmg"))
|
| 319 |
+
if err != nil {
|
| 320 |
+
return "", fmt.Errorf("findDevImage: %v", err)
|
| 321 |
+
}
|
| 322 |
+
if len(matches) > 0 {
|
| 323 |
+
return matches[0], nil
|
| 324 |
+
}
|
| 325 |
+
}
|
| 326 |
+
return "", fmt.Errorf("failed to find matching developer image for iOS version %s build %s", iosVer, buildVer)
|
| 327 |
+
}
|
| 328 |
+
|
| 329 |
+
// startDebugBridge ensures that the idevicedebugserverproxy runs on
|
| 330 |
+
// port 3222.
|
| 331 |
+
func startDebugBridge() (func(), error) {
|
| 332 |
+
errChan := make(chan error, 1)
|
| 333 |
+
cmd := idevCmd(exec.Command("idevicedebugserverproxy", "3222"))
|
| 334 |
+
var stderr bytes.Buffer
|
| 335 |
+
cmd.Stderr = &stderr
|
| 336 |
+
if err := cmd.Start(); err != nil {
|
| 337 |
+
return nil, fmt.Errorf("idevicedebugserverproxy: %v", err)
|
| 338 |
+
}
|
| 339 |
+
go func() {
|
| 340 |
+
if err := cmd.Wait(); err != nil {
|
| 341 |
+
if _, ok := err.(*exec.ExitError); ok {
|
| 342 |
+
errChan <- fmt.Errorf("idevicedebugserverproxy: %s", stderr.Bytes())
|
| 343 |
+
} else {
|
| 344 |
+
errChan <- fmt.Errorf("idevicedebugserverproxy: %v", err)
|
| 345 |
+
}
|
| 346 |
+
}
|
| 347 |
+
errChan <- nil
|
| 348 |
+
}()
|
| 349 |
+
closer := func() {
|
| 350 |
+
cmd.Process.Kill()
|
| 351 |
+
<-errChan
|
| 352 |
+
}
|
| 353 |
+
// Dial localhost:3222 to ensure the proxy is ready.
|
| 354 |
+
delay := time.Second / 4
|
| 355 |
+
for attempt := 0; attempt < 5; attempt++ {
|
| 356 |
+
conn, err := net.DialTimeout("tcp", "localhost:3222", 5*time.Second)
|
| 357 |
+
if err == nil {
|
| 358 |
+
conn.Close()
|
| 359 |
+
return closer, nil
|
| 360 |
+
}
|
| 361 |
+
select {
|
| 362 |
+
case <-time.After(delay):
|
| 363 |
+
delay *= 2
|
| 364 |
+
case err := <-errChan:
|
| 365 |
+
return nil, err
|
| 366 |
+
}
|
| 367 |
+
}
|
| 368 |
+
closer()
|
| 369 |
+
return nil, errors.New("failed to set up idevicedebugserverproxy")
|
| 370 |
+
}
|
| 371 |
+
|
| 372 |
+
// findDeviceAppPath returns the device path to the app with the
|
| 373 |
+
// given bundle ID. It parses the output of ideviceinstaller -l -o xml,
|
| 374 |
+
// looking for the bundle ID and the corresponding Path value.
|
| 375 |
+
func findDeviceAppPath(bundleID string) (string, error) {
|
| 376 |
+
cmd := idevCmd(exec.Command("ideviceinstaller", "-l", "-o", "xml"))
|
| 377 |
+
out, err := cmd.CombinedOutput()
|
| 378 |
+
if err != nil {
|
| 379 |
+
os.Stderr.Write(out)
|
| 380 |
+
return "", fmt.Errorf("ideviceinstaller: -l -o xml %v", err)
|
| 381 |
+
}
|
| 382 |
+
var list struct {
|
| 383 |
+
Apps []struct {
|
| 384 |
+
Data []byte `xml:",innerxml"`
|
| 385 |
+
} `xml:"array>dict"`
|
| 386 |
+
}
|
| 387 |
+
if err := xml.Unmarshal(out, &list); err != nil {
|
| 388 |
+
return "", fmt.Errorf("failed to parse ideviceinstaller output: %v", err)
|
| 389 |
+
}
|
| 390 |
+
for _, app := range list.Apps {
|
| 391 |
+
values, err := parsePlistDict(app.Data)
|
| 392 |
+
if err != nil {
|
| 393 |
+
return "", fmt.Errorf("findDeviceAppPath: failed to parse app dict: %v", err)
|
| 394 |
+
}
|
| 395 |
+
if values["CFBundleIdentifier"] == bundleID {
|
| 396 |
+
if path, ok := values["Path"]; ok {
|
| 397 |
+
return path, nil
|
| 398 |
+
}
|
| 399 |
+
}
|
| 400 |
+
}
|
| 401 |
+
return "", fmt.Errorf("failed to find device path for bundle: %s", bundleID)
|
| 402 |
+
}
|
| 403 |
+
|
| 404 |
+
// Parse an xml encoded plist. Plist values are mapped to string.
|
| 405 |
+
func parsePlistDict(dict []byte) (map[string]string, error) {
|
| 406 |
+
d := xml.NewDecoder(bytes.NewReader(dict))
|
| 407 |
+
values := make(map[string]string)
|
| 408 |
+
var key string
|
| 409 |
+
var hasKey bool
|
| 410 |
+
for {
|
| 411 |
+
tok, err := d.Token()
|
| 412 |
+
if err == io.EOF {
|
| 413 |
+
break
|
| 414 |
+
}
|
| 415 |
+
if err != nil {
|
| 416 |
+
return nil, err
|
| 417 |
+
}
|
| 418 |
+
if tok, ok := tok.(xml.StartElement); ok {
|
| 419 |
+
if tok.Name.Local == "key" {
|
| 420 |
+
if err := d.DecodeElement(&key, &tok); err != nil {
|
| 421 |
+
return nil, err
|
| 422 |
+
}
|
| 423 |
+
hasKey = true
|
| 424 |
+
} else if hasKey {
|
| 425 |
+
var val string
|
| 426 |
+
var err error
|
| 427 |
+
switch n := tok.Name.Local; n {
|
| 428 |
+
case "true", "false":
|
| 429 |
+
// Bools are represented as <true/> and <false/>.
|
| 430 |
+
val = n
|
| 431 |
+
err = d.Skip()
|
| 432 |
+
default:
|
| 433 |
+
err = d.DecodeElement(&val, &tok)
|
| 434 |
+
}
|
| 435 |
+
if err != nil {
|
| 436 |
+
return nil, err
|
| 437 |
+
}
|
| 438 |
+
values[key] = val
|
| 439 |
+
hasKey = false
|
| 440 |
+
} else {
|
| 441 |
+
if err := d.Skip(); err != nil {
|
| 442 |
+
return nil, err
|
| 443 |
+
}
|
| 444 |
+
}
|
| 445 |
+
}
|
| 446 |
+
}
|
| 447 |
+
return values, nil
|
| 448 |
+
}
|
| 449 |
+
|
| 450 |
+
func installSimulator(appdir string) error {
|
| 451 |
+
cmd := exec.Command(
|
| 452 |
+
"xcrun", "simctl", "install",
|
| 453 |
+
"booted", // Install to the booted simulator.
|
| 454 |
+
appdir,
|
| 455 |
+
)
|
| 456 |
+
if out, err := cmd.CombinedOutput(); err != nil {
|
| 457 |
+
os.Stderr.Write(out)
|
| 458 |
+
return fmt.Errorf("xcrun simctl install booted %q: %v", appdir, err)
|
| 459 |
+
}
|
| 460 |
+
return nil
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
func uninstallDevice(bundleID string) error {
|
| 464 |
+
cmd := idevCmd(exec.Command(
|
| 465 |
+
"ideviceinstaller",
|
| 466 |
+
"-U", bundleID,
|
| 467 |
+
))
|
| 468 |
+
if out, err := cmd.CombinedOutput(); err != nil {
|
| 469 |
+
os.Stderr.Write(out)
|
| 470 |
+
return fmt.Errorf("ideviceinstaller -U %q: %s", bundleID, err)
|
| 471 |
+
}
|
| 472 |
+
return nil
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
func installDevice(appdir string) error {
|
| 476 |
+
attempt := 0
|
| 477 |
+
for {
|
| 478 |
+
cmd := idevCmd(exec.Command(
|
| 479 |
+
"ideviceinstaller",
|
| 480 |
+
"-i", appdir,
|
| 481 |
+
))
|
| 482 |
+
if out, err := cmd.CombinedOutput(); err != nil {
|
| 483 |
+
// Sometimes, installing the app fails for some reason.
|
| 484 |
+
// Give the device a few seconds and try again.
|
| 485 |
+
if attempt < 5 {
|
| 486 |
+
time.Sleep(5 * time.Second)
|
| 487 |
+
attempt++
|
| 488 |
+
continue
|
| 489 |
+
}
|
| 490 |
+
os.Stderr.Write(out)
|
| 491 |
+
return fmt.Errorf("ideviceinstaller -i %q: %v (%d attempts)", appdir, err, attempt)
|
| 492 |
+
}
|
| 493 |
+
return nil
|
| 494 |
+
}
|
| 495 |
+
}
|
| 496 |
+
|
| 497 |
+
func idevCmd(cmd *exec.Cmd) *exec.Cmd {
|
| 498 |
+
if deviceID != "" {
|
| 499 |
+
// Inject -u device_id after the executable, but before the arguments.
|
| 500 |
+
args := []string{cmd.Args[0], "-u", deviceID}
|
| 501 |
+
cmd.Args = append(args, cmd.Args[1:]...)
|
| 502 |
+
}
|
| 503 |
+
return cmd
|
| 504 |
+
}
|
| 505 |
+
|
| 506 |
+
func runSimulator(appdir, bundleID string, args []string) error {
|
| 507 |
+
cmd := exec.Command(
|
| 508 |
+
"xcrun", "simctl", "launch",
|
| 509 |
+
"--wait-for-debugger",
|
| 510 |
+
"booted",
|
| 511 |
+
bundleID,
|
| 512 |
+
)
|
| 513 |
+
out, err := cmd.CombinedOutput()
|
| 514 |
+
if err != nil {
|
| 515 |
+
os.Stderr.Write(out)
|
| 516 |
+
return fmt.Errorf("xcrun simctl launch booted %q: %v", bundleID, err)
|
| 517 |
+
}
|
| 518 |
+
var processID int
|
| 519 |
+
var ignore string
|
| 520 |
+
if _, err := fmt.Sscanf(string(out), "%s %d", &ignore, &processID); err != nil {
|
| 521 |
+
return fmt.Errorf("runSimulator: couldn't find processID from `simctl launch`: %v (%q)", err, out)
|
| 522 |
+
}
|
| 523 |
+
_, err = runLLDB("ios-simulator", appdir, strconv.Itoa(processID), args)
|
| 524 |
+
return err
|
| 525 |
+
}
|
| 526 |
+
|
| 527 |
+
func runDevice(appdir, bundleID string, args []string) error {
|
| 528 |
+
attempt := 0
|
| 529 |
+
for {
|
| 530 |
+
// The device app path reported by the device might be stale, so retry
|
| 531 |
+
// the lookup of the device path along with the lldb launching below.
|
| 532 |
+
deviceapp, err := findDeviceAppPath(bundleID)
|
| 533 |
+
if err != nil {
|
| 534 |
+
// The device app path might not yet exist for a newly installed app.
|
| 535 |
+
if attempt == 5 {
|
| 536 |
+
return err
|
| 537 |
+
}
|
| 538 |
+
attempt++
|
| 539 |
+
time.Sleep(5 * time.Second)
|
| 540 |
+
continue
|
| 541 |
+
}
|
| 542 |
+
out, err := runLLDB("remote-ios", appdir, deviceapp, args)
|
| 543 |
+
// If the program was not started it can be retried without papering over
|
| 544 |
+
// real test failures.
|
| 545 |
+
started := bytes.HasPrefix(out, []byte("lldb: running program"))
|
| 546 |
+
if started || err == nil || attempt == 5 {
|
| 547 |
+
return err
|
| 548 |
+
}
|
| 549 |
+
// Sometimes, the app was not yet ready to launch or the device path was
|
| 550 |
+
// stale. Retry.
|
| 551 |
+
attempt++
|
| 552 |
+
time.Sleep(5 * time.Second)
|
| 553 |
+
}
|
| 554 |
+
}
|
| 555 |
+
|
| 556 |
+
func runLLDB(target, appdir, deviceapp string, args []string) ([]byte, error) {
|
| 557 |
+
var env []string
|
| 558 |
+
for _, e := range os.Environ() {
|
| 559 |
+
// Don't override TMPDIR, HOME, GOCACHE on the device.
|
| 560 |
+
if strings.HasPrefix(e, "TMPDIR=") || strings.HasPrefix(e, "HOME=") || strings.HasPrefix(e, "GOCACHE=") {
|
| 561 |
+
continue
|
| 562 |
+
}
|
| 563 |
+
env = append(env, e)
|
| 564 |
+
}
|
| 565 |
+
lldb := exec.Command(
|
| 566 |
+
"python",
|
| 567 |
+
"-", // Read script from stdin.
|
| 568 |
+
target,
|
| 569 |
+
appdir,
|
| 570 |
+
deviceapp,
|
| 571 |
+
)
|
| 572 |
+
lldb.Args = append(lldb.Args, args...)
|
| 573 |
+
lldb.Env = env
|
| 574 |
+
lldb.Stdin = strings.NewReader(lldbDriver)
|
| 575 |
+
lldb.Stdout = os.Stdout
|
| 576 |
+
var out bytes.Buffer
|
| 577 |
+
lldb.Stderr = io.MultiWriter(&out, os.Stderr)
|
| 578 |
+
err := lldb.Start()
|
| 579 |
+
if err == nil {
|
| 580 |
+
// Forward SIGQUIT to the lldb driver which in turn will forward
|
| 581 |
+
// to the running program.
|
| 582 |
+
sigs := make(chan os.Signal, 1)
|
| 583 |
+
signal.Notify(sigs, syscall.SIGQUIT)
|
| 584 |
+
proc := lldb.Process
|
| 585 |
+
go func() {
|
| 586 |
+
for sig := range sigs {
|
| 587 |
+
proc.Signal(sig)
|
| 588 |
+
}
|
| 589 |
+
}()
|
| 590 |
+
err = lldb.Wait()
|
| 591 |
+
signal.Stop(sigs)
|
| 592 |
+
close(sigs)
|
| 593 |
+
}
|
| 594 |
+
return out.Bytes(), err
|
| 595 |
+
}
|
| 596 |
+
|
| 597 |
+
func copyLocalDir(dst, src string) error {
|
| 598 |
+
if err := os.Mkdir(dst, 0755); err != nil {
|
| 599 |
+
return err
|
| 600 |
+
}
|
| 601 |
+
|
| 602 |
+
d, err := os.Open(src)
|
| 603 |
+
if err != nil {
|
| 604 |
+
return err
|
| 605 |
+
}
|
| 606 |
+
defer d.Close()
|
| 607 |
+
fi, err := d.Readdir(-1)
|
| 608 |
+
if err != nil {
|
| 609 |
+
return err
|
| 610 |
+
}
|
| 611 |
+
|
| 612 |
+
for _, f := range fi {
|
| 613 |
+
if f.IsDir() {
|
| 614 |
+
if f.Name() == "testdata" {
|
| 615 |
+
if err := cp(dst, filepath.Join(src, f.Name())); err != nil {
|
| 616 |
+
return err
|
| 617 |
+
}
|
| 618 |
+
}
|
| 619 |
+
continue
|
| 620 |
+
}
|
| 621 |
+
if err := cp(dst, filepath.Join(src, f.Name())); err != nil {
|
| 622 |
+
return err
|
| 623 |
+
}
|
| 624 |
+
}
|
| 625 |
+
return nil
|
| 626 |
+
}
|
| 627 |
+
|
| 628 |
+
func cp(dst, src string) error {
|
| 629 |
+
out, err := exec.Command("cp", "-a", src, dst).CombinedOutput()
|
| 630 |
+
if err != nil {
|
| 631 |
+
os.Stderr.Write(out)
|
| 632 |
+
}
|
| 633 |
+
return err
|
| 634 |
+
}
|
| 635 |
+
|
| 636 |
+
func copyLocalData(dstbase string) (pkgpath string, err error) {
|
| 637 |
+
cwd, err := os.Getwd()
|
| 638 |
+
if err != nil {
|
| 639 |
+
return "", err
|
| 640 |
+
}
|
| 641 |
+
|
| 642 |
+
finalPkgpath, underGoRoot, err := subdir()
|
| 643 |
+
if err != nil {
|
| 644 |
+
return "", err
|
| 645 |
+
}
|
| 646 |
+
cwd = strings.TrimSuffix(cwd, finalPkgpath)
|
| 647 |
+
|
| 648 |
+
// Copy all immediate files and testdata directories between
|
| 649 |
+
// the package being tested and the source root.
|
| 650 |
+
pkgpath = ""
|
| 651 |
+
for _, element := range strings.Split(finalPkgpath, string(filepath.Separator)) {
|
| 652 |
+
if debug {
|
| 653 |
+
log.Printf("copying %s", pkgpath)
|
| 654 |
+
}
|
| 655 |
+
pkgpath = filepath.Join(pkgpath, element)
|
| 656 |
+
dst := filepath.Join(dstbase, pkgpath)
|
| 657 |
+
src := filepath.Join(cwd, pkgpath)
|
| 658 |
+
if err := copyLocalDir(dst, src); err != nil {
|
| 659 |
+
return "", err
|
| 660 |
+
}
|
| 661 |
+
}
|
| 662 |
+
|
| 663 |
+
if underGoRoot {
|
| 664 |
+
// Copy timezone file.
|
| 665 |
+
//
|
| 666 |
+
// Typical apps have the zoneinfo.zip in the root of their app bundle,
|
| 667 |
+
// read by the time package as the working directory at initialization.
|
| 668 |
+
// As we move the working directory to the GOROOT pkg directory, we
|
| 669 |
+
// install the zoneinfo.zip file in the pkgpath.
|
| 670 |
+
err := cp(
|
| 671 |
+
filepath.Join(dstbase, pkgpath),
|
| 672 |
+
filepath.Join(cwd, "lib", "time", "zoneinfo.zip"),
|
| 673 |
+
)
|
| 674 |
+
if err != nil {
|
| 675 |
+
return "", err
|
| 676 |
+
}
|
| 677 |
+
// Copy src/runtime/textflag.h for (at least) Test386EndToEnd in
|
| 678 |
+
// cmd/asm/internal/asm.
|
| 679 |
+
runtimePath := filepath.Join(dstbase, "src", "runtime")
|
| 680 |
+
if err := os.MkdirAll(runtimePath, 0755); err != nil {
|
| 681 |
+
return "", err
|
| 682 |
+
}
|
| 683 |
+
err = cp(
|
| 684 |
+
filepath.Join(runtimePath, "textflag.h"),
|
| 685 |
+
filepath.Join(cwd, "src", "runtime", "textflag.h"),
|
| 686 |
+
)
|
| 687 |
+
if err != nil {
|
| 688 |
+
return "", err
|
| 689 |
+
}
|
| 690 |
+
}
|
| 691 |
+
|
| 692 |
+
return finalPkgpath, nil
|
| 693 |
+
}
|
| 694 |
+
|
| 695 |
+
// subdir determines the package based on the current working directory,
|
| 696 |
+
// and returns the path to the package source relative to $GOROOT (or $GOPATH).
|
| 697 |
+
func subdir() (pkgpath string, underGoRoot bool, err error) {
|
| 698 |
+
cwd, err := os.Getwd()
|
| 699 |
+
if err != nil {
|
| 700 |
+
return "", false, err
|
| 701 |
+
}
|
| 702 |
+
cwd, err = filepath.EvalSymlinks(cwd)
|
| 703 |
+
if err != nil {
|
| 704 |
+
log.Fatal(err)
|
| 705 |
+
}
|
| 706 |
+
goroot, err := filepath.EvalSymlinks(runtime.GOROOT())
|
| 707 |
+
if err != nil {
|
| 708 |
+
return "", false, err
|
| 709 |
+
}
|
| 710 |
+
if strings.HasPrefix(cwd, goroot) {
|
| 711 |
+
subdir, err := filepath.Rel(goroot, cwd)
|
| 712 |
+
if err != nil {
|
| 713 |
+
return "", false, err
|
| 714 |
+
}
|
| 715 |
+
return subdir, true, nil
|
| 716 |
+
}
|
| 717 |
+
|
| 718 |
+
for _, p := range filepath.SplitList(build.Default.GOPATH) {
|
| 719 |
+
pabs, err := filepath.EvalSymlinks(p)
|
| 720 |
+
if err != nil {
|
| 721 |
+
return "", false, err
|
| 722 |
+
}
|
| 723 |
+
if !strings.HasPrefix(cwd, pabs) {
|
| 724 |
+
continue
|
| 725 |
+
}
|
| 726 |
+
subdir, err := filepath.Rel(pabs, cwd)
|
| 727 |
+
if err == nil {
|
| 728 |
+
return subdir, false, nil
|
| 729 |
+
}
|
| 730 |
+
}
|
| 731 |
+
return "", false, fmt.Errorf(
|
| 732 |
+
"working directory %q is not in either GOROOT(%q) or GOPATH(%q)",
|
| 733 |
+
cwd,
|
| 734 |
+
runtime.GOROOT(),
|
| 735 |
+
build.Default.GOPATH,
|
| 736 |
+
)
|
| 737 |
+
}
|
| 738 |
+
|
| 739 |
+
func infoPlist(pkgpath string) string {
|
| 740 |
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
| 741 |
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
| 742 |
+
<plist version="1.0">
|
| 743 |
+
<dict>
|
| 744 |
+
<key>CFBundleName</key><string>golang.gotest</string>
|
| 745 |
+
<key>CFBundleSupportedPlatforms</key><array><string>iPhoneOS</string></array>
|
| 746 |
+
<key>CFBundleExecutable</key><string>gotest</string>
|
| 747 |
+
<key>CFBundleVersion</key><string>1.0</string>
|
| 748 |
+
<key>CFBundleShortVersionString</key><string>1.0</string>
|
| 749 |
+
<key>CFBundleIdentifier</key><string>` + bundleID + `</string>
|
| 750 |
+
<key>CFBundleResourceSpecification</key><string>ResourceRules.plist</string>
|
| 751 |
+
<key>LSRequiresIPhoneOS</key><true/>
|
| 752 |
+
<key>CFBundleDisplayName</key><string>gotest</string>
|
| 753 |
+
<key>GoExecWrapperWorkingDirectory</key><string>` + pkgpath + `</string>
|
| 754 |
+
</dict>
|
| 755 |
+
</plist>
|
| 756 |
+
`
|
| 757 |
+
}
|
| 758 |
+
|
| 759 |
+
func entitlementsPlist() string {
|
| 760 |
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
| 761 |
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
| 762 |
+
<plist version="1.0">
|
| 763 |
+
<dict>
|
| 764 |
+
<key>keychain-access-groups</key>
|
| 765 |
+
<array><string>` + appID + `</string></array>
|
| 766 |
+
<key>get-task-allow</key>
|
| 767 |
+
<true/>
|
| 768 |
+
<key>application-identifier</key>
|
| 769 |
+
<string>` + appID + `</string>
|
| 770 |
+
<key>com.apple.developer.team-identifier</key>
|
| 771 |
+
<string>` + teamID + `</string>
|
| 772 |
+
</dict>
|
| 773 |
+
</plist>
|
| 774 |
+
`
|
| 775 |
+
}
|
| 776 |
+
|
| 777 |
+
const resourceRules = `<?xml version="1.0" encoding="UTF-8"?>
|
| 778 |
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
| 779 |
+
<plist version="1.0">
|
| 780 |
+
<dict>
|
| 781 |
+
<key>rules</key>
|
| 782 |
+
<dict>
|
| 783 |
+
<key>.*</key>
|
| 784 |
+
<true/>
|
| 785 |
+
<key>Info.plist</key>
|
| 786 |
+
<dict>
|
| 787 |
+
<key>omit</key>
|
| 788 |
+
<true/>
|
| 789 |
+
<key>weight</key>
|
| 790 |
+
<integer>10</integer>
|
| 791 |
+
</dict>
|
| 792 |
+
<key>ResourceRules.plist</key>
|
| 793 |
+
<dict>
|
| 794 |
+
<key>omit</key>
|
| 795 |
+
<true/>
|
| 796 |
+
<key>weight</key>
|
| 797 |
+
<integer>100</integer>
|
| 798 |
+
</dict>
|
| 799 |
+
</dict>
|
| 800 |
+
</dict>
|
| 801 |
+
</plist>
|
| 802 |
+
`
|
| 803 |
+
|
| 804 |
+
const lldbDriver = `
|
| 805 |
+
import sys
|
| 806 |
+
import os
|
| 807 |
+
import signal
|
| 808 |
+
|
| 809 |
+
platform, exe, device_exe_or_pid, args = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4:]
|
| 810 |
+
|
| 811 |
+
env = []
|
| 812 |
+
for k, v in os.environ.items():
|
| 813 |
+
env.append(k + "=" + v)
|
| 814 |
+
|
| 815 |
+
sys.path.append('/Applications/Xcode.app/Contents/SharedFrameworks/LLDB.framework/Resources/Python')
|
| 816 |
+
|
| 817 |
+
import lldb
|
| 818 |
+
|
| 819 |
+
debugger = lldb.SBDebugger.Create()
|
| 820 |
+
debugger.SetAsync(True)
|
| 821 |
+
debugger.SkipLLDBInitFiles(True)
|
| 822 |
+
|
| 823 |
+
err = lldb.SBError()
|
| 824 |
+
target = debugger.CreateTarget(exe, None, platform, True, err)
|
| 825 |
+
if not target.IsValid() or not err.Success():
|
| 826 |
+
sys.stderr.write("lldb: failed to setup up target: %s\n" % (err))
|
| 827 |
+
sys.exit(1)
|
| 828 |
+
|
| 829 |
+
listener = debugger.GetListener()
|
| 830 |
+
|
| 831 |
+
if platform == 'remote-ios':
|
| 832 |
+
target.modules[0].SetPlatformFileSpec(lldb.SBFileSpec(device_exe_or_pid))
|
| 833 |
+
process = target.ConnectRemote(listener, 'connect://localhost:3222', None, err)
|
| 834 |
+
else:
|
| 835 |
+
process = target.AttachToProcessWithID(listener, int(device_exe_or_pid), err)
|
| 836 |
+
|
| 837 |
+
if not err.Success():
|
| 838 |
+
sys.stderr.write("lldb: failed to connect to remote target %s: %s\n" % (device_exe_or_pid, err))
|
| 839 |
+
sys.exit(1)
|
| 840 |
+
|
| 841 |
+
# Don't stop on signals.
|
| 842 |
+
sigs = process.GetUnixSignals()
|
| 843 |
+
for i in range(0, sigs.GetNumSignals()):
|
| 844 |
+
sig = sigs.GetSignalAtIndex(i)
|
| 845 |
+
sigs.SetShouldStop(sig, False)
|
| 846 |
+
sigs.SetShouldNotify(sig, False)
|
| 847 |
+
|
| 848 |
+
event = lldb.SBEvent()
|
| 849 |
+
running = False
|
| 850 |
+
prev_handler = None
|
| 851 |
+
|
| 852 |
+
def signal_handler(signal, frame):
|
| 853 |
+
process.Signal(signal)
|
| 854 |
+
|
| 855 |
+
def run_program():
|
| 856 |
+
# Forward SIGQUIT to the program.
|
| 857 |
+
prev_handler = signal.signal(signal.SIGQUIT, signal_handler)
|
| 858 |
+
# Tell the Go driver that the program is running and should not be retried.
|
| 859 |
+
sys.stderr.write("lldb: running program\n")
|
| 860 |
+
running = True
|
| 861 |
+
# Process is stopped at attach/launch. Let it run.
|
| 862 |
+
process.Continue()
|
| 863 |
+
|
| 864 |
+
if platform != 'remote-ios':
|
| 865 |
+
# For the local emulator the program is ready to run.
|
| 866 |
+
# For remote device runs, we need to wait for eStateConnected,
|
| 867 |
+
# below.
|
| 868 |
+
run_program()
|
| 869 |
+
|
| 870 |
+
while True:
|
| 871 |
+
if not listener.WaitForEvent(1, event):
|
| 872 |
+
continue
|
| 873 |
+
if not lldb.SBProcess.EventIsProcessEvent(event):
|
| 874 |
+
continue
|
| 875 |
+
if running:
|
| 876 |
+
# Pass through stdout and stderr.
|
| 877 |
+
while True:
|
| 878 |
+
out = process.GetSTDOUT(8192)
|
| 879 |
+
if not out:
|
| 880 |
+
break
|
| 881 |
+
sys.stdout.write(out)
|
| 882 |
+
while True:
|
| 883 |
+
out = process.GetSTDERR(8192)
|
| 884 |
+
if not out:
|
| 885 |
+
break
|
| 886 |
+
sys.stderr.write(out)
|
| 887 |
+
state = process.GetStateFromEvent(event)
|
| 888 |
+
if state in [lldb.eStateCrashed, lldb.eStateDetached, lldb.eStateUnloaded, lldb.eStateExited]:
|
| 889 |
+
if running:
|
| 890 |
+
signal.signal(signal.SIGQUIT, prev_handler)
|
| 891 |
+
break
|
| 892 |
+
elif state == lldb.eStateConnected:
|
| 893 |
+
if platform == 'remote-ios':
|
| 894 |
+
process.RemoteLaunch(args, env, None, None, None, None, 0, False, err)
|
| 895 |
+
if not err.Success():
|
| 896 |
+
sys.stderr.write("lldb: failed to launch remote process: %s\n" % (err))
|
| 897 |
+
process.Kill()
|
| 898 |
+
debugger.Terminate()
|
| 899 |
+
sys.exit(1)
|
| 900 |
+
run_program()
|
| 901 |
+
|
| 902 |
+
exitStatus = process.GetExitStatus()
|
| 903 |
+
exitDesc = process.GetExitDescription()
|
| 904 |
+
process.Kill()
|
| 905 |
+
debugger.Terminate()
|
| 906 |
+
if exitStatus == 0 and exitDesc is not None:
|
| 907 |
+
# Ensure tests fail when killed by a signal.
|
| 908 |
+
exitStatus = 123
|
| 909 |
+
|
| 910 |
+
sys.exit(exitStatus)
|
| 911 |
+
`
|
platform/dbops/binaries/go/go/misc/linkcheck/linkcheck.go
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
// The linkcheck command finds missing links in the godoc website.
|
| 6 |
+
// It crawls a URL recursively and notes URLs and URL fragments
|
| 7 |
+
// that it's seen and prints a report of missing links at the end.
|
| 8 |
+
package main
|
| 9 |
+
|
| 10 |
+
import (
|
| 11 |
+
"errors"
|
| 12 |
+
"flag"
|
| 13 |
+
"fmt"
|
| 14 |
+
"io"
|
| 15 |
+
"log"
|
| 16 |
+
"net/http"
|
| 17 |
+
"os"
|
| 18 |
+
"regexp"
|
| 19 |
+
"strings"
|
| 20 |
+
"sync"
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
var (
|
| 24 |
+
root = flag.String("root", "http://localhost:6060", "Root to crawl")
|
| 25 |
+
verbose = flag.Bool("verbose", false, "verbose")
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
var wg sync.WaitGroup // outstanding fetches
|
| 29 |
+
var urlq = make(chan string) // URLs to crawl
|
| 30 |
+
|
| 31 |
+
// urlFrag is a URL and its optional #fragment (without the #)
|
| 32 |
+
type urlFrag struct {
|
| 33 |
+
url, frag string
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
var (
|
| 37 |
+
mu sync.Mutex
|
| 38 |
+
crawled = make(map[string]bool) // URL without fragment -> true
|
| 39 |
+
neededFrags = make(map[urlFrag][]string) // URL#frag -> who needs it
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
var aRx = regexp.MustCompile(`<a href=['"]?(/[^\s'">]+)`)
|
| 43 |
+
|
| 44 |
+
// Owned by crawlLoop goroutine:
|
| 45 |
+
var (
|
| 46 |
+
linkSources = make(map[string][]string) // url no fragment -> sources
|
| 47 |
+
fragExists = make(map[urlFrag]bool)
|
| 48 |
+
problems []string
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
func localLinks(body string) (links []string) {
|
| 52 |
+
seen := map[string]bool{}
|
| 53 |
+
mv := aRx.FindAllStringSubmatch(body, -1)
|
| 54 |
+
for _, m := range mv {
|
| 55 |
+
ref := m[1]
|
| 56 |
+
if strings.HasPrefix(ref, "/src/") {
|
| 57 |
+
continue
|
| 58 |
+
}
|
| 59 |
+
if !seen[ref] {
|
| 60 |
+
seen[ref] = true
|
| 61 |
+
links = append(links, m[1])
|
| 62 |
+
}
|
| 63 |
+
}
|
| 64 |
+
return
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
var idRx = regexp.MustCompile(`\bid=['"]?([^\s'">]+)`)
|
| 68 |
+
|
| 69 |
+
func pageIDs(body string) (ids []string) {
|
| 70 |
+
mv := idRx.FindAllStringSubmatch(body, -1)
|
| 71 |
+
for _, m := range mv {
|
| 72 |
+
ids = append(ids, m[1])
|
| 73 |
+
}
|
| 74 |
+
return
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
// url may contain a #fragment, and the fragment is then noted as needing to exist.
|
| 78 |
+
func crawl(url string, sourceURL string) {
|
| 79 |
+
if strings.Contains(url, "/devel/release") {
|
| 80 |
+
return
|
| 81 |
+
}
|
| 82 |
+
mu.Lock()
|
| 83 |
+
defer mu.Unlock()
|
| 84 |
+
if u, frag, ok := strings.Cut(url, "#"); ok {
|
| 85 |
+
url = u
|
| 86 |
+
if frag != "" {
|
| 87 |
+
uf := urlFrag{url, frag}
|
| 88 |
+
neededFrags[uf] = append(neededFrags[uf], sourceURL)
|
| 89 |
+
}
|
| 90 |
+
}
|
| 91 |
+
if crawled[url] {
|
| 92 |
+
return
|
| 93 |
+
}
|
| 94 |
+
crawled[url] = true
|
| 95 |
+
|
| 96 |
+
wg.Add(1)
|
| 97 |
+
go func() {
|
| 98 |
+
urlq <- url
|
| 99 |
+
}()
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
func addProblem(url, errmsg string) {
|
| 103 |
+
msg := fmt.Sprintf("Error on %s: %s (from %s)", url, errmsg, linkSources[url])
|
| 104 |
+
if *verbose {
|
| 105 |
+
log.Print(msg)
|
| 106 |
+
}
|
| 107 |
+
problems = append(problems, msg)
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
func crawlLoop() {
|
| 111 |
+
for url := range urlq {
|
| 112 |
+
if err := doCrawl(url); err != nil {
|
| 113 |
+
addProblem(url, err.Error())
|
| 114 |
+
}
|
| 115 |
+
}
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
func doCrawl(url string) error {
|
| 119 |
+
defer wg.Done()
|
| 120 |
+
|
| 121 |
+
req, err := http.NewRequest("GET", url, nil)
|
| 122 |
+
if err != nil {
|
| 123 |
+
return err
|
| 124 |
+
}
|
| 125 |
+
res, err := http.DefaultTransport.RoundTrip(req)
|
| 126 |
+
if err != nil {
|
| 127 |
+
return err
|
| 128 |
+
}
|
| 129 |
+
// Handle redirects.
|
| 130 |
+
if res.StatusCode/100 == 3 {
|
| 131 |
+
newURL, err := res.Location()
|
| 132 |
+
if err != nil {
|
| 133 |
+
return fmt.Errorf("resolving redirect: %v", err)
|
| 134 |
+
}
|
| 135 |
+
if !strings.HasPrefix(newURL.String(), *root) {
|
| 136 |
+
// Skip off-site redirects.
|
| 137 |
+
return nil
|
| 138 |
+
}
|
| 139 |
+
crawl(newURL.String(), url)
|
| 140 |
+
return nil
|
| 141 |
+
}
|
| 142 |
+
if res.StatusCode != 200 {
|
| 143 |
+
return errors.New(res.Status)
|
| 144 |
+
}
|
| 145 |
+
slurp, err := io.ReadAll(res.Body)
|
| 146 |
+
res.Body.Close()
|
| 147 |
+
if err != nil {
|
| 148 |
+
log.Fatalf("Error reading %s body: %v", url, err)
|
| 149 |
+
}
|
| 150 |
+
if *verbose {
|
| 151 |
+
log.Printf("Len of %s: %d", url, len(slurp))
|
| 152 |
+
}
|
| 153 |
+
body := string(slurp)
|
| 154 |
+
for _, ref := range localLinks(body) {
|
| 155 |
+
if *verbose {
|
| 156 |
+
log.Printf(" links to %s", ref)
|
| 157 |
+
}
|
| 158 |
+
dest := *root + ref
|
| 159 |
+
linkSources[dest] = append(linkSources[dest], url)
|
| 160 |
+
crawl(dest, url)
|
| 161 |
+
}
|
| 162 |
+
for _, id := range pageIDs(body) {
|
| 163 |
+
if *verbose {
|
| 164 |
+
log.Printf(" url %s has #%s", url, id)
|
| 165 |
+
}
|
| 166 |
+
fragExists[urlFrag{url, id}] = true
|
| 167 |
+
}
|
| 168 |
+
return nil
|
| 169 |
+
}
|
| 170 |
+
|
| 171 |
+
func main() {
|
| 172 |
+
flag.Parse()
|
| 173 |
+
|
| 174 |
+
go crawlLoop()
|
| 175 |
+
crawl(*root, "")
|
| 176 |
+
|
| 177 |
+
wg.Wait()
|
| 178 |
+
close(urlq)
|
| 179 |
+
for uf, needers := range neededFrags {
|
| 180 |
+
if !fragExists[uf] {
|
| 181 |
+
problems = append(problems, fmt.Sprintf("Missing fragment for %+v from %v", uf, needers))
|
| 182 |
+
}
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
for _, s := range problems {
|
| 186 |
+
fmt.Println(s)
|
| 187 |
+
}
|
| 188 |
+
if len(problems) > 0 {
|
| 189 |
+
os.Exit(1)
|
| 190 |
+
}
|
| 191 |
+
}
|
platform/dbops/binaries/go/go/misc/wasm/go_js_wasm_exec
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
# Copyright 2018 The Go Authors. All rights reserved.
|
| 3 |
+
# Use of this source code is governed by a BSD-style
|
| 4 |
+
# license that can be found in the LICENSE file.
|
| 5 |
+
|
| 6 |
+
SOURCE="${BASH_SOURCE[0]}"
|
| 7 |
+
while [ -h "$SOURCE" ]; do
|
| 8 |
+
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
| 9 |
+
SOURCE="$(readlink "$SOURCE")"
|
| 10 |
+
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
|
| 11 |
+
done
|
| 12 |
+
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
|
| 13 |
+
|
| 14 |
+
# Increase the V8 stack size from the default of 984K
|
| 15 |
+
# to 8192K to ensure all tests can pass without hitting
|
| 16 |
+
# stack size limits.
|
| 17 |
+
exec node --stack-size=8192 "$DIR/wasm_exec_node.js" "$@"
|
platform/dbops/binaries/go/go/misc/wasm/go_wasip1_wasm_exec
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
# Copyright 2023 The Go Authors. All rights reserved.
|
| 3 |
+
# Use of this source code is governed by a BSD-style
|
| 4 |
+
# license that can be found in the LICENSE file.
|
| 5 |
+
|
| 6 |
+
case "$GOWASIRUNTIME" in
|
| 7 |
+
"wasmedge")
|
| 8 |
+
exec wasmedge --dir=/ --env PWD="$PWD" --env PATH="$PATH" ${GOWASIRUNTIMEARGS:-} "$1" "${@:2}"
|
| 9 |
+
;;
|
| 10 |
+
"wasmer")
|
| 11 |
+
exec wasmer run --dir=/ --env PWD="$PWD" --env PATH="$PATH" ${GOWASIRUNTIMEARGS:-} "$1" -- "${@:2}"
|
| 12 |
+
;;
|
| 13 |
+
"wazero")
|
| 14 |
+
exec wazero run -mount /:/ -env-inherit -cachedir "${TMPDIR:-/tmp}"/wazero ${GOWASIRUNTIMEARGS:-} "$1" "${@:2}"
|
| 15 |
+
;;
|
| 16 |
+
"wasmtime" | "")
|
| 17 |
+
# Match the major version in "wasmtime-cli 14.0.0". For versions before 14
|
| 18 |
+
# we need to use the old CLI. This requires Bash v3.0 and above.
|
| 19 |
+
# TODO(johanbrandhorst): Remove this condition once 1.22 is released.
|
| 20 |
+
# From 1.23 onwards we'll only support the new wasmtime CLI.
|
| 21 |
+
if [[ "$(wasmtime --version)" =~ wasmtime-cli[[:space:]]([0-9]+)\.[0-9]+\.[0-9]+ && "${BASH_REMATCH[1]}" -lt 14 ]]; then
|
| 22 |
+
exec wasmtime run --dir=/ --env PWD="$PWD" --env PATH="$PATH" --max-wasm-stack 1048576 ${GOWASIRUNTIMEARGS:-} "$1" -- "${@:2}"
|
| 23 |
+
else
|
| 24 |
+
exec wasmtime run --dir=/ --env PWD="$PWD" --env PATH="$PATH" -W max-wasm-stack=1048576 ${GOWASIRUNTIMEARGS:-} "$1" "${@:2}"
|
| 25 |
+
fi
|
| 26 |
+
;;
|
| 27 |
+
*)
|
| 28 |
+
echo "Unknown Go WASI runtime specified: $GOWASIRUNTIME"
|
| 29 |
+
exit 1
|
| 30 |
+
;;
|
| 31 |
+
esac
|
platform/dbops/binaries/go/go/misc/wasm/wasm_exec.html
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<!--
|
| 3 |
+
Copyright 2018 The Go Authors. All rights reserved.
|
| 4 |
+
Use of this source code is governed by a BSD-style
|
| 5 |
+
license that can be found in the LICENSE file.
|
| 6 |
+
-->
|
| 7 |
+
<html>
|
| 8 |
+
|
| 9 |
+
<head>
|
| 10 |
+
<meta charset="utf-8">
|
| 11 |
+
<title>Go wasm</title>
|
| 12 |
+
</head>
|
| 13 |
+
|
| 14 |
+
<body>
|
| 15 |
+
<!--
|
| 16 |
+
Add the following polyfill for Microsoft Edge 17/18 support:
|
| 17 |
+
<script src="https://cdn.jsdelivr.net/npm/text-encoding@0.7.0/lib/encoding.min.js"></script>
|
| 18 |
+
(see https://caniuse.com/#feat=textencoder)
|
| 19 |
+
-->
|
| 20 |
+
<script src="wasm_exec.js"></script>
|
| 21 |
+
<script>
|
| 22 |
+
if (!WebAssembly.instantiateStreaming) { // polyfill
|
| 23 |
+
WebAssembly.instantiateStreaming = async (resp, importObject) => {
|
| 24 |
+
const source = await (await resp).arrayBuffer();
|
| 25 |
+
return await WebAssembly.instantiate(source, importObject);
|
| 26 |
+
};
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
const go = new Go();
|
| 30 |
+
let mod, inst;
|
| 31 |
+
WebAssembly.instantiateStreaming(fetch("test.wasm"), go.importObject).then((result) => {
|
| 32 |
+
mod = result.module;
|
| 33 |
+
inst = result.instance;
|
| 34 |
+
document.getElementById("runButton").disabled = false;
|
| 35 |
+
}).catch((err) => {
|
| 36 |
+
console.error(err);
|
| 37 |
+
});
|
| 38 |
+
|
| 39 |
+
async function run() {
|
| 40 |
+
console.clear();
|
| 41 |
+
await go.run(inst);
|
| 42 |
+
inst = await WebAssembly.instantiate(mod, go.importObject); // reset instance
|
| 43 |
+
}
|
| 44 |
+
</script>
|
| 45 |
+
|
| 46 |
+
<button onClick="run();" id="runButton" disabled>Run</button>
|
| 47 |
+
</body>
|
| 48 |
+
|
| 49 |
+
</html>
|
platform/dbops/binaries/go/go/misc/wasm/wasm_exec.js
ADDED
|
@@ -0,0 +1,561 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2018 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 |
+
"use strict";
|
| 6 |
+
|
| 7 |
+
(() => {
|
| 8 |
+
const enosys = () => {
|
| 9 |
+
const err = new Error("not implemented");
|
| 10 |
+
err.code = "ENOSYS";
|
| 11 |
+
return err;
|
| 12 |
+
};
|
| 13 |
+
|
| 14 |
+
if (!globalThis.fs) {
|
| 15 |
+
let outputBuf = "";
|
| 16 |
+
globalThis.fs = {
|
| 17 |
+
constants: { O_WRONLY: -1, O_RDWR: -1, O_CREAT: -1, O_TRUNC: -1, O_APPEND: -1, O_EXCL: -1 }, // unused
|
| 18 |
+
writeSync(fd, buf) {
|
| 19 |
+
outputBuf += decoder.decode(buf);
|
| 20 |
+
const nl = outputBuf.lastIndexOf("\n");
|
| 21 |
+
if (nl != -1) {
|
| 22 |
+
console.log(outputBuf.substring(0, nl));
|
| 23 |
+
outputBuf = outputBuf.substring(nl + 1);
|
| 24 |
+
}
|
| 25 |
+
return buf.length;
|
| 26 |
+
},
|
| 27 |
+
write(fd, buf, offset, length, position, callback) {
|
| 28 |
+
if (offset !== 0 || length !== buf.length || position !== null) {
|
| 29 |
+
callback(enosys());
|
| 30 |
+
return;
|
| 31 |
+
}
|
| 32 |
+
const n = this.writeSync(fd, buf);
|
| 33 |
+
callback(null, n);
|
| 34 |
+
},
|
| 35 |
+
chmod(path, mode, callback) { callback(enosys()); },
|
| 36 |
+
chown(path, uid, gid, callback) { callback(enosys()); },
|
| 37 |
+
close(fd, callback) { callback(enosys()); },
|
| 38 |
+
fchmod(fd, mode, callback) { callback(enosys()); },
|
| 39 |
+
fchown(fd, uid, gid, callback) { callback(enosys()); },
|
| 40 |
+
fstat(fd, callback) { callback(enosys()); },
|
| 41 |
+
fsync(fd, callback) { callback(null); },
|
| 42 |
+
ftruncate(fd, length, callback) { callback(enosys()); },
|
| 43 |
+
lchown(path, uid, gid, callback) { callback(enosys()); },
|
| 44 |
+
link(path, link, callback) { callback(enosys()); },
|
| 45 |
+
lstat(path, callback) { callback(enosys()); },
|
| 46 |
+
mkdir(path, perm, callback) { callback(enosys()); },
|
| 47 |
+
open(path, flags, mode, callback) { callback(enosys()); },
|
| 48 |
+
read(fd, buffer, offset, length, position, callback) { callback(enosys()); },
|
| 49 |
+
readdir(path, callback) { callback(enosys()); },
|
| 50 |
+
readlink(path, callback) { callback(enosys()); },
|
| 51 |
+
rename(from, to, callback) { callback(enosys()); },
|
| 52 |
+
rmdir(path, callback) { callback(enosys()); },
|
| 53 |
+
stat(path, callback) { callback(enosys()); },
|
| 54 |
+
symlink(path, link, callback) { callback(enosys()); },
|
| 55 |
+
truncate(path, length, callback) { callback(enosys()); },
|
| 56 |
+
unlink(path, callback) { callback(enosys()); },
|
| 57 |
+
utimes(path, atime, mtime, callback) { callback(enosys()); },
|
| 58 |
+
};
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
if (!globalThis.process) {
|
| 62 |
+
globalThis.process = {
|
| 63 |
+
getuid() { return -1; },
|
| 64 |
+
getgid() { return -1; },
|
| 65 |
+
geteuid() { return -1; },
|
| 66 |
+
getegid() { return -1; },
|
| 67 |
+
getgroups() { throw enosys(); },
|
| 68 |
+
pid: -1,
|
| 69 |
+
ppid: -1,
|
| 70 |
+
umask() { throw enosys(); },
|
| 71 |
+
cwd() { throw enosys(); },
|
| 72 |
+
chdir() { throw enosys(); },
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
if (!globalThis.crypto) {
|
| 77 |
+
throw new Error("globalThis.crypto is not available, polyfill required (crypto.getRandomValues only)");
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
if (!globalThis.performance) {
|
| 81 |
+
throw new Error("globalThis.performance is not available, polyfill required (performance.now only)");
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
if (!globalThis.TextEncoder) {
|
| 85 |
+
throw new Error("globalThis.TextEncoder is not available, polyfill required");
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
if (!globalThis.TextDecoder) {
|
| 89 |
+
throw new Error("globalThis.TextDecoder is not available, polyfill required");
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
const encoder = new TextEncoder("utf-8");
|
| 93 |
+
const decoder = new TextDecoder("utf-8");
|
| 94 |
+
|
| 95 |
+
globalThis.Go = class {
|
| 96 |
+
constructor() {
|
| 97 |
+
this.argv = ["js"];
|
| 98 |
+
this.env = {};
|
| 99 |
+
this.exit = (code) => {
|
| 100 |
+
if (code !== 0) {
|
| 101 |
+
console.warn("exit code:", code);
|
| 102 |
+
}
|
| 103 |
+
};
|
| 104 |
+
this._exitPromise = new Promise((resolve) => {
|
| 105 |
+
this._resolveExitPromise = resolve;
|
| 106 |
+
});
|
| 107 |
+
this._pendingEvent = null;
|
| 108 |
+
this._scheduledTimeouts = new Map();
|
| 109 |
+
this._nextCallbackTimeoutID = 1;
|
| 110 |
+
|
| 111 |
+
const setInt64 = (addr, v) => {
|
| 112 |
+
this.mem.setUint32(addr + 0, v, true);
|
| 113 |
+
this.mem.setUint32(addr + 4, Math.floor(v / 4294967296), true);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
const setInt32 = (addr, v) => {
|
| 117 |
+
this.mem.setUint32(addr + 0, v, true);
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
const getInt64 = (addr) => {
|
| 121 |
+
const low = this.mem.getUint32(addr + 0, true);
|
| 122 |
+
const high = this.mem.getInt32(addr + 4, true);
|
| 123 |
+
return low + high * 4294967296;
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
const loadValue = (addr) => {
|
| 127 |
+
const f = this.mem.getFloat64(addr, true);
|
| 128 |
+
if (f === 0) {
|
| 129 |
+
return undefined;
|
| 130 |
+
}
|
| 131 |
+
if (!isNaN(f)) {
|
| 132 |
+
return f;
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
const id = this.mem.getUint32(addr, true);
|
| 136 |
+
return this._values[id];
|
| 137 |
+
}
|
| 138 |
+
|
| 139 |
+
const storeValue = (addr, v) => {
|
| 140 |
+
const nanHead = 0x7FF80000;
|
| 141 |
+
|
| 142 |
+
if (typeof v === "number" && v !== 0) {
|
| 143 |
+
if (isNaN(v)) {
|
| 144 |
+
this.mem.setUint32(addr + 4, nanHead, true);
|
| 145 |
+
this.mem.setUint32(addr, 0, true);
|
| 146 |
+
return;
|
| 147 |
+
}
|
| 148 |
+
this.mem.setFloat64(addr, v, true);
|
| 149 |
+
return;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
if (v === undefined) {
|
| 153 |
+
this.mem.setFloat64(addr, 0, true);
|
| 154 |
+
return;
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
let id = this._ids.get(v);
|
| 158 |
+
if (id === undefined) {
|
| 159 |
+
id = this._idPool.pop();
|
| 160 |
+
if (id === undefined) {
|
| 161 |
+
id = this._values.length;
|
| 162 |
+
}
|
| 163 |
+
this._values[id] = v;
|
| 164 |
+
this._goRefCounts[id] = 0;
|
| 165 |
+
this._ids.set(v, id);
|
| 166 |
+
}
|
| 167 |
+
this._goRefCounts[id]++;
|
| 168 |
+
let typeFlag = 0;
|
| 169 |
+
switch (typeof v) {
|
| 170 |
+
case "object":
|
| 171 |
+
if (v !== null) {
|
| 172 |
+
typeFlag = 1;
|
| 173 |
+
}
|
| 174 |
+
break;
|
| 175 |
+
case "string":
|
| 176 |
+
typeFlag = 2;
|
| 177 |
+
break;
|
| 178 |
+
case "symbol":
|
| 179 |
+
typeFlag = 3;
|
| 180 |
+
break;
|
| 181 |
+
case "function":
|
| 182 |
+
typeFlag = 4;
|
| 183 |
+
break;
|
| 184 |
+
}
|
| 185 |
+
this.mem.setUint32(addr + 4, nanHead | typeFlag, true);
|
| 186 |
+
this.mem.setUint32(addr, id, true);
|
| 187 |
+
}
|
| 188 |
+
|
| 189 |
+
const loadSlice = (addr) => {
|
| 190 |
+
const array = getInt64(addr + 0);
|
| 191 |
+
const len = getInt64(addr + 8);
|
| 192 |
+
return new Uint8Array(this._inst.exports.mem.buffer, array, len);
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
const loadSliceOfValues = (addr) => {
|
| 196 |
+
const array = getInt64(addr + 0);
|
| 197 |
+
const len = getInt64(addr + 8);
|
| 198 |
+
const a = new Array(len);
|
| 199 |
+
for (let i = 0; i < len; i++) {
|
| 200 |
+
a[i] = loadValue(array + i * 8);
|
| 201 |
+
}
|
| 202 |
+
return a;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
const loadString = (addr) => {
|
| 206 |
+
const saddr = getInt64(addr + 0);
|
| 207 |
+
const len = getInt64(addr + 8);
|
| 208 |
+
return decoder.decode(new DataView(this._inst.exports.mem.buffer, saddr, len));
|
| 209 |
+
}
|
| 210 |
+
|
| 211 |
+
const timeOrigin = Date.now() - performance.now();
|
| 212 |
+
this.importObject = {
|
| 213 |
+
_gotest: {
|
| 214 |
+
add: (a, b) => a + b,
|
| 215 |
+
},
|
| 216 |
+
gojs: {
|
| 217 |
+
// Go's SP does not change as long as no Go code is running. Some operations (e.g. calls, getters and setters)
|
| 218 |
+
// may synchronously trigger a Go event handler. This makes Go code get executed in the middle of the imported
|
| 219 |
+
// function. A goroutine can switch to a new stack if the current stack is too small (see morestack function).
|
| 220 |
+
// This changes the SP, thus we have to update the SP used by the imported function.
|
| 221 |
+
|
| 222 |
+
// func wasmExit(code int32)
|
| 223 |
+
"runtime.wasmExit": (sp) => {
|
| 224 |
+
sp >>>= 0;
|
| 225 |
+
const code = this.mem.getInt32(sp + 8, true);
|
| 226 |
+
this.exited = true;
|
| 227 |
+
delete this._inst;
|
| 228 |
+
delete this._values;
|
| 229 |
+
delete this._goRefCounts;
|
| 230 |
+
delete this._ids;
|
| 231 |
+
delete this._idPool;
|
| 232 |
+
this.exit(code);
|
| 233 |
+
},
|
| 234 |
+
|
| 235 |
+
// func wasmWrite(fd uintptr, p unsafe.Pointer, n int32)
|
| 236 |
+
"runtime.wasmWrite": (sp) => {
|
| 237 |
+
sp >>>= 0;
|
| 238 |
+
const fd = getInt64(sp + 8);
|
| 239 |
+
const p = getInt64(sp + 16);
|
| 240 |
+
const n = this.mem.getInt32(sp + 24, true);
|
| 241 |
+
fs.writeSync(fd, new Uint8Array(this._inst.exports.mem.buffer, p, n));
|
| 242 |
+
},
|
| 243 |
+
|
| 244 |
+
// func resetMemoryDataView()
|
| 245 |
+
"runtime.resetMemoryDataView": (sp) => {
|
| 246 |
+
sp >>>= 0;
|
| 247 |
+
this.mem = new DataView(this._inst.exports.mem.buffer);
|
| 248 |
+
},
|
| 249 |
+
|
| 250 |
+
// func nanotime1() int64
|
| 251 |
+
"runtime.nanotime1": (sp) => {
|
| 252 |
+
sp >>>= 0;
|
| 253 |
+
setInt64(sp + 8, (timeOrigin + performance.now()) * 1000000);
|
| 254 |
+
},
|
| 255 |
+
|
| 256 |
+
// func walltime() (sec int64, nsec int32)
|
| 257 |
+
"runtime.walltime": (sp) => {
|
| 258 |
+
sp >>>= 0;
|
| 259 |
+
const msec = (new Date).getTime();
|
| 260 |
+
setInt64(sp + 8, msec / 1000);
|
| 261 |
+
this.mem.setInt32(sp + 16, (msec % 1000) * 1000000, true);
|
| 262 |
+
},
|
| 263 |
+
|
| 264 |
+
// func scheduleTimeoutEvent(delay int64) int32
|
| 265 |
+
"runtime.scheduleTimeoutEvent": (sp) => {
|
| 266 |
+
sp >>>= 0;
|
| 267 |
+
const id = this._nextCallbackTimeoutID;
|
| 268 |
+
this._nextCallbackTimeoutID++;
|
| 269 |
+
this._scheduledTimeouts.set(id, setTimeout(
|
| 270 |
+
() => {
|
| 271 |
+
this._resume();
|
| 272 |
+
while (this._scheduledTimeouts.has(id)) {
|
| 273 |
+
// for some reason Go failed to register the timeout event, log and try again
|
| 274 |
+
// (temporary workaround for https://github.com/golang/go/issues/28975)
|
| 275 |
+
console.warn("scheduleTimeoutEvent: missed timeout event");
|
| 276 |
+
this._resume();
|
| 277 |
+
}
|
| 278 |
+
},
|
| 279 |
+
getInt64(sp + 8),
|
| 280 |
+
));
|
| 281 |
+
this.mem.setInt32(sp + 16, id, true);
|
| 282 |
+
},
|
| 283 |
+
|
| 284 |
+
// func clearTimeoutEvent(id int32)
|
| 285 |
+
"runtime.clearTimeoutEvent": (sp) => {
|
| 286 |
+
sp >>>= 0;
|
| 287 |
+
const id = this.mem.getInt32(sp + 8, true);
|
| 288 |
+
clearTimeout(this._scheduledTimeouts.get(id));
|
| 289 |
+
this._scheduledTimeouts.delete(id);
|
| 290 |
+
},
|
| 291 |
+
|
| 292 |
+
// func getRandomData(r []byte)
|
| 293 |
+
"runtime.getRandomData": (sp) => {
|
| 294 |
+
sp >>>= 0;
|
| 295 |
+
crypto.getRandomValues(loadSlice(sp + 8));
|
| 296 |
+
},
|
| 297 |
+
|
| 298 |
+
// func finalizeRef(v ref)
|
| 299 |
+
"syscall/js.finalizeRef": (sp) => {
|
| 300 |
+
sp >>>= 0;
|
| 301 |
+
const id = this.mem.getUint32(sp + 8, true);
|
| 302 |
+
this._goRefCounts[id]--;
|
| 303 |
+
if (this._goRefCounts[id] === 0) {
|
| 304 |
+
const v = this._values[id];
|
| 305 |
+
this._values[id] = null;
|
| 306 |
+
this._ids.delete(v);
|
| 307 |
+
this._idPool.push(id);
|
| 308 |
+
}
|
| 309 |
+
},
|
| 310 |
+
|
| 311 |
+
// func stringVal(value string) ref
|
| 312 |
+
"syscall/js.stringVal": (sp) => {
|
| 313 |
+
sp >>>= 0;
|
| 314 |
+
storeValue(sp + 24, loadString(sp + 8));
|
| 315 |
+
},
|
| 316 |
+
|
| 317 |
+
// func valueGet(v ref, p string) ref
|
| 318 |
+
"syscall/js.valueGet": (sp) => {
|
| 319 |
+
sp >>>= 0;
|
| 320 |
+
const result = Reflect.get(loadValue(sp + 8), loadString(sp + 16));
|
| 321 |
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
| 322 |
+
storeValue(sp + 32, result);
|
| 323 |
+
},
|
| 324 |
+
|
| 325 |
+
// func valueSet(v ref, p string, x ref)
|
| 326 |
+
"syscall/js.valueSet": (sp) => {
|
| 327 |
+
sp >>>= 0;
|
| 328 |
+
Reflect.set(loadValue(sp + 8), loadString(sp + 16), loadValue(sp + 32));
|
| 329 |
+
},
|
| 330 |
+
|
| 331 |
+
// func valueDelete(v ref, p string)
|
| 332 |
+
"syscall/js.valueDelete": (sp) => {
|
| 333 |
+
sp >>>= 0;
|
| 334 |
+
Reflect.deleteProperty(loadValue(sp + 8), loadString(sp + 16));
|
| 335 |
+
},
|
| 336 |
+
|
| 337 |
+
// func valueIndex(v ref, i int) ref
|
| 338 |
+
"syscall/js.valueIndex": (sp) => {
|
| 339 |
+
sp >>>= 0;
|
| 340 |
+
storeValue(sp + 24, Reflect.get(loadValue(sp + 8), getInt64(sp + 16)));
|
| 341 |
+
},
|
| 342 |
+
|
| 343 |
+
// valueSetIndex(v ref, i int, x ref)
|
| 344 |
+
"syscall/js.valueSetIndex": (sp) => {
|
| 345 |
+
sp >>>= 0;
|
| 346 |
+
Reflect.set(loadValue(sp + 8), getInt64(sp + 16), loadValue(sp + 24));
|
| 347 |
+
},
|
| 348 |
+
|
| 349 |
+
// func valueCall(v ref, m string, args []ref) (ref, bool)
|
| 350 |
+
"syscall/js.valueCall": (sp) => {
|
| 351 |
+
sp >>>= 0;
|
| 352 |
+
try {
|
| 353 |
+
const v = loadValue(sp + 8);
|
| 354 |
+
const m = Reflect.get(v, loadString(sp + 16));
|
| 355 |
+
const args = loadSliceOfValues(sp + 32);
|
| 356 |
+
const result = Reflect.apply(m, v, args);
|
| 357 |
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
| 358 |
+
storeValue(sp + 56, result);
|
| 359 |
+
this.mem.setUint8(sp + 64, 1);
|
| 360 |
+
} catch (err) {
|
| 361 |
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
| 362 |
+
storeValue(sp + 56, err);
|
| 363 |
+
this.mem.setUint8(sp + 64, 0);
|
| 364 |
+
}
|
| 365 |
+
},
|
| 366 |
+
|
| 367 |
+
// func valueInvoke(v ref, args []ref) (ref, bool)
|
| 368 |
+
"syscall/js.valueInvoke": (sp) => {
|
| 369 |
+
sp >>>= 0;
|
| 370 |
+
try {
|
| 371 |
+
const v = loadValue(sp + 8);
|
| 372 |
+
const args = loadSliceOfValues(sp + 16);
|
| 373 |
+
const result = Reflect.apply(v, undefined, args);
|
| 374 |
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
| 375 |
+
storeValue(sp + 40, result);
|
| 376 |
+
this.mem.setUint8(sp + 48, 1);
|
| 377 |
+
} catch (err) {
|
| 378 |
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
| 379 |
+
storeValue(sp + 40, err);
|
| 380 |
+
this.mem.setUint8(sp + 48, 0);
|
| 381 |
+
}
|
| 382 |
+
},
|
| 383 |
+
|
| 384 |
+
// func valueNew(v ref, args []ref) (ref, bool)
|
| 385 |
+
"syscall/js.valueNew": (sp) => {
|
| 386 |
+
sp >>>= 0;
|
| 387 |
+
try {
|
| 388 |
+
const v = loadValue(sp + 8);
|
| 389 |
+
const args = loadSliceOfValues(sp + 16);
|
| 390 |
+
const result = Reflect.construct(v, args);
|
| 391 |
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
| 392 |
+
storeValue(sp + 40, result);
|
| 393 |
+
this.mem.setUint8(sp + 48, 1);
|
| 394 |
+
} catch (err) {
|
| 395 |
+
sp = this._inst.exports.getsp() >>> 0; // see comment above
|
| 396 |
+
storeValue(sp + 40, err);
|
| 397 |
+
this.mem.setUint8(sp + 48, 0);
|
| 398 |
+
}
|
| 399 |
+
},
|
| 400 |
+
|
| 401 |
+
// func valueLength(v ref) int
|
| 402 |
+
"syscall/js.valueLength": (sp) => {
|
| 403 |
+
sp >>>= 0;
|
| 404 |
+
setInt64(sp + 16, parseInt(loadValue(sp + 8).length));
|
| 405 |
+
},
|
| 406 |
+
|
| 407 |
+
// valuePrepareString(v ref) (ref, int)
|
| 408 |
+
"syscall/js.valuePrepareString": (sp) => {
|
| 409 |
+
sp >>>= 0;
|
| 410 |
+
const str = encoder.encode(String(loadValue(sp + 8)));
|
| 411 |
+
storeValue(sp + 16, str);
|
| 412 |
+
setInt64(sp + 24, str.length);
|
| 413 |
+
},
|
| 414 |
+
|
| 415 |
+
// valueLoadString(v ref, b []byte)
|
| 416 |
+
"syscall/js.valueLoadString": (sp) => {
|
| 417 |
+
sp >>>= 0;
|
| 418 |
+
const str = loadValue(sp + 8);
|
| 419 |
+
loadSlice(sp + 16).set(str);
|
| 420 |
+
},
|
| 421 |
+
|
| 422 |
+
// func valueInstanceOf(v ref, t ref) bool
|
| 423 |
+
"syscall/js.valueInstanceOf": (sp) => {
|
| 424 |
+
sp >>>= 0;
|
| 425 |
+
this.mem.setUint8(sp + 24, (loadValue(sp + 8) instanceof loadValue(sp + 16)) ? 1 : 0);
|
| 426 |
+
},
|
| 427 |
+
|
| 428 |
+
// func copyBytesToGo(dst []byte, src ref) (int, bool)
|
| 429 |
+
"syscall/js.copyBytesToGo": (sp) => {
|
| 430 |
+
sp >>>= 0;
|
| 431 |
+
const dst = loadSlice(sp + 8);
|
| 432 |
+
const src = loadValue(sp + 32);
|
| 433 |
+
if (!(src instanceof Uint8Array || src instanceof Uint8ClampedArray)) {
|
| 434 |
+
this.mem.setUint8(sp + 48, 0);
|
| 435 |
+
return;
|
| 436 |
+
}
|
| 437 |
+
const toCopy = src.subarray(0, dst.length);
|
| 438 |
+
dst.set(toCopy);
|
| 439 |
+
setInt64(sp + 40, toCopy.length);
|
| 440 |
+
this.mem.setUint8(sp + 48, 1);
|
| 441 |
+
},
|
| 442 |
+
|
| 443 |
+
// func copyBytesToJS(dst ref, src []byte) (int, bool)
|
| 444 |
+
"syscall/js.copyBytesToJS": (sp) => {
|
| 445 |
+
sp >>>= 0;
|
| 446 |
+
const dst = loadValue(sp + 8);
|
| 447 |
+
const src = loadSlice(sp + 16);
|
| 448 |
+
if (!(dst instanceof Uint8Array || dst instanceof Uint8ClampedArray)) {
|
| 449 |
+
this.mem.setUint8(sp + 48, 0);
|
| 450 |
+
return;
|
| 451 |
+
}
|
| 452 |
+
const toCopy = src.subarray(0, dst.length);
|
| 453 |
+
dst.set(toCopy);
|
| 454 |
+
setInt64(sp + 40, toCopy.length);
|
| 455 |
+
this.mem.setUint8(sp + 48, 1);
|
| 456 |
+
},
|
| 457 |
+
|
| 458 |
+
"debug": (value) => {
|
| 459 |
+
console.log(value);
|
| 460 |
+
},
|
| 461 |
+
}
|
| 462 |
+
};
|
| 463 |
+
}
|
| 464 |
+
|
| 465 |
+
async run(instance) {
|
| 466 |
+
if (!(instance instanceof WebAssembly.Instance)) {
|
| 467 |
+
throw new Error("Go.run: WebAssembly.Instance expected");
|
| 468 |
+
}
|
| 469 |
+
this._inst = instance;
|
| 470 |
+
this.mem = new DataView(this._inst.exports.mem.buffer);
|
| 471 |
+
this._values = [ // JS values that Go currently has references to, indexed by reference id
|
| 472 |
+
NaN,
|
| 473 |
+
0,
|
| 474 |
+
null,
|
| 475 |
+
true,
|
| 476 |
+
false,
|
| 477 |
+
globalThis,
|
| 478 |
+
this,
|
| 479 |
+
];
|
| 480 |
+
this._goRefCounts = new Array(this._values.length).fill(Infinity); // number of references that Go has to a JS value, indexed by reference id
|
| 481 |
+
this._ids = new Map([ // mapping from JS values to reference ids
|
| 482 |
+
[0, 1],
|
| 483 |
+
[null, 2],
|
| 484 |
+
[true, 3],
|
| 485 |
+
[false, 4],
|
| 486 |
+
[globalThis, 5],
|
| 487 |
+
[this, 6],
|
| 488 |
+
]);
|
| 489 |
+
this._idPool = []; // unused ids that have been garbage collected
|
| 490 |
+
this.exited = false; // whether the Go program has exited
|
| 491 |
+
|
| 492 |
+
// Pass command line arguments and environment variables to WebAssembly by writing them to the linear memory.
|
| 493 |
+
let offset = 4096;
|
| 494 |
+
|
| 495 |
+
const strPtr = (str) => {
|
| 496 |
+
const ptr = offset;
|
| 497 |
+
const bytes = encoder.encode(str + "\0");
|
| 498 |
+
new Uint8Array(this.mem.buffer, offset, bytes.length).set(bytes);
|
| 499 |
+
offset += bytes.length;
|
| 500 |
+
if (offset % 8 !== 0) {
|
| 501 |
+
offset += 8 - (offset % 8);
|
| 502 |
+
}
|
| 503 |
+
return ptr;
|
| 504 |
+
};
|
| 505 |
+
|
| 506 |
+
const argc = this.argv.length;
|
| 507 |
+
|
| 508 |
+
const argvPtrs = [];
|
| 509 |
+
this.argv.forEach((arg) => {
|
| 510 |
+
argvPtrs.push(strPtr(arg));
|
| 511 |
+
});
|
| 512 |
+
argvPtrs.push(0);
|
| 513 |
+
|
| 514 |
+
const keys = Object.keys(this.env).sort();
|
| 515 |
+
keys.forEach((key) => {
|
| 516 |
+
argvPtrs.push(strPtr(`${key}=${this.env[key]}`));
|
| 517 |
+
});
|
| 518 |
+
argvPtrs.push(0);
|
| 519 |
+
|
| 520 |
+
const argv = offset;
|
| 521 |
+
argvPtrs.forEach((ptr) => {
|
| 522 |
+
this.mem.setUint32(offset, ptr, true);
|
| 523 |
+
this.mem.setUint32(offset + 4, 0, true);
|
| 524 |
+
offset += 8;
|
| 525 |
+
});
|
| 526 |
+
|
| 527 |
+
// The linker guarantees global data starts from at least wasmMinDataAddr.
|
| 528 |
+
// Keep in sync with cmd/link/internal/ld/data.go:wasmMinDataAddr.
|
| 529 |
+
const wasmMinDataAddr = 4096 + 8192;
|
| 530 |
+
if (offset >= wasmMinDataAddr) {
|
| 531 |
+
throw new Error("total length of command line and environment variables exceeds limit");
|
| 532 |
+
}
|
| 533 |
+
|
| 534 |
+
this._inst.exports.run(argc, argv);
|
| 535 |
+
if (this.exited) {
|
| 536 |
+
this._resolveExitPromise();
|
| 537 |
+
}
|
| 538 |
+
await this._exitPromise;
|
| 539 |
+
}
|
| 540 |
+
|
| 541 |
+
_resume() {
|
| 542 |
+
if (this.exited) {
|
| 543 |
+
throw new Error("Go program has already exited");
|
| 544 |
+
}
|
| 545 |
+
this._inst.exports.resume();
|
| 546 |
+
if (this.exited) {
|
| 547 |
+
this._resolveExitPromise();
|
| 548 |
+
}
|
| 549 |
+
}
|
| 550 |
+
|
| 551 |
+
_makeFuncWrapper(id) {
|
| 552 |
+
const go = this;
|
| 553 |
+
return function () {
|
| 554 |
+
const event = { id: id, this: this, args: arguments };
|
| 555 |
+
go._pendingEvent = event;
|
| 556 |
+
go._resume();
|
| 557 |
+
return event.result;
|
| 558 |
+
};
|
| 559 |
+
}
|
| 560 |
+
}
|
| 561 |
+
})();
|
platform/dbops/binaries/go/go/misc/wasm/wasm_exec_node.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2021 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 |
+
"use strict";
|
| 6 |
+
|
| 7 |
+
if (process.argv.length < 3) {
|
| 8 |
+
console.error("usage: go_js_wasm_exec [wasm binary] [arguments]");
|
| 9 |
+
process.exit(1);
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
globalThis.require = require;
|
| 13 |
+
globalThis.fs = require("fs");
|
| 14 |
+
globalThis.TextEncoder = require("util").TextEncoder;
|
| 15 |
+
globalThis.TextDecoder = require("util").TextDecoder;
|
| 16 |
+
|
| 17 |
+
globalThis.performance ??= require("performance");
|
| 18 |
+
|
| 19 |
+
globalThis.crypto ??= require("crypto");
|
| 20 |
+
|
| 21 |
+
require("./wasm_exec");
|
| 22 |
+
|
| 23 |
+
const go = new Go();
|
| 24 |
+
go.argv = process.argv.slice(2);
|
| 25 |
+
go.env = Object.assign({ TMPDIR: require("os").tmpdir() }, process.env);
|
| 26 |
+
go.exit = process.exit;
|
| 27 |
+
WebAssembly.instantiate(fs.readFileSync(process.argv[2]), go.importObject).then((result) => {
|
| 28 |
+
process.on("exit", (code) => { // Node.js exits if no event handler is pending
|
| 29 |
+
if (code === 0 && !go.exited) {
|
| 30 |
+
// deadlock, make Go print error and stack traces
|
| 31 |
+
go._pendingEvent = { id: 0 };
|
| 32 |
+
go._resume();
|
| 33 |
+
}
|
| 34 |
+
});
|
| 35 |
+
return go.run(result.instance);
|
| 36 |
+
}).catch((err) => {
|
| 37 |
+
console.error(err);
|
| 38 |
+
process.exit(1);
|
| 39 |
+
});
|
platform/dbops/binaries/go/go/pkg/include/asm_amd64.h
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2021 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 |
+
// Define features that are guaranteed to be supported by setting the AMD64 variable.
|
| 6 |
+
// If a feature is supported, there's no need to check it at runtime every time.
|
| 7 |
+
|
| 8 |
+
#ifdef GOAMD64_v2
|
| 9 |
+
#define hasPOPCNT
|
| 10 |
+
#define hasSSE42
|
| 11 |
+
#endif
|
| 12 |
+
|
| 13 |
+
#ifdef GOAMD64_v3
|
| 14 |
+
#define hasAVX
|
| 15 |
+
#define hasAVX2
|
| 16 |
+
#define hasPOPCNT
|
| 17 |
+
#define hasSSE42
|
| 18 |
+
#endif
|
| 19 |
+
|
| 20 |
+
#ifdef GOAMD64_v4
|
| 21 |
+
#define hasAVX
|
| 22 |
+
#define hasAVX2
|
| 23 |
+
#define hasAVX512F
|
| 24 |
+
#define hasAVX512BW
|
| 25 |
+
#define hasAVX512VL
|
| 26 |
+
#define hasPOPCNT
|
| 27 |
+
#define hasSSE42
|
| 28 |
+
#endif
|
platform/dbops/binaries/go/go/pkg/include/asm_ppc64x.h
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
// FIXED_FRAME defines the size of the fixed part of a stack frame. A stack
|
| 6 |
+
// frame looks like this:
|
| 7 |
+
//
|
| 8 |
+
// +---------------------+
|
| 9 |
+
// | local variable area |
|
| 10 |
+
// +---------------------+
|
| 11 |
+
// | argument area |
|
| 12 |
+
// +---------------------+ <- R1+FIXED_FRAME
|
| 13 |
+
// | fixed area |
|
| 14 |
+
// +---------------------+ <- R1
|
| 15 |
+
//
|
| 16 |
+
// So a function that sets up a stack frame at all uses as least FIXED_FRAME
|
| 17 |
+
// bytes of stack. This mostly affects assembly that calls other functions
|
| 18 |
+
// with arguments (the arguments should be stored at FIXED_FRAME+0(R1),
|
| 19 |
+
// FIXED_FRAME+8(R1) etc) and some other low-level places.
|
| 20 |
+
//
|
| 21 |
+
// The reason for using a constant is to make supporting PIC easier (although
|
| 22 |
+
// we only support PIC on ppc64le which has a minimum 32 bytes of stack frame,
|
| 23 |
+
// and currently always use that much, PIC on ppc64 would need to use 48).
|
| 24 |
+
|
| 25 |
+
#define FIXED_FRAME 32
|
| 26 |
+
|
| 27 |
+
// aix/ppc64 uses XCOFF which uses function descriptors.
|
| 28 |
+
// AIX cannot perform the TOC relocation in a text section.
|
| 29 |
+
// Therefore, these descriptors must live in a data section.
|
| 30 |
+
#ifdef GOOS_aix
|
| 31 |
+
#ifdef GOARCH_ppc64
|
| 32 |
+
#define GO_PPC64X_HAS_FUNCDESC
|
| 33 |
+
#define DEFINE_PPC64X_FUNCDESC(funcname, localfuncname) \
|
| 34 |
+
DATA funcname+0(SB)/8, $localfuncname(SB) \
|
| 35 |
+
DATA funcname+8(SB)/8, $TOC(SB) \
|
| 36 |
+
DATA funcname+16(SB)/8, $0 \
|
| 37 |
+
GLOBL funcname(SB), NOPTR, $24
|
| 38 |
+
#endif
|
| 39 |
+
#endif
|
| 40 |
+
|
| 41 |
+
// linux/ppc64 uses ELFv1 which uses function descriptors.
|
| 42 |
+
// These must also look like ABI0 functions on linux/ppc64
|
| 43 |
+
// to work with abi.FuncPCABI0(sigtramp) in os_linux.go.
|
| 44 |
+
// Only static codegen is supported on linux/ppc64, so TOC
|
| 45 |
+
// is not needed.
|
| 46 |
+
#ifdef GOOS_linux
|
| 47 |
+
#ifdef GOARCH_ppc64
|
| 48 |
+
#define GO_PPC64X_HAS_FUNCDESC
|
| 49 |
+
#define DEFINE_PPC64X_FUNCDESC(funcname, localfuncname) \
|
| 50 |
+
TEXT funcname(SB),NOSPLIT|NOFRAME,$0 \
|
| 51 |
+
DWORD $localfuncname(SB) \
|
| 52 |
+
DWORD $0 \
|
| 53 |
+
DWORD $0
|
| 54 |
+
#endif
|
| 55 |
+
#endif
|
platform/dbops/binaries/go/go/pkg/include/funcdata.h
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2013 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 |
+
// This file defines the IDs for PCDATA and FUNCDATA instructions
|
| 6 |
+
// in Go binaries. It is included by assembly sources, so it must
|
| 7 |
+
// be written using #defines.
|
| 8 |
+
//
|
| 9 |
+
// These must agree with internal/abi/symtab.go.
|
| 10 |
+
|
| 11 |
+
#define PCDATA_UnsafePoint 0
|
| 12 |
+
#define PCDATA_StackMapIndex 1
|
| 13 |
+
#define PCDATA_InlTreeIndex 2
|
| 14 |
+
#define PCDATA_ArgLiveIndex 3
|
| 15 |
+
|
| 16 |
+
#define FUNCDATA_ArgsPointerMaps 0 /* garbage collector blocks */
|
| 17 |
+
#define FUNCDATA_LocalsPointerMaps 1
|
| 18 |
+
#define FUNCDATA_StackObjects 2
|
| 19 |
+
#define FUNCDATA_InlTree 3
|
| 20 |
+
#define FUNCDATA_OpenCodedDeferInfo 4 /* info for func with open-coded defers */
|
| 21 |
+
#define FUNCDATA_ArgInfo 5
|
| 22 |
+
#define FUNCDATA_ArgLiveInfo 6
|
| 23 |
+
#define FUNCDATA_WrapInfo 7
|
| 24 |
+
|
| 25 |
+
// Pseudo-assembly statements.
|
| 26 |
+
|
| 27 |
+
// GO_ARGS, GO_RESULTS_INITIALIZED, and NO_LOCAL_POINTERS are macros
|
| 28 |
+
// that communicate to the runtime information about the location and liveness
|
| 29 |
+
// of pointers in an assembly function's arguments, results, and stack frame.
|
| 30 |
+
// This communication is only required in assembly functions that make calls
|
| 31 |
+
// to other functions that might be preempted or grow the stack.
|
| 32 |
+
// NOSPLIT functions that make no calls do not need to use these macros.
|
| 33 |
+
|
| 34 |
+
// GO_ARGS indicates that the Go prototype for this assembly function
|
| 35 |
+
// defines the pointer map for the function's arguments.
|
| 36 |
+
// GO_ARGS should be the first instruction in a function that uses it.
|
| 37 |
+
// It can be omitted if there are no arguments at all.
|
| 38 |
+
// GO_ARGS is inserted implicitly by the assembler for any function
|
| 39 |
+
// whose package-qualified symbol name belongs to the current package;
|
| 40 |
+
// it is therefore usually not necessary to write explicitly.
|
| 41 |
+
#define GO_ARGS FUNCDATA $FUNCDATA_ArgsPointerMaps, go_args_stackmap(SB)
|
| 42 |
+
|
| 43 |
+
// GO_RESULTS_INITIALIZED indicates that the assembly function
|
| 44 |
+
// has initialized the stack space for its results and that those results
|
| 45 |
+
// should be considered live for the remainder of the function.
|
| 46 |
+
#define GO_RESULTS_INITIALIZED PCDATA $PCDATA_StackMapIndex, $1
|
| 47 |
+
|
| 48 |
+
// NO_LOCAL_POINTERS indicates that the assembly function stores
|
| 49 |
+
// no pointers to heap objects in its local stack variables.
|
| 50 |
+
#define NO_LOCAL_POINTERS FUNCDATA $FUNCDATA_LocalsPointerMaps, no_pointers_stackmap(SB)
|
| 51 |
+
|
| 52 |
+
// ArgsSizeUnknown is set in Func.argsize to mark all functions
|
| 53 |
+
// whose argument size is unknown (C vararg functions, and
|
| 54 |
+
// assembly code without an explicit specification).
|
| 55 |
+
// This value is generated by the compiler, assembler, or linker.
|
| 56 |
+
#define ArgsSizeUnknown 0x80000000
|
platform/dbops/binaries/go/go/pkg/include/textflag.h
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Copyright 2013 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 |
+
// This file defines flags attached to various functions
|
| 6 |
+
// and data objects. The compilers, assemblers, and linker must
|
| 7 |
+
// all agree on these values.
|
| 8 |
+
//
|
| 9 |
+
// Keep in sync with src/cmd/internal/obj/textflag.go.
|
| 10 |
+
|
| 11 |
+
// Don't profile the marked routine. This flag is deprecated.
|
| 12 |
+
#define NOPROF 1
|
| 13 |
+
// It is ok for the linker to get multiple of these symbols. It will
|
| 14 |
+
// pick one of the duplicates to use.
|
| 15 |
+
#define DUPOK 2
|
| 16 |
+
// Don't insert stack check preamble.
|
| 17 |
+
#define NOSPLIT 4
|
| 18 |
+
// Put this data in a read-only section.
|
| 19 |
+
#define RODATA 8
|
| 20 |
+
// This data contains no pointers.
|
| 21 |
+
#define NOPTR 16
|
| 22 |
+
// This is a wrapper function and should not count as disabling 'recover'.
|
| 23 |
+
#define WRAPPER 32
|
| 24 |
+
// This function uses its incoming context register.
|
| 25 |
+
#define NEEDCTXT 64
|
| 26 |
+
// Allocate a word of thread local storage and store the offset from the
|
| 27 |
+
// thread local base to the thread local storage in this variable.
|
| 28 |
+
#define TLSBSS 256
|
| 29 |
+
// Do not insert instructions to allocate a stack frame for this function.
|
| 30 |
+
// Only valid on functions that declare a frame size of 0.
|
| 31 |
+
#define NOFRAME 512
|
| 32 |
+
// Function can call reflect.Type.Method or reflect.Type.MethodByName.
|
| 33 |
+
#define REFLECTMETHOD 1024
|
| 34 |
+
// Function is the outermost frame of the call stack. Call stack unwinders
|
| 35 |
+
// should stop at this function.
|
| 36 |
+
#define TOPFRAME 2048
|
| 37 |
+
// Function is an ABI wrapper.
|
| 38 |
+
#define ABIWRAPPER 4096
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/addr2line
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0b02a8739961c2046260428136f7eae9ce9945dc507e4933015ed47ec9bd2692
|
| 3 |
+
size 2438886
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/asm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:15caf769e16ef4f45946ad3a75d1db530e24c85fa475c0349f53aa6b918f3d88
|
| 3 |
+
size 3831435
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/buildid
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c10ad54c78cca8020d4f80a39f3aad81978c71e783aaf7fcdeab1640f2e8d42a
|
| 3 |
+
size 1912930
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/cgo
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6c1ac2603d5bf81a81dfa23cc758e9ff1d6e91f960558bec23e8a5f2901fd78b
|
| 3 |
+
size 3740784
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/compile
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f383e7e2ca22d5a919f090d853dc64affcfbacee08cba01f2deaa2028b09670e
|
| 3 |
+
size 19344805
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/covdata
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6f0ccc0d9c6af3436010511331a4b366dcf1416ebadfab92ee6267c175a30401
|
| 3 |
+
size 2469664
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/cover
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:afaa7cbaf4b5ffd934db56332244d1642cfa995ebd93a1cc8fcd906e97f8a96e
|
| 3 |
+
size 4262861
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/doc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:53252c278c34c6d391ee989266a170a5e0b169cd5e5b4b16f3c7cacae9b81599
|
| 3 |
+
size 3092557
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/fix
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e1b45cdc2462ba0805d6360ec15442bd1b1ea0d943c82ce8f6c8c9d3567f59cc
|
| 3 |
+
size 2587022
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/link
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6e1dba72dde7241fbf2ee48d673207cc0ff919aa4c20d1012d3a0e8ebd82a0a5
|
| 3 |
+
size 5294592
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/nm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:762dae3c3b6be8ab66bc6500bf59d3fc2e32630d6403d874e12694455ef9f6ae
|
| 3 |
+
size 2383139
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/objdump
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:235f23fcbb5ba7ab0f1fef284a659ecb5afcc439205f8269683295189d85283f
|
| 3 |
+
size 3474997
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/pack
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b6bd5de4c7fddc4cb4b47a6d4d02b24657da625ab468d4487cb4d38be57101ee
|
| 3 |
+
size 1648605
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/pprof
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f096330f9273813f249bb33a9b6e500b07ebc481ac8c7c20649878220bde4e2a
|
| 3 |
+
size 11756511
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/test2json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:938056a19176a46de4e22235b7e1f9ccd553346a8bcb55583dd55753b44f56a1
|
| 3 |
+
size 2034509
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/trace
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5059b667ea1fdd76f651f0e72d212f0334e72bdd65414725bf0ad0c72ec759d8
|
| 3 |
+
size 12107541
|
platform/dbops/binaries/go/go/pkg/tool/linux_amd64/vet
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:14956467adeefd6dfd463dc162df0a7e66ff242d00164eb6bbb2923320ec2fed
|
| 3 |
+
size 6119625
|