v
Raw file
		
1@setlocal EnableDelayedExpansion EnableExtensions
2
3IF 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
14REM TCC variables
15set "tcc_url=https://github.com/vlang/tccbin"
16set "tcc_dir=thirdparty\tcc"
17set "tcc_exe=thirdparty\tcc\tcc.exe"
18if "%PROCESSOR_ARCHITECTURE%" == "x86" ( set "tcc_branch=thirdparty-windows-i386" ) else ( set "tcc_branch=thirdparty-windows-amd64" )
19if "%~1" == "-tcc32" set "tcc_branch=thirdparty-windows-i386"
20
21REM VC settings
22set "vc_url=https://github.com/vlang/vc"
23set "vc_dir=%~dp0vc"
24
25REM Let a particular environment specify their own TCC and VC repos (to help mirrors)
26if /I not ["%TCC_GIT%"] == [""] set "tcc_url=%TCC_GIT%"
27if /I not ["%TCC_BRANCH%"] == [""] set "tcc_branch=%TCC_BRANCH%"
28
29if /I not ["%VC_GIT%"] == [""] set "vc_url=%VC_GIT%"
30
31pushd %~dp0
32
33:verifyopt
34REM Read stdin EOF
35if ["%~1"] == [""] goto :init
36
37REM Target options
38if !shift_counter! LSS 1 (
39 if "%~1" == "help" (
40 if not ["%~2"] == [""] set "subcmd=%~2"& shift& set /a shift_counter+=1
41 )
42 for %%z in (build clean cleanall check help) do (
43 if "%~1" == "%%z" set target=%1& shift& set /a shift_counter+=1& goto :verifyopt
44 )
45)
46
47REM Compiler option
48for %%g in (-gcc -msvc -tcc -tcc32 -clang) do (
49 if "%~1" == "%%g" set compiler=%~1& set compiler=!compiler:~1!& shift& set /a shift_counter+=1& goto :verifyopt
50)
51
52REM Standard options
53if "%~1" == "--local" (
54 if !flag_local! NEQ 0 (
55 echo The flag %~1 has already been specified. 1>&2
56 exit /b 2
57 )
58 set /a flag_local=1
59 set /a shift_counter+=1
60 shift
61 goto :verifyopt
62)
63
64echo Undefined option: %~1
65exit /b 2
66
67:init
68goto :!target!
69
70:check
71echo.
72echo Check everything
73v.exe test-all
74exit /b 0
75
76:cleanall
77call :clean
78if %ERRORLEVEL% NEQ 0 exit /b %ERRORLEVEL%
79echo.
80echo Cleanup vc
81echo ^> Purge TCC binaries
82rmdir /s /q "%tcc_dir%"
83echo ^> Purge vc repository
84rmdir /s /q "%vc_dir%"
85exit /b 0
86
87:clean
88echo Cleanup build artifacts
89echo ^> Purge debug symbols
90del *.pdb *.lib *.bak *.out *.ilk *.exp *.obj *.o *.a *.so
91
92echo ^> Delete old V executable
93del v_old.exe v*.exe
94exit /b 0
95
96:help
97if [!subcmd!] == [] (
98 call :usage
99) else (
100 call :help_!subcmd!
101)
102if %ERRORLEVEL% NEQ 0 echo Invalid subcommand: !subcmd!
103exit /b %ERRORLEVEL%
104
105:build
106if !flag_local! NEQ 1 (
107 call :download_tcc
108 if %ERRORLEVEL% NEQ 0 goto :error
109 pushd "%vc_dir%" && (
110 echo Updating vc...
111 echo ^> Sync with remote !vc_url!
112 cd "%vc_dir%"
113 git pull --quiet
114 cd ..
115 popd
116 ) || call :cloning_vc
117 echo.
118)
119
120echo Building V...
121if not [!compiler!] == [] goto :!compiler!_strap
122
123
124REM By default, use tcc, since we have it prebuilt:
125:tcc_strap
126:tcc32_strap
127echo ^> Attempting to build v_win.c with "!tcc_exe!"
128"!tcc_exe!" -Bthirdparty/tcc -bt10 -g -w -o v.exe vc\v_win.c -ladvapi32
129if %ERRORLEVEL% NEQ 0 goto :compile_error
130echo ^> Compiling .\v.exe with itself
131v.exe -keepc -g -showcc -cc "!tcc_exe!" -cflags -Bthirdparty/tcc -o v2.exe cmd/v
132if %ERRORLEVEL% NEQ 0 goto :clang_strap
133call :move_v2_to_v
134goto :success
135
136:clang_strap
137where /q clang
138if %ERRORLEVEL% NEQ 0 (
139 echo ^> Clang not found
140 if not [!compiler!] == [] goto :error
141 goto :gcc_strap
142)
143
144echo ^> Attempting to build v_win.c with Clang
145clang -std=c99 -municode -g -w -o v.exe .\vc\v_win.c -ladvapi32
146if %ERRORLEVEL% NEQ 0 (
147 echo In most cases, compile errors happen because the version of Clang installed is too old
148 clang --version
149 goto :compile_error
150)
151
152echo ^> Compiling .\v.exe with itself
153v.exe -keepc -g -showcc -cc clang -o v2.exe cmd/v
154if %ERRORLEVEL% NEQ 0 goto :compile_error
155call :move_v2_to_v
156goto :success
157
158:gcc_strap
159where /q gcc
160if %ERRORLEVEL% NEQ 0 (
161 echo ^> GCC not found
162 if not [!compiler!] == [] goto :error
163 goto :msvc_strap
164)
165
166echo ^> Attempting to build v_win.c with GCC
167gcc -std=c99 -municode -g -w -o v.exe .\vc\v_win.c -ladvapi32
168if %ERRORLEVEL% NEQ 0 (
169 echo In most cases, compile errors happen because the version of GCC installed is too old
170 gcc --version
171 goto :compile_error
172)
173
174echo ^> Compiling .\v.exe with itself
175v.exe -keepc -g -showcc -cc gcc -o v2.exe cmd/v
176if %ERRORLEVEL% NEQ 0 goto :compile_error
177call :move_v2_to_v
178goto :success
179
180:msvc_strap
181set VsWhereDir=%ProgramFiles(x86)%
182set HostArch=x64
183if "%PROCESSOR_ARCHITECTURE%" == "x86" (
184 echo Using x86 Build Tools...
185 set VsWhereDir=%ProgramFiles%
186 set HostArch=x86
187)
188
189if not exist "%VsWhereDir%\Microsoft Visual Studio\Installer\vswhere.exe" (
190 echo ^> MSVC not found
191 if not [!compiler!] == [] goto :error
192 goto :compile_error
193)
194
195for /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 (
196 set InstallDir=%%i
197)
198
199if exist "%InstallDir%\Common7\Tools\vsdevcmd.bat" (
200 call "%InstallDir%\Common7\Tools\vsdevcmd.bat" -arch=%HostArch% -host_arch=%HostArch% -no_logo
201) else if exist "%VsWhereDir%\Microsoft Visual Studio 14.0\Common7\Tools\vsdevcmd.bat" (
202 call "%VsWhereDir%\Microsoft Visual Studio 14.0\Common7\Tools\vsdevcmd.bat" -arch=%HostArch% -host_arch=%HostArch% -no_logo
203)
204
205set ObjFile=.v.c.obj
206
207echo ^> Attempting to build v_win.c with MSVC
208cl.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
209if %ERRORLEVEL% NEQ 0 (
210 echo In some cases, compile errors happen because of the MSVC compiler version
211 cl.exe
212 goto :compile_error
213)
214
215echo ^> Compiling .\v.exe with itself
216v.exe -keepc -g -showcc -cc msvc -o v2.exe cmd/v
217del %ObjFile%
218if %ERRORLEVEL% NEQ 0 goto :compile_error
219call :move_v2_to_v
220goto :success
221
222:download_tcc
223pushd %tcc_dir% && (
224 echo Updating TCC
225 echo ^> Syncing TCC from !tcc_url!
226 git pull --quiet
227 popd
228) || call :bootstrap_tcc
229
230if [!tcc_exe!] == [] echo ^> TCC not found, even after cloning& goto :error
231echo.
232exit /b 0
233
234:compile_error
235echo.
236echo Backend compiler error
237goto :error
238
239:error
240echo.
241echo Exiting from error
242echo ERROR: please follow the instructions in https://github.com/vlang/v/wiki/Installing-a-C-compiler-on-Windows
243exit /b 1
244
245:success
246.\v.exe run cmd\tools\detect_tcc.v
247echo ^> V built successfully!
248echo ^> To add V to your PATH, run `.\v.exe symlink`.
249
250:version
251echo.
252echo | set /p="V version: "
253.\v.exe version
254goto :eof
255
256:usage
257echo Usage:
258echo make.bat [target] [compiler] [options]
259echo.
260echo Compiler:
261echo -msvc ^| -gcc ^| -tcc ^| -tcc32 ^| -clang Set C compiler
262echo.
263echo Target:
264echo build[default] Compiles V using the given C compiler
265echo clean Clean build artifacts and debugging symbols
266echo cleanall Cleanup entire ALL build artifacts and vc repository
267echo check Check that tests pass, and the repository is in a good shape for Pull Requests
268echo help Display help for the given target
269echo.
270echo Examples:
271echo make.bat -msvc
272echo make.bat -gcc --local
273echo make.bat build -tcc --local
274echo make.bat -tcc32
275echo make.bat help clean
276echo.
277echo Use "make help <target>" for more information about a target, for instance: "make help clean"
278echo.
279echo Note: Any undefined/unsupported options will be ignored
280exit /b 0
281
282:help_help
283echo Usage:
284echo make.bat help [target]
285echo.
286echo Target:
287echo build ^| clean ^| cleanall ^| help Query given target
288exit /b 0
289
290:help_clean
291echo Usage:
292echo make.bat clean
293echo.
294exit /b 0
295
296:help_cleanall
297echo Usage:
298echo make.bat cleanall
299echo.
300exit /b 0
301
302:help_build
303echo Usage:
304echo make.bat build [compiler] [options]
305echo.
306echo Compiler:
307echo -msvc ^| -gcc ^| -tcc ^| -tcc32 ^| -clang Set C compiler
308echo.
309echo Options:
310echo --local Use the local vc repository without
311echo syncing with remote
312exit /b 0
313
314:bootstrap_tcc
315echo Bootstraping TCC...
316echo ^> TCC not found
317if "!tcc_branch!" == "thirdparty-windows-i386" ( echo ^> Downloading TCC32 from !tcc_url! , branch !tcc_branch! ) else ( echo ^> Downloading TCC64 from !tcc_url! , branch !tcc_branch! )
318git clone --depth 1 --quiet --single-branch --branch !tcc_branch! !tcc_url! "%tcc_dir%"
319git -C "%tcc_dir%" log -n3
320exit /b 0
321
322:cloning_vc
323echo Cloning vc...
324echo ^> Cloning from remote !vc_url!
325git clone --depth 1 --quiet "%vc_url%"
326exit /b 0
327
328:eof
329popd
330endlocal
331exit /b 0
332
333:move_v2_to_v
334del v.exe
335REM sleep for at most 100ms
336ping 192.0.2.1 -n 1 -w 100 >nul
337move v2.exe v.exe
338exit /b 0