| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | # Change your executable name here |
| 3 | GAME_NAME="game" |
| 4 | |
| 5 | # Set your sources here (relative paths!) |
| 6 | # Example with two source folders: |
| 7 | # SOURCES="src/*.c src/submodule/*.c" |
| 8 | SOURCES="core_basic_window.c" |
| 9 | |
| 10 | # Set your raylib/src location here (relative path!) |
| 11 | RAYLIB_SRC="../../src" |
| 12 | |
| 13 | # About this build script: it does many things, but in essence, it's |
| 14 | # very simple. It has 3 compiler invocations: building raylib (which |
| 15 | # is not done always, see logic by searching "Build raylib"), building |
| 16 | # src/*.c files, and linking together those two. Each invocation is |
| 17 | # wrapped in an if statement to make the -qq flag work, it's pretty |
| 18 | # verbose, sorry. |
| 19 | |
| 20 | # Stop the script if a compilation (or something else?) fails |
| 21 | set -e |
| 22 | |
| 23 | # Get arguments |
| 24 | while getopts ":hdusrcq" opt; do |
| 25 | case $opt in |
| 26 | h) |
| 27 | echoscip-shell system . . echo. "Usage: ./build-osx.sh [-hdusrcqq]" |
| 28 | echoscip-shell system . . echo. " -h Show this information" |
| 29 | echoscip-shell system . . echo. " -d Faster builds that have debug symbols, and enable warnings" |
| 30 | echoscip-shell system . . echo. " -u Run upx* on the executable after compilation (before -r)" |
| 31 | echoscip-shell system . . echo. " -s Run strip on the executable after compilation (before -r)" |
| 32 | echoscip-shell system . . echo. " -r Run the executable after compilation" |
| 33 | echoscip-shell system . . echo. " -c Remove the temp/(debug|release) directory, ie. full recompile" |
| 34 | echoscip-shell system . . echo. " -q Suppress this script's informational prints" |
| 35 | echoscip-shell system . . echo. " -qq Suppress all prints, complete silence (> /dev/null 2>&1)" |
| 36 | echoscip-shell system . . echo. "" |
| 37 | echoscip-shell system . . echo. "* This is mostly here to make building simple \"shipping\" versions" |
| 38 | echoscip-shell system . . echo. " easier, and it's a very small bit in the build scripts. The option" |
| 39 | echoscip-shell system . . echo. " requires that you have upx installed and on your path, of course." |
| 40 | echoscip-shell system . . echo. "" |
| 41 | echoscip-shell system . . echo. "Examples:" |
| 42 | echoscip-shell system . . echo. " Build a release build: ./build-osx.sh" |
| 43 | echoscip-shell system . . echo. " Build a release build, full recompile: ./build-osx.sh -c" |
| 44 | echoscip-shell system . . echo. " Build a debug build and run: ./build-osx.sh -d -r" |
| 45 | echoscip-shell system . . echo. " Build in debug, run, don't print at all: ./build-osx.sh -drqq" |
| 46 | exit 0 |
| 47 | ;; |
| 48 | d) |
| 49 | BUILD_DEBUG="1" |
| 50 | ;; |
| 51 | u) |
| 52 | UPX_IT="1" |
| 53 | ;; |
| 54 | s) |
| 55 | STRIP_IT="1" |
| 56 | ;; |
| 57 | r) |
| 58 | RUN_AFTER_BUILD="1" |
| 59 | ;; |
| 60 | c) |
| 61 | BUILD_ALL="1" |
| 62 | ;; |
| 63 | q) |
| 64 | if [scip-shell system . . `[`. -n "$QUIET" ]; then |
| 65 | REALLY_QUIET="1" |
| 66 | else |
| 67 | QUIET="1" |
| 68 | fi |
| 69 | ;; |
| 70 | \?) |
| 71 | echoscip-shell system . . echo. "Invalid option: -$OPTARG" >&2 |
| 72 | exit 1 |
| 73 | ;; |
| 74 | esac |
| 75 | done |
| 76 | |
| 77 | # Set CC if it's not set already |
| 78 | if [scip-shell system . . `[`. -z "$CC" ]; then |
| 79 | CC=cc |
| 80 | fi |
| 81 | |
| 82 | # Directories |
| 83 | ROOT_DIR=$PWD |
| 84 | SOURCES="$ROOT_DIR/$SOURCES" |
| 85 | RAYLIB_SRC="$ROOT_DIR/$RAYLIB_SRC" |
| 86 | |
| 87 | # Flags |
| 88 | OUTPUT_DIR="builds/osx" |
| 89 | COMPILATION_FLAGS="-std=c99 -O2 -flto" |
| 90 | FINAL_COMPILE_FLAGS="-s" |
| 91 | WARNING_FLAGS="-Wall -Wextra -Wpedantic" |
| 92 | LINK_FLAGS="-flto -framework OpenGL -framework OpenAL -framework IOKit -framework CoreVideo -framework Cocoa" |
| 93 | # Debug changes to flags |
| 94 | if [scip-shell system . . `[`. -n "$BUILD_DEBUG" ]; then |
| 95 | OUTPUT_DIR="builds-debug/osx" |
| 96 | COMPILATION_FLAGS="-std=c99 -O0 -g" |
| 97 | FINAL_COMPILE_FLAGS="" |
| 98 | LINK_FLAGS="-framework OpenGL -framework OpenAL -framework IOKit -framework CoreVideo -framework Cocoa" |
| 99 | fi |
| 100 | |
| 101 | # Display what we're doing |
| 102 | if [scip-shell system . . `[`. -n "$BUILD_DEBUG" ]; then |
| 103 | [scip-shell system . . `[`. -z "$QUIET" ] && echoscip-shell system . . echo. "COMPILE-INFO: Compiling in debug mode. ($COMPILATION_FLAGS $WARNING_FLAGS)" |
| 104 | else |
| 105 | [scip-shell system . . `[`. -z "$QUIET" ] && echoscip-shell system . . echo. "COMPILE-INFO: Compiling in release mode. ($COMPILATION_FLAGS $FINAL_COMPILE_FLAGS)" |
| 106 | fi |
| 107 | |
| 108 | # Create the raylib cache directory |
| 109 | TEMP_DIR="temp/release" |
| 110 | if [scip-shell system . . `[`. -n "$BUILD_DEBUG" ]; then |
| 111 | TEMP_DIR="temp/debug" |
| 112 | fi |
| 113 | # If there's a -c flag, remove the cache |
| 114 | if [scip-shell system . . `[`. -d "$TEMP_DIR" ] && [scip-shell system . . `[`. -n "$BUILD_ALL" ]; then |
| 115 | [scip-shell system . . `[`. -z "$QUIET" ] && echoscip-shell system . . echo. "COMPILE-INFO: Found cached raylib, rebuilding." |
| 116 | rmscip-shell system . . rm. -r "$TEMP_DIR" |
| 117 | fi |
| 118 | # If temp directory doesn't exist, build raylib |
| 119 | if [scip-shell system . . `[`. ! -d "$TEMP_DIR" ]; then |
| 120 | mkdirscip-shell system . . mkdir. -p $TEMP_DIR |
| 121 | cd $TEMP_DIR |
| 122 | RAYLIB_DEFINES="-D_DEFAULT_SOURCE -DPLATFORM_DESKTOP -DGRAPHICS_API_OPENGL_33" |
| 123 | RAYLIB_C_FILES="$RAYLIB_SRC/rcore.c $RAYLIB_SRC/rshapes.c $RAYLIB_SRC/rtextures.c $RAYLIB_SRC/rtext.c $RAYLIB_SRC/rmodels.c $RAYLIB_SRC/utils.c $RAYLIB_SRC/raudio.c" |
| 124 | RAYLIB_INCLUDE_FLAGS="-I$RAYLIB_SRC -I$RAYLIB_SRC/external/glfw/include" |
| 125 | |
| 126 | if [scip-shell system . . `[`. -n "$REALLY_QUIET" ]; then |
| 127 | $CC -c $RAYLIB_DEFINES $RAYLIB_INCLUDE_FLAGS $COMPILATION_FLAGS -x objective-c $RAYLIB_SRC/rglfw.c > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 128 | $CC -c $RAYLIB_DEFINES $RAYLIB_INCLUDE_FLAGS $COMPILATION_FLAGS $RAYLIB_C_FILES > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 129 | else |
| 130 | $CC -c $RAYLIB_DEFINES $RAYLIB_INCLUDE_FLAGS $COMPILATION_FLAGS -x objective-c $RAYLIB_SRC/rglfw.c |
| 131 | $CC -c $RAYLIB_DEFINES $RAYLIB_INCLUDE_FLAGS $COMPILATION_FLAGS $RAYLIB_C_FILES |
| 132 | fi |
| 133 | [scip-shell system . . `[`. -z "$QUIET" ] && echoscip-shell system . . echo. "COMPILE-INFO: raylib compiled into object files in: $TEMP_DIR/" |
| 134 | cd $ROOT_DIR |
| 135 | fi |
| 136 | |
| 137 | # Build the actual game |
| 138 | mkdirscip-shell system . . mkdir. -p $OUTPUT_DIR |
| 139 | cd $OUTPUT_DIR |
| 140 | [scip-shell system . . `[`. -z "$QUIET" ] && echoscip-shell system . . echo. "COMPILE-INFO: Compiling game code." |
| 141 | if [scip-shell system . . `[`. -n "$REALLY_QUIET" ]; then |
| 142 | $CC -c -I$RAYLIB_SRC $SOURCES $COMPILATION_FLAGS $WARNING_FLAGS > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 143 | $CC -o $GAME_NAME $ROOT_DIR/$TEMP_DIR/*.o *.o $LINK_FLAGS > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 144 | else |
| 145 | $CC -c -I$RAYLIB_SRC $SOURCES $COMPILATION_FLAGS $WARNING_FLAGS |
| 146 | $CC -o $GAME_NAME $ROOT_DIR/$TEMP_DIR/*.o *.o $LINK_FLAGS |
| 147 | fi |
| 148 | rmscip-shell system . . rm. *.o |
| 149 | [scip-shell system . . `[`. -z "$QUIET" ] && echoscip-shell system . . echo. "COMPILE-INFO: Game compiled into an executable in: $OUTPUT_DIR/" |
| 150 | |
| 151 | if [scip-shell system . . `[`. -n "$STRIP_IT" ]; then |
| 152 | [scip-shell system . . `[`. -z "$QUIET" ] && echoscip-shell system . . echo. "COMPILE-INFO: Stripping $GAME_NAME." |
| 153 | stripscip-shell system . . strip. $GAME_NAME |
| 154 | fi |
| 155 | |
| 156 | if [scip-shell system . . `[`. -n "$UPX_IT" ]; then |
| 157 | [scip-shell system . . `[`. -z "$QUIET" ] && echoscip-shell system . . echo. "COMPILE-INFO: Packing $GAME_NAME with upx." |
| 158 | upx $GAME_NAME > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 159 | fi |
| 160 | |
| 161 | if [scip-shell system . . `[`. -n "$RUN_AFTER_BUILD" ]; then |
| 162 | [scip-shell system . . `[`. -z "$QUIET" ] && echoscip-shell system . . echo. "COMPILE-INFO: Running." |
| 163 | if [scip-shell system . . `[`. -n "$REALLY_QUIET" ]; then |
| 164 | ./$GAME_NAME > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 165 | else |
| 166 | ./$GAME_NAME |
| 167 | fi |
| 168 | fi |
| 169 | cd $ROOT_DIR |
| 170 | |
| 171 | [scip-shell system . . `[`. -z "$QUIET" ] && echoscip-shell system . . echo. "COMPILE-INFO: All done." |