Skip to content

Commit

Permalink
Remove support for libtool-wrapper (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzbarsky authored Sep 1, 2023
1 parent f94335f commit 7fcfac0
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 152 deletions.
1 change: 0 additions & 1 deletion toolchain/BUILD.toolchain.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ filegroup(
name = "internal-use-wrapped-tools",
srcs = [
"%{wrapper_bin_prefix}cc_wrapper.sh",
"%{wrapper_bin_prefix}host_libtool_wrapper.sh",
],
visibility = ["//visibility:private"],
)
Expand Down
11 changes: 1 addition & 10 deletions toolchain/cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ load(
load(
"//toolchain/internal:common.bzl",
_check_os_arch_keys = "check_os_arch_keys",
_host_tool_features = "host_tool_features",
_host_tools = "host_tools",
_os_arch_pair = "os_arch_pair",
)
Expand Down Expand Up @@ -304,15 +303,7 @@ def cc_toolchain_config(
# TODO: The command line formed on darwin does not work with llvm-ar.
ar_binary = tools_path_prefix + "llvm-ar"
if host_os == "darwin":
# Bazel uses arg files for longer commands; some old macOS `libtool`
# versions do not support this.
#
# In these cases we want to use `libtool_wrapper.sh` which translates
# the arg file back into command line arguments.
if not _host_tools.tool_supports(host_tools_info, "libtool", features = [_host_tool_features.SUPPORTS_ARG_FILE]):
ar_binary = wrapper_bin_prefix + "host_libtool_wrapper.sh"
else:
ar_binary = host_tools_info["libtool"]["path"]
ar_binary = host_tools_info["libtool"]["path"]

# The tool names come from [here](https://github.com/bazelbuild/bazel/blob/c7e58e6ce0a78fdaff2d716b4864a5ace8917626/src/main/java/com/google/devtools/build/lib/rules/cpp/CppConfiguration.java#L76-L90):
# NOTE: Ensure these are listed in toolchain_tools in toolchain/internal/common.bzl.
Expand Down
32 changes: 0 additions & 32 deletions toolchain/host_libtool_wrapper.sh.tpl

This file was deleted.

92 changes: 8 additions & 84 deletions toolchain/internal/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@

SUPPORTED_TARGETS = [("linux", "x86_64"), ("linux", "aarch64"), ("darwin", "x86_64"), ("darwin", "aarch64")]

host_tool_features = struct(
SUPPORTS_ARG_FILE = "supports_arg_file",
)

toolchain_tools = [
"clang-cpp",
"ld.lld",
Expand Down Expand Up @@ -181,103 +177,31 @@ def attr_dict(attr):

return dict(tuples)

# Tries to figure out if a tool supports newline separated arg files (i.e.
# `@file`).
def _tool_supports_arg_file(rctx, tool_path):
# We assume nothing other than that `tool_path` is an executable.
#
# First we have to find out what command line flag gets the tool to just
# print out some text and exit successfully.
#
# Most tools support `-v` or `--version` or (for `libtool`) `-V` but some
# tools don't have such an option (BSD `ranlib` and `ar`, for example).
#
# We just try all the options we know of until one works and if none work
# we return "None" indicating an indeterminate result.
opts = (
["-v", "--version", "-version", "-V"] +
["-h", "--help", "-help", "-H"]
)

no_op_opt = None
for opt in opts:
if rctx.execute([tool_path, opt], timeout = 2).return_code == 0:
no_op_opt = opt
break

if no_op_opt == None:
return None

# Okay! Once we have an opt that we *know* does nothing but make the
# executable exit successfully, we'll stick that opt in a file and try
# again:
tmp_file = "tmp-arg-file"
rctx.file(tmp_file, content = "{}\n".format(no_op_opt), executable = False)

res = rctx.execute([tool_path, "@{}".format(tmp_file)]).return_code == 0
rctx.delete(tmp_file)

return res

def _get_host_tool_info(rctx, tool_path, features_to_test = [], tool_key = None):
def _get_host_tool_info(rctx, tool_path, tool_key = None):
if tool_key == None:
tool_key = tool_path

if tool_path == None or not rctx.path(tool_path).exists:
return {}

f = host_tool_features
features = {}
for feature in features_to_test:
features[feature] = {
f.SUPPORTS_ARG_FILE: _tool_supports_arg_file,
}[feature](rctx, tool_path)

return {
tool_key: struct(
path = tool_path,
features = features,
features = [],
),
}

def _extract_tool_path_and_features(tool_info):
def _extract_tool_path(tool_info):
# Have to support structs or dicts:
tool_path = tool_info.path if type(tool_info) == "struct" else tool_info["path"]
tool_features = tool_info.features if type(tool_info) == "struct" else tool_info["features"]
return tool_info.path if type(tool_info) == "struct" else tool_info["path"]

return (tool_path, tool_features)

def _check_host_tool_supports(host_tool_info, tool_key, features = []):
def _get_host_tool(host_tool_info, tool_key):
if tool_key in host_tool_info:
_, tool_features = _extract_tool_path_and_features(host_tool_info[tool_key])

for f in features:
if not f in tool_features or not tool_features[f]:
return False

return True
return _extract_tool_path(host_tool_info[tool_key])
else:
return False

def _get_host_tool_and_assert_supports(host_tool_info, tool_key, features = []):
if tool_key in host_tool_info:
tool_path, tool_features = _extract_tool_path_and_features(host_tool_info[tool_key])

missing = [f for f in features if not f in tool_features or not tool_features[f]]

if missing:
fail("Host tool `{key}` (`{path}`) is missing these features: `{missing}`.".format(
key = tool_key,
path = tool_path,
missing = missing,
))

return tool_path
else:
return False
return None

host_tools = struct(
get_tool_info = _get_host_tool_info,
tool_supports = _check_host_tool_supports,
get_and_assert = _get_host_tool_and_assert_supports,
get_and_assert = _get_host_tool,
)
27 changes: 5 additions & 22 deletions toolchain/internal/configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ load(
_canonical_dir_path = "canonical_dir_path",
_check_os_arch_keys = "check_os_arch_keys",
_host_os_arch_dict_value = "host_os_arch_dict_value",
_host_tool_features = "host_tool_features",
_host_tools = "host_tools",
_list_to_string = "list_to_string",
_os = "os",
_os_arch_pair = "os_arch_pair",
_os_bzl = "os_bzl",
_pkg_name_from_label = "pkg_name_from_label",
_pkg_path_from_label = "pkg_path_from_label",
_supported_targets = "SUPPORTED_TARGETS",
_toolchain_tools = "toolchain_tools",
Expand All @@ -40,12 +38,6 @@ load(
_aliased_tools = "aliased_tools",
)

def _include_dirs_str(rctx, key):
dirs = rctx.attr.cxx_builtin_include_directories.get(key)
if not dirs:
return ""
return ("\n" + 12 * " ").join(["\"%s\"," % d for d in dirs])

def llvm_config_impl(rctx):
_check_os_arch_keys(rctx.attr.sysroot)
_check_os_arch_keys(rctx.attr.cxx_builtin_include_directories)
Expand Down Expand Up @@ -160,16 +152,16 @@ def llvm_register_toolchains():
host_dl_ext = "dylib" if os == "darwin" else "so"
host_tools_info = dict([
pair
for (key, tool_path, features) in [
for (key, tool_path) in [
# This is used for macOS hosts:
("libtool", "/usr/bin/libtool", [_host_tool_features.SUPPORTS_ARG_FILE]),
("libtool", "/usr/bin/libtool"),
# This is used with old (pre 7) LLVM versions:
("strip", "/usr/bin/strip", []),
("strip", "/usr/bin/strip"),
# This is used when lld doesn't support the target platform (i.e.
# Mach-O for macOS):
("ld", "/usr/bin/ld", []),
("ld", "/usr/bin/ld"),
]
for pair in _host_tools.get_tool_info(rctx, tool_path, features, key).items()
for pair in _host_tools.get_tool_info(rctx, tool_path, key).items()
])
cc_toolchains_str, toolchain_labels_str = _cc_toolchains_str(
workspace_name,
Expand Down Expand Up @@ -221,15 +213,6 @@ def llvm_register_toolchains():
},
)

# libtool wrapper; used if the host libtool doesn't support arg files:
rctx.template(
"bin/host_libtool_wrapper.sh",
rctx.attr._host_libtool_wrapper_sh_tpl,
{
"%{libtool_path}": "/usr/bin/libtool",
},
)

def _cc_toolchains_str(
workspace_name,
toolchain_info,
Expand Down
3 changes: 0 additions & 3 deletions toolchain/rules.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,6 @@ _llvm_config_attrs.update({
"_cc_wrapper_sh_tpl": attr.label(
default = "//toolchain:cc_wrapper.sh.tpl",
),
"_host_libtool_wrapper_sh_tpl": attr.label(
default = "//toolchain:host_libtool_wrapper.sh.tpl",
),
})

llvm = repository_rule(
Expand Down

0 comments on commit 7fcfac0

Please sign in to comment.