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