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 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 15 | package sh |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "fmt" |
Colin Cross | 7c7c114 | 2019-07-29 16:46:49 -0700 | [diff] [blame] | 19 | "path/filepath" |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 20 | "sort" |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 21 | "strings" |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 22 | |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint" |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint/proptools" |
| 25 | |
| 26 | "android/soong/android" |
Rupert Shuttleworth | a1a56e8 | 2021-02-09 10:59:06 +0000 | [diff] [blame] | 27 | "android/soong/bazel" |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 28 | "android/soong/cc" |
frankfeng | c5b8749 | 2020-06-03 10:28:47 -0700 | [diff] [blame] | 29 | "android/soong/tradefed" |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | // sh_binary is for shell scripts (and batch files) that are installed as |
| 33 | // executable files into .../bin/ |
| 34 | // |
| 35 | // Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary |
| 36 | // instead. |
| 37 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 38 | var pctx = android.NewPackageContext("android/soong/sh") |
| 39 | |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 40 | func init() { |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 41 | pctx.Import("android/soong/android") |
| 42 | |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 43 | registerShBuildComponents(android.InitRegistrationContext) |
Rupert Shuttleworth | a1a56e8 | 2021-02-09 10:59:06 +0000 | [diff] [blame] | 44 | |
| 45 | android.RegisterBp2BuildMutator("sh_binary", ShBinaryBp2Build) |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Paul Duffin | 56fb8ee | 2021-03-08 15:05:52 +0000 | [diff] [blame] | 48 | func registerShBuildComponents(ctx android.RegistrationContext) { |
| 49 | ctx.RegisterModuleType("sh_binary", ShBinaryFactory) |
| 50 | ctx.RegisterModuleType("sh_binary_host", ShBinaryHostFactory) |
| 51 | ctx.RegisterModuleType("sh_test", ShTestFactory) |
| 52 | ctx.RegisterModuleType("sh_test_host", ShTestHostFactory) |
| 53 | } |
| 54 | |
| 55 | // Test fixture preparer that will register most sh build components. |
| 56 | // |
| 57 | // Singletons and mutators should only be added here if they are needed for a majority of sh |
| 58 | // module types, otherwise they should be added under a separate preparer to allow them to be |
| 59 | // selected only when needed to reduce test execution time. |
| 60 | // |
| 61 | // Module types do not have much of an overhead unless they are used so this should include as many |
| 62 | // module types as possible. The exceptions are those module types that require mutators and/or |
| 63 | // singletons in order to function in which case they should be kept together in a separate |
| 64 | // preparer. |
| 65 | var PrepareForTestWithShBuildComponents = android.GroupFixturePreparers( |
| 66 | android.FixtureRegisterWithContext(registerShBuildComponents), |
| 67 | ) |
| 68 | |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 69 | type shBinaryProperties struct { |
| 70 | // Source file of this prebuilt. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 71 | Src *string `android:"path,arch_variant"` |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 72 | |
| 73 | // optional subdirectory under which this file is installed into |
| 74 | Sub_dir *string `android:"arch_variant"` |
| 75 | |
| 76 | // optional name for the installed file. If unspecified, name of the module is used as the file name |
| 77 | Filename *string `android:"arch_variant"` |
| 78 | |
| 79 | // when set to true, and filename property is not set, the name for the installed file |
| 80 | // is the same as the file name of the source file. |
| 81 | Filename_from_src *bool `android:"arch_variant"` |
| 82 | |
| 83 | // Whether this module is directly installable to one of the partitions. Default: true. |
| 84 | Installable *bool |
Rashed Abdel-Tawab | 6a34131 | 2019-10-04 20:38:01 -0700 | [diff] [blame] | 85 | |
| 86 | // install symlinks to the binary |
| 87 | Symlinks []string `android:"arch_variant"` |
Colin Cross | cc83efb | 2020-08-21 14:25:33 -0700 | [diff] [blame] | 88 | |
| 89 | // Make this module available when building for ramdisk. |
Yifan Hong | 39143a9 | 2020-10-26 12:43:12 -0700 | [diff] [blame] | 90 | // On device without a dedicated recovery partition, the module is only |
| 91 | // available after switching root into |
| 92 | // /first_stage_ramdisk. To expose the module before switching root, install |
| 93 | // the recovery variant instead. |
Colin Cross | cc83efb | 2020-08-21 14:25:33 -0700 | [diff] [blame] | 94 | Ramdisk_available *bool |
| 95 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 96 | // Make this module available when building for vendor ramdisk. |
Yifan Hong | 39143a9 | 2020-10-26 12:43:12 -0700 | [diff] [blame] | 97 | // On device without a dedicated recovery partition, the module is only |
| 98 | // available after switching root into |
| 99 | // /first_stage_ramdisk. To expose the module before switching root, install |
| 100 | // the recovery variant instead. |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 101 | Vendor_ramdisk_available *bool |
| 102 | |
Colin Cross | cc83efb | 2020-08-21 14:25:33 -0700 | [diff] [blame] | 103 | // Make this module available when building for recovery. |
| 104 | Recovery_available *bool |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 107 | type TestProperties struct { |
| 108 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 109 | // installed into. |
| 110 | Test_suites []string `android:"arch_variant"` |
| 111 | |
| 112 | // the name of the test configuration (for example "AndroidTest.xml") that should be |
| 113 | // installed with the module. |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 114 | Test_config *string `android:"path,arch_variant"` |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 115 | |
| 116 | // list of files or filegroup modules that provide data that should be installed alongside |
| 117 | // the test. |
| 118 | Data []string `android:"path,arch_variant"` |
frankfeng | c5b8749 | 2020-06-03 10:28:47 -0700 | [diff] [blame] | 119 | |
| 120 | // Add RootTargetPreparer to auto generated test config. This guarantees the test to run |
| 121 | // with root permission. |
| 122 | Require_root *bool |
| 123 | |
| 124 | // the name of the test configuration template (for example "AndroidTestTemplate.xml") that |
| 125 | // should be installed with the module. |
| 126 | Test_config_template *string `android:"path,arch_variant"` |
| 127 | |
| 128 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml |
| 129 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true |
| 130 | // explicitly. |
| 131 | Auto_gen_config *bool |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 132 | |
| 133 | // list of binary modules that should be installed alongside the test |
| 134 | Data_bins []string `android:"path,arch_variant"` |
| 135 | |
| 136 | // list of library modules that should be installed alongside the test |
| 137 | Data_libs []string `android:"path,arch_variant"` |
| 138 | |
| 139 | // list of device binary modules that should be installed alongside the test. |
| 140 | // Only available for host sh_test modules. |
| 141 | Data_device_bins []string `android:"path,arch_variant"` |
| 142 | |
| 143 | // list of device library modules that should be installed alongside the test. |
| 144 | // Only available for host sh_test modules. |
| 145 | Data_device_libs []string `android:"path,arch_variant"` |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 148 | type ShBinary struct { |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 149 | android.ModuleBase |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 150 | android.BazelModuleBase |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 151 | |
| 152 | properties shBinaryProperties |
| 153 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 154 | sourceFilePath android.Path |
| 155 | outputFilePath android.OutputPath |
| 156 | installedFile android.InstallPath |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 159 | var _ android.HostToolProvider = (*ShBinary)(nil) |
Colin Cross | 7c7c114 | 2019-07-29 16:46:49 -0700 | [diff] [blame] | 160 | |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 161 | type ShTest struct { |
| 162 | ShBinary |
| 163 | |
| 164 | testProperties TestProperties |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 165 | |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 166 | installDir android.InstallPath |
| 167 | |
frankfeng | c5b8749 | 2020-06-03 10:28:47 -0700 | [diff] [blame] | 168 | data android.Paths |
| 169 | testConfig android.Path |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 170 | |
| 171 | dataModules map[string]android.Path |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 174 | func (s *ShBinary) HostToolPath() android.OptionalPath { |
| 175 | return android.OptionalPathForPath(s.installedFile) |
Colin Cross | 7c7c114 | 2019-07-29 16:46:49 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 178 | func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) { |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 181 | func (s *ShBinary) OutputFile() android.OutputPath { |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 182 | return s.outputFilePath |
| 183 | } |
| 184 | |
| 185 | func (s *ShBinary) SubDir() string { |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 186 | return proptools.String(s.properties.Sub_dir) |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | func (s *ShBinary) Installable() bool { |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 190 | return s.properties.Installable == nil || proptools.Bool(s.properties.Installable) |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Rashed Abdel-Tawab | 6a34131 | 2019-10-04 20:38:01 -0700 | [diff] [blame] | 193 | func (s *ShBinary) Symlinks() []string { |
| 194 | return s.properties.Symlinks |
| 195 | } |
| 196 | |
Colin Cross | cc83efb | 2020-08-21 14:25:33 -0700 | [diff] [blame] | 197 | var _ android.ImageInterface = (*ShBinary)(nil) |
| 198 | |
| 199 | func (s *ShBinary) ImageMutatorBegin(ctx android.BaseModuleContext) {} |
| 200 | |
| 201 | func (s *ShBinary) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
| 202 | return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk() |
| 203 | } |
| 204 | |
| 205 | func (s *ShBinary) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 206 | return proptools.Bool(s.properties.Ramdisk_available) || s.ModuleBase.InstallInRamdisk() |
| 207 | } |
| 208 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 209 | func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 210 | return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.ModuleBase.InstallInVendorRamdisk() |
| 211 | } |
| 212 | |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 213 | func (s *ShBinary) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 214 | return false |
| 215 | } |
| 216 | |
Colin Cross | cc83efb | 2020-08-21 14:25:33 -0700 | [diff] [blame] | 217 | func (s *ShBinary) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { |
| 218 | return proptools.Bool(s.properties.Recovery_available) || s.ModuleBase.InstallInRecovery() |
| 219 | } |
| 220 | |
| 221 | func (s *ShBinary) ExtraImageVariations(ctx android.BaseModuleContext) []string { |
| 222 | return nil |
| 223 | } |
| 224 | |
| 225 | func (s *ShBinary) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { |
| 226 | } |
| 227 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 228 | func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) { |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 229 | if s.properties.Src == nil { |
| 230 | ctx.PropertyErrorf("src", "missing prebuilt source file") |
| 231 | } |
| 232 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 233 | s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src)) |
| 234 | filename := proptools.String(s.properties.Filename) |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 235 | filenameFromSrc := proptools.Bool(s.properties.Filename_from_src) |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 236 | if filename == "" { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 237 | if filenameFromSrc { |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 238 | filename = s.sourceFilePath.Base() |
| 239 | } else { |
| 240 | filename = ctx.ModuleName() |
| 241 | } |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 242 | } else if filenameFromSrc { |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 243 | ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true") |
| 244 | return |
| 245 | } |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 246 | s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 247 | |
| 248 | // This ensures that outputFilePath has the correct name for others to |
| 249 | // use, as the source file may have a different name. |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 250 | ctx.Build(pctx, android.BuildParams{ |
| 251 | Rule: android.CpExecutable, |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 252 | Output: s.outputFilePath, |
| 253 | Input: s.sourceFilePath, |
| 254 | }) |
| 255 | } |
| 256 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 257 | func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 7c7c114 | 2019-07-29 16:46:49 -0700 | [diff] [blame] | 258 | s.generateAndroidBuildActions(ctx) |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 259 | installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir)) |
Colin Cross | 7c7c114 | 2019-07-29 16:46:49 -0700 | [diff] [blame] | 260 | s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath) |
| 261 | } |
| 262 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 263 | func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries { |
| 264 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 265 | Class: "EXECUTABLES", |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 266 | OutputFile: android.OptionalPathForPath(s.outputFilePath), |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 267 | Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk", |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 268 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 269 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 270 | s.customAndroidMkEntries(entries) |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 271 | entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir)) |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 272 | }, |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 273 | }, |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 274 | }} |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 277 | func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) { |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 278 | entries.SetString("LOCAL_MODULE_SUFFIX", "") |
| 279 | entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel()) |
Rashed Abdel-Tawab | 6a34131 | 2019-10-04 20:38:01 -0700 | [diff] [blame] | 280 | if len(s.properties.Symlinks) > 0 { |
| 281 | entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " ")) |
| 282 | } |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 285 | type dependencyTag struct { |
| 286 | blueprint.BaseDependencyTag |
| 287 | name string |
| 288 | } |
| 289 | |
| 290 | var ( |
| 291 | shTestDataBinsTag = dependencyTag{name: "dataBins"} |
| 292 | shTestDataLibsTag = dependencyTag{name: "dataLibs"} |
| 293 | shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"} |
| 294 | shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"} |
| 295 | ) |
| 296 | |
| 297 | var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}} |
| 298 | |
| 299 | func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 300 | s.ShBinary.DepsMutator(ctx) |
| 301 | |
| 302 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...) |
| 303 | ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...), |
| 304 | shTestDataLibsTag, s.testProperties.Data_libs...) |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 305 | if (ctx.Target().Os.Class == android.Host || ctx.BazelConversionMode()) && len(ctx.Config().Targets[android.Android]) > 0 { |
Jaewoong Jung | 642916f | 2020-10-09 17:25:15 -0700 | [diff] [blame] | 306 | deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations() |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 307 | ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...) |
| 308 | ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...), |
| 309 | shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...) |
| 310 | } else if ctx.Target().Os.Class != android.Host { |
| 311 | if len(s.testProperties.Data_device_bins) > 0 { |
| 312 | ctx.PropertyErrorf("data_device_bins", "only available for host modules") |
| 313 | } |
| 314 | if len(s.testProperties.Data_device_libs) > 0 { |
| 315 | ctx.PropertyErrorf("data_device_libs", "only available for host modules") |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) { |
| 321 | if _, exists := s.dataModules[relPath]; exists { |
| 322 | ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s", |
| 323 | relPath, s.dataModules[relPath].String(), path.String()) |
| 324 | return |
| 325 | } |
| 326 | s.dataModules[relPath] = path |
| 327 | } |
| 328 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 329 | func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Colin Cross | 7c7c114 | 2019-07-29 16:46:49 -0700 | [diff] [blame] | 330 | s.ShBinary.generateAndroidBuildActions(ctx) |
| 331 | testDir := "nativetest" |
| 332 | if ctx.Target().Arch.ArchType.Multilib == "lib64" { |
| 333 | testDir = "nativetest64" |
| 334 | } |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 335 | if ctx.Target().NativeBridge == android.NativeBridgeEnabled { |
Colin Cross | 7c7c114 | 2019-07-29 16:46:49 -0700 | [diff] [blame] | 336 | testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath) |
| 337 | } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) { |
| 338 | testDir = filepath.Join(testDir, ctx.Arch().ArchType.String()) |
| 339 | } |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 340 | if s.SubDir() != "" { |
| 341 | // Don't add the module name to the installation path if sub_dir is specified for backward |
| 342 | // compatibility. |
| 343 | s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir()) |
| 344 | } else { |
| 345 | s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name()) |
| 346 | } |
| 347 | s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 348 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 349 | s.data = android.PathsForModuleSrc(ctx, s.testProperties.Data) |
frankfeng | c5b8749 | 2020-06-03 10:28:47 -0700 | [diff] [blame] | 350 | |
| 351 | var configs []tradefed.Config |
| 352 | if Bool(s.testProperties.Require_root) { |
| 353 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil}) |
| 354 | } else { |
| 355 | options := []tradefed.Option{{Name: "force-root", Value: "false"}} |
| 356 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options}) |
| 357 | } |
frankfeng | be6ae77 | 2020-09-28 13:22:57 -0700 | [diff] [blame] | 358 | if len(s.testProperties.Data_device_bins) > 0 { |
| 359 | moduleName := s.Name() |
| 360 | remoteDir := "/data/local/tests/unrestricted/" + moduleName + "/" |
| 361 | options := []tradefed.Option{{Name: "cleanup", Value: "true"}} |
| 362 | for _, bin := range s.testProperties.Data_device_bins { |
| 363 | options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: remoteDir + bin}) |
| 364 | } |
| 365 | configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options}) |
| 366 | } |
frankfeng | c5b8749 | 2020-06-03 10:28:47 -0700 | [diff] [blame] | 367 | s.testConfig = tradefed.AutoGenShellTestConfig(ctx, s.testProperties.Test_config, |
| 368 | s.testProperties.Test_config_template, s.testProperties.Test_suites, configs, s.testProperties.Auto_gen_config, s.outputFilePath.Base()) |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 369 | |
| 370 | s.dataModules = make(map[string]android.Path) |
| 371 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 372 | depTag := ctx.OtherModuleDependencyTag(dep) |
| 373 | switch depTag { |
| 374 | case shTestDataBinsTag, shTestDataDeviceBinsTag: |
Anton Hansson | 2f6422c | 2020-12-31 13:42:10 +0000 | [diff] [blame] | 375 | path := android.OutputFileForModule(ctx, dep, "") |
| 376 | s.addToDataModules(ctx, path.Base(), path) |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 377 | case shTestDataLibsTag, shTestDataDeviceLibsTag: |
| 378 | if cc, isCc := dep.(*cc.Module); isCc { |
| 379 | // Copy to an intermediate output directory to append "lib[64]" to the path, |
| 380 | // so that it's compatible with the default rpath values. |
| 381 | var relPath string |
| 382 | if cc.Arch().ArchType.Multilib == "lib64" { |
| 383 | relPath = filepath.Join("lib64", cc.OutputFile().Path().Base()) |
| 384 | } else { |
| 385 | relPath = filepath.Join("lib", cc.OutputFile().Path().Base()) |
| 386 | } |
| 387 | if _, exist := s.dataModules[relPath]; exist { |
| 388 | return |
| 389 | } |
| 390 | relocatedLib := android.PathForModuleOut(ctx, "relocated", relPath) |
| 391 | ctx.Build(pctx, android.BuildParams{ |
| 392 | Rule: android.Cp, |
| 393 | Input: cc.OutputFile().Path(), |
| 394 | Output: relocatedLib, |
| 395 | }) |
| 396 | s.addToDataModules(ctx, relPath, relocatedLib) |
| 397 | return |
| 398 | } |
| 399 | property := "data_libs" |
| 400 | if depTag == shTestDataDeviceBinsTag { |
| 401 | property = "data_device_libs" |
| 402 | } |
| 403 | ctx.PropertyErrorf(property, "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep)) |
| 404 | } |
| 405 | }) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Colin Cross | 7c7c114 | 2019-07-29 16:46:49 -0700 | [diff] [blame] | 408 | func (s *ShTest) InstallInData() bool { |
| 409 | return true |
| 410 | } |
| 411 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 412 | func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries { |
| 413 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 414 | Class: "NATIVE_TESTS", |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 415 | OutputFile: android.OptionalPathForPath(s.outputFilePath), |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 416 | Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk", |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 417 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
Colin Cross | aa25553 | 2020-07-03 13:18:24 -0700 | [diff] [blame] | 418 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 419 | s.customAndroidMkEntries(entries) |
Jaewoong Jung | 4aedc86 | 2020-06-10 17:23:46 -0700 | [diff] [blame] | 420 | entries.SetPath("LOCAL_MODULE_PATH", s.installDir.ToMakePath()) |
Liz Kammer | 57f5b33 | 2020-11-24 12:42:58 -0800 | [diff] [blame] | 421 | entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...) |
Colin Cross | a638482 | 2020-06-09 15:09:22 -0700 | [diff] [blame] | 422 | if s.testConfig != nil { |
| 423 | entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig) |
frankfeng | c5b8749 | 2020-06-03 10:28:47 -0700 | [diff] [blame] | 424 | } |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 425 | for _, d := range s.data { |
| 426 | rel := d.Rel() |
| 427 | path := d.String() |
| 428 | if !strings.HasSuffix(path, rel) { |
| 429 | panic(fmt.Errorf("path %q does not end with %q", path, rel)) |
| 430 | } |
| 431 | path = strings.TrimSuffix(path, rel) |
| 432 | entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel) |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 433 | } |
Jaewoong Jung | 6e0eee5 | 2020-05-29 16:15:32 -0700 | [diff] [blame] | 434 | relPaths := make([]string, 0) |
| 435 | for relPath, _ := range s.dataModules { |
| 436 | relPaths = append(relPaths, relPath) |
| 437 | } |
| 438 | sort.Strings(relPaths) |
| 439 | for _, relPath := range relPaths { |
| 440 | dir := strings.TrimSuffix(s.dataModules[relPath].String(), relPath) |
| 441 | entries.AddStrings("LOCAL_TEST_DATA", dir+":"+relPath) |
| 442 | } |
Jaewoong Jung | e0dc8df | 2019-08-27 17:33:16 -0700 | [diff] [blame] | 443 | }, |
Jaewoong Jung | 8eaeb09 | 2019-05-16 14:58:29 -0700 | [diff] [blame] | 444 | }, |
Jiyong Park | 0b0e1b9 | 2019-12-03 13:24:29 +0900 | [diff] [blame] | 445 | }} |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 448 | func InitShBinaryModule(s *ShBinary) { |
| 449 | s.AddProperties(&s.properties) |
Liz Kammer | ea6666f | 2021-02-17 10:17:28 -0500 | [diff] [blame] | 450 | android.InitBazelModule(s) |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Patrice Arruda | e103419 | 2019-03-11 13:20:17 -0700 | [diff] [blame] | 453 | // sh_binary is for a shell script or batch file to be installed as an |
| 454 | // executable binary to <partition>/bin. |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 455 | func ShBinaryFactory() android.Module { |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 456 | module := &ShBinary{} |
| 457 | InitShBinaryModule(module) |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 458 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst) |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 459 | return module |
| 460 | } |
| 461 | |
Patrice Arruda | e103419 | 2019-03-11 13:20:17 -0700 | [diff] [blame] | 462 | // sh_binary_host is for a shell script to be installed as an executable binary |
| 463 | // to $(HOST_OUT)/bin. |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 464 | func ShBinaryHostFactory() android.Module { |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 465 | module := &ShBinary{} |
| 466 | InitShBinaryModule(module) |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 467 | android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst) |
Dan Willemsen | b055267 | 2019-01-25 16:04:11 -0800 | [diff] [blame] | 468 | return module |
| 469 | } |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 470 | |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 471 | // sh_test defines a shell script based test module. |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 472 | func ShTestFactory() android.Module { |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 473 | module := &ShTest{} |
| 474 | InitShBinaryModule(&module.ShBinary) |
| 475 | module.AddProperties(&module.testProperties) |
| 476 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 477 | android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst) |
Julien Desprez | 9e7fc14 | 2019-03-08 11:07:05 -0800 | [diff] [blame] | 478 | return module |
| 479 | } |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 480 | |
| 481 | // sh_test_host defines a shell script based test module that runs on a host. |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 482 | func ShTestHostFactory() android.Module { |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 483 | module := &ShTest{} |
| 484 | InitShBinaryModule(&module.ShBinary) |
| 485 | module.AddProperties(&module.testProperties) |
| 486 | |
Jaewoong Jung | 4b79e98 | 2020-06-01 10:45:49 -0700 | [diff] [blame] | 487 | android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst) |
Jaewoong Jung | 61a8368 | 2019-07-01 09:08:50 -0700 | [diff] [blame] | 488 | return module |
| 489 | } |
frankfeng | c5b8749 | 2020-06-03 10:28:47 -0700 | [diff] [blame] | 490 | |
Rupert Shuttleworth | a1a56e8 | 2021-02-09 10:59:06 +0000 | [diff] [blame] | 491 | type bazelShBinaryAttributes struct { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 492 | Srcs bazel.LabelListAttribute |
Rupert Shuttleworth | a1a56e8 | 2021-02-09 10:59:06 +0000 | [diff] [blame] | 493 | // Bazel also supports the attributes below, but (so far) these are not required for Bionic |
| 494 | // deps |
| 495 | // data |
| 496 | // args |
| 497 | // compatible_with |
| 498 | // deprecation |
| 499 | // distribs |
| 500 | // env |
| 501 | // exec_compatible_with |
| 502 | // exec_properties |
| 503 | // features |
| 504 | // licenses |
| 505 | // output_licenses |
| 506 | // restricted_to |
| 507 | // tags |
| 508 | // target_compatible_with |
| 509 | // testonly |
| 510 | // toolchains |
| 511 | // visibility |
| 512 | } |
| 513 | |
| 514 | type bazelShBinary struct { |
| 515 | android.BazelTargetModuleBase |
| 516 | bazelShBinaryAttributes |
| 517 | } |
| 518 | |
| 519 | func BazelShBinaryFactory() android.Module { |
| 520 | module := &bazelShBinary{} |
| 521 | module.AddProperties(&module.bazelShBinaryAttributes) |
| 522 | android.InitBazelTargetModule(module) |
| 523 | return module |
| 524 | } |
| 525 | |
| 526 | func ShBinaryBp2Build(ctx android.TopDownMutatorContext) { |
| 527 | m, ok := ctx.Module().(*ShBinary) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 528 | if !ok || !m.ConvertWithBp2build(ctx) { |
Rupert Shuttleworth | a1a56e8 | 2021-02-09 10:59:06 +0000 | [diff] [blame] | 529 | return |
| 530 | } |
| 531 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 532 | srcs := bazel.MakeLabelListAttribute( |
| 533 | android.BazelLabelForModuleSrc(ctx, []string{*m.properties.Src})) |
Rupert Shuttleworth | a1a56e8 | 2021-02-09 10:59:06 +0000 | [diff] [blame] | 534 | |
| 535 | attrs := &bazelShBinaryAttributes{ |
| 536 | Srcs: srcs, |
| 537 | } |
| 538 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 539 | props := bazel.BazelTargetModuleProperties{ |
| 540 | Rule_class: "sh_binary", |
| 541 | } |
Rupert Shuttleworth | a1a56e8 | 2021-02-09 10:59:06 +0000 | [diff] [blame] | 542 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 543 | ctx.CreateBazelTargetModule(BazelShBinaryFactory, m.Name(), props, attrs) |
Rupert Shuttleworth | a1a56e8 | 2021-02-09 10:59:06 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | func (m *bazelShBinary) Name() string { |
| 547 | return m.BaseModuleName() |
| 548 | } |
| 549 | |
| 550 | func (m *bazelShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {} |
| 551 | |
frankfeng | c5b8749 | 2020-06-03 10:28:47 -0700 | [diff] [blame] | 552 | var Bool = proptools.Bool |