Skip to content

Instantly share code, notes, and snippets.

@zturtleman
Last active October 30, 2023 07:24
Show Gist options
  • Save zturtleman/c6a7df7e134c7b9c958fd634bf38e9aa to your computer and use it in GitHub Desktop.
Save zturtleman/c6a7df7e134c7b9c958fd634bf38e9aa to your computer and use it in GitHub Desktop.
Scripts for building SDL 2 libraries for Windows (MinGW, MSVC) and macOS.
#!/bin/bash
# Run in SDL 2 source directory on Linux.
# Built libraries will be copied to ./ztm-libs/{win32,win64,macos-ub1,macos-ub2}
#
# Set up:
# 1. Windows libaries:
# Install mingw-w64 for i686 and x86_64
#
# 2. macOS libraries:
# Follow osxcross directions to set up macOS (11.3) SDK to build a Universal 2 (arm64/x86_64) dylib.
# (I rename osxcross/target/ to osxcross/target-11.3/ but you could change OSXCROSS_TARGET_UB2 below instead.)
#
# Download rcodesign to ad-hoc sign macOS libraries.
# I'm not entirely sure how necessary this is but it fixes broken signiture checksums after using strip.
# Info: https://github.com/indygreg/PyOxidizer/tree/main/apple-codesign
# Download page: https://github.com/indygreg/PyOxidizer/releases/tag/apple-codesign%2F0.17.0
#
# 3. If building SDL < 2.24.0, you can also build Universal 1 dylib with ppc/x86/x86_64.
# If you haven't yet, rename osxcross/target/ to osxcross/target-11.3/
# Repeat osxcross set up with macOS 10.13 SDK. Rename osxcross/target/ to osxcross/target-10.13/.
#
# For Universal 1, you need to extract the SDL 2.0.1 ppc build from ioq3.
# git clone https://github.com/ioquake/ioq3
# mkdir ./ztm-libs-ioq3-prebuilt-ppc
# x86_64-apple-darwin17-lipo -extract ppc ioq3/code/libs/macos-ub/libSDL2-2.0.0.dylib -o ./ztm-libs-ioq3-prebuilt-ppc/libSDL2-2.0.0.dylib
# x86_64-apple-darwin17-lipo -extract ppc ioq3/code/libs/macos-ub/libSDL2main.a -o ./ztm-libs-ioq3-prebuilt-ppc/libSDL2main.a
# For building macOS libraries; change for your system.
OSXCROSS_TARGET_UB1=$HOME/tools/osxcross/target-10.13
OSXCROSS_TARGET_UB2=$HOME/tools/osxcross/target-11.3
RCODESIGN=$HOME/tools/apple-codesign-0.17.0-x86_64-unknown-linux-musl/rcodesign
# You can set this to 0 and then enable one BUILD_* manually below.
BUILD_DEFAULT=1
# Build Windows x86 (SDL2.dll).
BUILD_WIN32=$BUILD_DEFAULT
# Build Windows x86_64 (SDL2.dll).
BUILD_WIN64=$BUILD_DEFAULT
# Build Windows x86_64 (SDL264.dll).
# Renames SDL2.dll to SDL264.dll to match ioquake3 naming convension.
BUILD_WIN64_SDL264=$BUILD_DEFAULT
# Build macOS Universal Binary 1 (macOS 10.6+, x86, x86_64)
# - Merged with pre-built powerpc (macOS 10.5+, ppc)
# - Only for SDL < 2.24.0
BUILD_MACOS_UB1=0
# Build macOS Universal Binary 2 (macOS 10.9+, x86_64, arm64)
# I'm not sure what the oldest version that works on macOS arm64 is (2.0.14 works).
BUILD_MACOS_UB2=$BUILD_DEFAULT
if [ -n "$BUILD_WIN32" ] && [ "$BUILD_WIN32" -eq 1 ]; then
mkdir -p ztm-mingw-x86
cd ztm-mingw-x86
if [ ! -f ./config.log ]; then
../configure --host=i686-w64-mingw32 --prefix=$(pwd)/prefix
fi
make -j$(nproc)
make install
i686-w64-mingw32-strip -x $(pwd)/prefix/bin/*.dll $(pwd)/prefix/lib/*.a
mkdir -p ../ztm-libs/win32/
cp $(pwd)/prefix/bin/*.dll $(pwd)/prefix/lib/*.a ../ztm-libs/win32/
cd ..
fi
if [ -n "$BUILD_WIN64" ] && [ "$BUILD_WIN64" -eq 1 ]; then
mkdir -p ztm-mingw-x86_64
cd ztm-mingw-x86_64
if [ ! -f ./config.log ]; then
../configure --host=x86_64-w64-mingw32 --prefix=$(pwd)/prefix
fi
make -j$(nproc)
make install
x86_64-w64-mingw32-strip -x $(pwd)/prefix/bin/*.dll $(pwd)/prefix/lib/*.a
mkdir -p ../ztm-libs/win64/
cp $(pwd)/prefix/bin/*.dll $(pwd)/prefix/lib/*.a ../ztm-libs/win64/
cd ..
fi
if [ -n "$BUILD_WIN64_SDL264" ] && [ "$BUILD_WIN64_SDL264" -eq 1 ]; then
mkdir -p ztm-mingw-x86_64-sdl264
cd ztm-mingw-x86_64-sdl264
if [ ! -f ./config.log ]; then
../configure --host=x86_64-w64-mingw32 --prefix=$(pwd)/prefix
sed -i -s "s/libSDL2/libSDL264/g" Makefile
fi
make -j$(nproc)
make install
x86_64-w64-mingw32-strip -x $(pwd)/prefix/bin/*.dll $(pwd)/prefix/lib/*.a
mkdir -p ../ztm-libs/win64/
cp $(pwd)/prefix/bin/*.dll $(pwd)/prefix/lib/*.a ../ztm-libs/win64/
cd ..
fi
if [ -n "$BUILD_MACOS_UB1" ] && [ "$BUILD_MACOS_UB1" -eq 1 ]; then
# This needs to be added to PATH for macOS configure and make.
export PATH="${OSXCROSS_TARGET_UB1}/bin/:$PATH"
# Build macOS x86 (10.13 SDK)
mkdir -p ztm-macos-x86-darwin17
cd ztm-macos-x86-darwin17
if [ ! -f ./config.log ]; then
../configure --prefix=$(pwd)/prefix --host=i386-apple-darwin17 CC=i386-apple-darwin17-cc
fi
make -j$(nproc)
make install
i386-apple-darwin17-install_name_tool -id "@executable_path/libSDL2-2.0.0.dylib" $(pwd)/prefix/lib/libSDL2-2.0.0.dylib
i386-apple-darwin17-strip -x $(pwd)/prefix/lib/libSDL2-2.0.0.dylib $(pwd)/prefix/lib/*.a
#mkdir -p ../ztm-libs/x86/
#cp $(pwd)/prefix/lib/*.dylib $(pwd)/prefix/lib/*.a ../ztm-libs/x86/
cd ..
# Build macOS x86_64 (10.13 SDK)
mkdir -p ztm-macos-x86_64-darwin17
cd ztm-macos-x86_64-darwin17
if [ ! -f ./config.log ]; then
../configure --prefix=$(pwd)/prefix --host=x86_64-apple-darwin17 CC=x86_64-apple-darwin17-cc
fi
make -j$(nproc)
make install
x86_64-apple-darwin17-install_name_tool -id "@executable_path/libSDL2-2.0.0.dylib" $(pwd)/prefix/lib/libSDL2-2.0.0.dylib
x86_64-apple-darwin17-strip -x $(pwd)/prefix/lib/libSDL2-2.0.0.dylib $(pwd)/prefix/lib/*.a
#mkdir -p ../ztm-libs/x86_64/
#cp $(pwd)/prefix/lib/*.dylib $(pwd)/prefix/lib/*.a ../ztm-libs/x86_64/
cd ..
# Merge x86, x86_64, and SDL 2.0.1 PPC build from ioquake3.
mkdir -p ./ztm-libs/macos-ub1
x86_64-apple-darwin17-lipo -create -o ./ztm-libs/macos-ub1/libSDL2-2.0.0.dylib ./ztm-libs-ioq3-prebuilt-ppc/libSDL2-2.0.0.dylib ./ztm-macos-{x86,x86_64}-darwin17/prefix/lib/libSDL2-2.0.0.dylib
x86_64-apple-darwin17-lipo -create -o ./ztm-libs/macos-ub1/libSDL2main.a ./ztm-libs-ioq3-prebuilt-ppc/libSDL2main.a ./ztm-macos-{x86,x86_64}-darwin17/prefix/lib/libSDL2main.a
# Ad-hoc sign
$RCODESIGN sign ./ztm-libs/macos-ub1/libSDL2-2.0.0.dylib
fi
if [ -n "$BUILD_MACOS_UB2" ] && [ "$BUILD_MACOS_UB2" -eq 1 ]; then
# This needs to be added to PATH for macOS configure and make.
export PATH="${OSXCROSS_TARGET_UB2}/bin/:$PATH"
# Build macOS x86_64 (11.3 SDK)
mkdir -p ztm-macos-x86_64
cd ztm-macos-x86_64
if [ ! -f ./config.log ]; then
../configure --prefix=$(pwd)/prefix --host=x86_64-apple-darwin20.4 CC=x86_64-apple-darwin20.4-cc
fi
make -j$(nproc)
make install
x86_64-apple-darwin20.4-install_name_tool -id "@executable_path/libSDL2-2.0.0.dylib" $(pwd)/prefix/lib/libSDL2-2.0.0.dylib
x86_64-apple-darwin20.4-strip -x $(pwd)/prefix/lib/libSDL2-2.0.0.dylib $(pwd)/prefix/lib/*.a
#mkdir -p ../ztm-libs/x86_64/
#cp $(pwd)/prefix/lib/*.dylib $(pwd)/prefix/lib/*.a ../ztm-libs/x86_64/
cd ..
# Build macOS Apple Silicon (11.3 SDK)
mkdir -p ztm-macos-aarch64
cd ztm-macos-aarch64
if [ ! -f ./config.log ]; then
../configure --prefix=$(pwd)/prefix --host=aarch64-apple-darwin20.4 CC=aarch64-apple-darwin20.4-cc
fi
make -j$(nproc)
make install
aarch64-apple-darwin20.4-install_name_tool -id "@executable_path/libSDL2-2.0.0.dylib" $(pwd)/prefix/lib/libSDL2-2.0.0.dylib
aarch64-apple-darwin20.4-strip -x $(pwd)/prefix/lib/libSDL2-2.0.0.dylib $(pwd)/prefix/lib/*.a
#mkdir -p ../ztm-libs/aarch64/
#cp $(pwd)/prefix/lib/*.dylib $(pwd)/prefix/lib/*.a ../ztm-libs/aarch64/
cd ..
# Merge x86_64 and aarch64 libraries.
mkdir -p ./ztm-libs/macos-ub2
aarch64-apple-darwin20.4-lipo -create -o ./ztm-libs/macos-ub2/libSDL2-2.0.0.dylib ./ztm-macos-{x86_64,aarch64}/prefix/lib/libSDL2-2.0.0.dylib
aarch64-apple-darwin20.4-lipo -create -o ./ztm-libs/macos-ub2/libSDL2main.a ./ztm-macos-{x86_64,aarch64}/prefix/lib/libSDL2main.a
# Ad-hoc sign
$RCODESIGN sign ./ztm-libs/macos-ub2/libSDL2-2.0.0.dylib
fi
REM Windows cmd.exe batch script to build SDL 2.
REM Install Visual Studio Community 2022 with the following:
REM Workloads: "Desktop development with C++"
REM Individual Commponents: "C++ Windows XP Support for VS 2017 (v141) tools" aka v141_xp
REM If VS is already installed, you can add them in Visual Studio using menubar, Tools, Get Tools and Features...
REM This script can be placed anywhere. The libries are copied to .\ztm-libs\msvc\{win32,win64}.
set MSBUILD=C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe
set SDLDIR=C:\Users\%USERNAME%\Desktop\SDL
set OUTPUT=.\ztm-libs\msvc
REM Build 32-bit
"%MSBUILD%" /t:Rebuild /p:Platform=Win32 /p:Configuration=Release /p:PlatformToolSet=v141_xp "%SDLDIR%\VisualC\SDL\SDL.vcxproj"
"%MSBUILD%" /t:Rebuild /p:Platform=Win32 /p:Configuration=Release /p:PlatformToolSet=v141_xp "%SDLDIR%\VisualC\SDLmain\SDLmain.vcxproj"
mkdir "%OUTPUT%\win32\"
copy "%SDLDIR%\VisualC\SDL\Win32\Release\SDL2.dll" "%OUTPUT%\win32\"
copy "%SDLDIR%\VisualC\SDL\Win32\Release\SDL2.lib" "%OUTPUT%\win32\"
copy "%SDLDIR%\VisualC\SDLmain\Win32\Release\SDL2main.lib" "%OUTPUT%\win32\"
REM Build 64-bit (SDL2.dll)
"%MSBUILD%" /t:Rebuild /p:Platform=x64 /p:Configuration=Release /p:PlatformToolSet=v141_xp "%SDLDIR%\VisualC\SDL\SDL.vcxproj"
"%MSBUILD%" /t:Rebuild /p:Platform=x64 /p:Configuration=Release /p:PlatformToolSet=v141_xp "%SDLDIR%\VisualC\SDLmain\SDLmain.vcxproj"
mkdir "%OUTPUT%\win64\"
copy /b "%SDLDIR%\VisualC\SDL\x64\Release\SDL2.dll" "%OUTPUT%\win64\"
copy /b "%SDLDIR%\VisualC\SDL\x64\Release\SDL2.lib" "%OUTPUT%\win64\"
copy /b "%SDLDIR%\VisualC\SDLmain\x64\Release\SDL2main.lib" "%OUTPUT%\win64\"
REM Build 64-bit, renamed to SDL264.dll to match ioquake3 naming convention
"%MSBUILD%" /t:Rebuild /p:Platform=x64 /p:Configuration=Release /p:PlatformToolSet=v141_xp /p:TargetName=SDL264 "%SDLDIR%\VisualC\SDL\SDL.vcxproj"
"%MSBUILD%" /t:Rebuild /p:Platform=x64 /p:Configuration=Release /p:PlatformToolSet=v141_xp /p:TargetName=SDL264main "%SDLDIR%\VisualC\SDLmain\SDLmain.vcxproj"
mkdir "%OUTPUT%\win64\"
copy /b "%SDLDIR%\VisualC\SDL\x64\Release\SDL264.dll" "%OUTPUT%\win64\"
copy /b "%SDLDIR%\VisualC\SDL\x64\Release\SDL264.lib" "%OUTPUT%\win64\"
copy /b "%SDLDIR%\VisualC\SDLmain\x64\Release\SDL264main.lib" "%OUTPUT%\win64\"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment