Skip to content

Commit

Permalink
Making scripts a bit more generalized and usable for testing purposes
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Feb 8, 2024
1 parent 9d31443 commit d3bee42
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 74 deletions.
102 changes: 66 additions & 36 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,52 +17,70 @@ declare -A REPOS=(
[libheif]="https://github.com/strukturag/libheif/archive/refs/tags/v1.17.6.tar.gz 2c8d3eedfd238a05311533f45ebfac5c"
)

BUILDS="libde265 x265 aom libwebp svt zlib libheif"
BUILDS=""
CLEAN=0
EXIT=0
CACHE=$SRC/.cache
BUILD_DIR=$SRC/build
DIST_DIR=
PRESET=
DUMP=
KVAZAAR=OFF
SVT=ON
DUMP=
OSX_SDK=
EXIT=0

OPTIND=1
while getopts "b:cC:d:p:k:s:D:A:x" opt; do
while getopts "b:cC:d:p:P:D:k:s:x" opt; do
case "$opt" in
b) BUILDS=$OPTARG ;;
b) BUILDS+="$OPTARG " ;;
c) CLEAN=1 ;;
C) CACHE=$OPTARG ;;
d) BUILD_DIR=$OPTARG ;;
p) DIST_DIR=$OPTARG ;;
P) PRESET=$OPTARG ;;
D) DUMP=$OPTARG ;;
k) KVAZAAR=$(tr '[a-z]' '[A-Z]' <<< "$OPTARG") ;;
s) SVT=$(tr '[a-z]' '[A-Z]' <<< "$OPTARG") ;;
D) DUMP=$(realpath "$OPTARG") ;;
A) OSX_SDK=$OPTARG ;;
x) EXIT=1 ;;
esac
done

if [ -z "$BUILDS" ]; then
BUILDS="libde265 x265 aom libwebp svt zlib libheif"
fi

# cross config for darwin
CROSS_TRIPLE= CMAKE=cmake CC= CXX=
if [ ! -z "$OSX_SDK" ]; then
CROSS_TRIPLE="$(awk '{print $1}' <<< "$OSX_SDK")-apple-$(awk '{print $2}' <<< "$OSX_SDK")"
CMAKE=cmake CC=$CC CXX=$CXX
if [[ "$CROSS_TRIPLE" =~ apple ]]; then
CMAKE="$CROSS_TRIPLE-cmake"
CC="$CROSS_TRIPLE-clang"
CXX="$CROSS_TRIPLE-clang++"
fi

echo "KVAZAAR: $KVAZAAR SVT: $SVT"
# swap x265 for kvazaar
if [[ ! ("$BUILDS" =~ kvazaar) && "$KVAZAAR" == "ON" ]]; then
if [[ "$BUILDS" =~ x265 && "$KVAZAAR" == "ON" ]]; then
BUILDS=$(sed -e 's/x265/kvazaar/' <<< "$BUILDS")
fi
# remove svt
if [[ "$BUILDS" =~ svt && "$SVT" == "OFF" ]]; then
BUILDS=$(sed -e 's/svt//' <<< "$BUILDS")
fi
echo "BUILDS: $BUILDS"

# working directories
mkdir -p $CACHE $BUILD_DIR

CACHE=$(realpath "$CACHE")
BUILD_DIR=$(realpath "$BUILD_DIR")
if [ -z "$DIST_DIR" ]; then
mkdir -p $BUILD_DIR/dist
DIST_DIR=$(realpath $BUILD_DIR/dist)
DIST_DIR=$BUILD_DIR/dist
fi
mkdir -p $DIST_DIR
DIST_DIR=$(realpath "$DIST_DIR")

# check preset
if [[ ! -z "$PRESET" && ! -f $CACHE/$PRESET ]]; then
echo "error: $CACHE/$PRESET doesn't exist!"
exit 1
fi

repourl() {
Expand Down Expand Up @@ -131,29 +149,53 @@ for build in $BUILDS; do
fi
done

# dump preset config to cache
if [ ! -z "$DUMP" ]; then
echo "DUMPING: $DUMP"
DUMPFILE=$CACHE/$DUMP
echo "DUMPING: $DUMPFILE"
pushd $BUILD_DIR/libheif &> /dev/null
$CMAKE --preset release-noplugins -N|sed 1,2d > $DUMP
$CMAKE --preset release-noplugins -N|sed 1,2d > $DUMPFILE
popd &> /dev/null
perl -pi -e 's/^\s*//' $DUMP
perl -pi -e 's/"//g' $DUMP
cat $DUMP
perl -pi -e 's/^\s*//' $DUMPFILE
perl -pi -e 's/"//g' $DUMPFILE
cat $DUMPFILE
echo "DUMPED: $DUMPFILE"
exit
fi

if [ "$EXIT" = "1" ]; then
exit
fi

build_vars() {
preset_vars() {
if [ -z "$PRESET" ]; then
echo -n "--preset release-noplugins"
return
fi

while read line; do
if [[ "$KVAZAAR" = "ON" && "$line" =~ [xX]265 ]]; then
continue
elif [[ "$SVT" = "ON" && "$line" =~ SvtEnc ]]; then
continue
fi
echo -n "-D$line "
done < "$1"
done < $CACHE/$PRESET

local packages="LIBDE265 X265 AOM LIBSHARPYUV SvtEnc ZLIB"

# turn off x265, swapping for kvazaar
if [ "$KVAZAAR" = "ON" ]; then
echo -n "-DWITH_X265=OFF -DWITH_KVAZAAR=ON -DCMAKE_CXX_FLAGS=-Wno-error=sometimes-uninitialized "
packages=$(sed -e 's/X265/KVAZAAR/' <<< "$packages")
fi

# turn off svt
if [ "$SVT" != "ON" ]; then
echo -n "-DWITH_SvtEnc=OFF "
fi

# output package config
for pkg in $packages; do
extra=""
if [ "$pkg" = "LIBSHARPYUV" ]; then
Expand Down Expand Up @@ -259,9 +301,6 @@ build_libwebp() {
}

build_svt() {
if [ "$SVT" != "ON" ]; then
return
fi
mkdir -p $1/build
pushd $1 &> /dev/null
(set -x;
Expand Down Expand Up @@ -292,17 +331,9 @@ build_zlib() {
}

build_libheif() {
# hack for windows preset issue
local preset="--preset release-noplugins" extra=""
if [[ "$CROSS_TRIPLE" =~ (w64|apple) ]]; then
preset=$(build_vars $CACHE/linux_amd64.preset)
fi
if [ "$KVAZAAR" = "ON" ]; then
extra="-DWITH_X265=OFF -DWITH_KVAZAAR=ON -DCMAKE_CXX_FLAGS=-Wno-error=sometimes-uninitialized"
fi

mkdir -p $1/build
pushd $1 &> /dev/null
local preset=$(preset_vars)
(set -x;
PKG_CONFIG_PATH=$DIST_DIR/lib/pkgconfig \
$CMAKE \
Expand All @@ -316,7 +347,6 @@ build_libheif() {
-DWITH_SvtEnc=$SVT \
$extra \
-B ./build
#CMAKE --install ./build
ninja -C ./build install
)
popd &> /dev/null
Expand All @@ -325,5 +355,5 @@ build_libheif() {
# build
for build in $BUILDS; do
echo "BUILDING: $build"
eval "build_$build" "$(realpath $BUILD_DIR/$build)"
eval "build_$build" "$BUILD_DIR/$build"
done
111 changes: 73 additions & 38 deletions gen.sh
Original file line number Diff line number Diff line change
@@ -1,81 +1,105 @@
#!/bin/bash

OSX_SDK="darwin20.4"

SRC=$(realpath $(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd))

set -e

CACHE=$SRC/.cache
BUILD_TARGETS=""
UPDATE=0

mkdir -p $CACHE

declare -A TARGETS=(
[darwin_amd64]=darwin_x86_64
[darwin_arm64]=darwin_aarch64
[linux_amd64]=linux_x86_64-full
[linux_arm64]=linux_arm64-full
[linux_arm]=linux_armv7
[windows_amd64]=windows_static-x64-posix
# [windows_amd64]=windows_static-x64
# [windows_arm64]=windows_arm64
)
#[dragonfly_amd64]=
#[freebsd_amd64]=
#[netbsd_amd64]=
#[openbsd_amd64]=
#[windows_arm64]=
#[solaris_amd64]=

BUILD_TARGETS=
CLEAN=0
CACHE=$SRC/.cache
BUILDS=
UPDATE=0

OPTIND=1
while getopts "t:u" opt; do
while getopts "t:b:cC:u" opt; do
case "$opt" in
t) BUILD_TARGETS="$OPTARG" ;;
t) BUILD_TARGETS=$OPTARG ;;
b) BUILDS=$OPTARG ;;
c) CLEAN=1 ;;
C) CACHE=$OPTARG ;;
u) UPDATE=1 ;;
esac
done

mkdir -p $CACHE
CACHE=$(realpath "$CACHE")

relname() {
sed -e "s%^$PWD/%%" <<< "$1"
}

osxcross() {
local target=$1
local target=$1 extra=$2
local build_target="${TARGETS[$target]}"
local platform=$(sed -e 's/[-_]/ /' <<< "$build_target"|awk '{print $1}')
local arch=$(sed -e 's/[-_]/ /' <<< "$build_target"|awk '{print $2}')
local extra=""

if [ ! -z "$extra" ]; then
extra+=" "
fi

# add builds
for build in $BUILDS; do
extra+="-b $build "
done

# enable kvazaar for arm64
if [[ "$target" =~ arm ]]; then
extra="-k ON"
case $target in
darwin_arm64) extra+="-k ON ";;
esac

# determine osxcross config info
local osxcross_conf=$(type -p osxcross-conf)
if [ -z "$osxcross_conf" ]; then
echo "error: could not find osxcross-conf"
exit 1
fi
eval "$($osxcross_conf)"
if [ -z "$OSXCROSS_TARGET" ]; then
echo "error: OSXCROSS_TARGET is empty!"
exit 1
fi

dockcross linux_amd64 ".cache/linux_amd64.preset"
$SRC/build.sh -d $SRC/.cache/$target -A "$arch $OSX_SDK" $extra
dockcross linux_amd64 "-D $target.preset"
CROSS_TRIPLE="$arch-apple-$OSXCROSS_TARGET" \
$SRC/build.sh \
-d $SRC/.cache/$target \
-P $target.preset \
$extra
}

dockcross() {
local target=$1
local target=$1 extra=$2
local build_target="${TARGETS[$target]}"
local platform=$(sed -e 's/[-_]/ /' <<< "$build_target"|awk '{print $1}')
local arch=$(sed -e 's/[-_]/ /' <<< "$build_target"|awk '{print $2}')
local image="docker.io/dockcross/$platform-$arch:latest"

# determine if svt-av1 should be used or not
local svt=ON
if [ ! -z "$extra" ]; then
extra+=" "
fi

# add builds
for build in $BUILDS; do
extra+="-b $build "
done

# disable svt-av1 for arm
case $target in
linux_arm) svt=OFF ;;
linux_arm) extra+="-s OFF " ;;
windows_arm64) extra+="-s OFF -k OFF " ;;
esac

# hack for dump param
local dump=""
if [ ! -z "$2" ]; then
dump="-D $2"
fi

if [ "$UPDATE" = "1" ]; then
(set -x;
podman pull $image
Expand All @@ -87,14 +111,19 @@ dockcross() {
$image \
> $CACHE/build-$target.sh
chmod +x $CACHE/build-$target.sh
)
pushd $SRC &> /dev/null
(set -x;
$CACHE/build-$target.sh \
bash -c "./build.sh -d .cache/$target -s $svt $dump"
bash -c "./build.sh -d .cache/$target $extra"
)
popd &> /dev/null
}

wincross() {
dockcross linux_amd64 ".cache/linux_amd64.preset"
dockcross "$1"
local target=$1 extra=$2
dockcross linux_amd64 "-D $target.preset"
dockcross "$target" "-P $target.preset $extra"
}

if [ -z "$BUILD_TARGETS" ]; then
Expand All @@ -109,21 +138,27 @@ for TARGET in $BUILD_TARGETS; do
BUILD_TARGET="${TARGETS[$TARGET]}"
PLATFORM=$(sed -e 's/[-_]/ /' <<< "$BUILD_TARGET"|awk '{print $1}')
ARCH=$(sed -e 's/[-_]/ /' <<< "$BUILD_TARGET"|awk '{print $2}')
DEST=$SRC/libheif/$TARGET

if [[ -z "$BUILD_TARGET" || -z "$PLATFORM" || -z "$ARCH" ]]; then
echo "error: invalid target '$TARGET'"
exit 1
fi

echo "TARGET: $TARGET -> $(relname "$BUILD_DIR") ($PLATFORM/$ARCH)"
if [ "$CLEAN" = "1" ]; then
(set -x;
rm -rf $BUILD_DIR $DEST
)
fi

echo "GENERATING: $TARGET -> $(relname "$BUILD_DIR") ($PLATFORM/$ARCH)"
case $TARGET in
darwin*) osxcross "$TARGET" ;;
windows*) wincross "$TARGET" ;;
*) dockcross "$TARGET" ;;
esac

DIST_DIR=$BUILD_DIR/dist
DEST=$SRC/libheif/$TARGET
mkdir -p $DEST
(set -x;
cp $DIST_DIR/lib/*.a $DEST
Expand Down

0 comments on commit d3bee42

Please sign in to comment.