• 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.59.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	}
54
55	deviceGlobalRustFlags = []string{
56		"-C panic=abort",
57		"-Z link-native-libraries=no",
58		// Generate additional debug info for AutoFDO
59		"-Z debug-info-for-profiling",
60	}
61
62	deviceGlobalLinkFlags = []string{
63		// Prepend the lld flags from cc_config so we stay in sync with cc
64		"${cc_config.DeviceGlobalLldflags}",
65
66		// Override cc's --no-undefined-version to allow rustc's generated alloc functions
67		"-Wl,--undefined-version",
68
69		"-Wl,-Bdynamic",
70		"-nostdlib",
71		"-Wl,--pack-dyn-relocs=android+relr",
72		"-Wl,--use-android-relr-tags",
73		"-Wl,--no-undefined",
74		"-B${cc_config.ClangBin}",
75	}
76)
77
78func init() {
79	pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase)
80	pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
81
82	pctx.VariableFunc("RustBase", func(ctx android.PackageVarContext) string {
83		if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" {
84			return override
85		}
86		return "${RustDefaultBase}"
87	})
88
89	pctx.VariableFunc("RustVersion", getRustVersionPctx)
90
91	pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}")
92	pctx.StaticVariable("RustBin", "${RustPath}/bin")
93
94	pctx.ImportAs("cc_config", "android/soong/cc/config")
95	pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++")
96	pctx.StaticVariable("RustLinkerArgs", "")
97
98	pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " "))
99
100}
101
102func getRustVersionPctx(ctx android.PackageVarContext) string {
103	return GetRustVersion(ctx)
104}
105
106func GetRustVersion(ctx android.PathContext) string {
107	if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" {
108		return override
109	}
110	return RustDefaultVersion
111}
112