github.com/jfrazelle/docker@v1.1.2-0.20210712172922-bf78e25fe508/hack/make/.binary (about)

     1  #!/usr/bin/env bash
     2  set -e
     3  
     4  # a helper to provide ".exe" when it's appropriate
     5  binary_extension() {
     6  	if [ "$(go env GOOS)" = 'windows' ]; then
     7  		echo -n '.exe'
     8  	fi
     9  }
    10  
    11  BINARY_NAME="$BINARY_SHORT_NAME-$VERSION"
    12  BINARY_EXTENSION="$(binary_extension)"
    13  BINARY_FULLNAME="$BINARY_NAME$BINARY_EXTENSION"
    14  
    15  source "${MAKEDIR}/.go-autogen"
    16  
    17  hash_files() {
    18  	while [ $# -gt 0 ]; do
    19  		f="$1"
    20  		shift
    21  		dir="$(dirname "$f")"
    22  		base="$(basename "$f")"
    23  		for hashAlgo in md5 sha256; do
    24  			if command -v "${hashAlgo}sum" &> /dev/null; then
    25  				(
    26  					# subshell and cd so that we get output files like:
    27  					#   $HASH docker-$VERSION
    28  					# instead of:
    29  					#   $HASH /go/src/github.com/.../$VERSION/binary/docker-$VERSION
    30  					cd "$dir"
    31  					"${hashAlgo}sum" "$base" > "$base.$hashAlgo"
    32  				)
    33  			fi
    34  		done
    35  	done
    36  }
    37  
    38  (
    39  	export GOGC=${DOCKER_BUILD_GOGC:-1000}
    40  
    41  	if [ "$(go env GOOS)/$(go env GOARCH)" != "$(go env GOHOSTOS)/$(go env GOHOSTARCH)" ]; then
    42  		# must be cross-compiling!
    43  		case "$(go env GOOS)/$(go env GOARCH)" in
    44  			windows/amd64)
    45  				export CC="${CC:-x86_64-w64-mingw32-gcc}"
    46  				export CGO_ENABLED=1
    47  				;;
    48  			linux/arm)
    49  				case "${GOARM}" in
    50  					5 | "")
    51  						export CC="${CC:-arm-linux-gnueabi-gcc}"
    52  						export CGO_ENABLED=1
    53  						;;
    54  					7)
    55  						export CC="${CC:-arm-linux-gnueabihf-gcc}"
    56  						export CGO_ENABLED=1
    57  						;;
    58  				esac
    59  				;;
    60  			linux/arm64)
    61  				export CC="${CC:-aarch64-linux-gnu-gcc}"
    62  				export CGO_ENABLED=1
    63  				;;
    64  			linux/amd64)
    65  				export CC="${CC:-x86_64-linux-gnu-gcc}"
    66  				export CGO_ENABLED=1
    67  				;;
    68  			linux/ppc64le)
    69  				export CC="${CC:-powerpc64le-linux-gnu-gcc}"
    70  				export CGO_ENABLED=1
    71  				;;
    72  			linux/s390x)
    73  				export CC="${CC:-s390x-linux-gnu-gcc}"
    74  				export CGO_ENABLED=1
    75  				;;
    76  		esac
    77  	fi
    78  
    79  	# -buildmode=pie is not supported on Windows and Linux on mips, riscv64 and ppc64be.
    80  	# https://github.com/golang/go/blob/77aa209b386a184e7f4b44938f2a05a1b5c5a3cf/src/cmd/internal/sys/supported.go#L89-L99
    81  	case "$(go env GOOS)/$(go env GOARCH)" in
    82  		windows/* | linux/mips* | linux/riscv* | linux/ppc64) ;;
    83  		# TODO remove windows in Go 1.15+: https://github.com/golang/go/commit/95f382139043059a2a0780ba577b53893408f7e4
    84  		# TODO remove riscv64 in Go 1.16+: https://github.com/golang/go/commit/8eb846fd37eb7bded8a1cf6932be2c59069863e5
    85  
    86  		*)
    87  			BUILDFLAGS+=("-buildmode=pie")
    88  			;;
    89  	esac
    90  
    91  	echo "Building: $DEST/$BINARY_FULLNAME"
    92  	echo "GOOS=\"${GOOS}\" GOARCH=\"${GOARCH}\" GOARM=\"${GOARM}\""
    93  	go build \
    94  		-o "$DEST/$BINARY_FULLNAME" \
    95  		"${BUILDFLAGS[@]}" \
    96  		-ldflags "
    97  		$LDFLAGS
    98  		$LDFLAGS_STATIC_DOCKER
    99  		$DOCKER_LDFLAGS
   100  	" \
   101  		${GO_PACKAGE}
   102  )
   103  
   104  echo "Created binary: $DEST/$BINARY_FULLNAME"
   105  ln -sf "$BINARY_FULLNAME" "$DEST/$BINARY_SHORT_NAME$BINARY_EXTENSION"
   106  
   107  hash_files "$DEST/$BINARY_FULLNAME"