Rev 7 | Blame | Last modification | View Log | Download | RSS feed
#!/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"
cd "${MY_PATH}" || exit 1
# cleanup
rm -rf .build
rm -rf "${APP_NAME}.app"
# compiler and linker flags to use
CPPFLAGS="-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 -Ilib"
# 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
echo "${SHADER}"
SYMBOL_NAME="$(echo "${SHADER}"|tr '[:lower:]' '[:upper:]'|sed 's/[\.\/]/_/g')"
(
echo "// Auto generated file."
echo "static const char ${SYMBOL_NAME}[] ="
echo "{"
cat "${SHADER}"|hexdump -e '16/1 "%02x" "\n"' -v|sed 's/[0-9a-fA-F][0-9a-fA-F]/0x&, /g' || exit 1
echo "};"
) > "${SHADER}.h"
done
cd ../..
# target x86_64 and ARM64
for ARCH in x86_64 arm64; do
echo "Compiling for ${ARCH}..."
rm -rf ".build/${ARCH}"
mkdir -p ".build/${ARCH}" || exit 1
# compile source files
for SRCFILE in ${SRCLIST}; do
echo "${SRCFILE}"
cc -o ".build/${ARCH}/$(basename "${SRCFILE}").o" -mmacosx-version-min=10.11 -arch "${ARCH}" ${CPPFLAGS} -c "${SRCFILE}" || exit 1
done
# link them together
echo "Linking for ${ARCH}..."
cc -o ".build/${ARCH}/${APP_NAME}" ".build/${ARCH}/"*".o" -mmacosx-version-min=10.11 -arch "${ARCH}" -framework Cocoa -framework OpenGL -F. -framework SDL2 -rpath @loader_path/../Frameworks || exit 1
done
# create the app layout
mkdir "${APP_NAME}.app"
mkdir "${APP_NAME}.app/Contents"
mkdir "${APP_NAME}.app/Contents/MacOS"
mkdir "${APP_NAME}.app/Contents/Frameworks"
mkdir "${APP_NAME}.app/Contents/Resources"
# generate and deploy fat Mach-O executable
echo "Generating fat executable..."
lipo ".build/x86_64/${APP_NAME}" ".build/arm64/${APP_NAME}" -create -output "${APP_NAME}.app/Contents/MacOS/${APP_NAME}" || exit 1
# deploy the SDL2 framework
echo "Deploying SDL2 framework..."
cp -pPR SDL2.framework "${APP_NAME}.app/Contents/Frameworks" || exit 1
rm -rf "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Versions/A/_CodeSignature"
rm -rf "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Versions/A/Headers"
rm -rf "${APP_NAME}.app/Contents/Frameworks/SDL2.framework/Headers"
# deploy the game data
echo "Deploying game data..."
cp -pPR DATA "${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
# finished
echo "Carmageddon was built successfully"
exit 0