#!/bin/sh
MY_PATH="$(dirname "$0")"
APP_NAME="Carmageddon"
APP_VERSION="1.0"
APP_COPYRIGHT="© 2023 Pierre-Marie Baty
© 2023 Dethrace Labs
© 1997 Stainless Games"
BUNDLE_ID="com.pmbaty.carmageddon"
ICON_FILE="icon.png"
CS_IDENTITY="Apple Distribution: Pierre-Marie Baty"
# compiler and linker flags to use
CPPFLAGS="-O3 -DDETHRACE_FIX_BUGS=1 -Isrc/harness/include -Isrc/harness -Isrc/smackw32/include -Isrc/BRSRC13/include -Isrc/BRSRC13 -Isrc/S3/include -Isrc/S3 -Isrc/DETHRACE/common -Isrc/DETHRACE/pd -Isrc/DETHRACE -Ilib/glad/include -Ilib/libsmacker -Ilib/miniaudio/include -Ilib/miniaudio/include/miniaudio -Ilib/SDL2/include/SDL2 -Ilib"
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 -F. -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 -lexecinfo ${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
# generate the shader programs
echo "Compacting GLSL shaders..."
cd src/harness || exit 1
for SHADER in resources/*.glsl; do
SYMBOL_NAME="$(echo "${SHADER}"|tr '[:lower:]' '[:upper:]'|sed 's/[\.\/]/_/g')"
echo "${SHADER} -> ${SYMBOL_NAME}"
(
echo "// Auto generated file."
echo "static const char ${SYMBOL_NAME}[] ="
echo "{"
cat "${SHADER}"|hexdump -e '16/1 "%02x" "\n"' -v|sed 's/[0-9a-f][0-9a-f]/0x&, /g' || exit 1
echo "};"
) > "${SHADER}.h" || exit 1
done
cd ../..
# 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 without the symlinks and change the LC_LOAD_DYLIB in the main executable
# (macOS codesign will refuse to sign the executable if a bundled framework contains symlinks)
echo "Deploying SDL2 framework..."
mkdir "${APP_NAME}.app/Contents/Frameworks/SDL2.framework" || exit 1
cp lib/SDL2/macOS/SDL2.framework/SDL2 "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/SDL2" || exit 1
install_name_tool -change "@rpath/SDL2.framework/Versions/A/SDL2" "@rpath/SDL2.framework/SDL2" "${APP_NAME}.app/Contents/MacOS/${APP_NAME}"
cp -LR lib/SDL2/macOS/SDL2.framework/Resources "${APP_NAME}.app/Contents/Frameworks/SDL2.framework" || exit 1
find "${APP_NAME}.app/Contents/Frameworks/SDL2.framework" -name Thumbs.db -exec rm {} \;
find "${APP_NAME}.app/Contents/Frameworks/SDL2.framework" -name .DS_Store -exec rm {} \;
find "${APP_NAME}.app/Contents/Frameworks/SDL2.framework" -type f -exec chmod -x {} \;
# deploy the game data (excluding the savegames) and drop a notice that the actual game data is instantiated elsewhere
echo "Deploying game data..."
cp -LR DATA "${APP_NAME}.app/Contents/Resources" || exit 1
rm "${APP_NAME}.app/Contents/Resources/DATA/SAVEGAME/"*
echo "This game data is READONLY. The actual game data (read/write) is instantiated out of this template in ~/Library/Application Support/Carmageddon/DATA." > "${APP_NAME}.app/Contents/Resources/README.txt"
# 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
# code signing is mandatory
codesign --force --deep --sign "${CS_IDENTITY}" --timestamp "${APP_NAME}.app"
echo "Output app bundle: ${APP_NAME}.app"
# zip on Mac to preserve executable flags
zip -r "${APP_NAME} (Mac).zip" "${APP_NAME}.app" && echo "A Zip file of the app bundle was created for convenience."
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