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 _ "android/soong/cc/config" 22) 23 24var pctx = android.NewPackageContext("android/soong/rust/config") 25 26var ( 27 RustDefaultVersion = "1.51.0" 28 RustDefaultBase = "prebuilts/rust/" 29 DefaultEdition = "2018" 30 Stdlibs = []string{ 31 "libstd", 32 "libtest", 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 "--remap-path-prefix $$(pwd)=", 46 "-C codegen-units=1", 47 "-C debuginfo=2", 48 "-C opt-level=3", 49 "-C relocation-model=pic", 50 "-C overflow-checks=on", 51 // Use v0 mangling to distinguish from C++ symbols 52 "-Z symbol-mangling-version=v0", 53 } 54 55 deviceGlobalRustFlags = []string{ 56 "-C panic=abort", 57 "-Z link-native-libraries=no", 58 } 59 60 deviceGlobalLinkFlags = []string{ 61 // Prepend the lld flags from cc_config so we stay in sync with cc 62 "${cc_config.DeviceGlobalLldflags}", 63 64 // Override cc's --no-undefined-version to allow rustc's generated alloc functions 65 "-Wl,--undefined-version", 66 67 "-Wl,-Bdynamic", 68 "-nostdlib", 69 "-Wl,--pack-dyn-relocs=android+relr", 70 "-Wl,--use-android-relr-tags", 71 "-Wl,--no-undefined", 72 "-B${cc_config.ClangBin}", 73 } 74) 75 76func init() { 77 pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase) 78 pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS) 79 80 pctx.VariableFunc("RustBase", func(ctx android.PackageVarContext) string { 81 if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" { 82 return override 83 } 84 return "${RustDefaultBase}" 85 }) 86 87 pctx.VariableFunc("RustVersion", func(ctx android.PackageVarContext) string { 88 if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" { 89 return override 90 } 91 return RustDefaultVersion 92 }) 93 94 pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}") 95 pctx.StaticVariable("RustBin", "${RustPath}/bin") 96 97 pctx.ImportAs("cc_config", "android/soong/cc/config") 98 pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++") 99 pctx.StaticVariable("RustLinkerArgs", "") 100 101 pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " ")) 102 103} 104