Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Build libclang on Windows: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
(Performance)
(Simplify + streamline article and refer to easier instructions provided by the LLVM project.)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
== Configuring builds ==
<br />
==Download source==
The LLVM Project is a collection of modular and reusable compiler and toolchain technologies, including Clang and libclang. It can be obtained by cloning its source with Git:<syntaxhighlight lang="bash">
git clone https://github.com/llvm/llvm-project.git
</syntaxhighlight>


=== MSVC (2015 and 2017) and MinGW ===
== Configure + build ==
Can be configured with Qt Creator. It's enough to manually set CMAKE_INSTALL_PREFIX, CMAKE_BUILD_TYPE and LLVM_ENABLE_RTTI
The LLVM Project already offers configure and build instructions for Windows [https://clang.llvm.org/get_started.html here]. The steps outlined under "[https://clang.llvm.org/get_started.html#:~:text=Using%20Ninja%20alongside%20Visual%20Studio Using Ninja alongside Visual Studio]" are recommended.


For MinGW there are extra flags though to link libraries into libclang.dll statically (CMAKE_C_FLAGS and CMAKE_CXX_FLAGS):
Notes:
-static-libgcc -static-libstdc++ -static


=== Clang ===
* Visual Studio 2019 and 2022 are also supported.
Configurable with cmake:
* Skip steps 5. and 6. if you want to build with MinGW GCC instead of MSVC (or set the values accordingly).
You need msvc command line and append PATH with bin directory of clang (set PATH=%PATH%;path\to\clang\bin)
* Add <code>-DCMAKE_BUILD_TYPE=Debug</code> to your configure command if you need a debug build of libclang, e.g., <code>cmake -GNinja -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Debug ..\llvm</code>
* It is recommended to add an install prefix to your configure command, e.g., <code>cmake -GNinja -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_INSTALL_PREFIX=C:\libclang\ ..\llvm</code>. Run <code>cmake -- install .</code> after the build to install libclang to the directory you provided through the install prefix.


cmake -G <Generator ("NMake Makefiles JOM" or "NMake Makefiles")> -DCMAKE_BUILD_TYPE=<Release|Debug> -DLLVM_ENABLE_RTTI=1 -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DCMAKE_INSTALL_PREFIX=path\to\install -DCMAKE_C_FLAGS="-fms-compatibility-version=19.0" -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.0" -DMSVC=1 -DCMAKE_CL_64=<1|0> path\to\llvm
===Intel Compiler===
Currently llvm requires some tweaks to compile with icl (https://software.intel.com/en-us/forums/intel-c-compiler/topic/759252 may solve some of them).


=== Intel Compiler ===
==Profile-guided builds extra configuration==
Currently llvm requires some tweaks to compile with icl (mostly inline namespaces from windows headers and binary ops with enums). After you apply tweaks for llvm and windows headers it compiles but performance is similar to msvc version so there's not much profit in icl builds currently.


== Build ==
===MSVC===
In creator and command line you can just use "jom install" or use default step from Qt Creator
 
== Profile-guided builds extra configuration ==
 
=== MSVC ===
  * First build round:
  * First build round:
  <nowiki> </nowiki> CMAKE_C_FLAGS and CMAKE_CXX_FLAGS: /GL
  <nowiki> </nowiki> CMAKE_C_FLAGS and CMAKE_CXX_FLAGS: /GL
Line 31: Line 30:
  <nowiki> </nowiki> CMAKE_EXE_LINKER_FLAGS and CMAKE_SHARED_LINKER_FLAGS: /LTCG and /USEPROFILE:PGD=same\pgd\files\path
  <nowiki> </nowiki> CMAKE_EXE_LINKER_FLAGS and CMAKE_SHARED_LINKER_FLAGS: /LTCG and /USEPROFILE:PGD=same\pgd\files\path


=== MinGW ===
===MinGW===
  * First build round:
  * First build round:
  <nowiki> </nowiki> CMAKE_C_FLAGS and CMAKE_CXX_FLAGS: -fprofile-generate=path\to\profile\files
  <nowiki> </nowiki> CMAKE_C_FLAGS and CMAKE_CXX_FLAGS: -fprofile-generate=path\to\profile\files
Line 39: Line 38:
  <nowiki> </nowiki> CMAKE_C_FLAGS and CMAKE_CXX_FLAGS: -fprofile-use=path\to\profile\files
  <nowiki> </nowiki> CMAKE_C_FLAGS and CMAKE_CXX_FLAGS: -fprofile-use=path\to\profile\files


=== Clang ===
===Clang===
Clang requires compiler-rt to build llvm with -fprofile-instr-generate flag
Clang requires compiler-rt to build llvm with -fprofile-instr-generate flag
Follow the https://compiler-rt.llvm.org/ to build it. You need additional configuration though:
Follow the https://compiler-rt.llvm.org/ to build it. You need additional configuration though:
Line 52: Line 51:
  <nowiki> </nowiki> cmake -G "NMake Makefiles JOM" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=1 -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DCMAKE_INSTALL_PREFIX=path\to\install -DCMAKE_C_FLAGS="-fms-compatibility-version=19.0 -fprofile-instr-use=path\llvm.prodata" -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.0 -fprofile-instr-use=path\llvm.prodata" -DMSVC=1 -DCMAKE_CL_64=<1|0> path\to\llvm
  <nowiki> </nowiki> cmake -G "NMake Makefiles JOM" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=1 -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DCMAKE_INSTALL_PREFIX=path\to\install -DCMAKE_C_FLAGS="-fms-compatibility-version=19.0 -fprofile-instr-use=path\llvm.prodata" -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.0 -fprofile-instr-use=path\llvm.prodata" -DMSVC=1 -DCMAKE_CL_64=<1|0> path\to\llvm


== Performance ==
===Intel Compiler===
* First build round:
cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=1 -DCMAKE_C_COMPILER=icl -DCMAKE_CXX_COMPILER=icl -DCMAKE_INSTALL_PREFIX=path\to\install -DLLVM_ENABLE_WARNINGS=0 -DCMAKE_CXX_FLAGS="/Qvc14.1 /Qprof-gen /Qprof-dirc:\icl_profiled" -DCMAKE_C_FLAGS="/Qvc14.1 /Qprof-gen /Qprof-dirc:\icl_profiled" path\to\llvm
<nowiki>*</nowiki> Training
<nowiki>*</nowiki> Second build round:
cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=1 -DCMAKE_C_COMPILER=icl -DCMAKE_CXX_COMPILER=icl -DCMAKE_INSTALL_PREFIX=path\to\install -DLLVM_ENABLE_WARNINGS=0 -DCMAKE_CXX_FLAGS="/Qvc14.1 /Qprof-use /O2 /Qip /Qprof-dirc:\icl_profiled" -DCMAKE_C_FLAGS="/Qvc14.1 /Qprof-use /O2 /Qip /Qprof-dirc:\icl_profiled" path\to\llvm
 
==Performance==
We have measurements comparison between different llvm builds. The most important there are parse and re-parse times.
We have measurements comparison between different llvm builds. The most important there are parse and re-parse times.


This is the comparison for sample file that was done for our modified llvm 3.9 builds:
This is the comparison for sample file that was done for our llvm builds (all compilers are 64-bit):
{| class="wikitable"
{| class="wikitable"
|action
|action
|LLVM_msvc2015_64
|LLVM 5.0 mingw PGO
|LLVM_mingw64
|LLVM 5.0 intel compiler PGO
|LLVM_msvc2017_64
|LLVM 3.9 mingw PGO
|LLVM_mingw64_profile
|LLVM 3.9 msvc2015
|LLVM_msvc17_profile
|LLVM 3.9 msvc2017 PGO
|LLVM_clang
|LLVM 3.9 clang PGO
|LLVM_clang_profile
|LLVM 3.9 mingw
|LLVM 3.9 clang
|LLVM 3.9 msvc2017
|-
|-
|Parsing
|Parsing
|1.306
|1.5426
|1.2874
|2.7482
|2.7482
|1.473
|1.5015
|1.5797
|1.5797
|1.7486
|1.8487
|1.8487
|1.2874
|1.473
|1.7486
|1.5015
|-
|-
|Reparsing
|Reparsing
|1.9258
|2.2318
|2.3362
|2.9665
|2.9665
|2.5521
|2.6727
|3.183
|3.183
|2.8431
|3.0255
|3.0255
|2.3362
|2.5521
|2.8431
|2.6727
|}
|}
That means that currently the fastest build is mingw with applied profile-guided optimization.
That means that currently the fastest build is mingw with applied profile-guided optimization.

Latest revision as of 12:28, 19 July 2022


Download source

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies, including Clang and libclang. It can be obtained by cloning its source with Git:

git clone https://github.com/llvm/llvm-project.git

Configure + build

The LLVM Project already offers configure and build instructions for Windows here. The steps outlined under "Using Ninja alongside Visual Studio" are recommended.

Notes:

  • Visual Studio 2019 and 2022 are also supported.
  • Skip steps 5. and 6. if you want to build with MinGW GCC instead of MSVC (or set the values accordingly).
  • Add
    -DCMAKE_BUILD_TYPE=Debug
    
    to your configure command if you need a debug build of libclang, e.g.,
    cmake -GNinja -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Debug ..\llvm
    
  • It is recommended to add an install prefix to your configure command, e.g.,
    cmake -GNinja -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_INSTALL_PREFIX=C:\libclang\ ..\llvm
    
    . Run
    cmake -- install .
    
    after the build to install libclang to the directory you provided through the install prefix.

Intel Compiler

Currently llvm requires some tweaks to compile with icl (https://software.intel.com/en-us/forums/intel-c-compiler/topic/759252 may solve some of them).

Profile-guided builds extra configuration

MSVC

* First build round:
  CMAKE_C_FLAGS and CMAKE_CXX_FLAGS: /GL
  CMAKE_EXE_LINKER_FLAGS and CMAKE_SHARED_LINKER_FLAGS: /LTCG and /GENPROFILE:PGD=pgd\files\path
* Training
  Just run parsing/completion
* Second build round:
  CMAKE_C_FLAGS and CMAKE_CXX_FLAGS: /GL
  CMAKE_EXE_LINKER_FLAGS and CMAKE_SHARED_LINKER_FLAGS: /LTCG and /USEPROFILE:PGD=same\pgd\files\path

MinGW

* First build round:
  CMAKE_C_FLAGS and CMAKE_CXX_FLAGS: -fprofile-generate=path\to\profile\files
* Training
  Just run parsing/completion
* Second build round:
  CMAKE_C_FLAGS and CMAKE_CXX_FLAGS: -fprofile-use=path\to\profile\files

Clang

Clang requires compiler-rt to build llvm with -fprofile-instr-generate flag Follow the https://compiler-rt.llvm.org/ to build it. You need additional configuration though: -DCMAKE_INSTALL_PREFIX=<path\to\llvm\install>\lib\clang\5.0.0 and a proper -DCMAKE_BUILD_TYPE

* First build round:
  cmake -G "NMake Makefiles JOM" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=1 -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DCMAKE_INSTALL_PREFIX=path\to\install -DCMAKE_C_FLAGS="-fms-compatibility-version=19.0 -fprofile-instr-generate=path\llvm-%p.profraw" -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.0 -fprofile-instr-generate=path\llvm-%p.profraw" -DMSVC=1 -DCMAKE_CL_64=<1|0> -DCMAKE_EXE_LINKER_FLAGS="<path\to\llvm\install>\lib\clang\5.0.0\lib\windows\clang_rt.profile-x86_64.lib /FORCE:MULTIPLE" path\to\llvm
* Training
* Merging train data
  llvm-profdata merge -output= llvm.profdata llvm-*.profraw
* Second build round:
  cmake -G "NMake Makefiles JOM" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=1 -DCMAKE_C_COMPILER=clang-cl -DCMAKE_CXX_COMPILER=clang-cl -DCMAKE_INSTALL_PREFIX=path\to\install -DCMAKE_C_FLAGS="-fms-compatibility-version=19.0 -fprofile-instr-use=path\llvm.prodata" -DCMAKE_CXX_FLAGS="-fms-compatibility-version=19.0 -fprofile-instr-use=path\llvm.prodata" -DMSVC=1 -DCMAKE_CL_64=<1|0> path\to\llvm

Intel Compiler

* First build round:
cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=1 -DCMAKE_C_COMPILER=icl -DCMAKE_CXX_COMPILER=icl -DCMAKE_INSTALL_PREFIX=path\to\install -DLLVM_ENABLE_WARNINGS=0 -DCMAKE_CXX_FLAGS="/Qvc14.1 /Qprof-gen /Qprof-dirc:\icl_profiled" -DCMAKE_C_FLAGS="/Qvc14.1 /Qprof-gen /Qprof-dirc:\icl_profiled" path\to\llvm
* Training
* Second build round:
cmake -G "Ninja" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_RTTI=1 -DCMAKE_C_COMPILER=icl -DCMAKE_CXX_COMPILER=icl -DCMAKE_INSTALL_PREFIX=path\to\install -DLLVM_ENABLE_WARNINGS=0 -DCMAKE_CXX_FLAGS="/Qvc14.1 /Qprof-use /O2 /Qip /Qprof-dirc:\icl_profiled" -DCMAKE_C_FLAGS="/Qvc14.1 /Qprof-use /O2 /Qip /Qprof-dirc:\icl_profiled" path\to\llvm

Performance

We have measurements comparison between different llvm builds. The most important there are parse and re-parse times.

This is the comparison for sample file that was done for our llvm builds (all compilers are 64-bit):

action LLVM 5.0 mingw PGO LLVM 5.0 intel compiler PGO LLVM 3.9 mingw PGO LLVM 3.9 msvc2015 LLVM 3.9 msvc2017 PGO LLVM 3.9 clang PGO LLVM 3.9 mingw LLVM 3.9 clang LLVM 3.9 msvc2017
Parsing 1.306 1.5426 1.2874 2.7482 1.473 1.5015 1.5797 1.7486 1.8487
Reparsing 1.9258 2.2318 2.3362 2.9665 2.5521 2.6727 3.183 2.8431 3.0255

That means that currently the fastest build is mingw with applied profile-guided optimization.