ruipacheco
ruipacheco

Reputation: 16422

How to build the latest clang-tidy?

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?

Upvotes: 36

Views: 20532

Answers (7)

YvesgereY
YvesgereY

Reputation: 3888

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.

Upvotes: 30

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.

Upvotes: 0

Kjeld
Kjeld

Reputation: 139

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

Upvotes: 1

evg656e
evg656e

Reputation: 664

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

Upvotes: 2

9cvele3
9cvele3

Reputation: 321

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

Upvotes: 16

jtbandes
jtbandes

Reputation: 118681

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

Upvotes: 18

natersoz
natersoz

Reputation: 1752

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

Upvotes: 13

Related Questions