You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
5.6 KiB

  1. #!/bin/sh
  2. # Change your executable name here
  3. GAME_NAME="game"
  4. # Set your sources here (relative to the ./builds/linux directory)
  5. # Example with two source folders:
  6. # SOURCES="../../src/*.c ../../src/submodule/*.c"
  7. SOURCES="../../core_basic_window.c"
  8. # Set your raylib/src location here, relative to the ./temp/x directory
  9. RAYLIB_SRC="../../../../src"
  10. # About this build script: it does many things, but in essence, it's
  11. # very simple. It has 3 compiler invocations: building raylib (which
  12. # is not done always, see logic by searching "Build raylib"), building
  13. # src/*.c files, and linking together those two. Each invocation is
  14. # wrapped in an if statement to make the -qq flag work, it's pretty
  15. # verbose, sorry.
  16. # Get arguments
  17. while getopts ":hdurcq" opt; do
  18. case $opt in
  19. h)
  20. echo "Usage: ./linux-build.sh [-hdurcqq]"
  21. echo " -h Show this information"
  22. echo " -d Faster builds that have debug symbols, and enable warnings"
  23. echo " -u Run upx* on the executable after compilation (before -r)"
  24. echo " -r Run the executable after compilation"
  25. echo " -c Remove the temp/(debug|release) directory, ie. full recompile"
  26. echo " -q Suppress this script's informational prints"
  27. echo " -qq Suppress all prints, complete silence (> /dev/null 2>&1)"
  28. echo ""
  29. echo "* This is mostly here to make building simple \"shipping\" versions"
  30. echo " easier, and it's a very small bit in the build scripts. The option"
  31. echo " requires that you have upx installed and on your path, of course."
  32. echo ""
  33. echo "Examples:"
  34. echo " Build a release build: ./linux-build.sh"
  35. echo " Build a release build, full recompile: ./linux-build.sh -c"
  36. echo " Build a debug build and run: ./linux-build.sh -d -r"
  37. echo " Build in debug, run, don't print at all: ./linux-build.sh -drqq"
  38. exit 0
  39. ;;
  40. d)
  41. BUILD_DEBUG="1"
  42. ;;
  43. u)
  44. UPX_IT="1"
  45. ;;
  46. r)
  47. RUN_AFTER_BUILD="1"
  48. ;;
  49. c)
  50. BUILD_ALL="1"
  51. ;;
  52. q)
  53. if [ -n "$QUIET" ]; then
  54. REALLY_QUIET="1"
  55. else
  56. QUIET="1"
  57. fi
  58. ;;
  59. \?)
  60. echo "Invalid option: -$OPTARG" >&2
  61. exit 1
  62. ;;
  63. esac
  64. done
  65. # Set CC if it's not set already
  66. if [ -z "$CC" ]; then
  67. CC=cc
  68. fi
  69. # Flags
  70. OUTPUT_DIR="builds/linux"
  71. COMPILATION_FLAGS="-std=c99 -Os -flto"
  72. if [ "$CC" = "clang" ]; then
  73. # Clang 7.0.1 fails to compile with -Os, possibly the same bug as this:
  74. # https://www.mail-archive.com/llvm-bugs@lists.llvm.org/msg25771.html
  75. COMPILATION_FLAGS="-std=c99 -O2 -flto"
  76. [ -z "$QUIET" ] && echo "COMPILE-WARNING: \$CC is clang, using -O2 instead of -Os."
  77. fi
  78. FINAL_COMPILE_FLAGS="-s"
  79. WARNING_FLAGS="-Wall -Wextra -Wpedantic"
  80. LINK_FLAGS="-lm -ldl -lpthread -lX11 -lxcb -lGL -lGLX -lXext -lGLdispatch -lXau -lXdmcp"
  81. # Debug changes to flags
  82. if [ -n "$BUILD_DEBUG" ]; then
  83. OUTPUT_DIR="builds-debug/linux"
  84. COMPILATION_FLAGS="-std=c99 -O0 -g"
  85. FINAL_COMPILE_FLAGS=""
  86. fi
  87. # Display what we're doing
  88. if [ -n "$BUILD_DEBUG" ]; then
  89. [ -z "$QUIET" ] && echo "COMPILE-INFO: Compiling in debug mode. ($COMPILATION_FLAGS $WARNING_FLAGS)"
  90. else
  91. [ -z "$QUIET" ] && echo "COMPILE-INFO: Compiling in release mode. ($COMPILATION_FLAGS $FINAL_COMPILE_FLAGS)"
  92. fi
  93. # Create the raylib cache directory
  94. ROOT_DIR=$(pwd)
  95. TEMP_DIR="temp/release"
  96. if [ -n "$BUILD_DEBUG" ]; then
  97. TEMP_DIR="temp/debug"
  98. fi
  99. # If there's a -c flag, remove the cache
  100. if [ -d "$TEMP_DIR" ] && [ -n "$BUILD_ALL" ]; then
  101. [ -z "$QUIET" ] && echo "COMPILE-INFO: Found cached raylib, rebuilding."
  102. rm -r "$TEMP_DIR"
  103. fi
  104. # If temp directory doesn't exist, build raylib
  105. if [ ! -d "$TEMP_DIR" ]; then
  106. mkdir -p $TEMP_DIR
  107. cd $TEMP_DIR
  108. RAYLIB_DEFINES="-D_DEFAULT_SOURCE -DPLATFORM_DESKTOP -DGRAPHICS_API_OPENGL_33"
  109. RAYLIB_C_FILES="$RAYLIB_SRC/core.c $RAYLIB_SRC/shapes.c $RAYLIB_SRC/textures.c $RAYLIB_SRC/text.c $RAYLIB_SRC/models.c $RAYLIB_SRC/utils.c $RAYLIB_SRC/raudio.c $RAYLIB_SRC/rglfw.c"
  110. RAYLIB_INCLUDE_FLAGS="-I$RAYLIB_SRC -I$RAYLIB_SRC/external/glfw/include"
  111. if [ -n "$REALLY_QUIET" ]; then
  112. $CC -c $RAYLIB_DEFINES $RAYLIB_INCLUDE_FLAGS $COMPILATION_FLAGS $RAYLIB_C_FILES > /dev/null 2>&1
  113. else
  114. $CC -c $RAYLIB_DEFINES $RAYLIB_INCLUDE_FLAGS $COMPILATION_FLAGS $RAYLIB_C_FILES
  115. fi
  116. [ -z "$QUIET" ] && echo "COMPILE-INFO: Raylib compiled into object files in: $TEMP_DIR/"
  117. cd $ROOT_DIR
  118. fi
  119. # Build the actual game
  120. mkdir -p $OUTPUT_DIR
  121. cd $OUTPUT_DIR
  122. [ -z "$QUIET" ] && echo "COMPILE-INFO: Compiling game code."
  123. if [ -n "$REALLY_QUIET" ]; then
  124. $CC -c -o main.o -I$RAYLIB_SRC $COMPILATION_FLAGS $WARNING_FLAGS $SOURCES > /dev/null 2>&1
  125. $CC -o $GAME_NAME $ROOT_DIR/$TEMP_DIR/*.o main.o $LINK_FLAGS > /dev/null 2>&1
  126. else
  127. $CC -c -o main.o -I$RAYLIB_SRC $COMPILATION_FLAGS $WARNING_FLAGS $SOURCES
  128. $CC -o $GAME_NAME $ROOT_DIR/$TEMP_DIR/*.o main.o $LINK_FLAGS
  129. fi
  130. rm main.o
  131. [ -z "$QUIET" ] && echo "COMPILE-INFO: Game compiled into an executable in: $OUTPUT_DIR/"
  132. if [ -n "$UPX_IT" ]; then
  133. [ -z "$QUIET" ] && echo "COMPILE-INFO: Packing $GAME_NAME with upx."
  134. upx $GAME_NAME > /dev/null 2>&1
  135. fi
  136. if [ -n "$RUN_AFTER_BUILD" ]; then
  137. [ -z "$QUIET" ] && echo "COMPILE-INFO: Running."
  138. if [ -n "$REALLY_QUIET" ]; then
  139. ./$GAME_NAME > /dev/null 2>&1
  140. else
  141. ./$GAME_NAME
  142. fi
  143. fi
  144. cd $ROOT_DIR
  145. [ -z "$QUIET" ] && echo "COMPILE-INFO: All done."