transpile.sh (3013B)
1 #!/bin/sh 2 # 3 # Transpile libwebp to Go with ccgo v4, one file per GOOS/GOARCH. 4 # 5 # Requires: 6 # ccgo v4 go install modernc.org/ccgo/v4@latest 7 # zig darwin headers (replaces osxcross; no Apple SDK needed) 8 # x86_64-w64-mingw32-gcc windows headers 9 # aarch64-linux-gnu-gcc linux/arm64 headers 10 # 11 # ccgo v4 supplies target predefines via --goos/--goarch but not target libc 12 # headers, so each cross target needs a header source. zig covers darwin from 13 # its bundled any-macos-any headers, which is what removes the osxcross and 14 # Apple SDK requirement. It does not cover linux: ccgo cannot parse zig's 15 # generic-glibc bits/types.h, so linux/arm64 still uses a real cross compiler 16 # and linux/amd64 just uses the host. 17 18 set -eux 19 20 HERE=$(cd "$(dirname "$0")" && pwd) 21 SRC=$HERE/../c-lib 22 OUT=$HERE/../lib/transpiled/webp 23 ZIG=${ZIG:-zig} 24 ZIGLIB=$($ZIG env | sed -n 's/.*\.lib_dir[ ]*=[ ]*"\([^"]*\)".*/\1/p') 25 26 # dsp.h only enables SIMD when HAVE_CONFIG_H is set together with 27 # WEBP_HAVE_SSE2, so an empty config.h selects the scalar paths. ccgo has no 28 # -msse2 to turn off. 29 INC=$(mktemp -d) 30 mkdir -p "$INC/src/webp" 31 # HAVE_BUILTIN_BSWAP* matter: endian_inl_utils.h only probes for the builtins 32 # when HAVE_CONFIG_H is absent, so without these libwebp falls back to x86 33 # inline asm, which ccgo turns into a runtime assertion that fires on the 34 # first lossy decode. A real autotools configure detects these too. 35 cat > "$INC/src/webp/config.h" <<'CONFIG' 36 #define HAVE_BUILTIN_BSWAP16 1 37 #define HAVE_BUILTIN_BSWAP32 1 38 #define HAVE_BUILTIN_BSWAP64 1 39 CONFIG 40 trap 'rm -rf "$INC"' EXIT 41 42 # libwebp declares GetCoeffs as a volatile function pointer; ccgo v4 cannot 43 # emit a call through one and aborts. Threading is disabled in this build, so 44 # dropping volatile is safe. 45 # 46 # Patched in place rather than in a scratch copy: ccgo resolves 47 # modernc.org/libc through the module it is run from, so it has to run inside 48 # this repository. 49 VP8DEC=$SRC/src/dec/vp8_dec.c 50 cp "$VP8DEC" "$VP8DEC.orig" 51 trap 'mv -f "$VP8DEC.orig" "$VP8DEC"; rm -rf "$INC"' EXIT 52 sed -i 's/^static volatile GetCoeffsFunc GetCoeffs = NULL;/static GetCoeffsFunc GetCoeffs = NULL;/' \ 53 "$VP8DEC" 54 55 transpile() { 56 goos=$1 57 goarch=$2 58 shift 2 59 echo "=== $goos/$goarch ===" 60 ( cd "$SRC" && ccgo \ 61 --goos="$goos" --goarch="$goarch" \ 62 --package-name=webp \ 63 -ignore-asm-errors \ 64 "$@" \ 65 -o "$OUT/libwebp_${goos}_${goarch}.go" \ 66 -DNDEBUG -DHAVE_CONFIG_H -I"$INC" -I. \ 67 src/dec/*.c src/enc/*.c src/utils/*.c src/dsp/*.c \ 68 "$HERE/sharpyuv_stub.c" ) 69 gofmt -w "$OUT/libwebp_${goos}_${goarch}.go" 70 } 71 72 MACOS="-nostdinc -isystem $ZIGLIB/include -isystem $ZIGLIB/libc/include/any-macos-any" 73 74 transpile linux amd64 75 transpile linux arm64 --cpp=aarch64-linux-gnu-gcc 76 transpile darwin amd64 $MACOS 77 transpile darwin arm64 $MACOS 78 transpile windows amd64 --cpp=x86_64-w64-mingw32-gcc 79 transpile windows arm64 --cpp=x86_64-w64-mingw32-gcc 80 81 ls -la "$OUT"