Skip to content

Instantly share code, notes, and snippets.

@mrvcoder
Forked from makew0rld/go-build-all.sh
Created April 26, 2024 09:51
Show Gist options
  • Save mrvcoder/16cdac143ce01ad14733ca3fcbee8d0d to your computer and use it in GitHub Desktop.
Save mrvcoder/16cdac143ce01ad14733ca3fcbee8d0d to your computer and use it in GitHub Desktop.

Revisions

  1. @makew0rld makew0rld revised this gist Jul 29, 2020. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions go-build-all.sh
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,9 @@ CURRENT_DIRECTORY="${PWD##*/}"
    OUTPUT=${SOURCE_FILE:-$CURRENT_DIRECTORY} # if no src file given, use current dir name
    FAILURES=""

    # You can set your own flags on the command line
    FLAGS=${FLAGS:-"-ldflags=\"-s -w\""}

    # A list of OSes to not build for, space-separated
    # It can be set from the command line when the script is called.
    NOT_ALLOWED_OS=${NOT_ALLOWED_OS:-"js android ios solaris illumos aix"}
    @@ -49,14 +52,14 @@ while IFS= read -r target; do
    for GOARM in $arms; do
    BIN_FILENAME="${OUTPUT}-${GOOS}-${GOARCH}${GOARM}"
    if [[ "${GOOS}" == "windows" ]]; then BIN_FILENAME="${BIN_FILENAME}.exe"; fi
    CMD="GOARM=${GOARM} GOOS=${GOOS} GOARCH=${GOARCH} go build -o ${BIN_FILENAME} $@"
    CMD="GOARM=${GOARM} GOOS=${GOOS} GOARCH=${GOARCH} go build $FLAGS -o ${BIN_FILENAME} $@"
    echo "${CMD}"
    eval "${CMD}" || FAILURES="${FAILURES} ${GOOS}/${GOARCH}${GOARM}"
    done
    else
    # Build non-arm here
    if [[ "${GOOS}" == "windows" ]]; then BIN_FILENAME="${BIN_FILENAME}.exe"; fi
    CMD="GOOS=${GOOS} GOARCH=${GOARCH} go build -o ${BIN_FILENAME} $@"
    CMD="GOOS=${GOOS} GOARCH=${GOARCH} go build $FLAGS -o ${BIN_FILENAME} $@"
    echo "${CMD}"
    eval "${CMD}" || FAILURES="${FAILURES} ${GOOS}/${GOARCH}"
    fi
  2. @makew0rld makew0rld revised this gist Jun 22, 2020. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions go-build-all.sh
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,9 @@ CURRENT_DIRECTORY="${PWD##*/}"
    OUTPUT=${SOURCE_FILE:-$CURRENT_DIRECTORY} # if no src file given, use current dir name
    FAILURES=""

    # CHANGE THIS! It's a list of OSes to not build for, space-separated
    NOT_ALLOWED_OS="windows js android ios"
    # A list of OSes to not build for, space-separated
    # It can be set from the command line when the script is called.
    NOT_ALLOWED_OS=${NOT_ALLOWED_OS:-"js android ios solaris illumos aix"}

    # Get all targets
    while IFS= read -r target; do
  3. @makew0rld makew0rld revised this gist May 25, 2020. 1 changed file with 10 additions and 3 deletions.
    13 changes: 10 additions & 3 deletions go-build-all.sh
    Original file line number Diff line number Diff line change
    @@ -5,19 +5,26 @@

    type setopt >/dev/null 2>&1

    contains() {
    # Source: https://stackoverflow.com/a/8063398/7361270
    [[ $1 =~ (^|[[:space:]])$2($|[[:space:]]) ]]
    }

    SOURCE_FILE=$(echo "$@" | sed 's/\.go//')
    CURRENT_DIRECTORY="${PWD##*/}"
    OUTPUT=${SOURCE_FILE:-$CURRENT_DIRECTORY} # if no src file given, use current dir name
    FAILURES=""

    # CHANGE THIS! It's a list of OSes to not build for, space-separated
    NOT_ALLOWED_OS="windows js android ios"

    # Get all targets
    while IFS= read -r target; do
    GOOS=${target%/*}
    GOARCH=${target#*/}
    BIN_FILENAME="${OUTPUT}-${GOOS}-${GOARCH}"

    # Not allowed OSes, feel free to remove the ones that you want
    if [[ $GOOS == "windows" ]] || [[ $GOOS == "js" ]] || [[ $GOOS == "android" ]] || [[ $GOOS == "ios" ]]; then

    if contains "$NOT_ALLOWED_OS" "$GOOS" ; then
    continue
    fi

  4. @makew0rld makew0rld created this gist May 20, 2020.
    61 changes: 61 additions & 0 deletions go-build-all.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    #!/usr/bin/env bash

    # Based on https://gist.github.com/eduncan911/68775dba9d3c028181e4
    # but improved to use the `go` command so it never goes out of date.

    type setopt >/dev/null 2>&1

    SOURCE_FILE=$(echo "$@" | sed 's/\.go//')
    CURRENT_DIRECTORY="${PWD##*/}"
    OUTPUT=${SOURCE_FILE:-$CURRENT_DIRECTORY} # if no src file given, use current dir name
    FAILURES=""

    # Get all targets
    while IFS= read -r target; do
    GOOS=${target%/*}
    GOARCH=${target#*/}
    BIN_FILENAME="${OUTPUT}-${GOOS}-${GOARCH}"

    # Not allowed OSes, feel free to remove the ones that you want
    if [[ $GOOS == "windows" ]] || [[ $GOOS == "js" ]] || [[ $GOOS == "android" ]] || [[ $GOOS == "ios" ]]; then
    continue
    fi

    # Check for arm and set arm version
    if [[ $GOARCH == "arm" ]]; then
    # Set what arm versions each platform supports
    if [[ $GOOS == "darwin" ]]; then
    arms="7"
    elif [[ $GOOS == "windows" ]]; then
    # This is a guess, it's not clear what Windows supports from the docs
    # But I was able to build all these on my machine
    arms="5 6 7"
    elif [[ $GOOS == *"bsd" ]]; then
    arms="6 7"
    else
    # Linux goes here
    arms="5 6 7"
    fi

    # Now do the arm build
    for GOARM in $arms; do
    BIN_FILENAME="${OUTPUT}-${GOOS}-${GOARCH}${GOARM}"
    if [[ "${GOOS}" == "windows" ]]; then BIN_FILENAME="${BIN_FILENAME}.exe"; fi
    CMD="GOARM=${GOARM} GOOS=${GOOS} GOARCH=${GOARCH} go build -o ${BIN_FILENAME} $@"
    echo "${CMD}"
    eval "${CMD}" || FAILURES="${FAILURES} ${GOOS}/${GOARCH}${GOARM}"
    done
    else
    # Build non-arm here
    if [[ "${GOOS}" == "windows" ]]; then BIN_FILENAME="${BIN_FILENAME}.exe"; fi
    CMD="GOOS=${GOOS} GOARCH=${GOARCH} go build -o ${BIN_FILENAME} $@"
    echo "${CMD}"
    eval "${CMD}" || FAILURES="${FAILURES} ${GOOS}/${GOARCH}"
    fi
    done <<< "$(go tool dist list)"

    if [[ "${FAILURES}" != "" ]]; then
    echo ""
    echo "${SCRIPT_NAME} failed on: ${FAILURES}"
    exit 1
    fi