Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 | "strings" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
| 23 | var ( |
| 24 | linuxCflags = []string{ |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 25 | "-fdiagnostics-color", |
| 26 | |
| 27 | "-Wa,--noexecstack", |
| 28 | |
| 29 | "-fPIC", |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 30 | |
| 31 | "-U_FORTIFY_SOURCE", |
| 32 | "-D_FORTIFY_SOURCE=2", |
| 33 | "-fstack-protector", |
| 34 | |
| 35 | // Workaround differences in inttypes.h between host and target. |
| 36 | //See bug 12708004. |
| 37 | "-D__STDC_FORMAT_MACROS", |
| 38 | "-D__STDC_CONSTANT_MACROS", |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | linuxLdflags = []string{ |
| 42 | "-Wl,-z,noexecstack", |
| 43 | "-Wl,-z,relro", |
| 44 | "-Wl,-z,now", |
| 45 | "-Wl,--no-undefined-version", |
| 46 | } |
| 47 | |
| 48 | // Extended cflags |
| 49 | linuxX86Cflags = []string{ |
| 50 | "-msse3", |
| 51 | "-mfpmath=sse", |
| 52 | "-m32", |
| 53 | "-march=prescott", |
| 54 | "-D_FILE_OFFSET_BITS=64", |
| 55 | "-D_LARGEFILE_SOURCE=1", |
| 56 | } |
| 57 | |
| 58 | linuxX8664Cflags = []string{ |
| 59 | "-m64", |
| 60 | } |
| 61 | |
| 62 | linuxX86Ldflags = []string{ |
| 63 | "-m32", |
| 64 | } |
| 65 | |
| 66 | linuxX8664Ldflags = []string{ |
| 67 | "-m64", |
| 68 | } |
| 69 | |
| 70 | linuxClangCflags = append(ClangFilterUnknownCflags(linuxCflags), []string{ |
| 71 | "--gcc-toolchain=${LinuxGccRoot}", |
| 72 | "--sysroot ${LinuxGccRoot}/sysroot", |
| 73 | "-fstack-protector-strong", |
| 74 | }...) |
| 75 | |
| 76 | linuxClangLdflags = append(ClangFilterUnknownCflags(linuxLdflags), []string{ |
| 77 | "--gcc-toolchain=${LinuxGccRoot}", |
| 78 | "--sysroot ${LinuxGccRoot}/sysroot", |
| 79 | }...) |
| 80 | |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame^] | 81 | linuxClangLldflags = ClangFilterUnknownLldflags(linuxClangLdflags) |
| 82 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 83 | linuxX86ClangLdflags = append(ClangFilterUnknownCflags(linuxX86Ldflags), []string{ |
| 84 | "-B${LinuxGccRoot}/lib/gcc/${LinuxGccTriple}/${LinuxGccVersion}/32", |
| 85 | "-L${LinuxGccRoot}/lib/gcc/${LinuxGccTriple}/${LinuxGccVersion}/32", |
| 86 | "-L${LinuxGccRoot}/${LinuxGccTriple}/lib32", |
| 87 | }...) |
| 88 | |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame^] | 89 | linuxX86ClangLldflags = ClangFilterUnknownLldflags(linuxX86ClangLdflags) |
| 90 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 91 | linuxX8664ClangLdflags = append(ClangFilterUnknownCflags(linuxX8664Ldflags), []string{ |
| 92 | "-B${LinuxGccRoot}/lib/gcc/${LinuxGccTriple}/${LinuxGccVersion}", |
| 93 | "-L${LinuxGccRoot}/lib/gcc/${LinuxGccTriple}/${LinuxGccVersion}", |
| 94 | "-L${LinuxGccRoot}/${LinuxGccTriple}/lib64", |
| 95 | }...) |
| 96 | |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame^] | 97 | linuxX8664ClangLldflags = ClangFilterUnknownLldflags(linuxX8664ClangLdflags) |
| 98 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 99 | linuxClangCppflags = []string{ |
| 100 | "-isystem ${LinuxGccRoot}/${LinuxGccTriple}/include/c++/${LinuxGccVersion}", |
| 101 | "-isystem ${LinuxGccRoot}/${LinuxGccTriple}/include/c++/${LinuxGccVersion}/backward", |
| 102 | } |
| 103 | |
| 104 | linuxX86ClangCppflags = []string{ |
| 105 | "-isystem ${LinuxGccRoot}/${LinuxGccTriple}/include/c++/${LinuxGccVersion}/${LinuxGccTriple}/32", |
| 106 | } |
| 107 | |
| 108 | linuxX8664ClangCppflags = []string{ |
| 109 | "-isystem ${LinuxGccRoot}/${LinuxGccTriple}/include/c++/${LinuxGccVersion}/${LinuxGccTriple}", |
| 110 | } |
| 111 | |
| 112 | linuxAvailableLibraries = addPrefix([]string{ |
| 113 | "c", |
| 114 | "dl", |
| 115 | "gcc", |
| 116 | "gcc_s", |
| 117 | "m", |
| 118 | "ncurses", |
| 119 | "pthread", |
| 120 | "resolv", |
| 121 | "rt", |
| 122 | "util", |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 123 | }, "-l") |
| 124 | ) |
| 125 | |
| 126 | const ( |
| 127 | linuxGccVersion = "4.8" |
| 128 | ) |
| 129 | |
| 130 | func init() { |
| 131 | pctx.StaticVariable("LinuxGccVersion", linuxGccVersion) |
| 132 | |
| 133 | pctx.SourcePathVariable("LinuxGccRoot", |
| 134 | "prebuilts/gcc/${HostPrebuiltTag}/host/x86_64-linux-glibc2.15-${LinuxGccVersion}") |
| 135 | |
| 136 | pctx.StaticVariable("LinuxGccTriple", "x86_64-linux") |
| 137 | |
| 138 | pctx.StaticVariable("LinuxCflags", strings.Join(linuxCflags, " ")) |
| 139 | pctx.StaticVariable("LinuxLdflags", strings.Join(linuxLdflags, " ")) |
| 140 | |
| 141 | pctx.StaticVariable("LinuxClangCflags", strings.Join(linuxClangCflags, " ")) |
| 142 | pctx.StaticVariable("LinuxClangLdflags", strings.Join(linuxClangLdflags, " ")) |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame^] | 143 | pctx.StaticVariable("LinuxClangLldflags", strings.Join(linuxClangLldflags, " ")) |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 144 | pctx.StaticVariable("LinuxClangCppflags", strings.Join(linuxClangCppflags, " ")) |
| 145 | |
| 146 | // Extended cflags |
| 147 | pctx.StaticVariable("LinuxX86Cflags", strings.Join(linuxX86Cflags, " ")) |
| 148 | pctx.StaticVariable("LinuxX8664Cflags", strings.Join(linuxX8664Cflags, " ")) |
| 149 | pctx.StaticVariable("LinuxX86Ldflags", strings.Join(linuxX86Ldflags, " ")) |
| 150 | pctx.StaticVariable("LinuxX8664Ldflags", strings.Join(linuxX8664Ldflags, " ")) |
| 151 | |
| 152 | pctx.StaticVariable("LinuxX86ClangCflags", |
| 153 | strings.Join(ClangFilterUnknownCflags(linuxX86Cflags), " ")) |
| 154 | pctx.StaticVariable("LinuxX8664ClangCflags", |
| 155 | strings.Join(ClangFilterUnknownCflags(linuxX8664Cflags), " ")) |
| 156 | pctx.StaticVariable("LinuxX86ClangLdflags", strings.Join(linuxX86ClangLdflags, " ")) |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame^] | 157 | pctx.StaticVariable("LinuxX86ClangLldflags", strings.Join(linuxX86ClangLldflags, " ")) |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 158 | pctx.StaticVariable("LinuxX8664ClangLdflags", strings.Join(linuxX8664ClangLdflags, " ")) |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame^] | 159 | pctx.StaticVariable("LinuxX8664ClangLldflags", strings.Join(linuxX8664ClangLldflags, " ")) |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 160 | pctx.StaticVariable("LinuxX86ClangCppflags", strings.Join(linuxX86ClangCppflags, " ")) |
| 161 | pctx.StaticVariable("LinuxX8664ClangCppflags", strings.Join(linuxX8664ClangCppflags, " ")) |
Greg Hartman | 09302be | 2017-10-04 17:31:43 -0700 | [diff] [blame] | 162 | // Yasm flags |
| 163 | pctx.StaticVariable("LinuxX86YasmFlags", "-f elf32 -m x86") |
| 164 | pctx.StaticVariable("LinuxX8664YasmFlags", "-f elf64 -m amd64") |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | type toolchainLinux struct { |
| 168 | cFlags, ldFlags string |
| 169 | } |
| 170 | |
| 171 | type toolchainLinuxX86 struct { |
| 172 | toolchain32Bit |
| 173 | toolchainLinux |
| 174 | } |
| 175 | |
| 176 | type toolchainLinuxX8664 struct { |
| 177 | toolchain64Bit |
| 178 | toolchainLinux |
| 179 | } |
| 180 | |
| 181 | func (t *toolchainLinuxX86) Name() string { |
| 182 | return "x86" |
| 183 | } |
| 184 | |
| 185 | func (t *toolchainLinuxX8664) Name() string { |
| 186 | return "x86_64" |
| 187 | } |
| 188 | |
| 189 | func (t *toolchainLinux) GccRoot() string { |
| 190 | return "${config.LinuxGccRoot}" |
| 191 | } |
| 192 | |
| 193 | func (t *toolchainLinux) GccTriple() string { |
| 194 | return "${config.LinuxGccTriple}" |
| 195 | } |
| 196 | |
| 197 | func (t *toolchainLinux) GccVersion() string { |
| 198 | return linuxGccVersion |
| 199 | } |
| 200 | |
| 201 | func (t *toolchainLinuxX86) Cflags() string { |
| 202 | return "${config.LinuxCflags} ${config.LinuxX86Cflags}" |
| 203 | } |
| 204 | |
| 205 | func (t *toolchainLinuxX8664) Cflags() string { |
| 206 | return "${config.LinuxCflags} ${config.LinuxX8664Cflags}" |
| 207 | } |
| 208 | |
| 209 | func (t *toolchainLinux) Cppflags() string { |
| 210 | return "" |
| 211 | } |
| 212 | |
| 213 | func (t *toolchainLinuxX86) Ldflags() string { |
| 214 | return "${config.LinuxLdflags} ${config.LinuxX86Ldflags}" |
| 215 | } |
| 216 | |
| 217 | func (t *toolchainLinuxX8664) Ldflags() string { |
| 218 | return "${config.LinuxLdflags} ${config.LinuxX8664Ldflags}" |
| 219 | } |
| 220 | |
| 221 | func (t *toolchainLinux) IncludeFlags() string { |
| 222 | return "" |
| 223 | } |
| 224 | |
| 225 | func (t *toolchainLinuxX86) ClangTriple() string { |
| 226 | return "i686-linux-gnu" |
| 227 | } |
| 228 | |
| 229 | func (t *toolchainLinuxX86) ClangCflags() string { |
| 230 | return "${config.LinuxClangCflags} ${config.LinuxX86ClangCflags}" |
| 231 | } |
| 232 | |
| 233 | func (t *toolchainLinuxX86) ClangCppflags() string { |
| 234 | return "${config.LinuxClangCppflags} ${config.LinuxX86ClangCppflags}" |
| 235 | } |
| 236 | |
| 237 | func (t *toolchainLinuxX8664) ClangTriple() string { |
| 238 | return "x86_64-linux-gnu" |
| 239 | } |
| 240 | |
| 241 | func (t *toolchainLinuxX8664) ClangCflags() string { |
| 242 | return "${config.LinuxClangCflags} ${config.LinuxX8664ClangCflags}" |
| 243 | } |
| 244 | |
| 245 | func (t *toolchainLinuxX8664) ClangCppflags() string { |
| 246 | return "${config.LinuxClangCppflags} ${config.LinuxX8664ClangCppflags}" |
| 247 | } |
| 248 | |
| 249 | func (t *toolchainLinuxX86) ClangLdflags() string { |
| 250 | return "${config.LinuxClangLdflags} ${config.LinuxX86ClangLdflags}" |
| 251 | } |
| 252 | |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 253 | func (t *toolchainLinuxX86) ClangLldflags() string { |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame^] | 254 | return "${config.LinuxClangLldflags} ${config.LinuxX86ClangLldflags}" |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 257 | func (t *toolchainLinuxX8664) ClangLdflags() string { |
| 258 | return "${config.LinuxClangLdflags} ${config.LinuxX8664ClangLdflags}" |
| 259 | } |
| 260 | |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 261 | func (t *toolchainLinuxX8664) ClangLldflags() string { |
Chih-Hung Hsieh | 3101a96 | 2018-04-17 14:16:05 -0700 | [diff] [blame^] | 262 | return "${config.LinuxClangLldflags} ${config.LinuxX8664ClangLldflags}" |
Chih-Hung Hsieh | 02b4da5 | 2018-04-03 11:33:34 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Greg Hartman | 09302be | 2017-10-04 17:31:43 -0700 | [diff] [blame] | 265 | func (t *toolchainLinuxX86) YasmFlags() string { |
| 266 | return "${config.LinuxX86YasmFlags}" |
| 267 | } |
| 268 | |
| 269 | func (t *toolchainLinuxX8664) YasmFlags() string { |
| 270 | return "${config.LinuxX8664YasmFlags}" |
| 271 | } |
| 272 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 273 | func (t *toolchainLinux) AvailableLibraries() []string { |
| 274 | return linuxAvailableLibraries |
| 275 | } |
| 276 | |
Dan Willemsen | 2e47b34 | 2016-11-17 01:02:25 -0800 | [diff] [blame] | 277 | func (t *toolchainLinux) Bionic() bool { |
| 278 | return false |
| 279 | } |
| 280 | |
Colin Cross | b98c8b0 | 2016-07-29 13:44:28 -0700 | [diff] [blame] | 281 | var toolchainLinuxX86Singleton Toolchain = &toolchainLinuxX86{} |
| 282 | var toolchainLinuxX8664Singleton Toolchain = &toolchainLinuxX8664{} |
| 283 | |
| 284 | func linuxX86ToolchainFactory(arch android.Arch) Toolchain { |
| 285 | return toolchainLinuxX86Singleton |
| 286 | } |
| 287 | |
| 288 | func linuxX8664ToolchainFactory(arch android.Arch) Toolchain { |
| 289 | return toolchainLinuxX8664Singleton |
| 290 | } |
| 291 | |
| 292 | func init() { |
| 293 | registerToolchainFactory(android.Linux, android.X86, linuxX86ToolchainFactory) |
| 294 | registerToolchainFactory(android.Linux, android.X86_64, linuxX8664ToolchainFactory) |
| 295 | } |