14
14
15
15
load ("@rules_cc//cc:defs.bzl" , "cc_library" , "cc_test" )
16
16
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
+
17
57
cc_library (
18
58
name = "stdlib" ,
19
59
srcs = ["stdlib.cc" ],
@@ -34,3 +74,82 @@ sh_test(
34
74
"@llvm_toolchain//:lib/libc++.a" ,
35
75
],
36
76
)
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