Skip to content

Commit

Permalink
Merge pull request #2540 from livepeer/eli/windows-cross-gcc
Browse files Browse the repository at this point in the history
windows: cross-compile on linux
  • Loading branch information
iameli authored Jul 27, 2022
2 parents 32cadcc + df13931 commit 6a212e7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 73 deletions.
81 changes: 17 additions & 64 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ on:

jobs:
linux:
name: Build linux binaries
name: Build binaries
runs-on: ubuntu-18.04
strategy:
matrix:
arch:
- amd64
- arm64
include:
- arch: amd64
os: linux
- arch: arm64
os: linux
- arch: amd64
os: windows

steps:
- name: Check out code
uses: actions/checkout@v3
Expand All @@ -29,6 +34,7 @@ jobs:
- name: Set build environment
run: |
echo "GOARCH=${{ matrix.arch }}" >> $GITHUB_ENV
echo "GOOS=${{ matrix.os }}" >> $GITHUB_ENV
echo "GO_BUILD_DIR=lp-builds/" >> $GITHUB_ENV
- name: Set up go
Expand All @@ -42,24 +48,24 @@ jobs:
uses: actions/[email protected]
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-${{ matrix.arch }}-go-${{ hashFiles('**/go.sum') }}
key: ${{ runner.os }}-${{ matrix.os }}-${{ matrix.arch }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-${{ matrix.arch }}-go-
${{ runner.os }}-${{ matrix.os }}-${{ matrix.arch }}-go-
- name: Cache ffmpeg
id: cache-ffmpeg
uses: actions/[email protected]
with:
path: ~/compiled
key: ${{ runner.os }}-${{ matrix.arch }}-ffmpeg-${{ hashFiles('**/install_ffmpeg.sh') }}
key: ${{ runner.os }}-${{ matrix.os }}-${{ matrix.arch }}-ffmpeg-${{ hashFiles('**/install_ffmpeg.sh') }}
restore-keys: |
${{ runner.os }}-${{ matrix.arch }}-ffmpeg
${{ runner.os }}-${{ matrix.os }}-${{ matrix.arch }}-ffmpeg-
- name: Cache binaries
uses: actions/[email protected]
with:
path: ~/build
key: ${{ runner.os }}-${{ matrix.arch }}-binaries-${{ github.sha }}
key: ${{ runner.os }}-${{ matrix.os }}-${{ matrix.arch }}-binaries-${{ github.sha }}

- name: Install dependencies
run: |
Expand All @@ -68,7 +74,7 @@ jobs:
&& sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 15CF4D18AF4F7421 \
&& sudo add-apt-repository "deb [arch=amd64] http://apt.llvm.org/xenial/ llvm-toolchain-xenial-12 main" \
&& sudo apt-get update \
&& sudo apt-get -y install clang-12 clang-tools-12 lld-12 build-essential pkg-config autoconf git python gcc-multilib libgcc-8-dev-arm64-cross
&& sudo apt-get -y install clang-12 clang-tools-12 lld-12 build-essential pkg-config autoconf git python gcc-multilib libgcc-8-dev-arm64-cross mingw-w64-tools gcc-mingw-w64-x86-64
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-12 30 \
&& sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-12 30 \
Expand Down Expand Up @@ -204,59 +210,6 @@ jobs:
name: release-artifacts
path: releases/

windows:
name: Build windows binaries
runs-on: windows-latest
steps:
- name: configure git line endings
run: git config --global core.autocrlf false

- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 0
# Check https://github.com/livepeer/go-livepeer/pull/1891
# for ref value discussion
ref: ${{ github.event.pull_request.head.sha }}

- name: Setup Msys2 environment
uses: msys2/setup-msys2@v2
with:
msystem: MSYS

- name: Prepare mingw64 environment
shell: msys2 {0}
run: ./prepare_mingw64.sh

- name: Build ffmpeg
shell: msys2 {0}
run: ./install_ffmpeg.sh
# For some reason the next step sometimes cannot find protoc
# - name: Install protoc
# uses: arduino/setup-protoc@v1
# We do not just run `make` because it would also require protoc
# Due to the issue described above with finding protoc, for now we just specify the individual binaries

- name: Build binaries
shell: msys2 {0}
run: ./ci_env.sh make livepeer livepeer_cli livepeer_bench livepeer_router

- name: Upload build
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name == github.repository
shell: msys2 {0}
env:
GHA_REF: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref }}
GCLOUD_KEY: ${{ secrets.GCLOUD_KEY }}
GCLOUD_SECRET: ${{ secrets.GCLOUD_SECRET }}
DISCORD_URL: ${{ secrets.DISCORD_URL }}
run: ./upload_build.sh

- name: Upload artifacts for cutting release
uses: actions/upload-artifact@master
with:
name: release-artifacts
path: releases/

linux-tensorflow:
name: Build binaries for linux using tensorflow
runs-on: ubuntu-18.04
Expand Down Expand Up @@ -346,7 +299,7 @@ jobs:
GCLOUD_KEY: ${{ secrets.GCLOUD_KEY }}
GCLOUD_SECRET: ${{ secrets.GCLOUD_SECRET }}
DISCORD_URL: ${{ secrets.DISCORD_URL }}
RELEASE_TAG: 'tensorflow'
RELEASE_TAG: "tensorflow"

- name: Upload artifacts for cutting release
uses: actions/upload-artifact@master
Expand Down
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ ifeq ($(uname_s),Linux)
endif
endif

pkg_config_libdir :=
ifeq ($(uname_s),Linux)
ifeq ($(GOOS),windows)
cc = x86_64-w64-mingw32-gcc
endif
endif

.PHONY: livepeer
livepeer:
GO111MODULE=on CGO_ENABLED=1 CC="$(cc)" CGO_CFLAGS="$(cgo_cflags)" CGO_LDFLAGS="$(cgo_ldflags)" go build -o $(GO_BUILD_DIR) -tags "$(BUILD_TAGS)" -ldflags="$(ldflags)" cmd/livepeer/*.go
Expand Down
4 changes: 3 additions & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
FROM --platform=$BUILDPLATFORM ubuntu:18.04 as build

ARG TARGETARCH
ARG GOOS

ENV GOARCH="$TARGETARCH" \
GOOS="$GOOS" \
PATH="/usr/local/go/bin:/go/bin:${PATH}" \
PKG_CONFIG_PATH="/root/compiled/lib/pkgconfig" \
CPATH="/usr/local/cuda/include" \
Expand All @@ -16,7 +18,7 @@ RUN apt update \
&& curl -fsSl https://apt.llvm.org/llvm-snapshot.gpg.key | apt-key add - \
&& add-apt-repository "deb [arch=amd64] https://apt.llvm.org/xenial/ llvm-toolchain-xenial-12 main" \
&& apt update \
&& apt -y install clang-12 clang-tools-12 lld-12 build-essential pkg-config autoconf git python docker-ce-cli pciutils gcc-multilib libgcc-8-dev-arm64-cross
&& apt -y install clang-12 clang-tools-12 lld-12 build-essential pkg-config autoconf git python docker-ce-cli pciutils gcc-multilib libgcc-8-dev-arm64-cross gcc-mingw-w64-x86-64

RUN update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-12 30 \
&& update-alternatives --install /usr/bin/clang clang /usr/bin/clang-12 30 \
Expand Down
17 changes: 15 additions & 2 deletions install_ffmpeg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ROOT="${1:-$HOME}"
NPROC=${NPROC:-$(nproc)}
EXTRA_CFLAGS=""
EXTRA_LDFLAGS=""
EXTRA_X264_FLAGS=""
EXTRA_FFMPEG_FLAGS=""

if [[ "$ARCH" == "arm64" && "$UNAME" == "Darwin" ]]; then
Expand All @@ -25,6 +26,18 @@ if [[ "$ARCH" == "x86_64" && "$UNAME" == "Linux" && "${GOARCH:-}" == "arm64" ]];
IS_ARM64=1
fi

if [[ "$ARCH" == "x86_64" && "$UNAME" == "Linux" && "${GOOS:-}" == "windows" ]]; then
echo "cross-compiling windows-amd64"
EXTRA_CFLAGS="-L/usr/x86_64-w64-mingw32/lib -I/usr/x86_64-w64-mingw32/include $EXTRA_CFLAGS"
EXTRA_LDFLAGS="-L/usr/x86_64-w64-mingw32/lib $EXTRA_LDFLAGS"
EXTRA_FFMPEG_FLAGS="$EXTRA_FFMPEG_FLAGS --arch=x86_64 --enable-cross-compile --cross-prefix=x86_64-w64-mingw32- --target-os=mingw64 --sysroot=/usr/x86_64-w64-mingw32"
EXTRA_X264_FLAGS="$EXTRA_X264_FLAGS --cross-prefix=x86_64-w64-mingw32- --sysroot=/usr/x86_64-w64-mingw32"
HOST_OS="--host=mingw64"
# Workaround for https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=967969
export PKG_CONFIG_LIBDIR="/usr/local/x86_64-w64-mingw32/lib/pkgconfig"
EXTRA_FFMPEG_FLAGS="$EXTRA_FFMPEG_FLAGS --pkg-config=$(which pkg-config)"
fi

if [[ "$ARCH" == "x86_64" && "$UNAME" == "Darwin" && "${GOARCH:-}" == "arm64" ]]; then
echo "cross-compiling darwin-arm64"
EXTRA_CFLAGS="$EXTRA_CFLAGS --target=arm64-apple-macos11"
Expand Down Expand Up @@ -62,7 +75,7 @@ if [[ "$UNAME" != "Darwin" ]]; then
if [[ ! -e "$ROOT/nv-codec-headers" ]]; then
git clone https://git.videolan.org/git/ffmpeg/nv-codec-headers.git "$ROOT/nv-codec-headers"
cd $ROOT/nv-codec-headers
git checkout 250292dd20af60edc6e0d07f1d6e489a2f8e1c44
git checkout n9.1.23.1
make -e PREFIX="$ROOT/compiled"
make install -e PREFIX="$ROOT/compiled"
fi
Expand Down Expand Up @@ -94,7 +107,7 @@ if [[ ! -e "$ROOT/x264" ]]; then
# older git master, does not compile on Apple Silicon
git checkout 545de2ffec6ae9a80738de1b2c8cf820249a2530
fi
./configure --prefix="$ROOT/compiled" --enable-pic --enable-static ${HOST_OS:-} --disable-cli --extra-cflags="$EXTRA_CFLAGS" --extra-asflags="$EXTRA_CFLAGS" || cat "$ROOT/x264/config.log"
./configure --prefix="$ROOT/compiled" --enable-pic --enable-static ${HOST_OS:-} --disable-cli --extra-cflags="$EXTRA_CFLAGS" --extra-asflags="$EXTRA_CFLAGS" --extra-ldflags="$EXTRA_LDFLAGS" --disable-asm $EXTRA_X264_FLAGS || (cat $ROOT/x264/config.log && exit 1)
make -j$NPROC
make -j$NPROC install-lib-static
fi
Expand Down
22 changes: 16 additions & 6 deletions upload_build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ RELEASES_DIR="${BASE_DIR}/${RELEASES_DIR:-releases}/"

mkdir -p "$RELEASES_DIR"

if [[ $(uname) == *"MSYS"* ]]; then
if [[ "${GOOS:-}" != "" ]]; then
PLATFORM="$GOOS"
elif [[ $(uname) == *"MSYS"* ]]; then
PLATFORM="windows"
EXT=".exe"
else
PLATFORM=$(uname | tr '[:upper:]' '[:lower:]')
EXT=""
if [[ -n "${RELEASE_TAG:-}" ]]; then
PLATFORM="$PLATFORM-$RELEASE_TAG"
fi
fi

EXT=""
if [[ "$PLATFORM" == "windows" ]]; then
EXT=".exe"
fi
if [[ "$PLATFORM" != "linux" ]] && [[ "$PLATFORM" != "darwin" ]] && [[ "$PLATFORM" != "windows" ]]; then
echo "Unknown/unsupported platform: $PLATFORM"
exit 1
fi

if [[ -n "${RELEASE_TAG:-}" ]]; then
PLATFORM="$PLATFORM-$RELEASE_TAG"
fi

ARCH="$(uname -m)"
Expand Down

0 comments on commit 6a212e7

Please sign in to comment.