summaryrefslogtreecommitdiffstats
path: root/build.bat
blob: 77439434dd57be6c916c0847fefa01071065bd35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
@echo off
rem author: nasr
rem windows build for design-it. the counterpart of compile.sh / reload.sh.
rem
rem   build.bat                 debug build + hot reload (build\design_ui.dll)
rem   build.bat release         release: one self contained build\release\design-it.exe
rem   build.bat ui              rebuild only build\design_ui.dll, a running debug build swaps it in
rem   build.bat clean           delete build\
rem   build.bat ... arm64       target windows on arm (default x64)
rem
rem every build links freetype + harfbuzz and the c runtime statically, and vulkan is loaded at
rem runtime from the gpu driver, so the exe has no dll dependencies beyond windows itself.
rem the first build compiles freetype + harfbuzz from source into build\deps (curl, tar, cmake and
rem ninja: windows 10+ ships curl / tar, visual studio ships cmake / ninja).
rem
rem toolchains (set TOOLCHAIN=..., default clang):
rem   clang   LLVM clang++ + lld against the msvc abi. needs the visual studio build tools (c++
rem           workload; vcvars is found with vswhere if you're not in a developer prompt) and
rem           clang (LLVM installer, or the "C++ Clang tools for Windows" component).
rem   mingw   an msys2 ucrt64 / mingw64 / clangarm64 shell:
rem             pacman -S mingw-w64-ucrt-x86_64-{gcc,cmake,ninja}
rem
rem other knobs: BUILD_DIR, HOT_RELOAD (= debug), WERROR (1), DEPS_DIR (prebuilt static freetype +
rem harfbuzz with include\ and lib\, e.g. vcpkg's x64-windows-static: skips building them)

setlocal EnableDelayedExpansion
cd /d "%~dp0"

set MODE=debug
set ONLY_UI=0
set ARCH=x64
:parse_args
if "%~1"=="" goto args_done
if /i "%~1"=="debug"   set MODE=debug
if /i "%~1"=="release" set MODE=release
if /i "%~1"=="ui"      set ONLY_UI=1
if /i "%~1"=="x64"     set ARCH=x64
if /i "%~1"=="arm64"   set ARCH=arm64
if /i "%~1"=="clean"   goto clean
shift
goto parse_args
:args_done

if not defined TOOLCHAIN set TOOLCHAIN=clang
if not defined WERROR    set WERROR=1
if "%MODE%"=="debug" (set DEBUG=1) else (set DEBUG=0)
if not defined HOT_RELOAD set HOT_RELOAD=%DEBUG%
if "%ONLY_UI%"=="1" (set HOT_RELOAD=1& set DEBUG=1& set MODE=debug)
if not defined BUILD_DIR (
	if "%DEBUG%"=="1" (set BUILD_DIR=build) else (set BUILD_DIR=build\release)
)
if /i "%ARCH%"=="arm64" if "%BUILD_DIR%"=="build" set BUILD_DIR=build\arm64
if /i "%ARCH%"=="arm64" if "%BUILD_DIR%"=="build\release" set BUILD_DIR=build\release-arm64

set SOURCE_DIR=source
set GEN_DIR=%BUILD_DIR%\generated
set TARGET=design-it

echo %DATE% %TIME%
echo Compiling design-it [%MODE%, %ARCH%] with %TOOLCHAIN%
echo.

if not exist "%BUILD_DIR%" mkdir "%BUILD_DIR%"
if not exist "%GEN_DIR%"   mkdir "%GEN_DIR%"

rem ---------------------------------------------------------------------------------------------
rem flags shared by both toolchains (same as compile.sh)

set INCLUDES=-I%SOURCE_DIR% -I%SOURCE_DIR%\base -I%SOURCE_DIR%\core -I%SOURCE_DIR%\platform -I%SOURCE_DIR%\render -I%SOURCE_DIR%\diagram -I%SOURCE_DIR%\app -I%GEN_DIR% -I%SOURCE_DIR%\render\shaders\prebuilt -I%SOURCE_DIR%\third_party
set CFLAGS=-std=c++20 -fno-rtti -fno-exceptions -Wall -Wundef -Wno-unused-function -Wno-unused-variable -Wno-unused-but-set-variable -Wno-missing-field-initializers -fno-omit-frame-pointer -DVULKAN=1
if "%WERROR%"=="1" set CFLAGS=%CFLAGS% -Werror
if "%DEBUG%"=="1" (
	set CFLAGS=!CFLAGS! -O0 -g -DDEBUG=1
) else (
	set CFLAGS=!CFLAGS! -O2 -g -DDEBUG=0
)
set SYSLIBS=-luser32 -lshell32 -lwinmm -lsynchronization -lpsapi

if /i "%TOOLCHAIN%"=="clang" goto setup_clang
if /i "%TOOLCHAIN%"=="mingw" goto setup_mingw
echo unknown TOOLCHAIN=%TOOLCHAIN% (clang or mingw)
exit /b 1

rem ---------------------------------------------------------------------------------------------
:setup_clang

rem NOTE: clang needs the msvc crt + windows sdk paths, vcvarsall sets INCLUDE / LIB / PATH.
rem (gotos instead of an if block: %ProgramFiles(x86)% has a ')' that would close the block)
set VCVARS_ARCH=x64
if /i "%ARCH%"=="arm64" (
	if /i "%PROCESSOR_ARCHITECTURE%"=="ARM64" (set VCVARS_ARCH=arm64) else (set VCVARS_ARCH=x64_arm64)
)
if defined VCINSTALLDIR goto have_vcvars
set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
if exist "%VSWHERE%" goto have_vswhere
echo vswhere not found: install the visual studio build tools, or run from a developer prompt
exit /b 1
:have_vswhere
for /f "usebackq tokens=*" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do set "VSPATH=%%i"
if defined VSPATH goto have_vspath
echo no visual studio install with the c++ tools found
exit /b 1
:have_vspath
call "%VSPATH%\VC\Auxiliary\Build\vcvarsall.bat" %VCVARS_ARCH% >nul
:have_vcvars

where clang++ >nul 2>nul
if errorlevel 1 (
	rem NOTE: the "C++ Clang tools for Windows" component puts clang here
	if exist "!VCINSTALLDIR!Tools\Llvm\x64\bin\clang++.exe" (
		set "PATH=!VCINSTALLDIR!Tools\Llvm\x64\bin;!PATH!"
	) else (
		echo clang++ not found: install LLVM or the "C++ Clang tools for Windows" component
		exit /b 1
	)
)

set CXX=clang++
if /i "%ARCH%"=="arm64" (set CLANG_TARGET=aarch64-pc-windows-msvc) else (set CLANG_TARGET=x86_64-pc-windows-msvc)
rem NOTE: _CRT_* silence the msvc crt deprecations (fopen, getenv, access) that -Werror would trip.
rem static crt (/MT): no vcruntime140.dll needed, has to match how build\deps was compiled.
set CFLAGS=%CFLAGS% --target=%CLANG_TARGET% -fms-runtime-lib=static -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE -Wno-unknown-warning-option
set EXE_LDFLAGS=
if "%DEBUG%"=="0" set EXE_LDFLAGS=-Wl,/SUBSYSTEM:WINDOWS -Wl,/ENTRY:mainCRTStartup
set RES=%BUILD_DIR%\design-it.res
set LIB_FT=freetype.lib
set LIB_HB=harfbuzz.lib

if defined DEPS_DIR goto deps_ready
set DEPS_DIR=build\deps\windows-clang-%ARCH%
if exist "%DEPS_DIR%\lib\freetype.lib" if exist "%DEPS_DIR%\lib\harfbuzz.lib" goto deps_ready
call :build_deps_cmake "-DCMAKE_C_COMPILER=clang" "-DCMAKE_CXX_COMPILER=clang++" "-DCMAKE_C_COMPILER_TARGET=%CLANG_TARGET%" "-DCMAKE_CXX_COMPILER_TARGET=%CLANG_TARGET%" "-DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded" "-DCMAKE_POLICY_DEFAULT_CMP0091=NEW"
if errorlevel 1 exit /b 1
goto deps_ready

rem ---------------------------------------------------------------------------------------------
:setup_mingw

where g++ >nul 2>nul
if errorlevel 1 (
	echo g++ not found: run this from an msys2 ucrt64 / mingw64 / clangarm64 shell
	exit /b 1
)
set CXX=g++
set CFLAGS=%CFLAGS% -Wno-format-truncation
set EXE_LDFLAGS=
if "%DEBUG%"=="0" set EXE_LDFLAGS=-mwindows -Wl,-e,mainCRTStartup
set RES=%BUILD_DIR%\design-it.res.o
set LIB_FT=libfreetype.a
set LIB_HB=libharfbuzz.a

if defined DEPS_DIR goto deps_ready
set DEPS_DIR=build\deps\windows-mingw-%ARCH%
if exist "%DEPS_DIR%\lib\libfreetype.a" if exist "%DEPS_DIR%\lib\libharfbuzz.a" goto deps_ready
sh scripts/deps.sh "%DEPS_DIR:\=/%"
if errorlevel 1 (
	echo building the static deps failed
	exit /b 1
)
goto deps_ready

rem ---------------------------------------------------------------------------------------------
:deps_ready

set INCLUDES=%INCLUDES% -I"%DEPS_DIR%\include\freetype2" -I"%DEPS_DIR%\include\harfbuzz" -I"%DEPS_DIR%\include"
set LIBS="%DEPS_DIR%\lib\%LIB_HB%" "%DEPS_DIR%\lib\%LIB_FT%" %SYSLIBS%
if /i "%TOOLCHAIN%"=="clang" (
	set LIBS=-fuse-ld=lld !LIBS!
	rem NOTE: a vcpkg freetype with its default features also needs these, link what's there
	for %%l in (zlib bz2 libpng16 brotlidec brotlicommon) do if exist "!DEPS_DIR!\lib\%%l.lib" set LIBS=!LIBS! "!DEPS_DIR!\lib\%%l.lib"
) else (
	set LIBS=!LIBS! -lpthread -static -static-libgcc -static-libstdc++
)

rem - shaders
rem NOTE: optional: without glslc the checked in source\render\shaders\prebuilt\*.spv.h are used
where glslc >nul 2>nul
if errorlevel 1 goto shaders_done
for %%s in (%SOURCE_DIR%\render\shaders\*.vert %SOURCE_DIR%\render\shaders\*.frag) do (
	set NAME=%%~nxs
	set NAME=!NAME:.=_!
	glslc -O -mfmt=c "%%s" -o "!GEN_DIR!\!NAME!.spv.h"
	if errorlevel 1 (
		echo shader compile failed: %%s
		exit /b 1
	)
)
:shaders_done

rem - hot reload ui dll
rem NOTE: linked to a temp name and moved, so the running app never loads a half written dll.
rem every build gets a fresh pdb name, a debugger attached to the old dll keeps its pdb locked.
if "%HOT_RELOAD%"=="1" (
	set DLL_FLAGS=-shared -DDESIGN_HOT_RELOAD=1 -DDESIGN_UI_DLL=1
	if /i "%TOOLCHAIN%"=="clang" set DLL_FLAGS=!DLL_FLAGS! -Wl,/PDB:!BUILD_DIR!\design_ui_!RANDOM!.pdb
	echo Building !BUILD_DIR!\design_ui.dll...
	!CXX! !INCLUDES! !CFLAGS! !DLL_FLAGS! !SOURCE_DIR!\app\app.cpp -o !BUILD_DIR!\design_ui.tmp.dll !LIBS!
	if errorlevel 1 (
		echo ==== UI BUILD FAILED ====
		exit /b 1
	)
	move /y "!BUILD_DIR!\design_ui.tmp.dll" "!BUILD_DIR!\design_ui.dll" >nul
	if exist "!BUILD_DIR!\design_ui.tmp.lib" del /q "!BUILD_DIR!\design_ui.tmp.lib" "!BUILD_DIR!\design_ui.tmp.exp" 2>nul
)
if "%ONLY_UI%"=="1" (
	echo ==== RELOAD BUILT ====
	exit /b 0
)

rem - manifest (utf-8 code page, per monitor dpi, long paths)
if /i "%TOOLCHAIN%"=="clang" (
	where llvm-rc >nul 2>nul
	if errorlevel 1 (
		rc /nologo /fo "!RES!" data\windows\design-it.rc
	) else (
		llvm-rc /fo "!RES!" data\windows\design-it.rc
	)
) else (
	windres data\windows\design-it.rc -O coff -o "!RES!"
)
if errorlevel 1 (
	echo resource compile failed
	exit /b 1
)

rem - the app
set HOST_FLAGS=
if "%HOT_RELOAD%"=="1" set HOST_FLAGS=-DDESIGN_HOT_RELOAD=1
echo Building %BUILD_DIR%\%TARGET%.exe...
%CXX% %INCLUDES% %CFLAGS% %HOST_FLAGS% %SOURCE_DIR%\app\app.cpp %RES% -o %BUILD_DIR%\%TARGET%.exe %EXE_LDFLAGS% %LIBS%
if errorlevel 1 (
	echo ==== BUILD FAILED ====
	exit /b 1
)

echo ==== BUILD SUCCESS ====
if "%DEBUG%"=="0" echo single file: %BUILD_DIR%\%TARGET%.exe
exit /b 0

rem ---------------------------------------------------------------------------------------------
rem static freetype + harfbuzz with cmake, same recipe as scripts\deps.sh. args: extra cmake flags
:build_deps_cmake
where cmake >nul 2>nul
if errorlevel 1 (
	echo cmake not found: install the "C++ CMake tools for Windows" component, or set DEPS_DIR
	exit /b 1
)
set FT_VER=2.13.3
set HB_VER=10.1.0
set DEPS_SRC=%DEPS_DIR%\src
if not exist "%DEPS_SRC%" mkdir "%DEPS_SRC%"
if not exist "%DEPS_SRC%\freetype.tar.xz" curl -fL --retry 3 -o "%DEPS_SRC%\freetype.tar.xz" https://download.savannah.gnu.org/releases/freetype/freetype-%FT_VER%.tar.xz
if errorlevel 1 exit /b 1
if not exist "%DEPS_SRC%\harfbuzz.tar.xz" curl -fL --retry 3 -o "%DEPS_SRC%\harfbuzz.tar.xz" https://github.com/harfbuzz/harfbuzz/releases/download/%HB_VER%/harfbuzz-%HB_VER%.tar.xz
if errorlevel 1 exit /b 1
if not exist "%DEPS_SRC%\freetype-%FT_VER%" tar -xf "%DEPS_SRC%\freetype.tar.xz" -C "%DEPS_SRC%"
if not exist "%DEPS_SRC%\harfbuzz-%HB_VER%" tar -xf "%DEPS_SRC%\harfbuzz.tar.xz" -C "%DEPS_SRC%"
set GENERATOR=NMake Makefiles
where ninja >nul 2>nul
if not errorlevel 1 set GENERATOR=Ninja
set COMMON=-DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF "-DCMAKE_INSTALL_PREFIX=%CD%\%DEPS_DIR%" -DCMAKE_INSTALL_LIBDIR=lib "-DCMAKE_PREFIX_PATH=%CD%\%DEPS_DIR%" -DCMAKE_POLICY_VERSION_MINIMUM=3.5
echo building freetype %FT_VER% (static)
cmake -S "%DEPS_SRC%\freetype-%FT_VER%" -B "%DEPS_SRC%\build-freetype" -G "%GENERATOR%" %COMMON% -DFT_DISABLE_ZLIB=ON -DFT_DISABLE_BZIP2=ON -DFT_DISABLE_PNG=ON -DFT_DISABLE_HARFBUZZ=ON -DFT_DISABLE_BROTLI=ON %* >nul
if errorlevel 1 exit /b 1
cmake --build "%DEPS_SRC%\build-freetype" --target install >nul
if errorlevel 1 exit /b 1
echo building harfbuzz %HB_VER% (static)
cmake -S "%DEPS_SRC%\harfbuzz-%HB_VER%" -B "%DEPS_SRC%\build-harfbuzz" -G "%GENERATOR%" %COMMON% -DHB_HAVE_FREETYPE=ON -DHB_HAVE_GLIB=OFF -DHB_HAVE_GOBJECT=OFF -DHB_HAVE_ICU=OFF -DHB_HAVE_GRAPHITE2=OFF -DHB_BUILD_SUBSET=OFF -DHB_BUILD_UTILS=OFF %* >nul
if errorlevel 1 exit /b 1
cmake --build "%DEPS_SRC%\build-harfbuzz" --target install >nul
if errorlevel 1 exit /b 1
rem NOTE: some freetype versions install freetyped.lib / freetype-static.lib names, normalize
if not exist "%DEPS_DIR%\lib\freetype.lib" for %%f in ("%DEPS_DIR%\lib\freetype*.lib") do copy /y "%%f" "%DEPS_DIR%\lib\freetype.lib" >nul
exit /b 0

:clean
if exist build rmdir /s /q build
echo cleaned
exit /b 0