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