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 "fmt" 19 "strings" 20 21 "android/soong/android" 22 _ "android/soong/cc/config" 23) 24 25var ( 26 pctx = android.NewPackageContext("android/soong/rust/config") 27 28 RustDefaultVersion = "1.81.0" 29 RustDefaultBase = "prebuilts/rust/" 30 DefaultEdition = "2021" 31 Stdlibs = []string{ 32 "libstd", 33 } 34 35 // Mapping between Soong internal arch types and std::env constants. 36 // Required as Rust uses aarch64 when Soong uses arm64. 37 StdEnvArch = map[android.ArchType]string{ 38 android.Arm: "arm", 39 android.Arm64: "aarch64", 40 android.X86: "x86", 41 android.X86_64: "x86_64", 42 } 43 44 GlobalRustFlags = []string{ 45 "-Z stack-protector=strong", 46 "-Z remap-cwd-prefix=.", 47 "-C debuginfo=2", 48 "-C opt-level=3", 49 "-C relocation-model=pic", 50 "-C overflow-checks=on", 51 "-C force-unwind-tables=yes", 52 // Use v0 mangling to distinguish from C++ symbols 53 "-C symbol-mangling-version=v0", 54 "--color=always", 55 "-Z dylib-lto", 56 "-Z link-native-libraries=no", 57 58 // cfg flag to indicate that we are building in AOSP with Soong 59 "--cfg soong", 60 } 61 62 LinuxHostGlobalLinkFlags = []string{ 63 "-lc", 64 "-lrt", 65 "-ldl", 66 "-lpthread", 67 "-lm", 68 "-lgcc_s", 69 "-Wl,--compress-debug-sections=zstd", 70 } 71 72 deviceGlobalRustFlags = []string{ 73 "-C panic=abort", 74 // Generate additional debug info for AutoFDO 75 "-Z debug-info-for-profiling", 76 // Android has ELF TLS on platform 77 "-Z tls-model=global-dynamic", 78 } 79 80 deviceGlobalLinkFlags = []string{ 81 // Prepend the lld flags from cc_config so we stay in sync with cc 82 "${cc_config.DeviceGlobalLldflags}", 83 84 // Override cc's --no-undefined-version to allow rustc's generated alloc functions 85 "-Wl,--undefined-version", 86 87 "-Wl,-Bdynamic", 88 "-nostdlib", 89 "-Wl,--pack-dyn-relocs=android+relr", 90 "-Wl,--use-android-relr-tags", 91 "-Wl,--no-undefined", 92 "-B${cc_config.ClangBin}", 93 "-Wl,--compress-debug-sections=zstd", 94 } 95) 96 97func RustPath(ctx android.PathContext) string { 98 // I can't see any way to flatten the static variable inside Soong, so this 99 // reproduces the init logic. 100 var RustBase string = RustDefaultBase 101 if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" { 102 RustBase = override 103 } 104 return fmt.Sprintf("%s/%s/%s", RustBase, HostPrebuiltTag(ctx.Config()), GetRustVersion(ctx)) 105} 106 107func init() { 108 pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase) 109 pctx.VariableConfigMethod("HostPrebuiltTag", HostPrebuiltTag) 110 111 pctx.VariableFunc("RustBase", func(ctx android.PackageVarContext) string { 112 if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" { 113 return override 114 } 115 return "${RustDefaultBase}" 116 }) 117 118 pctx.VariableFunc("RustVersion", getRustVersionPctx) 119 120 pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}") 121 pctx.StaticVariable("RustBin", "${RustPath}/bin") 122 123 pctx.ImportAs("cc_config", "android/soong/cc/config") 124 pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++") 125 126 pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " ")) 127 128 pctx.StaticVariable("RUST_DEFAULT_VERSION", RustDefaultVersion) 129 pctx.StaticVariable("GLOBAL_RUSTC_FLAGS", strings.Join(GlobalRustFlags, " ")) 130 pctx.StaticVariable("LINUX_HOST_GLOBAL_LINK_FLAGS", strings.Join(LinuxHostGlobalLinkFlags, " ")) 131 132 pctx.StaticVariable("DEVICE_GLOBAL_RUSTC_FLAGS", strings.Join(deviceGlobalRustFlags, " ")) 133 pctx.StaticVariable("DEVICE_GLOBAL_LINK_FLAGS", 134 strings.Join(android.RemoveListFromList(deviceGlobalLinkFlags, []string{ 135 // The cc_config flags are retrieved from cc_toolchain by rust rules. 136 "${cc_config.DeviceGlobalLldflags}", 137 "-B${cc_config.ClangBin}", 138 }), " ")) 139} 140 141func HostPrebuiltTag(config android.Config) string { 142 if config.UseHostMusl() { 143 return "linux-musl-x86" 144 } else { 145 return config.PrebuiltOS() 146 } 147} 148 149func getRustVersionPctx(ctx android.PackageVarContext) string { 150 return GetRustVersion(ctx) 151} 152 153func GetRustVersion(ctx android.PathContext) string { 154 if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" { 155 return override 156 } 157 return RustDefaultVersion 158} 159