#!/bin/sh
MY_PATH="$(dirname "$0")"
APP_NAME="Rick Dangerous"
APP_VERSION="1.4"
APP_COPYRIGHT="© 2016 Pierre-Marie Baty
© 2002 BigOrno
© 1989 CORE Design
Artwork by Simon Phipps"
BUNDLE_ID="com.pmbaty.rick.rickdangerous"
ICON_FILE="icon.png"
# compiler and linker flags to use
CPPFLAGS="-Isrc -Ilib/SDL2/include"
LDFLAGS=""
cd "${MY_PATH}" || exit 1
# cleanup
rm -rf .build
if [ "$(uname)" = "Darwin" ]; then
# macOS
rm -rf "${APP_NAME}.app"
ARCHS="x86_64 arm64"
CPPFLAGS="-mmacosx-version-min=10.11 ${CPPFLAGS}"
LDFLAGS="-mmacosx-version-min=10.11 -framework Cocoa -framework OpenGL -Flib/SDL2/macOS -framework SDL2 -rpath @loader_path/../Frameworks ${LDFLAGS}"
else
# Linux/BSD/whatever
rm -rf "${APP_NAME}.$(uname)-$(uname -m)"
ARCHS="$(uname -m)"
CPPFLAGS="${CPPFLAGS}"
LDFLAGS="-L/usr/local/lib -lSDL2 -lpthread -lm ${LDFLAGS}"
test "$(uname)" = "FreeBSD" && LDFLAGS="-linotify -lusbhid ${LDFLAGS}"
fi
# read the source files list from the Visual Studio project file if it exists, else list all the .c files from "lib" and "src"
if [ -f "${APP_NAME}.vcxproj" ]; then
SRCLIST="$(grep "<ClCompile " "${APP_NAME}.vcxproj"|awk -F\" '{print $2}'|tr '\\' '/')"
else
SRCLIST="$(find lib -name '*.c') $(find src -name '*.c')"
fi
# for each specified architecture...
THIN_OUTPUTS=
for ARCH in ${ARCHS}; do
rm -rf ".build/${ARCH}"
mkdir -p ".build/${ARCH}" || exit 1
OPTIONAL_ARCH_FLAG="$(test "${ARCH}" = "${ARCHS}" || echo "-arch ${ARCH}")"
# compile source files
echo "Compiling for ${ARCH}..."
for SRCFILE in ${SRCLIST}; do
echo "${SRCFILE}"
cc -o ".build/${ARCH}/$(basename "${SRCFILE}").o" -c "${SRCFILE}" ${OPTIONAL_ARCH_FLAG} ${CPPFLAGS} || exit 1
done
# link them together
echo "Linking for ${ARCH}..."
cc -o ".build/${ARCH}/a.out" ".build/${ARCH}/"*".o" ${OPTIONAL_ARCH_FLAG} ${LDFLAGS} || exit 1
THIN_OUTPUTS="${THIN_OUTPUTS} .build/${ARCH}/a.out"
done
if [ "$(uname)" = "Darwin" ]; then
# create the macOS app layout
echo "Creating macOS app layout..."
mkdir "${APP_NAME}.app" || exit 1
mkdir "${APP_NAME}.app/Contents" || exit 1
mkdir "${APP_NAME}.app/Contents/MacOS" || exit 1
mkdir "${APP_NAME}.app/Contents/Frameworks" || exit 1
mkdir "${APP_NAME}.app/Contents/Resources" || exit 1
# generate and deploy fat Mach-O executable
echo "Generating fat executable..."
lipo ${THIN_OUTPUTS} -create -output "${APP_NAME}.app/Contents/MacOS/${APP_NAME}" || exit 1
# deploy the SDL2 framework
echo "Deploying SDL2 framework..."
mkdir "${APP_NAME}.app/Contents/Frameworks/SDL2.framework" || exit 1
mkdir "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Versions" || exit 1
mkdir "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Versions/A" || exit 1
cp lib/SDL2/macOS/SDL2.framework/SDL2 "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Versions/A/SDL2" || exit 1
ln -s Versions/A/SDL2 "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/SDL2" || exit 1
cp -LR lib/SDL2/macOS/SDL2.framework/Resources "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Versions/A" || exit 1
ln -s Versions/A/Resources "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Resources" || exit 1
# deploy the game data
echo "Deploying game data..."
cp -LR sounds "${APP_NAME}.app/Contents/Resources" || exit 1
# generate and deploy the app icon
echo "Generating app icon..."
mkdir ".build/${APP_NAME}.iconset" || exit 1
sips -z 16 16 "${ICON_FILE}" --out ".build/${APP_NAME}.iconset/icon_16x16.png" > /dev/null
sips -z 32 32 "${ICON_FILE}" --out ".build/${APP_NAME}.iconset/icon_16x16@2x.png" > /dev/null
sips -z 32 32 "${ICON_FILE}" --out ".build/${APP_NAME}.iconset/icon_32x32.png" > /dev/null
sips -z 64 64 "${ICON_FILE}" --out ".build/${APP_NAME}.iconset/icon_32x32@2x.png" > /dev/null
sips -z 128 128 "${ICON_FILE}" --out ".build/${APP_NAME}.iconset/icon_128x128.png" > /dev/null
sips -z 256 256 "${ICON_FILE}" --out ".build/${APP_NAME}.iconset/icon_128x128@2x.png" > /dev/null
sips -z 256 256 "${ICON_FILE}" --out ".build/${APP_NAME}.iconset/icon_256x256.png" > /dev/null
sips -z 512 512 "${ICON_FILE}" --out ".build/${APP_NAME}.iconset/icon_256x256@2x.png" > /dev/null
sips -z 512 512 "${ICON_FILE}" --out ".build/${APP_NAME}.iconset/icon_512x512.png" > /dev/null
sips -z 1024 1024 "${ICON_FILE}" --out ".build/${APP_NAME}.iconset/icon_512x512@2x.png" > /dev/null
iconutil -c icns ".build/${APP_NAME}.iconset" -o "${APP_NAME}.app/Contents/Resources/AppIcon.icns" || exit 1
# generate the PkgInfo file
echo -n "APPL???" > "${APP_NAME}.app/Contents/PkgInfo" || exit 1
# generate the Info.plist
(
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
echo "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
echo "<plist version=\"1.0\">"
echo "<dict>"
echo " <key>CFBundleDevelopmentRegion</key>"
echo " <string>en</string>"
echo " <key>CFBundleExecutable</key>"
echo " <string>${APP_NAME}</string>"
echo " <key>CFBundleIconFile</key>"
echo " <string>AppIcon.icns</string>"
echo " <key>CFBundleIdentifier</key>"
echo " <string>${BUNDLE_ID}</string>"
echo " <key>CFBundleInfoDictionaryVersion</key>"
echo " <string>6.0</string>"
echo " <key>CFBundleName</key>"
echo " <string>${APP_NAME}</string>"
echo " <key>CFBundleVersion</key>"
echo " <string>${APP_VERSION}</string>"
echo " <key>NSHumanReadableCopyright</key>"
echo " <string>${APP_COPYRIGHT}</string>"
echo " <key>CFBundlePackageType</key>"
echo " <string>APPL</string>"
echo " <key>CFBundleSignature</key>"
echo " <string>????</string>"
echo "</dict>"
echo "</plist>"
) > "${APP_NAME}.app/Contents/Info.plist" || exit 1
echo "Output app bundle: ${APP_NAME}.app"
else
# Linux/BSD/whatever: simply retrieve the compiled executable
cp ".build/${ARCH}/a.out" "${APP_NAME}.$(uname)-$(uname -m)"
echo "Output executable: ${APP_NAME}.$(uname)-$(uname -m)"
fi
# finished
echo "${APP_NAME} was built successfully"
exit 0
rm -rf "${APP_NAME}.app"
# create app bundle
echo "Creating macOS app layout..."
mkdir "${APP_NAME}.app" || exit 1
mkdir "${APP_NAME}.app/Contents" || exit 1
mkdir "${APP_NAME}.app/Contents/MacOS" || exit 1
mkdir "${APP_NAME}.app/Contents/Frameworks" || exit 1
mkdir "${APP_NAME}.app/Contents/Resources" || exit 1
# target El Capitan
for ARCH in x86_64 arm64; do
cc -mmacosx-version-min=10.11 -arch "${ARCH}" *.c osx-sdlmain.m -I3rdparty/SDL2/include -framework Foundation -framework Cocoa -F3rdparty/SDL2/macOS -framework SDL2 -rpath "@loader_path/../Frameworks" -o "${APP_NAME}.mac-${ARCH}"
if [ ! "_$?" = "_0" ]; then
rm -rf "${APP_NAME}.app"
exit 1
fi
done
lipo "${APP_NAME}.mac-x86_64" "${APP_NAME}.mac-arm64" -create -output "${APP_NAME}.app/Contents/MacOS/${APP_NAME}"
if [ ! "_$?" = "_0" ]; then
rm "${APP_NAME}.mac-x86_64" "${APP_NAME}.mac-arm64"
rm -rf "${APP_NAME}.app"
exit 1
fi
rm "${APP_NAME}.mac-x86_64" "${APP_NAME}.mac-arm64"
# deploy the SDL2 framework
mkdir "${APP_NAME}.app/Contents/Frameworks/SDL2.framework" || exit 1
mkdir "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Versions" || exit 1
mkdir "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Versions/A" || exit 1
cp 3rdparty/SDL2/macOS/SDL2.framework/SDL2 "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Versions/A/SDL2" || exit 1
ln -s Versions/A/SDL2 "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/SDL2" || exit 1
cp -LR 3rdparty/SDL2/macOS/SDL2.framework/Resources "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Versions/A" || exit 1
ln -s Versions/A/Resources "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Resources" || exit 1
# deploy icon
mkdir "__${APP_NAME}.iconset"
sips -z 16 16 "${ICON_FILE}" --out "__${APP_NAME}.iconset/icon_16x16.png" > /dev/null
sips -z 32 32 "${ICON_FILE}" --out "__${APP_NAME}.iconset/icon_16x16@2x.png" > /dev/null
sips -z 32 32 "${ICON_FILE}" --out "__${APP_NAME}.iconset/icon_32x32.png" > /dev/null
sips -z 64 64 "${ICON_FILE}" --out "__${APP_NAME}.iconset/icon_32x32@2x.png" > /dev/null
sips -z 128 128 "${ICON_FILE}" --out "__${APP_NAME}.iconset/icon_128x128.png" > /dev/null
sips -z 256 256 "${ICON_FILE}" --out "__${APP_NAME}.iconset/icon_128x128@2x.png" > /dev/null
sips -z 256 256 "${ICON_FILE}" --out "__${APP_NAME}.iconset/icon_256x256.png" > /dev/null
sips -z 512 512 "${ICON_FILE}" --out "__${APP_NAME}.iconset/icon_256x256@2x.png" > /dev/null
sips -z 512 512 "${ICON_FILE}" --out "__${APP_NAME}.iconset/icon_512x512.png" > /dev/null
sips -z 1024 1024 "${ICON_FILE}" --out "__${APP_NAME}.iconset/icon_512x512@2x.png" > /dev/null
iconutil -c icns "__${APP_NAME}.iconset"
if [ ! "_$?" = "_0" ]; then
rm -rf "__${APP_NAME}.iconset"
rm -rf "${APP_NAME}.app"
exit 1
fi
rm -rf "__${APP_NAME}.iconset"
mv "__${APP_NAME}.icns" "${APP_NAME}.app/Contents/Resources/${APP_NAME}.icns"
# deploy resources
cp -r "../sounds" "${APP_NAME}.app/Contents/Resources"
# create PkgInfo file
printf "%s" "APPL???" > "${APP_NAME}.app/Contents/PkgInfo"
# create Info.plist
(
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
echo "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
echo "<plist version=\"1.0\">"
echo "<dict>"
echo " <key>CFBundleDevelopmentRegion</key>"
echo " <string>en</string>"
echo " <key>CFBundleExecutable</key>"
echo " <string>${APP_NAME}</string>"
echo " <key>CFBundleIconFile</key>"
echo " <string>${APP_NAME}.icns</string>"
echo " <key>CFBundleIdentifier</key>"
echo " <string>${BUNDLE_ID}</string>"
echo " <key>CFBundleInfoDictionaryVersion</key>"
echo " <string>6.0</string>"
echo " <key>CFBundleName</key>"
echo " <string>${APP_NAME}</string>"
echo " <key>CFBundleVersion</key>"
echo " <string>${APP_VERSION}</string>"
echo " <key>NSHumanReadableCopyright</key>"
echo " <string>${APP_COPYRIGHT}</string>"
echo " <key>CFBundlePackageType</key>"
echo " <string>APPL</string>"
echo " <key>CFBundleSignature</key>"
echo " <string>????</string>"
echo "</dict>"
echo "</plist>"
) > "${APP_NAME}.app/Contents/Info.plist"
# finished
echo "${APP_NAME} was built successfully"
exit 0