Skip to content

Commit

Permalink
build: refactor install_ffmpeg.sh, add macos --> linux support
Browse files Browse the repository at this point in the history
  • Loading branch information
iameli committed Oct 9, 2023
1 parent 45a03e7 commit a462ce8
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 40 deletions.
62 changes: 49 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
97 changes: 70 additions & 27 deletions install_ffmpeg.sh
Original file line number Diff line number Diff line change
@@ -1,56 +1,99 @@
#!/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:-}"

# 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

# Override these for cross-compilation
export GOOS="${GOOS:-$BUILDOS}"
export GOARCH="${GOARCH:-$BUILDARCH}"

if [[ "$ARCH" == "arm64" && "$UNAME" == "Darwin" ]]; then
# Detect Apple Silicon
echo "BUILDOS: $BUILDOS"
echo "BUILDARCH: $BUILDARCH"
echo "GOOS: $GOOS"
echo "GOARCH: $GOARCH"

if [[ "$GOARCH" == "arm64" ]]; then
IS_ARM64=1
fi

if [[ "$ARCH" == "x86_64" && "$UNAME" == "Linux" && "${GOARCH:-}" == "arm64" ]]; then
echo "cross-compiling linux-arm64"
export CC="clang --sysroot=/usr/aarch64-linux-gnu"
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"
Expand All @@ -72,7 +115,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
Expand All @@ -82,7 +125,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"
Expand Down Expand Up @@ -113,7 +156,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"
Expand All @@ -140,9 +183,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"
Expand Down Expand Up @@ -170,7 +213,7 @@ else
fi

if [[ ! -e "$ROOT/ffmpeg/libavcodec/libavcodec.a" ]]; then
git clone https://github.com/livepeer/FFmpeg.git "$ROOT/ffmpeg" || echo "FFmpeg dir already exists"
git clone /Users/iameli/code/FFmpeg "$ROOT/ffmpeg" || echo "FFmpeg dir already exists"
cd "$ROOT/ffmpeg"
git checkout 2e18d069668c143f3c251067abd25389e411d022
./configure ${TARGET_OS:-} $DISABLE_FFMPEG_COMPONENTS --fatal-warnings \
Expand All @@ -181,7 +224,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" \
Expand Down

0 comments on commit a462ce8

Please sign in to comment.