502 lines · plain
1@echo off
2
3REM Filter out tests that are known to fail.
4set "LIT_FILTER_OUT=gh110231.cpp|crt_initializers.cpp|init-order-atexit.cpp|use_after_return_linkage.cpp|initialization-bug.cpp|initialization-bug-no-global.cpp|trace-malloc-unbalanced.test|trace-malloc-2.test|TraceMallocTest"
5
6setlocal enabledelayedexpansion
7goto begin
8
9:usage
10echo Script for building the LLVM installer on Windows,
11echo used for the releases at https://github.com/llvm/llvm-project/releases
12echo.
13echo Usage: build_llvm_release.bat --version ^<version^> [--x86,--x64, --arm64] [--skip-checkout] [--local-python] [--force-msvc]
14echo.
15echo Options:
16echo --version: [required] version to build
17echo --help: display this help
18echo --x86: build and test x86 variant
19echo --x64: build and test x64 variant
20echo --arm64: build and test arm64 variant
21echo --skip-checkout: use local git checkout instead of downloading src.zip
22echo --local-python: use installed Python and does not try to use a specific version (3.10)
23echo --force-msvc: use MSVC compiler for stage0, even if clang-cl is present
24echo.
25echo Note: At least one variant to build is required.
26echo.
27echo Example: build_llvm_release.bat --version 15.0.0 --x86 --x64
28exit /b 1
29
30
31:begin
32
33::==============================================================================
34:: parse args
35set version=
36set help=
37set x86=
38set x64=
39set arm64=
40set skip-checkout=
41set local-python=
42set force-msvc=
43call :parse_args %*
44
45if "%help%" NEQ "" goto usage
46
47if "%version%" == "" (
48 echo --version option is required
49 echo =============================
50 goto usage
51)
52
53if "%arm64%" == "" if "%x64%" == "" if "%x86%" == "" (
54 echo nothing to build!
55 echo choose one or several variants from: --x86 --x64 --arm64
56 exit /b 1
57)
58
59::==============================================================================
60:: check prerequisites
61REM Note:
62REM 7zip versions 21.x and higher will try to extract the symlinks in
63REM llvm's git archive, which requires running as administrator.
64
65REM Check 7-zip version and/or administrator permissions.
66for /f "delims=" %%i in ('7z.exe ^| findstr /r "2[1-9].[0-9][0-9]"') do set version_7z=%%i
67if not "%version_7z%"=="" (
68 REM Unique temporary filename to use by the 'mklink' command.
69 set "link_name=%temp%\%username%_%random%_%random%.tmp"
70
71 REM As the 'mklink' requires elevated permissions, the symbolic link
72 REM creation will fail if the script is not running as administrator.
73 mklink /d "!link_name!" . 1>nul 2>nul
74 if errorlevel 1 (
75 echo.
76 echo Script requires administrator permissions, or a 7-zip version 20.x or older.
77 echo Current version is "%version_7z%"
78 exit /b 1
79 ) else (
80 REM Remove the temporary symbolic link.
81 rd "!link_name!"
82 )
83)
84
85REM Prerequisites:
86REM
87REM Visual Studio 2019, CMake, Ninja, GNUWin32, SWIG, Python 3,
88REM NSIS with the strlen_8192 patch,
89REM Perl (for the OpenMP run-time).
90REM
91REM
92REM For LLDB, SWIG version 4.1.1 should be used.
93REM
94
95:: Detect Visual Studio
96set vsinstall=
97set vswhere=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
98
99if "%VSINSTALLDIR%" NEQ "" (
100 echo using enabled Visual Studio installation
101 set "vsinstall=%VSINSTALLDIR%"
102) else (
103 echo using vswhere to detect Visual Studio installation
104 FOR /F "delims=" %%r IN ('^""%vswhere%" -nologo -latest -products "*" -all -property installationPath^"') DO set vsinstall=%%r
105)
106set "vsdevcmd=%vsinstall%\Common7\Tools\VsDevCmd.bat"
107
108if not exist "%vsdevcmd%" (
109 echo Can't find any installation of Visual Studio
110 exit /b 1
111)
112echo Using VS devcmd: %vsdevcmd%
113
114::==============================================================================
115:: start echoing what we do
116@echo on
117
118set python32_dir=C:\Users\%USERNAME%\AppData\Local\Programs\Python\Python310-32
119set python64_dir=C:\Users\%USERNAME%\AppData\Local\Programs\Python\Python310
120set pythonarm64_dir=C:\Users\%USERNAME%\AppData\Local\Programs\Python\Python311-arm64
121
122set revision=llvmorg-%version%
123set package_version=%version%
124set build_dir=%cd%\llvm_package_%package_version%
125
126echo Revision: %revision%
127echo Package version: %package_version%
128echo Build dir: %build_dir%
129echo.
130
131if exist %build_dir% (
132 echo Build directory already exists: %build_dir%
133 exit /b 1
134)
135mkdir %build_dir%
136cd %build_dir% || exit /b 1
137
138if "%skip-checkout%" == "true" (
139 echo Using local source
140 set llvm_src=%~dp0..\..\..
141) else (
142 echo Checking out %revision%
143 curl -L https://github.com/llvm/llvm-project/archive/%revision%.zip -o src.zip || exit /b 1
144 7z x src.zip || exit /b 1
145 mv llvm-project-* llvm-project || exit /b 1
146 set llvm_src=%build_dir%\llvm-project
147)
148
149curl -O https://gitlab.gnome.org/GNOME/libxml2/-/archive/v2.9.12/libxml2-v2.9.12.tar.gz || exit /b 1
150tar zxf libxml2-v2.9.12.tar.gz
151
152REM Setting CMAKE_CL_SHOWINCLUDES_PREFIX to work around PR27226.
153REM Common flags for all builds.
154set common_compiler_flags=-DLIBXML_STATIC
155set common_cmake_flags=^
156 -DCMAKE_BUILD_TYPE=Release ^
157 -DLLVM_ENABLE_ASSERTIONS=OFF ^
158 -DLLVM_INSTALL_TOOLCHAIN_ONLY=ON ^
159 -DLLVM_TARGETS_TO_BUILD="AArch64;ARM;X86;BPF;WebAssembly;RISCV;NVPTX" ^
160 -DLLVM_BUILD_LLVM_C_DYLIB=ON ^
161 -DPython3_FIND_REGISTRY=NEVER ^
162 -DPACKAGE_VERSION=%package_version% ^
163 -DCMAKE_CL_SHOWINCLUDES_PREFIX="Note: including file: " ^
164 -DLLVM_ENABLE_LIBXML2=FORCE_ON ^
165 -DCLANG_ENABLE_LIBXML2=OFF ^
166 -DCMAKE_C_FLAGS="%common_compiler_flags%" ^
167 -DCMAKE_CXX_FLAGS="%common_compiler_flags%" ^
168 -DLLVM_ENABLE_RPMALLOC=ON ^
169 -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld" ^
170 -DLLVM_ENABLE_RUNTIMES="compiler-rt;openmp" ^
171 -DCOMPILER_RT_BUILD_ORC=OFF
172
173if "%force-msvc%" == "" (
174 where /q clang-cl
175 if %errorlevel% EQU 0 (
176 where /q lld-link
177 if %errorlevel% EQU 0 (
178 set common_compiler_flags=%common_compiler_flags% -fuse-ld=lld
179
180 set common_cmake_flags=%common_cmake_flags%^
181 -DCMAKE_C_COMPILER=clang-cl.exe ^
182 -DCMAKE_CXX_COMPILER=clang-cl.exe ^
183 -DCMAKE_LINKER=lld-link.exe ^
184 -DLLVM_ENABLE_LLD=ON ^
185 -DCMAKE_C_FLAGS="%common_compiler_flags%" ^
186 -DCMAKE_CXX_FLAGS="%common_compiler_flags%"
187 )
188 )
189)
190
191set common_lldb_flags=^
192 -DLLDB_RELOCATABLE_PYTHON=1 ^
193 -DLLDB_EMBED_PYTHON_HOME=OFF ^
194 -DLLDB_ENABLE_LIBXML2=OFF
195
196set cmake_profile_flags=""
197
198REM Preserve original path
199set OLDPATH=%PATH%
200
201REM Build the 32-bits and/or 64-bits binaries.
202if "%x86%" == "true" call :do_build_32 || exit /b 1
203if "%x64%" == "true" call :do_build_64_common amd64 %python64_dir% || exit /b 1
204if "%arm64%" == "true" call :do_build_64_common arm64 %pythonarm64_dir% || exit /b 1
205exit /b 0
206
207::==============================================================================
208:: Build 32-bits binaries.
209::==============================================================================
210:do_build_32
211call :set_environment %python32_dir% || exit /b 1
212call "%vsdevcmd%" -arch=x86 || exit /b 1
213@echo on
214mkdir build32_stage0
215cd build32_stage0
216call :do_build_libxml || exit /b 1
217
218REM Stage0 binaries directory; used in stage1.
219set "stage0_bin_dir=%build_dir%/build32_stage0/bin"
220set cmake_flags=^
221 %common_cmake_flags% ^
222 -DLLVM_ENABLE_RPMALLOC=OFF ^
223 -DPython3_ROOT_DIR=%PYTHONHOME% ^
224 -DLIBXML2_INCLUDE_DIR=%libxmldir%/include/libxml2 ^
225 -DLIBXML2_LIBRARIES=%libxmldir%/lib/libxml2s.lib
226
227cmake -GNinja %cmake_flags% %llvm_src%\llvm || exit /b 1
228ninja || ninja || ninja || exit /b 1
229REM ninja check-llvm || ninja check-llvm || ninja check-llvm || exit /b 1
230REM ninja check-clang || ninja check-clang || ninja check-clang || exit /b 1
231ninja check-lld || ninja check-lld || ninja check-lld || exit /b 1
232REM ninja check-runtimes || ninja check-runtimes || ninja check-runtimes || exit /b 1
233REM ninja check-clang-tools || ninja check-clang-tools || ninja check-clang-tools || exit /b 1
234cd..
235
236REM CMake expects the paths that specifies the compiler and linker to be
237REM with forward slash.
238set all_cmake_flags=^
239 %cmake_flags% ^
240 -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld;lldb;" ^
241 %common_lldb_flags% ^
242 -DPYTHON_HOME=%PYTHONHOME% ^
243 -DCMAKE_C_COMPILER=%stage0_bin_dir%/clang-cl.exe ^
244 -DCMAKE_CXX_COMPILER=%stage0_bin_dir%/clang-cl.exe ^
245 -DCMAKE_LINKER=%stage0_bin_dir%/lld-link.exe ^
246 -DCMAKE_AR=%stage0_bin_dir%/llvm-lib.exe ^
247 -DCMAKE_RC=%stage0_bin_dir%/llvm-windres.exe
248set cmake_flags=%all_cmake_flags:\=/%
249
250mkdir build32
251cd build32
252cmake -GNinja %cmake_flags% %llvm_src%\llvm || exit /b 1
253ninja || ninja || ninja || exit /b 1
254REM ninja check-llvm || ninja check-llvm || ninja check-llvm || exit /b 1
255REM ninja check-clang || ninja check-clang || ninja check-clang || exit /b 1
256ninja check-lld || ninja check-lld || ninja check-lld || exit /b 1
257REM ninja check-runtimes || ninja check-runtimes || ninja check-runtimes || exit /b 1
258REM ninja check-clang-tools || ninja check-clang-tools || ninja check-clang-tools || exit /b 1
259ninja package || exit /b 1
260cd ..
261
262exit /b 0
263::==============================================================================
264
265::==============================================================================
266:: Build 64-bits binaries (common function for both x64 and arm64)
267::==============================================================================
268:do_build_64_common
269set arch=%1
270set python_dir=%2
271
272call :set_environment %python_dir% || exit /b 1
273call "%vsdevcmd%" -arch=%arch% || exit /b 1
274@echo on
275mkdir build_%arch%_stage0
276cd build_%arch%_stage0
277call :do_build_libxml || exit /b 1
278
279REM Stage0 binaries directory; used in stage1.
280set "stage0_bin_dir=%build_dir%/build_%arch%_stage0/bin"
281set cmake_flags=^
282 %common_cmake_flags% ^
283 -DPython3_ROOT_DIR=%PYTHONHOME% ^
284 -DLIBXML2_INCLUDE_DIR=%libxmldir%/include/libxml2 ^
285 -DLIBXML2_LIBRARIES=%libxmldir%/lib/libxml2s.lib ^
286 -DCLANG_DEFAULT_LINKER=lld
287if "%arch%"=="arm64" (
288 set cmake_flags=%cmake_flags% ^
289 -DCOMPILER_RT_BUILD_SANITIZERS=OFF
290)
291
292cmake -GNinja %cmake_flags% ^
293 -DLLVM_TARGETS_TO_BUILD=Native ^
294 %llvm_src%\llvm || exit /b 1
295ninja || ninja || ninja || exit /b 1
296ninja check-llvm || ninja check-llvm || ninja check-llvm || exit /b 1
297ninja check-clang || ninja check-clang || ninja check-clang || exit /b 1
298ninja check-lld || ninja check-lld || ninja check-lld || exit /b 1
299if "%arch%"=="amd64" (
300 ninja check-runtimes || ninja check-runtimes || ninja check-runtimes || exit /b 1
301)
302ninja check-clang-tools || ninja check-clang-tools || ninja check-clang-tools || exit /b 1
303ninja check-clangd || ninja check-clangd || ninja check-clangd || exit /b 1
304cd..
305
306REM CMake expects the paths that specifies the compiler and linker to be
307REM with forward slash.
308set all_cmake_flags=^
309 %cmake_flags% ^
310 -DCMAKE_C_COMPILER=%stage0_bin_dir%/clang-cl.exe ^
311 -DCMAKE_CXX_COMPILER=%stage0_bin_dir%/clang-cl.exe ^
312 -DCMAKE_LINKER=%stage0_bin_dir%/lld-link.exe ^
313 -DCMAKE_AR=%stage0_bin_dir%/llvm-lib.exe ^
314 -DCMAKE_RC=%stage0_bin_dir%/llvm-windres.exe
315if "%arch%"=="arm64" (
316 set all_cmake_flags=%all_cmake_flags% ^
317 -DCPACK_SYSTEM_NAME=woa64
318)
319set cmake_flags=%all_cmake_flags:\=/%
320
321mkdir build_%arch%
322cd build_%arch%
323call :do_generate_profile || exit /b 1
324cmake -GNinja %cmake_flags% ^
325 -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld;lldb;flang;mlir" ^
326 %common_lldb_flags% ^
327 -DPYTHON_HOME=%PYTHONHOME% ^
328 %cmake_profile_flags% %llvm_src%\llvm || exit /b 1
329ninja || ninja || ninja || exit /b 1
330ninja check-llvm || ninja check-llvm || ninja check-llvm || exit /b 1
331ninja check-clang || ninja check-clang || ninja check-clang || exit /b 1
332ninja check-lld || ninja check-lld || ninja check-lld || exit /b 1
333if "%arch%"=="amd64" (
334 ninja check-runtimes || ninja check-runtimes || ninja check-runtimes || exit /b 1
335)
336ninja check-clang-tools || ninja check-clang-tools || ninja check-clang-tools || exit /b 1
337ninja check-clangd || ninja check-clangd || ninja check-clangd || exit /b 1
338REM ninja check-flang || ninja check-flang || ninja check-flang || exit /b 1
339REM ninja check-mlir || ninja check-mlir || ninja check-mlir || exit /b 1
340REM ninja check-lldb || ninja check-lldb || ninja check-lldb || exit /b 1
341ninja package || exit /b 1
342
343:: generate tarball with install toolchain only off
344if "%arch%"=="amd64" (
345 set filename=clang+llvm-%version%-x86_64-pc-windows-msvc
346) else (
347 set filename=clang+llvm-%version%-aarch64-pc-windows-msvc
348)
349cmake -GNinja %cmake_flags% %cmake_profile_flags% -DLLVM_INSTALL_TOOLCHAIN_ONLY=OFF ^
350 -DCMAKE_INSTALL_PREFIX=%build_dir%/%filename% ..\llvm-project\llvm || exit /b 1
351ninja install || exit /b 1
352:: check llvm_config is present & returns something
353%build_dir%/%filename%/bin/llvm-config.exe --bindir || exit /b 1
354cd ..
3557z a -ttar -so %filename%.tar %filename% | 7z a -txz -si %filename%.tar.xz
356
357exit /b 0
358
359::==============================================================================
360:: Set PATH and some environment variables.
361::==============================================================================
362:set_environment
363REM Restore original path
364set PATH=%OLDPATH%
365
366set python_dir=%1
367
368REM Set Python environment
369if "%local-python%" == "true" (
370 FOR /F "delims=" %%i IN ('where python.exe ^| head -1') DO set python_exe=%%i
371 set PYTHONHOME=!python_exe:~0,-11!
372) else (
373 %python_dir%/python.exe --version || exit /b 1
374 set PYTHONHOME=%python_dir%
375)
376set PATH=%PYTHONHOME%;%PATH%
377
378set "VSCMD_START_DIR=%build_dir%"
379
380exit /b 0
381
382::=============================================================================
383
384::==============================================================================
385:: Build libxml.
386::==============================================================================
387:do_build_libxml
388mkdir libxmlbuild
389cd libxmlbuild
390cmake -GNinja -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=install ^
391 -DBUILD_SHARED_LIBS=OFF -DLIBXML2_WITH_C14N=OFF -DLIBXML2_WITH_CATALOG=OFF ^
392 -DLIBXML2_WITH_DEBUG=OFF -DLIBXML2_WITH_DOCB=OFF -DLIBXML2_WITH_FTP=OFF ^
393 -DLIBXML2_WITH_HTML=OFF -DLIBXML2_WITH_HTTP=OFF -DLIBXML2_WITH_ICONV=OFF ^
394 -DLIBXML2_WITH_ICU=OFF -DLIBXML2_WITH_ISO8859X=OFF -DLIBXML2_WITH_LEGACY=OFF ^
395 -DLIBXML2_WITH_LZMA=OFF -DLIBXML2_WITH_MEM_DEBUG=OFF -DLIBXML2_WITH_MODULES=OFF ^
396 -DLIBXML2_WITH_OUTPUT=ON -DLIBXML2_WITH_PATTERN=OFF -DLIBXML2_WITH_PROGRAMS=OFF ^
397 -DLIBXML2_WITH_PUSH=OFF -DLIBXML2_WITH_PYTHON=OFF -DLIBXML2_WITH_READER=OFF ^
398 -DLIBXML2_WITH_REGEXPS=OFF -DLIBXML2_WITH_RUN_DEBUG=OFF -DLIBXML2_WITH_SAX1=OFF ^
399 -DLIBXML2_WITH_SCHEMAS=OFF -DLIBXML2_WITH_SCHEMATRON=OFF -DLIBXML2_WITH_TESTS=OFF ^
400 -DLIBXML2_WITH_THREADS=ON -DLIBXML2_WITH_THREAD_ALLOC=OFF -DLIBXML2_WITH_TREE=ON ^
401 -DLIBXML2_WITH_VALID=OFF -DLIBXML2_WITH_WRITER=OFF -DLIBXML2_WITH_XINCLUDE=OFF ^
402 -DLIBXML2_WITH_XPATH=OFF -DLIBXML2_WITH_XPTR=OFF -DLIBXML2_WITH_ZLIB=OFF ^
403 -DCMAKE_MSVC_RUNTIME_LIBRARY=MultiThreaded ^
404 ../../libxml2-v2.9.12 || exit /b 1
405ninja install || exit /b 1
406set libxmldir=%cd%\install
407set "libxmldir=%libxmldir:\=/%"
408cd ..
409exit /b 0
410
411::==============================================================================
412:: Generate a PGO profile.
413::==============================================================================
414:do_generate_profile
415REM Build Clang with instrumentation.
416mkdir instrument
417cd instrument
418cmake -GNinja %cmake_flags% -DLLVM_TARGETS_TO_BUILD=Native ^
419 -DLLVM_BUILD_INSTRUMENTED=IR %llvm_src%\llvm || exit /b 1
420ninja clang || ninja clang || ninja clang || exit /b 1
421set instrumented_clang=%cd:\=/%/bin/clang-cl.exe
422cd ..
423REM Use that to build part of llvm to generate a profile.
424mkdir train
425cd train
426cmake -GNinja %cmake_flags% ^
427 -DCMAKE_C_COMPILER=%instrumented_clang% ^
428 -DCMAKE_CXX_COMPILER=%instrumented_clang% ^
429 -DLLVM_ENABLE_PROJECTS=clang ^
430 -DLLVM_TARGETS_TO_BUILD=Native ^
431 %llvm_src%\llvm || exit /b 1
432REM Drop profiles generated from running cmake; those are not representative.
433del ..\instrument\profiles\*.profraw
434ninja tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/Sema.cpp.obj
435cd ..
436set profile=%cd:\=/%/profile.profdata
437%stage0_bin_dir%\llvm-profdata merge -output=%profile% instrument\profiles\*.profraw || exit /b 1
438set common_compiler_flags=%common_compiler_flags% -Wno-backend-plugin
439set cmake_profile_flags=-DLLVM_PROFDATA_FILE=%profile% ^
440 -DCMAKE_C_FLAGS="%common_compiler_flags%" ^
441 -DCMAKE_CXX_FLAGS="%common_compiler_flags%"
442exit /b 0
443
444::=============================================================================
445:: Parse command line arguments.
446:: The format for the arguments is:
447:: Boolean: --option
448:: Value: --option<separator>value
449:: with <separator> being: space, colon, semicolon or equal sign
450::
451:: Command line usage example:
452:: my-batch-file.bat --build --type=release --version 123
453:: It will create 3 variables:
454:: 'build' with the value 'true'
455:: 'type' with the value 'release'
456:: 'version' with the value '123'
457::
458:: Usage:
459:: set "build="
460:: set "type="
461:: set "version="
462::
463:: REM Parse arguments.
464:: call :parse_args %*
465::
466:: if defined build (
467:: ...
468:: )
469:: if %type%=='release' (
470:: ...
471:: )
472:: if %version%=='123' (
473:: ...
474:: )
475::=============================================================================
476:parse_args
477 set "arg_name="
478 :parse_args_start
479 if "%1" == "" (
480 :: Set a seen boolean argument.
481 if "%arg_name%" neq "" (
482 set "%arg_name%=true"
483 )
484 goto :parse_args_done
485 )
486 set aux=%1
487 if "%aux:~0,2%" == "--" (
488 :: Set a seen boolean argument.
489 if "%arg_name%" neq "" (
490 set "%arg_name%=true"
491 )
492 set "arg_name=%aux:~2,250%"
493 ) else (
494 set "%arg_name%=%1"
495 set "arg_name="
496 )
497 shift
498 goto :parse_args_start
499
500:parse_args_done
501exit /b 0
502