Skip to content

Commit

Permalink
devex: initial commit of devcontainer setup (#11207)
Browse files Browse the repository at this point in the history
Risk Level: Low (not affecting any output)
Testing: local
Docs Changes: `.devcontainer/README.md`
Release Notes: N/A

Signed-off-by: Lizan Zhou <lizan@tetrate.io>
  • Loading branch information
lizan authored May 20, 2020
1 parent a691438 commit 01cc47a
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 3 deletions.
1 change: 1 addition & 0 deletions .devcontainer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
devcontainer.env
21 changes: 21 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM gcr.io/envoy-ci/envoy-build:04f06115b6ee7cfea74930353fb47a41149cbec3

ARG USERNAME=vscode
ARG USER_UID=501
ARG USER_GID=$USER_UID

ENV BUILD_DIR=/build
ENV ENVOY_STDLIB=libstdc++

ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get -y update \
&& apt-get -y install --no-install-recommends libpython2.7 net-tools psmisc vim 2>&1 \
#
# Create a non-root user to use if preferred - see https://aka.ms/vscode-remote/containers/non-root-user.
&& groupadd --gid $USER_GID $USERNAME \
&& useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m $USERNAME \
# [Optional] Add sudo support for non-root user
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME

ENV DEBIAN_FRONTEND=
35 changes: 35 additions & 0 deletions .devcontainer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Envoy Dev Container (experimental)

This directory contains some experimental tools for Envoy Development in [VSCode Remote - Containers](https://code.visualstudio.com/docs/remote/containers).

## How to use

Open with VSCode with the Container extension installed. Follow the [official guide](https://code.visualstudio.com/docs/remote/containers) to open this
repository directly from GitHub or from checked-out source tree.

After opening, run the `Refresh Compilation Database` task to generate compilation database to navigate in source code.
This will run partial build of Envoy and may take a while depends on the machine performance.
This task is needed to run everytime after:
- Changing a BUILD file that add/remove files from a target, changes dependencies
- Changing API proto files

## Advanced Usages

### Using Remote Build Execution

Write the following content to `devcontainer.env` and rebuild the container. The key will be persisted in the container's `~/.bazelrc`.

```
GCP_SERVICE_ACCOUNT_KEY=<base64 encoded service account key>
BAZEL_REMOTE_INSTANCE=<RBE Instance>
BAZEL_REMOTE_CACHE=grpcs://remotebuildexecution.googleapis.com
BAZEL_BUILD_EXTRA_OPTIONS=--config=remote-ci --config=remote --jobs=<Number of jobs>
```

By default the `--config=remote` implies [`--remote_download_toplevel`](https://docs.bazel.build/versions/master/command-line-reference.html#flag--remote_download_toplevel),
change this to `minimal` or `all` depending on where you're running the container by adding them to `BAZEL_BUILD_EXTRA_OPTIONS`.

### Disk performance

Docker for Mac/Windows is known to have disk performance issue, this makes formatting all files in the container very slow.
[Update the mount consistency to 'delegated'](https://code.visualstudio.com/docs/remote/containers-advanced#_update-the-mount-consistency-to-delegated-for-macos) is recommended.
33 changes: 33 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "Envoy Dev",
"dockerFile": "Dockerfile",
"runArgs": [
"--user=vscode",
"--cap-add=SYS_PTRACE",
"--security-opt=seccomp=unconfined",
"--volume=${env:HOME}:${env:HOME}",
"--volume=envoy-build:/build",
// Uncomment next line if you have devcontainer.env
// "--env-file=.devcontainer/devcontainer.env"
],
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"bazel.buildifierFixOnFormat": true,
"clangd.path": "/opt/llvm/bin/clangd",
"python.pythonPath": "/usr/bin/python3",
"files.exclude": {
"**/.clangd/**": true,
"**/bazel-*/**": true
}
},
"remoteUser": "vscode",
"containerUser": "vscode",
"postCreateCommand": ".devcontainer/setup.sh",
"extensions": [
"github.vscode-pull-request-github",
"zxh404.vscode-proto3",
"bazelbuild.vscode-bazel",
"llvm-vs-code-extensions.vscode-clangd",
"webfreak.debug"
]
}
15 changes: 15 additions & 0 deletions .devcontainer/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env bash

. ci/setup_cache.sh
trap - EXIT # Don't remove the key file written into a temporary file

BAZELRC_FILE=~/.bazelrc bazel/setup_clang.sh /opt/llvm

# Use generated toolchain config because we know the base container is the one we're using in RBE.
# Not using libc++ here because clangd will raise some tidy issue in libc++ header as of version 9.
echo "build --config=rbe-toolchain-clang" >> ~/.bazelrc
echo "build --symlink_prefix=/" >> ~/.bazelrc
echo "build ${BAZEL_BUILD_EXTRA_OPTIONS}" | tee -a ~/.bazelrc
echo "startup --output_base=/build/tmp"

[[ ! -z "${BUILD_DIR}" ]] && sudo chown -R "$(id -u):$(id -g)" ${BUILD_DIR}
2 changes: 2 additions & 0 deletions .vscode/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
settings.json
launch.json
31 changes: 31 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Build All Tests",
"type": "shell",
"command": "bazel build //test/...",
"group": {
"kind": "build",
"isDefault": true
}
},
{
"label": "Run All Tests",
"type": "shell",
"command": "bazel test //test/...",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "Refresh Compilation Database",
"type": "shell",
"command": "tools/vscode/refresh_compdb.sh",
"problemMatcher": []
}
]
}
3 changes: 1 addition & 2 deletions bazel/setup_clang.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

BAZELRC_FILE="$(bazel info workspace)/clang.bazelrc"
BAZELRC_FILE="${BAZELRC_FILE:-$(bazel info workspace)/clang.bazelrc}"

LLVM_PREFIX=$1

Expand Down Expand Up @@ -28,6 +28,5 @@ build:clang-asan --linkopt=-fsanitize=vptr,function
build:clang-asan --linkopt=-L${RT_LIBRARY_PATH}
build:clang-asan --linkopt=-l:libclang_rt.ubsan_standalone-x86_64.a
build:clang-asan --linkopt=-l:libclang_rt.ubsan_standalone_cxx-x86_64.a
" > ${BAZELRC_FILE}

12 changes: 11 additions & 1 deletion tools/gen_compilation_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
import glob
import json
import logging
import os
import shlex
import subprocess
Expand Down Expand Up @@ -30,8 +31,16 @@ def generateCompilationDatabase(args):
"--config=compdb",
"--remote_download_outputs=all",
]
if args.keep_going:
bazel_options.append("-k")
if args.run_bazel_build:
runBazelBuildForCompilationDatabase(bazel_options, args.bazel_targets)
try:
runBazelBuildForCompilationDatabase(bazel_options, args.bazel_targets)
except subprocess.CalledProcessError as e:
if not args.keep_going:
raise
else:
logging.warning("bazel build failed {}: {}".format(e.returncode, e.cmd))

subprocess.check_call(["bazel", "build"] + bazel_options + [
"--aspects=@bazel_compdb//:aspects.bzl%compilation_database_aspect",
Expand Down Expand Up @@ -102,6 +111,7 @@ def fixCompilationDatabase(args, db):
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Generate JSON compilation database')
parser.add_argument('--run_bazel_build', action='store_true')
parser.add_argument('-k', '--keep_going', action='store_true')
parser.add_argument('--include_external', action='store_true')
parser.add_argument('--include_genfiles', action='store_true')
parser.add_argument('--include_headers', action='store_true')
Expand Down
9 changes: 9 additions & 0 deletions tools/vscode/refresh_compdb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

tools/proto_format/proto_format.sh fix

# Setting platform suffix here so the compdb headers won't be overwritten by another bazel run
BAZEL_BUILD_OPTIONS=--platform_suffix=-compdb tools/gen_compilation_database.py --run_bazel_build -k

# Kill clangd to reload the compilation database
killall -v /opt/llvm/bin/clangd

0 comments on commit 01cc47a

Please sign in to comment.