1// Copyright 2019 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://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, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package config 16 17import ( 18 "strings" 19 20 "android/soong/android" 21) 22 23var ( 24 Arm64RustFlags = []string{ 25 "-C force-frame-pointers=y", 26 } 27 Arm64ArchFeatureRustFlags = map[string][]string{} 28 Arm64LinkFlags = []string{} 29 30 Arm64ArchVariantRustFlags = map[string][]string{ 31 "armv8-a": []string{}, 32 "armv8-a-branchprot": []string{ 33 // branch-protection=bti,pac-ret is equivalent to Clang's mbranch-protection=standard 34 "-Z branch-protection=bti,pac-ret", 35 }, 36 "armv8-2a": []string{}, 37 "armv8-2a-dotprod": []string{}, 38 39 // branch-protection=bti,pac-ret is equivalent to Clang's mbranch-protection=standard 40 "armv9-a": []string{ 41 "-Z branch-protection=bti,pac-ret", 42 "-Z stack-protector=none", 43 }, 44 "armv9-2a": []string{ 45 "-Z branch-protection=bti,pac-ret", 46 "-Z stack-protector=none", 47 }, 48 "armv9-3a": []string{ 49 "-Z branch-protection=bti,pac-ret", 50 "-Z stack-protector=none", 51 }, 52 "armv9-4a": []string{ 53 "-Z branch-protection=bti,pac-ret", 54 "-Z stack-protector=none", 55 }, 56 } 57) 58 59func init() { 60 registerToolchainFactory(android.Android, android.Arm64, Arm64ToolchainFactory) 61 62 pctx.StaticVariable("Arm64ToolchainRustFlags", strings.Join(Arm64RustFlags, " ")) 63 pctx.StaticVariable("Arm64ToolchainLinkFlags", strings.Join(Arm64LinkFlags, " ")) 64 65 for variant, rustFlags := range Arm64ArchVariantRustFlags { 66 pctx.StaticVariable("Arm64"+variant+"VariantRustFlags", 67 strings.Join(rustFlags, " ")) 68 } 69 70 pctx.StaticVariable("DEVICE_ARM64_RUSTC_FLAGS", strings.Join(Arm64RustFlags, " ")) 71} 72 73type toolchainArm64 struct { 74 toolchain64Bit 75 toolchainRustFlags string 76} 77 78func (t *toolchainArm64) RustTriple() string { 79 return "aarch64-linux-android" 80} 81 82func (t *toolchainArm64) ToolchainLinkFlags() string { 83 // Prepend the lld flags from cc_config so we stay in sync with cc 84 return "${config.DeviceGlobalLinkFlags} ${cc_config.Arm64Lldflags} ${config.Arm64ToolchainLinkFlags}" 85} 86 87func (t *toolchainArm64) ToolchainRustFlags() string { 88 return t.toolchainRustFlags 89} 90 91func (t *toolchainArm64) RustFlags() string { 92 return "${config.Arm64ToolchainRustFlags}" 93} 94 95func (t *toolchainArm64) Supported() bool { 96 return true 97} 98 99func (toolchainArm64) LibclangRuntimeLibraryArch() string { 100 return "aarch64" 101} 102 103func Arm64ToolchainFactory(arch android.Arch) Toolchain { 104 archVariant := arch.ArchVariant 105 if archVariant == "" { 106 // arch variants defaults to armv8-a. This is mostly for 107 // the host target which borrows toolchain configs from here. 108 archVariant = "armv8-a" 109 } 110 111 toolchainRustFlags := []string{ 112 "${config.Arm64ToolchainRustFlags}", 113 "${config.Arm64" + archVariant + "VariantRustFlags}", 114 } 115 116 toolchainRustFlags = append(toolchainRustFlags, deviceGlobalRustFlags...) 117 118 for _, feature := range arch.ArchFeatures { 119 toolchainRustFlags = append(toolchainRustFlags, Arm64ArchFeatureRustFlags[feature]...) 120 } 121 122 return &toolchainArm64{ 123 toolchainRustFlags: strings.Join(toolchainRustFlags, " "), 124 } 125} 126