compile.bat 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. :: This file is dedicated to the public domain.
  2. @echo off
  3. setlocal
  4. if not exist .build\ (
  5. md .build
  6. attrib +H .build
  7. )
  8. if not exist .build\include\ md .build\include
  9. if "%CC%"=="" set CC=clang --target=i686-pc-windows-msvc
  10. if "%HOSTCC%"=="" set HOSTCC=clang
  11. set host64=0
  12. (
  13. echo:#ifndef _WIN64
  14. echo:#error
  15. echo:#endif
  16. ) | %HOSTCC% -E - >nul 2>nul && set host64=1
  17. set warnings=-Wall -pedantic -Wno-parentheses -Wno-missing-braces ^
  18. -Wno-gnu-zero-variadic-macro-arguments -Werror=implicit-function-declaration ^
  19. -Werror=vla
  20. set dbg=0
  21. :: XXX: -Og would be nice but apparently a bunch of stuff still gets inlined
  22. :: which can be somewhat annoying so -O0 it is.
  23. if "%dbg%"=="1" (
  24. set cflags=-O0 -g3
  25. set ldflags=-O0 -g3
  26. ) else (
  27. set cflags=-O2
  28. set ldflags=-O2
  29. )
  30. set objs=
  31. goto :main
  32. :cc
  33. for /F %%b in ("%1") do set basename=%%~nb
  34. set dmodname= -DMODULE_NAME=%basename%
  35. :: ugly annoying special cases
  36. if "%dmodname%"==" -DMODULE_NAME=con_" set dmodname= -DMODULE_NAME=con
  37. if "%dmodname%"==" -DMODULE_NAME=sst" set dmodname=
  38. set objs=%objs% .build/%basename%.o
  39. :: note: we use a couple of C23 things now because otherwise we'd have to wait a
  40. :: year to get anything done. typeof=__typeof prevents pedantic warnings caused
  41. :: by typeof still technically being an extension, and stdbool gives us
  42. :: predefined bool/true/false before compilers start doing that by default
  43. %CC% -c -flto -mno-stack-arg-probe %cflags% %warnings% -I.build/include ^
  44. -D_CRT_SECURE_NO_WARNINGS -D_DLL -DWIN32_LEAN_AND_MEAN -DNOMINMAX%dmodname% ^
  45. -Dtypeof=__typeof -include stdbool.h -o .build/%basename%.o %1 || goto :end
  46. goto :eof
  47. :src
  48. goto :eof
  49. :main
  50. set src=
  51. :: funny hack to build a list conveniently, lol.
  52. setlocal EnableDelayedExpansion
  53. for /f "tokens=2" %%f in ('findstr /B /C:":+ " "%~nx0"') do set src=!src! src/%%f
  54. setlocal DisableDelayedExpansion
  55. :+ ac.c
  56. :+ alias.c
  57. :+ autojump.c
  58. :+ bind.c
  59. :+ clientcon.c
  60. :+ con_.c
  61. :+ chunklets/fastspin.c
  62. :+ chunklets/msg.c
  63. :+ crypto.c
  64. :+ democustom.c
  65. :+ demorec.c
  66. :+ engineapi.c
  67. :+ ent.c
  68. :+ errmsg.c
  69. :+ extmalloc.c
  70. :+ fastfwd.c
  71. :+ fixes.c
  72. :+ fov.c
  73. :+ gamedata.c
  74. :+ gameinfo.c
  75. :+ gameserver.c
  76. :+ hexcolour.c
  77. :+ hook.c
  78. :+ hud.c
  79. :+ inputhud.c
  80. :+ kvsys.c
  81. :+ l4dmm.c
  82. :+ l4dreset.c
  83. :+ l4dwarp.c
  84. :+ nomute.c
  85. :+ nosleep.c
  86. :+ os.c
  87. :+ portalcolours.c
  88. :+ rinput.c
  89. :+ sst.c
  90. :+ trace.c
  91. :+ xhair.c
  92. :+ x86.c
  93. :: just tack these on, whatever (repeated condition because of expansion memes)
  94. if "%dbg%"=="1" set src=%src% src/dbg.c
  95. if "%dbg%"=="1" set src=%src% src/udis86.c
  96. if "%dbg%"=="0" set src=%src% src/wincrt.c
  97. %CC% -fuse-ld=lld -shared -O0 -w -o .build/bcryptprimitives.dll -Wl,-def:src/stubs/bcryptprimitives.def src/stubs/bcryptprimitives.c
  98. set lbcryptprimitives_host=-lbcryptprimitives
  99. if %host64%==1 (
  100. :: note: no mangling madness on x64, so we can just call the linker directly
  101. lld-link -machine:x64 -def:src/stubs/bcryptprimitives.def -implib:.build/bcryptprimitives64.lib
  102. set lbcryptprimitives_host=-lbcryptprimitives64
  103. )
  104. %CC% -fuse-ld=lld -shared -O0 -w -o .build/tier0.dll src/stubs/tier0.c
  105. %CC% -fuse-ld=lld -shared -O0 -w -o .build/vstdlib.dll src/stubs/vstdlib.c
  106. %HOSTCC% -fuse-ld=lld -O2 %warnings% -D_CRT_SECURE_NO_WARNINGS -include stdbool.h ^
  107. -L.build %lbcryptprimitives_host% -o .build/codegen.exe src/build/codegen.c src/build/cmeta.c src/os.c || goto :end
  108. %HOSTCC% -fuse-ld=lld -O2 %warnings% -D_CRT_SECURE_NO_WARNINGS -include stdbool.h ^
  109. -L.build %lbcryptprimitives_host% -o .build/mkgamedata.exe src/build/mkgamedata.c src/os.c || goto :end
  110. %HOSTCC% -fuse-ld=lld -O2 -g %warnings% -D_CRT_SECURE_NO_WARNINGS -include stdbool.h ^
  111. -L.build %lbcryptprimitives_host% -o .build/mkentprops.exe src/build/mkentprops.c src/os.c || goto :end
  112. .build\codegen.exe%src% || goto :end
  113. .build\mkgamedata.exe gamedata/engine.txt gamedata/gamelib.txt gamedata/inputsystem.txt ^
  114. gamedata/matchmaking.txt gamedata/vgui2.txt gamedata/vguimatsurface.txt || goto :end
  115. .build\mkentprops.exe gamedata/entprops.txt || goto :end
  116. llvm-rc /FO .build\dll.res src\dll.rc || goto :end
  117. for %%b in (%src%) do ( call :cc %%b || goto :end )
  118. :: we need different library names for debugging because Microsoft...
  119. :: actually, it's different anyway because we don't use vcruntime for releases
  120. :: any more. see comment in wincrt.c
  121. if "%dbg%"=="1" (
  122. set clibs=-lmsvcrtd -lvcruntimed -lucrtd
  123. ) else (
  124. set clibs=-lucrt
  125. )
  126. %CC% -fuse-ld=lld -shared -flto %ldflags% -Wl,/IMPLIB:.build/sst.lib,/Brepro,/nodefaultlib ^
  127. -L.build %clibs% -lkernel32 -luser32 -lbcryptprimitives -lshlwapi -ld3d9 -ldsound ^
  128. -ltier0 -lvstdlib -lntdll -o .build/sst.dll%objs% .build/dll.res || goto :end
  129. :: get rid of another useless file (can we just not create this???)
  130. del .build\sst.lib
  131. :: awkward logic to replace sst.dll while it's potentially loaded, because
  132. :: windows likes to lock things and/or doesn't have atomic rename.
  133. :: very TOCTOU-ish code, not good at all, but sometimes bad is good enough
  134. move /y .build\sst.dll sst.dll >nul 2>nul || (
  135. move /y sst.dll .build\sst.old.dll >nul 2>nul || (
  136. echo:ERROR: couldn't remove sst.dll OR .build/sst.old.dll - are both loaded?>&2
  137. goto :end
  138. )
  139. move .build\sst.dll sst.dll >nul 2>nul || (
  140. echo:ERROR: couldn't replace sst.dll - did something just try to load it?>&2
  141. goto :end
  142. )
  143. )
  144. :: try to cleanup the .old again - it'll fail if we just moved it but may work
  145. :: in some eventual future invocation
  146. if exist .build\sst.old.dll del .build\sst.old.dll >nul 2>nul
  147. %HOSTCC% -fuse-ld=lld -O2 -g -include test/test.h -o .build/bitbuf.test.exe test/bitbuf.test.c || goto :end
  148. .build\bitbuf.test.exe || goto :end
  149. :: special case: test must be 32-bit
  150. %HOSTCC% -fuse-ld=lld -m32 -O2 -g -L.build -lbcryptprimitives -include test/test.h -o .build/hook.test.exe test/hook.test.c || goto :end
  151. .build\hook.test.exe || goto :end
  152. %HOSTCC% -fuse-ld=lld -O2 -g -include test/test.h -o .build/x86.test.exe test/x86.test.c || goto :end
  153. .build\x86.test.exe || goto :end
  154. :end
  155. exit /b %errorlevel%
  156. :: vi: sw=4 tw=4 noet tw=80 cc=80