compile.bat 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 -fsanitize-trap=undefined -DSST_DBG
  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. %CC% -c -flto -mno-stack-arg-probe %cflags% %warnings% %stdflags% -I.build/include ^
  40. -D_DLL%dmodname% -o .build/%basename%.o %1 || goto :end
  41. goto :eof
  42. :src
  43. goto :eof
  44. :main
  45. set stdflags=-std=c2x -D_CRT_SECURE_NO_WARNINGS -DWIN32_LEAN_AND_MEAN -DNOMINMAX
  46. set src=
  47. :: funny hack to build a list conveniently, lol.
  48. setlocal EnableDelayedExpansion
  49. for /f "tokens=2" %%f in ('findstr /B /C:":+ " "%~nx0"') do set src=!src! src/%%f
  50. setlocal DisableDelayedExpansion
  51. :+ ac.c
  52. :+ alias.c
  53. :+ autojump.c
  54. :+ bind.c
  55. :+ clientcon.c
  56. :+ con_.c
  57. :+ chatrate.c
  58. :+ chunklets/fastspin.c
  59. :+ chunklets/msg.c
  60. :+ crypto.c
  61. :+ democustom.c
  62. :+ demorec.c
  63. :+ engineapi.c
  64. :+ ent.c
  65. :+ errmsg.c
  66. :+ extmalloc.c
  67. :+ fastfwd.c
  68. :+ fixes.c
  69. :+ fov.c
  70. :+ gameinfo.c
  71. :+ gameserver.c
  72. :+ hexcolour.c
  73. :+ hook.c
  74. :+ hud.c
  75. :+ inputhud.c
  76. :+ kvsys.c
  77. :+ l4d1democompat.c
  78. :+ l4daddon.c
  79. :+ l4dmm.c
  80. :+ l4dreset.c
  81. :+ l4dwarp.c
  82. :+ nomute.c
  83. :+ nosleep.c
  84. :+ os.c
  85. :+ portalcolours.c
  86. :+ rinput.c
  87. :+ sst.c
  88. :+ trace.c
  89. :+ x86.c
  90. :+ xhair.c
  91. :: just tack these on, whatever (repeated condition because of expansion memes)
  92. if "%dbg%"=="1" set src=%src% src/dbg.c
  93. if "%dbg%"=="1" set src=%src% src/udis86.c
  94. if "%dbg%"=="0" set src=%src% src/wincrt.c
  95. %CC% -fuse-ld=lld -shared -O0 -w -o .build/bcryptprimitives.dll -Wl,-def:src/stubs/bcryptprimitives.def src/stubs/bcryptprimitives.c
  96. set lbcryptprimitives_host=-lbcryptprimitives
  97. if %host64%==1 (
  98. :: note: no mangling madness on x64, so we can just call the linker directly
  99. lld-link -machine:x64 -def:src/stubs/bcryptprimitives.def -implib:.build/bcryptprimitives64.lib
  100. set lbcryptprimitives_host=-lbcryptprimitives64
  101. )
  102. %CC% -fuse-ld=lld -shared -O0 -w -o .build/tier0.dll src/stubs/tier0.c
  103. %CC% -fuse-ld=lld -shared -O0 -w -o .build/vstdlib.dll src/stubs/vstdlib.c
  104. %HOSTCC% -fuse-ld=lld -O2 %warnings% %stdflags% -include stdbool.h ^
  105. -L.build %lbcryptprimitives_host% -o .build/gluegen.exe src/build/gluegen.c src/build/cmeta.c src/os.c || goto :end
  106. %HOSTCC% -fuse-ld=lld -O2 %warnings% %stdflags% -include stdbool.h ^
  107. -L.build %lbcryptprimitives_host% -o .build/mkgamedata.exe src/build/mkgamedata.c src/os.c || goto :end
  108. %HOSTCC% -fuse-ld=lld -O2 %warnings% %stdflags% -include stdbool.h ^
  109. -L.build %lbcryptprimitives_host% -o .build/mkentprops.exe src/build/mkentprops.c src/os.c || goto :end
  110. .build\gluegen.exe%src% || goto :end
  111. .build\mkgamedata.exe gamedata/engine.txt gamedata/gamelib.txt gamedata/inputsystem.txt ^
  112. gamedata/matchmaking.txt gamedata/vgui2.txt gamedata/vguimatsurface.txt || goto :end
  113. .build\mkentprops.exe gamedata/entprops.txt || goto :end
  114. llvm-rc /FO .build\dll.res src\dll.rc || goto :end
  115. for %%b in (%src%) do ( call :cc %%b || goto :end )
  116. :: we need different library names for debugging because Microsoft...
  117. :: actually, it's different anyway because we don't use vcruntime for releases
  118. :: any more. see comment in wincrt.c
  119. if "%dbg%"=="1" (
  120. set clibs=-lmsvcrtd -lvcruntimed -lucrtd
  121. ) else (
  122. set clibs=-lucrt
  123. )
  124. %CC% -fuse-ld=lld -shared -flto %ldflags% -Wl,/IMPLIB:.build/sst.lib,/Brepro,/nodefaultlib ^
  125. -L.build %clibs% -lkernel32 -luser32 -lbcryptprimitives -lshlwapi -ld3d9 -ldsound ^
  126. -ltier0 -lvstdlib -lntdll -o .build/sst.dll%objs% .build/dll.res || goto :end
  127. :: get rid of another useless file (can we just not create this???)
  128. del .build\sst.lib
  129. :: awkward logic to replace sst.dll while it's potentially loaded, because
  130. :: windows likes to lock things and/or doesn't have atomic rename.
  131. :: very TOCTOU-ish code, not good at all, but sometimes bad is good enough
  132. move /y .build\sst.dll sst.dll >nul 2>nul || (
  133. move /y sst.dll .build\sst.old.dll >nul 2>nul || (
  134. echo:ERROR: couldn't remove sst.dll OR .build/sst.old.dll - are both loaded?>&2
  135. goto :end
  136. )
  137. move .build\sst.dll sst.dll >nul 2>nul || (
  138. echo:ERROR: couldn't replace sst.dll - did something just try to load it?>&2
  139. goto :end
  140. )
  141. )
  142. :: try to cleanup the .old again - it'll fail if we just moved it but may work
  143. :: in some eventual future invocation
  144. if exist .build\sst.old.dll del .build\sst.old.dll >nul 2>nul
  145. %HOSTCC% -fuse-ld=lld -O2 -g %warnings% %stdflags% -include test/test.h -o .build/bitbuf.test.exe test/bitbuf.test.c || goto :end
  146. .build\bitbuf.test.exe || goto :end
  147. :: special case: test must be 32-bit
  148. %HOSTCC% -fuse-ld=lld -m32 -O2 -g %warnings% %stdflags% -L.build -lbcryptprimitives -include test/test.h -o .build/hook.test.exe test/hook.test.c || goto :end
  149. .build\hook.test.exe || goto :end
  150. %HOSTCC% -fuse-ld=lld -O2 -g %warnings% %stdflags% -include test/test.h -o .build/x86.test.exe test/x86.test.c || goto :end
  151. .build\x86.test.exe || goto :end
  152. :end
  153. exit /b %errorlevel%
  154. :: vi: sw=4 tw=4 noet tw=80 cc=80