x86_darwin_host.go 5.8 KB

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