Skip to content

Commit 43ffd1b

Browse files
committed
setup autocomplete for c++
1 parent dbf22c7 commit 43ffd1b

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,4 @@
3939
bazel-*
4040

4141
node_modules
42+
compile_commands.json

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ Leetcode exercises
66

77
### Run C++ Tests
88
`bazel test --test_output=all //cpp:tests`
9+
10+
### Generate Compilation Database (for better autocomplete)
11+
`./generate_compilation_db.sh`

WORKSPACE

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ http_archive(
2525
strip_prefix = "rules_cc-40548a2974f1aea06215272d9c2b47a14a24e556",
2626
)
2727

28+
http_archive(
29+
name = "com_grail_bazel_compdb",
30+
strip_prefix = "bazel-compilation-database-master",
31+
urls = ["https://github.com/grailbio/bazel-compilation-database/archive/master.tar.gz"],
32+
)
33+
2834
# Fetch rules_nodejs so we can install our npm dependencies
2935
http_archive(
3036
name = "build_bazel_rules_nodejs",

cpp/BUILD

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package(default_visibility = ["//visibility:public"])
22
load("@rules_cc//cc:defs.bzl", "cc_test")
3+
load("@com_grail_bazel_compdb//:aspects.bzl", "compilation_database")
34

45
cc_library(
56
name="exercises",
@@ -14,8 +15,17 @@ cc_library(
1415
cc_test(
1516
name = "tests",
1617
srcs = glob(["**/**/*_test.cc"]),
18+
testonly = False,
1719
deps = [
1820
":exercises",
1921
"@com_google_googletest//:gtest_main"
2022
],
2123
)
24+
25+
compilation_database(
26+
name = "compilation_db",
27+
targets = [
28+
":exercises",
29+
":tests"
30+
],
31+
)

generate_compilation_db.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Command to generate the compilation database file.
2+
bazel build cpp:compilation_db
3+
4+
# Location of the compilation database file.
5+
outfile="$(bazel info bazel-bin)/cpp/compile_commands.json"
6+
7+
# Command to replace the marker for exec_root in the file.
8+
execroot=$(bazel info execution_root)
9+
sed -i.bak "s@__EXEC_ROOT__@${execroot}@" "${outfile}"
10+
11+
# The compilation database is now ready to use at this location.
12+
echo "Compilation Database: ${outfile}"
13+
14+
workspace="$(bazel info workspace)"
15+
ln -s "${outfile}" "${workspace}"

0 commit comments

Comments
 (0)