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
15package filesystem
16
17import (
18	"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{
27			Command: "${objcopy} --output-target=binary ${in} ${out} &&" +
28				"chmod -x ${out}",
29			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,
78		Description: "raw binary " + outputFile.Base(),
79		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)
89
90	ctx.SetOutputFiles([]android.Path{r.output}, "")
91}
92
93var _ android.AndroidMkEntriesProvider = (*rawBinary)(nil)
94
95// Implements android.AndroidMkEntriesProvider
96func (r *rawBinary) AndroidMkEntries() []android.AndroidMkEntries {
97	return []android.AndroidMkEntries{{
98		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}
112