blob: 748bb3d2f3ec791e2ced89608acd19862f0fcc9c [file] [log] [blame]
Ivan Lozanoffee3342019-08-27 12:03:00 -07001// 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 (
Ivan Lozanof1c84332019-09-20 11:00:37 -070018 "strings"
19
Ivan Lozanoffee3342019-08-27 12:03:00 -070020 "android/soong/android"
21 _ "android/soong/cc/config"
22)
23
24var pctx = android.NewPackageContext("android/soong/rust/config")
25
26var (
Chris Wailes6b826162023-05-11 16:07:30 -070027 RustDefaultVersion = "1.69.0"
Ivan Lozanoffee3342019-08-27 12:03:00 -070028 RustDefaultBase = "prebuilts/rust/"
Matthew Maurerc1738b42021-12-13 18:20:53 +000029 DefaultEdition = "2021"
Ivan Lozanoffee3342019-08-27 12:03:00 -070030 Stdlibs = []string{
Ivan Lozanoffee3342019-08-27 12:03:00 -070031 "libstd",
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070033
Thiébaud Weksteen71512f32020-11-03 15:17:51 +010034 // 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 Galenson724286c2019-09-30 13:01:37 -070043 GlobalRustFlags = []string{
Chris Wailesbc621932021-12-09 13:56:32 -080044 "-Z remap-cwd-prefix=.",
Ivan Lozano31b095d2019-11-20 10:14:33 -080045 "-C codegen-units=1",
Peter Collingbourneb143cd92020-12-30 21:14:37 -080046 "-C debuginfo=2",
Ivan Lozano31b095d2019-11-20 10:14:33 -080047 "-C opt-level=3",
48 "-C relocation-model=pic",
Matthew Maurer378bf732021-03-30 11:49:08 -070049 "-C overflow-checks=on",
Matthew Maurer0e714da2021-11-24 22:03:25 +000050 "-C force-unwind-tables=yes",
Matthew Maurer20768b82021-02-01 14:56:21 -080051 // Use v0 mangling to distinguish from C++ symbols
Charisee5ddec432022-03-01 03:02:51 +000052 "-C symbol-mangling-version=v0",
Martin Geislerd352e3b2022-04-29 15:21:15 +020053 "--color always",
Chris Wailes83ed1872023-02-06 20:35:46 +000054 // TODO (b/267698452): Temporary workaround until the "no unstable
55 // features" policy is enforced.
56 "-A stable-features",
Chris Wailes5f788402023-03-02 16:06:01 -080057 "-Zdylib-lto",
Joel Galenson724286c2019-09-30 13:01:37 -070058 }
59
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +010060 deviceGlobalRustFlags = []string{
61 "-C panic=abort",
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +010062 "-Z link-native-libraries=no",
Yi Kong203e6f42021-12-07 02:36:52 +080063 // Generate additional debug info for AutoFDO
64 "-Z debug-info-for-profiling",
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +010065 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070066
67 deviceGlobalLinkFlags = []string{
Ivan Lozano6d45a982020-09-09 09:08:44 -040068 // Prepend the lld flags from cc_config so we stay in sync with cc
69 "${cc_config.DeviceGlobalLldflags}",
70
71 // Override cc's --no-undefined-version to allow rustc's generated alloc functions
72 "-Wl,--undefined-version",
73
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +010074 "-Wl,-Bdynamic",
Ivan Lozanof1c84332019-09-20 11:00:37 -070075 "-nostdlib",
Ivan Lozanof1c84332019-09-20 11:00:37 -070076 "-Wl,--pack-dyn-relocs=android+relr",
Thiébaud Weksteen19e1c6c2020-08-19 15:01:44 +020077 "-Wl,--use-android-relr-tags",
Ivan Lozanof1c84332019-09-20 11:00:37 -070078 "-Wl,--no-undefined",
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020079 "-B${cc_config.ClangBin}",
Ivan Lozanof1c84332019-09-20 11:00:37 -070080 }
Ivan Lozanoffee3342019-08-27 12:03:00 -070081)
82
83func init() {
84 pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase)
Colin Cross567d3422022-04-28 15:30:09 -070085 pctx.VariableConfigMethod("HostPrebuiltTag", func(config android.Config) string {
86 if config.UseHostMusl() {
87 return "linux-musl-x86"
88 } else {
89 return config.PrebuiltOS()
90 }
91 })
Ivan Lozanoffee3342019-08-27 12:03:00 -070092
93 pctx.VariableFunc("RustBase", func(ctx android.PackageVarContext) string {
94 if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" {
95 return override
96 }
97 return "${RustDefaultBase}"
98 })
99
Ivan Lozano6d14ed12022-04-12 10:29:43 -0400100 pctx.VariableFunc("RustVersion", getRustVersionPctx)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700101
102 pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}")
103 pctx.StaticVariable("RustBin", "${RustPath}/bin")
104
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +0200105 pctx.ImportAs("cc_config", "android/soong/cc/config")
106 pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++")
Peter Collingbourne4629f7c2023-04-14 17:36:51 -0700107 pctx.StaticVariable("RustLinkerArgs", "-Wl,--as-needed")
Ivan Lozanof1c84332019-09-20 11:00:37 -0700108
109 pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " "))
110
Ivan Lozanoffee3342019-08-27 12:03:00 -0700111}
Ivan Lozano6d14ed12022-04-12 10:29:43 -0400112
113func getRustVersionPctx(ctx android.PackageVarContext) string {
114 return GetRustVersion(ctx)
115}
116
117func GetRustVersion(ctx android.PathContext) string {
118 if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" {
119 return override
120 }
121 return RustDefaultVersion
122}