| David Brazdil | 08f7ead | 2022-04-25 14:48:14 +0100 | [diff] [blame] | 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 |  | 
 | 15 | package filesystem | 
 | 16 |  | 
 | 17 | import ( | 
| David Brazdil | 08f7ead | 2022-04-25 14:48:14 +0100 | [diff] [blame] | 18 | 	"github.com/google/blueprint" | 
 | 19 | 	"github.com/google/blueprint/proptools" | 
 | 20 |  | 
 | 21 | 	"android/soong/android" | 
 | 22 | ) | 
 | 23 |  | 
 | 24 | var ( | 
 | 25 | 	toRawBinary = pctx.AndroidStaticRule("toRawBinary", | 
 | 26 | 		blueprint.RuleParams{ | 
| Andrew Walbran | 79c3b77 | 2022-05-24 13:52:58 +0000 | [diff] [blame] | 27 | 			Command: "${objcopy} --output-target=binary ${in} ${out} &&" + | 
 | 28 | 				"chmod -x ${out}", | 
| David Brazdil | 08f7ead | 2022-04-25 14:48:14 +0100 | [diff] [blame] | 29 | 			CommandDeps: []string{"$objcopy"}, | 
 | 30 | 		}, | 
 | 31 | 		"objcopy") | 
 | 32 | ) | 
 | 33 |  | 
 | 34 | func init() { | 
 | 35 | 	pctx.Import("android/soong/cc/config") | 
 | 36 |  | 
 | 37 | 	android.RegisterModuleType("raw_binary", rawBinaryFactory) | 
 | 38 | } | 
 | 39 |  | 
 | 40 | type rawBinary struct { | 
 | 41 | 	android.ModuleBase | 
 | 42 |  | 
 | 43 | 	properties rawBinaryProperties | 
 | 44 |  | 
 | 45 | 	output     android.OutputPath | 
 | 46 | 	installDir android.InstallPath | 
 | 47 | } | 
 | 48 |  | 
 | 49 | type 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 |  | 
 | 57 | func rawBinaryFactory() android.Module { | 
 | 58 | 	module := &rawBinary{} | 
 | 59 | 	module.AddProperties(&module.properties) | 
 | 60 | 	android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) | 
 | 61 | 	return module | 
 | 62 | } | 
 | 63 |  | 
 | 64 | func (r *rawBinary) DepsMutator(ctx android.BottomUpMutatorContext) { | 
 | 65 | 	// do nothing | 
 | 66 | } | 
 | 67 |  | 
 | 68 | func (r *rawBinary) installFileName() string { | 
 | 69 | 	return proptools.StringDefault(r.properties.Stem, r.BaseModuleName()+".bin") | 
 | 70 | } | 
 | 71 |  | 
 | 72 | func (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 Walbran | 79c3b77 | 2022-05-24 13:52:58 +0000 | [diff] [blame] | 78 | 		Description: "raw binary " + outputFile.Base(), | 
| David Brazdil | 08f7ead | 2022-04-25 14:48:14 +0100 | [diff] [blame] | 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) | 
| mrziwang | 555d133 | 2024-06-07 11:15:33 -0700 | [diff] [blame] | 89 |  | 
 | 90 | 	ctx.SetOutputFiles([]android.Path{r.output}, "") | 
| David Brazdil | 08f7ead | 2022-04-25 14:48:14 +0100 | [diff] [blame] | 91 | } | 
 | 92 |  | 
 | 93 | var _ android.AndroidMkEntriesProvider = (*rawBinary)(nil) | 
 | 94 |  | 
 | 95 | // Implements android.AndroidMkEntriesProvider | 
 | 96 | func (r *rawBinary) AndroidMkEntries() []android.AndroidMkEntries { | 
| Andrew Walbran | 79c3b77 | 2022-05-24 13:52:58 +0000 | [diff] [blame] | 97 | 	return []android.AndroidMkEntries{{ | 
| David Brazdil | 08f7ead | 2022-04-25 14:48:14 +0100 | [diff] [blame] | 98 | 		Class:      "ETC", | 
 | 99 | 		OutputFile: android.OptionalPathForPath(r.output), | 
 | 100 | 	}} | 
 | 101 | } | 
 | 102 |  | 
 | 103 | var _ Filesystem = (*rawBinary)(nil) | 
 | 104 |  | 
 | 105 | func (r *rawBinary) OutputPath() android.Path { | 
 | 106 | 	return r.output | 
 | 107 | } | 
 | 108 |  | 
 | 109 | func (r *rawBinary) SignedOutputPath() android.Path { | 
 | 110 | 	return nil | 
 | 111 | } |