github.com/netdata/go.d.plugin@v0.58.1/hack/go-build.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # SPDX-License-Identifier: GPL-3.0-or-later
     4  
     5  set -e
     6  
     7  PLATFORMS=(
     8    darwin/amd64
     9    darwin/arm64
    10    freebsd/386
    11    freebsd/amd64
    12    freebsd/arm
    13    freebsd/arm64
    14    linux/386
    15    linux/amd64
    16    linux/arm
    17    linux/arm64
    18    linux/ppc64
    19    linux/ppc64le
    20    linux/mips
    21    linux/mipsle
    22    linux/mips64
    23    linux/mips64le
    24  )
    25  
    26  getos() {
    27    local IFS=/ && read -ra array <<<"$1" && echo "${array[0]}"
    28  }
    29  
    30  getarch() {
    31    local IFS=/ && read -ra array <<<"$1" && echo "${array[1]}"
    32  }
    33  
    34  WHICH="$1"
    35  
    36  VERSION="${TRAVIS_TAG:-$(git describe --tags --always --dirty)}"
    37  
    38  GOLDFLAGS=${GLDFLAGS:-}
    39  GOLDFLAGS="$GOLDFLAGS -w -s -X main.version=$VERSION"
    40  
    41  build() {
    42    echo "Building ${GOOS}/${GOARCH}"
    43    CGO_ENABLED=0 GOOS="$1" GOARCH="$2" go build -ldflags "${GOLDFLAGS}" -o "$3" "github.com/netdata/go.d.plugin/cmd/godplugin"
    44  }
    45  
    46  create_config_archives() {
    47    mkdir -p bin
    48    tar -zcvf "bin/config.tar.gz" -C config .
    49    tar -zcvf "bin/go.d.plugin-config-${VERSION}.tar.gz" -C config .
    50  }
    51  
    52  create_vendor_archives() {
    53    mkdir -p bin
    54    go mod vendor
    55    tar -zc --transform "s:^:go.d.plugin-${VERSION#v}/:" -f "bin/vendor.tar.gz" vendor
    56    tar -zc --transform "s:^:go.d.plugin-${VERSION#v}/:" -f "bin/go.d.plugin-vendor-${VERSION}.tar.gz" vendor
    57  }
    58  
    59  build_all_platforms() {
    60    for PLATFORM in "${PLATFORMS[@]}"; do
    61      GOOS=$(getos "$PLATFORM")
    62      GOARCH=$(getarch "$PLATFORM")
    63      FILE="bin/go.d.plugin-${VERSION}.${GOOS}-${GOARCH}"
    64  
    65      build "$GOOS" "$GOARCH" "$FILE"
    66  
    67      ARCHIVE="${FILE}.tar.gz"
    68      tar -C bin -cvzf "${ARCHIVE}" "${FILE/bin\//}"
    69      rm "${FILE}"
    70    done
    71  }
    72  
    73  build_specific_platform() {
    74    GOOS=$(getos "$1")
    75    GOARCH=$(getarch "$1")
    76    : "${GOARCH:=amd64}"
    77  
    78    build "$GOOS" "$GOARCH" bin/godplugin
    79  }
    80  
    81  build_current_platform() {
    82    eval "$(go env | grep -e "GOHOSTOS" -e "GOHOSTARCH")"
    83    GOOS=${GOOS:-$GOHOSTOS}
    84    GOARCH=${GOARCH:-$GOHOSTARCH}
    85  
    86    build "$GOOS" "$GOARCH" bin/godplugin
    87  }
    88  
    89  if [[ "$WHICH" == "configs" ]]; then
    90    echo "Creating config archives for version: $VERSION"
    91    create_config_archives
    92    exit 0
    93  fi
    94  
    95  if [[ "$WHICH" == "vendor" ]]; then
    96    echo "Creating vendor archives for version: $VERSION"
    97    create_vendor_archives
    98    exit 0
    99  fi
   100  
   101  echo "Building binaries for version: $VERSION"
   102  
   103  if [[ "$WHICH" == "all" ]]; then
   104    build_all_platforms
   105  elif [[ -n "$WHICH" ]]; then
   106    build_specific_platform "$WHICH"
   107  else
   108    build_current_platform
   109  fi