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 ( |
Matthew Maurer | 5524b5b | 2024-11-01 18:38:58 +0000 | [diff] [blame] | 18 | "fmt" |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 19 | "strings" |
| 20 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 21 | "android/soong/android" |
| 22 | _ "android/soong/cc/config" |
| 23 | ) |
| 24 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 25 | var ( |
Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 26 | pctx = android.NewPackageContext("android/soong/rust/config") |
Vinh Tran | 80f6b21 | 2023-08-23 13:49:13 -0400 | [diff] [blame] | 27 | |
Charisee | 037e129 | 2024-09-10 23:57:30 +0000 | [diff] [blame] | 28 | RustDefaultVersion = "1.81.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=.", |
Peter Collingbourne | b143cd9 | 2020-12-30 21:14:37 -0800 | [diff] [blame] | 47 | "-C debuginfo=2", |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 48 | "-C opt-level=3", |
| 49 | "-C relocation-model=pic", |
Matthew Maurer | 378bf73 | 2021-03-30 11:49:08 -0700 | [diff] [blame] | 50 | "-C overflow-checks=on", |
Matthew Maurer | 0e714da | 2021-11-24 22:03:25 +0000 | [diff] [blame] | 51 | "-C force-unwind-tables=yes", |
Matthew Maurer | 20768b8 | 2021-02-01 14:56:21 -0800 | [diff] [blame] | 52 | // Use v0 mangling to distinguish from C++ symbols |
Charisee | 5ddec43 | 2022-03-01 03:02:51 +0000 | [diff] [blame] | 53 | "-C symbol-mangling-version=v0", |
Vinh Tran | 50de8be | 2023-09-21 15:28:53 -0400 | [diff] [blame] | 54 | "--color=always", |
Chris Wailes | 7c1b29c | 2023-10-12 10:56:34 -0700 | [diff] [blame] | 55 | "-Z dylib-lto", |
Ivan Lozano | 5467a39 | 2023-08-23 14:20:25 -0400 | [diff] [blame] | 56 | "-Z link-native-libraries=no", |
Ivan Lozano | 07fd7e3 | 2024-04-30 11:22:26 -0400 | [diff] [blame] | 57 | |
| 58 | // cfg flag to indicate that we are building in AOSP with Soong |
| 59 | "--cfg soong", |
Joel Galenson | 724286c | 2019-09-30 13:01:37 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Vinh Tran | 50de8be | 2023-09-21 15:28:53 -0400 | [diff] [blame] | 62 | LinuxHostGlobalLinkFlags = []string{ |
| 63 | "-lc", |
| 64 | "-lrt", |
| 65 | "-ldl", |
| 66 | "-lpthread", |
| 67 | "-lm", |
| 68 | "-lgcc_s", |
Eric Rahm | 25acde2 | 2023-10-20 15:56:30 +0000 | [diff] [blame] | 69 | "-Wl,--compress-debug-sections=zstd", |
Vinh Tran | 50de8be | 2023-09-21 15:28:53 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Jeff Vander Stoep | bf7a902 | 2021-01-26 14:35:38 +0100 | [diff] [blame] | 72 | deviceGlobalRustFlags = []string{ |
| 73 | "-C panic=abort", |
Yi Kong | 203e6f4 | 2021-12-07 02:36:52 +0800 | [diff] [blame] | 74 | // Generate additional debug info for AutoFDO |
| 75 | "-Z debug-info-for-profiling", |
Matthew Maurer | 15675d5 | 2023-11-30 23:12:55 +0000 | [diff] [blame] | 76 | // Android has ELF TLS on platform |
| 77 | "-Z tls-model=global-dynamic", |
Jeff Vander Stoep | bf7a902 | 2021-01-26 14:35:38 +0100 | [diff] [blame] | 78 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 79 | |
| 80 | deviceGlobalLinkFlags = []string{ |
Ivan Lozano | 6d45a98 | 2020-09-09 09:08:44 -0400 | [diff] [blame] | 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 | |
Thiébaud Weksteen | f1ff54a | 2021-03-22 14:24:54 +0100 | [diff] [blame] | 87 | "-Wl,-Bdynamic", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 88 | "-nostdlib", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 89 | "-Wl,--pack-dyn-relocs=android+relr", |
Thiébaud Weksteen | 19e1c6c | 2020-08-19 15:01:44 +0200 | [diff] [blame] | 90 | "-Wl,--use-android-relr-tags", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 91 | "-Wl,--no-undefined", |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 92 | "-B${cc_config.ClangBin}", |
Eric Rahm | 25acde2 | 2023-10-20 15:56:30 +0000 | [diff] [blame] | 93 | "-Wl,--compress-debug-sections=zstd", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 94 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 95 | ) |
| 96 | |
Matthew Maurer | 5524b5b | 2024-11-01 18:38:58 +0000 | [diff] [blame] | 97 | func 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 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 107 | func init() { |
| 108 | pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase) |
Sam Delmerico | f2b1606 | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 109 | pctx.VariableConfigMethod("HostPrebuiltTag", HostPrebuiltTag) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 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 | |
Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame] | 118 | pctx.VariableFunc("RustVersion", getRustVersionPctx) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 119 | |
| 120 | pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}") |
| 121 | pctx.StaticVariable("RustBin", "${RustPath}/bin") |
| 122 | |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 123 | pctx.ImportAs("cc_config", "android/soong/cc/config") |
| 124 | pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++") |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 125 | |
| 126 | pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " ")) |
| 127 | |
Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 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, " ")) |
Vinh Tran | 8321763 | 2023-10-04 17:35:22 -0400 | [diff] [blame] | 131 | |
Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 132 | pctx.StaticVariable("DEVICE_GLOBAL_RUSTC_FLAGS", strings.Join(deviceGlobalRustFlags, " ")) |
| 133 | pctx.StaticVariable("DEVICE_GLOBAL_LINK_FLAGS", |
| 134 | strings.Join(android.RemoveListFromList(deviceGlobalLinkFlags, []string{ |
Vinh Tran | 8321763 | 2023-10-04 17:35:22 -0400 | [diff] [blame] | 135 | // The cc_config flags are retrieved from cc_toolchain by rust rules. |
| 136 | "${cc_config.DeviceGlobalLldflags}", |
| 137 | "-B${cc_config.ClangBin}", |
Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 138 | }), " ")) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 139 | } |
Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame] | 140 | |
Sam Delmerico | f2b1606 | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 141 | func HostPrebuiltTag(config android.Config) string { |
| 142 | if config.UseHostMusl() { |
| 143 | return "linux-musl-x86" |
| 144 | } else { |
| 145 | return config.PrebuiltOS() |
| 146 | } |
| 147 | } |
| 148 | |
Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame] | 149 | func getRustVersionPctx(ctx android.PackageVarContext) string { |
| 150 | return GetRustVersion(ctx) |
| 151 | } |
| 152 | |
| 153 | func GetRustVersion(ctx android.PathContext) string { |
| 154 | if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" { |
| 155 | return override |
| 156 | } |
| 157 | return RustDefaultVersion |
| 158 | } |