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