How to build the latest clang-tidy?

11,559

Solution 1

EDIT: this answer is out of date — the LLVM project has moved to a single git repository at https://github.com/llvm/llvm-project. See answers below for updated instructions.


clang-tidy is intended to be built inside a checkout of llvm/clang, and depends on CMake macros from the llvm project. You should check out the llvm repo, then the clang repo inside llvm/tools/clang, then the clang-tools-extra repo inside llvm/tools/clang/tools/extra. Then you can run CMake on the top-level directory, and make clang-tidy should work.

If you're not interested in building it yourself, it looks like the Homebrew formula for LLVM also includes the extra tools: https://github.com/Homebrew/homebrew-core/blob/382d3defb5bc48ce2dccd17261be70c4ada9a124/Formula/llvm.rb#L181

Solution 2

Up-to-date steps:

git clone https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir build 
cd build
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" ../llvm
make install clang-tidy

Reference, ninja, and other details: my own blog post.

Solution 3

I had same problem as Per Mildner. Got is solved with slightly modified code YvesgereY posted (I don't have enough reputation to post a comment to that answer, hence a new answer instead).

In short, I added -G "Unix Makefiles" to cmake. Without this option, no makefile will be generated. Also, I used -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;". It didn't work when just clang-tools-extra was specified.

Here is the whole snippet:

git clone https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir build 
cd build
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=RelWithDebInfo -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;" ../llvm
make -j8 install-clang-tidy

Solution 4

@jtbandes: Thank you for the information.

I'd like to share these explicit steps for us noobs:

1. Download the released sources from LLVM Download Page

2. Detar each of these into the proper directory:

$ tar -zxvf <download_dir_path>/llvm-6.0.1.src.tar.xz
$ cd llvm-6.0.1.src/tools
$ tar -zxcf <download_dir_path>/cfe-6.0.1.src.tar.xz
$ cd llvm-6.0.1.src/tools/cfe-6.0.1.src/tools
$ tar -zxvf <download_dir_path>/clang-tools-extra-6.0.1.src.tar.xz

Results in a directory llvm-6.0.1.src/tools/cfe-6.0.1.src/tools/clang-tools-extra-6.0.1.src/clang-tidy; Which is incorrect. The lang-tools-extra-6.0.1.src needs to be renamed to extra (as mentioned by @jtbandes).

3. So rename it or provide a symbolic link:

$ cd llvm-6.0.1.src/tools/cfe-6.0.1.src/tools
$ mv clang-tools-extra-6.0.1.src extra
or
$ ln -s clang-tools-extra-6.0.1.src extra

The path llvm-6.0.1.src/tools/cfe-6.0.1.src/tools/extra/clang-tidy should now be valid

4. Build it:

$ cd llvm-6.0.1.src
$ mkdir build
$ cd build
$ cmake ..
$ make 

Everything should make without errors or warnings.

5. Build Output:

The build output can be found in llvm-6.0.1.src/build/bin.

Solution 5

For everyone who are looking for latest (LLVM 11) Windows build instructions (ensure CMake, Visual Studio 2019 and git are installed and set in PATH):

git clone --config core.autocrlf=false https://github.com/llvm/llvm-project.git

cd llvm-project
mkdir build
cd build

cmake -G "Visual Studio 16 2019" -Thost=x64 -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" ../llvm
cmake --build . --target clang-tidy --config RelWithDebInfo --parallel
cmake --build . --target clang-query --config RelWithDebInfo --parallel
Share:
11,559

Related videos on Youtube

ruipacheco
Author by

ruipacheco

Updated on June 13, 2022

Comments

  • ruipacheco
    ruipacheco about 2 years

    I've tried to build clang-tidy from sources but it complains about an undefined CMake command:

    CMake Error at clang-apply-replacements/CMakeLists.txt:5 (add_clang_library):
      Unknown CMake command "add_clang_library".
    
    
    CMake Warning (dev) in CMakeLists.txt:
      No cmake_minimum_required command is present.  A line of code such as
    
        cmake_minimum_required(VERSION 3.9)
    
      should be added at the top of the file.  The version specified may be lower
      if you wish to support older CMake versions for this project.  For more
      information run "cmake --help-policy CMP0000".
    This warning is for project developers.  Use -Wno-dev to suppress it.
    
    -- Configuring incomplete, errors occurred!
    

    How can I build clang-tidy or, alternatively, how can I install the latest version on macOS?

    • jww
      jww over 5 years
      Also see Noloader | build-llvm on GitHib. It is a shell script to download and build LLVM and components. As of this writing it builds the latest release tarballs, which are 7.0.0. At minimum it performs download and configures directory structures correctly so you don't waste time on it. (The LLVM project should supply a script like this for all developers to use).
  • ruipacheco
    ruipacheco over 6 years
    So I need to checkout clang inside llvm/tools and then clang-tools-extra inside llvm/clang/tools/extra? In total 3 checkout 3 repositories?
  • Mine
    Mine about 6 years
    Well explained...except I did get an error. Transforms/IPO/PassManagerBuilder.cpp:835: undefined reference to 'llvm::createSLVPectorizerPass()' then collect2: error: ld returned 1 exit status
  • TUNAPRO1234
    TUNAPRO1234 about 6 years
    What platform were you building under?
  • dawid
    dawid over 4 years
    You have a type in the detar commands. Should be zxvf not zxcf. Also there is another typo where it says lang-tools-extra-6.0.1.src instead of clang-tools-extra-6.0.1.src.
  • Per Mildner
    Per Mildner over 4 years
    Tried this today but make complained that there is no target install-clang-tidy.
  • 9cvele3
    9cvele3 over 4 years
    You should be aware that RelWithDebInfo will result in 1.8GB large clang-tidy. Better to use just Release instead.
  • Vector
    Vector about 4 years
    @PerMildner Try the solution from 9cvele3, it worked for me on llvm 10.0
  • wreckgar23
    wreckgar23 over 3 years
    I needed to add -DLLVM_ENABLE_LIBXML2=OFF on OSX
  • Sebastian
    Sebastian over 2 years
    @PerMildner make install clang-tidy, without semicolon after install
  • kkpattern
    kkpattern over 2 years
    I'm building llvm 13. This is the right answer for me. only clang-tools-extra is not enough.
  • PJ127
    PJ127 about 2 years
    Thank you. I could not finish the build, it is taking about 50 Go on my disk, which is now full. Is this normal? Is there an easier way to compile clang-tidy only.
  • Sébastien Mascha
    Sébastien Mascha about 2 years
    I suggest you use --target clang-tidy to only build this executable.
  • Sébastien Mascha
    Sébastien Mascha about 2 years
    Therefore, to build only clang-tidy, one could use the following: clone the repo git clone https://github.com/llvm/llvm-project.git; cd llvm-project, create the build folder: cmake -S llvm -B build -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_PROJECTS="clang-tools-extra" and finally building the clang-tidy target: cmake --build build/ -j 16 --target clang-tidy --config Release