From 11ffcd671e39e2dc7f851db786b348259db5830d Mon Sep 17 00:00:00 2001 From: Richard Goodwin Date: Mon, 18 Jan 2021 13:08:44 -0600 Subject: [PATCH 1/4] Random patches and tricks for building on M1 Needs newer code in some cases, and some conditionals to check platform. The external patches possibly should be brought in? Seems to work for now if anyone is in need. Learnings from https://github.com/ssut/ffmpeg-on-apple-silicon --- build-ffmpeg | 42 +++++++++++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/build-ffmpeg b/build-ffmpeg index 88b1e273..dcdbbdc5 100755 --- a/build-ffmpeg +++ b/build-ffmpeg @@ -8,11 +8,18 @@ VERSION=1.21 CWD=$(pwd) PACKAGES="$CWD/packages" WORKSPACE="$CWD/workspace" -CFLAGS="-I$WORKSPACE/include" +CFLAGS='-I$WORKSPACE/include' LDFLAGS="-L$WORKSPACE/lib" LDEXEFLAGS="" EXTRALIBS="-ldl -lpthread -lm -lz" CONFIGURE_OPTIONS=() +# Check for Apple Silicon +if [[ ( "$(uname -m)" == "arm64" ) && ( "$OSTYPE" == "darwin"* ) ]]; then + # If arm64 AND darwin (macOS) + export ARCH=arm64 + export MACOSX_DEPLOYMENT_TARGET=11.0 +fi + # Speed up the process # Env Var NUMJOBS overrides automatic detection @@ -350,11 +357,15 @@ if build "zlib"; then fi if build "openssl"; then - download "https://www.openssl.org/source/openssl-1.1.1h.tar.gz" - execute ./config --prefix="${WORKSPACE}" --openssldir="${WORKSPACE}" --with-zlib-include="${WORKSPACE}"/include/ --with-zlib-lib="${WORKSPACE}"/lib no-shared zlib + download "https://www.openssl.org/source/openssl-1.1.1i.tar.gz" + if [[ ( "$ARCH" == "arm64" ) && ( "$OSTYPE" == "darwin"* ) ]]; then + sed -n 's/\(##### GNU Hurd\)/"darwin64-arm64-cc" => { \n inherit_from => [ "darwin-common", asm("aarch64_asm") ],\n CFLAGS => add("-Wall"),\n cflags => add("-arch arm64 "),\n lib_cppflags => add("-DL_ENDIAN"),\n bn_ops => "SIXTY_FOUR_BIT_LONG", \n perlasm_scheme => "macosx", \n}, \n\1/g' Configurations/10-main.conf + execute ./Configure --prefix="${WORKSPACE}" no-shared no-asm darwin64-arm64-cc + else + execute ./config --prefix="${WORKSPACE}" --openssldir="${WORKSPACE}" --with-zlib-include="${WORKSPACE}"/include/ --with-zlib-lib="${WORKSPACE}"/lib no-shared zlib + fi execute make -j $MJOBS execute make install_sw - build_done "openssl" fi CONFIGURE_OPTIONS+=("--enable-openssl") @@ -377,7 +388,11 @@ if build "x264"; then if [[ "$OSTYPE" == "linux-gnu" ]]; then execute ./configure --prefix="${WORKSPACE}" --enable-static --enable-pic CXXFLAGS="-fPIC" - else + elif [[ ( "$ARCH" == "arm64" ) && ( "$OSTYPE" == "darwin"* ) ]]; then + ## Get latest code to support arm64 + git clone --depth 1 -b master https://code.videolan.org/videolan/x264.git + cd x264 + else execute ./configure --prefix="${WORKSPACE}" --enable-static --enable-pic fi @@ -392,6 +407,7 @@ CONFIGURE_OPTIONS+=("--enable-libx264") if build "x265"; then download "https://github.com/videolan/x265/archive/Release_3.5.tar.gz" "x265-3.5.tar.gz" cd build/linux || exit + execute cmake -DCMAKE_INSTALL_PREFIX="${WORKSPACE}" -DENABLE_SHARED=off -DBUILD_SHARED_LIBS=OFF ../../source execute make -j $MJOBS execute make install @@ -442,6 +458,12 @@ CONFIGURE_OPTIONS+=("--enable-libxvid") if build "vid_stab"; then download "https://github.com/georgmartius/vid.stab/archive/v1.1.0.tar.gz" "vid.stab-1.1.0.tar.gz" + ## rich + if [[ ( "$ARCH" == "arm64" ) && ( "$OSTYPE" == "darwin"* ) ]]; then + curl -s -o "$PACKAGES/vid.stab-1.1.0/fix_cmake_quoting.patch" https://raw.githubusercontent.com/Homebrew/formula-patches/5bf1a0e0cfe666ee410305cece9c9c755641bfdf/libvidstab/fix_cmake_quoting.patch + patch -p1 < fix_cmake_quoting.patch + fi + ## rich execute cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="${WORKSPACE}" -DUSE_OMP=OFF -DENABLE_SHARED=off . execute make execute make install @@ -454,7 +476,13 @@ if build "av1"; then download "https://aomedia.googlesource.com/aom/+archive/b52ee6d44adaef8a08f6984390de050d64df9faa.tar.gz" "av1.tar.gz" "av1" make_dir "$PACKAGES"/aom_build cd "$PACKAGES"/aom_build || exit - execute cmake -DENABLE_TESTS=0 -DCMAKE_INSTALL_PREFIX="${WORKSPACE}" -DCMAKE_INSTALL_LIBDIR=lib "$PACKAGES"/av1 + ## rich + if [[ ( "$ARCH" == "arm64" ) && ( "$OSTYPE" == "darwin"* ) ]]; then + execute cmake -DENABLE_TESTS=0 -DCMAKE_INSTALL_PREFIX="${WORKSPACE}" -DCMAKE_INSTALL_LIBDIR=lib -DCONFIG_RUNTIME_CPU_DETECT=0 "$PACKAGES"/av1 + else + execute cmake -DENABLE_TESTS=0 -DCMAKE_INSTALL_PREFIX="${WORKSPACE}" -DCMAKE_INSTALL_LIBDIR=lib "$PACKAGES"/av1 + fi + ## rich (added runtime detect ) execute make -j $MJOBS execute make install @@ -560,7 +588,7 @@ CONFIGURE_OPTIONS+=("--enable-libwebp") ## if build "libsdl"; then - download "https://www.libsdl.org/release/SDL2-2.0.12.tar.gz" + download "https://www.libsdl.org/release/SDL2-2.0.14.tar.gz" execute ./configure --prefix="${WORKSPACE}" --disable-shared --enable-static execute make -j $MJOBS execute make install From b18759f9fc4112314145c7f755dd5d6cf32aa434 Mon Sep 17 00:00:00 2001 From: Richard Goodwin Date: Tue, 19 Jan 2021 10:25:40 -0600 Subject: [PATCH 2/4] Changes per request, plus bonus * Define M1 variable once * Grab x264 archive vs git * configure vs Configure * bonus: output binary type. Feel free to take or leave :) --- build-ffmpeg | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/build-ffmpeg b/build-ffmpeg index dcdbbdc5..c7bc7760 100755 --- a/build-ffmpeg +++ b/build-ffmpeg @@ -12,12 +12,14 @@ CFLAGS='-I$WORKSPACE/include' LDFLAGS="-L$WORKSPACE/lib" LDEXEFLAGS="" EXTRALIBS="-ldl -lpthread -lm -lz" +MACOS_M1=false CONFIGURE_OPTIONS=() # Check for Apple Silicon if [[ ( "$(uname -m)" == "arm64" ) && ( "$OSTYPE" == "darwin"* ) ]]; then # If arm64 AND darwin (macOS) export ARCH=arm64 export MACOSX_DEPLOYMENT_TARGET=11.0 + MACOS_M1=true fi @@ -153,6 +155,19 @@ build_done () { touch "$PACKAGES/$1.done" } +verify_binary_type () { + BINARY_TYPE=$(file $WORKSPACE/bin/ffmpeg | sed -n 's/^.*\:\ \(.*$\)/\1/p') + echo "" + case $BINARY_TYPE in + "Mach-O 64-bit executable arm64") + echo "Successfully built Apple Silicon (M1): ${BINARY_TYPE}" + ;; + *) + echo "Successfully built binary for ${OSTYPE}: ${BINARY_TYPE}" + ;; + esac +} + cleanup () { remove_dir "$PACKAGES" remove_dir "$WORKSPACE" @@ -358,9 +373,9 @@ fi if build "openssl"; then download "https://www.openssl.org/source/openssl-1.1.1i.tar.gz" - if [[ ( "$ARCH" == "arm64" ) && ( "$OSTYPE" == "darwin"* ) ]]; then + if $MACOS_M1 ; then sed -n 's/\(##### GNU Hurd\)/"darwin64-arm64-cc" => { \n inherit_from => [ "darwin-common", asm("aarch64_asm") ],\n CFLAGS => add("-Wall"),\n cflags => add("-arch arm64 "),\n lib_cppflags => add("-DL_ENDIAN"),\n bn_ops => "SIXTY_FOUR_BIT_LONG", \n perlasm_scheme => "macosx", \n}, \n\1/g' Configurations/10-main.conf - execute ./Configure --prefix="${WORKSPACE}" no-shared no-asm darwin64-arm64-cc + execute ./configure --prefix="${WORKSPACE}" no-shared no-asm darwin64-arm64-cc else execute ./config --prefix="${WORKSPACE}" --openssldir="${WORKSPACE}" --with-zlib-include="${WORKSPACE}"/include/ --with-zlib-lib="${WORKSPACE}"/lib no-shared zlib fi @@ -388,13 +403,14 @@ if build "x264"; then if [[ "$OSTYPE" == "linux-gnu" ]]; then execute ./configure --prefix="${WORKSPACE}" --enable-static --enable-pic CXXFLAGS="-fPIC" - elif [[ ( "$ARCH" == "arm64" ) && ( "$OSTYPE" == "darwin"* ) ]]; then + elif $MACOS_M1 ; then ## Get latest code to support arm64 - git clone --depth 1 -b master https://code.videolan.org/videolan/x264.git - cd x264 - else + download "https://code.videolan.org/videolan/x264/-/archive/0d754ec36013fee82978496cd56fbd48824910b3/x264-0d754ec36013fee82978496cd56fbd48824910b3.tar.gz" "x264-0d754ec.tar.gz" + cd "${PACKAGES}"/x264-0d754ec + else execute ./configure --prefix="${WORKSPACE}" --enable-static --enable-pic fi + execute make -j $MJOBS execute make install @@ -458,9 +474,10 @@ CONFIGURE_OPTIONS+=("--enable-libxvid") if build "vid_stab"; then download "https://github.com/georgmartius/vid.stab/archive/v1.1.0.tar.gz" "vid.stab-1.1.0.tar.gz" - ## rich - if [[ ( "$ARCH" == "arm64" ) && ( "$OSTYPE" == "darwin"* ) ]]; then - curl -s -o "$PACKAGES/vid.stab-1.1.0/fix_cmake_quoting.patch" https://raw.githubusercontent.com/Homebrew/formula-patches/5bf1a0e0cfe666ee410305cece9c9c755641bfdf/libvidstab/fix_cmake_quoting.patch + + if $MACOS_M1 ; then + + curl -s -o "$PACKAGES/vid.stab-1.1.0/fix_cmake_quoting.patch" https://raw.githubusercontent.com/Homebrew/formula-patches/5bf1a0e0cfe666ee410305cece9c9c755641bfdf/libvidstab/fix_cmake_quoting.patch patch -p1 < fix_cmake_quoting.patch fi ## rich @@ -476,13 +493,11 @@ if build "av1"; then download "https://aomedia.googlesource.com/aom/+archive/b52ee6d44adaef8a08f6984390de050d64df9faa.tar.gz" "av1.tar.gz" "av1" make_dir "$PACKAGES"/aom_build cd "$PACKAGES"/aom_build || exit - ## rich - if [[ ( "$ARCH" == "arm64" ) && ( "$OSTYPE" == "darwin"* ) ]]; then + if $MACOS_M1 ; then execute cmake -DENABLE_TESTS=0 -DCMAKE_INSTALL_PREFIX="${WORKSPACE}" -DCMAKE_INSTALL_LIBDIR=lib -DCONFIG_RUNTIME_CPU_DETECT=0 "$PACKAGES"/av1 else execute cmake -DENABLE_TESTS=0 -DCMAKE_INSTALL_PREFIX="${WORKSPACE}" -DCMAKE_INSTALL_LIBDIR=lib "$PACKAGES"/av1 fi - ## rich (added runtime detect ) execute make -j $MJOBS execute make install @@ -680,9 +695,11 @@ execute make install INSTALL_FOLDER="/usr/bin" if [[ "$OSTYPE" == "darwin"* ]]; then -INSTALL_FOLDER="/usr/local/bin" + INSTALL_FOLDER="/usr/local/bin" fi +verify_binary_type + echo "" echo "Building done. The following binaries can be found here:" echo "- ffmpeg: $WORKSPACE/bin/ffmpeg" @@ -710,11 +727,11 @@ elif [[ ! "$SKIPINSTALL" == "yes" ]]; then if command_exists "sudo"; then sudo cp "$WORKSPACE/bin/ffmpeg" "$INSTALL_FOLDER/ffmpeg" sudo cp "$WORKSPACE/bin/ffprobe" "$INSTALL_FOLDER/ffprobe" - sudo cp "$WORKSPACE/bin/ffplay" "$INSTALL_FOLDER/ffplay" + sudo cp "$WORKSPACE/bin/ffplay" "$INSTALL_FOLDER/ffplay" else cp "$WORKSPACE/bin/ffmpeg" "$INSTALL_FOLDER/ffmpeg" cp "$WORKSPACE/bin/ffprobe" "$INSTALL_FOLDER/ffprobe" - cp "$WORKSPACE/bin/ffplay" "$INSTALL_FOLDER/ffplay" + cp "$WORKSPACE/bin/ffplay" "$INSTALL_FOLDER/ffplay" fi echo "Done. FFmpeg is now installed to your system." ;; From 64a2c0ed8c892a409d3013b947d7663a0af7976d Mon Sep 17 00:00:00 2001 From: Richard Goodwin Date: Tue, 19 Jan 2021 10:29:45 -0600 Subject: [PATCH 3/4] Make binary echo statements balanced --- build-ffmpeg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-ffmpeg b/build-ffmpeg index c7bc7760..85141eb3 100755 --- a/build-ffmpeg +++ b/build-ffmpeg @@ -160,7 +160,7 @@ verify_binary_type () { echo "" case $BINARY_TYPE in "Mach-O 64-bit executable arm64") - echo "Successfully built Apple Silicon (M1): ${BINARY_TYPE}" + echo "Successfully built Apple Silicon (M1) for ${OSTYPE}: ${BINARY_TYPE}" ;; *) echo "Successfully built binary for ${OSTYPE}: ${BINARY_TYPE}" From c5434ee4a2ebec2b409500325602721119bd0be5 Mon Sep 17 00:00:00 2001 From: Richard Goodwin Date: Tue, 19 Jan 2021 12:32:22 -0600 Subject: [PATCH 4/4] Use referenced commit for x264 to test in CI/CD branched from "apple_silicon" branch. --- build-ffmpeg | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/build-ffmpeg b/build-ffmpeg index 85141eb3..e5641c3c 100755 --- a/build-ffmpeg +++ b/build-ffmpeg @@ -399,14 +399,12 @@ fi ## if build "x264"; then - download "https://code.videolan.org/videolan/x264/-/archive/stable/x264-stable.tar.bz2" + download "https://code.videolan.org/videolan/x264/-/archive/0d754ec36013fee82978496cd56fbd48824910b3/x264-0d754ec36013fee82978496cd56fbd48824910b3.tar.gz" "x264-0d754ec.tar.gz" + cd "${PACKAGES}"/x264-0d754ec + if [[ "$OSTYPE" == "linux-gnu" ]]; then execute ./configure --prefix="${WORKSPACE}" --enable-static --enable-pic CXXFLAGS="-fPIC" - elif $MACOS_M1 ; then - ## Get latest code to support arm64 - download "https://code.videolan.org/videolan/x264/-/archive/0d754ec36013fee82978496cd56fbd48824910b3/x264-0d754ec36013fee82978496cd56fbd48824910b3.tar.gz" "x264-0d754ec.tar.gz" - cd "${PACKAGES}"/x264-0d754ec else execute ./configure --prefix="${WORKSPACE}" --enable-static --enable-pic fi @@ -477,10 +475,10 @@ if build "vid_stab"; then if $MACOS_M1 ; then - curl -s -o "$PACKAGES/vid.stab-1.1.0/fix_cmake_quoting.patch" https://raw.githubusercontent.com/Homebrew/formula-patches/5bf1a0e0cfe666ee410305cece9c9c755641bfdf/libvidstab/fix_cmake_quoting.patch + curl -s -o "$PACKAGES/vid.stab-1.1.0/fix_cmake_quoting.patch" https://raw.githubusercontent.com/Homebrew/formula-patches/5bf1a0e0cfe666ee410305cece9c9c755641bfdf/libvidstab/fix_cmake_quoting.patch patch -p1 < fix_cmake_quoting.patch fi - ## rich + execute cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX="${WORKSPACE}" -DUSE_OMP=OFF -DENABLE_SHARED=off . execute make execute make install