v /
1@echo off
2setlocal EnableDelayedExpansion EnableExtensions
3
4REM Option flags
5set /a shift_counter=0
6set /a flag_local=0
7set /a flag_verbose=0
8
9REM Option variables
10set "log_file=%TEMP%\v_make.log"
11set compiler=
12set subcmd=
13set target=build
14
15REM TCC variables
16set "tcc_url=https://github.com/vlang/tccbin"
17set "tcc_dir=%~dp0thirdparty\tcc"
18set "tcc_exe=%~dp0thirdparty\tcc\tcc.exe"
19if "%PROCESSOR_ARCHITECTURE%" == "x86" ( set "tcc_branch=thirdparty-windows-i386" ) else ( set "tcc_branch=thirdparty-windows-amd64" )
20if "%~1" == "-tcc32" set "tcc_branch=thirdparty-windows-i386"
21
22REM VC settings
23set "vc_url=https://github.com/vlang/vc"
24set "vc_dir=%~dp0vc"
25
26REM Let a particular environment specify their own TCC and VC repos (to help mirrors)
27if /I not ["%TCC_GIT%"] == [""] set "tcc_url=%TCC_GIT%"
28if /I not ["%TCC_BRANCH%"] == [""] set "tcc_branch=%TCC_BRANCH%"
29
30if /I not ["%VC_GIT%"] == [""] set "vc_url=%VC_GIT%"
31
32pushd %~dp0
33
34:verifyopt
35REM Read stdin EOF
36if ["%~1"] == [""] goto :init
37
38REM Target options
39if !shift_counter! LSS 1 (
40 if "%~1" == "help" (
41 if not ["%~2"] == [""] set "subcmd=%~2"& shift& set /a shift_counter+=1
42 )
43 for %%z in (build clean cleanall help) do (
44 if "%~1" == "%%z" set target=%1& shift& set /a shift_counter+=1& goto :verifyopt
45 )
46)
47
48REM Compiler option
49for %%g in (-gcc -msvc -tcc -tcc32 -clang) do (
50 if "%~1" == "%%g" set compiler=%~1& set compiler=!compiler:~1!& shift& set /a shift_counter+=1& goto :verifyopt
51)
52
53REM Standard options
54if "%~1" == "--verbose" (
55 if !flag_verbose! NEQ 0 (
56 echo The flag %~1 has already been specified. 1>&2
57 exit /b 2
58 )
59 set /a flag_verbose=1
60 set /a shift_counter+=1
61 shift
62 goto :verifyopt
63)
64if "%~1" == "--local" (
65 if !flag_local! NEQ 0 (
66 echo The flag %~1 has already been specified. 1>&2
67 exit /b 2
68 )
69 set /a flag_local=1
70 set /a shift_counter+=1
71 shift
72 goto :verifyopt
73)
74if "%~1" == "--logfile" (
75 if ["%~2"] == [""] (
76 echo Log file is not specified for -logfile parameter. 1>&2
77 exit /b 2
78 )
79 pushd "%~dp2" 2>NUL || (
80 echo The log file specified for -logfile parameter does not exist. 1>&2
81 exit /b 2
82 )
83 popd
84 set "log_file=%~sf2"
85 set /a shift_counter+=2
86 shift
87 shift
88 goto :verifyopt
89)
90
91echo Undefined option: %~1
92exit /b 2
93
94:init
95goto :!target!
96
97:cleanall
98call :clean
99if %ERRORLEVEL% NEQ 0 exit /b %ERRORLEVEL%
100echo.
101echo Cleanup vc
102echo ^> Purge TCC binaries
103if !flag_verbose! EQU 1 (
104 echo [Debug] rmdir /s /q "%tcc_dir%">>"!log_file!"
105 echo rmdir /s /q "%tcc_dir%"
106)
107rmdir /s /q "%tcc_dir%">>"!log_file!" 2>NUL
108echo ^> Purge vc repository
109if !flag_verbose! EQU 1 (
110 echo [Debug] rmdir /s /q "%vc_dir%">>"!log_file!"
111 echo rmdir /s /q "%vc_dir%"
112)
113rmdir /s /q "%vc_dir%">>"!log_file!" 2>NUL
114exit /b 0
115
116:clean
117echo Cleanup build artifacts
118echo ^> Purge debug symbols
119if !flag_verbose! EQU 1 (
120 echo [Debug] del *.pdb *.lib *.bak *.out *.ilk *.exp *.obj *.o *.a *.so>>"!log_file!"
121 echo del *.pdb *.lib *.bak *.out *.ilk *.exp *.obj *.o *.a *.so
122)
123del *.pdb *.lib *.bak *.out *.ilk *.exp *.obj *.o *.a *.so>>"!log_file!" 2>NUL
124echo ^> Delete old V executable
125if !flag_verbose! EQU 1 (
126 echo [Debug] del v_old.exe v*.exe>>"!log_file!"
127 echo del v_old.exe v*.exe
128)
129del v_old.exe v*.exe>>"!log_file!" 2>NUL
130exit /b 0
131
132:help
133if [!subcmd!] == [] (
134 call :usage 2>NUL
135) else (
136 call :help_!subcmd! 2>NUL
137)
138if %ERRORLEVEL% NEQ 0 echo Invalid subcommand: !subcmd!
139exit /b %ERRORLEVEL%
140
141:build
142if !flag_local! NEQ 1 (
143 call :download_tcc
144 if %ERRORLEVEL% NEQ 0 goto :error
145 del "!log_file!">NUL 2>&1
146 pushd "%vc_dir%" 2>NUL && (
147 echo Updating vc...
148 echo ^> Sync with remote !vc_url!
149 if !flag_verbose! EQU 1 (
150 echo [Debug] cd "%vc_dir%">>"!log_file!"
151 echo cd "%vc_dir%"
152 cd "%vc_dir%">>"!log_file!" 2>NUL
153 echo [Debug] git pull --quiet>>"!log_file!"
154 echo git pull --quiet
155 git pull --quiet>>"!log_file!" 2>NUL
156 echo [Debug] cd ..>>"!log_file!"
157 echo cd ..
158 cd ..>>"!log_file!" 2>NUL
159 ) else (
160 cd "%vc_dir%">>"!log_file!" 2>NUL
161 git pull --quiet>>"!log_file!" 2>NUL
162 cd ..>>"!log_file!" 2>NUL
163 )
164 popd
165 ) || call :cloning_vc
166 echo.
167)
168
169echo Building V...
170if not [!compiler!] == [] goto :!compiler!_strap
171
172
173REM By default, use tcc, since we have it prebuilt:
174:tcc_strap
175:tcc32_strap
176echo ^> Attempting to build v_win.c with TCC
177if !flag_verbose! EQU 1 (
178 echo [Debug] "!tcc_exe!" -ladvapi32 -bt10 -w -o v.exe vc\v_win.c>>"!log_file!"
179 echo "!tcc_exe!" -ladvapi32 -bt10 -w -o v.exe vc\v_win.c
180)
181"!tcc_exe!" -ladvapi32 -bt10 -w -o v.exe vc\v_win.c>>"!log_file!" 2>NUL
182if %ERRORLEVEL% NEQ 0 goto :compile_error
183
184echo ^> Compiling with .\v.exe self
185if !flag_verbose! EQU 1 (
186 echo [Debug] v.exe -cc "!tcc_exe!" self>>"!log_file!"
187 echo v.exe -cc "!tcc_exe!" self
188)
189v.exe -cc "!tcc_exe!" self>>"!log_file!" 2>NUL
190if %ERRORLEVEL% NEQ 0 goto :clang_strap
191goto :success
192
193
194
195:clang_strap
196where /q clang
197if %ERRORLEVEL% NEQ 0 (
198 echo ^> Clang not found
199 if not [!compiler!] == [] goto :error
200 goto :gcc_strap
201)
202
203echo ^> Attempting to build v_win.c with Clang
204if !flag_verbose! EQU 1 (
205 echo [Debug] clang -std=c99 -municode -w -o v.exe .\vc\v_win.c>>"!log_File!"
206 echo clang -std=c99 -municode -w -o v.exe .\vc\v_win.c
207)
208clang -std=c99 -municode -w -o v.exe .\vc\v_win.c>>"!log_file!" 2>NUL
209if %ERRORLEVEL% NEQ 0 (
210 REM In most cases, compile errors happen because the version of Clang installed is too old
211 clang --version>>"!log_file!"
212 goto :compile_error
213)
214
215echo ^> Compiling with .\v.exe self
216if !flag_verbose! EQU 1 (
217 echo [Debug] v.exe -cc clang self>>"!log_file!"
218 echo v.exe -cc clang self
219)
220v.exe -cc clang self>>"!log_file!" 2>NUL
221if %ERRORLEVEL% NEQ 0 goto :compile_error
222goto :success
223
224:gcc_strap
225where /q gcc
226if %ERRORLEVEL% NEQ 0 (
227 echo ^> GCC not found
228 if not [!compiler!] == [] goto :error
229 goto :msvc_strap
230)
231
232echo ^> Attempting to build v_win.c with GCC
233if !flag_verbose! EQU 1 (
234 echo [Debug] gcc -std=c99 -municode -w -o v.exe .\vc\v_win.c>>"!log_File!"
235 echo gcc -std=c99 -municode -w -o v.exe .\vc\v_win.c
236)
237gcc -std=c99 -municode -w -o v.exe .\vc\v_win.c>>"!log_File!" 2>NUL
238if %ERRORLEVEL% NEQ 0 (
239 REM In most cases, compile errors happen because the version of GCC installed is too old
240 gcc --version>>"!log_File!"
241 goto :compile_error
242)
243
244echo ^> Compiling with .\v.exe self
245if !flag_verbose! EQU 1 (
246 echo [Debug] v.exe -cc gcc self>>"!log_file!"
247 echo v.exe -cc gcc self
248)
249v.exe -cc gcc self>>"!log_file!" 2>NUL
250if %ERRORLEVEL% NEQ 0 goto :compile_error
251goto :success
252
253:msvc_strap
254set VsWhereDir=%ProgramFiles(x86)%
255set HostArch=x64
256if "%PROCESSOR_ARCHITECTURE%" == "x86" (
257 echo Using x86 Build Tools...
258 set VsWhereDir=%ProgramFiles%
259 set HostArch=x86
260)
261
262if not exist "%VsWhereDir%\Microsoft Visual Studio\Installer\vswhere.exe" (
263 echo ^> MSVC not found
264 if not [!compiler!] == [] goto :error
265 goto :compile_error
266)
267
268for /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 (
269 set InstallDir=%%i
270)
271
272if exist "%InstallDir%\Common7\Tools\vsdevcmd.bat" (
273 call "%InstallDir%\Common7\Tools\vsdevcmd.bat" -arch=%HostArch% -host_arch=%HostArch% -no_logo > NUL
274) else if exist "%VsWhereDir%\Microsoft Visual Studio 14.0\Common7\Tools\vsdevcmd.bat" (
275 call "%VsWhereDir%\Microsoft Visual Studio 14.0\Common7\Tools\vsdevcmd.bat" -arch=%HostArch% -host_arch=%HostArch% -no_logo > NUL
276)
277
278set ObjFile=.v.c.obj
279
280echo ^> Attempting to build v_win.c with MSVC
281if !flag_verbose! EQU 1 (
282 echo [Debug] cl.exe /volatile:ms /Fo%ObjFile% /O2 /MD /D_VBOOTSTRAP vc\v_win.c user32.lib kernel32.lib advapi32.lib shell32.lib /link /nologo /out:v.exe /incremental:no>>"!log_file!"
283 echo cl.exe /volatile:ms /Fo%ObjFile% /O2 /MD /D_VBOOTSTRAP vc\v_win.c user32.lib kernel32.lib advapi32.lib shell32.lib /link /nologo /out:v.exe /incremental:no
284)
285cl.exe /volatile:ms /Fo%ObjFile% /O2 /MD /D_VBOOTSTRAP vc\v_win.c user32.lib kernel32.lib advapi32.lib shell32.lib /link /nologo /out:v.exe /incremental:no>>"!log_file!" 2>NUL
286if %ERRORLEVEL% NEQ 0 (
287 REM In some cases, compile errors happen because of the MSVC compiler version
288 cl.exe 1>NUL 2>"!log_file!"
289 goto :compile_error
290)
291
292echo ^> Compiling with .\v.exe self
293if !flag_verbose! EQU 1 (
294 echo [Debug] v.exe -cc msvc self>>"!log_file!"
295 echo v.exe -cc msvc self
296)
297v.exe -cc msvc self>>"!log_file!" 2>NUL
298del %ObjFile%>>"!log_file!" 2>>&1
299if %ERRORLEVEL% NEQ 0 goto :compile_error
300goto :success
301
302:download_tcc
303pushd %tcc_dir% 2>NUL && (
304 echo Updating TCC
305 echo ^> Syncing TCC from !tcc_url!
306 if !flag_verbose! EQU 1 (
307 echo [Debug] git pull --quiet>>"!log_file!"
308 echo git pull --quiet
309 )
310 git pull --quiet>>"!log_file!" 2>NUL
311 popd
312) || call :bootstrap_tcc
313
314for /f "usebackq delims=" %%i in (`dir "%tcc_dir%" /b /a /s tcc.exe`) do (
315 set "attrib=%%~ai"
316 set "dattrib=%attrib:~0,1%"
317 if /I not "%dattrib%" == "d" set "tcc_exe=%%~sfi"
318)
319if [!tcc_exe!] == [] echo ^> TCC not found, even after cloning& goto :error
320echo.
321exit /b 0
322
323:compile_error
324echo.
325type "!log_file!">NUL 2>&1
326goto :error
327
328:error
329echo.
330echo Exiting from error
331type "!log_file!"
332echo ERROR: please follow the instructions in https://github.com/vlang/v/wiki/Installing-a-C-compiler-on-Windows
333exit /b 1
334
335:success
336echo ^> V built successfully!
337echo ^> To add V to your PATH, run `.\v.exe symlink`.
338
339:version
340echo.
341echo | set /p="V version: "
342.\v.exe version
343goto :eof
344
345:usage
346echo Usage:
347echo make.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 clean-all Cleanup entire ALL build artifacts and vc repository
356echo help Display usage help for the given target
357echo.
358echo Examples:
359echo make.bat -msvc
360echo make.bat -gcc --local --logpath output.log
361echo make.bat build -tcc --local
362echo make.bat -tcc32
363echo make.bat help clean
364echo.
365echo Use "make help <target>" for more information about a target, for instance: "make help clean"
366echo.
367echo Note: Any undefined/unsupported options will be ignored
368exit /b 0
369
370:help_help
371echo Usage:
372echo make.bat help [target]
373echo.
374echo Target:
375echo build ^| clean ^| clean-all ^| help Query given target
376exit /b 0
377
378:help_clean
379echo Usage:
380echo make.bat clean
381echo.
382echo Options:
383echo --logfile PATH Use the specified PATH as the log
384echo --verbose Output compilation commands to stdout
385exit /b 0
386
387:help_cleanall
388echo Usage:
389echo make.bat clean-all
390echo.
391echo Options:
392echo --logfile PATH Use the specified PATH as the log
393echo --verbose Output compilation commands to stdout
394exit /b 0
395
396:help_build
397echo Usage:
398echo make.bat build [compiler] [options]
399echo.
400echo Compiler:
401echo -msvc ^| -gcc ^| -tcc ^| -tcc32 ^| -clang Set C compiler
402echo.
403echo Options:
404echo --local Use the local vc repository without
405echo syncing with remote
406echo --logfile PATH Use the specified PATH as the log
407echo file
408echo --verbose Output compilation commands to stdout
409exit /b 0
410
411:bootstrap_tcc
412echo Bootstraping TCC...
413echo ^> TCC not found
414if "!tcc_branch!" == "thirdparty-windows-i386" ( echo ^> Downloading TCC32 from !tcc_url! ) else ( echo ^> Downloading TCC64 from !tcc_url! )
415if !flag_verbose! EQU 1 (
416 echo [Debug] git clone --depth 1 --quiet --single-branch --branch !tcc_branch! !tcc_url! "%tcc_dir%">>"!log_file!"
417 echo git clone --depth 1 --quiet --single-branch --branch !tcc_branch! !tcc_url! "%tcc_dir%"
418)
419git clone --depth 1 --quiet --single-branch --branch !tcc_branch! !tcc_url! "%tcc_dir%">>"!log_file!" 2>NUL
420exit /b 0
421
422:cloning_vc
423echo Cloning vc...
424echo ^> Cloning from remote !vc_url!
425if !flag_verbose! EQU 1 (
426 echo [Debug] git clone --depth 1 --quiet %vc_url%>>"!log_file!"
427 echo git clone --depth 1 --quiet %vc_url%
428)
429git clone --depth 1 --quiet %vc_url%>>"!log_file!" 2>NUL
430exit /b 0
431
432:eof
433popd
434endlocal
435exit /b 0