• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 pctx = android.NewPackageContext("android/soong/rust/config")
25
26var (
27	RustDefaultVersion = "1.68.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 remap-cwd-prefix=.",
45		"-C codegen-units=1",
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		// TODO (b/267698452): Temporary workaround until the "no unstable
55		// features" policy is enforced.
56		"-A stable-features",
57		"-Zdylib-lto",
58	}
59
60	deviceGlobalRustFlags = []string{
61		"-C panic=abort",
62		"-Z link-native-libraries=no",
63		// Generate additional debug info for AutoFDO
64		"-Z debug-info-for-profiling",
65	}
66
67	deviceGlobalLinkFlags = []string{
68		// 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
74		"-Wl,-Bdynamic",
75		"-nostdlib",
76		"-Wl,--pack-dyn-relocs=android+relr",
77		"-Wl,--use-android-relr-tags",
78		"-Wl,--no-undefined",
79		"-B${cc_config.ClangBin}",
80	}
81)
82
83func init() {
84	pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase)
85	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	})
92
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
100	pctx.VariableFunc("RustVersion", getRustVersionPctx)
101
102	pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}")
103	pctx.StaticVariable("RustBin", "${RustPath}/bin")
104
105	pctx.ImportAs("cc_config", "android/soong/cc/config")
106	pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++")
107	pctx.StaticVariable("RustLinkerArgs", "-Wl,--as-needed")
108
109	pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " "))
110
111}
112
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}
123