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 | |
| 24 | var pctx = android.NewPackageContext("android/soong/rust/config") |
| 25 | |
| 26 | var ( |
Charisee | 5ddec43 | 2022-03-01 03:02:51 +0000 | [diff] [blame] | 27 | RustDefaultVersion = "1.59.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{ |
Chris Wailes | bc62193 | 2021-12-09 13:56:32 -0800 | [diff] [blame] | 44 | "-Z remap-cwd-prefix=.", |
Ivan Lozano | 31b095d | 2019-11-20 10:14:33 -0800 | [diff] [blame] | 45 | "-C codegen-units=1", |
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", |
Joel Galenson | 724286c | 2019-09-30 13:01:37 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Jeff Vander Stoep | bf7a902 | 2021-01-26 14:35:38 +0100 | [diff] [blame] | 55 | deviceGlobalRustFlags = []string{ |
| 56 | "-C panic=abort", |
Thiébaud Weksteen | f1ff54a | 2021-03-22 14:24:54 +0100 | [diff] [blame] | 57 | "-Z link-native-libraries=no", |
Yi Kong | 203e6f4 | 2021-12-07 02:36:52 +0800 | [diff] [blame] | 58 | // Generate additional debug info for AutoFDO |
| 59 | "-Z debug-info-for-profiling", |
Jeff Vander Stoep | bf7a902 | 2021-01-26 14:35:38 +0100 | [diff] [blame] | 60 | } |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 61 | |
| 62 | deviceGlobalLinkFlags = []string{ |
Ivan Lozano | 6d45a98 | 2020-09-09 09:08:44 -0400 | [diff] [blame] | 63 | // Prepend the lld flags from cc_config so we stay in sync with cc |
| 64 | "${cc_config.DeviceGlobalLldflags}", |
| 65 | |
| 66 | // Override cc's --no-undefined-version to allow rustc's generated alloc functions |
| 67 | "-Wl,--undefined-version", |
| 68 | |
Thiébaud Weksteen | f1ff54a | 2021-03-22 14:24:54 +0100 | [diff] [blame] | 69 | "-Wl,-Bdynamic", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 70 | "-nostdlib", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 71 | "-Wl,--pack-dyn-relocs=android+relr", |
Thiébaud Weksteen | 19e1c6c | 2020-08-19 15:01:44 +0200 | [diff] [blame] | 72 | "-Wl,--use-android-relr-tags", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 73 | "-Wl,--no-undefined", |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 74 | "-B${cc_config.ClangBin}", |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 75 | } |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 76 | ) |
| 77 | |
| 78 | func init() { |
| 79 | pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase) |
| 80 | pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS) |
| 81 | |
| 82 | pctx.VariableFunc("RustBase", func(ctx android.PackageVarContext) string { |
| 83 | if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" { |
| 84 | return override |
| 85 | } |
| 86 | return "${RustDefaultBase}" |
| 87 | }) |
| 88 | |
Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame^] | 89 | pctx.VariableFunc("RustVersion", getRustVersionPctx) |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 90 | |
| 91 | pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}") |
| 92 | pctx.StaticVariable("RustBin", "${RustPath}/bin") |
| 93 | |
Thiébaud Weksteen | 682c9d7 | 2020-08-31 10:06:16 +0200 | [diff] [blame] | 94 | pctx.ImportAs("cc_config", "android/soong/cc/config") |
| 95 | pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++") |
Jeff Vander Stoep | 6e97a7b | 2020-07-15 21:34:45 +0200 | [diff] [blame] | 96 | pctx.StaticVariable("RustLinkerArgs", "") |
Ivan Lozano | f1c8433 | 2019-09-20 11:00:37 -0700 | [diff] [blame] | 97 | |
| 98 | pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " ")) |
| 99 | |
Ivan Lozano | ffee334 | 2019-08-27 12:03:00 -0700 | [diff] [blame] | 100 | } |
Ivan Lozano | 6d14ed1 | 2022-04-12 10:29:43 -0400 | [diff] [blame^] | 101 | |
| 102 | func getRustVersionPctx(ctx android.PackageVarContext) string { |
| 103 | return GetRustVersion(ctx) |
| 104 | } |
| 105 | |
| 106 | func GetRustVersion(ctx android.PathContext) string { |
| 107 | if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" { |
| 108 | return override |
| 109 | } |
| 110 | return RustDefaultVersion |
| 111 | } |