#!/usr/bin/env bash # # Cross-compile the sshtools binaries for the three desktop platforms. # # ./build.sh cross-compile every program and target # ./build.sh local only the host binaries, at dist/golink and dist/goscp # # Everything is pure Go (CGO disabled), so no cross toolchain is needed and the # build works offline: the only dependency, golang.org/x/crypto, is already in # the local module cache. set -euo pipefail cd "$(dirname "${BASH_SOURCE[0]}")" version="${VERSION:-1.0.0}" export CGO_ENABLED=0 export GOCACHE="${GOCACHE:-$PWD/.gocache}" export GOFLAGS="${GOFLAGS:--mod=mod}" ldflags="-s -w -X main.version=$version" programs="golink goscp" targets="linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 windows/amd64 windows/arm64" mkdir -p dist build() { # GOOS="$2" GOARCH="$3" go build -trimpath -ldflags "$ldflags" -o "$4" "./cmd/$1" printf ' %-32s %7s %s\n' "$4" "$(du -h "$4" | cut -f1)" "$(file -b "$4" 2>/dev/null | cut -c1-58)" } hostos="$(go env GOOS)" hostarch="$(go env GOARCH)" if [ "${1:-all}" = local ]; then for program in $programs; do ext="" [ "$hostos" = windows ] && ext=".exe" build "$program" "$hostos" "$hostarch" "dist/${program}${ext}" done exit 0 fi echo "sshtools $version" for program in $programs; do for target in $targets; do os="${target%%/*}" arch="${target##*/}" ext="" [ "$os" = windows ] && ext=".exe" build "$program" "$os" "$arch" "dist/${program}_${os}_${arch}${ext}" done done # convenience copies for the machine running this script ext="" [ "$hostos" = windows ] && ext=".exe" for program in $programs; do native="dist/${program}_${hostos}_${hostarch}${ext}" if [ -f "$native" ]; then cp "$native" "dist/${program}${ext}" fi done echo ( cd dist && sha256sum golink_* goscp_* > SHA256SUMS && cat SHA256SUMS )