| Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 1 | // Copyright 2022 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 |  | 
 | 15 | package config | 
 | 16 |  | 
 | 17 | import ( | 
 | 18 | 	"fmt" | 
 | 19 | 	"strings" | 
 | 20 |  | 
 | 21 | 	"android/soong/android" | 
 | 22 | ) | 
 | 23 |  | 
 | 24 | var ( | 
 | 25 | 	riscv64Cflags = []string{ | 
| Elliott Hughes | c260046 | 2024-03-18 19:37:13 +0000 | [diff] [blame] | 26 | 		// Help catch common 32/64-bit errors. (This is duplicated in all 64-bit | 
 | 27 | 		// architectures' cflags.) | 
| Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 28 | 		"-Werror=implicit-function-declaration", | 
| Elliott Hughes | c260046 | 2024-03-18 19:37:13 +0000 | [diff] [blame] | 29 | 		// This is already the driver's Android default, but duplicated here (and | 
 | 30 | 		// below) for ease of experimentation with additional extensions. | 
| Elliott Hughes | 726b001 | 2023-09-14 18:12:18 +0000 | [diff] [blame] | 31 | 		"-march=rv64gcv_zba_zbb_zbs", | 
| Elliott Hughes | c260046 | 2024-03-18 19:37:13 +0000 | [diff] [blame] | 32 | 		// TODO: remove when qemu V works (https://gitlab.com/qemu-project/qemu/-/issues/1976) | 
 | 33 | 		// (Note that we'll probably want to wait for berberis to be good enough | 
 | 34 | 		// that most people don't care about qemu's V performance either!) | 
| Elliott Hughes | 6c93f69 | 2024-01-22 11:00:52 -0800 | [diff] [blame] | 35 | 		"-mno-implicit-float", | 
| Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 36 | 	} | 
 | 37 |  | 
 | 38 | 	riscv64ArchVariantCflags = map[string][]string{} | 
 | 39 |  | 
 | 40 | 	riscv64Ldflags = []string{ | 
| Elliott Hughes | c260046 | 2024-03-18 19:37:13 +0000 | [diff] [blame] | 41 | 		// This is already the driver's Android default, but duplicated here (and | 
 | 42 | 		// above) for ease of experimentation with additional extensions. | 
| Elliott Hughes | 726b001 | 2023-09-14 18:12:18 +0000 | [diff] [blame] | 43 | 		"-march=rv64gcv_zba_zbb_zbs", | 
| Elliott Hughes | c260046 | 2024-03-18 19:37:13 +0000 | [diff] [blame] | 44 | 		// TODO: remove when clang default changed (https://github.com/google/android-riscv64/issues/124) | 
| AdityaK | 431c0b9 | 2024-01-19 07:31:29 -0800 | [diff] [blame] | 45 | 		"-Wl,-mllvm -Wl,-jump-is-expensive=false", | 
| Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 46 | 	} | 
 | 47 |  | 
 | 48 | 	riscv64Lldflags = append(riscv64Ldflags, | 
| AdityaK | f7c0afd | 2023-02-10 09:50:39 -0800 | [diff] [blame] | 49 | 		"-Wl,-z,max-page-size=4096", | 
| AdityaK | f7c0afd | 2023-02-10 09:50:39 -0800 | [diff] [blame] | 50 | 	) | 
| Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 51 |  | 
 | 52 | 	riscv64Cppflags = []string{} | 
 | 53 |  | 
 | 54 | 	riscv64CpuVariantCflags = map[string][]string{} | 
 | 55 | ) | 
 | 56 |  | 
 | 57 | const () | 
 | 58 |  | 
 | 59 | func init() { | 
 | 60 |  | 
| Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 61 | 	pctx.StaticVariable("Riscv64Ldflags", strings.Join(riscv64Ldflags, " ")) | 
 | 62 | 	pctx.StaticVariable("Riscv64Lldflags", strings.Join(riscv64Lldflags, " ")) | 
| Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 63 |  | 
| Cole Faust | 8982b1c | 2024-04-08 16:54:45 -0700 | [diff] [blame] | 64 | 	pctx.StaticVariable("Riscv64Cflags", strings.Join(riscv64Cflags, " ")) | 
 | 65 | 	pctx.StaticVariable("Riscv64Cppflags", strings.Join(riscv64Cppflags, " ")) | 
| Colin Cross | f05b0d3 | 2022-07-14 18:10:34 -0700 | [diff] [blame] | 66 | } | 
 | 67 |  | 
 | 68 | var ( | 
 | 69 | 	riscv64ArchVariantCflagsVar = map[string]string{} | 
 | 70 |  | 
 | 71 | 	riscv64CpuVariantCflagsVar = map[string]string{} | 
 | 72 |  | 
 | 73 | 	riscv64CpuVariantLdflags = map[string]string{} | 
 | 74 | ) | 
 | 75 |  | 
 | 76 | type toolchainRiscv64 struct { | 
 | 77 | 	toolchainBionic | 
 | 78 | 	toolchain64Bit | 
 | 79 |  | 
 | 80 | 	ldflags         string | 
 | 81 | 	lldflags        string | 
 | 82 | 	toolchainCflags string | 
 | 83 | } | 
 | 84 |  | 
 | 85 | func (t *toolchainRiscv64) Name() string { | 
 | 86 | 	return "riscv64" | 
 | 87 | } | 
 | 88 |  | 
 | 89 | func (t *toolchainRiscv64) IncludeFlags() string { | 
 | 90 | 	return "" | 
 | 91 | } | 
 | 92 |  | 
 | 93 | func (t *toolchainRiscv64) ClangTriple() string { | 
 | 94 | 	return "riscv64-linux-android" | 
 | 95 | } | 
 | 96 |  | 
 | 97 | func (t *toolchainRiscv64) Cflags() string { | 
 | 98 | 	return "${config.Riscv64Cflags}" | 
 | 99 | } | 
 | 100 |  | 
 | 101 | func (t *toolchainRiscv64) Cppflags() string { | 
 | 102 | 	return "${config.Riscv64Cppflags}" | 
 | 103 | } | 
 | 104 |  | 
 | 105 | func (t *toolchainRiscv64) Ldflags() string { | 
 | 106 | 	return t.ldflags | 
 | 107 | } | 
 | 108 |  | 
 | 109 | func (t *toolchainRiscv64) Lldflags() string { | 
 | 110 | 	return t.lldflags | 
 | 111 | } | 
 | 112 |  | 
 | 113 | func (t *toolchainRiscv64) ToolchainCflags() string { | 
 | 114 | 	return t.toolchainCflags | 
 | 115 | } | 
 | 116 |  | 
 | 117 | func (toolchainRiscv64) LibclangRuntimeLibraryArch() string { | 
 | 118 | 	return "riscv64" | 
 | 119 | } | 
 | 120 |  | 
 | 121 | func riscv64ToolchainFactory(arch android.Arch) Toolchain { | 
 | 122 | 	switch arch.ArchVariant { | 
 | 123 | 	case "": | 
 | 124 | 	default: | 
 | 125 | 		panic(fmt.Sprintf("Unknown Riscv64 architecture version: %q", arch.ArchVariant)) | 
 | 126 | 	} | 
 | 127 |  | 
 | 128 | 	toolchainCflags := []string{riscv64ArchVariantCflagsVar[arch.ArchVariant]} | 
 | 129 | 	toolchainCflags = append(toolchainCflags, | 
 | 130 | 		variantOrDefault(riscv64CpuVariantCflagsVar, arch.CpuVariant)) | 
 | 131 |  | 
 | 132 | 	extraLdflags := variantOrDefault(riscv64CpuVariantLdflags, arch.CpuVariant) | 
 | 133 | 	return &toolchainRiscv64{ | 
 | 134 | 		ldflags: strings.Join([]string{ | 
 | 135 | 			"${config.Riscv64Ldflags}", | 
 | 136 | 			extraLdflags, | 
 | 137 | 		}, " "), | 
 | 138 | 		lldflags: strings.Join([]string{ | 
 | 139 | 			"${config.Riscv64Lldflags}", | 
 | 140 | 			extraLdflags, | 
 | 141 | 		}, " "), | 
 | 142 | 		toolchainCflags: strings.Join(toolchainCflags, " "), | 
 | 143 | 	} | 
 | 144 | } | 
 | 145 |  | 
 | 146 | func init() { | 
 | 147 | 	registerToolchainFactory(android.Android, android.Riscv64, riscv64ToolchainFactory) | 
 | 148 | } |