1# Copyright 2024 The Pigweed Authors 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); you may not 4# use this file except in compliance with the License. You may obtain a copy of 5# the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12# License for the specific language governing permissions and limitations under 13# the License. 14"""Private templates for generating toolchain repos.""" 15 16_rust_toolchain_template = """\ 17rust_toolchain( 18 name = "{name}_rust_toolchain", 19 binary_ext = "", 20 clippy_driver = "{toolchain_repo}//:bin/clippy-driver", 21 default_edition = "2021", 22 dylib_ext = "{dylib_ext}", 23 exec_compatible_with = {exec_compatible_with}, 24 exec_triple = "{exec_triple}", 25 rust_doc = "{toolchain_repo}//:bin/rustdoc", 26 rust_std = "{target_repo}//:rust_std", 27 rustc = "{toolchain_repo}//:bin/rustc", 28 rustc_lib = "{toolchain_repo}//:rustc_lib", 29 staticlib_ext = ".a", 30 stdlib_linkflags = [], 31 target_compatible_with = {target_compatible_with}, 32 target_triple = "{target_triple}", 33 extra_rustc_flags = {extra_rustc_flags}, 34 extra_exec_rustc_flags = {extra_rustc_flags}, 35 # TODO: https://pwbug.dev/342695883 - Works around confusing 36 # target_compatible_with semantics in rust_toolchain. Figure out how to 37 # do better. 38 tags = ["manual"], 39) 40""" 41 42def rust_toolchain_template( 43 name, 44 exec_triple, 45 target_triple, 46 toolchain_repo, 47 target_repo, 48 dylib_ext, 49 exec_compatible_with, 50 target_compatible_with, 51 extra_rustc_flags): 52 return _rust_toolchain_template.format( 53 name = name, 54 exec_triple = exec_triple, 55 target_triple = target_triple, 56 toolchain_repo = toolchain_repo, 57 target_repo = target_repo, 58 dylib_ext = dylib_ext, 59 exec_compatible_with = json.encode(exec_compatible_with), 60 target_compatible_with = json.encode(target_compatible_with), 61 extra_rustc_flags = json.encode(extra_rustc_flags), 62 ) 63 64_toolchain_template = """\ 65toolchain( 66 name = "{name}", 67 exec_compatible_with = {exec_compatible_with}, 68 target_compatible_with = {target_compatible_with}, 69 target_settings = {target_settings}, 70 toolchain = ":{name}_rust_toolchain", 71 toolchain_type = "@rules_rust//rust:toolchain", 72 ) 73""" 74 75def toolchain_template( 76 name, 77 exec_compatible_with, 78 target_compatible_with, 79 target_settings): 80 return _toolchain_template.format( 81 name = name, 82 exec_compatible_with = json.encode(exec_compatible_with), 83 target_compatible_with = json.encode(target_compatible_with), 84 target_settings = json.encode(target_settings), 85 ) 86 87_rust_analyzer_toolchain_template = """\ 88rust_analyzer_toolchain( 89 name = "{name}_rust_analyzer_toolchain", 90 exec_compatible_with = {exec_compatible_with}, 91 proc_macro_srv = "{toolchain_repo}//:libexec/rust-analyzer-proc-macro-srv", 92 rustc = "{toolchain_repo}//:bin/rustc", 93 rustc_srcs = "{toolchain_repo}//:rustc_srcs", 94 target_compatible_with = {target_compatible_with}, 95 visibility = ["//visibility:public"], 96) 97 98toolchain( 99 name = "{name}", 100 exec_compatible_with = {exec_compatible_with}, 101 target_compatible_with = {target_compatible_with}, 102 target_settings = {target_settings}, 103 toolchain = ":{name}_rust_analyzer_toolchain", 104 toolchain_type = "@rules_rust//rust/rust_analyzer:toolchain_type", 105) 106""" 107 108def rust_analyzer_toolchain_template( 109 name, 110 toolchain_repo, 111 exec_compatible_with, 112 target_compatible_with, 113 target_settings): 114 return _rust_analyzer_toolchain_template.format( 115 name = name, 116 toolchain_repo = toolchain_repo, 117 exec_compatible_with = json.encode(exec_compatible_with), 118 target_compatible_with = json.encode(target_compatible_with), 119 target_settings = json.encode(target_settings), 120 ) 121