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