linkerconfig.go 5.3 KB

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