Skip to content

Commit 563ec76

Browse files
committed
misc: some WIP examples
1 parent 017f21c commit 563ec76

File tree

2 files changed

+130
-1
lines changed

2 files changed

+130
-1
lines changed

WORKSPACE

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,22 @@ llvm_toolchain(
2727
# LLVM 9.0.0 needs /usr/lib/libtinfo.so.5 that is not very straightforward
2828
# to set up in all linux distros we test.
2929
llvm_version = "8.0.0",
30+
extra_targets = [
31+
"wasm32-unknown-wasi",
32+
],
33+
# absolute_paths = True,
3034
)
3135

32-
load("@llvm_toolchain//:toolchains.bzl", "llvm_register_toolchains")
36+
load("@llvm_toolchain//:toolchains.bzl", "llvm_register_toolchains", "register_toolchain")
3337

3438
llvm_register_toolchains()
3539

40+
# http_archive(
41+
# name = "thumbv7-sysroot",
42+
# urls = ["example.com"],
43+
# )
44+
register_toolchain("//tests:custom_toolchain_example")
45+
3646
## Toolchain example with a sysroot.
3747
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
3848

tests/BUILD.bazel

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,46 @@
1414

1515
load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
1616

17+
# config_setting(
18+
# name = "no_shared_objects",
19+
# values = {
20+
# "features": "-supports_dynamic_linker",
21+
# }
22+
# )
23+
24+
platform(
25+
name = "wasm",
26+
constraint_values = [
27+
"@platforms//cpu:wasm32",
28+
"@platforms//os:wasi",
29+
# ":no_shared_objects",
30+
],
31+
)
32+
33+
cc_library(
34+
name = "stdlib-wasm",
35+
srcs = ["stdlib.cc"],
36+
hdrs = ["stdlib.h"],
37+
features = [
38+
"-supports_dynamic_linker",
39+
],
40+
target_compatible_with = [
41+
"@platforms//cpu:wasm32",
42+
"@platforms//os:wasi",
43+
],
44+
)
45+
46+
# TODO: add a rule that transitions --platform to `//test:wasm` to ensure that
47+
# toolchain resolution picks `@llvm_toolchain//:cc-clang-*_wasm32-unknown-unknown`.
48+
#
49+
# also have it set `--features=-supports_dynamic_linker`; ideally this would be in
50+
# the toolchain definition but the downside of switching to using
51+
# `unix_cc_toolchain_config.bzl` is that we lose this knob
52+
#
53+
# it's not clear yet if supporting different targets runs counter to using
54+
# `unix_cc_toolchain_config.bzl` altogether – so far things are manageable but
55+
# it's already apparent that we'll be sacrificing some power/configurability
56+
1757
cc_library(
1858
name = "stdlib",
1959
srcs = ["stdlib.cc"],
@@ -34,3 +74,82 @@ sh_test(
3474
"@llvm_toolchain//:lib/libc++.a",
3575
],
3676
)
77+
78+
# TODO: Eventually expose a nicer function for this that's less boilerplate-y
79+
#
80+
# It should be generated and do things like fail immediately if absolute_paths != True, etc.
81+
82+
# Example Custom Toolchain:
83+
load("@llvm_toolchain//:cc_toolchain_config.bzl", "cc_toolchain_config")
84+
85+
# Docs for this function and `overrides` are in `cc_toolchain_config.bzl.tpl`.
86+
cc_toolchain_config(
87+
name = "custom_toolchain_example_config",
88+
host_platform = "darwin",
89+
custom_target_triple = "thumbv7em-unknown-none-gnueabihf",
90+
overrides = {
91+
"target_system_name": "thumbv7em-unknown-none-gnueabihf",
92+
"target_cpu": "thumbv7em",
93+
"target_libc": "unknown",
94+
"abi_libc_version": "unknown",
95+
96+
# If you omit this, be sure to depend on
97+
# `@llvm_toolchain:host_sysroot_components`.
98+
# "sysroot_path": "external/thumbv7-sysroot/sysroot", # TODO: check
99+
100+
"extra_compile_flags": [
101+
"-mthumb",
102+
"-mcpu=cortex-m4",
103+
"-mfpu=fpv4-sp-d16",
104+
"-mfloat-abi=hard",
105+
],
106+
"omit_hosted_linker_flags": True,
107+
"omit_cxx_stdlib_flag": False,
108+
"use_llvm_ar_instead_of_libtool_on_macos": True,
109+
}
110+
)
111+
112+
load("@com_grail_bazel_toolchain//toolchain:rules.bzl", "conditional_cc_toolchain")
113+
conditional_cc_toolchain(
114+
name = "custom_toolchain",
115+
toolchain_config = ":custom_toolchain_example_config",
116+
host_is_darwin = True, # TODO
117+
118+
sysroot_label = "@llvm_toolchain//:host_sysroot_components", # use this if not overriding
119+
# sysroot_label = "@thumbv7-sysroot//:sysroot", # override
120+
121+
absolute_paths = True, # this is required for toolchains set up outside of `@llvm_toolchain`, unfortunately
122+
llvm_repo_label_prefix = "@llvm_toolchain//",
123+
)
124+
125+
# Constraints come from here: https://github.com/bazelbuild/platforms
126+
toolchain(
127+
name = "custom_toolchain_example",
128+
exec_compatible_with = [
129+
"@platforms//cpu:x86_64",
130+
"@platforms//os:osx",
131+
],
132+
target_compatible_with = [
133+
"@platforms//cpu:armv7", # `v7e-mf` has not yet made it to stable Bazel?
134+
# "@platforms//os:none",
135+
],
136+
toolchain = ":custom_toolchain",
137+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
138+
)
139+
140+
platform(
141+
name = "arm",
142+
constraint_values = [
143+
"@platforms//cpu:armv7",
144+
# "@platforms//os:none",
145+
]
146+
)
147+
148+
cc_library(
149+
name = "custom_target_test",
150+
srcs = ["stdlib.cc"],
151+
hdrs = ["stdlib.h"],
152+
target_compatible_with = [
153+
"@platforms//cpu:armv7",
154+
]
155+
)

0 commit comments

Comments
 (0)