Skip to content

Commit

Permalink
Update example for new issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskorn committed Feb 4, 2022
1 parent 89bf3f4 commit 1439989
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 72 deletions.
44 changes: 6 additions & 38 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,54 +9,22 @@ defaults:

jobs:
build:
name: "Build with Zig ${{ matrix.zig_version }}"
name: "Build with Zig"
runs-on: macos-11
strategy:
fail-fast: false
matrix:
include:
- zig_version: 0.9.0
- zig_version: 0.10.0-dev.183+5c228765f
steps:
- name: Check out code
uses: actions/[email protected]
- name: Set up Zig
uses: goto-bus-stop/[email protected]
with:
version: ${{ matrix.zig_version }}
version: 0.9.9
- name: Set up Golang
uses: actions/[email protected]
with:
go-version: 1.17.5
- name: System information
run: xcrun --show-sdk-path
- name: "Build: shared darwin amd64"
if: ${{ always() }}
run: ./build.sh shared darwin amd64
- name: "Build: archive darwin amd64"
if: ${{ always() }}
run: ./build.sh archive darwin amd64
- name: "Build: shared darwin arm64"
if: ${{ always() }}
run: ./build.sh shared darwin arm64
- name: "Build: archive darwin arm64"
if: ${{ always() }}
run: ./build.sh archive darwin arm64
- name: "Build: shared linux amd64"
if: ${{ always() }}
run: ./build.sh shared linux amd64
- name: "Build: archive linux amd64"
if: ${{ always() }}
run: ./build.sh archive linux amd64
- name: "Build: shared linux arm64"
if: ${{ always() }}
run: ./build.sh shared linux arm64
- name: "Build: archive linux arm64"
if: ${{ always() }}
run: ./build.sh archive linux arm64
- name: "Build: shared windows amd64"
if: ${{ always() }}
run: ./build.sh shared windows amd64
- name: "Build: archive windows amd64"
if: ${{ always() }}
run: ./build.sh archive windows amd64
- name: Run local build
run: ./build-native.sh shared
- name: Run cross-compilation build
run: ./build-cross.sh shared $(uname | tr '[:upper:]' '[:lower:]') $(uname -m)
34 changes: 10 additions & 24 deletions build.sh → build-cross.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,23 @@ set -euo pipefail

PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD_DIR="$PROJECT_ROOT/build"
ANY_TARGETS_FAILED=false

function main() {
if [[ $# -ne 3 ]]; then
echoRed "Must provide exactly three arguments."
exit 1
fi

echoBlue "Preparing..."
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"

if [[ $# -lt 3 ]]; then
build shared darwin amd64
build archive darwin amd64
build shared darwin arm64
build archive darwin arm64
build shared linux amd64
build archive linux amd64
build shared linux arm64
build archive linux arm64
build shared windows amd64
build archive windows amd64
else
build "$1" "$2" "$3"
fi

echo
clearGoBuildCache
build "$1" "$2" "$3"
}

if [[ "$ANY_TARGETS_FAILED" == "true" ]]; then
echoRed "One or more targets failed."
exit 1
else
echoGreen "All targets finished."
fi
function clearGoBuildCache() {
go clean -cache
}

function build() {
Expand Down Expand Up @@ -123,7 +110,6 @@ function build() {
go build -buildmode="c-$binaryType" -o="$outputDir/$outputFile" -ldflags "-s" . 2>&1 &&
echoGreen "Succeeded.";
} || {
ANY_TARGETS_FAILED=true &&
echoRed "Failed!";
}
}
Expand Down
112 changes: 112 additions & 0 deletions build-native.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#! /usr/bin/env bash

set -euo pipefail

PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD_DIR="$PROJECT_ROOT/build"

function main() {
if [[ $# -ne 1 ]]; then
echoRed "Must provide exactly one argument."
exit 1
fi

echoBlue "Preparing..."
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"

clearGoBuildCache
build "$1"
}

function clearGoBuildCache() {
go clean -cache
}

function build() {
local binaryType=$1
local os=$(uname | tr '[:upper:]' '[:lower:]')
local arch=$(uname -m)

echo
echoBlue "Building $binaryType library for local environment ($os $arch)..."

local outputDir="$BUILD_DIR/$os/$arch/$binaryType"
mkdir -p "$outputDir"

local outputFile

case $binaryType in
shared)
case $os in
linux)
outputFile="libmain.so"
;;
darwin)
outputFile="libmain.dylib"
;;
windows)
outputFile="main.dll"
;;
*)
echo "Unknown OS $os" >/dev/stderr
;;
esac
;;
archive)
case $os in
windows)
outputFile="main.lib"
;;
*)
outputFile="libmain.a"
;;
esac
;;
*)
echo "Unknown binary type $binaryType" >/dev/stderr
exit 1
;;
esac

{
cd "$PROJECT_ROOT/src"

# -ldflags "-s" below is inspired by https://github.com/ziglang/zig/issues/9050#issuecomment-859939664
# and fixes errors like the following when building the shared library for darwin/amd64:
# /opt/homebrew/Cellar/go/1.17.5/libexec/pkg/tool/darwin_arm64/link: /opt/homebrew/Cellar/go/1.17.5/libexec/pkg/tool/darwin_arm64/link: running strip failed: exit status 1
# /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/strip: error: bad n_sect for symbol table entry 556 in: /private/var/folders/7t/_rsz39554vvgvq2t6b4ztktc0000gn/T/go-build3526877506/b001/exe/libmain.dylib

{
CGO_ENABLED=1 \
GOOS=$os \
GOARCH=$arch \
CC="$PROJECT_ROOT/helpers/cc.sh" \
CXX="$PROJECT_ROOT/helpers/cxx.sh" \
go build -buildmode="c-$binaryType" -o="$outputDir/$outputFile" -ldflags "-s" . 2>&1 &&
echoGreen "Succeeded.";
} || {
echoRed "Failed!";
}
}
}

function echoRed() {
local text=$1

echo "$(tput setaf 1)$text$(tput sgr0)"
}

function echoBlue() {
local text=$1

echo "$(tput setaf 4)$text$(tput sgr0)"
}

function echoGreen() {
local text=$1

echo "$(tput setaf 2)$text$(tput sgr0)"
}

main "$@"
9 changes: 4 additions & 5 deletions helpers/cc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ export ZIG_LOCAL_CACHE_DIR="$PROJECT_ROOT/.zigcache/"

set -x

if [[ "$ZTARGET" == *-macos-* ]]; then
if [[ -z "${ZTARGET+x}" ]]; then
zig cc "$@"
else
SYSROOT="$(xcrun --show-sdk-path)"
# The -I, -F and -L flags are required to configure the sysroot correctly - see https://github.com/ziglang/zig/issues/10513#issuecomment-1005652047.
# These -Wno-... flags are based on the suggestions in https://github.com/golang/go/issues/38876#issuecomment-669338355. They could also be set with the CGO_CPPFLAGS environment variable.
zig cc -target "$ZTARGET" --sysroot "$SYSROOT" "-I$SYSROOT/usr/include" "-F$SYSROOT/System/Library/Frameworks" "-L$SYSROOT/usr/lib" -Wno-expansion-to-defined -Wno-availability -Wno-nullability-completeness "$@"
else
zig cc -target "$ZTARGET" "$@"
zig cc -target "$ZTARGET" --sysroot "$SYSROOT" "-I$SYSROOT/usr/include" "-F$SYSROOT/System/Library/Frameworks" "-L$SYSROOT/usr/lib" "$@"
fi

set +x
10 changes: 5 additions & 5 deletions helpers/cxx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export ZIG_LOCAL_CACHE_DIR="$PROJECT_ROOT/.zigcache/"

set -x

if [[ "$ZTARGET" == *-macos-* ]]; then
SYSROOT="$(xcrun --show-sdk-path)"
# These -Wno-... flags are based on the suggestions in https://github.com/golang/go/issues/38876#issuecomment-669338355. They could also be set with the CGO_CPPFLAGS environment variable.
zig c++ -target "$ZTARGET" --sysroot "$SYSROOT" "-I$SYSROOT/usr/include" "-F$SYSROOT/System/Library/Frameworks" "-L$SYSROOT/usr/lib" -Wno-expansion-to-defined -Wno-availability -Wno-nullability-completeness "$@"
if [[ -z "${ZTARGET+x}" ]]; then
zig c++ "$@"
else
zig c++ -target "$ZTARGET" "$@"
SYSROOT="$(xcrun --show-sdk-path)"
# The -I, -F and -L flags are required to configure the sysroot correctly - see https://github.com/ziglang/zig/issues/10513#issuecomment-1005652047.
zig c++ -target "$ZTARGET" --sysroot "$SYSROOT" "-I$SYSROOT/usr/include" "-F$SYSROOT/System/Library/Frameworks" "-L$SYSROOT/usr/lib" "$@"
fi

set +x

0 comments on commit 1439989

Please sign in to comment.