| 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 ( | 
| Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 25 | pctx = android.NewPackageContext("android/soong/rust/config") | 
| Vinh Tran | 80f6b21 | 2023-08-23 13:49:13 -0400 | [diff] [blame] | 26 |  | 
| Charisee | 037e129 | 2024-09-10 23:57:30 +0000 | [diff] [blame] | 27 | RustDefaultVersion = "1.81.0" | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 28 | RustDefaultBase    = "prebuilts/rust/" | 
| Matthew Maurer | c1738b4 | 2021-12-13 18:20:53 +0000 | [diff] [blame] | 29 | DefaultEdition     = "2021" | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 30 | Stdlibs            = []string{ | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 31 | "libstd", | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 32 | } | 
| Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 33 |  | 
| Thiébaud Weksteen | 71512f3 | 2020-11-03 15:17:51 +0100 | [diff] [blame] | 34 | // Mapping between Soong internal arch types and std::env constants. | 
|  | 35 | // Required as Rust uses aarch64 when Soong uses arm64. | 
|  | 36 | StdEnvArch = map[android.ArchType]string{ | 
|  | 37 | android.Arm:    "arm", | 
|  | 38 | android.Arm64:  "aarch64", | 
|  | 39 | android.X86:    "x86", | 
|  | 40 | android.X86_64: "x86_64", | 
|  | 41 | } | 
|  | 42 |  | 
| Joel Galenson | 724286c | 2019-09-30 13:01:37 -0700 | [diff] [blame] | 43 | GlobalRustFlags = []string{ | 
| Ivan Lozano | 464f3b3 | 2023-06-22 18:03:16 +0000 | [diff] [blame] | 44 | "-Z stack-protector=strong", | 
| Chris Wailes | bc62193 | 2021-12-09 13:56:32 -0800 | [diff] [blame] | 45 | "-Z remap-cwd-prefix=.", | 
| Peter Collingbourne | b143cd9 | 2020-12-30 21:14:37 -0800 | [diff] [blame] | 46 | "-C debuginfo=2", | 
| Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 47 | "-C opt-level=3", | 
|  | 48 | "-C relocation-model=pic", | 
| Matthew Maurer | 378bf73 | 2021-03-30 11:49:08 -0700 | [diff] [blame] | 49 | "-C overflow-checks=on", | 
| Matthew Maurer | 0e714da | 2021-11-24 22:03:25 +0000 | [diff] [blame] | 50 | "-C force-unwind-tables=yes", | 
| Matthew Maurer | 20768b8 | 2021-02-01 14:56:21 -0800 | [diff] [blame] | 51 | // Use v0 mangling to distinguish from C++ symbols | 
| Charisee | 5ddec43 | 2022-03-01 03:02:51 +0000 | [diff] [blame] | 52 | "-C symbol-mangling-version=v0", | 
| Vinh Tran | 50de8be | 2023-09-21 15:28:53 -0400 | [diff] [blame] | 53 | "--color=always", | 
| Chris Wailes | 7c1b29c | 2023-10-12 10:56:34 -0700 | [diff] [blame] | 54 | "-Z dylib-lto", | 
| Ivan Lozano | 5467a39 | 2023-08-23 14:20:25 -0400 | [diff] [blame] | 55 | "-Z link-native-libraries=no", | 
| Ivan Lozano | 07fd7e3 | 2024-04-30 11:22:26 -0400 | [diff] [blame] | 56 |  | 
|  | 57 | // cfg flag to indicate that we are building in AOSP with Soong | 
|  | 58 | "--cfg soong", | 
| Joel Galenson | 724286c | 2019-09-30 13:01:37 -0700 | [diff] [blame] | 59 | } | 
|  | 60 |  | 
| Vinh Tran | 50de8be | 2023-09-21 15:28:53 -0400 | [diff] [blame] | 61 | LinuxHostGlobalLinkFlags = []string{ | 
|  | 62 | "-lc", | 
|  | 63 | "-lrt", | 
|  | 64 | "-ldl", | 
|  | 65 | "-lpthread", | 
|  | 66 | "-lm", | 
|  | 67 | "-lgcc_s", | 
| Eric Rahm | 25acde2 | 2023-10-20 15:56:30 +0000 | [diff] [blame] | 68 | "-Wl,--compress-debug-sections=zstd", | 
| Vinh Tran | 50de8be | 2023-09-21 15:28:53 -0400 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
| Jeff Vander Stoep | bf7a902 | 2021-01-26 14:35:38 +0100 | [diff] [blame] | 71 | deviceGlobalRustFlags = []string{ | 
|  | 72 | "-C panic=abort", | 
| Yi Kong | 203e6f4 | 2021-12-07 02:36:52 +0800 | [diff] [blame] | 73 | // Generate additional debug info for AutoFDO | 
|  | 74 | "-Z debug-info-for-profiling", | 
| Matthew Maurer | 15675d5 | 2023-11-30 23:12:55 +0000 | [diff] [blame] | 75 | // Android has ELF TLS on platform | 
|  | 76 | "-Z tls-model=global-dynamic", | 
| Jeff Vander Stoep | bf7a902 | 2021-01-26 14:35:38 +0100 | [diff] [blame] | 77 | } | 
| Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 78 |  | 
|  | 79 | deviceGlobalLinkFlags = []string{ | 
| Ivan Lozano | 6d45a98 | 2020-09-09 09:08:44 -0400 | [diff] [blame] | 80 | // Prepend the lld flags from cc_config so we stay in sync with cc | 
|  | 81 | "${cc_config.DeviceGlobalLldflags}", | 
|  | 82 |  | 
|  | 83 | // Override cc's --no-undefined-version to allow rustc's generated alloc functions | 
|  | 84 | "-Wl,--undefined-version", | 
|  | 85 |  | 
| Thiébaud Weksteen | f1ff54a | 2021-03-22 14:24:54 +0100 | [diff] [blame] | 86 | "-Wl,-Bdynamic", | 
| Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 87 | "-nostdlib", | 
| Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 88 | "-Wl,--pack-dyn-relocs=android+relr", | 
| Thiébaud Weksteen | 19e1c6c | 2020-08-19 15:01:44 +0200 | [diff] [blame] | 89 | "-Wl,--use-android-relr-tags", | 
| Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 90 | "-Wl,--no-undefined", | 
| Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 91 | "-B${cc_config.ClangBin}", | 
| Eric Rahm | 25acde2 | 2023-10-20 15:56:30 +0000 | [diff] [blame] | 92 | "-Wl,--compress-debug-sections=zstd", | 
| Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 93 | } | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 94 | ) | 
|  | 95 |  | 
|  | 96 | func init() { | 
|  | 97 | pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase) | 
| Sam Delmerico | f2b1606 | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 98 | pctx.VariableConfigMethod("HostPrebuiltTag", HostPrebuiltTag) | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 99 |  | 
|  | 100 | pctx.VariableFunc("RustBase", func(ctx android.PackageVarContext) string { | 
|  | 101 | if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" { | 
|  | 102 | return override | 
|  | 103 | } | 
|  | 104 | return "${RustDefaultBase}" | 
|  | 105 | }) | 
|  | 106 |  | 
| Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame] | 107 | pctx.VariableFunc("RustVersion", getRustVersionPctx) | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 108 |  | 
|  | 109 | pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}") | 
|  | 110 | pctx.StaticVariable("RustBin", "${RustPath}/bin") | 
|  | 111 |  | 
| Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 112 | pctx.ImportAs("cc_config", "android/soong/cc/config") | 
|  | 113 | pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++") | 
| Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 114 |  | 
|  | 115 | pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " ")) | 
|  | 116 |  | 
| Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 117 | pctx.StaticVariable("RUST_DEFAULT_VERSION", RustDefaultVersion) | 
|  | 118 | pctx.StaticVariable("GLOBAL_RUSTC_FLAGS", strings.Join(GlobalRustFlags, " ")) | 
|  | 119 | pctx.StaticVariable("LINUX_HOST_GLOBAL_LINK_FLAGS", strings.Join(LinuxHostGlobalLinkFlags, " ")) | 
| Vinh Tran | 8321763 | 2023-10-04 17:35:22 -0400 | [diff] [blame] | 120 |  | 
| Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 121 | pctx.StaticVariable("DEVICE_GLOBAL_RUSTC_FLAGS", strings.Join(deviceGlobalRustFlags, " ")) | 
|  | 122 | pctx.StaticVariable("DEVICE_GLOBAL_LINK_FLAGS", | 
|  | 123 | strings.Join(android.RemoveListFromList(deviceGlobalLinkFlags, []string{ | 
| Vinh Tran | 8321763 | 2023-10-04 17:35:22 -0400 | [diff] [blame] | 124 | // The cc_config flags are retrieved from cc_toolchain by rust rules. | 
|  | 125 | "${cc_config.DeviceGlobalLldflags}", | 
|  | 126 | "-B${cc_config.ClangBin}", | 
| Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 127 | }), " ")) | 
| Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 128 | } | 
| Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame] | 129 |  | 
| Sam Delmerico | f2b1606 | 2023-09-25 12:13:17 +0000 | [diff] [blame] | 130 | func HostPrebuiltTag(config android.Config) string { | 
|  | 131 | if config.UseHostMusl() { | 
|  | 132 | return "linux-musl-x86" | 
|  | 133 | } else { | 
|  | 134 | return config.PrebuiltOS() | 
|  | 135 | } | 
|  | 136 | } | 
|  | 137 |  | 
| Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame] | 138 | func getRustVersionPctx(ctx android.PackageVarContext) string { | 
|  | 139 | return GetRustVersion(ctx) | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | func GetRustVersion(ctx android.PathContext) string { | 
|  | 143 | if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" { | 
|  | 144 | return override | 
|  | 145 | } | 
|  | 146 | return RustDefaultVersion | 
|  | 147 | } |