1// Copyright 2015 Google Inc. All rights reserved.
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	"fmt"
19	"strings"
20
21	"android/soong/android"
22)
23
24var (
25	arm64Cflags = []string{
26		// Help catch common 32/64-bit errors.
27		"-Werror=implicit-function-declaration",
28	}
29
30	arm64ArchVariantCflags = map[string][]string{
31		"armv8-a": []string{
32			"-march=armv8-a",
33		},
34		"armv8-a-branchprot": []string{
35			"-march=armv8-a",
36			"-mbranch-protection=standard",
37		},
38		"armv8-2a": []string{
39			"-march=armv8.2-a",
40		},
41		"armv8-2a-dotprod": []string{
42			"-march=armv8.2-a+dotprod",
43		},
44	}
45
46	arm64Ldflags = []string{
47		"-Wl,--hash-style=gnu",
48		"-Wl,-z,separate-code",
49	}
50
51	arm64Lldflags = append(ClangFilterUnknownLldflags(arm64Ldflags),
52		"-Wl,-z,max-page-size=4096")
53
54	arm64Cppflags = []string{}
55
56	arm64ClangCpuVariantCflags = map[string][]string{
57		"cortex-a53": []string{
58			"-mcpu=cortex-a53",
59		},
60		"cortex-a55": []string{
61			"-mcpu=cortex-a55",
62		},
63		"cortex-a75": []string{
64			// Use the cortex-a55 since it is similar to the little
65			// core (cortex-a55) and is sensitive to ordering.
66			"-mcpu=cortex-a55",
67		},
68		"cortex-a76": []string{
69			// Use the cortex-a55 since it is similar to the little
70			// core (cortex-a55) and is sensitive to ordering.
71			"-mcpu=cortex-a55",
72		},
73		"kryo": []string{
74			"-mcpu=kryo",
75		},
76		"kryo385": []string{
77			// Use cortex-a53 because kryo385 is not supported in GCC/clang.
78			"-mcpu=cortex-a53",
79		},
80		"exynos-m1": []string{
81			"-mcpu=exynos-m1",
82		},
83		"exynos-m2": []string{
84			"-mcpu=exynos-m2",
85		},
86	}
87)
88
89const (
90	arm64GccVersion = "4.9"
91)
92
93func init() {
94	pctx.StaticVariable("arm64GccVersion", arm64GccVersion)
95
96	pctx.SourcePathVariable("Arm64GccRoot",
97		"prebuilts/gcc/${HostPrebuiltTag}/aarch64/aarch64-linux-android-${arm64GccVersion}")
98
99	pctx.StaticVariable("Arm64Ldflags", strings.Join(arm64Ldflags, " "))
100	pctx.StaticVariable("Arm64Lldflags", strings.Join(arm64Lldflags, " "))
101
102	pctx.StaticVariable("Arm64ClangCflags", strings.Join(ClangFilterUnknownCflags(arm64Cflags), " "))
103	pctx.StaticVariable("Arm64ClangLdflags", strings.Join(ClangFilterUnknownCflags(arm64Ldflags), " "))
104	pctx.StaticVariable("Arm64ClangLldflags", strings.Join(ClangFilterUnknownCflags(arm64Lldflags), " "))
105	pctx.StaticVariable("Arm64ClangCppflags", strings.Join(ClangFilterUnknownCflags(arm64Cppflags), " "))
106
107	pctx.StaticVariable("Arm64ClangArmv8ACflags", strings.Join(arm64ArchVariantCflags["armv8-a"], " "))
108	pctx.StaticVariable("Arm64ClangArmv8ABranchProtCflags", strings.Join(arm64ArchVariantCflags["armv8-a-branchprot"], " "))
109	pctx.StaticVariable("Arm64ClangArmv82ACflags", strings.Join(arm64ArchVariantCflags["armv8-2a"], " "))
110	pctx.StaticVariable("Arm64ClangArmv82ADotprodCflags", strings.Join(arm64ArchVariantCflags["armv8-2a-dotprod"], " "))
111
112	pctx.StaticVariable("Arm64ClangCortexA53Cflags",
113		strings.Join(arm64ClangCpuVariantCflags["cortex-a53"], " "))
114
115	pctx.StaticVariable("Arm64ClangCortexA55Cflags",
116		strings.Join(arm64ClangCpuVariantCflags["cortex-a55"], " "))
117
118	pctx.StaticVariable("Arm64ClangKryoCflags",
119		strings.Join(arm64ClangCpuVariantCflags["kryo"], " "))
120
121	pctx.StaticVariable("Arm64ClangExynosM1Cflags",
122		strings.Join(arm64ClangCpuVariantCflags["exynos-m1"], " "))
123
124	pctx.StaticVariable("Arm64ClangExynosM2Cflags",
125		strings.Join(arm64ClangCpuVariantCflags["exynos-m2"], " "))
126}
127
128var (
129	arm64ClangArchVariantCflagsVar = map[string]string{
130		"armv8-a":            "${config.Arm64ClangArmv8ACflags}",
131		"armv8-a-branchprot": "${config.Arm64ClangArmv8ABranchProtCflags}",
132		"armv8-2a":           "${config.Arm64ClangArmv82ACflags}",
133		"armv8-2a-dotprod":   "${config.Arm64ClangArmv82ADotprodCflags}",
134	}
135
136	arm64ClangCpuVariantCflagsVar = map[string]string{
137		"":           "",
138		"cortex-a53": "${config.Arm64ClangCortexA53Cflags}",
139		"cortex-a55": "${config.Arm64ClangCortexA55Cflags}",
140		"cortex-a72": "${config.Arm64ClangCortexA53Cflags}",
141		"cortex-a73": "${config.Arm64ClangCortexA53Cflags}",
142		"cortex-a75": "${config.Arm64ClangCortexA55Cflags}",
143		"cortex-a76": "${config.Arm64ClangCortexA55Cflags}",
144		"kryo":       "${config.Arm64ClangKryoCflags}",
145		"kryo385":    "${config.Arm64ClangCortexA53Cflags}",
146		"exynos-m1":  "${config.Arm64ClangExynosM1Cflags}",
147		"exynos-m2":  "${config.Arm64ClangExynosM2Cflags}",
148	}
149)
150
151type toolchainArm64 struct {
152	toolchain64Bit
153
154	ldflags              string
155	lldflags             string
156	toolchainClangCflags string
157}
158
159func (t *toolchainArm64) Name() string {
160	return "arm64"
161}
162
163func (t *toolchainArm64) GccRoot() string {
164	return "${config.Arm64GccRoot}"
165}
166
167func (t *toolchainArm64) GccTriple() string {
168	return "aarch64-linux-android"
169}
170
171func (t *toolchainArm64) GccVersion() string {
172	return arm64GccVersion
173}
174
175func (t *toolchainArm64) IncludeFlags() string {
176	return ""
177}
178
179func (t *toolchainArm64) ClangTriple() string {
180	return t.GccTriple()
181}
182
183func (t *toolchainArm64) ClangCflags() string {
184	return "${config.Arm64ClangCflags}"
185}
186
187func (t *toolchainArm64) ClangCppflags() string {
188	return "${config.Arm64ClangCppflags}"
189}
190
191func (t *toolchainArm64) ClangLdflags() string {
192	return t.ldflags
193}
194
195func (t *toolchainArm64) ClangLldflags() string {
196	return t.lldflags
197}
198
199func (t *toolchainArm64) ToolchainClangCflags() string {
200	return t.toolchainClangCflags
201}
202
203func (toolchainArm64) LibclangRuntimeLibraryArch() string {
204	return "aarch64"
205}
206
207func arm64ToolchainFactory(arch android.Arch) Toolchain {
208	switch arch.ArchVariant {
209	case "armv8-a":
210	case "armv8-a-branchprot":
211	case "armv8-2a":
212	case "armv8-2a-dotprod":
213		// Nothing extra for armv8-a/armv8-2a
214	default:
215		panic(fmt.Sprintf("Unknown ARM architecture version: %q", arch.ArchVariant))
216	}
217
218	toolchainClangCflags := []string{arm64ClangArchVariantCflagsVar[arch.ArchVariant]}
219	toolchainClangCflags = append(toolchainClangCflags,
220		variantOrDefault(arm64ClangCpuVariantCflagsVar, arch.CpuVariant))
221
222	var extraLdflags string
223	switch arch.CpuVariant {
224	case "cortex-a53", "cortex-a72", "cortex-a73", "kryo", "exynos-m1", "exynos-m2":
225		extraLdflags = "-Wl,--fix-cortex-a53-843419"
226	}
227
228	return &toolchainArm64{
229		ldflags: strings.Join([]string{
230			"${config.Arm64Ldflags}",
231			extraLdflags,
232		}, " "),
233		lldflags: strings.Join([]string{
234			"${config.Arm64Lldflags}",
235			extraLdflags,
236		}, " "),
237		toolchainClangCflags: strings.Join(toolchainClangCflags, " "),
238	}
239}
240
241func init() {
242	registerToolchainFactory(android.Android, android.Arm64, arm64ToolchainFactory)
243}
244