diff --git a/Makefile b/Makefile index 3f3189c370..350f131392 100644 --- a/Makefile +++ b/Makefile @@ -22,30 +22,66 @@ cgo_cflags := cgo_ldflags := cc := -uname_s := $(shell uname -s) -ifeq ($(uname_s),Darwin) +# Build platform flags +BUILDOS ?= $(shell uname -s | tr '[:upper:]' '[:lower:]') +BUILDARCH ?= $(shell uname -m | tr '[:upper:]' '[:lower:]') +ifeq ($(BUILDARCH),aarch64) + BUILDARCH=arm64 +endif +ifeq ($(BUILDARCH),x86_64) + BUILDARCH=amd64 +endif + +# Override these (not BUILDOS/BUILDARCH) for cross-compilation +export GOOS ?= $(BUILDOS) +export GOARCH ?= $(BUILDARCH) + +# Cross-compilation section. Currently supported: +# darwin amd64 --> darwin arm64 +# darwin arm64 --> linux arm64 +# linux amd64 --> linux arm64 +# linux amd64 --> windows amd64 + +ifeq ($(BUILDOS),darwin) + ifeq ($(GOOS),darwin) cgo_ldflags += -framework CoreFoundation -framework Security - ifeq ($(GOARCH),arm64) - cgo_cflags += --target=arm64-apple-macos11 - cgo_ldflags += --target=arm64-apple-macos11 + ifeq ($(BUILDARCH),amd64) + ifeq ($(GOARCH),arm64) + cgo_cflags += --target=arm64-apple-macos11 + cgo_ldflags += --target=arm64-apple-macos11 + endif + endif endif -endif -ifeq ($(uname_s),Linux) - ifeq ($(GOARCH),arm64) - cgo_cflags += --target=aarch64-linux-gnu - cgo_ldflags += --target=aarch64-linux-gnu - cc = clang --sysroot=/usr/aarch64-linux-gnu + ifeq ($(GOOS),linux) + ifeq ($(GOARCH),arm64) + LLVM_PATH ?= /opt/homebrew/opt/llvm/bin + SYSROOT ?= /tmp/sysroot-aarch64-linux-gnu + cgo_cflags += --target=aarch64-linux-gnu -Wno-error=unused-command-line-argument + cgo_ldflags += --target=aarch64-linux-gnu + cc = $(LLVM_PATH)/clang -fuse-ld=$(LLVM_PATH)/ld.lld --sysroot=$(SYSROOT) + endif endif endif -pkg_config_libdir := -ifeq ($(uname_s),Linux) +ifeq ($(BUILDOS),linux) + ifeq ($(BUILDARCH),amd64) + ifeq ($(GOARCH),arm64) + ifeq ($(GOOS),linux) + SYSROOT ?= /usr/aarch64-linux-gnu + cgo_cflags += --target=aarch64-linux-gnu + cgo_ldflags += --target=aarch64-linux-gnu + cc = clang --sysroot=$(SYSROOT) + endif + endif + endif + ifeq ($(GOOS),windows) cc = x86_64-w64-mingw32-gcc endif endif + .PHONY: livepeer livepeer_bench livepeer_cli livepeer_router docker livepeer: diff --git a/install_ffmpeg.sh b/install_ffmpeg.sh index 56ef2cf901..daaf37352d 100755 --- a/install_ffmpeg.sh +++ b/install_ffmpeg.sh @@ -1,56 +1,94 @@ #!/usr/bin/env bash -set -ex +set -exuo pipefail ROOT="${1:-$HOME}" -[[ -z "$ARCH" ]] && ARCH="$(uname -m)" -[[ -z "$UNAME" ]] && UNAME="$(uname)" NPROC=${NPROC:-$(nproc)} EXTRA_CFLAGS="" EXTRA_LDFLAGS="" EXTRA_X264_FLAGS="" EXTRA_FFMPEG_FLAGS="" +BUILD_TAGS="${BUILD_TAGS:-}" - -if [[ "$ARCH" == "arm64" && "$UNAME" == "Darwin" ]]; then - # Detect Apple Silicon - IS_ARM64=1 +# Build platform flags +BUILDOS=$(uname -s | tr '[:upper:]' '[:lower:]') +BUILDARCH=$(uname -m | tr '[:upper:]' '[:lower:]') +if [[ $BUILDARCH == "aarch64" ]]; then + BUILDARCH=arm64 +fi +if [[ $BUILDARCH == "x86_64" ]]; then + BUILDARCH=amd64 fi -if [[ "$ARCH" == "x86_64" && "$UNAME" == "Linux" && "${GOARCH:-}" == "arm64" ]]; then - echo "cross-compiling linux-arm64" - export CC="clang --sysroot=/usr/aarch64-linux-gnu" +# Override these for cross-compilation +export GOOS="${GOOS:-$BUILDOS}" +export GOARCH="${GOARCH:-$BUILDARCH}" + +echo "BUILDOS: $BUILDOS" +echo "BUILDARCH: $BUILDARCH" +echo "GOOS: $GOOS" +echo "GOARCH: $GOARCH" + +SYSROOT="${SYSROOT:-}" +function check_sysroot() { + if ! stat $SYSROOT > /dev/null; then + echo "cross-compilation sysroot not found at $SYSROOT, try setting SYSROOT to the correct path" + exit 1 + fi +} + +if [[ "$BUILDARCH" == "amd64" && "$BUILDOS" == "linux" && "$GOARCH" == "arm64" && "$GOOS" == "linux" ]]; then + SYSROOT="${SYSROOT:-/usr/aarch64-linux-gnu}" + check_sysroot + echo "cross-compiling linux-amd64 --> linux-arm64" + export CC="clang --sysroot=$SYSROOT" EXTRA_CFLAGS="--target=aarch64-linux-gnu -I/usr/local/cuda_arm64/include $EXTRA_CFLAGS" EXTRA_LDFLAGS="--target=aarch64-linux-gnu -L/usr/local/cuda_arm64/lib64 $EXTRA_LDFLAGS" - EXTRA_FFMPEG_FLAGS="$EXTRA_FFMPEG_FLAGS --arch=aarch64 --enable-cross-compile --cc=clang --sysroot=/usr/aarch64-linux-gnu" + EXTRA_FFMPEG_FLAGS="$EXTRA_FFMPEG_FLAGS --arch=aarch64 --enable-cross-compile --cc=clang --sysroot=$SYSROOT" + HOST_OS="--host=aarch64-linux-gnu" +fi + +if [[ "$BUILDARCH" == "arm64" && "$BUILDOS" == "darwin" && "$GOARCH" == "arm64" && "$GOOS" == "linux" ]]; then + SYSROOT="${SYSROOT:-/tmp/sysroot-aarch64-linux-gnu}" + check_sysroot + echo "cross-compiling darwin-arm64 --> linux-arm64" + LLVM_PATH="${LLVM_PATH:-/opt/homebrew/opt/llvm/bin}" + if [[ ! -f "$LLVM_PATH/ld.lld" ]]; then + echo "llvm linker not found at '$LLVM_PATH/ld.lld'. try 'brew install llvm' or set LLVM_PATH to your LLVM bin directory" + exit 1 + fi + export CC="$LLVM_PATH/clang --sysroot=$SYSROOT" + EXTRA_CFLAGS="--target=aarch64-linux-gnu $EXTRA_CFLAGS" + EXTRA_LDFLAGS="--target=aarch64-linux-gnu -fuse-ld=$LLVM_PATH/ld.lld $EXTRA_LDFLAGS" + EXTRA_FFMPEG_FLAGS="$EXTRA_FFMPEG_FLAGS --arch=aarch64 --enable-cross-compile --cc=$LLVM_PATH/clang --sysroot=$SYSROOT --target-os=linux" + EXTRA_X264_FLAGS="$EXTRA_X264_FLAGS --sysroot=$SYSROOT" HOST_OS="--host=aarch64-linux-gnu" - 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" +if [[ "$BUILDARCH" == "amd64" && "$BUILDOS" == "linux" && "$GOARCH" == "amd64" && "$GOOS" == "windows" ]]; then + echo "cross-compiling linux-amd64 --> windows-amd64" + SYSROOT="${SYSROOT:-/usr/x86_64-w64-mingw32}" + check_sysroot + EXTRA_CFLAGS="-L$SYSROOT/lib -I$SYSROOT/include $EXTRA_CFLAGS" + EXTRA_LDFLAGS="-L$SYSROOT/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=$SYSROOT" + EXTRA_X264_FLAGS="$EXTRA_X264_FLAGS --cross-prefix=x86_64-w64-mingw32- --sysroot=$SYSROOT" 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" +if [[ "$BUILDARCH" == "amd64" && "$BUILDOS" == "darwin" && "$GOARCH" == "arm64" && "$GOOS" == "darwin" ]]; then + echo "cross-compiling darwin-amd64 --> darwin-arm64" EXTRA_CFLAGS="$EXTRA_CFLAGS --target=arm64-apple-macos11" EXTRA_LDFLAGS="$EXTRA_LDFLAGS --target=arm64-apple-macos11" HOST_OS="--host=aarch64-darwin" EXTRA_FFMPEG_FLAGS="$EXTRA_FFMPEG_FLAGS --arch=aarch64 --enable-cross-compile" - IS_ARM64=1 fi -echo "Arch $ARCH ${IS_ARM64:+(ARM64)}" # Windows (MSYS2) needs a few tweaks -if [[ "$UNAME" == *"MSYS"* ]]; then +if [[ "$BUILDOS" == *"MSYS"* ]]; then ROOT="/build" export PATH="$PATH:/usr/bin:/mingw64/bin" export C_INCLUDE_PATH="${C_INCLUDE_PATH:-}:/mingw64/lib" @@ -72,7 +110,7 @@ export PKG_CONFIG_PATH="${PKG_CONFIG_PATH:-}:$ROOT/compiled/lib/pkgconfig" mkdir -p "$ROOT/" # NVENC only works on Windows/Linux -if [[ "$UNAME" != "Darwin" ]]; then +if [[ "$GOOS" != "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 @@ -82,7 +120,7 @@ if [[ "$UNAME" != "Darwin" ]]; then fi fi -if [[ "$UNAME" != *"MSYS"* && ! $IS_ARM64 ]]; then +if [[ "$GOOS" != "windows" && "$GOARCH" == "amd64" ]]; then if [[ ! -e "$ROOT/nasm-2.14.02" ]]; then # sudo apt-get -y install asciidoc xmlto # this fails :( cd "$ROOT" @@ -101,7 +139,7 @@ fi if [[ ! -e "$ROOT/x264" ]]; then git clone http://git.videolan.org/git/x264.git "$ROOT/x264" cd "$ROOT/x264" - if [[ $IS_ARM64 ]]; then + if [[ $GOARCH == "arm64" ]]; then # newer git master, compiles on Apple Silicon git checkout 66a5bc1bd1563d8227d5d18440b525a09bcf17ca else @@ -113,7 +151,7 @@ if [[ ! -e "$ROOT/x264" ]]; then make -j$NPROC install-lib-static fi -if [[ "$UNAME" == "Linux" && "${BUILD_TAGS}" == *"debug-video"* ]]; then +if [[ "$GOOS" == "linux" && "$BUILD_TAGS" == *"debug-video"* ]]; then sudo apt-get install -y libnuma-dev cmake if [[ ! -e "$ROOT/x265" ]]; then git clone https://bitbucket.org/multicoreware/x265_git.git "$ROOT/x265" @@ -140,9 +178,9 @@ EXTRA_FFMPEG_LDFLAGS="$EXTRA_LDFLAGS" # all flags which should be present for production build, but should be replaced/removed for debug build DEV_FFMPEG_FLAGS="--disable-programs" -if [[ "$UNAME" == "Darwin" ]]; then +if [[ "$BUILDOS" == "darwin" && "$GOOS" == "darwin" ]]; then EXTRA_FFMPEG_LDFLAGS="$EXTRA_FFMPEG_LDFLAGS -framework CoreFoundation -framework Security" -elif [[ "${GOOS:-}" == "windows" ]]; then +elif [[ "$GOOS" == "windows" ]]; then EXTRA_FFMPEG_FLAGS="$EXTRA_FFMPEG_FLAGS --enable-cuda --enable-cuda-llvm --enable-cuvid --enable-nvenc --enable-decoder=h264_cuvid,hevc_cuvid,vp8_cuvid,vp9_cuvid --enable-filter=scale_cuda,signature_cuda,hwupload_cuda --enable-encoder=h264_nvenc,hevc_nvenc" elif [[ -e "/usr/local/cuda/lib64" ]]; then echo "CUDA SDK detected, building with GPU support" @@ -181,7 +219,7 @@ if [[ ! -e "$ROOT/ffmpeg/libavcodec/libavcodec.a" ]]; then --enable-parser=aac,aac_latm,h264,hevc,vp8,vp9 \ --enable-filter=abuffer,buffer,abuffersink,buffersink,afifo,fifo,aformat,format \ --enable-filter=aresample,asetnsamples,fps,scale,hwdownload,select,livepeer_dnn,signature \ - --enable-encoder=aac,opus,libx264 \ + --enable-encoder=aac,opus \ --enable-decoder=aac,opus,h264 \ --extra-cflags="${EXTRA_CFLAGS} -I${ROOT}/compiled/include -I/usr/local/cuda/include" \ --extra-ldflags="${EXTRA_FFMPEG_LDFLAGS} -L${ROOT}/compiled/lib -L/usr/local/cuda/lib64" \