aboutsummaryrefslogtreecommitdiff
path: root/filesystem/raw_binary.go
blob: 1544ea7885267d4d25b46aed41ece41ad85f1948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2022 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package filesystem

import (
	"fmt"

	"github.com/google/blueprint"
	"github.com/google/blueprint/proptools"

	"android/soong/android"
)

var (
	toRawBinary = pctx.AndroidStaticRule("toRawBinary",
		blueprint.RuleParams{
			Command: "${objcopy} --output-target=binary ${in} ${out} &&" +
				"chmod -x ${out}",
			CommandDeps: []string{"$objcopy"},
		},
		"objcopy")
)

func init() {
	pctx.Import("android/soong/cc/config")

	android.RegisterModuleType("raw_binary", rawBinaryFactory)
}

type rawBinary struct {
	android.ModuleBase

	properties rawBinaryProperties

	output     android.OutputPath
	installDir android.InstallPath
}

type rawBinaryProperties struct {
	// Set the name of the output. Defaults to <module_name>.bin.
	Stem *string

	// Name of input executable. Can be a name of a target.
	Src *string `android:"path,arch_variant"`
}

func rawBinaryFactory() android.Module {
	module := &rawBinary{}
	module.AddProperties(&module.properties)
	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
	return module
}

func (r *rawBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
	// do nothing
}

func (r *rawBinary) installFileName() string {
	return proptools.StringDefault(r.properties.Stem, r.BaseModuleName()+".bin")
}

func (r *rawBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
	inputFile := android.PathForModuleSrc(ctx, proptools.String(r.properties.Src))
	outputFile := android.PathForModuleOut(ctx, r.installFileName()).OutputPath

	ctx.Build(pctx, android.BuildParams{
		Rule:        toRawBinary,
		Description: "raw binary " + outputFile.Base(),
		Output:      outputFile,
		Input:       inputFile,
		Args: map[string]string{
			"objcopy": "${config.ClangBin}/llvm-objcopy",
		},
	})

	r.output = outputFile
	r.installDir = android.PathForModuleInstall(ctx, "etc")
	ctx.InstallFile(r.installDir, r.installFileName(), r.output)
}

var _ android.AndroidMkEntriesProvider = (*rawBinary)(nil)

// Implements android.AndroidMkEntriesProvider
func (r *rawBinary) AndroidMkEntries() []android.AndroidMkEntries {
	return []android.AndroidMkEntries{{
		Class:      "ETC",
		OutputFile: android.OptionalPathForPath(r.output),
	}}
}

var _ Filesystem = (*rawBinary)(nil)

func (r *rawBinary) OutputPath() android.Path {
	return r.output
}

func (r *rawBinary) SignedOutputPath() android.Path {
	return nil
}

var _ android.OutputFileProducer = (*rawBinary)(nil)

// Implements android.OutputFileProducer
func (r *rawBinary) OutputFiles(tag string) (android.Paths, error) {
	if tag == "" {
		return []android.Path{r.output}, nil
	}
	return nil, fmt.Errorf("unsupported module reference tag %q", tag)
}