blob: 6b40e3c6746aa9576712ddb4910b02f52e80fd99 [file] [log] [blame]
Dan Willemsenb0552672019-01-25 16:04:11 -08001// 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 Jung4b79e982020-06-01 10:45:49 -070015package sh
Dan Willemsenb0552672019-01-25 16:04:11 -080016
17import (
18 "fmt"
Colin Cross7c7c1142019-07-29 16:46:49 -070019 "path/filepath"
Julien Desprez9e7fc142019-03-08 11:07:05 -080020 "strings"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070021
Aditya Choudhary9b593522023-10-06 19:54:58 +000022 "android/soong/testing"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070023 "github.com/google/blueprint"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070024 "github.com/google/blueprint/proptools"
25
26 "android/soong/android"
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +000027 "android/soong/bazel"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070028 "android/soong/cc"
Rob Seymour925aa092021-08-10 20:42:03 +000029 "android/soong/snapshot"
frankfengc5b87492020-06-03 10:28:47 -070030 "android/soong/tradefed"
Dan Willemsenb0552672019-01-25 16:04:11 -080031)
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 Jung4b79e982020-06-01 10:45:49 -070039var pctx = android.NewPackageContext("android/soong/sh")
40
Dan Willemsenb0552672019-01-25 16:04:11 -080041func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070042 pctx.Import("android/soong/android")
43
Paul Duffin56fb8ee2021-03-08 15:05:52 +000044 registerShBuildComponents(android.InitRegistrationContext)
Dan Willemsenb0552672019-01-25 16:04:11 -080045}
46
Paul Duffin56fb8ee2021-03-08 15:05:52 +000047func registerShBuildComponents(ctx android.RegistrationContext) {
48 ctx.RegisterModuleType("sh_binary", ShBinaryFactory)
49 ctx.RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
50 ctx.RegisterModuleType("sh_test", ShTestFactory)
51 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
52}
53
54// Test fixture preparer that will register most sh build components.
55//
56// Singletons and mutators should only be added here if they are needed for a majority of sh
57// module types, otherwise they should be added under a separate preparer to allow them to be
58// selected only when needed to reduce test execution time.
59//
60// Module types do not have much of an overhead unless they are used so this should include as many
61// module types as possible. The exceptions are those module types that require mutators and/or
62// singletons in order to function in which case they should be kept together in a separate
63// preparer.
64var PrepareForTestWithShBuildComponents = android.GroupFixturePreparers(
65 android.FixtureRegisterWithContext(registerShBuildComponents),
66)
67
Dan Willemsenb0552672019-01-25 16:04:11 -080068type shBinaryProperties struct {
69 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080070 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080071
72 // optional subdirectory under which this file is installed into
73 Sub_dir *string `android:"arch_variant"`
74
75 // optional name for the installed file. If unspecified, name of the module is used as the file name
76 Filename *string `android:"arch_variant"`
77
78 // when set to true, and filename property is not set, the name for the installed file
79 // is the same as the file name of the source file.
80 Filename_from_src *bool `android:"arch_variant"`
81
82 // Whether this module is directly installable to one of the partitions. Default: true.
83 Installable *bool
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -070084
85 // install symlinks to the binary
86 Symlinks []string `android:"arch_variant"`
Colin Crosscc83efb2020-08-21 14:25:33 -070087
88 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070089 // On device without a dedicated recovery partition, the module is only
90 // available after switching root into
91 // /first_stage_ramdisk. To expose the module before switching root, install
92 // the recovery variant instead.
Colin Crosscc83efb2020-08-21 14:25:33 -070093 Ramdisk_available *bool
94
Yifan Hong60e0cfb2020-10-21 15:17:56 -070095 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070096 // On device without a dedicated recovery partition, the module is only
97 // available after switching root into
98 // /first_stage_ramdisk. To expose the module before switching root, install
99 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700100 Vendor_ramdisk_available *bool
101
Colin Crosscc83efb2020-08-21 14:25:33 -0700102 // Make this module available when building for recovery.
103 Recovery_available *bool
Dan Willemsenb0552672019-01-25 16:04:11 -0800104}
105
Julien Desprez9e7fc142019-03-08 11:07:05 -0800106type TestProperties struct {
107 // list of compatibility suites (for example "cts", "vts") that the module should be
108 // installed into.
109 Test_suites []string `android:"arch_variant"`
110
111 // the name of the test configuration (for example "AndroidTest.xml") that should be
112 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -0700113 Test_config *string `android:"path,arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700114
115 // list of files or filegroup modules that provide data that should be installed alongside
116 // the test.
117 Data []string `android:"path,arch_variant"`
frankfengc5b87492020-06-03 10:28:47 -0700118
119 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
120 // with root permission.
121 Require_root *bool
122
123 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
124 // should be installed with the module.
125 Test_config_template *string `android:"path,arch_variant"`
126
127 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
128 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
129 // explicitly.
130 Auto_gen_config *bool
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700131
132 // list of binary modules that should be installed alongside the test
133 Data_bins []string `android:"path,arch_variant"`
134
135 // list of library modules that should be installed alongside the test
136 Data_libs []string `android:"path,arch_variant"`
137
138 // list of device binary modules that should be installed alongside the test.
139 // Only available for host sh_test modules.
140 Data_device_bins []string `android:"path,arch_variant"`
141
142 // list of device library modules that should be installed alongside the test.
143 // Only available for host sh_test modules.
144 Data_device_libs []string `android:"path,arch_variant"`
Dan Shib40deac2021-05-24 12:04:54 -0700145
Makoto Onuki1725b202023-08-24 22:17:56 +0000146 // list of java modules that provide data that should be installed alongside the test.
147 Java_data []string
148
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900149 // Install the test into a folder named for the module in all test suites.
150 Per_testcase_directory *bool
151
Dan Shib40deac2021-05-24 12:04:54 -0700152 // Test options.
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800153 Test_options android.CommonTestOptions
Julien Desprez9e7fc142019-03-08 11:07:05 -0800154}
155
Dan Willemsenb0552672019-01-25 16:04:11 -0800156type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700157 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500158 android.BazelModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -0800159
160 properties shBinaryProperties
161
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700162 sourceFilePath android.Path
163 outputFilePath android.OutputPath
164 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800165}
166
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700167var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700168
Julien Desprez9e7fc142019-03-08 11:07:05 -0800169type ShTest struct {
170 ShBinary
171
172 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700173
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700174 installDir android.InstallPath
175
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800176 data []android.DataPath
frankfengc5b87492020-06-03 10:28:47 -0700177 testConfig android.Path
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700178
179 dataModules map[string]android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800180}
181
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700182func (s *ShBinary) HostToolPath() android.OptionalPath {
183 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700184}
185
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700186func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800187}
188
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700189func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800190 return s.outputFilePath
191}
192
Edward Liaw492ca052023-08-07 19:03:22 +0000193func (s *ShBinary) OutputFiles(tag string) (android.Paths, error) {
194 switch tag {
195 case "":
196 return android.Paths{s.outputFilePath}, nil
197 default:
198 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
199 }
200}
201
Dan Willemsenb0552672019-01-25 16:04:11 -0800202func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700203 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800204}
205
Rob Seymour925aa092021-08-10 20:42:03 +0000206func (s *ShBinary) RelativeInstallPath() string {
207 return s.SubDir()
208}
Dan Willemsenb0552672019-01-25 16:04:11 -0800209func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700210 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800211}
212
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700213func (s *ShBinary) Symlinks() []string {
214 return s.properties.Symlinks
215}
216
Colin Crosscc83efb2020-08-21 14:25:33 -0700217var _ android.ImageInterface = (*ShBinary)(nil)
218
219func (s *ShBinary) ImageMutatorBegin(ctx android.BaseModuleContext) {}
220
221func (s *ShBinary) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
222 return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk()
223}
224
225func (s *ShBinary) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
226 return proptools.Bool(s.properties.Ramdisk_available) || s.ModuleBase.InstallInRamdisk()
227}
228
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700229func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
230 return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.ModuleBase.InstallInVendorRamdisk()
231}
232
Inseob Kim08758f02021-04-08 21:13:22 +0900233func (s *ShBinary) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
234 return false
235}
236
Colin Crosscc83efb2020-08-21 14:25:33 -0700237func (s *ShBinary) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
238 return proptools.Bool(s.properties.Recovery_available) || s.ModuleBase.InstallInRecovery()
239}
240
241func (s *ShBinary) ExtraImageVariations(ctx android.BaseModuleContext) []string {
242 return nil
243}
244
245func (s *ShBinary) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
246}
247
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700248func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500249 if s.properties.Src == nil {
250 ctx.PropertyErrorf("src", "missing prebuilt source file")
251 }
252
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700253 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
254 filename := proptools.String(s.properties.Filename)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800255 filenameFromSrc := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800256 if filename == "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800257 if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800258 filename = s.sourceFilePath.Base()
259 } else {
260 filename = ctx.ModuleName()
261 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800262 } else if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800263 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
264 return
265 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700266 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800267
268 // This ensures that outputFilePath has the correct name for others to
269 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700270 ctx.Build(pctx, android.BuildParams{
271 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800272 Output: s.outputFilePath,
273 Input: s.sourceFilePath,
274 })
275}
276
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700277func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700278 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700279 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000280 if !s.Installable() {
281 s.SkipInstall()
282 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700283 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
Colin Cross94bf5182021-11-09 17:21:14 -0800284 for _, symlink := range s.Symlinks() {
285 ctx.InstallSymlink(installDir, symlink, s.installedFile)
286 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700287}
288
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700289func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
290 return []android.AndroidMkEntries{android.AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800291 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700292 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500293 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700294 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700295 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700296 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700297 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000298 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !s.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700299 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800300 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900301 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800302}
303
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700304func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700305 entries.SetString("LOCAL_MODULE_SUFFIX", "")
306 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700307 if len(s.properties.Symlinks) > 0 {
308 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
309 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700310}
311
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700312type dependencyTag struct {
313 blueprint.BaseDependencyTag
314 name string
315}
316
317var (
318 shTestDataBinsTag = dependencyTag{name: "dataBins"}
319 shTestDataLibsTag = dependencyTag{name: "dataLibs"}
320 shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
321 shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"}
Makoto Onuki1725b202023-08-24 22:17:56 +0000322 shTestJavaDataTag = dependencyTag{name: "javaData"}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700323)
324
325var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}}
326
327func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) {
328 s.ShBinary.DepsMutator(ctx)
329
330 ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...)
331 ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...),
332 shTestDataLibsTag, s.testProperties.Data_libs...)
Liz Kammer3bf97bd2022-04-26 09:38:20 -0400333 if ctx.Target().Os.Class == android.Host && len(ctx.Config().Targets[android.Android]) > 0 {
Jaewoong Jung642916f2020-10-09 17:25:15 -0700334 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700335 ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...)
336 ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...),
337 shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...)
Makoto Onuki1725b202023-08-24 22:17:56 +0000338
339 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
340 ctx.AddVariationDependencies(javaDataVariation, shTestJavaDataTag, s.testProperties.Java_data...)
341
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700342 } else if ctx.Target().Os.Class != android.Host {
343 if len(s.testProperties.Data_device_bins) > 0 {
344 ctx.PropertyErrorf("data_device_bins", "only available for host modules")
345 }
346 if len(s.testProperties.Data_device_libs) > 0 {
347 ctx.PropertyErrorf("data_device_libs", "only available for host modules")
348 }
Makoto Onuki1725b202023-08-24 22:17:56 +0000349 if len(s.testProperties.Java_data) > 0 {
350 ctx.PropertyErrorf("Java_data", "only available for host modules")
351 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700352 }
353}
354
355func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) {
356 if _, exists := s.dataModules[relPath]; exists {
357 ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s",
358 relPath, s.dataModules[relPath].String(), path.String())
359 return
360 }
361 s.dataModules[relPath] = path
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800362 s.data = append(s.data, android.DataPath{SrcPath: path})
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700363}
364
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700365func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700366 s.ShBinary.generateAndroidBuildActions(ctx)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800367
368 expandedData := android.PathsForModuleSrc(ctx, s.testProperties.Data)
369 // Emulate the data property for java_data dependencies.
370 for _, javaData := range ctx.GetDirectDepsWithTag(shTestJavaDataTag) {
371 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
372 }
373 for _, d := range expandedData {
374 s.data = append(s.data, android.DataPath{SrcPath: d})
375 }
376
Colin Cross7c7c1142019-07-29 16:46:49 -0700377 testDir := "nativetest"
378 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
379 testDir = "nativetest64"
380 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700381 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700382 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
383 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
384 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
385 }
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700386 if s.SubDir() != "" {
387 // Don't add the module name to the installation path if sub_dir is specified for backward
388 // compatibility.
389 s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir())
390 } else {
391 s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name())
392 }
frankfengc5b87492020-06-03 10:28:47 -0700393
394 var configs []tradefed.Config
395 if Bool(s.testProperties.Require_root) {
396 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
397 } else {
398 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
399 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
400 }
frankfengbe6ae772020-09-28 13:22:57 -0700401 if len(s.testProperties.Data_device_bins) > 0 {
402 moduleName := s.Name()
403 remoteDir := "/data/local/tests/unrestricted/" + moduleName + "/"
404 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
405 for _, bin := range s.testProperties.Data_device_bins {
406 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: remoteDir + bin})
407 }
408 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options})
409 }
Cole Faust21680542022-12-07 18:18:37 -0800410 s.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
411 TestConfigProp: s.testProperties.Test_config,
412 TestConfigTemplateProp: s.testProperties.Test_config_template,
413 TestSuites: s.testProperties.Test_suites,
414 Config: configs,
415 AutoGenConfig: s.testProperties.Auto_gen_config,
416 OutputFileName: s.outputFilePath.Base(),
417 DeviceTemplate: "${ShellTestConfigTemplate}",
418 HostTemplate: "${ShellTestConfigTemplate}",
419 })
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700420
421 s.dataModules = make(map[string]android.Path)
422 ctx.VisitDirectDeps(func(dep android.Module) {
423 depTag := ctx.OtherModuleDependencyTag(dep)
424 switch depTag {
425 case shTestDataBinsTag, shTestDataDeviceBinsTag:
Anton Hansson2f6422c2020-12-31 13:42:10 +0000426 path := android.OutputFileForModule(ctx, dep, "")
427 s.addToDataModules(ctx, path.Base(), path)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700428 case shTestDataLibsTag, shTestDataDeviceLibsTag:
429 if cc, isCc := dep.(*cc.Module); isCc {
430 // Copy to an intermediate output directory to append "lib[64]" to the path,
431 // so that it's compatible with the default rpath values.
432 var relPath string
433 if cc.Arch().ArchType.Multilib == "lib64" {
434 relPath = filepath.Join("lib64", cc.OutputFile().Path().Base())
435 } else {
436 relPath = filepath.Join("lib", cc.OutputFile().Path().Base())
437 }
438 if _, exist := s.dataModules[relPath]; exist {
439 return
440 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800441 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700442 ctx.Build(pctx, android.BuildParams{
443 Rule: android.Cp,
444 Input: cc.OutputFile().Path(),
445 Output: relocatedLib,
446 })
447 s.addToDataModules(ctx, relPath, relocatedLib)
448 return
449 }
450 property := "data_libs"
451 if depTag == shTestDataDeviceBinsTag {
452 property = "data_device_libs"
453 }
454 ctx.PropertyErrorf(property, "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
455 }
456 })
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800457
458 installedData := ctx.InstallTestData(s.installDir, s.data)
459 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath, installedData...)
460
Aditya Choudhary9b593522023-10-06 19:54:58 +0000461 ctx.SetProvider(testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700462}
463
Colin Cross7c7c1142019-07-29 16:46:49 -0700464func (s *ShTest) InstallInData() bool {
465 return true
466}
467
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700468func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
469 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700470 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700471 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500472 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700473 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700474 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700475 s.customAndroidMkEntries(entries)
Colin Crossc68db4b2021-11-11 18:59:15 -0800476 entries.SetPath("LOCAL_MODULE_PATH", s.installDir)
Liz Kammer57f5b332020-11-24 12:42:58 -0800477 entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700478 if s.testConfig != nil {
479 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700480 }
yangbill22bafec2022-02-11 18:06:07 +0800481 if s.testProperties.Data_bins != nil {
482 entries.AddStrings("LOCAL_TEST_DATA_BINS", s.testProperties.Data_bins...)
483 }
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900484 entries.SetBoolIfTrue("LOCAL_COMPATIBILITY_PER_TESTCASE_DIRECTORY", Bool(s.testProperties.Per_testcase_directory))
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800485
486 s.testProperties.Test_options.SetAndroidMkEntries(entries)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700487 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700488 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900489 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800490}
491
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400492func initShBinaryModule(s *ShBinary, useBazel bool) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800493 s.AddProperties(&s.properties)
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400494 if useBazel {
495 android.InitBazelModule(s)
496 }
Dan Willemsenb0552672019-01-25 16:04:11 -0800497}
498
Patrice Arrudae1034192019-03-11 13:20:17 -0700499// sh_binary is for a shell script or batch file to be installed as an
500// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700501func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800502 module := &ShBinary{}
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400503 initShBinaryModule(module, true)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700504 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800505 return module
506}
507
Patrice Arrudae1034192019-03-11 13:20:17 -0700508// sh_binary_host is for a shell script to be installed as an executable binary
509// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700510func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800511 module := &ShBinary{}
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400512 initShBinaryModule(module, true)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700513 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800514 return module
515}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800516
Jaewoong Jung61a83682019-07-01 09:08:50 -0700517// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700518func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800519 module := &ShTest{}
Jason Wu6d8d44a2023-08-08 22:09:12 -0400520 initShBinaryModule(&module.ShBinary, true)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800521 module.AddProperties(&module.testProperties)
522
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700523 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800524 return module
525}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700526
527// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700528func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700529 module := &ShTest{}
Jason Wu6d8d44a2023-08-08 22:09:12 -0400530 initShBinaryModule(&module.ShBinary, true)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700531 module.AddProperties(&module.testProperties)
Julien Desprez06f13992021-06-28 13:41:43 -0700532 // Default sh_test_host to unit_tests = true
533 if module.testProperties.Test_options.Unit_test == nil {
534 module.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
535 }
Jaewoong Jung61a83682019-07-01 09:08:50 -0700536
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700537 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700538 return module
539}
frankfengc5b87492020-06-03 10:28:47 -0700540
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000541type bazelShBinaryAttributes struct {
Yu Liub3024552021-11-23 14:53:41 -0800542 Srcs bazel.LabelListAttribute
Liz Kammer48c6ead2021-12-21 15:36:11 -0500543 Filename *string
544 Sub_dir *string
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000545 // Bazel also supports the attributes below, but (so far) these are not required for Bionic
546 // deps
547 // data
548 // args
549 // compatible_with
550 // deprecation
551 // distribs
552 // env
553 // exec_compatible_with
554 // exec_properties
555 // features
556 // licenses
557 // output_licenses
558 // restricted_to
559 // tags
560 // target_compatible_with
561 // testonly
562 // toolchains
563 // visibility
564}
565
Jason Wu6d8d44a2023-08-08 22:09:12 -0400566type bazelShTestAttributes struct {
Jason Wu25c69ee2023-08-24 15:40:10 -0400567 Srcs bazel.LabelListAttribute
568 Data bazel.LabelListAttribute
569 Data_bins bazel.LabelListAttribute
570 Tags bazel.StringListAttribute
571 Runs_on bazel.StringListAttribute
572 tradefed.TestConfigAttributes
Jason Wu6d8d44a2023-08-08 22:09:12 -0400573}
574
Chris Parsons637458d2023-09-19 20:09:00 +0000575func (m *ShBinary) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -0400576 srcs := bazel.MakeLabelListAttribute(
577 android.BazelLabelForModuleSrc(ctx, []string{*m.properties.Src}))
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000578
Liz Kammer48c6ead2021-12-21 15:36:11 -0500579 var filename *string
Yu Liub3024552021-11-23 14:53:41 -0800580 if m.properties.Filename != nil {
Liz Kammer48c6ead2021-12-21 15:36:11 -0500581 filename = m.properties.Filename
Yu Liub3024552021-11-23 14:53:41 -0800582 }
583
Liz Kammer48c6ead2021-12-21 15:36:11 -0500584 var subDir *string
Yu Liub3024552021-11-23 14:53:41 -0800585 if m.properties.Sub_dir != nil {
Liz Kammer48c6ead2021-12-21 15:36:11 -0500586 subDir = m.properties.Sub_dir
Yu Liub3024552021-11-23 14:53:41 -0800587 }
588
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000589 attrs := &bazelShBinaryAttributes{
Yu Liub3024552021-11-23 14:53:41 -0800590 Srcs: srcs,
591 Filename: filename,
592 Sub_dir: subDir,
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000593 }
594
Liz Kammerfc46bc12021-02-19 11:06:17 -0500595 props := bazel.BazelTargetModuleProperties{
Yu Liub3024552021-11-23 14:53:41 -0800596 Rule_class: "sh_binary",
597 Bzl_load_location: "//build/bazel/rules:sh_binary.bzl",
Liz Kammerfc46bc12021-02-19 11:06:17 -0500598 }
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000599
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000600 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000601}
602
Chris Parsons637458d2023-09-19 20:09:00 +0000603func (m *ShTest) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Jason Wu6d8d44a2023-08-08 22:09:12 -0400604 srcs := bazel.MakeLabelListAttribute(
605 android.BazelLabelForModuleSrc(ctx, []string{*m.properties.Src}))
606
Jason Wu25c69ee2023-08-24 15:40:10 -0400607 dataBins := bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, m.testProperties.Data_bins))
Jason Wu6d8d44a2023-08-08 22:09:12 -0400608
Jason Wu25c69ee2023-08-24 15:40:10 -0400609 var combinedData bazel.LabelList
610 combinedData.Append(android.BazelLabelForModuleSrc(ctx, m.testProperties.Data))
611 combinedData.Append(android.BazelLabelForModuleDeps(ctx, m.testProperties.Data_bins))
612 combinedData.Append(android.BazelLabelForModuleDeps(ctx, m.testProperties.Data_libs))
613 data := bazel.MakeLabelListAttribute(combinedData)
Jason Wu6d8d44a2023-08-08 22:09:12 -0400614
615 tags := bazel.MakeStringListAttribute(
616 m.testProperties.Test_options.Tags)
617
Jason Wu25c69ee2023-08-24 15:40:10 -0400618 testConfigAttributes := tradefed.GetTestConfigAttributes(
619 ctx,
620 m.testProperties.Test_config,
621 []string{},
622 m.testProperties.Auto_gen_config,
623 m.testProperties.Test_suites,
624 m.testProperties.Test_config_template,
625 nil,
626 nil,
627 )
Jason Wu6d8d44a2023-08-08 22:09:12 -0400628
Jason Wu25c69ee2023-08-24 15:40:10 -0400629 unitTest := m.testProperties.Test_options.Unit_test
Jason Wu6d8d44a2023-08-08 22:09:12 -0400630
Jason Wu25c69ee2023-08-24 15:40:10 -0400631 runs_on := bazel.MakeStringListAttribute(android.RunsOn(
632 m.ModuleBase.HostSupported(),
633 m.ModuleBase.DeviceSupported(),
634 (unitTest != nil && *unitTest)))
Jason Wu6d8d44a2023-08-08 22:09:12 -0400635
636 attrs := &bazelShTestAttributes{
637 Srcs: srcs,
638 Data: data,
Jason Wu25c69ee2023-08-24 15:40:10 -0400639 Data_bins: dataBins,
Jason Wu6d8d44a2023-08-08 22:09:12 -0400640 Tags: tags,
Jason Wu25c69ee2023-08-24 15:40:10 -0400641 Runs_on: runs_on,
642 TestConfigAttributes: testConfigAttributes,
Jason Wu6d8d44a2023-08-08 22:09:12 -0400643 }
644
645 props := bazel.BazelTargetModuleProperties{
646 Rule_class: "sh_test",
647 Bzl_load_location: "//build/bazel/rules:sh_test.bzl",
648 }
649 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
650}
651
frankfengc5b87492020-06-03 10:28:47 -0700652var Bool = proptools.Bool
Rob Seymour925aa092021-08-10 20:42:03 +0000653
654var _ snapshot.RelativeInstallPath = (*ShBinary)(nil)