1*61c4878aSAndroid Build Coastguard Worker# Copyright 2023 The Pigweed Authors 2*61c4878aSAndroid Build Coastguard Worker# 3*61c4878aSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4*61c4878aSAndroid Build Coastguard Worker# use this file except in compliance with the License. You may obtain a copy of 5*61c4878aSAndroid Build Coastguard Worker# the License at 6*61c4878aSAndroid Build Coastguard Worker# 7*61c4878aSAndroid Build Coastguard Worker# https://www.apache.org/licenses/LICENSE-2.0 8*61c4878aSAndroid Build Coastguard Worker# 9*61c4878aSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software 10*61c4878aSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11*61c4878aSAndroid Build Coastguard Worker# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12*61c4878aSAndroid Build Coastguard Worker# License for the specific language governing permissions and limitations under 13*61c4878aSAndroid Build Coastguard Worker# the License. 14*61c4878aSAndroid Build Coastguard Worker"""Utilities for declaring Rust toolchains that are compatible with Arm gcc.""" 15*61c4878aSAndroid Build Coastguard Worker 16*61c4878aSAndroid Build Coastguard Workerload("@rules_rust//rust:toolchain.bzl", "rust_analyzer_toolchain", "rust_toolchain") 17*61c4878aSAndroid Build Coastguard Workerload("//pw_env_setup/bazel/cipd_setup:cipd_rules.bzl", "cipd_repository") 18*61c4878aSAndroid Build Coastguard Workerload(":toolchains.bzl", "CHANNELS", "EXTRA_TARGETS", "HOSTS") 19*61c4878aSAndroid Build Coastguard Worker 20*61c4878aSAndroid Build Coastguard Worker_rust_toolchain_repo_template = """\ 21*61c4878aSAndroid Build Coastguard Workerload("{pigweed_repo_name}//pw_toolchain/rust:defs.bzl", "pw_rust_declare_toolchain_targets") 22*61c4878aSAndroid Build Coastguard Worker 23*61c4878aSAndroid Build Coastguard Workerpackage(default_visibility = ["//visibility:public"]) 24*61c4878aSAndroid Build Coastguard Worker 25*61c4878aSAndroid Build Coastguard Workerlicenses(["notice"]) 26*61c4878aSAndroid Build Coastguard Worker 27*61c4878aSAndroid Build Coastguard Workerpw_rust_declare_toolchain_targets() 28*61c4878aSAndroid Build Coastguard Worker""" 29*61c4878aSAndroid Build Coastguard Worker 30*61c4878aSAndroid Build Coastguard Workerdef _rust_toolchain_repo_impl(ctx): 31*61c4878aSAndroid Build Coastguard Worker ctx.file("BUILD", _rust_toolchain_repo_template.format(pigweed_repo_name = ctx.attr.pigweed_repo_name)) 32*61c4878aSAndroid Build Coastguard Worker pass 33*61c4878aSAndroid Build Coastguard Worker 34*61c4878aSAndroid Build Coastguard Worker_rust_toolchain_repo = repository_rule( 35*61c4878aSAndroid Build Coastguard Worker _rust_toolchain_repo_impl, 36*61c4878aSAndroid Build Coastguard Worker attrs = { 37*61c4878aSAndroid Build Coastguard Worker "pigweed_repo_name": attr.string( 38*61c4878aSAndroid Build Coastguard Worker doc = "The name of the pigweed used to reference build files for the registered repositories.", 39*61c4878aSAndroid Build Coastguard Worker ), 40*61c4878aSAndroid Build Coastguard Worker }, 41*61c4878aSAndroid Build Coastguard Worker) 42*61c4878aSAndroid Build Coastguard Worker 43*61c4878aSAndroid Build Coastguard Worker# buildifier: disable=unnamed-macro 44*61c4878aSAndroid Build Coastguard Workerdef pw_rust_register_toolchain_and_target_repos(cipd_tag, pigweed_repo_name = "@pigweed"): 45*61c4878aSAndroid Build Coastguard Worker """Declare and register CIPD repos for Rust toolchain and target rupport. 46*61c4878aSAndroid Build Coastguard Worker 47*61c4878aSAndroid Build Coastguard Worker Args: 48*61c4878aSAndroid Build Coastguard Worker cipd_tag: Tag with which to select specific package versions. 49*61c4878aSAndroid Build Coastguard Worker pigweed_repo_name: The name of the pigweed used to reference build files 50*61c4878aSAndroid Build Coastguard Worker for the registered repositories. Defaults to "@pigweed". 51*61c4878aSAndroid Build Coastguard Worker """ 52*61c4878aSAndroid Build Coastguard Worker toolchain_repo_name = "{}_rust_toolchain_repo".format(pigweed_repo_name).lstrip("@") 53*61c4878aSAndroid Build Coastguard Worker _rust_toolchain_repo(name = toolchain_repo_name, pigweed_repo_name = pigweed_repo_name) 54*61c4878aSAndroid Build Coastguard Worker for host in HOSTS: 55*61c4878aSAndroid Build Coastguard Worker cipd_os = host["os"] 56*61c4878aSAndroid Build Coastguard Worker if cipd_os == "macos": 57*61c4878aSAndroid Build Coastguard Worker cipd_os = "mac" 58*61c4878aSAndroid Build Coastguard Worker 59*61c4878aSAndroid Build Coastguard Worker cipd_repository( 60*61c4878aSAndroid Build Coastguard Worker name = "rust_toolchain_host_{}_{}".format(host["os"], host["cpu"]), 61*61c4878aSAndroid Build Coastguard Worker build_file = "{}//pw_toolchain/rust:rust_toolchain.BUILD".format(pigweed_repo_name), 62*61c4878aSAndroid Build Coastguard Worker path = "fuchsia/third_party/rust/host/{}-{}".format(cipd_os, host["cipd_arch"]), 63*61c4878aSAndroid Build Coastguard Worker tag = cipd_tag, 64*61c4878aSAndroid Build Coastguard Worker ) 65*61c4878aSAndroid Build Coastguard Worker 66*61c4878aSAndroid Build Coastguard Worker cipd_repository( 67*61c4878aSAndroid Build Coastguard Worker name = "rust_toolchain_target_{}".format(host["triple"]), 68*61c4878aSAndroid Build Coastguard Worker build_file = "{}//pw_toolchain/rust:rust_stdlib.BUILD".format(pigweed_repo_name), 69*61c4878aSAndroid Build Coastguard Worker path = "fuchsia/third_party/rust/target/{}".format(host["triple"]), 70*61c4878aSAndroid Build Coastguard Worker tag = cipd_tag, 71*61c4878aSAndroid Build Coastguard Worker ) 72*61c4878aSAndroid Build Coastguard Worker 73*61c4878aSAndroid Build Coastguard Worker for target in EXTRA_TARGETS: 74*61c4878aSAndroid Build Coastguard Worker cipd_repository( 75*61c4878aSAndroid Build Coastguard Worker name = "rust_toolchain_target_{}".format(target["triple"]), 76*61c4878aSAndroid Build Coastguard Worker build_file = "{}//pw_toolchain/rust:rust_stdlib.BUILD".format(pigweed_repo_name), 77*61c4878aSAndroid Build Coastguard Worker path = "fuchsia/third_party/rust/target/{}".format(target["triple"]), 78*61c4878aSAndroid Build Coastguard Worker tag = cipd_tag, 79*61c4878aSAndroid Build Coastguard Worker ) 80*61c4878aSAndroid Build Coastguard Worker 81*61c4878aSAndroid Build Coastguard Worker# buildifier: disable=unnamed-macro 82*61c4878aSAndroid Build Coastguard Workerdef pw_rust_register_toolchains(pigweed_repo_name = "@pigweed"): 83*61c4878aSAndroid Build Coastguard Worker """Register Rust Toolchains 84*61c4878aSAndroid Build Coastguard Worker 85*61c4878aSAndroid Build Coastguard Worker Args: 86*61c4878aSAndroid Build Coastguard Worker pigweed_repo_name: The name of the pigweed used to reference build files 87*61c4878aSAndroid Build Coastguard Worker for the registered repositories. Defaults to "@pigweed". 88*61c4878aSAndroid Build Coastguard Worker 89*61c4878aSAndroid Build Coastguard Worker For this registration to be valid one must 90*61c4878aSAndroid Build Coastguard Worker 1. Call `pw_rust_register_toolchain_and_target_repos(tag)` pervisouly in the 91*61c4878aSAndroid Build Coastguard Worker WORKSPACE file. 92*61c4878aSAndroid Build Coastguard Worker 2. Call `pw_rust_declare_toolchain_targets()` from 93*61c4878aSAndroid Build Coastguard Worker `//pw_toolchain/rust/BUILD.bazel`. 94*61c4878aSAndroid Build Coastguard Worker """ 95*61c4878aSAndroid Build Coastguard Worker 96*61c4878aSAndroid Build Coastguard Worker toolchain_repo = "{}_rust_toolchain_repo".format(pigweed_repo_name) 97*61c4878aSAndroid Build Coastguard Worker 98*61c4878aSAndroid Build Coastguard Worker for channel in CHANNELS: 99*61c4878aSAndroid Build Coastguard Worker for host in HOSTS: 100*61c4878aSAndroid Build Coastguard Worker native.register_toolchains( 101*61c4878aSAndroid Build Coastguard Worker "{}//:host_rust_toolchain_{}_{}_{}".format(toolchain_repo, host["os"], host["cpu"], channel["name"]), 102*61c4878aSAndroid Build Coastguard Worker "{}//:host_rust_analyzer_toolchain_{}_{}_{}".format(toolchain_repo, host["os"], host["cpu"], channel["name"]), 103*61c4878aSAndroid Build Coastguard Worker ) 104*61c4878aSAndroid Build Coastguard Worker for target in EXTRA_TARGETS: 105*61c4878aSAndroid Build Coastguard Worker native.register_toolchains( 106*61c4878aSAndroid Build Coastguard Worker "{}//:{}_{}_rust_toolchain_{}_{}_{}".format(toolchain_repo, host["os"], host["cpu"], target["triple"], target["cpu"], channel["name"]), 107*61c4878aSAndroid Build Coastguard Worker ) 108*61c4878aSAndroid Build Coastguard Worker 109*61c4878aSAndroid Build Coastguard Worker# buildifier: disable=unnamed-macro 110*61c4878aSAndroid Build Coastguard Workerdef pw_rust_declare_toolchain_targets(): 111*61c4878aSAndroid Build Coastguard Worker """Declare rust toolchain targets""" 112*61c4878aSAndroid Build Coastguard Worker for channel in CHANNELS: 113*61c4878aSAndroid Build Coastguard Worker for host in HOSTS: 114*61c4878aSAndroid Build Coastguard Worker _pw_rust_host_toolchain( 115*61c4878aSAndroid Build Coastguard Worker name = "host_rust_toolchain_{}_{}_{}".format(host["os"], host["cpu"], channel["name"]), 116*61c4878aSAndroid Build Coastguard Worker analyzer_toolchain_name = "host_rust_analyzer_toolchain_{}_{}_{}".format(host["os"], host["cpu"], channel["name"]), 117*61c4878aSAndroid Build Coastguard Worker compatible_with = [ 118*61c4878aSAndroid Build Coastguard Worker "@platforms//cpu:{}".format(host["cpu"]), 119*61c4878aSAndroid Build Coastguard Worker "@platforms//os:{}".format(host["os"]), 120*61c4878aSAndroid Build Coastguard Worker ], 121*61c4878aSAndroid Build Coastguard Worker target_settings = channel["target_settings"], 122*61c4878aSAndroid Build Coastguard Worker dylib_ext = host["dylib_ext"], 123*61c4878aSAndroid Build Coastguard Worker target_repo = "@rust_toolchain_target_{}".format(host["triple"]), 124*61c4878aSAndroid Build Coastguard Worker toolchain_repo = "@rust_toolchain_host_{}_{}".format(host["os"], host["cpu"]), 125*61c4878aSAndroid Build Coastguard Worker triple = host["triple"], 126*61c4878aSAndroid Build Coastguard Worker extra_rustc_flags = channel["extra_rustc_flags"], 127*61c4878aSAndroid Build Coastguard Worker ) 128*61c4878aSAndroid Build Coastguard Worker for target in EXTRA_TARGETS: 129*61c4878aSAndroid Build Coastguard Worker _pw_rust_toolchain( 130*61c4878aSAndroid Build Coastguard Worker name = "{}_{}_rust_toolchain_{}_{}_{}".format(host["os"], host["cpu"], target["triple"], target["cpu"], channel["name"]), 131*61c4878aSAndroid Build Coastguard Worker exec_triple = host["triple"], 132*61c4878aSAndroid Build Coastguard Worker target_triple = target["triple"], 133*61c4878aSAndroid Build Coastguard Worker target_repo = "@rust_toolchain_target_{}".format(target["triple"]), 134*61c4878aSAndroid Build Coastguard Worker toolchain_repo = "@rust_toolchain_host_{}_{}".format(host["os"], host["cpu"]), 135*61c4878aSAndroid Build Coastguard Worker dylib_ext = "*.so", 136*61c4878aSAndroid Build Coastguard Worker exec_compatible_with = [ 137*61c4878aSAndroid Build Coastguard Worker "@platforms//cpu:{}".format(host["cpu"]), 138*61c4878aSAndroid Build Coastguard Worker "@platforms//os:{}".format(host["os"]), 139*61c4878aSAndroid Build Coastguard Worker ], 140*61c4878aSAndroid Build Coastguard Worker target_compatible_with = [ 141*61c4878aSAndroid Build Coastguard Worker "@platforms//cpu:{}".format(target["cpu"]), 142*61c4878aSAndroid Build Coastguard Worker ], 143*61c4878aSAndroid Build Coastguard Worker target_settings = channel["target_settings"], 144*61c4878aSAndroid Build Coastguard Worker extra_rustc_flags = channel["extra_rustc_flags"], 145*61c4878aSAndroid Build Coastguard Worker ) 146*61c4878aSAndroid Build Coastguard Worker 147*61c4878aSAndroid Build Coastguard Workerdef _pw_rust_toolchain( 148*61c4878aSAndroid Build Coastguard Worker name, 149*61c4878aSAndroid Build Coastguard Worker exec_triple, 150*61c4878aSAndroid Build Coastguard Worker target_triple, 151*61c4878aSAndroid Build Coastguard Worker toolchain_repo, 152*61c4878aSAndroid Build Coastguard Worker target_repo, 153*61c4878aSAndroid Build Coastguard Worker dylib_ext, 154*61c4878aSAndroid Build Coastguard Worker exec_compatible_with, 155*61c4878aSAndroid Build Coastguard Worker target_compatible_with, 156*61c4878aSAndroid Build Coastguard Worker target_settings, 157*61c4878aSAndroid Build Coastguard Worker extra_rustc_flags): 158*61c4878aSAndroid Build Coastguard Worker rust_toolchain( 159*61c4878aSAndroid Build Coastguard Worker name = "{}_rust_toolchain".format(name), 160*61c4878aSAndroid Build Coastguard Worker binary_ext = "", 161*61c4878aSAndroid Build Coastguard Worker clippy_driver = "{}//:bin/clippy-driver".format(toolchain_repo), 162*61c4878aSAndroid Build Coastguard Worker default_edition = "2021", 163*61c4878aSAndroid Build Coastguard Worker dylib_ext = dylib_ext, 164*61c4878aSAndroid Build Coastguard Worker exec_compatible_with = exec_compatible_with, 165*61c4878aSAndroid Build Coastguard Worker exec_triple = exec_triple, 166*61c4878aSAndroid Build Coastguard Worker rust_doc = "{}//:bin/rustdoc".format(toolchain_repo), 167*61c4878aSAndroid Build Coastguard Worker rust_std = "{}//:rust_std".format(target_repo), 168*61c4878aSAndroid Build Coastguard Worker rustc = "{}//:bin/rustc".format(toolchain_repo), 169*61c4878aSAndroid Build Coastguard Worker rustc_lib = "{}//:rustc_lib".format(toolchain_repo), 170*61c4878aSAndroid Build Coastguard Worker staticlib_ext = ".a", 171*61c4878aSAndroid Build Coastguard Worker stdlib_linkflags = [], 172*61c4878aSAndroid Build Coastguard Worker target_compatible_with = target_compatible_with, 173*61c4878aSAndroid Build Coastguard Worker target_triple = target_triple, 174*61c4878aSAndroid Build Coastguard Worker extra_rustc_flags = extra_rustc_flags, 175*61c4878aSAndroid Build Coastguard Worker extra_exec_rustc_flags = extra_rustc_flags, 176*61c4878aSAndroid Build Coastguard Worker # TODO: https://pwbug.dev/342695883 - Works around confusing 177*61c4878aSAndroid Build Coastguard Worker # target_compatible_with semantics in rust_toolchain. Figure out how to 178*61c4878aSAndroid Build Coastguard Worker # do better. 179*61c4878aSAndroid Build Coastguard Worker tags = ["manual"], 180*61c4878aSAndroid Build Coastguard Worker ) 181*61c4878aSAndroid Build Coastguard Worker native.toolchain( 182*61c4878aSAndroid Build Coastguard Worker name = name, 183*61c4878aSAndroid Build Coastguard Worker exec_compatible_with = exec_compatible_with, 184*61c4878aSAndroid Build Coastguard Worker target_compatible_with = target_compatible_with, 185*61c4878aSAndroid Build Coastguard Worker target_settings = target_settings, 186*61c4878aSAndroid Build Coastguard Worker toolchain = ":{}_rust_toolchain".format(name), 187*61c4878aSAndroid Build Coastguard Worker toolchain_type = "@rules_rust//rust:toolchain", 188*61c4878aSAndroid Build Coastguard Worker ) 189*61c4878aSAndroid Build Coastguard Worker 190*61c4878aSAndroid Build Coastguard Workerdef _pw_rust_host_toolchain( 191*61c4878aSAndroid Build Coastguard Worker name, 192*61c4878aSAndroid Build Coastguard Worker analyzer_toolchain_name, 193*61c4878aSAndroid Build Coastguard Worker triple, 194*61c4878aSAndroid Build Coastguard Worker toolchain_repo, 195*61c4878aSAndroid Build Coastguard Worker target_repo, 196*61c4878aSAndroid Build Coastguard Worker dylib_ext, 197*61c4878aSAndroid Build Coastguard Worker compatible_with, 198*61c4878aSAndroid Build Coastguard Worker target_settings, 199*61c4878aSAndroid Build Coastguard Worker extra_rustc_flags): 200*61c4878aSAndroid Build Coastguard Worker _pw_rust_toolchain( 201*61c4878aSAndroid Build Coastguard Worker name = name, 202*61c4878aSAndroid Build Coastguard Worker exec_triple = triple, 203*61c4878aSAndroid Build Coastguard Worker target_triple = triple, 204*61c4878aSAndroid Build Coastguard Worker toolchain_repo = toolchain_repo, 205*61c4878aSAndroid Build Coastguard Worker target_repo = target_repo, 206*61c4878aSAndroid Build Coastguard Worker dylib_ext = dylib_ext, 207*61c4878aSAndroid Build Coastguard Worker exec_compatible_with = compatible_with, 208*61c4878aSAndroid Build Coastguard Worker target_compatible_with = compatible_with, 209*61c4878aSAndroid Build Coastguard Worker target_settings = target_settings, 210*61c4878aSAndroid Build Coastguard Worker extra_rustc_flags = extra_rustc_flags, 211*61c4878aSAndroid Build Coastguard Worker ) 212*61c4878aSAndroid Build Coastguard Worker 213*61c4878aSAndroid Build Coastguard Worker rust_analyzer_toolchain( 214*61c4878aSAndroid Build Coastguard Worker name = "{}_rust_analyzer_toolchain".format(analyzer_toolchain_name), 215*61c4878aSAndroid Build Coastguard Worker exec_compatible_with = compatible_with, 216*61c4878aSAndroid Build Coastguard Worker proc_macro_srv = "{}//:libexec/rust-analyzer-proc-macro-srv".format(toolchain_repo), 217*61c4878aSAndroid Build Coastguard Worker rustc = "{}//:bin/rustc".format(toolchain_repo), 218*61c4878aSAndroid Build Coastguard Worker rustc_srcs = "{}//:rustc_srcs".format(toolchain_repo), 219*61c4878aSAndroid Build Coastguard Worker target_compatible_with = compatible_with, 220*61c4878aSAndroid Build Coastguard Worker visibility = ["//visibility:public"], 221*61c4878aSAndroid Build Coastguard Worker ) 222*61c4878aSAndroid Build Coastguard Worker 223*61c4878aSAndroid Build Coastguard Worker native.toolchain( 224*61c4878aSAndroid Build Coastguard Worker name = analyzer_toolchain_name, 225*61c4878aSAndroid Build Coastguard Worker exec_compatible_with = compatible_with, 226*61c4878aSAndroid Build Coastguard Worker target_compatible_with = compatible_with, 227*61c4878aSAndroid Build Coastguard Worker target_settings = target_settings, 228*61c4878aSAndroid Build Coastguard Worker toolchain = ":{}_rust_analyzer_toolchain".format(analyzer_toolchain_name), 229*61c4878aSAndroid Build Coastguard Worker toolchain_type = "@rules_rust//rust/rust_analyzer:toolchain_type", 230*61c4878aSAndroid Build Coastguard Worker ) 231