blob: f397ce90d6c56cda2bb88f84d690701fd7c3c3bd [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
Ivan Lozanoffee3342019-08-27 12:03:00 -070024var (
Vinh Tran80f6b212023-08-23 13:49:13 -040025 pctx = android.NewPackageContext("android/soong/rust/config")
26 exportedVars = android.NewExportedVariables(pctx)
27
James Farrell732b37f2023-09-13 23:34:18 +000028 RustDefaultVersion = "1.72.0"
Ivan Lozanoffee3342019-08-27 12:03:00 -070029 RustDefaultBase = "prebuilts/rust/"
Matthew Maurerc1738b42021-12-13 18:20:53 +000030 DefaultEdition = "2021"
Ivan Lozanoffee3342019-08-27 12:03:00 -070031 Stdlibs = []string{
Ivan Lozanoffee3342019-08-27 12:03:00 -070032 "libstd",
Ivan Lozanoffee3342019-08-27 12:03:00 -070033 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070034
Thiébaud Weksteen71512f32020-11-03 15:17:51 +010035 // 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 Galenson724286c2019-09-30 13:01:37 -070044 GlobalRustFlags = []string{
Ivan Lozano464f3b32023-06-22 18:03:16 +000045 "-Z stack-protector=strong",
Chris Wailesbc621932021-12-09 13:56:32 -080046 "-Z remap-cwd-prefix=.",
Ivan Lozano31b095d2019-11-20 10:14:33 -080047 "-C codegen-units=1",
Peter Collingbourneb143cd92020-12-30 21:14:37 -080048 "-C debuginfo=2",
Ivan Lozano31b095d2019-11-20 10:14:33 -080049 "-C opt-level=3",
50 "-C relocation-model=pic",
Matthew Maurer378bf732021-03-30 11:49:08 -070051 "-C overflow-checks=on",
Matthew Maurer0e714da2021-11-24 22:03:25 +000052 "-C force-unwind-tables=yes",
Matthew Maurer20768b82021-02-01 14:56:21 -080053 // Use v0 mangling to distinguish from C++ symbols
Charisee5ddec432022-03-01 03:02:51 +000054 "-C symbol-mangling-version=v0",
Martin Geislerd352e3b2022-04-29 15:21:15 +020055 "--color always",
Chris Wailes5f788402023-03-02 16:06:01 -080056 "-Zdylib-lto",
Ivan Lozano5467a392023-08-23 14:20:25 -040057 "-Z link-native-libraries=no",
Joel Galenson724286c2019-09-30 13:01:37 -070058 }
59
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +010060 deviceGlobalRustFlags = []string{
61 "-C panic=abort",
Yi Kong203e6f42021-12-07 02:36:52 +080062 // Generate additional debug info for AutoFDO
63 "-Z debug-info-for-profiling",
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +010064 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070065
66 deviceGlobalLinkFlags = []string{
Ivan Lozano6d45a982020-09-09 09:08:44 -040067 // 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 Weksteenf1ff54a2021-03-22 14:24:54 +010073 "-Wl,-Bdynamic",
Ivan Lozanof1c84332019-09-20 11:00:37 -070074 "-nostdlib",
Ivan Lozanof1c84332019-09-20 11:00:37 -070075 "-Wl,--pack-dyn-relocs=android+relr",
Thiébaud Weksteen19e1c6c2020-08-19 15:01:44 +020076 "-Wl,--use-android-relr-tags",
Ivan Lozanof1c84332019-09-20 11:00:37 -070077 "-Wl,--no-undefined",
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020078 "-B${cc_config.ClangBin}",
Ivan Lozanof1c84332019-09-20 11:00:37 -070079 }
Ivan Lozanoffee3342019-08-27 12:03:00 -070080)
81
82func init() {
83 pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase)
Sam Delmericof2b16062023-09-25 12:13:17 +000084 pctx.VariableConfigMethod("HostPrebuiltTag", HostPrebuiltTag)
Ivan Lozanoffee3342019-08-27 12:03:00 -070085
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 Lozano6d14ed12022-04-12 10:29:43 -040093 pctx.VariableFunc("RustVersion", getRustVersionPctx)
Ivan Lozanoffee3342019-08-27 12:03:00 -070094
95 pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}")
96 pctx.StaticVariable("RustBin", "${RustPath}/bin")
97
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020098 pctx.ImportAs("cc_config", "android/soong/cc/config")
99 pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++")
Ivan Lozanof1c84332019-09-20 11:00:37 -0700100
101 pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " "))
102
Vinh Tran80f6b212023-08-23 13:49:13 -0400103 exportedVars.ExportStringStaticVariable("RUST_DEFAULT_VERSION", RustDefaultVersion)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700104}
Ivan Lozano6d14ed12022-04-12 10:29:43 -0400105
Sam Delmericof2b16062023-09-25 12:13:17 +0000106func HostPrebuiltTag(config android.Config) string {
107 if config.UseHostMusl() {
108 return "linux-musl-x86"
109 } else {
110 return config.PrebuiltOS()
111 }
112}
113
Ivan Lozano6d14ed12022-04-12 10:29:43 -0400114func getRustVersionPctx(ctx android.PackageVarContext) string {
115 return GetRustVersion(ctx)
116}
117
118func GetRustVersion(ctx android.PathContext) string {
119 if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" {
120 return override
121 }
122 return RustDefaultVersion
123}
Vinh Tran80f6b212023-08-23 13:49:13 -0400124
125// BazelRustToolchainVars returns a string with
126func BazelRustToolchainVars(config android.Config) string {
127 return android.BazelToolchainVars(config, exportedVars)
128}