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.51.0"
28	RustDefaultBase    = "prebuilts/rust/"
29	DefaultEdition     = "2018"
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		"--remap-path-prefix $$(pwd)=",
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		// Use v0 mangling to distinguish from C++ symbols
51		"-Z symbol-mangling-version=v0",
52	}
53
54	deviceGlobalRustFlags = []string{
55		"-C panic=abort",
56		"-Z link-native-libraries=no",
57	}
58
59	deviceGlobalLinkFlags = []string{
60		// Prepend the lld flags from cc_config so we stay in sync with cc
61		"${cc_config.DeviceGlobalLldflags}",
62
63		// Override cc's --no-undefined-version to allow rustc's generated alloc functions
64		"-Wl,--undefined-version",
65
66		"-Wl,-Bdynamic",
67		"-nostdlib",
68		"-Wl,--pack-dyn-relocs=android+relr",
69		"-Wl,--use-android-relr-tags",
70		"-Wl,--no-undefined",
71		"-B${cc_config.ClangBin}",
72	}
73)
74
75func init() {
76	pctx.SourcePathVariable("RustDefaultBase", RustDefaultBase)
77	pctx.VariableConfigMethod("HostPrebuiltTag", android.Config.PrebuiltOS)
78
79	pctx.VariableFunc("RustBase", func(ctx android.PackageVarContext) string {
80		if override := ctx.Config().Getenv("RUST_PREBUILTS_BASE"); override != "" {
81			return override
82		}
83		return "${RustDefaultBase}"
84	})
85
86	pctx.VariableFunc("RustVersion", func(ctx android.PackageVarContext) string {
87		if override := ctx.Config().Getenv("RUST_PREBUILTS_VERSION"); override != "" {
88			return override
89		}
90		return RustDefaultVersion
91	})
92
93	pctx.StaticVariable("RustPath", "${RustBase}/${HostPrebuiltTag}/${RustVersion}")
94	pctx.StaticVariable("RustBin", "${RustPath}/bin")
95
96	pctx.ImportAs("cc_config", "android/soong/cc/config")
97	pctx.StaticVariable("RustLinker", "${cc_config.ClangBin}/clang++")
98	pctx.StaticVariable("RustLinkerArgs", "")
99
100	pctx.StaticVariable("DeviceGlobalLinkFlags", strings.Join(deviceGlobalLinkFlags, " "))
101
102}
103