| 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 ( | 
|  | 18 | "fmt" | 
|  | 19 |  | 
|  | 20 | "github.com/google/blueprint" | 
|  | 21 | "github.com/google/blueprint/proptools" | 
|  | 22 |  | 
|  | 23 | "android/soong/android" | 
|  | 24 | ) | 
|  | 25 |  | 
|  | 26 | var ( | 
|  | 27 | toRawBinary = pctx.AndroidStaticRule("toRawBinary", | 
|  | 28 | blueprint.RuleParams{ | 
| Andrew Walbran | 79c3b77 | 2022-05-24 13:52:58 +0000 | [diff] [blame] | 29 | Command: "${objcopy} --output-target=binary ${in} ${out} &&" + | 
|  | 30 | "chmod -x ${out}", | 
| David Brazdil | 08f7ead | 2022-04-25 14:48:14 +0100 | [diff] [blame] | 31 | CommandDeps: []string{"$objcopy"}, | 
|  | 32 | }, | 
|  | 33 | "objcopy") | 
|  | 34 | ) | 
|  | 35 |  | 
|  | 36 | func init() { | 
|  | 37 | pctx.Import("android/soong/cc/config") | 
|  | 38 |  | 
|  | 39 | android.RegisterModuleType("raw_binary", rawBinaryFactory) | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | type rawBinary struct { | 
|  | 43 | android.ModuleBase | 
|  | 44 |  | 
|  | 45 | properties rawBinaryProperties | 
|  | 46 |  | 
|  | 47 | output     android.OutputPath | 
|  | 48 | installDir android.InstallPath | 
|  | 49 | } | 
|  | 50 |  | 
|  | 51 | type 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 |  | 
|  | 59 | func rawBinaryFactory() android.Module { | 
|  | 60 | module := &rawBinary{} | 
|  | 61 | module.AddProperties(&module.properties) | 
|  | 62 | android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst) | 
|  | 63 | return module | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | func (r *rawBinary) DepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 67 | // do nothing | 
|  | 68 | } | 
|  | 69 |  | 
|  | 70 | func (r *rawBinary) installFileName() string { | 
|  | 71 | return proptools.StringDefault(r.properties.Stem, r.BaseModuleName()+".bin") | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | func (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, | 
| Andrew Walbran | 79c3b77 | 2022-05-24 13:52:58 +0000 | [diff] [blame] | 80 | Description: "raw binary " + outputFile.Base(), | 
| David Brazdil | 08f7ead | 2022-04-25 14:48:14 +0100 | [diff] [blame] | 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 |  | 
|  | 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 | } | 
|  | 112 |  | 
|  | 113 | var _ android.OutputFileProducer = (*rawBinary)(nil) | 
|  | 114 |  | 
|  | 115 | // Implements android.OutputFileProducer | 
|  | 116 | func (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 | } |