raw_binary.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (C) 2022 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 filesystem
  15. import (
  16. "fmt"
  17. "github.com/google/blueprint"
  18. "github.com/google/blueprint/proptools"
  19. "android/soong/android"
  20. )
  21. var (
  22. toRawBinary = pctx.AndroidStaticRule("toRawBinary",
  23. blueprint.RuleParams{
  24. Command: "${objcopy} --output-target=binary ${in} ${out} &&" +
  25. "chmod -x ${out}",
  26. CommandDeps: []string{"$objcopy"},
  27. },
  28. "objcopy")
  29. )
  30. func init() {
  31. pctx.Import("android/soong/cc/config")
  32. android.RegisterModuleType("raw_binary", rawBinaryFactory)
  33. }
  34. type rawBinary struct {
  35. android.ModuleBase
  36. properties rawBinaryProperties
  37. output android.OutputPath
  38. installDir android.InstallPath
  39. }
  40. type rawBinaryProperties struct {
  41. // Set the name of the output. Defaults to <module_name>.bin.
  42. Stem *string
  43. // Name of input executable. Can be a name of a target.
  44. Src *string `android:"path,arch_variant"`
  45. }
  46. func rawBinaryFactory() android.Module {
  47. module := &rawBinary{}
  48. module.AddProperties(&module.properties)
  49. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
  50. return module
  51. }
  52. func (r *rawBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
  53. // do nothing
  54. }
  55. func (r *rawBinary) installFileName() string {
  56. return proptools.StringDefault(r.properties.Stem, r.BaseModuleName()+".bin")
  57. }
  58. func (r *rawBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  59. inputFile := android.PathForModuleSrc(ctx, proptools.String(r.properties.Src))
  60. outputFile := android.PathForModuleOut(ctx, r.installFileName()).OutputPath
  61. ctx.Build(pctx, android.BuildParams{
  62. Rule: toRawBinary,
  63. Description: "raw binary " + outputFile.Base(),
  64. Output: outputFile,
  65. Input: inputFile,
  66. Args: map[string]string{
  67. "objcopy": "${config.ClangBin}/llvm-objcopy",
  68. },
  69. })
  70. r.output = outputFile
  71. r.installDir = android.PathForModuleInstall(ctx, "etc")
  72. ctx.InstallFile(r.installDir, r.installFileName(), r.output)
  73. }
  74. var _ android.AndroidMkEntriesProvider = (*rawBinary)(nil)
  75. // Implements android.AndroidMkEntriesProvider
  76. func (r *rawBinary) AndroidMkEntries() []android.AndroidMkEntries {
  77. return []android.AndroidMkEntries{{
  78. Class: "ETC",
  79. OutputFile: android.OptionalPathForPath(r.output),
  80. }}
  81. }
  82. var _ Filesystem = (*rawBinary)(nil)
  83. func (r *rawBinary) OutputPath() android.Path {
  84. return r.output
  85. }
  86. func (r *rawBinary) SignedOutputPath() android.Path {
  87. return nil
  88. }
  89. var _ android.OutputFileProducer = (*rawBinary)(nil)
  90. // Implements android.OutputFileProducer
  91. func (r *rawBinary) OutputFiles(tag string) (android.Paths, error) {
  92. if tag == "" {
  93. return []android.Path{r.output}, nil
  94. }
  95. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  96. }