1+ """WASI SDK toolchain definitions"""
2+
3+ load ("@bazel_tools//tools/build_defs/repo:http.bzl" , "http_archive" )
4+
5+ WASI_SDK_PLATFORMS = {
6+ "darwin_amd64" : struct (
7+ sha256 = "1234567890abcdef" , # TODO: Add real checksums
8+ url_suffix = "darwin.tar.gz" ,
9+ ),
10+ "darwin_arm64" : struct (
11+ sha256 = "1234567890abcdef" ,
12+ url_suffix = "darwin.tar.gz" ,
13+ ),
14+ "linux_amd64" : struct (
15+ sha256 = "1234567890abcdef" ,
16+ url_suffix = "linux.tar.gz" ,
17+ ),
18+ "linux_arm64" : struct (
19+ sha256 = "1234567890abcdef" ,
20+ url_suffix = "linux-arm64.tar.gz" ,
21+ ),
22+ }
23+
24+ def _wasi_sdk_toolchain_impl (ctx ):
25+ """Implementation of wasi_sdk_toolchain rule"""
26+
27+ toolchain_info = platform_common .ToolchainInfo (
28+ wasi_sdk_root = ctx .attr .wasi_sdk_root ,
29+ clang = ctx .file .clang ,
30+ ar = ctx .file .ar ,
31+ ld = ctx .file .ld ,
32+ nm = ctx .file .nm ,
33+ objdump = ctx .file .objdump ,
34+ strip = ctx .file .strip ,
35+ sysroot = ctx .attr .sysroot ,
36+ clang_version = ctx .attr .clang_version ,
37+ )
38+
39+ return [toolchain_info ]
40+
41+ wasi_sdk_toolchain = rule (
42+ implementation = _wasi_sdk_toolchain_impl ,
43+ attrs = {
44+ "wasi_sdk_root" : attr .label (
45+ doc = "Root directory of WASI SDK" ,
46+ ),
47+ "clang" : attr .label (
48+ allow_single_file = True ,
49+ executable = True ,
50+ cfg = "exec" ,
51+ doc = "clang compiler" ,
52+ ),
53+ "ar" : attr .label (
54+ allow_single_file = True ,
55+ executable = True ,
56+ cfg = "exec" ,
57+ doc = "ar archiver" ,
58+ ),
59+ "ld" : attr .label (
60+ allow_single_file = True ,
61+ executable = True ,
62+ cfg = "exec" ,
63+ doc = "wasm-ld linker" ,
64+ ),
65+ "nm" : attr .label (
66+ allow_single_file = True ,
67+ executable = True ,
68+ cfg = "exec" ,
69+ doc = "llvm-nm" ,
70+ ),
71+ "objdump" : attr .label (
72+ allow_single_file = True ,
73+ executable = True ,
74+ cfg = "exec" ,
75+ doc = "llvm-objdump" ,
76+ ),
77+ "strip" : attr .label (
78+ allow_single_file = True ,
79+ executable = True ,
80+ cfg = "exec" ,
81+ doc = "llvm-strip" ,
82+ ),
83+ "sysroot" : attr .string (
84+ doc = "Path to WASI sysroot" ,
85+ ),
86+ "clang_version" : attr .string (
87+ doc = "Clang version number" ,
88+ default = "19" ,
89+ ),
90+ },
91+ doc = "Declares a WASI SDK toolchain" ,
92+ )
93+
94+ def _detect_host_platform (repository_ctx ):
95+ """Detect the host platform"""
96+
97+ os_name = repository_ctx .os .name .lower ()
98+ arch = repository_ctx .os .arch .lower ()
99+
100+ if os_name == "mac os x" :
101+ os_name = "darwin"
102+
103+ if arch == "x86_64" :
104+ arch = "amd64"
105+ elif arch == "aarch64" :
106+ arch = "arm64"
107+
108+ return "{}_{}" .format (os_name , arch )
109+
110+ def _wasi_sdk_repository_impl (repository_ctx ):
111+ """Create WASI SDK repository"""
112+
113+ strategy = repository_ctx .attr .strategy
114+
115+ if strategy == "system" :
116+ _setup_system_wasi_sdk (repository_ctx )
117+ elif strategy == "download" :
118+ _setup_downloaded_wasi_sdk (repository_ctx )
119+ else :
120+ fail ("Unknown strategy: {}. Must be 'system' or 'download'" .format (strategy ))
121+
122+ # Create BUILD file
123+ _create_wasi_sdk_build_file (repository_ctx )
124+
125+ def _setup_system_wasi_sdk (repository_ctx ):
126+ """Use system-installed WASI SDK"""
127+
128+ # Default to /usr/local/wasi-sdk
129+ wasi_sdk_root = repository_ctx .attr .wasi_sdk_root or "/usr/local/wasi-sdk"
130+
131+ # Create symlinks to system tools
132+ repository_ctx .symlink (wasi_sdk_root , "wasi-sdk" )
133+
134+ def _setup_downloaded_wasi_sdk (repository_ctx ):
135+ """Download WASI SDK from GitHub releases"""
136+
137+ version = repository_ctx .attr .version
138+ platform = _detect_host_platform (repository_ctx )
139+
140+ # Download WASI SDK
141+ url = repository_ctx .attr .url
142+ if not url :
143+ # Example URL format - needs to be updated with actual WASI SDK URLs
144+ url = "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-{}/wasi-sdk-{}-{}.tar.gz" .format (
145+ version ,
146+ version ,
147+ platform ,
148+ )
149+
150+ repository_ctx .download_and_extract (
151+ url = url ,
152+ stripPrefix = "wasi-sdk-{}" .format (version ),
153+ )
154+
155+ def _create_wasi_sdk_build_file (repository_ctx ):
156+ """Create BUILD file for WASI SDK"""
157+
158+ build_content = '''
159+ load("@rules_wasm_component//toolchains:wasi_sdk_toolchain.bzl", "wasi_sdk_toolchain")
160+
161+ package(default_visibility = ["//visibility:public"])
162+
163+ # File targets for WASI SDK tools
164+ filegroup(
165+ name = "clang",
166+ srcs = ["wasi-sdk/bin/clang"],
167+ )
168+
169+ filegroup(
170+ name = "ar",
171+ srcs = ["wasi-sdk/bin/ar"],
172+ )
173+
174+ filegroup(
175+ name = "wasm_ld",
176+ srcs = ["wasi-sdk/bin/wasm-ld"],
177+ )
178+
179+ filegroup(
180+ name = "llvm_nm",
181+ srcs = ["wasi-sdk/bin/llvm-nm"],
182+ )
183+
184+ filegroup(
185+ name = "llvm_objdump",
186+ srcs = ["wasi-sdk/bin/llvm-objdump"],
187+ )
188+
189+ filegroup(
190+ name = "llvm_strip",
191+ srcs = ["wasi-sdk/bin/llvm-strip"],
192+ )
193+
194+ filegroup(
195+ name = "sysroot",
196+ srcs = glob(["wasi-sdk/share/wasi-sysroot/**"]),
197+ )
198+
199+ filegroup(
200+ name = "clang_includes",
201+ srcs = glob(["wasi-sdk/lib/clang/*/include/**"]),
202+ )
203+
204+ # WASI SDK toolchain
205+ wasi_sdk_toolchain(
206+ name = "wasi_sdk_impl",
207+ wasi_sdk_root = ":sysroot",
208+ clang = ":clang",
209+ ar = ":ar",
210+ ld = ":wasm_ld",
211+ nm = ":llvm_nm",
212+ objdump = ":llvm_objdump",
213+ strip = ":llvm_strip",
214+ sysroot = "wasi-sdk/share/wasi-sysroot",
215+ clang_version = "19",
216+ )
217+
218+ # Toolchain registration
219+ toolchain(
220+ name = "wasi_sdk_toolchain",
221+ toolchain = ":wasi_sdk_impl",
222+ toolchain_type = "@rules_wasm_component//toolchains:wasi_sdk_toolchain_type",
223+ exec_compatible_with = [],
224+ target_compatible_with = [
225+ "@platforms//cpu:wasm32",
226+ "@platforms//os:wasi",
227+ ],
228+ )
229+
230+ # Alias for registration
231+ alias(
232+ name = "all",
233+ actual = ":wasi_sdk_toolchain",
234+ )
235+ '''
236+
237+ repository_ctx .file ("BUILD.bazel" , build_content )
238+
239+ wasi_sdk_repository = repository_rule (
240+ implementation = _wasi_sdk_repository_impl ,
241+ attrs = {
242+ "strategy" : attr .string (
243+ doc = "Strategy: 'system' or 'download'" ,
244+ default = "system" ,
245+ values = ["system" , "download" ],
246+ ),
247+ "version" : attr .string (
248+ doc = "WASI SDK version" ,
249+ default = "22" ,
250+ ),
251+ "url" : attr .string (
252+ doc = "Custom download URL (optional)" ,
253+ ),
254+ "wasi_sdk_root" : attr .string (
255+ doc = "Path to system WASI SDK (for 'system' strategy)" ,
256+ ),
257+ },
258+ )
0 commit comments