Skip to content

Instantly share code, notes, and snippets.

@bettar
Last active July 28, 2022 15:28
Show Gist options
  • Save bettar/c258836150d398dc6bfa13c433beabc6 to your computer and use it in GitHub Desktop.
Save bettar/c258836150d398dc6bfa13c433beabc6 to your computer and use it in GitHub Desktop.
Bash shell script to build libjpeg on macOS for the purpose of creating a universal binary
#!/bin/bash
# Alex Bettarini - 28 Jul 2022
#-------------------------------------------------------------------------------
STEP_DOWNLOAD_LIB_JPEG=true
STEP_INFO_JPEG=true
STEP_CONFIGURE_LIB_JPEG=true
STEP_COMPILE_LIB_JPEG=true # and install
STEP_PACKAGE_LIB_JPEG=true
#-------------------------------------------------------------------------------
#JPEG_VERSION=9d
JPEG_VERSION=9e
#-------------------------------------------------------------------------------
CONFIG_BIN_DIR=bin
JPEG=jpeg-$JPEG_VERSION
WD=$(pwd)
SRC=$WD/temp/src
BLD=$WD/temp/bld
BIN=$WD/temp/$CONFIG_BIN_DIR
SRC_JPEG=$SRC/$JPEG
BLD_JPEG=$BLD/$JPEG
BIN_JPEG=$BIN/$JPEG
mkdir -p $SRC
#----------------------------------------------------------------------------
if [ $STEP_DOWNLOAD_LIB_JPEG ] ; then
if [ -d $SRC_JPEG ] ; then
echo "=== Download $JPEG exists"
else
cd $SRC
echo "=== Download $JPEG"
curl -O http://www.ijg.org/files/jpegsrc.v$JPEG_VERSION.tar.gz
tar -zxf jpegsrc.v$JPEG_VERSION.tar.gz
rm jpegsrc.v$JPEG_VERSION.tar.gz
fi
fi
#----------------------------------------------------------------------------
if [ $STEP_INFO_JPEG ] ; then
cd $SRC_JPEG
echo -n "=== JPEG: " ; grep "JVERSION" jversion.h | cut -d"\"" -f2
grep "define JPEG_LIB_VERSION" jpeglib.h
fi
#----------------------------------------------------------------------------
if [ $STEP_CONFIGURE_LIB_JPEG ] ; then
echo -e "\n=== Configure $JPEG in $BLD_JPEG"
mkdir -p "$BLD_JPEG" ; cd "$BLD_JPEG"
export CFLAGS="-arch x86_64 -arch arm64"
"$SRC_JPEG"/configure \
--prefix="$BIN_JPEG"
fi
#----------------------------------------------------------------------------
if [ $STEP_COMPILE_LIB_JPEG ] ; then
cd $BLD_JPEG
echo -e "\n=== Build $JPEG"
make
echo -e "\n=== Install $JPEG"
make install
fi
#----------------------------------------------------------------------------
if [ $STEP_PACKAGE_LIB_JPEG ] ; then
cd $BIN
echo -e "\n=== Package $JPEG"
zip -rXq $WD/$JPEG.zip "$JPEG"
fi
@bettar
Copy link
Author

bettar commented Jul 28, 2022

At present I have to run this script on two different machines, one with an X86_64 CPU and another with an arm64 CPU, then combine the two libraries with lipo. How can the script be extended to allow building the universal binary only one one machine ? I have tried passing CXX="g++ -arch x86_64 -arch arm64" to the configure command, but it didn't work...

@bettar
Copy link
Author

bettar commented Jul 28, 2022

Doing this kind of stuff with CMake is a breeze, but the libjpeg project uses Autotools to configure the build, and I haven't been able to configure it correctly so far.

@bettar
Copy link
Author

bettar commented Jul 28, 2022

It finally worked after adding export CFLAGS="-arch x86_64 -arch arm64" before calling config

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment