linkerconfig.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (C) 2020 The Android Open Source Project
  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 linkerconfig
  15. import (
  16. "fmt"
  17. "sort"
  18. "strings"
  19. "android/soong/ui/metrics/bp2build_metrics_proto"
  20. "github.com/google/blueprint/proptools"
  21. "android/soong/android"
  22. "android/soong/bazel"
  23. "android/soong/cc"
  24. "android/soong/etc"
  25. )
  26. var (
  27. pctx = android.NewPackageContext("android/soong/linkerconfig")
  28. )
  29. func init() {
  30. pctx.HostBinToolVariable("conv_linker_config", "conv_linker_config")
  31. registerLinkerConfigBuildComponent(android.InitRegistrationContext)
  32. }
  33. func registerLinkerConfigBuildComponent(ctx android.RegistrationContext) {
  34. ctx.RegisterModuleType("linker_config", LinkerConfigFactory)
  35. }
  36. type linkerConfigProperties struct {
  37. // source linker configuration property file
  38. Src *string `android:"path"`
  39. // If set to true, allow module to be installed to one of the partitions.
  40. // Default value is true.
  41. // Installable should be marked as false for APEX configuration to avoid
  42. // conflicts of configuration on /system/etc directory.
  43. Installable *bool
  44. }
  45. type linkerConfig struct {
  46. android.ModuleBase
  47. android.BazelModuleBase
  48. properties linkerConfigProperties
  49. outputFilePath android.OutputPath
  50. installDirPath android.InstallPath
  51. }
  52. // Implement PrebuiltEtcModule interface to fit in APEX prebuilt list.
  53. var _ etc.PrebuiltEtcModule = (*linkerConfig)(nil)
  54. func (l *linkerConfig) BaseDir() string {
  55. return "etc"
  56. }
  57. func (l *linkerConfig) SubDir() string {
  58. return ""
  59. }
  60. func (l *linkerConfig) OutputFile() android.OutputPath {
  61. return l.outputFilePath
  62. }
  63. var _ android.OutputFileProducer = (*linkerConfig)(nil)
  64. func (l *linkerConfig) OutputFiles(tag string) (android.Paths, error) {
  65. switch tag {
  66. case "":
  67. return android.Paths{l.outputFilePath}, nil
  68. default:
  69. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  70. }
  71. }
  72. func (l *linkerConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  73. input := android.PathForModuleSrc(ctx, android.String(l.properties.Src))
  74. output := android.PathForModuleOut(ctx, "linker.config.pb").OutputPath
  75. builder := android.NewRuleBuilder(pctx, ctx)
  76. BuildLinkerConfig(ctx, builder, input, nil, output)
  77. builder.Build("conv_linker_config", "Generate linker config protobuf "+output.String())
  78. l.outputFilePath = output
  79. l.installDirPath = android.PathForModuleInstall(ctx, "etc")
  80. if !proptools.BoolDefault(l.properties.Installable, true) {
  81. l.SkipInstall()
  82. }
  83. ctx.InstallFile(l.installDirPath, l.outputFilePath.Base(), l.outputFilePath)
  84. }
  85. type linkerConfigAttributes struct {
  86. Src bazel.LabelAttribute
  87. }
  88. func (l *linkerConfig) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
  89. if l.properties.Src == nil {
  90. ctx.PropertyErrorf("src", "empty src is not supported")
  91. ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNSUPPORTED, "")
  92. return
  93. }
  94. src := android.BazelLabelForModuleSrcSingle(ctx, *l.properties.Src)
  95. targetModuleProperties := bazel.BazelTargetModuleProperties{
  96. Rule_class: "linker_config",
  97. Bzl_load_location: "//build/bazel/rules:linker_config.bzl",
  98. }
  99. ctx.CreateBazelTargetModule(
  100. targetModuleProperties,
  101. android.CommonAttributes{Name: l.Name()},
  102. &linkerConfigAttributes{
  103. Src: bazel.LabelAttribute{Value: &src},
  104. })
  105. }
  106. func BuildLinkerConfig(ctx android.ModuleContext, builder *android.RuleBuilder,
  107. input android.Path, otherModules []android.Module, output android.OutputPath) {
  108. // First, convert the input json to protobuf format
  109. interimOutput := android.PathForModuleOut(ctx, "temp.pb")
  110. builder.Command().
  111. BuiltTool("conv_linker_config").
  112. Flag("proto").
  113. FlagWithInput("-s ", input).
  114. FlagWithOutput("-o ", interimOutput)
  115. // Secondly, if there's provideLibs gathered from otherModules, append them
  116. var provideLibs []string
  117. for _, m := range otherModules {
  118. if c, ok := m.(*cc.Module); ok && cc.IsStubTarget(c) {
  119. for _, ps := range c.PackagingSpecs() {
  120. provideLibs = append(provideLibs, ps.FileName())
  121. }
  122. }
  123. }
  124. provideLibs = android.FirstUniqueStrings(provideLibs)
  125. sort.Strings(provideLibs)
  126. if len(provideLibs) > 0 {
  127. builder.Command().
  128. BuiltTool("conv_linker_config").
  129. Flag("append").
  130. FlagWithInput("-s ", interimOutput).
  131. FlagWithOutput("-o ", output).
  132. FlagWithArg("--key ", "provideLibs").
  133. FlagWithArg("--value ", proptools.ShellEscapeIncludingSpaces(strings.Join(provideLibs, " ")))
  134. } else {
  135. // If nothing to add, just cp to the final output
  136. builder.Command().Text("cp").Input(interimOutput).Output(output)
  137. }
  138. builder.Temporary(interimOutput)
  139. builder.DeleteTemporaryFiles()
  140. }
  141. // linker_config generates protobuf file from json file. This protobuf file will be used from
  142. // linkerconfig while generating ld.config.txt. Format of this file can be found from
  143. // https://android.googlesource.com/platform/system/linkerconfig/+/master/README.md
  144. func LinkerConfigFactory() android.Module {
  145. m := &linkerConfig{}
  146. m.AddProperties(&m.properties)
  147. android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibFirst)
  148. android.InitBazelModule(m)
  149. return m
  150. }
  151. func (l *linkerConfig) AndroidMkEntries() []android.AndroidMkEntries {
  152. installable := proptools.BoolDefault(l.properties.Installable, true)
  153. return []android.AndroidMkEntries{android.AndroidMkEntries{
  154. Class: "ETC",
  155. OutputFile: android.OptionalPathForPath(l.outputFilePath),
  156. ExtraEntries: []android.AndroidMkExtraEntriesFunc{
  157. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  158. entries.SetString("LOCAL_MODULE_PATH", l.installDirPath.String())
  159. entries.SetString("LOCAL_INSTALLED_MODULE_STEM", l.outputFilePath.Base())
  160. entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !installable)
  161. },
  162. },
  163. }}
  164. }