Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 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 | |
| 15 | package config |
| 16 | |
| 17 | import ( |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 18 | "strings" |
| 19 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 20 | "android/soong/android" |
| 21 | _ "android/soong/cc/config" |
| 22 | ) |
| 23 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 24 | var ( |
Vinh Tran | 80f6b21 | 2023-08-23 13:49:13 -0400 | [diff] [blame] | 25 | pctx = android.NewPackageContext("android/soong/rust/config") |
| 26 | exportedVars = android.NewExportedVariables(pctx) |
| 27 | |
James Farrell | 732b37f | 2023-09-13 23:34:18 +0000 | [diff] [blame] | 28 | RustDefaultVersion = "1.72.0" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 29 | RustDefaultBase = "prebuilts/rust/" |
Matthew Maurer | c1738b4 | 2021-12-13 18:20:53 +0000 | [diff] [blame] | 30 | DefaultEdition = "2021" |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 31 | Stdlibs = []string{ |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 32 | "libstd", |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 33 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 34 | |
Thiébaud Weksteen | 71512f3 | 2020-11-03 15:17:51 +0100 | [diff] [blame] | 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 | |
Joel Galenson | 724286c | 2019-09-30 13:01:37 -0700 | [diff] [blame] | 44 | GlobalRustFlags = []string{ |
Ivan Lozano | 464f3b3 | 2023-06-22 18:03:16 +0000 | [diff] [blame] | 45 | "-Z stack-protector=strong", |
Chris Wailes | bc62193 | 2021-12-09 13:56:32 -0800 | [diff] [blame] | 46 | "-Z remap-cwd-prefix=.", |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 47 | "-C codegen-units=1", |
Peter Collingbourne | b143cd9 | 2020-12-30 21:14:37 -0800 | [diff] [blame] | 48 | "-C debuginfo=2", |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 49 | "-C opt-level=3", |
| 50 | "-C relocation-model=pic", |
Matthew Maurer | 378bf73 | 2021-03-30 11:49:08 -0700 | [diff] [blame] | 51 | "-C overflow-checks=on", |
Matthew Maurer | 0e714da | 2021-11-24 22:03:25 +0000 | [diff] [blame] | 52 | "-C force-unwind-tables=yes", |
Matthew Maurer | 20768b8 | 2021-02-01 14:56:21 -0800 | [diff] [blame] | 53 | // Use v0 mangling to distinguish from C++ symbols |
Charisee | 5ddec43 | 2022-03-01 03:02:51 +0000 | [diff] [blame] | 54 | "-C symbol-mangling-version=v0", |
Martin Geisler | d352e3b | 2022-04-29 15:21:15 +0200 | [diff] [blame] | 55 | "--color always", |
Chris Wailes | 5f78840 | 2023-03-02 16:06:01 -0800 | [diff] [blame] | 56 | "-Zdylib-lto", |
Ivan Lozano | 5467a39 | 2023-08-23 14:20:25 -0400 | [diff] [blame] | 57 | "-Z link-native-libraries=no", |
Joel Galenson | 724286c | 2019-09-30 13:01:37 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Jeff Vander Stoep | bf7a902 | 2021-01-26 14:35:38 +0100 | [diff] [blame] | 60 | deviceGlobalRustFlags = []string{ |
| 61 | "-C panic=abort", |
Yi Kong | 203e6f4 | 2021-12-07 02:36:52 +0800 | [diff] [blame] | 62 | // Generate additional debug info for AutoFDO |
| 63 | "-Z debug-info-for-profiling", |
Jeff Vander Stoep | bf7a902 | 2021-01-26 14:35:38 +0100 | [diff] [blame] | 64 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 65 | |
| 66 | deviceGlobalLinkFlags = []string{ |
Ivan Lozano | 6d45a98 | 2020-09-09 09:08:44 -0400 | [diff] [blame] | 67 | // Prepend the lld flags from cc_config so we stay in sync with cc |
| 68 | "${cc_config.DeviceGlobalLldflags}", |
| 69 | |
| 70 | // Override cc's --no-undefined-version to allow rustc's generated alloc functions |
| 71 | "-Wl,--undefined-version", |
| 72 | |
Thiébaud Weksteen | f1ff54a | 2021-03-22 14:24:54 +0100 | [diff] [blame] | 73 | "-Wl,-Bdynamic", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 74 | "-nostdlib", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 75 | "-Wl,--pack-dyn-relocs=android+relr", |
Thiébaud Weksteen | 19e1c6c | 2020-08-19 15:01:44 +0200 | [diff] [blame] | 76 | "-Wl,--use-android-relr-tags", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 77 | "-Wl,--no-undefined", |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 78 | "-B${cc_config.ClangBin}", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 79 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 80 | ) |
| 81 | |
| 82 | func init() { |
| 83 | pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase) |
Sam Delmerico | f2b1606 | 2023-09-25 12:13:17 +0000 | [diff] [blame^] | 84 | pctx.VariableConfigMethod("HostPrebuiltTag", HostPrebuiltTag) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 85 | |
| 86 | pctx.VariableFunc("RustBase", func(ctx android.PackageVarContext) string { |
| 87 | if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" { |
| 88 | return override |
| 89 | } |
| 90 | return "${RustDefaultBase}" |
| 91 | }) |
| 92 | |
Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame] | 93 | pctx.VariableFunc("RustVersion", getRustVersionPctx) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 94 | |
| 95 | pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}") |
| 96 | pctx.StaticVariable("RustBin", "${RustPath}/bin") |
| 97 | |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 98 | pctx.ImportAs("cc_config", "android/soong/cc/config") |
| 99 | pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++") |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 100 | |
| 101 | pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " ")) |
| 102 | |
Vinh Tran | 80f6b21 | 2023-08-23 13:49:13 -0400 | [diff] [blame] | 103 | exportedVars.ExportStringStaticVariable("RUST_DEFAULT_VERSION", RustDefaultVersion) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 104 | } |
Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame] | 105 | |
Sam Delmerico | f2b1606 | 2023-09-25 12:13:17 +0000 | [diff] [blame^] | 106 | func HostPrebuiltTag(config android.Config) string { |
| 107 | if config.UseHostMusl() { |
| 108 | return "linux-musl-x86" |
| 109 | } else { |
| 110 | return config.PrebuiltOS() |
| 111 | } |
| 112 | } |
| 113 | |
Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame] | 114 | func getRustVersionPctx(ctx android.PackageVarContext) string { |
| 115 | return GetRustVersion(ctx) |
| 116 | } |
| 117 | |
| 118 | func GetRustVersion(ctx android.PathContext) string { |
| 119 | if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" { |
| 120 | return override |
| 121 | } |
| 122 | return RustDefaultVersion |
| 123 | } |
Vinh Tran | 80f6b21 | 2023-08-23 13:49:13 -0400 | [diff] [blame] | 124 | |
| 125 | // BazelRustToolchainVars returns a string with |
| 126 | func BazelRustToolchainVars(config android.Config) string { |
| 127 | return android.BazelToolchainVars(config, exportedVars) |
| 128 | } |