You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 25, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+88-31Lines changed: 88 additions & 31 deletions
Original file line number
Diff line number
Diff line change
@@ -4,72 +4,129 @@ This project is a fork of [clangd](https://clangd.llvm.org/) with patches to add
4
4
5
5
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.
6
6
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.
8
13
9
-
## Dependencies
14
+
#Usage
10
15
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.
12
17
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.
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`
21
33
```
22
34
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
24
38
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:
26
40
27
-
Assuming you followed the steps above, do the following from this project's root directory:
Inspect the file when it's done, you should see lots of glorious JSON!
47
+
### MacOS
35
48
36
-
# Usage
49
+
```sh
50
+
brew install llvm cmake
51
+
```
37
52
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
+
```
39
75
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:
41
77
42
78
### CMake
43
79
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
+
```
45
84
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:
47
86
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:
Use the [bazel-compilation-database](https://github.com/grailbio/bazel-compilation-database) tool.
53
110
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
55
116
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:
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
73
130
74
131
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`.
75
132
@@ -85,6 +142,6 @@ $ lsif-clang \
85
142
compile_commands.json > dump.lsif
86
143
```
87
144
88
-
#Alternatives for C++ Projects
145
+
## Test the output
89
146
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.
0 commit comments