30

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?

ruipacheco
  • 15,025
  • 19
  • 82
  • 138
  • Also see [Noloader | build-llvm](https://github.com/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). – jww Nov 16 '18 at 17:59

7 Answers7

26

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.

Thomas Goyne
  • 8,010
  • 32
  • 30
YvesgereY
  • 3,778
  • 1
  • 20
  • 19
  • 4
    Tried this today but make complained that there is no target `install-clang-tidy`. – Per Mildner Jan 23 '20 at 12:24
  • 1
    @PerMildner Try the solution from 9cvele3, it worked for me on llvm 10.0 – Vector May 09 '20 at 14:09
  • @PerMildner `make install clang-tidy`, without semicolon after install – Sebastian Oct 14 '21 at 08:48
  • 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. – PJ127 Mar 31 '22 at 08:03
  • I suggest you use --target clang-tidy to only build this executable. – Sébastien Mascha May 05 '22 at 22:30
  • 1
    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` – Sébastien Mascha May 05 '22 at 22:34
  • 1
    change RelWithDebInfo to Release, otherwise the target binary is too big – Caster Jul 27 '22 at 00:36
17

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

jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • 2
    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? – ruipacheco Nov 13 '17 at 12:10
17

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
9cvele3
  • 313
  • 2
  • 10
13

@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.

natersoz
  • 1,674
  • 2
  • 19
  • 29
  • 1
    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 – Mine May 19 '18 at 08:18
  • What platform were you building under? – natersoz May 20 '18 at 02:04
  • 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`. – dawid Dec 17 '19 at 14:49
2

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
evg656e
  • 644
  • 7
  • 10
1

This worked for me:

mkdir build

files="
llvm-12.0.1.src.tar.xz
clang-12.0.1.src.tar.xz
clang-tools-extra-12.0.1.src.tar.xz
"

for f in $files; do
  echo "Untar $f"
  tar xf $f
done

mv llvm-12.0.1.src llvm
mv clang-12.0.1.src llvm/tools/clang
mv clang-tools-extra-12.0.1.src llvm/tools/clang/tools/extra

cd build
cmake -G "Unix Makefiles" \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX=/usr/local/llvm \
  -DCLANG_BUILD_TOOLS=ON \
  ../llvm
make -j16 install
Kjeld
  • 131
  • 1
  • 3
0

As of LLVM 14.0.0, sparse checkouts do no longer work (at least temporarily) and the top-level directory contains no CMakeLists.txt. I believe the tree layout has changed after LLVM 13.0.1. In consequence, none of the approaches here worked without quite some modification.

Here is how you can build version 15.0.0git (the most recent at the time of this writing). See related issue: https://github.com/llvm/llvm-project/issues/53281.

First, get the compressed code or clone with git (slower)

$ wget "https://github.com/llvm/llvm-project/archive/refs/heads/main.zip" -O llvm.zip
$ unzip llvm.zip

As usual, create a build directory and run cmake in the llvm directory.

$ mkdir /build
$ cd /build
$ cmake -G "Unix Makefiles" \
      -DCMAKE_INSTALL_PREFIX=/usr/local/llvm \
      -DCMAKE_BUILD_TYPE=Release \
      -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" \
      /llvm-project-main/llvm

Navigate downwards in the generated files and only build clang-tidy.

$ cd /build/tools/clang/tools/extra/clang-tidy
$ make install

Cmake will install to /usr/local/llvm. Also, if you want to check out a specific version, use tags in the first step like this:

$ wget "https://github.com/llvm/llvm-project/archive/refs/tags/llvmorg-14.0.0.zip"

Note that you need to supply the matching builtin headers for running clang-tidy, which are located in GitHub under llvm-project/clang/lib/Headers and can by pointed to with -extra-arg=-I/path/to/builtin/headers.

  • Do you know how to build clang-tidy which will see the headers automatically? The default Ubuntu clang-tidy does not need to provied path to clang headers (e.g. `stdarg.h`)... – Mi-La Apr 24 '23 at 09:28
  • I just found out that when you install also `clang-resource-headers` via `make install-clang-resource-headers`, the headers will be available for `clang-tidy` during running. – Mi-La Apr 24 '23 at 10:52