Skip to content

Commit

Permalink
Add compile_flags.txt for clangd
Browse files Browse the repository at this point in the history
This change moves to using compile_flags.txt with clangd rather than
compile_commands.json.

o Adds compile_flags.txt.
o Drops support for generating compile_commands.json with Bazel.
o Add script to create the ${workspaceFolder}/external/ symlink that
  compile_flags.txt uses.

Bug: #506
  • Loading branch information
ohodson committed Jun 25, 2023
1 parent 3c15df5 commit 92e6948
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 48 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

.idea
.DS_Store

Expand All @@ -11,8 +12,7 @@ node_modules
/npm/workerd/install.js
/npm/workerd/lib/

### Added by Hedron's Bazel Compile Commands Extractor: https://github.com/hedronvision/bazel-compile-commands-extractor
# The external link: Differs on Windows vs macOS/Linux, so we can't check it in. The pattern needs to not have a trailing / because it's a symlink on macOS/Linux.
# The external link for compile_flags.txt: Differs on Windows vs macOS/Linux, so we can't check it in. The pattern needs to not have a trailing / because it's a symlink on macOS/Linux.
/external
# Bazel output symlinks: Same reasoning as /external. You need the * because people can change the name of the directory your repository is cloned into, changing the bazel-<workspace_name> symlink.
/bazel-*
Expand Down
13 changes: 7 additions & 6 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@
}
},
{
"label": "Generate compile_commands.json",
"command": "bazel",
"args": ["run", "//:refresh_compile_commands"],
"options": {
"cwd": "${workspaceFolder}"
// Create ${workspaceFolder}/external for clangd on opening VSCode.
"label": "Symlink external directory",
"command": "${workspaceFolder}/tools/unix/create-external.sh",
"windows": {
"command": "${workspaceFolder}/tools/windows/create-external.bat",
},
"group": "build",
"problemMatcher": [],
Expand All @@ -217,7 +217,8 @@
"panel": "dedicated"
},
"runOptions": {
"runOn": "default"
"reevaluateOnRerun": false,
"runOn": "folderOpen"
}
}
]
Expand Down
5 changes: 0 additions & 5 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@capnp-cpp//src/capnp:cc_capnp_library.bzl", "cc_capnp_library")
load("@hedron_compile_commands//:refresh_compile_commands.bzl", "refresh_compile_commands")
load("@aspect_rules_js//npm:defs.bzl", "npm_link_package", "npm_package")
load("@npm//:defs.bzl", "npm_link_all_packages")
load("@npm//:capnpc-ts/package_json.bzl", capnpc_ts_bin = "bin")
Expand All @@ -15,10 +14,6 @@ cc_capnp_library(
defines = [ "WORKERD_ICU_DATA_EMBED" ],
)

refresh_compile_commands(
name = "refresh_compile_commands",
)

npm_link_all_packages(name = "node_modules")

npm_link_package(
Expand Down
16 changes: 0 additions & 16 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,3 @@ new_local_repository(
visibility = ["//visibility:public"])""",
path = "empty",
)

# ========================================================================================
# Tools

# Hedron's Compile Commands Extractor for Bazel
# https://github.com/hedronvision/bazel-compile-commands-extractor
http_archive(
name = "hedron_compile_commands",
sha256 = "ab6c6b4ceaf12b224e571ec075fd79086c52c3430993140bb2ed585b08dfc552",
strip_prefix = "bazel-compile-commands-extractor-d1e95ec162e050b04d0a191826f9bc478de639f7",
url = "https://github.com/hedronvision/bazel-compile-commands-extractor/archive/d1e95ec162e050b04d0a191826f9bc478de639f7.tar.gz",
)

load("@hedron_compile_commands//:workspace_setup.bzl", "hedron_compile_commands_setup")

hedron_compile_commands_setup()
93 changes: 93 additions & 0 deletions compile_flags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
-std=c++20
-stdlib=libc++
-xc++
-nostdinc
-Ibazel-bin
-Ibazel-bin/external/capnp-cpp/src
-Ibazel-bin/external/com_googlesource_chromium_base_trace_event_common/_virtual_includes/trace_event_common
-Iexternal/capnp-cpp/src
-Isrc
-isystem/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
-isystem/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1
-isystemC:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\include
-isystemc:\Program Files (x86)\Windows Kits\10\Include\10.0.22621.0\ucrt
-isystembazel-bin/external/v8
-isystembazel-bin/external/v8/icu
-isystembazel-bin/src
-isystemexternal/brotli/c/include
-isystemexternal/com_googlesource_chromium_icu/source/common
-isystemexternal/icu
-isystemexternal/ssl/src/include
-isystemexternal/v8
-isystemexternal/v8/include
-isystemexternal/zlib
-D_FORTIFY_SOURCE=1
-DBAZEL_CURRENT_REPOSITORY=\"\"
-DCAPNP_VERSION=11000
-DDEBUG
-DGOOGLE3
-DHAVE_DLOPEN=0
-DKJ_HAS_LIBDL
-DKJ_HAS_OPENSSL
-DKJ_HAS_ZLIB
-DKJ_SAVE_ACQUIRED_LOCK_INFO=0
-DKJ_TRACK_LOCK_BLOCKING=0
-DSQLITE_ENABLE_FTS5
-DSQLITE_ENABLE_NORMALIZE
-DSQLITE_MAX_ALLOCATION_SIZE=16777216
-DSQLITE_PRINTF_PRECISION_LIMIT=100000
-DUCONFIG_ONLY_HTML_CONVERSION=1
-DUNISTR_FROM_CHAR_EXPLICIT=
-DUNISTR_FROM_STRING_EXPLICIT=
-DUSE_CHROMIUM_ICU=1
-DU_CHARSET_IS_UTF8=1
-DU_COMMON_IMPLEMENTATION
-DU_ENABLE_DYLOAD=0
-DU_ENABLE_RESOURCE_TRACING=0
-DU_ENABLE_TRACING=1
-DU_I18N_IMPLEMENTATION
-DU_ICUDATAENTRY_IN_COMMON
-DU_USING_ICU_NAMESPACE=0
-DV8_31BIT_SMIS_ON_64BIT_ARCH
-DV8_ADVANCED_BIGINT_ALGORITHMS
-DV8_COMPRESS_POINTERS
-DV8_COMPRESS_POINTERS_IN_SHARED_CAGE
-DV8_CONCURRENT_MARKING
-DV8_DEPRECATION_WARNINGS
-DV8_ENABLE_CHECKS
-DV8_ENABLE_LAZY_SOURCE_POSITIONS
-DV8_ENABLE_TURBOFAN
-DV8_ENABLE_WEBASSEMBLY
-DV8_HAVE_TARGET_OS
-DV8_IMMINENT_DEPRECATION_WARNINGS
-DV8_SHORT_BUILTIN_CALLS
-DV8_TARGET_ARCH_X64
-DV8_TARGET_OS_LINUX
-DV8_TYPED_ARRAY_MAX_SIZE_IN_HEAP=64
-DWORKERD_ICU_DATA_EMBED
-Wall
-Werror=dangling
-Werror=implicit-fallthrough
-Werror=return-stack-address
-Werror=return-type
-Werror=switch
-Werror=uninitialized
-Werror=unreachable-code
-Werror=unused-function
-Werror=unused-lambda-capture
-Werror=unused-variable
-Wextra
-Wno-builtin-macro-redefined
-Wno-free-nonheap-object
-Wno-ignored-qualifiers
-Wno-missing-field-initializers
-Wno-sign-compare
-Wno-strict-aliasing
-Wno-unused-parameter
-Wself-assign
-Wthread-safety
-Wunused-but-set-parameter
-Wunused-function
-Wunused-lambda-capture
-Wunused-variable
-no-canonical-prefixes
25 changes: 6 additions & 19 deletions docs/vscode.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ The [.vscode/tasks.json](../.vscode/tasks.json) file provides a few useful tasks
* Bazel run all tests (dbg)
* Bazel run all tests (fastbuild)
* Bazel run all tests (opt)
* Generate compile_commands.json
* Generate rust-project.json

The keyboard shortcut for **Tasks: Run Build Task** is `shift+ctrl+b` on Linux and Windows, `shift+cmd+b` on OS X.
Expand Down Expand Up @@ -68,26 +67,14 @@ Launching "workerd test case (dbg)" will prompt for a test binary to debug, the

Launching "workerd wd-test case (dbg)" will prompt for wd-test file to provide to workerd to debug, the default is `src/workerd/api/node/path-test.wd-test`.

## Generating compile_commands.json
## Clangd code completion, navigation, language server

We use clangd for code completion and navigation with the editor. Clangd requires a `compile_commands.json` file to find and process the project source code. This can be generated from within VSCode using the **Run Build Task** command and choosing "Generate compile_commands.json" (or via `shift+ctrl+b` on Linux / Windows, `shift_cmd+b` on OS X).
We use clangd for code completion and navigation within the Visual Code. We use the simple
[compile_flags.txt](../compile_flags.txt) option provide compiler arguments for clangd to analyze sources.

Alternatively, you can generate `compile_commands.json` at the command-line directly:

```sh
bazel run //:refresh_compile_commands
```

There is an issue between workerd's bazel setup and Hedron's compile_commands.json generator (tracked in
https://github.com/cloudflare/workerd/issues/506).

If you hit any problems, you may find this helps (though it takes a long time!):

```sh
bazel clean --expunge
bazel test //...
bazel run //:refresh_compile_commands
```
In the past we used [Hedron's Bazel Compile Commands Extractor](https://github.com/hedronvision/bazel-compile-commands-extractor)
to generate a `compile_commands.json` file for clangd, but this was slow and unreliable for the `workerd` use case
(see https://github.com/cloudflare/workerd/issues/506).

## Miscellaneous tips

Expand Down
24 changes: 24 additions & 0 deletions tools/unix/create-external.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

# Create a symlink to Bazel's external/ directory. This simplifies the
# paths provided to clangd in compile_flags.txt for language server
# use.

output_path=$(bazel info output_path)
workspace=$(bazel info workspace)
external="${workspace}/external"
ln -sfF "${output_path}/../../../external" "${external}"

# Temporary warning that compile_commands.json exists and will
# interfere with the intended clangd setup.
compile_commands="${workspace}/compile_commands.json"
if [ -f "compile_commands.json" ] ; then
cat<<COMPILE_COMMANDS_WARNING
WARNING: This workspace has a compile_commands.json file, but workerd
has moved to using compile_flags.txt for clangd instead. To improve
code completion and navigation in your editor, consider running:
rm "${compile_commands}"
COMPILE_COMMANDS_WARNING
fi
17 changes: 17 additions & 0 deletions tools/windows/create-external.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@ECHO off
SETLOCAL EnableDelayedExpansion

@REM cmd.exe script to ensure external/ directory is present for clangd.

FOR /f %%i IN ('bazel info output_path') do SET "output_path=%%i"
FOR /f %%i IN ('bazel info workspace') do SET "workspace=%%i"
SET "external=%output_path%\..\..\..\external"

IF NOT EXIST "%workspace%\external" (
mklink /J "%workspace%\external" "%external%"
)

set "compile_commands=%workspace%\compile_commands.json"
if EXIST "%compile_commands%" (
del /q "%compile_commands%"
)

0 comments on commit 92e6948

Please sign in to comment.