blob: ad36c293501ef5839cbf08ce9e052dd61738b028 [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 (
David Brazdil08f7ead2022-04-25 14:48:14 +010018 "github.com/google/blueprint"
19 "github.com/google/blueprint/proptools"
20
21 "android/soong/android"
22)
23
24var (
25 toRawBinary = pctx.AndroidStaticRule("toRawBinary",
26 blueprint.RuleParams{
Andrew Walbran79c3b772022-05-24 13:52:58 +000027 Command: "${objcopy} --output-target=binary ${in} ${out} &&" +
28 "chmod -x ${out}",
David Brazdil08f7ead2022-04-25 14:48:14 +010029 CommandDeps: []string{"$objcopy"},
30 },
31 "objcopy")
32)
33
34func init() {
35 pctx.Import("android/soong/cc/config")
36
37 android.RegisterModuleType("raw_binary", rawBinaryFactory)
38}
39
40type rawBinary struct {
41 android.ModuleBase
42
43 properties rawBinaryProperties
44
45 output android.OutputPath
46 installDir android.InstallPath
47}
48
49type rawBinaryProperties struct {
50 // Set the name of the output. Defaults to <module_name>.bin.
51 Stem *string
52
53 // Name of input executable. Can be a name of a target.
54 Src *string `android:"path,arch_variant"`
55}
56
57func rawBinaryFactory() android.Module {
58 module := &rawBinary{}
59 module.AddProperties(&module.properties)
60 android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
61 return module
62}
63
64func (r *rawBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
65 // do nothing
66}
67
68func (r *rawBinary) installFileName() string {
69 return proptools.StringDefault(r.properties.Stem, r.BaseModuleName()+".bin")
70}
71
72func (r *rawBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
73 inputFile := android.PathForModuleSrc(ctx, proptools.String(r.properties.Src))
74 outputFile := android.PathForModuleOut(ctx, r.installFileName()).OutputPath
75
76 ctx.Build(pctx, android.BuildParams{
77 Rule: toRawBinary,
Andrew Walbran79c3b772022-05-24 13:52:58 +000078 Description: "raw binary " + outputFile.Base(),
David Brazdil08f7ead2022-04-25 14:48:14 +010079 Output: outputFile,
80 Input: inputFile,
81 Args: map[string]string{
82 "objcopy": "${config.ClangBin}/llvm-objcopy",
83 },
84 })
85
86 r.output = outputFile
87 r.installDir = android.PathForModuleInstall(ctx, "etc")
88 ctx.InstallFile(r.installDir, r.installFileName(), r.output)
mrziwang555d1332024-06-07 11:15:33 -070089
90 ctx.SetOutputFiles([]android.Path{r.output}, "")
David Brazdil08f7ead2022-04-25 14:48:14 +010091}
92
93var _ android.AndroidMkEntriesProvider = (*rawBinary)(nil)
94
95// Implements android.AndroidMkEntriesProvider
96func (r *rawBinary) AndroidMkEntries() []android.AndroidMkEntries {
Andrew Walbran79c3b772022-05-24 13:52:58 +000097 return []android.AndroidMkEntries{{
David Brazdil08f7ead2022-04-25 14:48:14 +010098 Class: "ETC",
99 OutputFile: android.OptionalPathForPath(r.output),
100 }}
101}
102
103var _ Filesystem = (*rawBinary)(nil)
104
105func (r *rawBinary) OutputPath() android.Path {
106 return r.output
107}
108
109func (r *rawBinary) SignedOutputPath() android.Path {
110 return nil
111}