Skip to content

Instantly share code, notes, and snippets.

@d235j
Last active December 23, 2022 22:48
Show Gist options
  • Save d235j/af271cd135152cd2a3f7cd6748fcf46e to your computer and use it in GitHub Desktop.
Save d235j/af271cd135152cd2a3f7cd6748fcf46e to your computer and use it in GitHub Desktop.
#!/bin/sh
# Released under CC0
DF_OSX=/tmp/df_osx # this is the extracted directory
# download SDL2.framework and place it in df_osx/libs
# DO THIS MANUALLY!!!
# verify that the above step has been done
if [ ! -d "${DF_OSX}/libs/SDL2.framework" ]; then
echo "Please download SDL2 and copy SDL2.framework to the libs/ directory within the extracted Dwarf Fortress folder."
echo "This should be ${DF_OSX}/libs/ ."
exit 1
fi
# copy the headers to /tmp as follows as this will be needed to compile sdl12-compat
mkdir /tmp/sdl2headers
cp -a ${DF_OSX}/libs/SDL2.framework/Headers/ /tmp/sdl2headers/SDL2/
# compile sdl12-compat as follows, with a patch to look for SDL2 in RPATH:
cd /tmp
git clone https://github.com/libsdl-org/sdl12-compat.git
cd sdl12-compat
patch -p1 <<'EOF'
diff --git a/src/SDL12_compat.c b/src/SDL12_compat.c
index 8cbcf73..6d5bde5 100644
--- a/src/SDL12_compat.c
+++ b/src/SDL12_compat.c
@@ -1104,6 +1104,9 @@ static char loaderror[256];
static SDL_bool LoadSDL20Library(void) {
/* I don't know if this is the _right_ order to try, but this seems reasonable */
static const char * const dylib_locations[] = {
+ "@rpath/" SDL20_LIBNAME, /* @rpath is defined in the binary as potential locations for libraries */
+ "@rpath/" SDL20_LIBNAME2,
+ "@rpath/" SDL20_FRAMEWORK,
"@loader_path/" SDL20_LIBNAME, /* MyApp.app/Contents/MacOS/libSDL2-2.0.0.dylib */
"@loader_path/" SDL20_LIBNAME2, /* MyApp.app/Contents/MacOS/libSDL2-2.0.dylib */
"@loader_path/../Frameworks/" SDL20_FRAMEWORK, /* MyApp.app/Contents/Frameworks/SDL2.framework */
EOF
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_OSX_ARCHITECTURES="x86_64" -DCMAKE_OSX_DEPLOYMENT_TARGET=10.7 -DSDL2_INCLUDE_DIR="/tmp/sdl2headers;/tmp/sdl2headers/SDL2"
make -j9
# delete SDL v1 from the DF libs directory
rm -rf ${DF_OSX}/libs/SDL.framework
# copy the built lib to the DF libs directory
cp libSDL-1.2.0.dylib ${DF_OSX}/libs/
# fix the library paths to point to the correct places
cd ${DF_OSX}
install_name_tool -add_rpath @executable_path/libs dwarfort.exe
install_name_tool -change @rpath/SDL.framework/Versions/A/SDL @rpath/libSDL-1.2.0.dylib dwarfort.exe
# also fix libstdc++ path
install_name_tool -change /usr/local/lib/x86_64/libstdc++.6.dylib @rpath/libstdc++.6.dylib dwarfort.exe
# fix paths for SDL_image and SDL_ttf
install_name_tool -change @rpath/SDL.framework/Versions/A/SDL @rpath/libSDL-1.2.0.dylib ${DF_OSX}/libs/SDL_image.framework/Versions/A/SDL_image
install_name_tool -change @rpath/SDL.framework/Versions/A/SDL @rpath/libSDL-1.2.0.dylib ${DF_OSX}/libs/SDL_ttf.framework/Versions/A/SDL_ttf
# thin libfmodex as install_name_tool cannot handle ppc library paths, then fix it
lipo libs/libfmodex.dylib -thin x86_64 -o /tmp/libfmodex.dylib
mv /tmp/libfmodex.dylib libs/
install_name_tool -change /usr/lib/libstdc++.6.dylib @rpath/libstdc++.6.dylib libs/libfmodex.dylib
exit 0
# Cleanup:
rm -rf /tmp/sdl2headers
rm -rf /tmp/sdl12-compat
rm -f /tmp/libfmodex.dylib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment