v
1@echo off
2
3echo Building V
4
5REM default tcc
6set tcc_url=https://github.com/vlang/tccbin_win
7set tcc_dir=%~dp0thirdparty\tcc
8
9REM let a particular environment specify their own tcc
10if "%TCC_GIT%" =="" goto :init
11set tcc_url="%TCC_GIT%"
12
13:init
14REM initialize the log file with the failure message
15set log_file=%TEMP%\v_make.bat.log
16echo Failed to compile - Create an issue at 'https://github.com/vlang/v' with the following info:>%log_file%
17echo.>>%log_file%
18
19REM alleviate weird issues with this var
20set cloned_tcc=
21set ERRORLEVEL=
22
23pushd %~dp0
24
25if "%~1"=="-local" goto :compile
26if "%~2"=="-local" goto :compile
27
28if exist "vc" (
29 echo Updating vc...
30 cd vc
31 git pull --quiet
32 cd ..
33) else (
34 echo Cloning vc...
35 git clone --depth 1 --quiet https://github.com/vlang/vc
36)
37
38:compile
39REM option to force msvc, gcc or tcc
40if "%~1"=="-gcc" set force_gcc=1 & goto :gcc_strap
41if "%~2"=="-gcc" set force_gcc=1 & goto :gcc_strap
42if "%~1"=="-msvc" set force_msvc=1 & goto :msvc_strap
43if "%~2"=="-msvc" set force_msvc=1 & goto :msvc_strap
44if "%~1"=="-tcc" set force_tcc=1 & goto :tcc_strap
45if "%~2"=="-tcc" set force_tcc=1 & goto :tcc_strap
46if "%~1"=="-fresh_tcc" set force_tcc=1 & goto :fresh_tcc
47if "%~2"=="-fresh_tcc" set force_tcc=1 & goto :fresh_tcc
48
49:gcc_strap
50echo.
51echo Attempting to build v.c with GCC...
52
53where /q gcc
54if %ERRORLEVEL% NEQ 0 (
55 echo ^> GCC not found
56 if "%force_gcc%" NEQ "" goto :error
57 goto :msvc_strap
58)
59
60gcc -std=c99 -municode -w -o v.exe .\vc\v_win.c>>%log_file% 2>>&1
61if %ERRORLEVEL% NEQ 0 (
62 rem In most cases, compile errors happen because the version of GCC installed is too old
63 gcc --version>>%log_file% 2>>&1
64 goto :compile_error
65)
66
67echo ^> Compiling with .\v.exe self
68v.exe self>>%log_file% 2>>&1
69if %ERRORLEVEL% NEQ 0 goto :compile_error
70goto :success
71
72:msvc_strap
73echo.
74echo Attempting to build v.c with MSVC...
75set VsWhereDir=%ProgramFiles(x86)%
76set HostArch=x64
77if "%PROCESSOR_ARCHITECTURE%" == "x86" (
78 echo Using x86 Build Tools...
79 set VsWhereDir=%ProgramFiles%
80 set HostArch=x86
81)
82
83if not exist "%VsWhereDir%\Microsoft Visual Studio\Installer\vswhere.exe" (
84 echo ^> MSVC not found
85 if "%force_msvc%" NEQ "" goto :error
86 goto :tcc_strap
87)
88
89for /f "usebackq tokens=*" %%i in (`"%VsWhereDir%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -prerelease -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
90 set InstallDir=%%i
91)
92
93if exist "%InstallDir%\Common7\Tools\vsdevcmd.bat" (
94 call "%InstallDir%\Common7\Tools\vsdevcmd.bat" -arch=%HostArch% -host_arch=%HostArch% -no_logo > NUL
95) else if exist "%VsWhereDir%\Microsoft Visual Studio 14.0\Common7\Tools\vsdevcmd.bat" (
96 call "%VsWhereDir%\Microsoft Visual Studio 14.0\Common7\Tools\vsdevcmd.bat" -arch=%HostArch% -host_arch=%HostArch% -no_logo > NUL
97)
98
99set ObjFile=.v.c.obj
100
101cl.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>>&1
102if %ERRORLEVEL% NEQ 0 goto :compile_error
103
104echo ^> Compiling with .\v.exe self
105v.exe -cc msvc self>>%log_file% 2>>&1
106del %ObjFile%>>%log_file% 2>>&1
107if %ERRORLEVEL% NEQ 0 goto :compile_error
108goto :success
109
110:fresh_tcc
111rd /s /q %tcc_dir%
112
113:clone_tcc
114git clone --depth 1 --quiet %tcc_url% %tcc_dir%
115set cloned_tcc=1
116goto :tcc_strap
117
118:tcc_strap
119echo.
120echo Attempting to build v.c with TCC...
121
122where /q tcc
123if %ERRORLEVEL% NEQ 0 (
124 if exist "%tcc_dir%" (
125 set tcc_exe=%tcc_dir%\tcc.exe
126 ) else if "%cloned_tcc%"=="" (
127 echo ^> TCC not found
128 echo ^> Downloading TCC from %tcc_url%
129 goto :clone_tcc
130 ) else (
131 echo ^> TCC not found, even after cloning %cloned_tcc%
132 goto :error
133 )
134) else (
135 for /f "delims=" %%i in ('where tcc') do set tcc_exe=%%i
136)
137
138if exist "%tcc_dir%" (
139 if "%cloned_tcc%"=="" (
140 echo ^> Updating prebuilt TCC...
141 pushd "%tcc_dir%"\
142 git pull -q
143 popd
144 )
145)
146
147%tcc_exe% -std=c99 -municode -lws2_32 -lshell32 -ladvapi32 -bt10 -w -o v.exe vc\v_win.c
148if %ERRORLEVEL% NEQ 0 goto :compile_error
149
150echo ^> Compiling with .\v.exe self
151v.exe -cc "%tcc_exe%" self>>%log_file% 2>>&1
152if %ERRORLEVEL% NEQ 0 goto :compile_error
153goto :success
154
155:compile_error
156echo.
157echo.
158type %log_file%
159del %log_file%
160goto :error
161
162:error
163echo.
164echo Exiting from error
165popd
166exit /b 1
167
168:success
169echo ^> V built successfully!
170echo ^> To add V to your PATH, run `.\v.exe symlink`.
171del v_old.exe >>%log_file% 2>>&1
172del %log_file%
173
174:version
175echo.
176echo | set /p="V version: "
177.\v.exe version
178if "%cloned_tcc%" NEQ "" (
179 if "%force_tcc%" == "" (
180 echo.
181 echo WARNING: No C compiler was detected in your PATH. `tcc` was used temporarily
182 echo to build V, but it may have some bugs and may not work in all cases.
183 echo A more advanced C compiler like GCC or MSVC is recommended.
184 echo https://github.com/vlang/v/wiki/Installing-a-C-compiler-on-Windows
185 echo.
186 )
187)
188
189popd