Skip to content

Commit

Permalink
Address some review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
gferon committed May 15, 2023
1 parent 228bfec commit 5df8dc1
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 243 deletions.
22 changes: 17 additions & 5 deletions toolchain/extensions.bzl
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
"Module extensions for use with bzlmod"

load("@com_grail_bazel_toolchain//toolchain:rules.bzl", "compiler_configuration_attrs", "llvm_repo_attrs", "llvm_toolchain")
load("@com_grail_bazel_toolchain//toolchain:rules.bzl", "llvm_toolchain")
load(
"@com_grail_bazel_toolchain//toolchain/internal:repo.bzl",
_compiler_configuration_attrs = "compiler_configuration_attrs",
_llvm_repo_attrs = "llvm_repo_attrs",
)

def _llvm_impl_(module_ctx):
for mod in module_ctx.modules:
for toolchain_attr in mod.tags.toolchain:
llvm_toolchain(
name = "llvm_toolchain",
name = toolchain_attr.name,
llvm_version = toolchain_attr.llvm_version,
stdlib = toolchain_attr.stdlib,
sha256 = toolchain_attr.sha256,
strip_prefix = toolchain_attr.strip_prefix,
urls = toolchain_attr.urls,
bzlmod = True,
bzlmod_module_version = module_ctx.modules[0].version,
)

attrs = dict(llvm_repo_attrs)
attrs.update(compiler_configuration_attrs)
_attrs = {
"name": attr.string(
doc = "Prefix used when registering toolchain repositories",
default = "llvm_toolchain",
),
}
_attrs.update(_llvm_repo_attrs)
_attrs.update(_compiler_configuration_attrs)

llvm = module_extension(
implementation = _llvm_impl_,
tag_classes = {
"toolchain": tag_class(
attrs = attrs,
attrs = _attrs,
),
},
)
224 changes: 223 additions & 1 deletion toolchain/internal/repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,238 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load(
"//toolchain/internal:common.bzl",
_os = "os",
_supported_os_arch_keys = "supported_os_arch_keys",
)
load(
"//toolchain/internal:llvm_distributions.bzl",
_download_llvm = "download_llvm",
)

_target_pairs = ", ".join(_supported_os_arch_keys())

compiler_configuration_attrs = {
"sysroot": attr.string_dict(
mandatory = False,
doc = ("System path or fileset, for each target OS and arch pair you want to support " +
"({}), ".format(_target_pairs) +
"used to indicate the set of files that form the sysroot for the compiler. " +
"If the value begins with exactly one forward slash '/', then the value is " +
"assumed to be a system path. Else, the value will be assumed to be a label " +
"containing the files and the sysroot path will be taken as the path to the " +
"package of this label."),
),
"cxx_builtin_include_directories": attr.string_list_dict(
mandatory = False,
doc = ("Additional builtin include directories to be added to the default system " +
"directories, for each target OS and arch pair you want to support " +
"({}); ".format(_target_pairs) +
"see documentation for bazel's create_cc_toolchain_config_info."),
),
"stdlib": attr.string_dict(
mandatory = False,
doc = ("stdlib implementation, for each target OS and arch pair you want to support " +
"({}), ".format(_target_pairs) +
"linked to the compiled binaries. An empty key can be used to specify a " +
"value for all target pairs. Possible values are `builtin-libc++` (default) " +
"which uses the libc++ shipped with clang, `libc++` which uses libc++ available on " +
"the host or sysroot, `stdc++` which uses libstdc++ available on the host or " +
"sysroot, and `none` which uses `-nostdlib` with the compiler."),
),
"cxx_standard": attr.string_dict(
mandatory = False,
doc = ("C++ standard, for each target OS and arch pair you want to support " +
"({}), ".format(_target_pairs) +
"passed as `-std` flag to the compiler. An empty key can be used to specify a " +
"value for all target pairs. Default value is c++17."),
),
# For default values of all the below flags overrides, consult
# cc_toolchain_config.bzl in this directory.
"compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for compile_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"cxx_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for cxx_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"link_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for link_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"link_libs": attr.string_list_dict(
mandatory = False,
doc = ("Override for link_libs, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"opt_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for opt_compile_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"opt_link_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for opt_link_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"dbg_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for dbg_compile_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"coverage_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for coverage_compile_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"coverage_link_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for coverage_link_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"unfiltered_compile_flags": attr.string_list_dict(
mandatory = False,
doc = ("Override for unfiltered_compile_flags, replacing the default values. " +
"`{toolchain_path_prefix}` in the flags will be substituted by the path " +
"to the root LLVM distribution directory. Provide one list for each " +
"target OS and arch pair you want to override " +
"({}); empty key overrides all.".format(_target_pairs)),
),
"target_settings": attr.string_list_dict(
mandatory = False,
doc = ("Override the toolchain's `target_settings` attribute."),
),
}

llvm_repo_attrs = {
"llvm_version": attr.string(
doc = ("One of the supported versions of LLVM, e.g. 12.0.0; used with the " +
"`auto` value for the `distribution` attribute, and as a default value " +
"for the `llvm_versions` attribute."),
),
"urls": attr.string_list_dict(
mandatory = False,
doc = ("URLs to LLVM pre-built binary distribution archives, keyed by host OS " +
"release name and architecture, e.g. darwin-x86_64, darwin-aarch64, " +
"ubuntu-20.04-x86_64, etc, or a less specific OS and arch pair " +
"({}). ".format(_target_pairs) +
"May also need the `strip_prefix` attribute. " +
"Consider also setting the `sha256` attribute. An empty key is " +
"used to specify a fallback default for all hosts. This attribute " +
"overrides `distribution`, `llvm_version`, `llvm_mirror` and " +
"`alternative_llvm_sources` attributes if the host OS key is present."),
),
"sha256": attr.string_dict(
mandatory = False,
doc = "The expected SHA-256 of the file downloaded as per the `urls` attribute.",
),
"strip_prefix": attr.string_dict(
mandatory = False,
doc = "The prefix to strip from the extracted file from the `urls` attribute.",
),
"distribution": attr.string(
default = "auto",
doc = ("LLVM pre-built binary distribution filename, must be one " +
"listed on http://releases.llvm.org/download.html for the version " +
"specified in the `llvm_version` attribute. A special value of " +
"'auto' tries to detect the version based on host OS."),
),
"llvm_mirror": attr.string(
doc = "Base URL for an LLVM release mirror." +
"\n\n" +
"This mirror must follow the same structure as the official LLVM release " +
"sources (`releases.llvm.org` for versions <= 9, `llvm/llvm-project` GitHub " +
"releases for newer versions)." +
"\n\n" +
"If provided, this mirror will be given precedence over the official LLVM release " +
"sources (see: " +
"https://github.com/grailbio/bazel-toolchain/toolchain/internal/llvm_distributions.bzl).",
),
"alternative_llvm_sources": attr.string_list(
doc = "Patterns for alternative LLVM release sources. Unlike URLs specified for `llvm_mirror` " +
"these do not have to follow the same structure as the official LLVM release sources." +
"\n\n" +
"Patterns may include `{llvm_version}` (which will be substituted for the full LLVM " +
"version, i.e. 13.0.0) and `{basename}` (which will be replaced with the filename " +
"used by the official LLVM release sources for a particular distribution; i.e. " +
"`llvm-13.0.0-x86_64-linux-gnu-ubuntu-20.04.tar.xz`)." +
"\n\n" +
"As with `llvm_mirror`, these sources will take precedence over the official LLVM " +
"release sources.",
),
"netrc": attr.string(
mandatory = False,
doc = "Path to the netrc file for authenticated LLVM URL downloads.",
),
"auth_patterns": attr.string_dict(
mandatory = False,
doc = "An optional dict mapping host names to custom authorization patterns.",
),
}

llvm_config_attrs = dict(compiler_configuration_attrs)
llvm_config_attrs.update({
"toolchain_roots": attr.string_dict(
mandatory = True,
# TODO: Ideally, we should be taking a filegroup label here instead of a package path, but
# we ultimately need to subset the files to be more selective in what we include in the
# sandbox for which operations, and it is not straightforward to subset a filegroup.
doc = ("System or package path, keyed by host OS release name and architecture, e.g. " +
"darwin-x86_64, darwin-aarch64, ubuntu-20.04-x86_64, etc., or a less specific " +
"OS and arch pair ({}), to be used as the LLVM toolchain ".format(_target_pairs) +
"distributions. An empty key can be used to specify a fallback default for " +
"all hosts, e.g. with the llvm_toolchain_repo rule. " +
"If the value begins with exactly one forward slash '/', then the value is " +
"assumed to be a system path and the toolchain is configured to use absolute " +
"paths. Else, the value will be assumed to be a bazel package containing the " +
"filegroup targets as in BUILD.llvm_repo."),
),
"llvm_versions": attr.string_dict(
mandatory = True,
doc = ("LLVM version strings for each entry in the `toolchain_roots` attribute; " +
"if unset, a default value is set from the `llvm_version` attribute."),
),
"absolute_paths": attr.bool(
default = False,
doc = "Use absolute paths in the toolchain. Avoids sandbox overhead.",
),
"_cc_toolchain_config_bzl": attr.label(
default = "//toolchain:cc_toolchain_config.bzl",
),
})

def llvm_repo_impl(rctx):
os = _os(rctx)
if os == "windows":
Expand Down
Loading

0 comments on commit 5df8dc1

Please sign in to comment.