1 package cc 2 3 import ( 4 "fmt" 5 "os/exec" 6 "strings" 7 8 "android/soong/common" 9 ) 10 11 var ( 12 darwinCflags = []string{ 13 "-fno-exceptions", // from build/core/combo/select.mk 14 "-Wno-multichar", // from build/core/combo/select.mk 15 16 "-fdiagnostics-color", 17 18 "-fPIC", 19 "-funwind-tables", 20 21 // Workaround differences in inttypes.h between host and target. 22 //See bug 12708004. 23 "-D__STDC_FORMAT_MACROS", 24 "-D__STDC_CONSTANT_MACROS", 25 26 // HOST_RELEASE_CFLAGS 27 "-O2", // from build/core/combo/select.mk 28 "-g", // from build/core/combo/select.mk 29 "-fno-strict-aliasing", // from build/core/combo/select.mk 30 "-isysroot ${macSdkRoot}", 31 "-mmacosx-version-min=${macSdkVersion}", 32 "-DMACOSX_DEPLOYMENT_TARGET=${macSdkVersion}", 33 } 34 35 darwinLdflags = []string{ 36 "-isysroot ${macSdkRoot}", 37 "-Wl,-syslibroot,${macSdkRoot}", 38 "-mmacosx-version-min=${macSdkVersion}", 39 } 40 41 darwinSystemCppCppflags = []string{ 42 "-isystem ${macToolchainRoot}/usr/include/c++/v1", 43 } 44 45 darwinSystemCppLdflags = []string{ 46 "-stdlib=libc++", 47 } 48 49 // Extended cflags 50 darwinX86Cflags = []string{ 51 "-m32", 52 } 53 54 darwinX8664Cflags = []string{ 55 "-m64", 56 } 57 58 darwinX86Ldflags = []string{ 59 "-m32", 60 "-Wl,-rpath,@loader_path/../lib", 61 "-Wl,-rpath,@loader_path/lib", 62 } 63 64 darwinX8664Ldflags = []string{ 65 "-m64", 66 "-Wl,-rpath,@loader_path/../lib64", 67 "-Wl,-rpath,@loader_path/lib64", 68 } 69 70 darwinClangCflags = append(clangFilterUnknownCflags(darwinCflags), []string{ 71 "-integrated-as", 72 "-fstack-protector-strong", 73 }...) 74 75 darwinClangLdflags = clangFilterUnknownCflags(darwinLdflags) 76 77 darwinX86ClangLdflags = clangFilterUnknownCflags(darwinX86Ldflags) 78 79 darwinX8664ClangLdflags = clangFilterUnknownCflags(darwinX8664Ldflags) 80 81 darwinSupportedSdkVersions = []string{ 82 "10.8", 83 "10.9", 84 "10.10", 85 "10.11", 86 } 87 ) 88 89 const ( 90 darwinGccVersion = "4.2.1" 91 ) 92 93 func init() { 94 pctx.VariableFunc("macSdkPath", func(config interface{}) (string, error) { 95 bytes, err := exec.Command("xcode-select", "--print-path").Output() 96 return strings.TrimSpace(string(bytes)), err 97 }) 98 pctx.StaticVariable("macToolchainRoot", "${macSdkPath}/Toolchains/XcodeDefault.xctoolchain") 99 pctx.VariableFunc("macSdkRoot", func(config interface{}) (string, error) { 100 return xcrunSdk(config.(common.Config), "--show-sdk-path") 101 }) 102 pctx.StaticVariable("macSdkVersion", darwinSupportedSdkVersions[0]) 103 pctx.VariableFunc("macArPath", func(config interface{}) (string, error) { 104 bytes, err := exec.Command("xcrun", "--find", "ar").Output() 105 return strings.TrimSpace(string(bytes)), err 106 }) 107 108 pctx.StaticVariable("darwinGccVersion", darwinGccVersion) 109 pctx.SourcePathVariable("darwinGccRoot", 110 "prebuilts/gcc/${HostPrebuiltTag}/host/i686-apple-darwin-${darwinGccVersion}") 111 112 pctx.StaticVariable("darwinGccTriple", "i686-apple-darwin11") 113 114 pctx.StaticVariable("darwinCflags", strings.Join(darwinCflags, " ")) 115 pctx.StaticVariable("darwinLdflags", strings.Join(darwinLdflags, " ")) 116 117 pctx.StaticVariable("darwinClangCflags", strings.Join(darwinClangCflags, " ")) 118 pctx.StaticVariable("darwinClangLdflags", strings.Join(darwinClangLdflags, " ")) 119 120 pctx.StaticVariable("darwinSystemCppCppflags", strings.Join(darwinSystemCppCppflags, " ")) 121 pctx.StaticVariable("darwinSystemCppLdflags", strings.Join(darwinSystemCppLdflags, " ")) 122 123 // Extended cflags 124 pctx.StaticVariable("darwinX86Cflags", strings.Join(darwinX86Cflags, " ")) 125 pctx.StaticVariable("darwinX8664Cflags", strings.Join(darwinX8664Cflags, " ")) 126 pctx.StaticVariable("darwinX86Ldflags", strings.Join(darwinX86Ldflags, " ")) 127 pctx.StaticVariable("darwinX8664Ldflags", strings.Join(darwinX8664Ldflags, " ")) 128 129 pctx.StaticVariable("darwinX86ClangCflags", 130 strings.Join(clangFilterUnknownCflags(darwinX86Cflags), " ")) 131 pctx.StaticVariable("darwinX8664ClangCflags", 132 strings.Join(clangFilterUnknownCflags(darwinX8664Cflags), " ")) 133 pctx.StaticVariable("darwinX86ClangLdflags", strings.Join(darwinX86ClangLdflags, " ")) 134 pctx.StaticVariable("darwinX8664ClangLdflags", strings.Join(darwinX8664ClangLdflags, " ")) 135 } 136 137 func xcrunSdk(config common.Config, arg string) (string, error) { 138 if selected := config.Getenv("MAC_SDK_VERSION"); selected != "" { 139 if !inList(selected, darwinSupportedSdkVersions) { 140 return "", fmt.Errorf("MAC_SDK_VERSION %s isn't supported: %q", selected, darwinSupportedSdkVersions) 141 } 142 143 bytes, err := exec.Command("xcrun", "--sdk", "macosx"+selected, arg).Output() 144 if err == nil { 145 return strings.TrimSpace(string(bytes)), err 146 } 147 return "", fmt.Errorf("MAC_SDK_VERSION %s is not installed", selected) 148 } 149 150 for _, sdk := range darwinSupportedSdkVersions { 151 bytes, err := exec.Command("xcrun", "--sdk", "macosx"+sdk, arg).Output() 152 if err == nil { 153 return strings.TrimSpace(string(bytes)), err 154 } 155 } 156 return "", fmt.Errorf("Could not find a supported mac sdk: %q", darwinSupportedSdkVersions) 157 } 158 159 type toolchainDarwin struct { 160 cFlags, ldFlags string 161 } 162 163 type toolchainDarwinX86 struct { 164 toolchain32Bit 165 toolchainDarwin 166 } 167 168 type toolchainDarwinX8664 struct { 169 toolchain64Bit 170 toolchainDarwin 171 } 172 173 func (t *toolchainDarwinX86) Name() string { 174 return "x86" 175 } 176 177 func (t *toolchainDarwinX8664) Name() string { 178 return "x86_64" 179 } 180 181 func (t *toolchainDarwin) GccRoot() string { 182 return "${darwinGccRoot}" 183 } 184 185 func (t *toolchainDarwin) GccTriple() string { 186 return "${darwinGccTriple}" 187 } 188 189 func (t *toolchainDarwin) GccVersion() string { 190 return darwinGccVersion 191 } 192 193 func (t *toolchainDarwin) Cflags() string { 194 return "${darwinCflags} ${darwinX86Cflags}" 195 } 196 197 func (t *toolchainDarwinX8664) Cflags() string { 198 return "${darwinCflags} ${darwinX8664Cflags}" 199 } 200 201 func (t *toolchainDarwin) Cppflags() string { 202 return "" 203 } 204 205 func (t *toolchainDarwinX86) Ldflags() string { 206 return "${darwinLdflags} ${darwinX86Ldflags}" 207 } 208 209 func (t *toolchainDarwinX8664) Ldflags() string { 210 return "${darwinLdflags} ${darwinX8664Ldflags}" 211 } 212 213 func (t *toolchainDarwin) IncludeFlags() string { 214 return "" 215 } 216 217 func (t *toolchainDarwinX86) ClangTriple() string { 218 return "i686-darwin-gnu" 219 } 220 221 func (t *toolchainDarwinX86) ClangCflags() string { 222 return "${darwinClangCflags} ${darwinX86ClangCflags}" 223 } 224 225 func (t *toolchainDarwinX8664) ClangTriple() string { 226 return "x86_64-darwin-gnu" 227 } 228 229 func (t *toolchainDarwinX8664) ClangCflags() string { 230 return "${darwinClangCflags} ${darwinX8664ClangCflags}" 231 } 232 233 func (t *toolchainDarwin) ClangCppflags() string { 234 return "" 235 } 236 237 func (t *toolchainDarwinX86) ClangLdflags() string { 238 return "${darwinClangLdflags} ${darwinX86ClangLdflags}" 239 } 240 241 func (t *toolchainDarwinX8664) ClangLdflags() string { 242 return "${darwinClangLdflags} ${darwinX8664ClangLdflags}" 243 } 244 245 func (t *toolchainDarwin) ShlibSuffix() string { 246 return ".dylib" 247 } 248 249 func (t *toolchainDarwin) SystemCppCppflags() string { 250 return "${darwinSystemCppCppflags}" 251 } 252 253 func (t *toolchainDarwin) SystemCppLdflags() string { 254 return "${darwinSystemCppLdflags}" 255 } 256 257 var toolchainDarwinX86Singleton Toolchain = &toolchainDarwinX86{} 258 var toolchainDarwinX8664Singleton Toolchain = &toolchainDarwinX8664{} 259 260 func darwinX86ToolchainFactory(arch common.Arch) Toolchain { 261 return toolchainDarwinX86Singleton 262 } 263 264 func darwinX8664ToolchainFactory(arch common.Arch) Toolchain { 265 return toolchainDarwinX8664Singleton 266 } 267 268 func init() { 269 registerHostToolchainFactory(common.Darwin, common.X86, darwinX86ToolchainFactory) 270 registerHostToolchainFactory(common.Darwin, common.X86_64, darwinX8664ToolchainFactory) 271 } 272