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
15package config
16
17import (
18	"strings"
19
20	"android/soong/android"
21	_ "android/soong/cc/config"
22)
23
24var (
25	pctx = android.NewPackageContext("android/soong/rust/config")
26
27	RustDefaultVersion = "1.78.0"
28	RustDefaultBase    = "prebuilts/rust/"
29	DefaultEdition     = "2021"
30	Stdlibs            = []string{
31		"libstd",
32	}
33
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
43	GlobalRustFlags = []string{
44		"-Z stack-protector=strong",
45		"-Z remap-cwd-prefix=.",
46		"-C debuginfo=2",
47		"-C opt-level=3",
48		"-C relocation-model=pic",
49		"-C overflow-checks=on",
50		"-C force-unwind-tables=yes",
51		// Use v0 mangling to distinguish from C++ symbols
52		"-C symbol-mangling-version=v0",
53		"--color=always",
54		"-Z dylib-lto",
55		"-Z link-native-libraries=no",
56
57		// cfg flag to indicate that we are building in AOSP with Soong
58		"--cfg soong",
59	}
60
61	LinuxHostGlobalLinkFlags = []string{
62		"-lc",
63		"-lrt",
64		"-ldl",
65		"-lpthread",
66		"-lm",
67		"-lgcc_s",
68		"-Wl,--compress-debug-sections=zstd",
69	}
70
71	deviceGlobalRustFlags = []string{
72		"-C panic=abort",
73		// Generate additional debug info for AutoFDO
74		"-Z debug-info-for-profiling",
75		// Android has ELF TLS on platform
76		"-Z tls-model=global-dynamic",
77	}
78
79	deviceGlobalLinkFlags = []string{
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
86		"-Wl,-Bdynamic",
87		"-nostdlib",
88		"-Wl,--pack-dyn-relocs=android+relr",
89		"-Wl,--use-android-relr-tags",
90		"-Wl,--no-undefined",
91		"-B${cc_config.ClangBin}",
92		"-Wl,--compress-debug-sections=zstd",
93	}
94)
95
96func init() {
97	pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase)
98	pctx.VariableConfigMethod("HostPrebuiltTag", HostPrebuiltTag)
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
107	pctx.VariableFunc("RustVersion", getRustVersionPctx)
108
109	pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}")
110	pctx.StaticVariable("RustBin", "${RustPath}/bin")
111
112	pctx.ImportAs("cc_config", "android/soong/cc/config")
113	pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++")
114
115	pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " "))
116
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, " "))
120
121	pctx.StaticVariable("DEVICE_GLOBAL_RUSTC_FLAGS", strings.Join(deviceGlobalRustFlags, " "))
122	pctx.StaticVariable("DEVICE_GLOBAL_LINK_FLAGS",
123		strings.Join(android.RemoveListFromList(deviceGlobalLinkFlags, []string{
124			// The cc_config flags are retrieved from cc_toolchain by rust rules.
125			"${cc_config.DeviceGlobalLldflags}",
126			"-B${cc_config.ClangBin}",
127		}), " "))
128}
129
130func HostPrebuiltTag(config android.Config) string {
131	if config.UseHostMusl() {
132		return "linux-musl-x86"
133	} else {
134		return config.PrebuiltOS()
135	}
136}
137
138func getRustVersionPctx(ctx android.PackageVarContext) string {
139	return GetRustVersion(ctx)
140}
141
142func GetRustVersion(ctx android.PathContext) string {
143	if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" {
144		return override
145	}
146	return RustDefaultVersion
147}
148