Skip to content

Commit

Permalink
.devcontainer: clang-based build inside the container.
Browse files Browse the repository at this point in the history
This allows the `bsv.cc.compdb.generate` command in vscode to generate a
valid `compile_commands.json` database, usable by clangd. Otherwise,
GCC command flags used are unusable by `clangd` which we install into
the devcontainer.
  • Loading branch information
ivucica authored Apr 30, 2024
1 parent d0de049 commit 0768e24
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .devcontainer/bazel-toolchains/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Based on https://bazel.build/versions/6.5.0/tutorials/ccp-toolchain-config

load(":cc_toolchain_config.bzl", "cc_toolchain_config")

package(default_visibility = ["//visibility:public"])

cc_toolchain_suite(
name = "clang_suite",
toolchains = {
"k8": ":k8_toolchain",
},
)
filegroup(name = "empty")

cc_toolchain(
name = "k8_toolchain",
toolchain_identifier = "k8-toolchain",
toolchain_config = ":k8_toolchain_config",
all_files = ":empty",
compiler_files = ":empty",
dwp_files = ":empty",
linker_files = ":empty",
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = 0,
)
cc_toolchain_config(name = "k8_toolchain_config")
96 changes: 96 additions & 0 deletions .devcontainer/bazel-toolchains/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Based on https://bazel.build/versions/6.5.0/tutorials/ccp-toolchain-config
load("@bazel_tools//tools/build_defs/cc:action_names.bzl", "ACTION_NAMES")
load(
"@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
"feature",
"flag_group",
"flag_set",
"tool_path",
)

all_link_actions = [ # NEW
ACTION_NAMES.cpp_link_executable,
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
]

def _impl(ctx):
tool_paths = [
tool_path(
name = "gcc",
path = "/usr/bin/clang",
),
tool_path(
name = "ld",
path = "/usr/bin/ld",
),
tool_path(
name = "ar",
path = "/usr/bin/ar",
),
tool_path(
name = "cpp",
path = "/bin/false",
),
tool_path(
name = "gcov",
path = "/bin/false",
),
tool_path(
name = "nm",
path = "/bin/false",
),
tool_path(
name = "objdump",
path = "/bin/false",
),
tool_path(
name = "strip",
path = "/bin/false",
),
]

features = [
feature(
name = "default_linker_flags",
enabled = True,
flag_sets = [
flag_set(
actions = all_link_actions,
flag_groups = ([
flag_group(
flags = [
"-lstdc++",
"-lm", # added because floorf from glibc was missing
],
),
]),
),
],
),
]


return cc_common.create_cc_toolchain_config_info(
ctx = ctx,
features = features,
cxx_builtin_include_directories = [
"/usr/lib/llvm-10/lib/clang/10.0.0/include", # updated to current image's version
"/usr/include",
],
toolchain_identifier = "local",
host_system_name = "local",
target_system_name = "local",
target_cpu = "k8",
target_libc = "unknown",
compiler = "clang",
abi_version = "unknown",
abi_libc_version = "unknown",
tool_paths = tool_paths,
)

cc_toolchain_config = rule(
implementation = _impl,
attrs = {},
provides = [CcToolchainConfigInfo],
)
5 changes: 4 additions & 1 deletion .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
"vscode": {
// Set *default* container specific settings.json values on container create.
"settings": {
"bazel.commandLine.commandArgs": [
"--config=devcontainer"
],
"bazel.executable": "bazelisk",
"bsv.bazel.buildFlags": [
// "--config=remote"
"--config=devcontainer"
],
"bsv.cc.compdb.targets": [
"//:yatc"
Expand Down
8 changes: 8 additions & 0 deletions tools/bazel.rc
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,11 @@ build:remotecache --remote_executor=grpcs://remote.buildbuddy.io
#build:remotecache --remote_download_toplevel # Helps remove network bottleneck if caching is enabled

try-import %workspace%/.bazelrc.buildbuddysecrets

# In devcontainer: use our custom-configured c++ toolchain valid inside the 20.04 devcontainer.
build:devcontainer --crosstool_top=//.devcontainer/bazel-toolchains:clang_suite
# Use --cpu as a differentiator.
build:devcontainer --cpu=k8
# Use the default Bazel C++ toolchain to build the tools used during the
# build.
build:devcontainer --host_crosstool_top=@bazel_tools//tools/cpp:toolchain

0 comments on commit 0768e24

Please sign in to comment.