Skip to content

Instantly share code, notes, and snippets.

@fador
Forked from fasterthanlime/windows.sh
Last active October 11, 2022 10:51
Show Gist options
  • Save fador/19c4eee37a0cfd80dcc01434bbea8765 to your computer and use it in GitHub Desktop.
Save fador/19c4eee37a0cfd80dcc01434bbea8765 to your computer and use it in GitHub Desktop.
Building ffmpeg & kvazaar with MSVC from msys2
#!/bin/bash
# put script to ~/Release/windows.sh
# run from ~: CI_ARCH=x86_64 ./Release/windows.sh
# Modified from https://gist.github.com/fasterthanlime/b674346115e88b762d76dac02fef6bd3
# to build Kvazaar with VS2022 support
set -e
if [[ -z "${FFRUST_VC_PATH}" ]]; then
if [[ -n "${FFRUST_RECURSING}" ]]; then
echo "Something went terribly wrong with our MVSC/msys2 magic"
exit 1
fi
###############################################################
# Detect Visual Studio installation, the galaxy brain way
###############################################################
VARSALL_2022_PATH='C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat'
VARSALL_2017_PATH='C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat'
VARSALL_2015_PATH='C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat'
# Look for various places MSVC could hide
for edition in 2022 2017 2015; do
echo "Looking for MVSC ${edition}..."
name="VARSALL_${edition}_PATH"
file="${!name}"
if [[ -f "${file}" ]]; then
echo "✓ Found MSVC ${edition}"
VARSALL_PATH=${file}
break
fi
done
# Translate our 'build architecture' to something the MSVC toolchain understands
case "${CI_ARCH}" in
x86_64) VARSALL_ARCH="amd64" ;;
i686) VARSALL_ARCH="x86" ;;
*) echo "Unknown arch: ${CI_ARCH}"; exit 1
esac
###############################################################
# Set up PATH so we can call Visual Studio command-line tools
###############################################################
# Make a temporary batch file that calls varsall.bat, and writes path
# to another temporary file.
(
cat <<EOF
call "${VARSALL_PATH}" ${VARSALL_ARCH}
set FFRUST_VC_PATH=%PATH%
%BASH_PATH% -lc '%SCRIPT_PATH%'
exit /b %ERRORLEVEL%
EOF
) > .generate_vc_path.bat
export BASH_PATH=$(cygpath -w $(which bash.exe))
export SCRIPT_PATH="${PWD}/Release/windows.sh"
export FFRUST_RECURSING=1
# gotta double up '/C' so that automatic mingw path translation
# does not kick in
cmd.exe //C .generate_vc_path.bat
exit 0
fi
###############################################################
# Prerequisites
###############################################################
if [[ -z $CI_ARCH ]]; then
echo "Missing argument: CI_ARCH"
exit 1
fi
for cmd in wget patch make; do
if hash ${cmd} 2>/dev/null; then
echo "✓ Found ${cmd}"
else
echo "Missing command: ${cmd}"
exit 1
fi
done
# cygpath translates between windows-style and unix-style paths
# '-p' translates a semi-colon-separated list to a colon-separate list
export PATH=${PATH}:$(cygpath -p -u "${FFRUST_VC_PATH}")
set -x
# Safety check
cl
###############################################################
# Config
###############################################################
FFMPEG_VERSION="5.1.2"
echo "Building for ${CI_ARCH}"
###############################################################
# Set up rest of build environment
###############################################################
# Make sources directory
rm -rf sources/
mkdir -p sources/
# Make prefix
export FFRUST_PREFIX=${PWD}/artifacts/${CI_ARCH}/prefix
mkdir -p ${FFRUST_PREFIX}
pushd sources
###############################################################
# Build Kvazaar
###############################################################
git clone https://github.com/ultravideo/kvazaar.git
pushd kvazaar
KVAZAAR_OPTS="--enable-static --disable-shared"
sed -i 's/-Wall -Wextra -Wvla -Wno-sign-compare -Wno-unused-parameter//g' configure.ac
sed -i 's/ -ftree-vectorize -fvisibility=hidden//g' configure.ac
sed -i 's/KVZ_CFLAGS=\"/KVZ_CFLAGS=\"-D__AVX2__ -DKVZ_STATIC_LIB -I.\/threadwrapper\/include\/ -I ..\/include\//g' configure.ac
sed -i 's/-I\$srcdir\/src/-I ./g' configure.ac
sed -i 's/AX_PTHREAD(/#AX_PTHREAD(/g' configure.ac
sed -i 's/PTHREAD_CFLAGS=-pthread//g' configure.ac
sed -i 's/PTHREAD_LIBS=-lpthread//g' configure.ac
sed -i 's/libkvazaar_la_SOURCES =/libkvazaar_la_SOURCES = \\\n\tthreadwrapper\/src\/pthread.cpp \\\n\tthreadwrapper\/src\/semaphore.cpp/g' src/Makefile.am
sed -i 's/kvazaar_SOURCES =/kvazaar_SOURCES = extras\/getopt.c /g' src/Makefile.am
sed -i 's/CFLAGS=\"\$PTHREAD_CFLAGS \$CFLAGS\"/CPPFLAGS=\"\$CFLAGS\"/g' configure.ac
sed -i 's/CC=\"\$PTHREAD_CC\"//g' configure.ac
sed -i 's/-mavx2/\/arch:AVX2/g' configure.ac
#--prefix=${FFRUST_PREFIX}
./autogen.sh
LD=link.exe CC=cl ./configure --host=x86_64-w64-mingw32 ${KVAZAAR_OPTS}
echo "configure done, running make"
make -j
make install
popd #kvazaar
###############################################################
# Build FFMPEG
###############################################################
wget -O - https://ffmpeg.org/releases/ffmpeg-${FFMPEG_VERSION}.tar.bz2 | tar xjf -
pushd ffmpeg-${FFMPEG_VERSION}/
EXTRA_CFLAGS="-DKVZ_STATIC_LIB -I/usr/include"
FEATURE_FFMPEG_OPTS="--disable-avdevice \
--disable-doc --disable-ffplay \
--disable-ffprobe --disable-network --enable-w32threads \
--enable-gpl --enable-libkvazaar \
--enable-avformat --enable-avcodec --enable-swscale --enable-swresample \
--enable-muxer=mp4 --enable-demuxer=mov \
--enable-decoder=h264 --enable-encoder=libkvazaar \
--enable-decoder=aac --enable-encoder=aac \
--enable-protocol=file \
--disable-shared --enable-static \
--enable-pic \
--extra-ldflags=-libpath:$(cygpath -w /usr/lib)"
case "${CI_ARCH}" in
x86_64) PLATFORM_FFMPEG_OPTS="--target-os=win64 --arch=x86_64" ;;
i686) PLATFORM_FFMPEG_OPTS="--target-os=win32 --arch=x86" ;;
*) echo "Unknown arch: ${CI_ARCH}"; exit 1
esac
./configure --toolchain=msvc --disable-x86asm ${PLATFORM_FFMPEG_OPTS} ${FEATURE_FFMPEG_OPTS} "--extra-cflags=${EXTRA_CFLAGS}"
make V=1
make install
popd # ffmpeg
popd # sources
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment