1@echo off
2REM Change your executable name here
3set GAME_NAME=game.exe
4
5REM Set your sources here (relative paths!)
6REM Example with two source folders:
7REM set SOURCES=src\*.c src\submodule\*.c
8set SOURCES=core_basic_window.c
9
10REM Set your raylib\src location here (relative path!)
11set RAYLIB_SRC=..\..\src
12
13REM Set the target platform for the compiler (Ex: x86 or x64)
14set TARGET_PLATFORM=x86
15
16REM About this build script: it does many things, but in essence, it's
17REM very simple. It has 3 compiler invocations: building raylib (which
18REM is not done always, see logic by searching "Build raylib"), building
19REM src/*.c files, and linking together those two. Each invocation is
20REM wrapped in an if statement to make the -qq flag work, it's pretty
21REM verbose, sorry.
22
23REM To skip to the actual building part of the script, search for ":BUILD"
24
25REM Checks if cl is available and skips to the argument loop if it is
26REM (Prevents calling vcvarsall every time you run this script)
27WHERE cl >nul 2>nul
28IF %ERRORLEVEL% == 0 goto READ_ARGS
29REM Activate the msvc build environment if cl isn't available yet
30IF EXIST "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" (
31  set VC_INIT="C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat"
32) ELSE IF EXIST "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" (
33  set VC_INIT="C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat"
34) ELSE IF EXIST "C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat" (
35  set VC_INIT="C:\Program Files (x86)\Microsoft Visual C++ Build Tools\vcbuildtools.bat"
36) ELSE (
37  REM Initialize your vc environment here if the defaults don't work
38  REM  set VC_INIT="C:\your\path\here\vcvarsall.bat"
39  REM And then remove/comment out the following two lines
40  echo "Couldn't find vcvarsall.bat or vcbuildtools.bat, please set it manually."
41  exit /B
42)
43echo Setting up the msvc build environment, this could take some time but the next builds should be faster
44REM Remove everything after %TARGET_PLATFORM% if you want to see
45REM the vcvarsall.bat or vcbuildtools.bat output
46call %VC_INIT% %TARGET_PLATFORM% > NUL 2>&1
47
48:READ_ARGS
49REM For the ! variable notation
50setlocal EnableDelayedExpansion
51REM For shifting, which the command line argument parsing needs
52setlocal EnableExtensions
53
54:ARG_LOOP
55set ARG=%1
56if "!ARG!" == "" ( goto BUILD )
57IF NOT "x!ARG!" == "x!ARG:h=!" (
58  goto HELP
59)
60IF NOT "x!ARG!" == "x!ARG:d=!" (
61  set BUILD_DEBUG=1
62)
63IF NOT "x!ARG!" == "x!ARG:u=!" (
64  set UPX_IT=1
65)
66IF NOT "x!ARG!" == "x!ARG:r=!" (
67  set RUN_AFTER_BUILD=1
68)
69IF NOT "x!ARG!" == "x!ARG:c=!" (
70  set BUILD_ALL=1
71)
72IF NOT "x!ARG!" == "x!ARG:qq=!" (
73  set QUIET=1
74  set REALLY_QUIET=1
75) ELSE IF NOT "x!ARG!" == "x!ARG:q=!" (
76  IF DEFINED QUIET (
77    set REALLY_QUIET=1
78  ) ELSE (
79    set QUIET=1
80  )
81)
82IF NOT "x!ARG!" == "x!ARG:v=!" (
83  set VERBOSE=1
84)
85IF NOT "%1" == "" (
86  shift /1
87  goto ARG_LOOP
88)
89
90
91:HELP
92echo Usage: build-windows.bat [-hdurcqqv]
93echo  -h  Show this information
94echo  -d  Faster builds that have debug symbols, and enable warnings
95echo  -u  Run upx* on the executable after compilation (before -r)
96echo  -r  Run the executable after compilation
97echo  -c  Remove the temp\{debug,release} directory, ie. full recompile
98echo  -q  Suppress this script's informational prints
99echo  -qq Suppress all prints, complete silence
100echo  -v  cl.exe normally prints out a lot of superficial information, as
101echo      well as the MSVC build environment activation scripts, but these are
102echo      mostly suppressed by default. If you do want to see everything, use
103echo      this flag.
104echo.
105echo * This is mostly here to make building simple "shipping" versions
106echo   easier, and it's a very small bit in the build scripts. The option
107echo   requires that you have upx installed and on your path, of course.
108echo.
109echo Examples:
110echo  Build a release build:                    build-windows.bat
111echo  Build a release build, full recompile:    build-windows.bat -c
112echo  Build a debug build and run:              build-windows.bat -d -r
113echo  Build in debug, run, don't print at all:  build-windows.bat -drqq
114exit /B
115
116
117:BUILD
118REM Directories
119set "ROOT_DIR=%CD%"
120set "SOURCES=!ROOT_DIR!\!SOURCES!"
121set "RAYLIB_SRC=!ROOT_DIR!\!RAYLIB_SRC!"
122
123REM Flags
124set OUTPUT_FLAG=/Fe: "!GAME_NAME!"
125set COMPILATION_FLAGS=/O1 /GL
126set WARNING_FLAGS=
127set SUBSYSTEM_FLAGS=/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup
128set LINK_FLAGS=/link /LTCG kernel32.lib user32.lib shell32.lib winmm.lib gdi32.lib opengl32.lib
129set OUTPUT_DIR=builds\windows-msvc
130REM Debug changes to flags
131IF DEFINED BUILD_DEBUG (
132  set OUTPUT_FLAG=/Fe: "!GAME_NAME!"
133  set COMPILATION_FLAGS=/Od /Zi
134  set WARNING_FLAGS=/Wall
135  set SUBSYSTEM_FLAGS=/DEBUG
136  set LINK_FLAGS=/link kernel32.lib user32.lib shell32.lib winmm.lib gdi32.lib opengl32.lib
137  set OUTPUT_DIR=builds-debug\windows-msvc
138)
139IF NOT DEFINED VERBOSE (
140  set VERBOSITY_FLAG=/nologo
141)
142
143REM Display what we're doing
144IF DEFINED BUILD_DEBUG (
145  IF NOT DEFINED QUIET echo COMPILE-INFO: Compiling in debug mode, flags: !COMPILATION_FLAGS! !WARNING_FLAGS!
146) ELSE (
147  IF NOT DEFINED QUIET echo COMPILE-INFO: Compiling in release mode, flags: !COMPILATION_FLAGS! /link /LTCG
148)
149
150REM Create the temp directory for raylib
151set "TEMP_DIR=temp\release"
152IF DEFINED BUILD_DEBUG (
153  set "TEMP_DIR=temp\debug"
154)
155
156IF DEFINED BUILD_ALL (
157  IF EXIST !TEMP_DIR!\ (
158    IF NOT DEFINED QUIET echo COMPILE-INFO: Found cached raylib, rebuilding.
159    del /Q !TEMP_DIR!
160    rmdir !TEMP_DIR!
161  )
162)
163
164REM Build raylib if it hasn't been cached in TEMP_DIR
165IF NOT EXIST !TEMP_DIR!\ (
166  mkdir !TEMP_DIR!
167  cd !TEMP_DIR!
168  REM Raylib's src folder
169  set "RAYLIB_DEFINES=/D_DEFAULT_SOURCE /DPLATFORM_DESKTOP /DGRAPHICS_API_OPENGL_33"
170  set 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"
171  set RAYLIB_INCLUDE_FLAGS=/I"!RAYLIB_SRC!" /I"!RAYLIB_SRC!\external\glfw\include"
172
173  IF DEFINED REALLY_QUIET (
174    cl.exe /w /c !RAYLIB_DEFINES! !RAYLIB_INCLUDE_FLAGS! !COMPILATION_FLAGS! !RAYLIB_C_FILES! > NUL 2>&1 || exit /B
175  ) ELSE (
176    cl.exe /w /c !VERBOSITY_FLAG! !RAYLIB_DEFINES! !RAYLIB_INCLUDE_FLAGS! !COMPILATION_FLAGS! !RAYLIB_C_FILES! || exit /B
177  )
178  IF NOT DEFINED QUIET echo COMPILE-INFO: Raylib compiled into object files in: !TEMP_DIR!\
179
180  REM Out of the temp directory
181  cd !ROOT_DIR!
182)
183
184REM Move to the build directory
185IF NOT EXIST !OUTPUT_DIR! mkdir !OUTPUT_DIR!
186cd !OUTPUT_DIR!
187
188REM Build the actual game
189IF NOT DEFINED QUIET echo COMPILE-INFO: Compiling game code.
190IF DEFINED REALLY_QUIET (
191  cl.exe !VERBOSITY_FLAG! !COMPILATION_FLAGS! !WARNING_FLAGS! /c /I"!RAYLIB_SRC!" !SOURCES! > NUL 2>&1 || exit /B
192  cl.exe !VERBOSITY_FLAG! !OUTPUT_FLAG! "!ROOT_DIR!\!TEMP_DIR!\*.obj" *.obj !LINK_FLAGS! !SUBSYSTEM_FLAGS! > NUL 2>&1 || exit /B
193) ELSE (
194  cl.exe !VERBOSITY_FLAG! !COMPILATION_FLAGS! !WARNING_FLAGS! /c /I"!RAYLIB_SRC!" !SOURCES! || exit /B
195  cl.exe !VERBOSITY_FLAG! !OUTPUT_FLAG! "!ROOT_DIR!\!TEMP_DIR!\*.obj" *.obj !LINK_FLAGS! !SUBSYSTEM_FLAGS! || exit /B
196)
197del *.obj
198IF NOT DEFINED QUIET echo COMPILE-INFO: Game compiled into an executable in: !OUTPUT_DIR!\
199
200REM Run upx
201IF DEFINED UPX_IT (
202  IF NOT DEFINED QUIET echo COMPILE-INFO: Packing !GAME_NAME! with upx.
203  upx !GAME_NAME! > NUL 2>&1
204)
205
206REM Finally, run the produced executable
207IF DEFINED RUN_AFTER_BUILD (
208  IF NOT DEFINED QUIET echo COMPILE-INFO: Running.
209  IF DEFINED REALLY_QUIET (
210    !GAME_NAME! > NUL 2>&1
211  ) ELSE (
212    !GAME_NAME!
213  )
214)
215
216REM Back to development directory
217cd !ROOT_DIR!
218
219IF NOT DEFINED QUIET echo COMPILE-INFO: All done.
220