v / makev.bat
517 lines · 455 sloc · 14.09 KB · c590ffe8f1fb1501c15a16e6ea33e6fda8524f97
Raw
1@setlocal EnableDelayedExpansion EnableExtensions
2
3@IF NOT DEFINED VERBOSE_MAKE @echo off
4
5REM Option flags
6set /a shift_counter=0
7set /a flag_local=0
8
9REM Option variables
10set compiler=
11set subcmd=
12set target=build
13
14set V_EXE=./v.exe
15set V_BOOTSTRAP=./v_win_bootstrap.exe
16set V_OLD=./v_old.exe
17set V_UPDATED=./v_up.exe
18set V_STAGE=./v_stage.exe
19set V_C_FILE=./vc/v_win.c
20REM Existing vc bootstraps may predate the TCC Win64 CRT prelude fix, so keep
21REM their cgen single-threaded while they build a fresh compiler from sources.
22REM TODO: remove this after vc/v_win.c is regenerated with the fixed CRT prelude.
23set V_BOOTSTRAP_VFLAGS=-no-parallel
24set where_exe=where.exe
25if not ["%SystemRoot%"] == [""] if exist "%SystemRoot%\System32\where.exe" set where_exe=%SystemRoot%\System32\where.exe
26
27REM TCC variables
28set tcc_url=https://github.com/vlang/tccbin
29set tcc_dir=%~dp0thirdparty\tcc
30set tcc_exe=%tcc_dir%\tcc.exe
31if "%PROCESSOR_ARCHITECTURE%" == "x86" ( set tcc_branch="thirdparty-windows-i386" ) else ( set tcc_branch="thirdparty-windows-amd64" )
32if "%~1" == "-tcc32" set tcc_branch="thirdparty-windows-i386"
33
34REM VC settings
35set vc_url=https://github.com/vlang/vc
36set vc_dir=%~dp0vc
37
38REM Let a particular environment specify their own TCC and VC repos (to help mirrors)
39if /I not ["%TCC_GIT%"] == [""] set tcc_url=%TCC_GIT%
40if /I not ["%TCC_BRANCH%"] == [""] set tcc_branch=%TCC_BRANCH%
41
42if /I not ["%VC_GIT%"] == [""] set vc_url=%VC_GIT%
43
44pushd "%~dp0"
45
46:verifyopt
47REM Read stdin EOF
48if ["%~1"] == [""] goto :init
49
50REM Target options
51if !shift_counter! LSS 1 (
52 if "%~1" == "help" (
53 if not ["%~2"] == [""] set subcmd=%~2& shift& set /a shift_counter+=1
54 )
55 for %%z in (build clean cleanall check help rebuild) do (
56 if "%~1" == "%%z" set target=%1& shift& set /a shift_counter+=1& goto :verifyopt
57 )
58)
59
60REM Compiler option
61for %%g in (-gcc -msvc -tcc -tcc32 -clang) do (
62 if "%~1" == "%%g" set compiler=%~1& set compiler=!compiler:~1!& shift& set /a shift_counter+=1& goto :verifyopt
63)
64
65REM Standard options
66if "%~1" == "--local" (
67 if !flag_local! NEQ 0 (
68 echo The flag %~1 has already been specified. 1>&2
69 exit /b 2
70 )
71 set /a flag_local=1
72 set /a shift_counter+=1
73 shift
74 goto :verifyopt
75)
76
77echo Undefined option: %~1
78exit /b 2
79
80:init
81goto :!target!
82
83:check
84echo.
85echo Check everything
86"%V_EXE%" test-all
87exit /b 0
88
89:cleanall
90call :clean
91if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
92echo.
93echo Cleanup vc
94echo ^> Purge TCC binaries
95if exist "%tcc_dir%" (
96 rmdir /s /q "%tcc_dir%"
97 if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
98)
99echo ^> Purge vc repository
100if exist "%vc_dir%" (
101 rmdir /s /q "%vc_dir%"
102 if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
103)
104exit /b 0
105
106:clean
107echo Cleanup build artifacts
108echo ^> Purge debug symbols
109del *.pdb *.lib *.bak *.out *.ilk *.exp *.obj *.o *.a *.so
110
111echo ^> Delete old V executable(s)
112del v*.exe
113exit /b 0
114
115:rebuild
116call :cleanall
117if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
118goto :build
119
120:help
121if [!subcmd!] == [] (
122 call :usage
123) else (
124 call :help_!subcmd!
125)
126if !ERRORLEVEL! NEQ 0 echo Invalid subcommand: !subcmd!
127exit /b !ERRORLEVEL!
128
129:build
130if !flag_local! NEQ 1 (
131 call :download_tcc
132 if !ERRORLEVEL! NEQ 0 goto :error
133 if exist "%vc_dir%" (
134 pushd "%vc_dir%"
135 if !ERRORLEVEL! NEQ 0 goto :error
136 echo Updating vc...
137 echo ^> Sync with remote !vc_url!
138 git pull --rebase --quiet
139 if !ERRORLEVEL! NEQ 0 (
140 popd
141 goto :error
142 )
143 popd
144 ) else (
145 call :cloning_vc
146 if !ERRORLEVEL! NEQ 0 goto :error
147 )
148 echo.
149)
150
151echo Building V...
152if not [!compiler!] == [] goto :!compiler!_strap
153
154
155REM By default, use tcc, since we have it prebuilt:
156:tcc_strap
157:tcc32_strap
158call :build_bootstrap_with_tcc
159if !ERRORLEVEL! NEQ 0 goto :compile_error
160call :build_fresh_v_with_tcc
161if !ERRORLEVEL! NEQ 0 goto :tcc_retry_with_host_bootstrap
162call :move_updated_to_v
163goto :success
164
165:tcc_retry_with_host_bootstrap
166echo ^> TCC-built bootstrap failed; retrying bootstrap with Clang/GCC before compiling "%V_EXE%" with TCC
167call :build_bootstrap_with_clang
168if !ERRORLEVEL! NEQ 0 call :build_bootstrap_with_gcc
169if !ERRORLEVEL! NEQ 0 (
170 if [!compiler!] == [] goto :clang_strap
171 goto :compile_error
172)
173call :build_fresh_v_with_tcc
174if !ERRORLEVEL! NEQ 0 (
175 if [!compiler!] == [] goto :clang_strap
176 goto :compile_error
177)
178call :move_updated_to_v
179goto :success
180
181:build_fresh_v_with_tcc
182echo ^> Compiling "%V_STAGE%" with "%V_BOOTSTRAP%"
183REM Keep the TCC root relative here; V forwards -cflags through a response file.
184REM An absolute -B path breaks there when the checkout path contains spaces.
185"%V_BOOTSTRAP%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc -cc "!tcc_exe!" -cflags -Bthirdparty/tcc -o "%V_STAGE%" cmd/v
186set stage_error=!ERRORLEVEL!
187if !stage_error! NEQ 0 (
188 if exist "%V_STAGE%" del "%V_STAGE%"
189 exit /b !stage_error!
190)
191echo ^> Compiling "%V_EXE%" with "%V_STAGE%"
192"%V_STAGE%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc -cc "!tcc_exe!" -cflags -Bthirdparty/tcc -o "%V_UPDATED%" cmd/v
193set stage_error=!ERRORLEVEL!
194if exist "%V_STAGE%" del "%V_STAGE%"
195exit /b !stage_error!
196
197:clang_strap
198call :build_bootstrap_with_clang
199if !ERRORLEVEL! NEQ 0 (
200 if not [!compiler!] == [] goto :error
201 goto :gcc_strap
202)
203
204echo ^> Compiling "%V_EXE%" with "%V_BOOTSTRAP%"
205"%V_BOOTSTRAP%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc -cc clang -cflags "--target=!clang_target!" -o "%V_UPDATED%" cmd/v
206if !ERRORLEVEL! NEQ 0 goto :compile_error
207call :move_updated_to_v
208goto :success
209
210:gcc_strap
211call :build_bootstrap_with_gcc
212if !ERRORLEVEL! NEQ 0 (
213 if not [!compiler!] == [] goto :error
214 goto :msvc_strap
215)
216
217echo ^> Compiling "%V_EXE%" with "%V_BOOTSTRAP%"
218"%V_BOOTSTRAP%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc -cc "!gcc_exe!" -o "%V_UPDATED%" cmd/v
219if !ERRORLEVEL! NEQ 0 goto :compile_error
220call :move_updated_to_v
221goto :success
222
223:msvc_strap
224set VsWhereDir=%ProgramFiles(x86)%
225set HostArch=x64
226if "%PROCESSOR_ARCHITECTURE%" == "x86" (
227 echo Using x86 Build Tools...
228 set VsWhereDir=%ProgramFiles%
229 set HostArch=x86
230)
231
232if not exist "%VsWhereDir%/Microsoft Visual Studio/Installer/vswhere.exe" (
233 echo ^> MSVC not found
234 if not [!compiler!] == [] goto :error
235 goto :compile_error
236)
237
238for /f "usebackq tokens=*" %%i in (`"%VsWhereDir%/Microsoft Visual Studio/Installer/vswhere.exe" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
239 set InstallDir=%%i
240)
241
242if exist "%InstallDir%/Common7/Tools/vsdevcmd.bat" (
243 call "%InstallDir%/Common7/Tools/vsdevcmd.bat" -arch=%HostArch% -host_arch=%HostArch% -no_logo
244) else if exist "%VsWhereDir%/Microsoft Visual Studio 14.0/Common7/Tools/vsdevcmd.bat" (
245 call "%VsWhereDir%/Microsoft Visual Studio 14.0/Common7/Tools/vsdevcmd.bat" -arch=%HostArch% -host_arch=%HostArch% -no_logo
246)
247
248set ObjFile=.v.c.obj
249
250echo ^> Bootstrapping "%V_BOOTSTRAP%" before compiling "%V_EXE%" with MSVC
251set stage_vflags=
252call :build_bootstrap_with_clang
253if !ERRORLEVEL! EQU 0 (
254 set stage_vflags=-cc clang -cflags "--target=!clang_target!"
255) else (
256 call :build_bootstrap_with_gcc
257 if !ERRORLEVEL! EQU 0 (
258 set stage_vflags=-cc "!gcc_exe!"
259 ) else (
260 call :build_bootstrap_with_tcc
261 if !ERRORLEVEL! EQU 0 set stage_vflags=-cc "!tcc_exe!" -cflags -Bthirdparty/tcc
262 )
263)
264if not defined stage_vflags (
265 echo Could not build a bootstrap compiler before compiling with MSVC
266 if exist %ObjFile% del %ObjFile%
267 goto :compile_error
268)
269
270echo ^> Compiling "%V_STAGE%" with "%V_BOOTSTRAP%"
271"%V_BOOTSTRAP%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc !stage_vflags! -o "%V_STAGE%" cmd/v
272if !ERRORLEVEL! NEQ 0 (
273 if exist %ObjFile% del %ObjFile%
274 if exist "%V_STAGE%" del "%V_STAGE%"
275 goto :compile_error
276)
277
278echo ^> Compiling "%V_EXE%" with "%V_STAGE%"
279"%V_STAGE%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc -cc msvc -o "%V_UPDATED%" cmd/v
280set msvc_error=!ERRORLEVEL!
281if exist %ObjFile% del %ObjFile%
282if exist "%V_STAGE%" del "%V_STAGE%"
283if %msvc_error% NEQ 0 goto :compile_error
284call :move_updated_to_v
285goto :success
286
287:download_tcc
288if exist "%tcc_dir%" (
289 pushd "%tcc_dir%"
290 if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
291 echo Updating TCC
292 echo ^> Syncing TCC from !tcc_url!
293 if exist "lib\advapi32.def" git checkout -- lib\advapi32.def >nul 2>nul
294 git pull --rebase --quiet
295 if !ERRORLEVEL! NEQ 0 (
296 set tcc_update_error=!ERRORLEVEL!
297 popd
298 exit /b !tcc_update_error!
299 )
300 popd
301) else (
302 call :bootstrap_tcc
303 if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
304)
305
306call :patch_tcc_defs
307if !ERRORLEVEL! NEQ 0 goto :error
308
309if not exist "%tcc_exe%" echo ^> TCC not found, even after cloning& goto :error
310echo.
311exit /b 0
312
313:patch_tcc_defs
314set "advapi32_def=%tcc_dir%\lib\advapi32.def"
315if not exist "%advapi32_def%" exit /b 0
316for %%G in (RegEnumKeyExW RegEnumValueW RegQueryInfoKeyW) do (
317 findstr /x /c:"%%G" "%advapi32_def%" >nul || >>"%advapi32_def%" echo %%G
318)
319exit /b 0
320
321:compile_error
322echo.
323echo Backend compiler error
324goto :error
325
326:error
327echo.
328echo Exiting from error
329echo ERROR: please follow the instructions in https://github.com/vlang/v/wiki/Installing-a-C-compiler-on-Windows
330exit /b 1
331
332:success
333"%V_EXE%" run cmd/tools/detect_tcc.v
334echo ^> V built successfully!
335echo ^> To add V to your PATH, run `%V_EXE% symlink`.
336echo ^> Note: Antivirus programs may sometimes tell you there is a virus in V (there aren't any). They can also slow compilation by a considerable amount. Consider adding exemptions for the V install directory as well as your V project folders.
337
338:version
339echo.
340echo | set /p="V version: "
341"%V_EXE%" version
342"%V_EXE%" run .github/problem-matchers/register_all.vsh
343goto :eof
344
345:usage
346echo Usage:
347echo makev.bat [target] [compiler] [options]
348echo.
349echo Compiler:
350echo -msvc ^| -gcc ^| -tcc ^| -tcc32 ^| -clang Set C compiler
351echo.
352echo Target:
353echo build[default] Compiles V using the given C compiler
354echo clean Clean build artifacts and debugging symbols
355echo cleanall Cleanup entire ALL build artifacts and vc repository
356echo check Check that tests pass, and the repository is in a good shape for Pull Requests
357echo help Display help for the given target
358echo rebuild Fully clean/reset repository and rebuild V
359echo.
360echo Examples:
361echo makev.bat -msvc
362echo makev.bat -gcc --local
363echo makev.bat build -tcc --local
364echo makev.bat -tcc32
365echo makev.bat help clean
366echo.
367echo Use "make help <target>" for more information about a target, for instance: "make help clean"
368echo.
369echo Note: Any undefined/unsupported options will be ignored
370exit /b 0
371
372:help_help
373echo Usage:
374echo makev.bat help [target]
375echo.
376echo Target:
377echo build ^| clean ^| cleanall ^| help Query given target
378exit /b 0
379
380:help_clean
381echo Usage:
382echo makev.bat clean
383echo.
384exit /b 0
385
386:help_cleanall
387echo Usage:
388echo makev.bat cleanall
389echo.
390exit /b 0
391
392:help_build
393echo Usage:
394echo makev.bat build [compiler] [options]
395echo.
396echo Compiler:
397echo -msvc ^| -gcc ^| -tcc ^| -tcc32 ^| -clang Set C compiler
398echo.
399echo Options:
400echo --local Use the local vc repository without
401echo syncing with remote
402exit /b 0
403
404:help_rebuild
405echo Usage:
406echo makev.bat rebuild [compiler] [options]
407echo.
408echo Compiler:
409echo -msvc ^| -gcc ^| -tcc ^| -tcc32 ^| -clang Set C compiler
410echo.
411echo Options:
412echo --local Use the local vc repository without
413echo syncing with remote
414exit /b 0
415
416:bootstrap_tcc
417echo Bootstrapping TCC...
418echo ^> TCC not found
419if "!tcc_branch!" == "thirdparty-windows-i386" ( echo ^> Downloading TCC32 from !tcc_url! , branch !tcc_branch! ) else ( echo ^> Downloading TCC64 from !tcc_url! , branch !tcc_branch! )
420git clone --filter=blob:none --quiet --branch !tcc_branch! !tcc_url! "%tcc_dir%"
421if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
422git --no-pager -C "%tcc_dir%" log -n3
423exit /b !ERRORLEVEL!
424
425:cloning_vc
426echo Cloning vc...
427echo ^> Cloning from remote !vc_url!
428git clone --filter=blob:none --quiet "%vc_url%"
429exit /b !ERRORLEVEL!
430
431:build_bootstrap_with_tcc
432if not exist "!tcc_exe!" (
433 echo ^> TCC not found
434 exit /b 1
435)
436echo ^> Attempting to build "%V_BOOTSTRAP%" (from %V_C_FILE%) with "!tcc_exe!"
437"!tcc_exe!" -B"%tcc_dir%" -bt10 -g -w -o "%V_BOOTSTRAP%" "%V_C_FILE%" -ladvapi32 -lws2_32 -Wl,-stack=33554432
438exit /b !ERRORLEVEL!
439
440:build_bootstrap_with_clang
441"%where_exe%" /q clang
442if !ERRORLEVEL! NEQ 0 (
443 echo ^> Clang not found
444 exit /b 1
445)
446if "%PROCESSOR_ARCHITECTURE%" == "x86" ( set clang_target=i686-w64-mingw32 ) else ( set clang_target=x86_64-w64-mingw32 )
447echo ^> Attempting to build "%V_BOOTSTRAP%" (from %V_C_FILE%) with Clang
448clang --target=!clang_target! -std=c99 -municode -g -w -Wno-error=implicit-function-declaration -Wno-error=incompatible-function-pointer-types -o "%V_BOOTSTRAP%" "%V_C_FILE%" -ladvapi32 -lws2_32 -Wl,-stack=33554432
449if !ERRORLEVEL! NEQ 0 (
450 echo In most cases, compile errors happen because the version of Clang installed is too old
451 clang --version
452 exit /b 1
453)
454exit /b 0
455
456:build_bootstrap_with_gcc
457call :find_gcc_exe
458if !ERRORLEVEL! NEQ 0 (
459 echo ^> GCC not found
460 exit /b 1
461)
462echo ^> Attempting to build "%V_BOOTSTRAP%" (from %V_C_FILE%) with GCC "!gcc_exe!"
463"!gcc_exe!" -std=c99 -municode -g -w -o "%V_BOOTSTRAP%" "%V_C_FILE%" -ladvapi32 -lws2_32 -Wl,-stack=33554432
464if !ERRORLEVEL! NEQ 0 (
465 echo In most cases, compile errors happen because the version of GCC installed is too old
466 "!gcc_exe!" --version
467 exit /b 1
468)
469exit /b 0
470
471:find_gcc_exe
472REM Prefer MinGW target-prefixed drivers when present, so PATH conflicts do not pick an unrelated gcc.exe.
473set "gcc_exe="
474if "%PROCESSOR_ARCHITECTURE%" == "x86" (
475 call :resolve_executable i686-w64-mingw32-gcc
476 if not [!resolved_exe!] == [] (
477 set "gcc_exe=!resolved_exe!"
478 exit /b 0
479 )
480) else (
481 call :resolve_executable x86_64-w64-mingw32-gcc
482 if not [!resolved_exe!] == [] (
483 set "gcc_exe=!resolved_exe!"
484 exit /b 0
485 )
486)
487call :resolve_executable gcc
488if not [!resolved_exe!] == [] (
489 set "gcc_exe=!resolved_exe!"
490 exit /b 0
491)
492exit /b 1
493
494:resolve_executable
495set "resolved_exe="
496for %%i in ("%~1") do if exist "%%~fi" (
497 set "resolved_exe=%%~fi"
498 exit /b 0
499)
500for /f "usebackq delims=" %%i in (`where "%~1" 2^>nul`) do (
501 set "resolved_exe=%%~fi"
502 exit /b 0
503)
504exit /b 1
505
506:eof
507popd
508endlocal
509exit /b 0
510
511:move_updated_to_v
512@REM del "%V_EXE%" &:: breaks if `makev.bat` is run from `v up` b/c of held file handle on `%V_EXE%`
513if exist "%V_EXE%" move "%V_EXE%" "%V_OLD%" >nul
514REM sleep for at most 100ms
515ping 192.0.2.1 -n 1 -w 100 >nul
516move "%V_UPDATED%" "%V_EXE%" >nul
517exit /b 0
518