Skip to content

Commit

Permalink
fix(toolchain): cleanup managed toolchain interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fffonion authored and dndx committed Apr 26, 2024
1 parent 46890a0 commit 33c91c4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 93 deletions.
2 changes: 1 addition & 1 deletion build/toolchain/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cc_toolchain_config(
compiler_configuration = {},
target_cpu = "aarch64",
toolchain_path_prefix = "/usr/aarch64-linux-gnu/", # is this required?
tools_path_prefix = "/usr/bin/aarch64-linux-gnu-",
tools_prefix = "aarch64-linux-gnu-",
)

cc_toolchain(
Expand Down
50 changes: 30 additions & 20 deletions build/toolchain/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,23 @@ def _fmt_flags(flags, toolchain_path_prefix):
def _cc_toolchain_config_impl(ctx):
target_cpu = ctx.attr.target_cpu
toolchain_path_prefix = ctx.attr.toolchain_path_prefix
tools_path_prefix = ctx.attr.tools_path_prefix
tools_prefix = ctx.attr.tools_prefix
compiler_configuration = ctx.attr.compiler_configuration

# update to absolute paths if we are using a managed toolchain (downloaded by bazel)
if len(ctx.files.src) > 0:
if toolchain_path_prefix:
fail("Both `src` and `toolchain_path_prefix` is set, but toolchain_path_prefix will be overrided if `src` is set.")

# file is something like external/aarch64-rhel9-linux-gnu-gcc-11/aarch64-rhel9-linux-gnu/bin/ar
# we will take aarch64-rhel9-linux-gnu-gcc-11/aarch64-rhel9-linux-gnu
toolchain_path_prefix = INTERNAL_ROOT + "/" + "/".join(ctx.files.src[0].path.split("/")[1:3])

_tools_root_dir = INTERNAL_ROOT + "/" + ctx.files.src[0].path.split("/")[1]
tools_prefix = _tools_root_dir + "/bin/" + tools_prefix
else:
tools_prefix = "/usr/bin/" + tools_prefix

# Unfiltered compiler flags; these are placed at the end of the command
# line, so take precendence over any user supplied flags through --copts or
# such.
Expand Down Expand Up @@ -110,13 +124,9 @@ def _cc_toolchain_config_impl(ctx):
if len(ctx.files.src) > 0:
# define absolute path for managed toolchain
# bazel doesn't support relative path for cxx_builtin_include_directories
# file is something like external/aarch64-rhel9-linux-gnu-gcc-11/aarch64-rhel9-linux-gnu/bin/ar
# we will take aarch64-rhel9-linux-gnu-gcc-11/aarch64-rhel9-linux-gnu
tools_dir = "/".join(ctx.files.src[0].path.split("/")[1:3])
cxx_builtin_include_directories.append(INTERNAL_ROOT + "/" + tools_dir + "/include")
cxx_builtin_include_directories.append(INTERNAL_ROOT + "/" + tools_dir + "/sysroot/usr/include")
gcc_dir = "/".join(ctx.files.src[0].path.split("/")[1:2]) + "/lib/gcc"
cxx_builtin_include_directories.append(INTERNAL_ROOT + "/" + gcc_dir)
cxx_builtin_include_directories.append(toolchain_path_prefix + "/include")
cxx_builtin_include_directories.append(toolchain_path_prefix + "/sysroot/usr/include")
cxx_builtin_include_directories.append(_tools_root_dir + "/lib/gcc")

# sysroot_path = compiler_configuration["sysroot_path"]
# sysroot_prefix = ""
Expand All @@ -140,39 +150,39 @@ def _cc_toolchain_config_impl(ctx):
tool_paths = [
tool_path(
name = "ar",
path = tools_path_prefix + "ar",
path = tools_prefix + "ar",
),
tool_path(
name = "cpp",
path = tools_path_prefix + "g++",
path = tools_prefix + "g++",
),
tool_path(
name = "gcc",
path = tools_path_prefix + "gcc",
path = tools_prefix + "gcc",
),
tool_path(
name = "gcov",
path = tools_path_prefix + "gcov",
path = tools_prefix + "gcov",
),
tool_path(
name = "ld",
path = tools_path_prefix + ctx.attr.ld,
path = tools_prefix + ctx.attr.ld,
),
tool_path(
name = "nm",
path = tools_path_prefix + "nm",
path = tools_prefix + "nm",
),
tool_path(
name = "objcopy",
path = tools_path_prefix + "objcopy",
path = tools_prefix + "objcopy",
),
tool_path(
name = "objdump",
path = tools_path_prefix + "objdump",
path = tools_prefix + "objdump",
),
tool_path(
name = "strip",
path = tools_path_prefix + "strip",
path = tools_prefix + "strip",
),
]

Expand Down Expand Up @@ -223,12 +233,12 @@ cc_toolchain_config = rule(
implementation = _cc_toolchain_config_impl,
attrs = {
"target_cpu": attr.string(),
"toolchain_path_prefix": attr.string(),
"tools_path_prefix": attr.string(),
"toolchain_path_prefix": attr.string(doc = "The root directory of the toolchain."),
"tools_prefix": attr.string(doc = "The tools prefix, for example aarch64-linux-gnu-"),
"compiler_configuration": attr.string_list_dict(allow_empty = True, default = {}),
"target_libc": attr.string(default = "gnu"),
"ld": attr.string(default = "gcc"),
"src": attr.label(),
"src": attr.label(doc = "Reference to the managed toolchain repository, if set, toolchain_path_prefix will not be used and tools_prefix will be infered "),
},
provides = [CcToolchainConfigInfo],
)
62 changes: 4 additions & 58 deletions build/toolchain/managed_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,6 @@ aarch64_glibc_distros = {
"aws2": "7",
}

def _generate_wrappers_impl(ctx):
wrapper_file = ctx.actions.declare_file("wrapper")
ctx.actions.expand_template(
template = ctx.file._wrapper_template,
output = wrapper_file,
substitutions = {
"{{TOOLCHAIN_NAME}}": ctx.attr.toolchain_name,
},
is_executable = True,
)

dummy_output = ctx.actions.declare_file(ctx.attr.name + ".wrapper-marker")

ctx.actions.run_shell(
command = "build/toolchain/generate_wrappers.sh %s %s %s %s" % (
ctx.attr.toolchain_name,
wrapper_file.path,
ctx.attr.tools_prefix,
dummy_output.path,
),
progress_message = "Create wrappers for " + ctx.attr.toolchain_name,
inputs = [wrapper_file],
outputs = [dummy_output],
)

return [DefaultInfo(files = depset([dummy_output, wrapper_file]))]

generate_wrappers = rule(
implementation = _generate_wrappers_impl,
attrs = {
"toolchain_name": attr.string(mandatory = True),
"tools_prefix": attr.string(mandatory = True),
"_wrapper_template": attr.label(
default = "//build/toolchain:templates/wrapper",
allow_single_file = True,
),
},
)

def define_managed_toolchain(
name = None,
arch = "x86_64",
Expand Down Expand Up @@ -86,31 +47,16 @@ def define_managed_toolchain(
ld = ld,
target_cpu = arch,
target_libc = libc,
toolchain_path_prefix = "wrappers-%s/" % identifier, # is this required?
tools_path_prefix = "wrappers-%s/%s" % (identifier, tools_prefix),
src = "@%s//:toolchain" % identifier,
)

generate_wrappers(
name = "%s_wrappers" % identifier,
toolchain_name = identifier,
tools_prefix = tools_prefix,
)

native.filegroup(
name = "%s_files" % identifier,
srcs = [
":%s_wrappers" % identifier,
"@%s//:toolchain" % identifier,
],
src = "@%s//:toolchain" % identifier,
)

native.cc_toolchain(
name = "%s_cc_toolchain" % identifier,
all_files = ":%s_files" % identifier,
compiler_files = ":%s_files" % identifier,
all_files = "@%s//:toolchain" % identifier,
compiler_files = "@%s//:toolchain" % identifier,
dwp_files = ":empty",
linker_files = "%s_files" % identifier,
linker_files = "@%s//:toolchain" % identifier,
objcopy_files = ":empty",
strip_files = ":empty",
supports_param_files = 0,
Expand Down
14 changes: 0 additions & 14 deletions build/toolchain/templates/wrapper

This file was deleted.

0 comments on commit 33c91c4

Please sign in to comment.