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