Home | History | Annotate | Download | only in cc
      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 
     15 package cc
     16 
     17 import (
     18 	"strings"
     19 
     20 	"android/soong/common"
     21 )
     22 
     23 var (
     24 	x86Cflags = []string{
     25 		"-fno-exceptions", // from build/core/combo/select.mk
     26 		"-Wno-multichar",  // from build/core/combo/select.mk
     27 		"-O2",
     28 		"-Wa,--noexecstack",
     29 		"-Werror=format-security",
     30 		"-D_FORTIFY_SOURCE=2",
     31 		"-Wstrict-aliasing=2",
     32 		"-ffunction-sections",
     33 		"-finline-functions",
     34 		"-finline-limit=300",
     35 		"-fno-short-enums",
     36 		"-fstrict-aliasing",
     37 		"-funswitch-loops",
     38 		"-funwind-tables",
     39 		"-fstack-protector-strong",
     40 		"-no-canonical-prefixes",
     41 		"-fno-canonical-system-headers",
     42 
     43 		// TARGET_RELEASE_CFLAGS from build/core/combo/select.mk
     44 		"-O2",
     45 		"-g",
     46 		"-fno-strict-aliasing",
     47 	}
     48 
     49 	x86Cppflags = []string{}
     50 
     51 	x86Ldflags = []string{
     52 		"-Wl,-z,noexecstack",
     53 		"-Wl,-z,relro",
     54 		"-Wl,-z,now",
     55 		"-Wl,--build-id=md5",
     56 		"-Wl,--warn-shared-textrel",
     57 		"-Wl,--fatal-warnings",
     58 		"-Wl,--gc-sections",
     59 		"-Wl,--hash-style=gnu",
     60 		"-Wl,--no-undefined-version",
     61 	}
     62 
     63 	x86ArchVariantCflags = map[string][]string{
     64 		"": []string{
     65 			"-march=prescott",
     66 		},
     67 		"atom": []string{
     68 			"-march=atom",
     69 			"-mfpmath=sse",
     70 		},
     71 		"haswell": []string{
     72 			"-march=core-avx2",
     73 			"-mfpmath=sse",
     74 		},
     75 		"ivybridge": []string{
     76 			"-march=core-avx-i",
     77 			"-mfpmath=sse",
     78 		},
     79 		"sandybridge": []string{
     80 			"-march=corei7",
     81 			"-mfpmath=sse",
     82 		},
     83 		"silvermont": []string{
     84 			"-march=slm",
     85 			"-mfpmath=sse",
     86 		},
     87 	}
     88 
     89 	x86ArchFeatureCflags = map[string][]string{
     90 		"ssse3":  []string{"-DUSE_SSSE3", "-mssse3"},
     91 		"sse4":   []string{"-msse4"},
     92 		"sse4_1": []string{"-msse4.1"},
     93 		"sse4_2": []string{"-msse4.2"},
     94 		"avx":    []string{"-mavx"},
     95 		"aes_ni": []string{"-maes"},
     96 	}
     97 )
     98 
     99 const (
    100 	x86GccVersion = "4.9"
    101 )
    102 
    103 func init() {
    104 	common.RegisterArchFeatures(common.X86, "atom",
    105 		"ssse3",
    106 		"movbe")
    107 	common.RegisterArchFeatures(common.X86, "haswell",
    108 		"ssse3",
    109 		"sse4",
    110 		"sse4_1",
    111 		"sse4_2",
    112 		"aes_ni",
    113 		"avx",
    114 		"popcnt",
    115 		"movbe")
    116 	common.RegisterArchFeatures(common.X86, "ivybridge",
    117 		"ssse3",
    118 		"sse4",
    119 		"sse4_1",
    120 		"sse4_2",
    121 		"aes_ni",
    122 		"avx",
    123 		"popcnt")
    124 	common.RegisterArchFeatures(common.X86, "sandybridge",
    125 		"ssse3",
    126 		"sse4",
    127 		"sse4_1",
    128 		"sse4_2",
    129 		"popcnt")
    130 	common.RegisterArchFeatures(common.X86, "silvermont",
    131 		"ssse3",
    132 		"sse4",
    133 		"sse4_1",
    134 		"sse4_2",
    135 		"aes_ni",
    136 		"popcnt",
    137 		"movbe")
    138 
    139 	pctx.StaticVariable("x86GccVersion", x86GccVersion)
    140 
    141 	pctx.SourcePathVariable("x86GccRoot",
    142 		"prebuilts/gcc/${HostPrebuiltTag}/x86/x86_64-linux-android-${x86GccVersion}")
    143 
    144 	pctx.StaticVariable("x86GccTriple", "x86_64-linux-android")
    145 
    146 	pctx.StaticVariable("x86ToolchainCflags", "-m32")
    147 	pctx.StaticVariable("x86ToolchainLdflags", "-m32")
    148 
    149 	pctx.StaticVariable("x86Cflags", strings.Join(x86Cflags, " "))
    150 	pctx.StaticVariable("x86Ldflags", strings.Join(x86Ldflags, " "))
    151 	pctx.StaticVariable("x86Cppflags", strings.Join(x86Cppflags, " "))
    152 	pctx.StaticVariable("x86IncludeFlags", strings.Join([]string{
    153 		"-isystem ${LibcRoot}/arch-x86/include",
    154 		"-isystem ${LibcRoot}/include",
    155 		"-isystem ${LibcRoot}/kernel/uapi",
    156 		"-isystem ${LibcRoot}/kernel/uapi/asm-x86",
    157 		"-isystem ${LibmRoot}/include",
    158 		"-isystem ${LibmRoot}/include/i387",
    159 	}, " "))
    160 
    161 	// Clang cflags
    162 	pctx.StaticVariable("x86ClangCflags", strings.Join(clangFilterUnknownCflags(x86Cflags), " "))
    163 	pctx.StaticVariable("x86ClangLdflags", strings.Join(clangFilterUnknownCflags(x86Ldflags), " "))
    164 	pctx.StaticVariable("x86ClangCppflags", strings.Join(clangFilterUnknownCflags(x86Cppflags), " "))
    165 
    166 	// Extended cflags
    167 
    168 	// Architecture variant cflags
    169 	for variant, cflags := range x86ArchVariantCflags {
    170 		pctx.StaticVariable("x86"+variant+"VariantCflags", strings.Join(cflags, " "))
    171 		pctx.StaticVariable("x86"+variant+"VariantClangCflags",
    172 			strings.Join(clangFilterUnknownCflags(cflags), " "))
    173 	}
    174 }
    175 
    176 type toolchainX86 struct {
    177 	toolchain32Bit
    178 	toolchainCflags, toolchainClangCflags string
    179 }
    180 
    181 func (t *toolchainX86) Name() string {
    182 	return "x86"
    183 }
    184 
    185 func (t *toolchainX86) GccRoot() string {
    186 	return "${x86GccRoot}"
    187 }
    188 
    189 func (t *toolchainX86) GccTriple() string {
    190 	return "${x86GccTriple}"
    191 }
    192 
    193 func (t *toolchainX86) GccVersion() string {
    194 	return x86GccVersion
    195 }
    196 
    197 func (t *toolchainX86) ToolchainLdflags() string {
    198 	return "${x86ToolchainLdflags}"
    199 }
    200 
    201 func (t *toolchainX86) ToolchainCflags() string {
    202 	return t.toolchainCflags
    203 }
    204 
    205 func (t *toolchainX86) Cflags() string {
    206 	return "${x86Cflags}"
    207 }
    208 
    209 func (t *toolchainX86) Cppflags() string {
    210 	return "${x86Cppflags}"
    211 }
    212 
    213 func (t *toolchainX86) Ldflags() string {
    214 	return "${x86Ldflags}"
    215 }
    216 
    217 func (t *toolchainX86) IncludeFlags() string {
    218 	return "${x86IncludeFlags}"
    219 }
    220 
    221 func (t *toolchainX86) ClangTriple() string {
    222 	return "${x86GccTriple}"
    223 }
    224 
    225 func (t *toolchainX86) ToolchainClangCflags() string {
    226 	return t.toolchainClangCflags
    227 }
    228 
    229 func (t *toolchainX86) ClangCflags() string {
    230 	return "${x86ClangCflags}"
    231 }
    232 
    233 func (t *toolchainX86) ClangCppflags() string {
    234 	return "${x86ClangCppflags}"
    235 }
    236 
    237 func (t *toolchainX86) ClangLdflags() string {
    238 	return "${x86Ldflags}"
    239 }
    240 
    241 func x86ToolchainFactory(arch common.Arch) Toolchain {
    242 	toolchainCflags := []string{
    243 		"${x86ToolchainCflags}",
    244 		"${x86" + arch.ArchVariant + "VariantCflags}",
    245 	}
    246 
    247 	toolchainClangCflags := []string{
    248 		"${x86ToolchainCflags}",
    249 		"${x86" + arch.ArchVariant + "VariantClangCflags}",
    250 	}
    251 
    252 	for _, feature := range arch.ArchFeatures {
    253 		toolchainCflags = append(toolchainCflags, x86ArchFeatureCflags[feature]...)
    254 		toolchainClangCflags = append(toolchainClangCflags, x86ArchFeatureCflags[feature]...)
    255 	}
    256 
    257 	return &toolchainX86{
    258 		toolchainCflags:      strings.Join(toolchainCflags, " "),
    259 		toolchainClangCflags: strings.Join(toolchainClangCflags, " "),
    260 	}
    261 }
    262 
    263 func init() {
    264 	registerDeviceToolchainFactory(common.X86, x86ToolchainFactory)
    265 }
    266