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