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