Skip to content
This repository was archived by the owner on Apr 25, 2023. It is now read-only.

Commit 48ebc80

Browse files
authored
Merge pull request #16 from sourcegraph/simplify-cmake
Simplify cmake
2 parents 0e7dd27 + 2e40f1a commit 48ebc80

File tree

3 files changed

+109
-45
lines changed

3 files changed

+109
-45
lines changed

CMakeLists.txt

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,30 @@ project(LSIFClang)
22
cmake_minimum_required(VERSION 3.16)
33
set(CMAKE_CXX_STANDARD 17)
44
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
56

67
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/)
78
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
89

9-
set (search_paths
10-
$ENV{PATH_TO_LLVM}
11-
$ENV{PATH_TO_LLVM}/lib/cmake
12-
$ENV{PATH_TO_LLVM}/lib/cmake/llvm
13-
$ENV{PATH_TO_LLVM}/lib/cmake/clang
14-
$ENV{PATH_TO_LLVM}/share/clang/cmake/
15-
$ENV{PATH_TO_LLVM}/share/llvm/cmake/
16-
)
10+
if (PATH_TO_LLVM)
11+
set (search_paths
12+
${PATH_TO_LLVM}
13+
${PATH_TO_LLVM}/lib/cmake
14+
${PATH_TO_LLVM}/lib/cmake/llvm
15+
${PATH_TO_LLVM}/lib/cmake/clang
16+
${PATH_TO_LLVM}/share/clang/cmake/
17+
${PATH_TO_LLVM}/share/llvm/cmake/
18+
)
19+
endif()
20+
21+
message(STATUS "${search_paths}")
1722

18-
find_package(LLVM REQUIRED CONFIG
19-
PATHS ${search_paths}
20-
NO_DEFAULT_PATH)
2123
find_package(Clang REQUIRED CONFIG
22-
PATHS ${search_paths}
23-
NO_DEFAULT_PATH)
24+
PATHS ${search_paths})
25+
26+
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
27+
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
28+
message(STATUS "Using ClangConfig.cmake in: ${CLANG_CMAKE_DIR}")
2429

2530
include_directories(${LLVM_INCLUDE_DIRS})
2631

README.md

Lines changed: 88 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,129 @@ This project is a fork of [clangd](https://clangd.llvm.org/) with patches to add
44

55
This project has only been tested extensively on C++ projects, but C and Objective C projects should both be supported as well following the same instructions.
66

7-
# Installation
7+
# Alternatives for C++ Projects
8+
9+
If you can't get `lsif-clang` working with your project, first file an issue! We want this to work everywhere.
10+
But the C++ ecosystem is fragmented, and it's possible that your project simply won't play nice with the `clang` toolchain.
11+
[lsif-cpp](https://github.com/sourcegraph/lsif-cpp) is also available, which acts as a plugin for arbitrary C++ compilers and might therefore be compatible.
12+
But it has several major defects compared to `lsif-clang` (it is much slower and does not provide hovers), and is not the recommended option.
813

9-
## Dependencies
14+
# Usage
1015

11-
This project depends on LLVM and Clang. The code builds against LLVM and Clang version 10, but can index a wide variety of code. Please file an issue if you need to build against a different version of LLVM or Clang and we can start adding some version pragmas! You can try finding the location of your llvm installation by running `readlink -f $(which clang)`. On my computer, this returns `/usr/lib/llvm-10/bin/clang`, so the installation path is `/usr/lib/llvm-10`.
16+
There are 4 steps, and instructions for each can vary by platform and build system.
1217

13-
## Building
14-
The project builds with CMake, so if you know what you're doing you can configure it yourself. For sensible defaults:
18+
1. Install dependencies
19+
1. Build lsif-clang
20+
1. Generate a compilation database.
21+
1. Run lsif-clang.
22+
23+
## Quick example
24+
Here's how you would build an index of the lsif-clang tool on Ubuntu 20.04.
1525

1626
```sh
17-
PATH_TO_LLVM=<path> ./config.sh build
18-
cd build
19-
make -j8
20-
sudo make install
27+
apt install llvm-10 clang clang-10 libclang-10-dev cmake `# install dependencies`
28+
git clone https://github.com/sourcegraph/lsif-clang && cd lsif-clang `# get the code`
29+
cmake -B build `# configure lsif-clang`
30+
make -C build -j8 `# build lsif-clang`
31+
ln -s $(pwd)/build/compile_commands.json ./ `# link the compilation database to the project root`
32+
./build/bin/lsif-clang --project-root=$(pwd) --executor=all-TUs compile_commands.json > dump.lsif `# generate an index`
2133
```
2234

23-
`PATH_TO_LLVM` should point to the llvm installation path from the previous step. The `8` in `make -j8` should be the number of threads you wish to allocate to the build (it's fairly small so it shouldn't matter much, but `make` is single threaded by default).
35+
The following sections provide detailed explanations of each step and variations on the commands for different platforms and build systems.
36+
37+
## Install dependencies
2438

25-
## Give it a whirl!
39+
This project depends on LLVM and Clang. lsif-clang itself should be built against LLVM and Clang version 10, and can index any code Clang 10 can compile. Work is ongoing to compile against other versions of LLVM. Here are instructions to get the dependencies on a few platforms:
2640

27-
Assuming you followed the steps above, do the following from this project's root directory:
41+
### Ubuntu 20.04
2842

2943
```sh
30-
ln -s $(pwd)/build/compile_commands.json ./
31-
lsif-clang --project-root=$(pwd) --executor=all-TUs compile_commands.json > dump.lsif
44+
apt install llvm-10 clang clang-10 libclang-10-dev cmake
3245
```
3346

34-
Inspect the file when it's done, you should see lots of glorious JSON!
47+
### MacOS
3548

36-
# Usage
49+
```sh
50+
brew install llvm cmake
51+
```
3752

38-
## Compilation Database
53+
## Build lsif-clang
54+
Here is a minimal example, known extra steps for specific platforms follow:
55+
56+
```sh
57+
cmake -B build
58+
make -C build -j8
59+
```
60+
61+
### MacOS
62+
Add the following extra argument to the `cmake` step:
63+
```sh
64+
cmake -B build -DPATH_TO_LLVM=/usr/local/opt/lib
65+
```
66+
67+
## Generate a compilation database
68+
69+
`lsif-clang` itself is configured to do this automatically, so to test the that the tool built properly you can simply sym-link it to the project root from the build directory and skip to [running lsif-clang]().
70+
71+
From the project root:
72+
```sh
73+
ln -s $(pwd)/build/compile_commands.json ./
74+
```
3975

40-
The tool depends on having a [JSON compilation database](https://clang.llvm.org/docs/JSONCompilationDatabase.html) available, which is generated differently depending on what build system is in use. Please get in touch if you're having trouble generating a compilation database, we'd be happy to help troubleshoot!
76+
Instructions for generating this compilation database for various build systems follows:
4177

4278
### CMake
4379

44-
Add `-DCMAKE_EXPORT_COMPILE_COMMANDS=ON` to your `cmake` invocation. The database will get generated in your build directory, so you should symlink it to the source root.
80+
If a project builds with CMake, you can ensure that a compilation database is generated in the build directory with the following flag:
81+
```sh
82+
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
83+
```
4584

46-
### Make and others
85+
For some projects, we've noticed that CMake generates a faulty database, but that the actual build step can output more sensible values. The Ninja build tool provides a convenient mechanism for this. Assuming Ninja is installed:
4786

48-
Install the [Bear](https://github.com/rizsotto/Bear) tool and run `bear make`, or `bear <your-build-command>`. This tool is build system agnostic so it's a good fallback option.
87+
```sh
88+
cmake <normal args> -G Ninja
89+
cd <build dir>
90+
ninja -t compdb > compile_commands.json
91+
```
92+
93+
This database will also contain irrelevant entries which will make lsif-clang output quite noisy but still functional. To filter the irrelevant entries, inspect the `compile_commands.json` and find an entry for an actual c++ compile command. Here's an example for me:
94+
```json
95+
{
96+
"directory": "/home/arrow/sourcegraph/lsif-clang/build2",
97+
"command": "/usr/bin/c++ -I../ -I/usr/lib/llvm-10/include -std=gnu++17 -o CMakeFiles/clangDaemonFork.dir/AST.cpp.o -c /home/arrow/sourcegraph/lsif-clang/AST.cpp",
98+
"file": "/home/arrow/sourcegraph/lsif-clang/AST.cpp"
99+
}
100+
```
101+
102+
I can then use the "command" value of `/usr/bin/c++` in the following jq snippet:
103+
```sh
104+
ninja -t compdb | jq '[ .[] | select(.command | startswith("/usr/bin/c++")) ] > compile_commands.json'
105+
```
49106

50107
### Bazel
51108

52109
Use the [bazel-compilation-database](https://github.com/grailbio/bazel-compilation-database) tool.
53110

54-
## Indexing
111+
### Make and others
112+
113+
Install the [Bear](https://github.com/rizsotto/Bear) tool and run `bear make`, or `bear <your-build-command>`. This tool is build system agnostic so it's a good fallback option.
114+
115+
## Run lsif-clang
55116

56-
Once you have `compile_commands.json` at the root of your source tree, you can invoke `lsif-clang` like so:
117+
Once you have a `compile_commands.json` in the root of your project's source, you can use the following command to index the entire project:
57118

58119
```sh
59120
lsif-clang --project-root=$(pwd) --executor=all-TUs compile_commands.json > dump.lsif
60121
```
61122

62-
This will index the entire project. To index only some files, run:
123+
To index individual files, use:
63124

64125
```sh
65126
lsif-clang --project-root=$(pwd) file1.cpp file2.cpp ... > dump.lsif
66127
```
67128

68-
Note that this will still include lots of data about other files to properly supply hovers and such.
69-
70-
### Platform-specific instructions
71-
72-
#### On MacOS
129+
### MacOS
73130

74131
The indexer may fail to find system header files on MacOS (and possibly other systems) resulting in console error messages such as `fatal error: stdarg.h not found`.
75132

@@ -85,6 +142,6 @@ $ lsif-clang \
85142
compile_commands.json > dump.lsif
86143
```
87144

88-
# Alternatives for C++ Projects
145+
## Test the output
89146

90-
If you can't get `lsif-clang` working with your project, first file an issue! We want this to work everywhere. But the C++ ecosystem is fragmented, and it's possible that your project simply won't play nice with the `clang` toolchain. [lsif-cpp](https://github.com/sourcegraph/lsif-cpp) is also available, which acts as a plugin for arbitrary C++ compilers and might therefore be compatible. But it has several major defects compared to `lsif-clang` (it is much slower and does not provide hovers), and is not the recommended option.
147+
You can use the [lsif-validate]() tool for basic sanity checking, or [upload the index to a Sourcegraph instance]() to see the hovers, definitions, and references in action.

config.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!/bin/sh
22

3-
cmake -B $1 -S . -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_BUILD_TYPE=Release
3+
cmake -B build -S . \
4+
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
5+
-DPATH_TO_LLVM=$PATH_TO_LLVM

0 commit comments

Comments
 (0)