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)
22
23var (
24	LinuxRustFlags     = []string{}
25	LinuxRustLinkFlags = []string{
26		"-B${cc_config.ClangBin}",
27		"-fuse-ld=lld",
28		"-Wl,--undefined-version",
29	}
30	linuxX86Rustflags   = []string{}
31	linuxX86Linkflags   = []string{}
32	linuxX8664Rustflags = []string{}
33	linuxX8664Linkflags = []string{}
34)
35
36func init() {
37	registerToolchainFactory(android.Linux, android.X86_64, linuxX8664ToolchainFactory)
38	registerToolchainFactory(android.Linux, android.X86, linuxX86ToolchainFactory)
39
40	pctx.StaticVariable("LinuxToolchainRustFlags", strings.Join(LinuxRustFlags, " "))
41	pctx.StaticVariable("LinuxToolchainLinkFlags", strings.Join(LinuxRustLinkFlags, " "))
42	pctx.StaticVariable("LinuxToolchainX86RustFlags", strings.Join(linuxX86Rustflags, " "))
43	pctx.StaticVariable("LinuxToolchainX86LinkFlags", strings.Join(linuxX86Linkflags, " "))
44	pctx.StaticVariable("LinuxToolchainX8664RustFlags", strings.Join(linuxX8664Rustflags, " "))
45	pctx.StaticVariable("LinuxToolchainX8664LinkFlags", strings.Join(linuxX8664Linkflags, " "))
46
47}
48
49type toolchainLinux struct {
50	toolchainRustFlags string
51	toolchainLinkFlags string
52}
53
54type toolchainLinuxX86 struct {
55	toolchain32Bit
56	toolchainLinux
57}
58
59type toolchainLinuxX8664 struct {
60	toolchain64Bit
61	toolchainLinux
62}
63
64func (toolchainLinuxX8664) Supported() bool {
65	return true
66}
67
68func (toolchainLinuxX8664) Bionic() bool {
69	return false
70}
71
72func (t *toolchainLinuxX8664) Name() string {
73	return "x86_64"
74}
75
76func (t *toolchainLinuxX8664) RustTriple() string {
77	return "x86_64-unknown-linux-gnu"
78}
79
80func (t *toolchainLinuxX8664) ToolchainLinkFlags() string {
81	// Prepend the lld flags from cc_config so we stay in sync with cc
82	return "${cc_config.LinuxClangLldflags} ${cc_config.LinuxX8664ClangLldflags} " +
83		"${config.LinuxToolchainLinkFlags} ${config.LinuxToolchainX8664LinkFlags}"
84}
85
86func (t *toolchainLinuxX8664) ToolchainRustFlags() string {
87	return "${config.LinuxToolchainRustFlags} ${config.LinuxToolchainX8664RustFlags}"
88}
89
90func linuxX8664ToolchainFactory(arch android.Arch) Toolchain {
91	return toolchainLinuxX8664Singleton
92}
93
94func (toolchainLinuxX86) Supported() bool {
95	return true
96}
97
98func (toolchainLinuxX86) Bionic() bool {
99	return false
100}
101
102func (t *toolchainLinuxX86) Name() string {
103	return "x86"
104}
105
106func (toolchainLinuxX86) LibclangRuntimeLibraryArch() string {
107	return "i386"
108}
109
110func (toolchainLinuxX8664) LibclangRuntimeLibraryArch() string {
111	return "x86_64"
112}
113
114func (t *toolchainLinuxX86) RustTriple() string {
115	return "i686-unknown-linux-gnu"
116}
117
118func (t *toolchainLinuxX86) ToolchainLinkFlags() string {
119	// Prepend the lld flags from cc_config so we stay in sync with cc
120	return "${cc_config.LinuxClangLldflags} ${cc_config.LinuxX86ClangLldflags} " +
121		"${config.LinuxToolchainLinkFlags} ${config.LinuxToolchainX86LinkFlags}"
122}
123
124func (t *toolchainLinuxX86) ToolchainRustFlags() string {
125	return "${config.LinuxToolchainRustFlags} ${config.LinuxToolchainX86RustFlags}"
126}
127
128func linuxX86ToolchainFactory(arch android.Arch) Toolchain {
129	return toolchainLinuxX86Singleton
130}
131
132var toolchainLinuxX8664Singleton Toolchain = &toolchainLinuxX8664{}
133var toolchainLinuxX86Singleton Toolchain = &toolchainLinuxX86{}
134