From 79030e69124868268b330b40e11544525723ddeb Mon Sep 17 00:00:00 2001 From: missinglink Date: Mon, 3 Feb 2020 11:53:44 +0100 Subject: [PATCH] xcompile: add some uncommitted code from my dev machine --- .gitignore | 1 + xcompile.sh | 130 ++++++++++++++-------------------------------------- 2 files changed, 35 insertions(+), 96 deletions(-) diff --git a/.gitignore b/.gitignore index a9f55a6..d641f24 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ pbf nodejs/ +build/ *.bits *.sqlite diff --git a/xcompile.sh b/xcompile.sh index 99aac2d..0fc517b 100755 --- a/xcompile.sh +++ b/xcompile.sh @@ -1,10 +1,8 @@ #!/bin/bash set -euo pipefail -# note: works with golang 1.7 but not on 1.8! - -# requires 'upx' -SKIP_COMPRESSION=true; +# ensure build directory exists +mkdir build # ensure the compiler exits with status 0 function assert() { @@ -14,113 +12,53 @@ function assert() { fi } -# ensure the files were compiled to the correct architecture -declare -A matrix -matrix["build/pbf.darwin-x64"]="Mach-O 64-bit " -matrix["build/pbf.linux-arm"]="ELF 32-bit " -matrix["build/pbf.linux-x64"]="ELF 64-bit " -matrix["build/pbf.win32-x64"]="PE32 executable " - -function checkFiles() { - for path in "${!matrix[@]}" - do - expected="$path: ${matrix[$path]}"; - actual=$(file $path); - actualArr=($actual); - start=$(printf "%s " "${actualArr[@]:0:3}"); - if [ "${start}" != "$expected" ]; then - echo "invalid file architecture: $path" - echo "expected: $expected" - echo "actual: $actual" - echo "start: $start" - exit 1 - fi - done +# check the reported file class matches what's expected +function check() { + actual=$(file -b ${1}); + if [[ "${actual}" != "${2}"* ]]; then + echo "invalid file architecture: ${1}" + echo "expected: ${2}" + echo "actual: ${actual}" + echo "-${actual}-${2}-" + exit 1 + fi } -echo "[compile] linux x64"; -GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH pbf.go; -assert $?; -chmod +x pbf; -mv pbf build/pbf.linux-x64; -$SKIP_COMPRESSION || upx --brute build/pbf.linux-x64; +# if the 'UPX' bindary packer is available, use it +# https://upx.github.io/ +function compress() { + [ -x "$(command -v upx)" ] && upx "${1}" +} echo "[compile] linux arm"; -GOOS=linux GOARCH=arm go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH pbf.go; -echo $?; +env GOOS=linux GOARCH=arm go build -ldflags="-s -w" -gcflags=-trimpath="${GOPATH}" -asmflags=-trimpath="${GOPATH}"; assert $?; chmod +x pbf; mv pbf build/pbf.linux-arm; -$SKIP_COMPRESSION || upx --brute build/pbf.linux-arm; +check 'build/pbf.linux-arm' 'ELF 32-bit LSB executable'; +compress 'build/pbf.linux-arm'; -# echo "[compile] linux i386"; -# GOOS=linux GOARCH=386 go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH pbf.go; -# assert $?; -# chmod +x pbf; -# mv pbf build/pbf.linux-ia32; -# $SKIP_COMPRESSION || upx --brute build/pbf.linux-ia32; +echo "[compile] linux x64"; +env GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -gcflags=-trimpath="${GOPATH}" -asmflags=-trimpath="${GOPATH}"; +assert $?; +chmod +x pbf; +mv pbf build/pbf.linux-x64; +check 'build/pbf.linux-x64' 'ELF 64-bit LSB executable'; +compress 'build/pbf.linux-x64'; echo "[compile] darwin x64"; -GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH pbf.go; +env GOOS=darwin GOARCH=amd64 go build -ldflags="-s -w" -gcflags=-trimpath="${GOPATH}" -asmflags=-trimpath="${GOPATH}"; assert $?; chmod +x pbf; mv pbf build/pbf.darwin-x64; -$SKIP_COMPRESSION || upx --brute build/pbf.darwin-x64; - -# echo "[compile] darwin i386"; -# GOOS=darwin GOARCH=386 go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH pbf.go; -# assert $?; -# chmod +x pbf; -# mv pbf build/pbf.darwin-ia32; -# $SKIP_COMPRESSION || upx --brute build/pbf.darwin-ia32; +check 'build/pbf.darwin-x64' 'Mach-O 64-bit'; +# UPX disabled due to https://github.com/upx/upx/issues/187 +# compress 'build/pbf.darwin-x64'; echo "[compile] windows x64"; -GOOS=windows GOARCH=386 go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -o pbf.exe pbf.go; +env GOOS=windows GOARCH=386 go build -ldflags="-s -w" -gcflags=-trimpath=${GOPATH} -asmflags=-trimpath=${GOPATH} -o pbf.exe; assert $?; chmod +x pbf.exe; mv pbf.exe build/pbf.win32-x64; -$SKIP_COMPRESSION || upx --brute build/pbf.win32-x64; - -# echo "[compile] windows i386"; -# GOOS=windows GOARCH=386 go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH pbf.go; -# assert $?; -# chmod +x pbf.exe; -# mv pbf.exe build/pbf.win32-ia32; -# $SKIP_COMPRESSION || upx --brute build/pbf.win32-ia32; - -# echo "[compile] freebsd arm"; -# GOOS=freebsd GOARCH=arm go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH pbf.go; -# assert $?; -# chmod +x pbf; -# mv pbf build/pbf.freebsd-arm; -# $SKIP_COMPRESSION || upx --brute build/pbf.freebsd-arm; - -# echo "[compile] freebsd x64"; -# GOOS=freebsd GOARCH=amd64 go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH pbf.go; -# assert $?; -# chmod +x pbf; -# mv pbf build/pbf.freebsd-x64; -# $SKIP_COMPRESSION || upx --brute build/pbf.freebsd-x64; - -# echo "[compile] freebsd i386"; -# GOOS=freebsd GOARCH=386 go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH pbf.go; -# assert $?; -# chmod +x pbf; -# mv pbf build/pbf.freebsd-ia32; -# $SKIP_COMPRESSION || upx --brute build/pbf.freebsd-ia32; - -# echo "[compile] openbsd i386"; -# GOOS=openbsd GOARCH=386 go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH pbf.go; -# assert $?; -# chmod +x pbf; -# mv pbf build/pbf.openbsd-ia32; -# $SKIP_COMPRESSION || upx --brute build/pbf.openbsd-ia32; - -# echo "[compile] openbsd x64"; -# GOOS=openbsd GOARCH=amd64 go build -ldflags="-s -w" -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH pbf.go; -# assert $?; -# chmod +x pbf; -# mv pbf build/pbf.openbsd-x64; -# $SKIP_COMPRESSION || upx --brute build/pbf.openbsd-x64; - -checkFiles +check 'build/pbf.win32-x64' 'PE32 executable'; +compress 'build/pbf.win32-x64';