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 15package config 16 17import ( 18 "fmt" 19 "os/exec" 20 "path/filepath" 21 "strings" 22 "sync" 23 24 "android/soong/android" 25) 26 27var ( 28 darwinCflags = []string{ 29 "-fdiagnostics-color", 30 31 "-fPIC", 32 "-funwind-tables", 33 34 // Workaround differences in inttypes.h between host and target. 35 //See bug 12708004. 36 "-D__STDC_FORMAT_MACROS", 37 "-D__STDC_CONSTANT_MACROS", 38 39 "-isysroot ${macSdkRoot}", 40 "-mmacosx-version-min=${macMinVersion}", 41 "-DMACOSX_DEPLOYMENT_TARGET=${macMinVersion}", 42 43 "-m64", 44 } 45 46 darwinLdflags = []string{ 47 "-isysroot ${macSdkRoot}", 48 "-Wl,-syslibroot,${macSdkRoot}", 49 "-mmacosx-version-min=${macMinVersion}", 50 "-m64", 51 } 52 53 darwinClangCflags = append(ClangFilterUnknownCflags(darwinCflags), []string{ 54 "-integrated-as", 55 "-fstack-protector-strong", 56 }...) 57 58 darwinClangLdflags = ClangFilterUnknownCflags(darwinLdflags) 59 60 darwinClangLldflags = ClangFilterUnknownLldflags(darwinClangLdflags) 61 62 darwinSupportedSdkVersions = []string{ 63 "10.10", 64 "10.11", 65 "10.12", 66 "10.13", 67 "10.14", 68 "10.15", 69 "11.0", 70 } 71 72 darwinAvailableLibraries = append( 73 addPrefix([]string{ 74 "c", 75 "dl", 76 "m", 77 "ncurses", 78 "objc", 79 "pthread", 80 }, "-l"), 81 "-framework AppKit", 82 "-framework CoreFoundation", 83 "-framework Foundation", 84 "-framework IOKit", 85 "-framework Security", 86 "-framework SystemConfiguration", 87 ) 88) 89 90const ( 91 darwinGccVersion = "4.2.1" 92) 93 94func init() { 95 pctx.VariableFunc("macSdkRoot", func(ctx android.PackageVarContext) string { 96 return getMacTools(ctx).sdkRoot 97 }) 98 pctx.StaticVariable("macMinVersion", "10.10") 99 pctx.VariableFunc("MacArPath", func(ctx android.PackageVarContext) string { 100 return getMacTools(ctx).arPath 101 }) 102 103 pctx.VariableFunc("MacStripPath", func(ctx android.PackageVarContext) string { 104 return getMacTools(ctx).stripPath 105 }) 106 107 pctx.VariableFunc("MacToolPath", func(ctx android.PackageVarContext) string { 108 return getMacTools(ctx).toolPath 109 }) 110 111 pctx.StaticVariable("DarwinGccVersion", darwinGccVersion) 112 pctx.SourcePathVariable("DarwinGccRoot", 113 "prebuilts/gcc/${HostPrebuiltTag}/host/i686-apple-darwin-${DarwinGccVersion}") 114 115 pctx.StaticVariable("DarwinGccTriple", "i686-apple-darwin11") 116 117 pctx.StaticVariable("DarwinClangCflags", strings.Join(darwinClangCflags, " ")) 118 pctx.StaticVariable("DarwinClangLdflags", strings.Join(darwinClangLdflags, " ")) 119 pctx.StaticVariable("DarwinClangLldflags", strings.Join(darwinClangLldflags, " ")) 120 121 pctx.StaticVariable("DarwinYasmFlags", "-f macho -m amd64") 122} 123 124type macPlatformTools struct { 125 once sync.Once 126 err error 127 128 sdkRoot string 129 arPath string 130 stripPath string 131 toolPath string 132} 133 134var macTools = &macPlatformTools{} 135 136func getMacTools(ctx android.PackageVarContext) *macPlatformTools { 137 macTools.once.Do(func() { 138 xcrunTool := ctx.Config().HostSystemTool("xcrun") 139 140 xcrun := func(args ...string) string { 141 if macTools.err != nil { 142 return "" 143 } 144 145 bytes, err := exec.Command(xcrunTool, args...).Output() 146 if err != nil { 147 macTools.err = fmt.Errorf("xcrun %q failed with: %q", args, err) 148 return "" 149 } 150 151 return strings.TrimSpace(string(bytes)) 152 } 153 154 xcrunSdk := func(arg string) string { 155 if selected := ctx.Config().Getenv("MAC_SDK_VERSION"); selected != "" { 156 if !inList(selected, darwinSupportedSdkVersions) { 157 macTools.err = fmt.Errorf("MAC_SDK_VERSION %s isn't supported: %q", selected, darwinSupportedSdkVersions) 158 return "" 159 } 160 161 return xcrun("--sdk", "macosx"+selected, arg) 162 } 163 164 for _, sdk := range darwinSupportedSdkVersions { 165 bytes, err := exec.Command(xcrunTool, "--sdk", "macosx"+sdk, arg).Output() 166 if err == nil { 167 return strings.TrimSpace(string(bytes)) 168 } 169 } 170 macTools.err = fmt.Errorf("Could not find a supported mac sdk: %q", darwinSupportedSdkVersions) 171 return "" 172 } 173 174 macTools.sdkRoot = xcrunSdk("--show-sdk-path") 175 176 macTools.arPath = xcrun("--find", "ar") 177 macTools.stripPath = xcrun("--find", "strip") 178 macTools.toolPath = filepath.Dir(xcrun("--find", "ld")) 179 }) 180 if macTools.err != nil { 181 ctx.Errorf("%q", macTools.err) 182 } 183 return macTools 184} 185 186type toolchainDarwin struct { 187 cFlags, ldFlags string 188 toolchain64Bit 189} 190 191func (t *toolchainDarwin) Name() string { 192 return "x86_64" 193} 194 195func (t *toolchainDarwin) GccRoot() string { 196 return "${config.DarwinGccRoot}" 197} 198 199func (t *toolchainDarwin) GccTriple() string { 200 return "${config.DarwinGccTriple}" 201} 202 203func (t *toolchainDarwin) GccVersion() string { 204 return darwinGccVersion 205} 206 207func (t *toolchainDarwin) IncludeFlags() string { 208 return "" 209} 210 211func (t *toolchainDarwin) ClangTriple() string { 212 return "x86_64-apple-darwin" 213} 214 215func (t *toolchainDarwin) ClangCflags() string { 216 return "${config.DarwinClangCflags}" 217} 218 219func (t *toolchainDarwin) ClangCppflags() string { 220 return "" 221} 222 223func (t *toolchainDarwin) ClangLdflags() string { 224 return "${config.DarwinClangLdflags}" 225} 226 227func (t *toolchainDarwin) ClangLldflags() string { 228 return "${config.DarwinClangLldflags}" 229} 230 231func (t *toolchainDarwin) YasmFlags() string { 232 return "${config.DarwinYasmFlags}" 233} 234 235func (t *toolchainDarwin) ShlibSuffix() string { 236 return ".dylib" 237} 238 239func (t *toolchainDarwin) AvailableLibraries() []string { 240 return darwinAvailableLibraries 241} 242 243func (t *toolchainDarwin) Bionic() bool { 244 return false 245} 246 247func (t *toolchainDarwin) ToolPath() string { 248 return "${config.MacToolPath}" 249} 250 251var toolchainDarwinSingleton Toolchain = &toolchainDarwin{} 252 253func darwinToolchainFactory(arch android.Arch) Toolchain { 254 return toolchainDarwinSingleton 255} 256 257func init() { 258 registerToolchainFactory(android.Darwin, android.X86_64, darwinToolchainFactory) 259} 260