github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/scripts/build/.variables (about) 1 #!/usr/bin/env sh 2 set -eu 3 4 : "${CGO_ENABLED=}" 5 : "${GO_LINKMODE=static}" 6 : "${GO_BUILDMODE=}" 7 : "${GO_BUILDTAGS=}" 8 : "${GO_STRIP=}" 9 10 TARGET=${TARGET:-"build"} 11 12 PLATFORM=${PLATFORM:-} 13 VERSION=${VERSION:-$(git describe --match 'v[0-9]*' --dirty='.m' --always --tags | sed 's/^v//' 2>/dev/null || echo "unknown-version" )} 14 GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)} 15 16 if [ "$(uname)" = "Darwin" ]; then 17 # Using BSD date (macOS), which doesn't suppoort the --date option 18 # date -jf "<input format>" "<input value>" +"<output format>" (https://unix.stackexchange.com/a/86510) 19 BUILDTIME=${BUILDTIME:-$(TZ=UTC date -jf "%s" "${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")} 20 else 21 # Using GNU date (Linux) 22 BUILDTIME=${BUILDTIME:-$(TZ=UTC date -u --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +"%Y-%m-%dT%H:%M:%SZ")} 23 fi 24 25 case "$VERSION" in 26 refs/tags/v*) VERSION=${VERSION#refs/tags/v} ;; 27 refs/tags/*) VERSION=${VERSION#refs/tags/} ;; 28 refs/heads/*) VERSION=$(echo "${VERSION#refs/heads/}" | sed -r 's#/+#-#g') ;; 29 refs/pull/*) VERSION=pr-$(echo "$VERSION" | grep -o '[0-9]\+') ;; 30 esac 31 32 GOOS="$(go env GOOS)" 33 GOARCH="$(go env GOARCH)" 34 if [ "${GOARCH}" = "arm" ]; then 35 GOARM="$(go env GOARM)" 36 fi 37 38 TARGET="$TARGET/docker-${GOOS}-${GOARCH}" 39 if [ "${GOARCH}" = "arm" ] && [ -n "${GOARM}" ]; then 40 TARGET="${TARGET}-v${GOARM}" 41 fi 42 if [ "${GOOS}" = "windows" ]; then 43 TARGET="${TARGET}.exe" 44 fi 45 export TARGET 46 47 if [ -z "$CGO_ENABLED" ]; then 48 case "$(go env GOOS)" in 49 linux) 50 case "$(go env GOARCH)" in 51 amd64|arm64|arm|s390x|riscv64) 52 CGO_ENABLED=1 53 ;; 54 *) 55 CGO_ENABLED=0 56 ;; 57 esac 58 ;; 59 darwin|windows) 60 CGO_ENABLED=1 61 ;; 62 *) 63 CGO_ENABLED=0 64 ;; 65 esac 66 fi 67 export CGO_ENABLED 68 69 if [ "$CGO_ENABLED" = "1" ] && [ "$(go env GOOS)" != "windows" ]; then 70 case "$(go env GOARCH)" in 71 mips*|ppc64) 72 # pie build mode is not supported on mips architectures 73 ;; 74 *) 75 GO_BUILDMODE="-buildmode=pie" 76 ;; 77 esac 78 fi 79 export GO_BUILDMODE 80 81 GO_LDFLAGS="${GO_LDFLAGS:-}" 82 GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/khulnasoft/cli/cli/version.GitCommit=${GITCOMMIT}\"" 83 GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/khulnasoft/cli/cli/version.BuildTime=${BUILDTIME}\"" 84 GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/khulnasoft/cli/cli/version.Version=${VERSION}\"" 85 if test -n "${PLATFORM}"; then 86 GO_LDFLAGS="$GO_LDFLAGS -X \"github.com/khulnasoft/cli/cli/version.PlatformName=${PLATFORM}\"" 87 fi 88 if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ] && [ "$(go env GOOS)" = "linux" ]; then 89 GO_LDFLAGS="$GO_LDFLAGS -extldflags -static" 90 fi 91 if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ]; then 92 # compiling statically with CGO enabled requires osusergo to be set. 93 GO_BUILDTAGS="$GO_BUILDTAGS osusergo" 94 fi 95 if [ -n "$GO_STRIP" ]; then 96 # if stripping enabled and building with llvm < 12 against darwin/amd64 97 # platform, it will fail with: 98 # # github.com/khulnasoft/cli/cmd/khulnasoft 99 # /usr/local/go/pkg/tool/linux_amd64/link: /usr/local/go/pkg/tool/linux_amd64/link: running strip failed: exit status 1 100 # llvm-strip: error: unsupported load command (cmd=0x5) 101 # more info: https://github.com/khulnasoft/cli/pull/3717 102 GO_LDFLAGS="$GO_LDFLAGS -s -w" 103 fi 104 export GO_LDFLAGS="$GO_LDFLAGS" # https://github.com/koalaman/shellcheck/issues/2064 105 106 export SOURCE="github.com/khulnasoft/cli/cmd/khulnasoft"