• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	"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				"chmod -x ${out}",
31			CommandDeps: []string{"$objcopy"},
32		},
33		"objcopy")
34)
35
36func init() {
37	pctx.Import("android/soong/cc/config")
38
39	android.RegisterModuleType("raw_binary", rawBinaryFactory)
40}
41
42type rawBinary struct {
43	android.ModuleBase
44
45	properties rawBinaryProperties
46
47	output     android.OutputPath
48	installDir android.InstallPath
49}
50
51type rawBinaryProperties struct {
52	// Set the name of the output. Defaults to <module_name>.bin.
53	Stem *string
54
55	// Name of input executable. Can be a name of a target.
56	Src *string `android:"path,arch_variant"`
57}
58
59func rawBinaryFactory() android.Module {
60	module := &rawBinary{}
61	module.AddProperties(&module.properties)
62	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
63	return module
64}
65
66func (r *rawBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
67	// do nothing
68}
69
70func (r *rawBinary) installFileName() string {
71	return proptools.StringDefault(r.properties.Stem, r.BaseModuleName()+".bin")
72}
73
74func (r *rawBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
75	inputFile := android.PathForModuleSrc(ctx, proptools.String(r.properties.Src))
76	outputFile := android.PathForModuleOut(ctx, r.installFileName()).OutputPath
77
78	ctx.Build(pctx, android.BuildParams{
79		Rule:        toRawBinary,
80		Description: "raw binary " + outputFile.Base(),
81		Output:      outputFile,
82		Input:       inputFile,
83		Args: map[string]string{
84			"objcopy": "${config.ClangBin}/llvm-objcopy",
85		},
86	})
87
88	r.output = outputFile
89	r.installDir = android.PathForModuleInstall(ctx, "etc")
90	ctx.InstallFile(r.installDir, r.installFileName(), 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
113var _ android.OutputFileProducer = (*rawBinary)(nil)
114
115// Implements android.OutputFileProducer
116func (r *rawBinary) OutputFiles(tag string) (android.Paths, error) {
117	if tag == "" {
118		return []android.Path{r.output}, nil
119	}
120	return nil, fmt.Errorf("unsupported module reference tag %q", tag)
121}
122