blob: 907316fab59bbb94de723267eee0fa8b6a7363da [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 (
Matthew Maurer5524b5b2024-11-01 18:38:58 +000018 "fmt"
Ivan Lozanof1c84332019-09-20 11:00:37 -070019 "strings"
20
Ivan Lozanoffee3342019-08-27 12:03:00 -070021 "android/soong/android"
22 _ "android/soong/cc/config"
23)
24
Ivan Lozanoffee3342019-08-27 12:03:00 -070025var (
Cole Faust8982b1c2024-04-08 16:54:45 -070026 pctx = android.NewPackageContext("android/soong/rust/config")
Vinh Tran80f6b212023-08-23 13:49:13 -040027
Chris Wailesdbad1182025-01-30 11:49:10 -080028 RustDefaultVersion = "1.83.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{
Matthew Maurerf67d2112025-01-07 01:45:14 +000045 // Allow `--extern force:foo` for dylib support
46 "-Z unstable-options",
Ivan Lozano464f3b32023-06-22 18:03:16 +000047 "-Z stack-protector=strong",
Chris Wailesbc621932021-12-09 13:56:32 -080048 "-Z remap-cwd-prefix=.",
Peter Collingbourneb143cd92020-12-30 21:14:37 -080049 "-C debuginfo=2",
Ivan Lozano31b095d2019-11-20 10:14:33 -080050 "-C opt-level=3",
51 "-C relocation-model=pic",
Matthew Maurer378bf732021-03-30 11:49:08 -070052 "-C overflow-checks=on",
Matthew Maurer0e714da2021-11-24 22:03:25 +000053 "-C force-unwind-tables=yes",
Matthew Maurer20768b82021-02-01 14:56:21 -080054 // Use v0 mangling to distinguish from C++ symbols
Charisee5ddec432022-03-01 03:02:51 +000055 "-C symbol-mangling-version=v0",
Vinh Tran50de8be2023-09-21 15:28:53 -040056 "--color=always",
Chris Wailes7c1b29c2023-10-12 10:56:34 -070057 "-Z dylib-lto",
Ivan Lozano5467a392023-08-23 14:20:25 -040058 "-Z link-native-libraries=no",
Ivan Lozano07fd7e32024-04-30 11:22:26 -040059
60 // cfg flag to indicate that we are building in AOSP with Soong
61 "--cfg soong",
Joel Galenson724286c2019-09-30 13:01:37 -070062 }
63
Vinh Tran50de8be2023-09-21 15:28:53 -040064 LinuxHostGlobalLinkFlags = []string{
65 "-lc",
66 "-lrt",
67 "-ldl",
68 "-lpthread",
69 "-lm",
70 "-lgcc_s",
Eric Rahm25acde22023-10-20 15:56:30 +000071 "-Wl,--compress-debug-sections=zstd",
Vinh Tran50de8be2023-09-21 15:28:53 -040072 }
73
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +010074 deviceGlobalRustFlags = []string{
75 "-C panic=abort",
Yi Kong203e6f42021-12-07 02:36:52 +080076 // Generate additional debug info for AutoFDO
77 "-Z debug-info-for-profiling",
Matthew Maurer15675d52023-11-30 23:12:55 +000078 // Android has ELF TLS on platform
79 "-Z tls-model=global-dynamic",
Jeff Vander Stoepbf7a9022021-01-26 14:35:38 +010080 }
Ivan Lozanof1c84332019-09-20 11:00:37 -070081
82 deviceGlobalLinkFlags = []string{
Ivan Lozano6d45a982020-09-09 09:08:44 -040083 // Prepend the lld flags from cc_config so we stay in sync with cc
84 "${cc_config.DeviceGlobalLldflags}",
85
86 // Override cc's --no-undefined-version to allow rustc's generated alloc functions
87 "-Wl,--undefined-version",
88
Thiébaud Weksteenf1ff54a2021-03-22 14:24:54 +010089 "-Wl,-Bdynamic",
Ivan Lozanof1c84332019-09-20 11:00:37 -070090 "-nostdlib",
Ivan Lozanof1c84332019-09-20 11:00:37 -070091 "-Wl,--pack-dyn-relocs=android+relr",
Thiébaud Weksteen19e1c6c2020-08-19 15:01:44 +020092 "-Wl,--use-android-relr-tags",
Ivan Lozanof1c84332019-09-20 11:00:37 -070093 "-Wl,--no-undefined",
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +020094 "-B${cc_config.ClangBin}",
Eric Rahm25acde22023-10-20 15:56:30 +000095 "-Wl,--compress-debug-sections=zstd",
Ivan Lozanof1c84332019-09-20 11:00:37 -070096 }
Ivan Lozanoffee3342019-08-27 12:03:00 -070097)
98
Matthew Maurer5524b5b2024-11-01 18:38:58 +000099func RustPath(ctx android.PathContext) string {
100 // I can't see any way to flatten the static variable inside Soong, so this
101 // reproduces the init logic.
102 var RustBase string = RustDefaultBase
103 if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" {
104 RustBase = override
105 }
106 return fmt.Sprintf("%s/%s/%s", RustBase, HostPrebuiltTag(ctx.Config()), GetRustVersion(ctx))
107}
108
Ivan Lozanoffee3342019-08-27 12:03:00 -0700109func init() {
110 pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase)
Sam Delmericof2b16062023-09-25 12:13:17 +0000111 pctx.VariableConfigMethod("HostPrebuiltTag", HostPrebuiltTag)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700112
113 pctx.VariableFunc("RustBase", func(ctx android.PackageVarContext) string {
114 if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" {
115 return override
116 }
117 return "${RustDefaultBase}"
118 })
119
Ivan Lozano6d14ed12022-04-12 10:29:43 -0400120 pctx.VariableFunc("RustVersion", getRustVersionPctx)
Ivan Lozanoffee3342019-08-27 12:03:00 -0700121
122 pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}")
123 pctx.StaticVariable("RustBin", "${RustPath}/bin")
124
Thiébaud Weksteen682c9d72020-08-31 10:06:16 +0200125 pctx.ImportAs("cc_config", "android/soong/cc/config")
Ivan Lozanof4589012024-11-20 22:18:11 +0000126 pctx.StaticVariable("ClangCmd", "${cc_config.ClangBin}/clang++")
Ivan Lozanof1c84332019-09-20 11:00:37 -0700127
128 pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " "))
129
Cole Faust8982b1c2024-04-08 16:54:45 -0700130 pctx.StaticVariable("RUST_DEFAULT_VERSION", RustDefaultVersion)
131 pctx.StaticVariable("GLOBAL_RUSTC_FLAGS", strings.Join(GlobalRustFlags, " "))
132 pctx.StaticVariable("LINUX_HOST_GLOBAL_LINK_FLAGS", strings.Join(LinuxHostGlobalLinkFlags, " "))
Vinh Tran83217632023-10-04 17:35:22 -0400133
Cole Faust8982b1c2024-04-08 16:54:45 -0700134 pctx.StaticVariable("DEVICE_GLOBAL_RUSTC_FLAGS", strings.Join(deviceGlobalRustFlags, " "))
135 pctx.StaticVariable("DEVICE_GLOBAL_LINK_FLAGS",
136 strings.Join(android.RemoveListFromList(deviceGlobalLinkFlags, []string{
Vinh Tran83217632023-10-04 17:35:22 -0400137 // The cc_config flags are retrieved from cc_toolchain by rust rules.
138 "${cc_config.DeviceGlobalLldflags}",
139 "-B${cc_config.ClangBin}",
Cole Faust8982b1c2024-04-08 16:54:45 -0700140 }), " "))
Ivan Lozanoffee3342019-08-27 12:03:00 -0700141}
Ivan Lozano6d14ed12022-04-12 10:29:43 -0400142
Sam Delmericof2b16062023-09-25 12:13:17 +0000143func HostPrebuiltTag(config android.Config) string {
144 if config.UseHostMusl() {
145 return "linux-musl-x86"
146 } else {
147 return config.PrebuiltOS()
148 }
149}
150
Ivan Lozano6d14ed12022-04-12 10:29:43 -0400151func getRustVersionPctx(ctx android.PackageVarContext) string {
152 return GetRustVersion(ctx)
153}
154
155func GetRustVersion(ctx android.PathContext) string {
156 if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" {
157 return override
158 }
159 return RustDefaultVersion
160}