Skip to content

Commit 3299108

Browse files
authored
Replace references of "host" with "exec". (#294)
This properly captures that we actually mean the exec platform in these contexts. Also remove host_tools utils because we can not really do that for the exec platform.
1 parent 81f85c0 commit 3299108

File tree

4 files changed

+43
-91
lines changed

4 files changed

+43
-91
lines changed

Diff for: toolchain/cc_toolchain_config.bzl

+13-13
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ load(
1919
load(
2020
"//toolchain/internal:common.bzl",
2121
_check_os_arch_keys = "check_os_arch_keys",
22-
_host_tools = "host_tools",
2322
_os_arch_pair = "os_arch_pair",
2423
)
2524

@@ -32,25 +31,23 @@ def _fmt_flags(flags, toolchain_path_prefix):
3231
# right paths and flags for the tools.
3332
def cc_toolchain_config(
3433
name,
35-
host_arch,
36-
host_os,
34+
exec_arch,
35+
exec_os,
3736
target_arch,
3837
target_os,
3938
target_system_name,
4039
toolchain_path_prefix,
4140
tools_path_prefix,
4241
wrapper_bin_prefix,
4342
compiler_configuration,
44-
cxx_builtin_include_directories,
45-
host_tools_info = {}):
46-
host_os_arch_key = _os_arch_pair(host_os, host_arch)
43+
cxx_builtin_include_directories):
44+
exec_os_arch_key = _os_arch_pair(exec_os, exec_arch)
4745
target_os_arch_key = _os_arch_pair(target_os, target_arch)
48-
_check_os_arch_keys([host_os_arch_key, target_os_arch_key])
46+
_check_os_arch_keys([exec_os_arch_key, target_os_arch_key])
4947

5048
# A bunch of variables that get passed straight through to
5149
# `create_cc_toolchain_config_info`.
5250
# TODO: What do these values mean, and are they actually all correct?
53-
host_system_name = host_arch
5451
(
5552
toolchain_identifier,
5653
target_cpu,
@@ -107,7 +104,7 @@ def cc_toolchain_config(
107104
"-fdebug-prefix-map={}=__bazel_toolchain_llvm_repo__/".format(toolchain_path_prefix),
108105
]
109106

110-
is_xcompile = not (host_os == target_os and host_arch == target_arch)
107+
is_xcompile = not (exec_os == target_os and exec_arch == target_arch)
111108

112109
# Default compiler flags:
113110
compile_flags = [
@@ -148,7 +145,7 @@ def cc_toolchain_config(
148145
archive_flags = []
149146

150147
# Linker flags:
151-
if host_os == "darwin" and not is_xcompile:
148+
if exec_os == "darwin" and not is_xcompile:
152149
# lld is experimental for Mach-O, so we use the native ld64 linker.
153150
# TODO: How do we cross-compile from Linux to Darwin?
154151
use_lld = False
@@ -264,15 +261,18 @@ def cc_toolchain_config(
264261
## NOTE: make variables are missing here; unix_cc_toolchain_config doesn't
265262
## pass these to `create_cc_toolchain_config_info`.
266263

267-
# 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):
264+
# The requirements here come from
265+
# https://cs.opensource.google/bazel/bazel/+/master:src/main/starlark/builtins_bzl/common/cc/cc_toolchain_provider_helper.bzl;l=75;drc=f0150efd1cca473640269caaf92b5a23c288089d
266+
# https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/rules/cpp/CcModule.java;l=1257;drc=6743d76f9ecde726d592e88d8914b9db007b1c43
267+
# https://cs.opensource.google/bazel/bazel/+/refs/tags/7.0.0:tools/cpp/unix_cc_toolchain_config.bzl;l=192,201;drc=044a14cca2747aeff258fc71eaeb153c08cb34d5
268268
# NOTE: Ensure these are listed in toolchain_tools in toolchain/internal/common.bzl.
269269
tool_paths = {
270270
"ar": tools_path_prefix + ("llvm-ar" if not use_libtool else "libtool"),
271271
"cpp": tools_path_prefix + "clang-cpp",
272272
"dwp": tools_path_prefix + "llvm-dwp",
273273
"gcc": wrapper_bin_prefix + "cc_wrapper.sh",
274274
"gcov": tools_path_prefix + "llvm-profdata",
275-
"ld": tools_path_prefix + "ld.lld" if use_lld else _host_tools.get_and_assert(host_tools_info, "ld"),
275+
"ld": tools_path_prefix + "ld.lld" if use_lld else "/usr/bin/ld",
276276
"llvm-cov": tools_path_prefix + "llvm-cov",
277277
"llvm-profdata": tools_path_prefix + "llvm-profdata",
278278
"nm": tools_path_prefix + "llvm-nm",
@@ -319,7 +319,7 @@ def cc_toolchain_config(
319319
cpu = target_cpu,
320320
compiler = compiler,
321321
toolchain_identifier = toolchain_identifier,
322-
host_system_name = host_system_name,
322+
host_system_name = exec_arch,
323323
target_system_name = target_system_name,
324324
target_libc = target_libc,
325325
abi_version = abi_version,

Diff for: toolchain/internal/common.bzl

+4-32
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
SUPPORTED_TARGETS = [("linux", "x86_64"), ("linux", "aarch64"), ("darwin", "x86_64"), ("darwin", "aarch64")]
1616

1717
# Map of tool name to its symlinked name in the tools directory.
18+
# See tool_paths in toolchain/cc_toolchain_config.bzl.
1819
_toolchain_tools = {
1920
name: name
2021
for name in [
@@ -40,7 +41,7 @@ _toolchain_tools_darwin = {
4041
"llvm-libtool-darwin": "libtool",
4142
}
4243

43-
def host_os_key(rctx):
44+
def exec_os_key(rctx):
4445
(os, version, arch) = os_version_arch(rctx)
4546
if version == "":
4647
return "%s-%s" % (os, arch)
@@ -154,14 +155,14 @@ def check_os_arch_keys(keys):
154155
keys = ", ".join(_supported_os_arch),
155156
))
156157

157-
def host_os_arch_dict_value(rctx, attr_name, debug = False):
158+
def exec_os_arch_dict_value(rctx, attr_name, debug = False):
158159
# Gets a value from a dictionary keyed by host OS and arch.
159160
# Checks for the more specific key, then the less specific,
160161
# and finally the empty key as fallback.
161162
# Returns a tuple of the matching key and value.
162163

163164
d = getattr(rctx.attr, attr_name)
164-
key1 = host_os_key(rctx)
165+
key1 = exec_os_key(rctx)
165166
if key1 in d:
166167
return (key1, d.get(key1))
167168

@@ -230,32 +231,3 @@ def toolchain_tools(os):
230231
if os == "darwin":
231232
tools.update(_toolchain_tools_darwin)
232233
return tools
233-
234-
def _get_host_tool_info(rctx, tool_path, tool_key = None):
235-
if tool_key == None:
236-
tool_key = tool_path
237-
238-
if tool_path == None or not rctx.path(tool_path).exists:
239-
return {}
240-
241-
return {
242-
tool_key: struct(
243-
path = tool_path,
244-
features = [],
245-
),
246-
}
247-
248-
def _extract_tool_path(tool_info):
249-
# Have to support structs or dicts:
250-
return tool_info.path if type(tool_info) == "struct" else tool_info["path"]
251-
252-
def _get_host_tool(host_tool_info, tool_key):
253-
if tool_key in host_tool_info:
254-
return _extract_tool_path(host_tool_info[tool_key])
255-
else:
256-
return None
257-
258-
host_tools = struct(
259-
get_tool_info = _get_host_tool_info,
260-
get_and_assert = _get_host_tool,
261-
)

Diff for: toolchain/internal/configure.bzl

+23-43
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ load(
2222
_arch = "arch",
2323
_canonical_dir_path = "canonical_dir_path",
2424
_check_os_arch_keys = "check_os_arch_keys",
25-
_host_os_arch_dict_value = "host_os_arch_dict_value",
26-
_host_tools = "host_tools",
25+
_exec_os_arch_dict_value = "exec_os_arch_dict_value",
2726
_is_absolute_path = "is_absolute_path",
2827
_list_to_string = "list_to_string",
2928
_os = "os",
@@ -63,11 +62,11 @@ def llvm_config_impl(rctx):
6362
if not rctx.attr.toolchain_roots:
6463
toolchain_root = ("@" if BZLMOD_ENABLED else "") + "@%s_llvm//" % rctx.attr.name
6564
else:
66-
(_key, toolchain_root) = _host_os_arch_dict_value(rctx, "toolchain_roots")
65+
(_key, toolchain_root) = _exec_os_arch_dict_value(rctx, "toolchain_roots")
6766

6867
if not toolchain_root:
6968
fail("LLVM toolchain root missing for ({}, {})".format(os, arch))
70-
(_key, llvm_version) = _host_os_arch_dict_value(rctx, "llvm_versions")
69+
(_key, llvm_version) = _exec_os_arch_dict_value(rctx, "llvm_versions")
7170
if not llvm_version:
7271
# LLVM version missing for (os, arch)
7372
_empty_repository(rctx)
@@ -161,30 +160,20 @@ def llvm_config_impl(rctx):
161160
llvm_version = llvm_version,
162161
extra_compiler_files = rctx.attr.extra_compiler_files,
163162
)
164-
host_dl_ext = "dylib" if os == "darwin" else "so"
165-
host_tools_info = dict([
166-
pair
167-
for (key, tool_path) in [
168-
# This is used when lld doesn't support the target platform (i.e.
169-
# Mach-O for macOS):
170-
("ld", "/usr/bin/ld"),
171-
]
172-
for pair in _host_tools.get_tool_info(rctx, tool_path, key).items()
173-
])
163+
exec_dl_ext = "dylib" if os == "darwin" else "so"
174164
cc_toolchains_str, toolchain_labels_str = _cc_toolchains_str(
175165
rctx,
176166
workspace_name,
177167
toolchain_info,
178168
use_absolute_paths_llvm,
179-
host_tools_info,
180169
)
181170

182171
convenience_targets_str = _convenience_targets_str(
183172
rctx,
184173
use_absolute_paths_llvm,
185174
llvm_dist_rel_path,
186175
llvm_dist_label_prefix,
187-
host_dl_ext,
176+
exec_dl_ext,
188177
)
189178

190179
# Convenience macro to register all generated toolchains.
@@ -226,8 +215,7 @@ def _cc_toolchains_str(
226215
rctx,
227216
workspace_name,
228217
toolchain_info,
229-
use_absolute_paths_llvm,
230-
host_tools_info):
218+
use_absolute_paths_llvm):
231219
# Since all the toolchains rely on downloading the right LLVM toolchain for
232220
# the host architecture, we don't need to explicitly specify
233221
# `exec_compatible_with` attribute. If the host and execution platform are
@@ -252,7 +240,6 @@ def _cc_toolchains_str(
252240
target_arch,
253241
toolchain_info,
254242
use_absolute_paths_llvm,
255-
host_tools_info,
256243
)
257244
if cc_toolchain_str:
258245
cc_toolchains_str = cc_toolchains_str + cc_toolchain_str
@@ -275,12 +262,11 @@ def _cc_toolchain_str(
275262
target_os,
276263
target_arch,
277264
toolchain_info,
278-
use_absolute_paths_llvm,
279-
host_tools_info):
280-
host_os = toolchain_info.os
281-
host_arch = toolchain_info.arch
265+
use_absolute_paths_llvm):
266+
exec_os = toolchain_info.os
267+
exec_arch = toolchain_info.arch
282268

283-
host_os_bzl = _os_bzl(host_os)
269+
exec_os_bzl = _os_bzl(exec_os)
284270
target_os_bzl = _os_bzl(target_os)
285271

286272
target_pair = _os_arch_pair(target_os, target_arch)
@@ -293,20 +279,16 @@ def _cc_toolchain_str(
293279
sysroot_label_str = ""
294280

295281
if not sysroot_path:
296-
if host_os == target_os and host_arch == target_arch:
282+
if exec_os == target_os and exec_arch == target_arch:
297283
# For darwin -> darwin, we can use the macOS SDK path.
298-
sysroot_path = _default_sysroot_path(rctx, host_os)
284+
sysroot_path = _default_sysroot_path(rctx, exec_os)
299285
else:
300286
# We are trying to cross-compile without a sysroot, let's bail.
301287
# TODO: Are there situations where we can continue?
302288
return ""
303289

304290
extra_files_str = "\":internal-use-files\""
305291

306-
# `struct` isn't allowed in `BUILD` files so we JSON encode + decode to turn
307-
# them into `dict`s.
308-
host_tools_info = json.decode(json.encode(host_tools_info))
309-
310292
# C++ built-in include directories.
311293
# This contains both the includes shipped with the compiler as well as the sysroot (or host)
312294
# include directories. While Bazel's default undeclared inclusions check does not seem to be
@@ -358,8 +340,8 @@ def _cc_toolchain_str(
358340
359341
cc_toolchain_config(
360342
name = "local-{suffix}",
361-
host_arch = "{host_arch}",
362-
host_os = "{host_os}",
343+
exec_arch = "{exec_arch}",
344+
exec_os = "{exec_os}",
363345
target_arch = "{target_arch}",
364346
target_os = "{target_os}",
365347
target_system_name = "{target_system_name}",
@@ -382,15 +364,14 @@ cc_toolchain_config(
382364
"coverage_link_flags": {coverage_link_flags},
383365
"unfiltered_compile_flags": {unfiltered_compile_flags},
384366
}},
385-
host_tools_info = {host_tools_info},
386367
cxx_builtin_include_directories = {cxx_builtin_include_directories},
387368
)
388369
389370
toolchain(
390371
name = "cc-toolchain-{suffix}",
391372
exec_compatible_with = [
392-
"@platforms//cpu:{host_arch}",
393-
"@platforms//os:{host_os_bzl}",
373+
"@platforms//cpu:{exec_arch}",
374+
"@platforms//os:{exec_os_bzl}",
394375
],
395376
target_compatible_with = [
396377
"@platforms//cpu:{target_arch}",
@@ -518,12 +499,12 @@ cc_toolchain(
518499
suffix = suffix,
519500
target_os = target_os,
520501
target_arch = target_arch,
521-
host_os = host_os,
522-
host_arch = host_arch,
502+
exec_os = exec_os,
503+
exec_arch = exec_arch,
523504
target_settings = _list_to_string(_dict_value(toolchain_info.target_settings_dict, target_pair)),
524505
target_os_bzl = target_os_bzl,
525506
target_system_name = target_system_name,
526-
host_os_bzl = host_os_bzl,
507+
exec_os_bzl = exec_os_bzl,
527508
llvm_dist_label_prefix = toolchain_info.llvm_dist_label_prefix,
528509
llvm_dist_path_prefix = toolchain_info.llvm_dist_path_prefix,
529510
tools_path_prefix = toolchain_info.tools_path_prefix,
@@ -544,7 +525,6 @@ cc_toolchain(
544525
coverage_link_flags = _list_to_string(_dict_value(toolchain_info.coverage_link_flags_dict, target_pair)),
545526
unfiltered_compile_flags = _list_to_string(_dict_value(toolchain_info.unfiltered_compile_flags_dict, target_pair)),
546527
extra_files_str = extra_files_str,
547-
host_tools_info = host_tools_info,
548528
cxx_builtin_include_directories = _list_to_string([
549529
# Filter out non-existing directories with absolute paths as they
550530
# result in a -Wincomplete-umbrella warning when mentioned in the
@@ -556,12 +536,12 @@ cc_toolchain(
556536
extra_compiler_files = ("\"%s\"," % str(toolchain_info.extra_compiler_files)) if toolchain_info.extra_compiler_files else "",
557537
)
558538

559-
def _convenience_targets_str(rctx, use_absolute_paths, llvm_dist_rel_path, llvm_dist_label_prefix, host_dl_ext):
539+
def _convenience_targets_str(rctx, use_absolute_paths, llvm_dist_rel_path, llvm_dist_label_prefix, exec_dl_ext):
560540
if use_absolute_paths:
561541
llvm_dist_label_prefix = ":"
562542
filenames = []
563543
for libname in _aliased_libs:
564-
filename = "lib/{}.{}".format(libname, host_dl_ext)
544+
filename = "lib/{}.{}".format(libname, exec_dl_ext)
565545
filenames.append(filename)
566546
for toolname in _aliased_tools:
567547
filename = "bin/{}".format(toolname)
@@ -575,7 +555,7 @@ def _convenience_targets_str(rctx, use_absolute_paths, llvm_dist_rel_path, llvm_
575555
template = """
576556
cc_import(
577557
name = "{name}",
578-
shared_library = "{{llvm_dist_label_prefix}}lib/lib{name}.{{host_dl_ext}}",
558+
shared_library = "{{llvm_dist_label_prefix}}lib/lib{name}.{{exec_dl_ext}}",
579559
)""".format(name = name)
580560
lib_target_strs.append(template)
581561

@@ -591,7 +571,7 @@ native_binary(
591571

592572
return "\n".join(lib_target_strs + tool_target_strs).format(
593573
llvm_dist_label_prefix = llvm_dist_label_prefix,
594-
host_dl_ext = host_dl_ext,
574+
exec_dl_ext = exec_dl_ext,
595575
)
596576

597577
def _is_hermetic_or_exists(rctx, path, sysroot_prefix):

Diff for: toolchain/internal/llvm_distributions.bzl

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
# limitations under the License.
1414

1515
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "read_netrc", "use_netrc")
16-
load("//toolchain/internal:common.bzl", _arch = "arch", _attr_dict = "attr_dict", _host_os_arch_dict_value = "host_os_arch_dict_value", _os = "os")
16+
load("//toolchain/internal:common.bzl", _arch = "arch", _attr_dict = "attr_dict", _exec_os_arch_dict_value = "exec_os_arch_dict_value", _os = "os")
1717
load("//toolchain/internal:release_name.bzl", _llvm_release_name = "llvm_release_name")
1818

1919
# If a new LLVM version is missing from this list, please add the shasums here
@@ -547,7 +547,7 @@ def download_llvm(rctx):
547547
return updated_attrs
548548

549549
def _urls(rctx):
550-
(key, urls) = _host_os_arch_dict_value(rctx, "urls", debug = False)
550+
(key, urls) = _exec_os_arch_dict_value(rctx, "urls", debug = False)
551551
if not urls:
552552
print("LLVM archive URLs missing and no default fallback provided; will try 'distribution' attribute") # buildifier: disable=print
553553

@@ -561,7 +561,7 @@ def _get_llvm_version(rctx):
561561
return rctx.attr.llvm_version
562562
if not rctx.attr.llvm_versions:
563563
fail("Neither 'llvm_version' nor 'llvm_versions' given.")
564-
(_, llvm_version) = _host_os_arch_dict_value(rctx, "llvm_versions")
564+
(_, llvm_version) = _exec_os_arch_dict_value(rctx, "llvm_versions")
565565
if not llvm_version:
566566
fail("LLVM version string missing for ({os}, {arch})", os = _os(rctx), arch = _arch(rctx))
567567
return llvm_version

0 commit comments

Comments
 (0)