blob: 1bebc60be9dbf3891ec045296131ad9d9c810752 [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"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070020 "sort"
Julien Desprez9e7fc142019-03-08 11:07:05 -080021 "strings"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070022
Aditya Choudhary9b593522023-10-06 19:54:58 +000023 "android/soong/testing"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070024 "github.com/google/blueprint"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070025 "github.com/google/blueprint/proptools"
26
27 "android/soong/android"
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +000028 "android/soong/bazel"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070029 "android/soong/cc"
Rob Seymour925aa092021-08-10 20:42:03 +000030 "android/soong/snapshot"
frankfengc5b87492020-06-03 10:28:47 -070031 "android/soong/tradefed"
Dan Willemsenb0552672019-01-25 16:04:11 -080032)
33
34// sh_binary is for shell scripts (and batch files) that are installed as
35// executable files into .../bin/
36//
37// Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary
38// instead.
39
Jaewoong Jung4b79e982020-06-01 10:45:49 -070040var pctx = android.NewPackageContext("android/soong/sh")
41
Dan Willemsenb0552672019-01-25 16:04:11 -080042func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070043 pctx.Import("android/soong/android")
44
Paul Duffin56fb8ee2021-03-08 15:05:52 +000045 registerShBuildComponents(android.InitRegistrationContext)
Dan Willemsenb0552672019-01-25 16:04:11 -080046}
47
Paul Duffin56fb8ee2021-03-08 15:05:52 +000048func registerShBuildComponents(ctx android.RegistrationContext) {
49 ctx.RegisterModuleType("sh_binary", ShBinaryFactory)
50 ctx.RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
51 ctx.RegisterModuleType("sh_test", ShTestFactory)
52 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
53}
54
55// Test fixture preparer that will register most sh build components.
56//
57// Singletons and mutators should only be added here if they are needed for a majority of sh
58// module types, otherwise they should be added under a separate preparer to allow them to be
59// selected only when needed to reduce test execution time.
60//
61// Module types do not have much of an overhead unless they are used so this should include as many
62// module types as possible. The exceptions are those module types that require mutators and/or
63// singletons in order to function in which case they should be kept together in a separate
64// preparer.
65var PrepareForTestWithShBuildComponents = android.GroupFixturePreparers(
66 android.FixtureRegisterWithContext(registerShBuildComponents),
67)
68
Dan Willemsenb0552672019-01-25 16:04:11 -080069type shBinaryProperties struct {
70 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080071 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080072
73 // optional subdirectory under which this file is installed into
74 Sub_dir *string `android:"arch_variant"`
75
76 // optional name for the installed file. If unspecified, name of the module is used as the file name
77 Filename *string `android:"arch_variant"`
78
79 // when set to true, and filename property is not set, the name for the installed file
80 // is the same as the file name of the source file.
81 Filename_from_src *bool `android:"arch_variant"`
82
83 // Whether this module is directly installable to one of the partitions. Default: true.
84 Installable *bool
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -070085
86 // install symlinks to the binary
87 Symlinks []string `android:"arch_variant"`
Colin Crosscc83efb2020-08-21 14:25:33 -070088
89 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070090 // On device without a dedicated recovery partition, the module is only
91 // available after switching root into
92 // /first_stage_ramdisk. To expose the module before switching root, install
93 // the recovery variant instead.
Colin Crosscc83efb2020-08-21 14:25:33 -070094 Ramdisk_available *bool
95
Yifan Hong60e0cfb2020-10-21 15:17:56 -070096 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070097 // On device without a dedicated recovery partition, the module is only
98 // available after switching root into
99 // /first_stage_ramdisk. To expose the module before switching root, install
100 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700101 Vendor_ramdisk_available *bool
102
Colin Crosscc83efb2020-08-21 14:25:33 -0700103 // Make this module available when building for recovery.
104 Recovery_available *bool
Dan Willemsenb0552672019-01-25 16:04:11 -0800105}
106
Julien Desprez9e7fc142019-03-08 11:07:05 -0800107type TestProperties struct {
108 // list of compatibility suites (for example "cts", "vts") that the module should be
109 // installed into.
110 Test_suites []string `android:"arch_variant"`
111
112 // the name of the test configuration (for example "AndroidTest.xml") that should be
113 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -0700114 Test_config *string `android:"path,arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700115
116 // list of files or filegroup modules that provide data that should be installed alongside
117 // the test.
118 Data []string `android:"path,arch_variant"`
frankfengc5b87492020-06-03 10:28:47 -0700119
120 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
121 // with root permission.
122 Require_root *bool
123
124 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
125 // should be installed with the module.
126 Test_config_template *string `android:"path,arch_variant"`
127
128 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
129 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
130 // explicitly.
131 Auto_gen_config *bool
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700132
133 // list of binary modules that should be installed alongside the test
134 Data_bins []string `android:"path,arch_variant"`
135
136 // list of library modules that should be installed alongside the test
137 Data_libs []string `android:"path,arch_variant"`
138
139 // list of device binary modules that should be installed alongside the test.
140 // Only available for host sh_test modules.
141 Data_device_bins []string `android:"path,arch_variant"`
142
143 // list of device library modules that should be installed alongside the test.
144 // Only available for host sh_test modules.
145 Data_device_libs []string `android:"path,arch_variant"`
Dan Shib40deac2021-05-24 12:04:54 -0700146
Makoto Onuki1725b202023-08-24 22:17:56 +0000147 // list of java modules that provide data that should be installed alongside the test.
148 Java_data []string
149
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900150 // Install the test into a folder named for the module in all test suites.
151 Per_testcase_directory *bool
152
Dan Shib40deac2021-05-24 12:04:54 -0700153 // Test options.
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800154 Test_options android.CommonTestOptions
Julien Desprez9e7fc142019-03-08 11:07:05 -0800155}
156
Dan Willemsenb0552672019-01-25 16:04:11 -0800157type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700158 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500159 android.BazelModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -0800160
161 properties shBinaryProperties
162
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700163 sourceFilePath android.Path
164 outputFilePath android.OutputPath
165 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800166}
167
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700168var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700169
Julien Desprez9e7fc142019-03-08 11:07:05 -0800170type ShTest struct {
171 ShBinary
172
173 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700174
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700175 installDir android.InstallPath
176
frankfengc5b87492020-06-03 10:28:47 -0700177 data android.Paths
178 testConfig android.Path
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700179
180 dataModules map[string]android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800181}
182
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700183func (s *ShBinary) HostToolPath() android.OptionalPath {
184 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700185}
186
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700187func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800188}
189
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700190func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800191 return s.outputFilePath
192}
193
Edward Liaw492ca052023-08-07 19:03:22 +0000194func (s *ShBinary) OutputFiles(tag string) (android.Paths, error) {
195 switch tag {
196 case "":
197 return android.Paths{s.outputFilePath}, nil
198 default:
199 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
200 }
201}
202
Dan Willemsenb0552672019-01-25 16:04:11 -0800203func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700204 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800205}
206
Rob Seymour925aa092021-08-10 20:42:03 +0000207func (s *ShBinary) RelativeInstallPath() string {
208 return s.SubDir()
209}
Dan Willemsenb0552672019-01-25 16:04:11 -0800210func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700211 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800212}
213
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700214func (s *ShBinary) Symlinks() []string {
215 return s.properties.Symlinks
216}
217
Colin Crosscc83efb2020-08-21 14:25:33 -0700218var _ android.ImageInterface = (*ShBinary)(nil)
219
220func (s *ShBinary) ImageMutatorBegin(ctx android.BaseModuleContext) {}
221
222func (s *ShBinary) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
223 return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk()
224}
225
226func (s *ShBinary) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
227 return proptools.Bool(s.properties.Ramdisk_available) || s.ModuleBase.InstallInRamdisk()
228}
229
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700230func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
231 return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.ModuleBase.InstallInVendorRamdisk()
232}
233
Inseob Kim08758f02021-04-08 21:13:22 +0900234func (s *ShBinary) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
235 return false
236}
237
Colin Crosscc83efb2020-08-21 14:25:33 -0700238func (s *ShBinary) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
239 return proptools.Bool(s.properties.Recovery_available) || s.ModuleBase.InstallInRecovery()
240}
241
242func (s *ShBinary) ExtraImageVariations(ctx android.BaseModuleContext) []string {
243 return nil
244}
245
246func (s *ShBinary) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
247}
248
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700249func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500250 if s.properties.Src == nil {
251 ctx.PropertyErrorf("src", "missing prebuilt source file")
252 }
253
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700254 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
255 filename := proptools.String(s.properties.Filename)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800256 filenameFromSrc := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800257 if filename == "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800258 if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800259 filename = s.sourceFilePath.Base()
260 } else {
261 filename = ctx.ModuleName()
262 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800263 } else if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800264 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
265 return
266 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700267 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800268
269 // This ensures that outputFilePath has the correct name for others to
270 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700271 ctx.Build(pctx, android.BuildParams{
272 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800273 Output: s.outputFilePath,
274 Input: s.sourceFilePath,
275 })
276}
277
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700278func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700279 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700280 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000281 if !s.Installable() {
282 s.SkipInstall()
283 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700284 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
Colin Cross94bf5182021-11-09 17:21:14 -0800285 for _, symlink := range s.Symlinks() {
286 ctx.InstallSymlink(installDir, symlink, s.installedFile)
287 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700288}
289
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700290func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
291 return []android.AndroidMkEntries{android.AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800292 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700293 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500294 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700295 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700296 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700297 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700298 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000299 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !s.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700300 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800301 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900302 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800303}
304
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700305func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700306 entries.SetString("LOCAL_MODULE_SUFFIX", "")
307 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700308 if len(s.properties.Symlinks) > 0 {
309 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
310 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700311}
312
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700313type dependencyTag struct {
314 blueprint.BaseDependencyTag
315 name string
316}
317
318var (
319 shTestDataBinsTag = dependencyTag{name: "dataBins"}
320 shTestDataLibsTag = dependencyTag{name: "dataLibs"}
321 shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
322 shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"}
Makoto Onuki1725b202023-08-24 22:17:56 +0000323 shTestJavaDataTag = dependencyTag{name: "javaData"}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700324)
325
326var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}}
327
328func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) {
329 s.ShBinary.DepsMutator(ctx)
330
331 ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...)
332 ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...),
333 shTestDataLibsTag, s.testProperties.Data_libs...)
Liz Kammer3bf97bd2022-04-26 09:38:20 -0400334 if ctx.Target().Os.Class == android.Host && len(ctx.Config().Targets[android.Android]) > 0 {
Jaewoong Jung642916f2020-10-09 17:25:15 -0700335 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700336 ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...)
337 ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...),
338 shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...)
Makoto Onuki1725b202023-08-24 22:17:56 +0000339
340 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
341 ctx.AddVariationDependencies(javaDataVariation, shTestJavaDataTag, s.testProperties.Java_data...)
342
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700343 } else if ctx.Target().Os.Class != android.Host {
344 if len(s.testProperties.Data_device_bins) > 0 {
345 ctx.PropertyErrorf("data_device_bins", "only available for host modules")
346 }
347 if len(s.testProperties.Data_device_libs) > 0 {
348 ctx.PropertyErrorf("data_device_libs", "only available for host modules")
349 }
Makoto Onuki1725b202023-08-24 22:17:56 +0000350 if len(s.testProperties.Java_data) > 0 {
351 ctx.PropertyErrorf("Java_data", "only available for host modules")
352 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700353 }
354}
355
356func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) {
357 if _, exists := s.dataModules[relPath]; exists {
358 ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s",
359 relPath, s.dataModules[relPath].String(), path.String())
360 return
361 }
362 s.dataModules[relPath] = path
363}
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)
367 testDir := "nativetest"
368 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
369 testDir = "nativetest64"
370 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700371 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700372 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
373 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
374 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
375 }
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700376 if s.SubDir() != "" {
377 // Don't add the module name to the installation path if sub_dir is specified for backward
378 // compatibility.
379 s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir())
380 } else {
381 s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name())
382 }
383 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700384
Makoto Onuki1725b202023-08-24 22:17:56 +0000385 expandedData := android.PathsForModuleSrc(ctx, s.testProperties.Data)
386
387 // Emulate the data property for java_data dependencies.
388 for _, javaData := range ctx.GetDirectDepsWithTag(shTestJavaDataTag) {
389 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
390 }
391 s.data = expandedData
frankfengc5b87492020-06-03 10:28:47 -0700392
393 var configs []tradefed.Config
394 if Bool(s.testProperties.Require_root) {
395 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
396 } else {
397 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
398 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
399 }
frankfengbe6ae772020-09-28 13:22:57 -0700400 if len(s.testProperties.Data_device_bins) > 0 {
401 moduleName := s.Name()
402 remoteDir := "/data/local/tests/unrestricted/" + moduleName + "/"
403 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
404 for _, bin := range s.testProperties.Data_device_bins {
405 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: remoteDir + bin})
406 }
407 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options})
408 }
Cole Faust21680542022-12-07 18:18:37 -0800409 s.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
410 TestConfigProp: s.testProperties.Test_config,
411 TestConfigTemplateProp: s.testProperties.Test_config_template,
412 TestSuites: s.testProperties.Test_suites,
413 Config: configs,
414 AutoGenConfig: s.testProperties.Auto_gen_config,
415 OutputFileName: s.outputFilePath.Base(),
416 DeviceTemplate: "${ShellTestConfigTemplate}",
417 HostTemplate: "${ShellTestConfigTemplate}",
418 })
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700419
420 s.dataModules = make(map[string]android.Path)
421 ctx.VisitDirectDeps(func(dep android.Module) {
422 depTag := ctx.OtherModuleDependencyTag(dep)
423 switch depTag {
424 case shTestDataBinsTag, shTestDataDeviceBinsTag:
Anton Hansson2f6422c2020-12-31 13:42:10 +0000425 path := android.OutputFileForModule(ctx, dep, "")
426 s.addToDataModules(ctx, path.Base(), path)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700427 case shTestDataLibsTag, shTestDataDeviceLibsTag:
428 if cc, isCc := dep.(*cc.Module); isCc {
429 // Copy to an intermediate output directory to append "lib[64]" to the path,
430 // so that it's compatible with the default rpath values.
431 var relPath string
432 if cc.Arch().ArchType.Multilib == "lib64" {
433 relPath = filepath.Join("lib64", cc.OutputFile().Path().Base())
434 } else {
435 relPath = filepath.Join("lib", cc.OutputFile().Path().Base())
436 }
437 if _, exist := s.dataModules[relPath]; exist {
438 return
439 }
440 relocatedLib := android.PathForModuleOut(ctx, "relocated", relPath)
441 ctx.Build(pctx, android.BuildParams{
442 Rule: android.Cp,
443 Input: cc.OutputFile().Path(),
444 Output: relocatedLib,
445 })
446 s.addToDataModules(ctx, relPath, relocatedLib)
447 return
448 }
449 property := "data_libs"
450 if depTag == shTestDataDeviceBinsTag {
451 property = "data_device_libs"
452 }
453 ctx.PropertyErrorf(property, "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
454 }
455 })
Aditya Choudhary9b593522023-10-06 19:54:58 +0000456 ctx.SetProvider(testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700457}
458
Colin Cross7c7c1142019-07-29 16:46:49 -0700459func (s *ShTest) InstallInData() bool {
460 return true
461}
462
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700463func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
464 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700465 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700466 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500467 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700468 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700469 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700470 s.customAndroidMkEntries(entries)
Colin Crossc68db4b2021-11-11 18:59:15 -0800471 entries.SetPath("LOCAL_MODULE_PATH", s.installDir)
Liz Kammer57f5b332020-11-24 12:42:58 -0800472 entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700473 if s.testConfig != nil {
474 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700475 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700476 for _, d := range s.data {
477 rel := d.Rel()
478 path := d.String()
479 if !strings.HasSuffix(path, rel) {
480 panic(fmt.Errorf("path %q does not end with %q", path, rel))
481 }
482 path = strings.TrimSuffix(path, rel)
483 entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700484 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700485 relPaths := make([]string, 0)
486 for relPath, _ := range s.dataModules {
487 relPaths = append(relPaths, relPath)
488 }
489 sort.Strings(relPaths)
490 for _, relPath := range relPaths {
491 dir := strings.TrimSuffix(s.dataModules[relPath].String(), relPath)
492 entries.AddStrings("LOCAL_TEST_DATA", dir+":"+relPath)
493 }
yangbill22bafec2022-02-11 18:06:07 +0800494 if s.testProperties.Data_bins != nil {
495 entries.AddStrings("LOCAL_TEST_DATA_BINS", s.testProperties.Data_bins...)
496 }
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900497 entries.SetBoolIfTrue("LOCAL_COMPATIBILITY_PER_TESTCASE_DIRECTORY", Bool(s.testProperties.Per_testcase_directory))
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800498
499 s.testProperties.Test_options.SetAndroidMkEntries(entries)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700500 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700501 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900502 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800503}
504
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400505func initShBinaryModule(s *ShBinary, useBazel bool) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800506 s.AddProperties(&s.properties)
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400507 if useBazel {
508 android.InitBazelModule(s)
509 }
Dan Willemsenb0552672019-01-25 16:04:11 -0800510}
511
Patrice Arrudae1034192019-03-11 13:20:17 -0700512// sh_binary is for a shell script or batch file to be installed as an
513// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700514func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800515 module := &ShBinary{}
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400516 initShBinaryModule(module, true)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700517 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800518 return module
519}
520
Patrice Arrudae1034192019-03-11 13:20:17 -0700521// sh_binary_host is for a shell script to be installed as an executable binary
522// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700523func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800524 module := &ShBinary{}
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400525 initShBinaryModule(module, true)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700526 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800527 return module
528}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800529
Jaewoong Jung61a83682019-07-01 09:08:50 -0700530// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700531func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800532 module := &ShTest{}
Jason Wu6d8d44a2023-08-08 22:09:12 -0400533 initShBinaryModule(&module.ShBinary, true)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800534 module.AddProperties(&module.testProperties)
535
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700536 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800537 return module
538}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700539
540// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700541func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700542 module := &ShTest{}
Jason Wu6d8d44a2023-08-08 22:09:12 -0400543 initShBinaryModule(&module.ShBinary, true)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700544 module.AddProperties(&module.testProperties)
Julien Desprez06f13992021-06-28 13:41:43 -0700545 // Default sh_test_host to unit_tests = true
546 if module.testProperties.Test_options.Unit_test == nil {
547 module.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
548 }
Jaewoong Jung61a83682019-07-01 09:08:50 -0700549
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700550 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700551 return module
552}
frankfengc5b87492020-06-03 10:28:47 -0700553
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000554type bazelShBinaryAttributes struct {
Yu Liub3024552021-11-23 14:53:41 -0800555 Srcs bazel.LabelListAttribute
Liz Kammer48c6ead2021-12-21 15:36:11 -0500556 Filename *string
557 Sub_dir *string
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000558 // Bazel also supports the attributes below, but (so far) these are not required for Bionic
559 // deps
560 // data
561 // args
562 // compatible_with
563 // deprecation
564 // distribs
565 // env
566 // exec_compatible_with
567 // exec_properties
568 // features
569 // licenses
570 // output_licenses
571 // restricted_to
572 // tags
573 // target_compatible_with
574 // testonly
575 // toolchains
576 // visibility
577}
578
Jason Wu6d8d44a2023-08-08 22:09:12 -0400579type bazelShTestAttributes struct {
Jason Wu25c69ee2023-08-24 15:40:10 -0400580 Srcs bazel.LabelListAttribute
581 Data bazel.LabelListAttribute
582 Data_bins bazel.LabelListAttribute
583 Tags bazel.StringListAttribute
584 Runs_on bazel.StringListAttribute
585 tradefed.TestConfigAttributes
Jason Wu6d8d44a2023-08-08 22:09:12 -0400586}
587
Chris Parsons637458d2023-09-19 20:09:00 +0000588func (m *ShBinary) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -0400589 srcs := bazel.MakeLabelListAttribute(
590 android.BazelLabelForModuleSrc(ctx, []string{*m.properties.Src}))
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000591
Liz Kammer48c6ead2021-12-21 15:36:11 -0500592 var filename *string
Yu Liub3024552021-11-23 14:53:41 -0800593 if m.properties.Filename != nil {
Liz Kammer48c6ead2021-12-21 15:36:11 -0500594 filename = m.properties.Filename
Yu Liub3024552021-11-23 14:53:41 -0800595 }
596
Liz Kammer48c6ead2021-12-21 15:36:11 -0500597 var subDir *string
Yu Liub3024552021-11-23 14:53:41 -0800598 if m.properties.Sub_dir != nil {
Liz Kammer48c6ead2021-12-21 15:36:11 -0500599 subDir = m.properties.Sub_dir
Yu Liub3024552021-11-23 14:53:41 -0800600 }
601
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000602 attrs := &bazelShBinaryAttributes{
Yu Liub3024552021-11-23 14:53:41 -0800603 Srcs: srcs,
604 Filename: filename,
605 Sub_dir: subDir,
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000606 }
607
Liz Kammerfc46bc12021-02-19 11:06:17 -0500608 props := bazel.BazelTargetModuleProperties{
Yu Liub3024552021-11-23 14:53:41 -0800609 Rule_class: "sh_binary",
610 Bzl_load_location: "//build/bazel/rules:sh_binary.bzl",
Liz Kammerfc46bc12021-02-19 11:06:17 -0500611 }
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000612
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000613 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000614}
615
Chris Parsons637458d2023-09-19 20:09:00 +0000616func (m *ShTest) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Jason Wu6d8d44a2023-08-08 22:09:12 -0400617 srcs := bazel.MakeLabelListAttribute(
618 android.BazelLabelForModuleSrc(ctx, []string{*m.properties.Src}))
619
Jason Wu25c69ee2023-08-24 15:40:10 -0400620 dataBins := bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, m.testProperties.Data_bins))
Jason Wu6d8d44a2023-08-08 22:09:12 -0400621
Jason Wu25c69ee2023-08-24 15:40:10 -0400622 var combinedData bazel.LabelList
623 combinedData.Append(android.BazelLabelForModuleSrc(ctx, m.testProperties.Data))
624 combinedData.Append(android.BazelLabelForModuleDeps(ctx, m.testProperties.Data_bins))
625 combinedData.Append(android.BazelLabelForModuleDeps(ctx, m.testProperties.Data_libs))
626 data := bazel.MakeLabelListAttribute(combinedData)
Jason Wu6d8d44a2023-08-08 22:09:12 -0400627
628 tags := bazel.MakeStringListAttribute(
629 m.testProperties.Test_options.Tags)
630
Jason Wu25c69ee2023-08-24 15:40:10 -0400631 testConfigAttributes := tradefed.GetTestConfigAttributes(
632 ctx,
633 m.testProperties.Test_config,
634 []string{},
635 m.testProperties.Auto_gen_config,
636 m.testProperties.Test_suites,
637 m.testProperties.Test_config_template,
638 nil,
639 nil,
640 )
Jason Wu6d8d44a2023-08-08 22:09:12 -0400641
Jason Wu25c69ee2023-08-24 15:40:10 -0400642 unitTest := m.testProperties.Test_options.Unit_test
Jason Wu6d8d44a2023-08-08 22:09:12 -0400643
Jason Wu25c69ee2023-08-24 15:40:10 -0400644 runs_on := bazel.MakeStringListAttribute(android.RunsOn(
645 m.ModuleBase.HostSupported(),
646 m.ModuleBase.DeviceSupported(),
647 (unitTest != nil && *unitTest)))
Jason Wu6d8d44a2023-08-08 22:09:12 -0400648
649 attrs := &bazelShTestAttributes{
650 Srcs: srcs,
651 Data: data,
Jason Wu25c69ee2023-08-24 15:40:10 -0400652 Data_bins: dataBins,
Jason Wu6d8d44a2023-08-08 22:09:12 -0400653 Tags: tags,
Jason Wu25c69ee2023-08-24 15:40:10 -0400654 Runs_on: runs_on,
655 TestConfigAttributes: testConfigAttributes,
Jason Wu6d8d44a2023-08-08 22:09:12 -0400656 }
657
658 props := bazel.BazelTargetModuleProperties{
659 Rule_class: "sh_test",
660 Bzl_load_location: "//build/bazel/rules:sh_test.bzl",
661 }
662 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
663}
664
frankfengc5b87492020-06-03 10:28:47 -0700665var Bool = proptools.Bool
Rob Seymour925aa092021-08-10 20:42:03 +0000666
667var _ snapshot.RelativeInstallPath = (*ShBinary)(nil)