linkerconfig.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. "android/soong/android"
  17. "android/soong/etc"
  18. "github.com/google/blueprint/proptools"
  19. )
  20. var (
  21. pctx = android.NewPackageContext("android/soong/linkerconfig")
  22. )
  23. func init() {
  24. pctx.HostBinToolVariable("conv_linker_config", "conv_linker_config")
  25. registerLinkerConfigBuildComponent(android.InitRegistrationContext)
  26. }
  27. func registerLinkerConfigBuildComponent(ctx android.RegistrationContext) {
  28. ctx.RegisterModuleType("linker_config", linkerConfigFactory)
  29. }
  30. type linkerConfigProperties struct {
  31. // source linker configuration property file
  32. Src *string `android:"path"`
  33. // If set to true, allow module to be installed to one of the partitions.
  34. // Default value is true.
  35. // Installable should be marked as false for APEX configuration to avoid
  36. // conflicts of configuration on /system/etc directory.
  37. Installable *bool
  38. }
  39. type linkerConfig struct {
  40. android.ModuleBase
  41. properties linkerConfigProperties
  42. outputFilePath android.OutputPath
  43. installDirPath android.InstallPath
  44. }
  45. // Implement PrebuiltEtcModule interface to fit in APEX prebuilt list.
  46. var _ etc.PrebuiltEtcModule = (*linkerConfig)(nil)
  47. func (l *linkerConfig) BaseDir() string {
  48. return "etc"
  49. }
  50. func (l *linkerConfig) SubDir() string {
  51. return ""
  52. }
  53. func (l *linkerConfig) OutputFile() android.OutputPath {
  54. return l.outputFilePath
  55. }
  56. func (l *linkerConfig) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  57. inputFile := android.PathForModuleSrc(ctx, android.String(l.properties.Src))
  58. l.outputFilePath = android.PathForModuleOut(ctx, "linker.config.pb").OutputPath
  59. l.installDirPath = android.PathForModuleInstall(ctx, "etc")
  60. linkerConfigRule := android.NewRuleBuilder(pctx, ctx)
  61. linkerConfigRule.Command().
  62. BuiltTool("conv_linker_config").
  63. Flag("proto").
  64. FlagWithInput("-s ", inputFile).
  65. FlagWithOutput("-o ", l.outputFilePath)
  66. linkerConfigRule.Build("conv_linker_config",
  67. "Generate linker config protobuf "+l.outputFilePath.String())
  68. if proptools.BoolDefault(l.properties.Installable, true) {
  69. ctx.InstallFile(l.installDirPath, l.outputFilePath.Base(), l.outputFilePath)
  70. }
  71. }
  72. // linker_config generates protobuf file from json file. This protobuf file will be used from
  73. // linkerconfig while generating ld.config.txt. Format of this file can be found from
  74. // https://android.googlesource.com/platform/system/linkerconfig/+/master/README.md
  75. func linkerConfigFactory() android.Module {
  76. m := &linkerConfig{}
  77. m.AddProperties(&m.properties)
  78. android.InitAndroidArchModule(m, android.HostAndDeviceSupported, android.MultilibFirst)
  79. return m
  80. }
  81. func (l *linkerConfig) AndroidMkEntries() []android.AndroidMkEntries {
  82. installable := proptools.BoolDefault(l.properties.Installable, true)
  83. return []android.AndroidMkEntries{android.AndroidMkEntries{
  84. Class: "ETC",
  85. OutputFile: android.OptionalPathForPath(l.outputFilePath),
  86. ExtraEntries: []android.AndroidMkExtraEntriesFunc{
  87. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  88. entries.SetString("LOCAL_MODULE_PATH", l.installDirPath.ToMakePath().String())
  89. entries.SetString("LOCAL_INSTALLED_MODULE_STEM", l.outputFilePath.Base())
  90. entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !installable)
  91. entries.SetString("LINKER_CONFIG_PATH_"+l.Name(), l.OutputFile().String())
  92. },
  93. },
  94. }}
  95. }