xref: /aosp_15_r20/build/soong/cc/config/arm64_device.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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		// On ARMv9 and later, Pointer Authentication Codes (PAC) are mandatory,
45		// so -fstack-protector is unnecessary.
46		"armv9-a": []string{
47			"-march=armv8.2-a+dotprod",
48			"-mbranch-protection=standard",
49			"-fno-stack-protector",
50		},
51		"armv9-2a": []string{
52			"-march=armv9.2-a",
53			"-mbranch-protection=standard",
54			"-fno-stack-protector",
55		},
56	}
57
58	arm64Ldflags = []string{
59		"-Wl,-z,separate-code",
60		"-Wl,-z,separate-loadable-segments",
61	}
62
63	arm64Lldflags = arm64Ldflags
64
65	arm64Cppflags = []string{}
66
67	arm64CpuVariantCflags = map[string][]string{
68		"cortex-a53": []string{
69			"-mcpu=cortex-a53",
70		},
71		"cortex-a55": []string{
72			"-mcpu=cortex-a55",
73		},
74		"cortex-a75": []string{
75			// Use the cortex-a55 since it is similar to the little
76			// core (cortex-a55) and is sensitive to ordering.
77			"-mcpu=cortex-a55",
78		},
79		"cortex-a76": []string{
80			// Use the cortex-a55 since it is similar to the little
81			// core (cortex-a55) and is sensitive to ordering.
82			"-mcpu=cortex-a55",
83		},
84		"kryo": []string{
85			"-mcpu=kryo",
86		},
87		"kryo385": []string{
88			// Use cortex-a53 because kryo385 is not supported in clang.
89			"-mcpu=cortex-a53",
90		},
91		"exynos-m1": []string{
92			"-mcpu=exynos-m1",
93		},
94		"exynos-m2": []string{
95			"-mcpu=exynos-m2",
96		},
97	}
98)
99
100func init() {
101	pctx.StaticVariable("Arm64Ldflags", strings.Join(arm64Ldflags, " "))
102
103	pctx.VariableFunc("Arm64Lldflags", func(ctx android.PackageVarContext) string {
104		maxPageSizeFlag := "-Wl,-z,max-page-size=" + ctx.Config().MaxPageSizeSupported()
105		flags := append(arm64Lldflags, maxPageSizeFlag)
106		return strings.Join(flags, " ")
107	})
108
109	pctx.VariableFunc("Arm64Cflags", func(ctx android.PackageVarContext) string {
110		flags := arm64Cflags
111		if ctx.Config().NoBionicPageSizeMacro() {
112			flags = append(flags, "-D__BIONIC_NO_PAGE_SIZE_MACRO")
113		} else {
114			flags = append(flags, "-D__BIONIC_DEPRECATED_PAGE_SIZE_MACRO")
115		}
116		return strings.Join(flags, " ")
117	})
118
119	pctx.StaticVariable("Arm64Cppflags", strings.Join(arm64Cppflags, " "))
120
121	for variant, cflags := range arm64ArchVariantCflags {
122		pctx.StaticVariable("Arm64"+variant+"VariantCflags", strings.Join(cflags, " "))
123	}
124
125	pctx.StaticVariable("Arm64CortexA53Cflags", strings.Join(arm64CpuVariantCflags["cortex-a53"], " "))
126	pctx.StaticVariable("Arm64CortexA55Cflags", strings.Join(arm64CpuVariantCflags["cortex-a55"], " "))
127	pctx.StaticVariable("Arm64KryoCflags", strings.Join(arm64CpuVariantCflags["kryo"], " "))
128	pctx.StaticVariable("Arm64ExynosM1Cflags", strings.Join(arm64CpuVariantCflags["exynos-m1"], " "))
129	pctx.StaticVariable("Arm64ExynosM2Cflags", strings.Join(arm64CpuVariantCflags["exynos-m2"], " "))
130
131	pctx.StaticVariable("Arm64FixCortexA53Ldflags", "-Wl,--fix-cortex-a53-843419")
132}
133
134var (
135	arm64CpuVariantCflagsVar = map[string]string{
136		"cortex-a53": "${config.Arm64CortexA53Cflags}",
137		"cortex-a55": "${config.Arm64CortexA55Cflags}",
138		"cortex-a72": "${config.Arm64CortexA53Cflags}",
139		"cortex-a73": "${config.Arm64CortexA53Cflags}",
140		"cortex-a75": "${config.Arm64CortexA55Cflags}",
141		"cortex-a76": "${config.Arm64CortexA55Cflags}",
142		"kryo":       "${config.Arm64KryoCflags}",
143		"kryo385":    "${config.Arm64CortexA53Cflags}",
144		"exynos-m1":  "${config.Arm64ExynosM1Cflags}",
145		"exynos-m2":  "${config.Arm64ExynosM2Cflags}",
146	}
147
148	arm64CpuVariantLdflags = map[string]string{
149		"cortex-a53": "${config.Arm64FixCortexA53Ldflags}",
150		"cortex-a72": "${config.Arm64FixCortexA53Ldflags}",
151		"cortex-a73": "${config.Arm64FixCortexA53Ldflags}",
152		"kryo":       "${config.Arm64FixCortexA53Ldflags}",
153		"exynos-m1":  "${config.Arm64FixCortexA53Ldflags}",
154		"exynos-m2":  "${config.Arm64FixCortexA53Ldflags}",
155	}
156)
157
158type toolchainArm64 struct {
159	toolchainBionic
160	toolchain64Bit
161
162	ldflags         string
163	lldflags        string
164	toolchainCflags string
165}
166
167func (t *toolchainArm64) Name() string {
168	return "arm64"
169}
170
171func (t *toolchainArm64) IncludeFlags() string {
172	return ""
173}
174
175func (t *toolchainArm64) ClangTriple() string {
176	return "aarch64-linux-android"
177}
178
179func (t *toolchainArm64) Cflags() string {
180	return "${config.Arm64Cflags}"
181}
182
183func (t *toolchainArm64) Cppflags() string {
184	return "${config.Arm64Cppflags}"
185}
186
187func (t *toolchainArm64) Ldflags() string {
188	return t.ldflags
189}
190
191func (t *toolchainArm64) Lldflags() string {
192	return t.lldflags
193}
194
195func (t *toolchainArm64) ToolchainCflags() string {
196	return t.toolchainCflags
197}
198
199func (toolchainArm64) LibclangRuntimeLibraryArch() string {
200	return "aarch64"
201}
202
203func arm64ToolchainFactory(arch android.Arch) Toolchain {
204	// Error now rather than having a confusing Ninja error
205	if _, ok := arm64ArchVariantCflags[arch.ArchVariant]; !ok {
206		panic(fmt.Sprintf("Unknown ARM64 architecture version: %q", arch.ArchVariant))
207	}
208
209	toolchainCflags := []string{"${config.Arm64" + arch.ArchVariant + "VariantCflags}"}
210	toolchainCflags = append(toolchainCflags,
211		variantOrDefault(arm64CpuVariantCflagsVar, arch.CpuVariant))
212
213	extraLdflags := variantOrDefault(arm64CpuVariantLdflags, arch.CpuVariant)
214	return &toolchainArm64{
215		ldflags: strings.Join([]string{
216			"${config.Arm64Ldflags}",
217			extraLdflags,
218		}, " "),
219		lldflags: strings.Join([]string{
220			"${config.Arm64Lldflags}",
221			extraLdflags,
222		}, " "),
223		toolchainCflags: strings.Join(toolchainCflags, " "),
224	}
225}
226
227func init() {
228	registerToolchainFactory(android.Android, android.Arm64, arm64ToolchainFactory)
229}
230