-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathmakev.bat
More file actions
641 lines (574 loc) · 19.9 KB
/
Copy pathmakev.bat
File metadata and controls
641 lines (574 loc) · 19.9 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
@setlocal EnableDelayedExpansion EnableExtensions
@IF NOT DEFINED VERBOSE_MAKE @echo off
REM Option flags
set /a shift_counter=0
set /a flag_local=0
REM Option variables
set compiler=
set subcmd=
set target=build
set V_EXE=./v.exe
set V_BOOTSTRAP=./v_win_bootstrap.exe
set V_OLD=./v_old.exe
set V_UPDATED=./v_up.exe
set V_STAGE=./v_stage.exe
set V_STAGE_C=./v_stage.c
set V_C_FILE=./vc/v_win.c
REM Portable vc snapshots need the full V1 compiler until the new driver is built.
REM Keep this aligned with VC_BOOTSTRAP_DEFINE in the Unix makefiles.
set VC_BOOTSTRAP_DEFINE=-DCUSTOM_DEFINE_v1_fallback
REM Existing vc bootstraps may predate the TCC Win64 CRT prelude fix, so keep
REM their cgen single-threaded while they build a fresh compiler from sources.
REM TODO: remove this after vc/v_win.c is regenerated with the fixed CRT prelude.
set V_BOOTSTRAP_VFLAGS=-no-parallel
set where_exe=where.exe
if not ["%SystemRoot%"] == [""] if exist "%SystemRoot%\System32\where.exe" set where_exe=%SystemRoot%\System32\where.exe
REM TCC variables
set tcc_url=https://github.com/vlang/tccbin
set tcc_dir=%~dp0thirdparty\tcc
set tcc_exe=%tcc_dir%\tcc.exe
if "%PROCESSOR_ARCHITECTURE%" == "x86" ( set tcc_branch="thirdparty-windows-i386" ) else ( set tcc_branch="thirdparty-windows-amd64" )
if "%~1" == "-tcc32" set tcc_branch="thirdparty-windows-i386"
REM VC settings
set vc_url=https://github.com/vlang/vc
set vc_dir=%~dp0vc
REM Let a particular environment specify their own TCC and VC repos (to help mirrors)
if /I not ["%TCC_GIT%"] == [""] set tcc_url=%TCC_GIT%
if /I not ["%TCC_BRANCH%"] == [""] set tcc_branch=%TCC_BRANCH%
if /I not ["%VC_GIT%"] == [""] set vc_url=%VC_GIT%
pushd "%~dp0"
:verifyopt
REM Read stdin EOF
if ["%~1"] == [""] goto :init
REM Target options
if !shift_counter! LSS 1 (
if "%~1" == "help" (
if not ["%~2"] == [""] set subcmd=%~2& shift& set /a shift_counter+=1
)
for %%z in (build clean cleanall check help latest_tcc rebuild) do (
if "%~1" == "%%z" set target=%1& shift& set /a shift_counter+=1& goto :verifyopt
)
)
REM Compiler option
for %%g in (-gcc -msvc -tcc -tcc32 -clang) do (
if "%~1" == "%%g" set compiler=%~1& set compiler=!compiler:~1!& shift& set /a shift_counter+=1& goto :verifyopt
)
REM Standard options
if "%~1" == "--local" (
if !flag_local! NEQ 0 (
echo The flag %~1 has already been specified. 1>&2
exit /b 2
)
set /a flag_local=1
set /a shift_counter+=1
shift
goto :verifyopt
)
echo Undefined option: %~1
exit /b 2
:init
goto :!target!
:check
echo.
echo Check everything
"%V_EXE%" test-all
exit /b !ERRORLEVEL!
:cleanall
call :clean
if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
echo.
echo Cleanup vc
echo ^> Purge TCC binaries
if exist "%tcc_dir%" (
rmdir /s /q "%tcc_dir%"
if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
)
echo ^> Purge vc repository
if exist "%vc_dir%" (
rmdir /s /q "%vc_dir%"
if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
)
exit /b 0
:clean
echo Cleanup build artifacts
echo ^> Purge debug symbols
del *.pdb *.lib *.bak *.out *.ilk *.exp *.obj *.o *.a *.so
echo ^> Delete old V executable(s)
del v*.exe
exit /b 0
:rebuild
call :cleanall
if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
goto :build
:latest_tcc
call :download_tcc
if !ERRORLEVEL! NEQ 0 goto :error
exit /b 0
:help
if [!subcmd!] == [] (
call :usage
) else (
call :help_!subcmd!
)
if !ERRORLEVEL! NEQ 0 echo Invalid subcommand: !subcmd!
exit /b !ERRORLEVEL!
:build
if !flag_local! NEQ 1 (
call :download_tcc
if !ERRORLEVEL! NEQ 0 goto :error
if exist "%vc_dir%" (
pushd "%vc_dir%"
if !ERRORLEVEL! NEQ 0 goto :error
echo Updating vc...
echo ^> Sync with remote !vc_url!
git pull --rebase --quiet
if !ERRORLEVEL! NEQ 0 (
popd
goto :error
)
popd
) else (
call :cloning_vc
if !ERRORLEVEL! NEQ 0 goto :error
)
echo.
)
echo Building V...
if not [!compiler!] == [] goto :!compiler!_strap
REM By default, use tcc, since we have it prebuilt:
:tcc_strap
:tcc32_strap
call :build_bootstrap_with_tcc
if !ERRORLEVEL! NEQ 0 goto :compile_error
call :build_fresh_v_with_tcc
if !ERRORLEVEL! NEQ 0 goto :tcc_retry_with_host_bootstrap
call :move_updated_to_v
if !ERRORLEVEL! NEQ 0 goto :compile_error
goto :success
:tcc_retry_with_host_bootstrap
echo ^> TCC-built bootstrap failed; retrying bootstrap with Clang/GCC before compiling "%V_EXE%" with TCC
call :build_bootstrap_with_clang
if !ERRORLEVEL! NEQ 0 call :build_bootstrap_with_gcc
if !ERRORLEVEL! NEQ 0 (
if [!compiler!] == [] goto :clang_strap
goto :compile_error
)
call :build_fresh_v_with_tcc
if !ERRORLEVEL! NEQ 0 (
if [!compiler!] == [] goto :clang_strap
goto :compile_error
)
call :move_updated_to_v
if !ERRORLEVEL! NEQ 0 goto :compile_error
goto :success
:build_fresh_v_with_tcc
echo ^> Compiling "%V_STAGE%" with "%V_BOOTSTRAP%"
REM V3 supplies the absolute bundled-TCC root itself. A relative -B here would
REM override it after V3 changes into its isolated link directory.
"%V_BOOTSTRAP%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc -cc "!tcc_exe!" -o "%V_STAGE%" cmd/v
set stage_error=!ERRORLEVEL!
if !stage_error! NEQ 0 (
call :try_delete "%V_STAGE%"
exit /b !stage_error!
)
echo ^> Compiling "%V_EXE%" with "%V_STAGE%"
REM V3 supplies the absolute bundled-TCC root itself. A relative -B here would
REM override it after V3 changes into its isolated link directory.
"%V_STAGE%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc -cc "!tcc_exe!" -o "%V_UPDATED%" cmd/v
set stage_error=!ERRORLEVEL!
call :try_delete "%V_STAGE%"
exit /b !stage_error!
:clang_strap
call :build_bootstrap_with_clang
if !ERRORLEVEL! NEQ 0 (
if not [!compiler!] == [] goto :error
goto :gcc_strap
)
call :build_stage_with_clang
if !ERRORLEVEL! NEQ 0 goto :compile_error
echo ^> Compiling "%V_EXE%" with "%V_STAGE%"
"%V_STAGE%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc -cc "!clang_exe!" -cflags "--target=!clang_target!" -o "%V_UPDATED%" cmd/v
set stage_error=!ERRORLEVEL!
call :try_delete "%V_STAGE%"
if !stage_error! NEQ 0 goto :compile_error
call :move_updated_to_v
if !ERRORLEVEL! NEQ 0 goto :compile_error
goto :success
:gcc_strap
call :build_bootstrap_with_gcc
if !ERRORLEVEL! NEQ 0 (
if not [!compiler!] == [] goto :error
goto :msvc_strap
)
echo ^> Compiling "%V_EXE%" with "%V_BOOTSTRAP%"
"%V_BOOTSTRAP%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc -cc "!gcc_exe!" -o "%V_UPDATED%" cmd/v
if !ERRORLEVEL! NEQ 0 goto :compile_error
call :move_updated_to_v
if !ERRORLEVEL! NEQ 0 goto :compile_error
goto :success
:msvc_strap
set VsWhereDir=%ProgramFiles(x86)%
set HostArch=x64
if "%PROCESSOR_ARCHITECTURE%" == "x86" (
echo Using x86 Build Tools...
set VsWhereDir=%ProgramFiles%
set HostArch=x86
)
if not exist "%VsWhereDir%/Microsoft Visual Studio/Installer/vswhere.exe" (
echo ^> MSVC not found
if not [!compiler!] == [] goto :error
goto :compile_error
)
for /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 (
set InstallDir=%%i
)
if exist "%InstallDir%/Common7/Tools/vsdevcmd.bat" (
call "%InstallDir%/Common7/Tools/vsdevcmd.bat" -arch=%HostArch% -host_arch=%HostArch% -no_logo
) else if exist "%VsWhereDir%/Microsoft Visual Studio 14.0/Common7/Tools/vsdevcmd.bat" (
call "%VsWhereDir%/Microsoft Visual Studio 14.0/Common7/Tools/vsdevcmd.bat" -arch=%HostArch% -host_arch=%HostArch% -no_logo
)
set ObjFile=.v.c.obj
echo ^> Bootstrapping "%V_BOOTSTRAP%" before compiling "%V_EXE%" with MSVC
set stage_vflags=
set stage_with_clang=0
REM Bootstrap order for -msvc: bundled TCC first (fastest, no external
REM toolchain needed), then MSVC itself (already confirmed present -
REM vsdevcmd.bat has already run above - and explicitly what -msvc asked
REM for), then Clang, then GCC as the remaining fallbacks. Any of these can
REM fail to compile vc/v_win.c on its own (e.g. a stale snapshot only some
REM toolchains can parse - see vlang/v#29025 and vlang/tccbin#96), which is
REM why every option here is still tried in order rather than stopping at
REM the first choice.
call :build_bootstrap_with_tcc
if !ERRORLEVEL! EQU 0 (
set stage_vflags=-cc "!tcc_exe!"
) else (
call :build_bootstrap_with_msvc
if !ERRORLEVEL! EQU 0 (
set stage_vflags=-cc msvc
) else (
call :build_bootstrap_with_clang
if !ERRORLEVEL! EQU 0 (
set stage_vflags=-cc "!clang_exe!" -cflags "--target=!clang_target!"
set stage_with_clang=1
) else (
call :build_bootstrap_with_gcc
if !ERRORLEVEL! EQU 0 set stage_vflags=-cc "!gcc_exe!"
)
)
)
if not defined stage_vflags (
echo Could not build a bootstrap compiler before compiling with MSVC
call :try_delete "%ObjFile%"
goto :compile_error
)
if !stage_with_clang! EQU 1 (
call :build_stage_with_clang
) else (
echo ^> Compiling "%V_STAGE%" with "%V_BOOTSTRAP%"
"%V_BOOTSTRAP%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc !stage_vflags! -o "%V_STAGE%" cmd/v
)
if !ERRORLEVEL! NEQ 0 (
call :try_delete "%ObjFile%"
call :try_delete "%V_STAGE%"
goto :compile_error
)
echo ^> Compiling "%V_EXE%" with "%V_STAGE%"
"%V_STAGE%" %V_BOOTSTRAP_VFLAGS% -keepc -g -showcc -cc msvc -o "%V_UPDATED%" cmd/v
set msvc_error=!ERRORLEVEL!
call :try_delete "%ObjFile%"
call :try_delete "%V_STAGE%"
if %msvc_error% NEQ 0 goto :compile_error
call :move_updated_to_v
if !ERRORLEVEL! NEQ 0 goto :compile_error
goto :success
:download_tcc
if exist "%tcc_dir%" (
pushd "%tcc_dir%"
if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
echo Updating TCC
echo ^> Syncing TCC from !tcc_url!
if exist "lib\advapi32.def" git checkout -- lib\advapi32.def >nul 2>nul
git pull --rebase --quiet
if !ERRORLEVEL! NEQ 0 (
set tcc_update_error=!ERRORLEVEL!
popd
exit /b !tcc_update_error!
)
popd
) else (
call :bootstrap_tcc
if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
)
call :patch_tcc_defs
if !ERRORLEVEL! NEQ 0 goto :error
if not exist "%tcc_exe%" echo ^> TCC not found, even after cloning& goto :error
echo.
exit /b 0
:patch_tcc_defs
set "advapi32_def=%tcc_dir%\lib\advapi32.def"
if not exist "%advapi32_def%" exit /b 0
for %%G in (RegEnumKeyExW RegEnumValueW RegQueryInfoKeyW) do (
findstr /x /c:"%%G" "%advapi32_def%" >nul || >>"%advapi32_def%" echo %%G
)
exit /b 0
:compile_error
echo.
echo Backend compiler error
goto :error
:error
echo.
echo Exiting from error
echo ERROR: please follow the instructions in https://github.com/vlang/v/wiki/Installing-a-C-compiler-on-Windows
exit /b 1
:success
"%V_EXE%" run cmd/tools/detect_tcc.v
if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
echo ^> V built successfully!
echo ^> To add V to your PATH, run `%V_EXE% symlink`.
echo ^> 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.
:version
echo.
echo | set /p="V version: "
"%V_EXE%" version
if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
"%V_EXE%" run .github/problem-matchers/register_all.vsh
if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
goto :eof
:usage
echo Usage:
echo makev.bat [target] [compiler] [options]
echo.
echo Compiler:
echo -msvc ^| -gcc ^| -tcc ^| -tcc32 ^| -clang Set C compiler
echo.
echo Target:
echo build[default] Compiles V using the given C compiler
echo clean Clean build artifacts and debugging symbols
echo cleanall Cleanup entire ALL build artifacts and vc repository
echo check Check that tests pass, and the repository is in a good shape for Pull Requests
echo help Display help for the given target
echo latest_tcc Update the bundled TCC without rebuilding V
echo rebuild Fully clean/reset repository and rebuild V
echo.
echo Examples:
echo makev.bat -msvc
echo makev.bat -gcc --local
echo makev.bat build -tcc --local
echo makev.bat -tcc32
echo makev.bat help clean
echo.
echo Use "make help <target>" for more information about a target, for instance: "make help clean"
echo.
echo Note: Any undefined/unsupported options will be ignored
exit /b 0
:help_help
echo Usage:
echo makev.bat help [target]
echo.
echo Target:
echo build ^| clean ^| cleanall ^| help Query given target
exit /b 0
:help_clean
echo Usage:
echo makev.bat clean
echo.
exit /b 0
:help_cleanall
echo Usage:
echo makev.bat cleanall
echo.
exit /b 0
:help_build
echo Usage:
echo makev.bat build [compiler] [options]
echo.
echo Compiler:
echo -msvc ^| -gcc ^| -tcc ^| -tcc32 ^| -clang Set C compiler
echo.
echo Options:
echo --local Use the local vc repository without
echo syncing with remote
exit /b 0
:help_latest_tcc
echo Usage:
echo makev.bat latest_tcc
echo.
exit /b 0
:help_rebuild
echo Usage:
echo makev.bat rebuild [compiler] [options]
echo.
echo Compiler:
echo -msvc ^| -gcc ^| -tcc ^| -tcc32 ^| -clang Set C compiler
echo.
echo Options:
echo --local Use the local vc repository without
echo syncing with remote
exit /b 0
:bootstrap_tcc
echo Bootstrapping TCC...
echo ^> TCC not found
if "!tcc_branch!" == "thirdparty-windows-i386" ( echo ^> Downloading TCC32 from !tcc_url! , branch !tcc_branch! ) else ( echo ^> Downloading TCC64 from !tcc_url! , branch !tcc_branch! )
git clone --filter=blob:none --quiet --branch !tcc_branch! !tcc_url! "%tcc_dir%"
if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
git --no-pager -C "%tcc_dir%" log -n3
exit /b !ERRORLEVEL!
:cloning_vc
echo Cloning vc...
echo ^> Cloning from remote !vc_url!
git clone --filter=blob:none --quiet "%vc_url%"
exit /b !ERRORLEVEL!
:build_bootstrap_with_tcc
if not exist "!tcc_exe!" (
echo ^> TCC not found
exit /b 1
)
echo ^> Attempting to build "%V_BOOTSTRAP%" (from %V_C_FILE%) with "!tcc_exe!"
"!tcc_exe!" %VC_BOOTSTRAP_DEFINE% -B"%tcc_dir%" -bt10 -g -w -o "%V_BOOTSTRAP%" "%V_C_FILE%" -ladvapi32 -lws2_32 -lbcrypt -Wl,-stack=33554432
exit /b !ERRORLEVEL!
:build_bootstrap_with_msvc
REM Only reachable from msvc_strap, where vsdevcmd.bat has already put cl.exe
REM on PATH and MSVC's presence is already confirmed. Tried right after the
REM bundled TCC there, since -msvc means MSVC was explicitly requested.
REM Clang/GCC are the remaining fallbacks for when neither TCC nor cl.exe can
REM compile %V_C_FILE% (e.g. a stale vc/v_win.c snapshot that only some
REM toolchains can parse - see vlang/v#29025 and vlang/tccbin#96 for two
REM concrete cases).
where cl >nul 2>&1
if !ERRORLEVEL! NEQ 0 (
echo ^> MSVC's cl.exe not found on PATH
exit /b 1
)
echo ^> Attempting to build "%V_BOOTSTRAP%" (from %V_C_FILE%) with MSVC
cl /nologo %VC_BOOTSTRAP_DEFINE% /volatile:ms /bigobj /MD /we4013 /utf-8 /w /std:c11 /D_CRT_DECLARE_NONSTDC_NAMES=1 /Fe"%V_BOOTSTRAP%" "%V_C_FILE%" kernel32.lib user32.lib dbghelp.lib ws2_32.lib bcrypt.lib advapi32.lib /link /STACK:33554432
set msvc_bootstrap_error=!ERRORLEVEL!
call :try_delete "%ObjFile%"
exit /b !msvc_bootstrap_error!
:build_bootstrap_with_clang
call :resolve_executable clang
if [!resolved_exe!] == [] (
echo ^> Clang not found
exit /b 1
)
set "clang_exe=!resolved_exe!"
if "%PROCESSOR_ARCHITECTURE%" == "x86" ( set clang_target=i686-w64-mingw32 ) else ( set clang_target=x86_64-w64-mingw32 )
echo ^> Attempting to build "%V_BOOTSTRAP%" (from %V_C_FILE%) with Clang
"!clang_exe!" %VC_BOOTSTRAP_DEFINE% --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 -lbcrypt -Wl,-stack=33554432
if !ERRORLEVEL! NEQ 0 (
echo In most cases, compile errors happen because the version of Clang installed is too old
"!clang_exe!" --version
exit /b 1
)
exit /b 0
:build_stage_with_clang
echo ^> Generating "%V_STAGE_C%" with "%V_BOOTSTRAP%"
REM This C file is linked directly with MinGW Clang below, even for -msvc builds.
REM Do not inherit a different C dialect or require an unlinked GC library.
"%V_BOOTSTRAP%" %V_BOOTSTRAP_VFLAGS% -gc none -g -cc "!clang_exe!" -cflags "--target=!clang_target!" -o "%V_STAGE_C%" cmd/v
set stage_error=!ERRORLEVEL!
if !stage_error! NEQ 0 exit /b !stage_error!
echo ^> Compiling "%V_STAGE%" from "%V_STAGE_C%" with Clang
"!clang_exe!" --target=!clang_target! -std=gnu11 -municode -g -w -fwrapv -Wno-int-conversion -o "%V_STAGE%" "%V_STAGE_C%" -ldbghelp -lws2_32 -L"%~dp0vlib\crypto\rand\internal\libraries\bcrypt" -lbcrypt -I "%~dp0thirdparty\stdatomic\win" -ladvapi32 -Wl,--stack=33554432
set stage_error=!ERRORLEVEL!
call :try_delete "%V_STAGE_C%"
exit /b !stage_error!
:build_bootstrap_with_gcc
call :find_gcc_exe
if !ERRORLEVEL! NEQ 0 (
echo ^> GCC not found
exit /b 1
)
echo ^> Attempting to build "%V_BOOTSTRAP%" (from %V_C_FILE%) with GCC "!gcc_exe!"
"!gcc_exe!" %VC_BOOTSTRAP_DEFINE% -std=c99 -municode -g -w -o "%V_BOOTSTRAP%" "%V_C_FILE%" -ladvapi32 -lws2_32 -lbcrypt -Wl,-stack=33554432
if !ERRORLEVEL! NEQ 0 (
echo In most cases, compile errors happen because the version of GCC installed is too old
"!gcc_exe!" --version
exit /b 1
)
exit /b 0
:find_gcc_exe
REM Prefer MinGW target-prefixed drivers when present, so PATH conflicts do not pick an unrelated gcc.exe.
set "gcc_exe="
if "%PROCESSOR_ARCHITECTURE%" == "x86" (
call :resolve_executable i686-w64-mingw32-gcc
if not [!resolved_exe!] == [] (
set "gcc_exe=!resolved_exe!"
exit /b 0
)
) else (
call :resolve_executable x86_64-w64-mingw32-gcc
if not [!resolved_exe!] == [] (
set "gcc_exe=!resolved_exe!"
exit /b 0
)
)
call :resolve_executable gcc
if not [!resolved_exe!] == [] (
set "gcc_exe=!resolved_exe!"
exit /b 0
)
exit /b 1
:resolve_executable
set "resolved_exe="
for %%i in ("%~1") do if exist "%%~fi" (
set "resolved_exe=%%~fi"
exit /b 0
)
for /f "usebackq delims=" %%i in (`where "%~1" 2^>nul`) do (
set "resolved_exe=%%~fi"
exit /b 0
)
exit /b 1
:eof
popd
endlocal
exit /b 0
:try_delete
REM Best-effort delete of a just-executed intermediate compiler binary.
REM Windows (or a real-time antivirus scanner) can briefly hold a lock on an
REM exe right after the process running it exits, so retry a couple of times
REM with short waits before giving up silently - a leftover intermediate
REM binary here does not affect build success either way, and the noisy
REM native "Access is denied." message is not worth surfacing to the user.
REM
REM CONTRACT for callers: this always `exit /b 0`, regardless of whether the
REM delete actually succeeded. Every call site MUST capture the real
REM compile/link step's !ERRORLEVEL! into a named variable BEFORE calling
REM this (see the "set msvc_error=!ERRORLEVEL!" / "set stage_error=!ERRORLEVEL!"
REM pattern at its call sites) and check that saved variable afterward, never
REM !ERRORLEVEL! directly after a `call :try_delete` - this always succeeds
REM and would silently swallow a real failure otherwise.
REM
REM KNOWN LIMITATION: cmd/tools/makev_test.py only unit-tests this routine in
REM isolation (BatchExecutionTests.run_try_delete) and only statically greps
REM for %VC_BOOTSTRAP_DEFINE% in build_bootstrap_with_msvc's source text - no
REM test drives msvc_strap/build_bootstrap_with_msvc/build_stage_with_clang
REM through cmd.exe with a real or stubbed failing compiler, so a future edit
REM that violates the contract above would not be caught by the test suite.
REM Found and empirically confirmed (by deliberately reintroducing the bug
REM and observing the full suite still pass) via an adversarial review of
REM vlang/v#29100; left undressed for now rather than adding a harder-to-get-
REM right execution-level test or a build-script refactor.
if not exist "%~1" exit /b 0
del "%~1" >nul 2>&1
if not exist "%~1" exit /b 0
ping 192.0.2.1 -n 1 -w 100 >nul
del "%~1" >nul 2>&1
if not exist "%~1" exit /b 0
ping 192.0.2.1 -n 1 -w 250 >nul
del "%~1" >nul 2>&1
exit /b 0
:move_updated_to_v
REM The V1 compatibility compiler is installed on demand by `make v1`.
REM Compiling cmd/v again would only produce another copy of the new driver.
if not exist "%V_UPDATED%" exit /b 1
@REM del "%V_EXE%" &:: breaks if `makev.bat` is run from `v up` b/c of held file handle on `%V_EXE%`
if exist "%V_EXE%" (
move /Y "%V_EXE%" "%V_OLD%" >nul
if !ERRORLEVEL! NEQ 0 exit /b !ERRORLEVEL!
)
REM sleep for at most 100ms
ping 192.0.2.1 -n 1 -w 100 >nul
move /Y "%V_UPDATED%" "%V_EXE%" >nul
exit /b !ERRORLEVEL!