binary_sdk_member.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2020 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 cc
  15. import (
  16. "path/filepath"
  17. "android/soong/android"
  18. "github.com/google/blueprint"
  19. "github.com/google/blueprint/proptools"
  20. )
  21. func init() {
  22. android.RegisterSdkMemberType(ccBinarySdkMemberType)
  23. }
  24. var ccBinarySdkMemberType = &binarySdkMemberType{
  25. SdkMemberTypeBase: android.SdkMemberTypeBase{
  26. PropertyName: "native_binaries",
  27. HostOsDependent: true,
  28. },
  29. }
  30. type binarySdkMemberType struct {
  31. android.SdkMemberTypeBase
  32. }
  33. func (mt *binarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
  34. targets := ctx.MultiTargets()
  35. for _, bin := range names {
  36. for _, target := range targets {
  37. variations := target.Variations()
  38. if ctx.Device() {
  39. variations = append(variations,
  40. blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
  41. }
  42. ctx.AddFarVariationDependencies(variations, dependencyTag, bin)
  43. }
  44. }
  45. }
  46. func (mt *binarySdkMemberType) IsInstance(module android.Module) bool {
  47. // Check the module to see if it can be used with this module type.
  48. if m, ok := module.(*Module); ok {
  49. for _, allowableMemberType := range m.sdkMemberTypes {
  50. if allowableMemberType == mt {
  51. return true
  52. }
  53. }
  54. }
  55. return false
  56. }
  57. func (mt *binarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
  58. pbm := ctx.SnapshotBuilder().AddPrebuiltModule(member, "cc_prebuilt_binary")
  59. ccModule := member.Variants()[0].(*Module)
  60. if stl := ccModule.stl.Properties.Stl; stl != nil {
  61. pbm.AddProperty("stl", proptools.String(stl))
  62. }
  63. return pbm
  64. }
  65. func (mt *binarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
  66. return &nativeBinaryInfoProperties{}
  67. }
  68. const (
  69. nativeBinaryDir = "bin"
  70. )
  71. // path to the native binary. Relative to <sdk_root>/<api_dir>
  72. func nativeBinaryPathFor(lib nativeBinaryInfoProperties) string {
  73. return filepath.Join(lib.OsPrefix(), lib.archType,
  74. nativeBinaryDir, lib.outputFile.Base())
  75. }
  76. // nativeBinaryInfoProperties represents properties of a native binary
  77. //
  78. // The exported (capitalized) fields will be examined and may be changed during common value extraction.
  79. // The unexported fields will be left untouched.
  80. type nativeBinaryInfoProperties struct {
  81. android.SdkMemberPropertiesBase
  82. // archType is not exported as if set (to a non default value) it is always arch specific.
  83. // This is "" for common properties.
  84. archType string
  85. // outputFile is not exported as it is always arch specific.
  86. outputFile android.Path
  87. // The set of shared libraries
  88. //
  89. // This field is exported as its contents may not be arch specific.
  90. SharedLibs []string
  91. // The set of system shared libraries
  92. //
  93. // This field is exported as its contents may not be arch specific.
  94. SystemSharedLibs []string
  95. // Arch specific flags.
  96. StaticExecutable bool
  97. Nocrt bool
  98. }
  99. func (p *nativeBinaryInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
  100. ccModule := variant.(*Module)
  101. p.archType = ccModule.Target().Arch.ArchType.String()
  102. p.outputFile = getRequiredMemberOutputFile(ctx, ccModule)
  103. binaryLinker := ccModule.linker.(*binaryDecorator)
  104. p.StaticExecutable = binaryLinker.static()
  105. p.Nocrt = Bool(binaryLinker.baseLinker.Properties.Nocrt)
  106. if ccModule.linker != nil {
  107. specifiedDeps := specifiedDeps{}
  108. specifiedDeps = ccModule.linker.linkerSpecifiedDeps(specifiedDeps)
  109. p.SharedLibs = specifiedDeps.sharedLibs
  110. p.SystemSharedLibs = specifiedDeps.systemSharedLibs
  111. }
  112. }
  113. func (p *nativeBinaryInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
  114. builder := ctx.SnapshotBuilder()
  115. if p.outputFile != nil {
  116. propertySet.AddProperty("srcs", []string{nativeBinaryPathFor(*p)})
  117. builder.CopyToSnapshot(p.outputFile, nativeBinaryPathFor(*p))
  118. }
  119. if len(p.SharedLibs) > 0 {
  120. propertySet.AddPropertyWithTag("shared_libs", p.SharedLibs, builder.SdkMemberReferencePropertyTag(false))
  121. }
  122. // SystemSharedLibs needs to be propagated if it's a list, even if it's empty,
  123. // so check for non-nil instead of nonzero length.
  124. if p.SystemSharedLibs != nil {
  125. propertySet.AddPropertyWithTag("system_shared_libs", p.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
  126. }
  127. if p.StaticExecutable {
  128. propertySet.AddProperty("static_executable", p.StaticExecutable)
  129. }
  130. if p.Nocrt {
  131. propertySet.AddProperty("nocrt", p.Nocrt)
  132. }
  133. }