Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 1 | // Copyright 2019 Google Inc. All rights reserved. |
| 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 android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 19 | "strings" |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | // sh_binary is for shell scripts (and batch files) that are installed as |
| 23 | // executable files into .../bin/ |
| 24 | // |
| 25 | // Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary |
| 26 | // instead. |
| 27 | |
| 28 | func init() { |
| 29 | RegisterModuleType("sh_binary", ShBinaryFactory) |
| 30 | RegisterModuleType("sh_binary_host", ShBinaryHostFactory) |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 31 | RegisterModuleType("sh_test", ShTestFactory) |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 32 | RegisterModuleType("sh_test_host", ShTestHostFactory) |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | type shBinaryProperties struct { |
| 36 | // Source file of this prebuilt. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 37 | Src *string `android:"path,arch_variant"` |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 38 | |
| 39 | // optional subdirectory under which this file is installed into |
| 40 | Sub_dir *string `android:"arch_variant"` |
| 41 | |
| 42 | // optional name for the installed file. If unspecified, name of the module is used as the file name |
| 43 | Filename *string `android:"arch_variant"` |
| 44 | |
| 45 | // when set to true, and filename property is not set, the name for the installed file |
| 46 | // is the same as the file name of the source file. |
| 47 | Filename_from_src *bool `android:"arch_variant"` |
| 48 | |
| 49 | // Whether this module is directly installable to one of the partitions. Default: true. |
| 50 | Installable *bool |
| 51 | } |
| 52 | |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 53 | type TestProperties struct { |
| 54 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 55 | // installed into. |
| 56 | Test_suites []string `android:"arch_variant"` |
| 57 | |
| 58 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 59 | // installed with the module. |
| 60 | Test_config *string `android:"arch_variant"` |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 61 | |
| 62 | // list of files or filegroup modules that provide data that should be installed alongside |
| 63 | // the test. |
| 64 | Data []string `android:"path,arch_variant"` |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 67 | type ShBinary struct { |
| 68 | ModuleBase |
| 69 | |
| 70 | properties shBinaryProperties |
| 71 | |
| 72 | sourceFilePath Path |
| 73 | outputFilePath OutputPath |
| 74 | } |
| 75 | |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 76 | type ShTest struct { |
| 77 | ShBinary |
| 78 | |
| 79 | testProperties TestProperties |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 80 | |
| 81 | data Paths |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 84 | func (s *ShBinary) DepsMutator(ctx BottomUpMutatorContext) { |
| 85 | if s.properties.Src == nil { |
| 86 | ctx.PropertyErrorf("src", "missing prebuilt source file") |
| 87 | } |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | func (s *ShBinary) SourceFilePath(ctx ModuleContext) Path { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 91 | return PathForModuleSrc(ctx, String(s.properties.Src)) |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func (s *ShBinary) OutputFile() OutputPath { |
| 95 | return s.outputFilePath |
| 96 | } |
| 97 | |
| 98 | func (s *ShBinary) SubDir() string { |
| 99 | return String(s.properties.Sub_dir) |
| 100 | } |
| 101 | |
| 102 | func (s *ShBinary) Installable() bool { |
| 103 | return s.properties.Installable == nil || Bool(s.properties.Installable) |
| 104 | } |
| 105 | |
| 106 | func (s *ShBinary) GenerateAndroidBuildActions(ctx ModuleContext) { |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 107 | s.sourceFilePath = PathForModuleSrc(ctx, String(s.properties.Src)) |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 108 | filename := String(s.properties.Filename) |
| 109 | filename_from_src := Bool(s.properties.Filename_from_src) |
| 110 | if filename == "" { |
| 111 | if filename_from_src { |
| 112 | filename = s.sourceFilePath.Base() |
| 113 | } else { |
| 114 | filename = ctx.ModuleName() |
| 115 | } |
| 116 | } else if filename_from_src { |
| 117 | ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true") |
| 118 | return |
| 119 | } |
| 120 | s.outputFilePath = PathForModuleOut(ctx, filename).OutputPath |
| 121 | |
| 122 | // This ensures that outputFilePath has the correct name for others to |
| 123 | // use, as the source file may have a different name. |
| 124 | ctx.Build(pctx, BuildParams{ |
| 125 | Rule: CpExecutable, |
| 126 | Output: s.outputFilePath, |
| 127 | Input: s.sourceFilePath, |
| 128 | }) |
| 129 | } |
| 130 | |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 131 | func (s *ShBinary) AndroidMkEntries() AndroidMkEntries { |
| 132 | return AndroidMkEntries{ |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 133 | Class: "EXECUTABLES", |
| 134 | OutputFile: OptionalPathForPath(s.outputFilePath), |
| 135 | Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk", |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame^] | 136 | ExtraEntries: []AndroidMkExtraEntriesFunc{ |
| 137 | func(entries *AndroidMkEntries) { |
| 138 | s.customAndroidMkEntries(entries) |
| 139 | }, |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 140 | }, |
| 141 | } |
| 142 | } |
| 143 | |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 144 | func (s *ShBinary) customAndroidMkEntries(entries *AndroidMkEntries) { |
| 145 | entries.SetString("LOCAL_MODULE_RELATIVE_PATH", String(s.properties.Sub_dir)) |
| 146 | entries.SetString("LOCAL_MODULE_SUFFIX", "") |
| 147 | entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel()) |
| 148 | } |
| 149 | |
| 150 | func (s *ShTest) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 151 | s.ShBinary.GenerateAndroidBuildActions(ctx) |
| 152 | |
| 153 | s.data = PathsForModuleSrc(ctx, s.testProperties.Data) |
| 154 | } |
| 155 | |
| 156 | func (s *ShTest) AndroidMkEntries() AndroidMkEntries { |
| 157 | return AndroidMkEntries{ |
| 158 | Class: "NATIVE_TESTS", |
| 159 | OutputFile: OptionalPathForPath(s.outputFilePath), |
| 160 | Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk", |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame^] | 161 | ExtraEntries: []AndroidMkExtraEntriesFunc{ |
| 162 | func(entries *AndroidMkEntries) { |
| 163 | s.customAndroidMkEntries(entries) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 164 | |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame^] | 165 | entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", s.testProperties.Test_suites...) |
| 166 | entries.SetString("LOCAL_TEST_CONFIG", String(s.testProperties.Test_config)) |
| 167 | for _, d := range s.data { |
| 168 | rel := d.Rel() |
| 169 | path := d.String() |
| 170 | if !strings.HasSuffix(path, rel) { |
| 171 | panic(fmt.Errorf("path %q does not end with %q", path, rel)) |
| 172 | } |
| 173 | path = strings.TrimSuffix(path, rel) |
| 174 | entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 175 | } |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame^] | 176 | }, |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 177 | }, |
| 178 | } |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 181 | func InitShBinaryModule(s *ShBinary) { |
| 182 | s.AddProperties(&s.properties) |
| 183 | } |
| 184 | |
Patrice Arruda | e103419 | 2019-03-11 13:20:17 -0700 | [diff] [blame] | 185 | // sh_binary is for a shell script or batch file to be installed as an |
| 186 | // executable binary to <partition>/bin. |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 187 | func ShBinaryFactory() Module { |
| 188 | module := &ShBinary{} |
| 189 | InitShBinaryModule(module) |
| 190 | InitAndroidArchModule(module, HostAndDeviceSupported, MultilibFirst) |
| 191 | return module |
| 192 | } |
| 193 | |
Patrice Arruda | e103419 | 2019-03-11 13:20:17 -0700 | [diff] [blame] | 194 | // sh_binary_host is for a shell script to be installed as an executable binary |
| 195 | // to $(HOST_OUT)/bin. |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 196 | func ShBinaryHostFactory() Module { |
| 197 | module := &ShBinary{} |
| 198 | InitShBinaryModule(module) |
| 199 | InitAndroidArchModule(module, HostSupported, MultilibFirst) |
| 200 | return module |
| 201 | } |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 202 | |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 203 | // sh_test defines a shell script based test module. |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 204 | func ShTestFactory() Module { |
| 205 | module := &ShTest{} |
| 206 | InitShBinaryModule(&module.ShBinary) |
| 207 | module.AddProperties(&module.testProperties) |
| 208 | |
| 209 | InitAndroidArchModule(module, HostAndDeviceSupported, MultilibFirst) |
| 210 | return module |
| 211 | } |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 212 | |
| 213 | // sh_test_host defines a shell script based test module that runs on a host. |
| 214 | func ShTestHostFactory() Module { |
| 215 | module := &ShTest{} |
| 216 | InitShBinaryModule(&module.ShBinary) |
| 217 | module.AddProperties(&module.testProperties) |
| 218 | |
| 219 | InitAndroidArchModule(module, HostSupported, MultilibFirst) |
| 220 | return module |
| 221 | } |