blob: 1e27375d13e5d6c7a2e5ecdbb2aeebb87567ffd7 [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"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070027 "android/soong/cc"
Rob Seymour925aa092021-08-10 20:42:03 +000028 "android/soong/snapshot"
frankfengc5b87492020-06-03 10:28:47 -070029 "android/soong/tradefed"
Dan Willemsenb0552672019-01-25 16:04:11 -080030)
31
32// sh_binary is for shell scripts (and batch files) that are installed as
33// executable files into .../bin/
34//
35// Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary
36// instead.
37
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038var pctx = android.NewPackageContext("android/soong/sh")
39
Dan Willemsenb0552672019-01-25 16:04:11 -080040func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070041 pctx.Import("android/soong/android")
42
Paul Duffin56fb8ee2021-03-08 15:05:52 +000043 registerShBuildComponents(android.InitRegistrationContext)
Dan Willemsenb0552672019-01-25 16:04:11 -080044}
45
Paul Duffin56fb8ee2021-03-08 15:05:52 +000046func registerShBuildComponents(ctx android.RegistrationContext) {
47 ctx.RegisterModuleType("sh_binary", ShBinaryFactory)
48 ctx.RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
49 ctx.RegisterModuleType("sh_test", ShTestFactory)
50 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
51}
52
53// Test fixture preparer that will register most sh build components.
54//
55// Singletons and mutators should only be added here if they are needed for a majority of sh
56// module types, otherwise they should be added under a separate preparer to allow them to be
57// selected only when needed to reduce test execution time.
58//
59// Module types do not have much of an overhead unless they are used so this should include as many
60// module types as possible. The exceptions are those module types that require mutators and/or
61// singletons in order to function in which case they should be kept together in a separate
62// preparer.
63var PrepareForTestWithShBuildComponents = android.GroupFixturePreparers(
64 android.FixtureRegisterWithContext(registerShBuildComponents),
65)
66
Dan Willemsenb0552672019-01-25 16:04:11 -080067type shBinaryProperties struct {
68 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080069 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080070
71 // optional subdirectory under which this file is installed into
72 Sub_dir *string `android:"arch_variant"`
73
74 // optional name for the installed file. If unspecified, name of the module is used as the file name
75 Filename *string `android:"arch_variant"`
76
77 // when set to true, and filename property is not set, the name for the installed file
78 // is the same as the file name of the source file.
79 Filename_from_src *bool `android:"arch_variant"`
80
81 // Whether this module is directly installable to one of the partitions. Default: true.
82 Installable *bool
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -070083
84 // install symlinks to the binary
85 Symlinks []string `android:"arch_variant"`
Colin Crosscc83efb2020-08-21 14:25:33 -070086
87 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070088 // On device without a dedicated recovery partition, the module is only
89 // available after switching root into
90 // /first_stage_ramdisk. To expose the module before switching root, install
91 // the recovery variant instead.
Colin Crosscc83efb2020-08-21 14:25:33 -070092 Ramdisk_available *bool
93
Yifan Hong60e0cfb2020-10-21 15:17:56 -070094 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070095 // On device without a dedicated recovery partition, the module is only
96 // available after switching root into
97 // /first_stage_ramdisk. To expose the module before switching root, install
98 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -070099 Vendor_ramdisk_available *bool
100
Colin Crosscc83efb2020-08-21 14:25:33 -0700101 // Make this module available when building for recovery.
102 Recovery_available *bool
Dan Willemsenb0552672019-01-25 16:04:11 -0800103}
104
Julien Desprez9e7fc142019-03-08 11:07:05 -0800105type TestProperties struct {
106 // list of compatibility suites (for example "cts", "vts") that the module should be
107 // installed into.
108 Test_suites []string `android:"arch_variant"`
109
110 // the name of the test configuration (for example "AndroidTest.xml") that should be
111 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -0700112 Test_config *string `android:"path,arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700113
114 // list of files or filegroup modules that provide data that should be installed alongside
115 // the test.
116 Data []string `android:"path,arch_variant"`
frankfengc5b87492020-06-03 10:28:47 -0700117
118 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
119 // with root permission.
120 Require_root *bool
121
122 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
123 // should be installed with the module.
124 Test_config_template *string `android:"path,arch_variant"`
125
126 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
127 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
128 // explicitly.
129 Auto_gen_config *bool
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700130
131 // list of binary modules that should be installed alongside the test
132 Data_bins []string `android:"path,arch_variant"`
133
134 // list of library modules that should be installed alongside the test
135 Data_libs []string `android:"path,arch_variant"`
136
137 // list of device binary modules that should be installed alongside the test.
138 // Only available for host sh_test modules.
139 Data_device_bins []string `android:"path,arch_variant"`
140
141 // list of device library modules that should be installed alongside the test.
142 // Only available for host sh_test modules.
143 Data_device_libs []string `android:"path,arch_variant"`
Dan Shib40deac2021-05-24 12:04:54 -0700144
Makoto Onuki1725b202023-08-24 22:17:56 +0000145 // list of java modules that provide data that should be installed alongside the test.
146 Java_data []string
147
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900148 // Install the test into a folder named for the module in all test suites.
149 Per_testcase_directory *bool
150
Dan Shib40deac2021-05-24 12:04:54 -0700151 // Test options.
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800152 Test_options android.CommonTestOptions
Julien Desprez9e7fc142019-03-08 11:07:05 -0800153}
154
Dan Willemsenb0552672019-01-25 16:04:11 -0800155type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700156 android.ModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -0800157
158 properties shBinaryProperties
159
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700160 sourceFilePath android.Path
161 outputFilePath android.OutputPath
162 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800163}
164
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700165var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700166
Julien Desprez9e7fc142019-03-08 11:07:05 -0800167type ShTest struct {
168 ShBinary
169
170 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700171
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700172 installDir android.InstallPath
173
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800174 data []android.DataPath
frankfengc5b87492020-06-03 10:28:47 -0700175 testConfig android.Path
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700176
177 dataModules map[string]android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800178}
179
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700180func (s *ShBinary) HostToolPath() android.OptionalPath {
181 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700182}
183
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700184func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800185}
186
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700187func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800188 return s.outputFilePath
189}
190
Edward Liaw492ca052023-08-07 19:03:22 +0000191func (s *ShBinary) OutputFiles(tag string) (android.Paths, error) {
192 switch tag {
193 case "":
194 return android.Paths{s.outputFilePath}, nil
195 default:
196 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
197 }
198}
199
Dan Willemsenb0552672019-01-25 16:04:11 -0800200func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700201 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800202}
203
Rob Seymour925aa092021-08-10 20:42:03 +0000204func (s *ShBinary) RelativeInstallPath() string {
205 return s.SubDir()
206}
Dan Willemsenb0552672019-01-25 16:04:11 -0800207func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700208 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800209}
210
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700211func (s *ShBinary) Symlinks() []string {
212 return s.properties.Symlinks
213}
214
Colin Crosscc83efb2020-08-21 14:25:33 -0700215var _ android.ImageInterface = (*ShBinary)(nil)
216
217func (s *ShBinary) ImageMutatorBegin(ctx android.BaseModuleContext) {}
218
219func (s *ShBinary) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
220 return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk()
221}
222
223func (s *ShBinary) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
224 return proptools.Bool(s.properties.Ramdisk_available) || s.ModuleBase.InstallInRamdisk()
225}
226
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700227func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
228 return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.ModuleBase.InstallInVendorRamdisk()
229}
230
Inseob Kim08758f02021-04-08 21:13:22 +0900231func (s *ShBinary) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
232 return false
233}
234
Colin Crosscc83efb2020-08-21 14:25:33 -0700235func (s *ShBinary) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
236 return proptools.Bool(s.properties.Recovery_available) || s.ModuleBase.InstallInRecovery()
237}
238
239func (s *ShBinary) ExtraImageVariations(ctx android.BaseModuleContext) []string {
240 return nil
241}
242
243func (s *ShBinary) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
244}
245
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700246func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500247 if s.properties.Src == nil {
248 ctx.PropertyErrorf("src", "missing prebuilt source file")
249 }
250
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700251 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
252 filename := proptools.String(s.properties.Filename)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800253 filenameFromSrc := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800254 if filename == "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800255 if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800256 filename = s.sourceFilePath.Base()
257 } else {
258 filename = ctx.ModuleName()
259 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800260 } else if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800261 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
262 return
263 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700264 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800265
266 // This ensures that outputFilePath has the correct name for others to
267 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700268 ctx.Build(pctx, android.BuildParams{
269 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800270 Output: s.outputFilePath,
271 Input: s.sourceFilePath,
272 })
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000273 ctx.SetProvider(blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: []string{s.sourceFilePath.String()}})
Dan Willemsenb0552672019-01-25 16:04:11 -0800274}
275
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700276func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700277 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700278 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000279 if !s.Installable() {
280 s.SkipInstall()
281 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700282 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
Colin Cross94bf5182021-11-09 17:21:14 -0800283 for _, symlink := range s.Symlinks() {
284 ctx.InstallSymlink(installDir, symlink, s.installedFile)
285 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700286}
287
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700288func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
289 return []android.AndroidMkEntries{android.AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800290 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700291 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500292 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700293 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700294 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700295 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700296 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000297 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !s.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700298 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800299 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900300 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800301}
302
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700303func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700304 entries.SetString("LOCAL_MODULE_SUFFIX", "")
305 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700306 if len(s.properties.Symlinks) > 0 {
307 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
308 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700309}
310
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700311type dependencyTag struct {
312 blueprint.BaseDependencyTag
313 name string
314}
315
316var (
317 shTestDataBinsTag = dependencyTag{name: "dataBins"}
318 shTestDataLibsTag = dependencyTag{name: "dataLibs"}
319 shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
320 shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"}
Makoto Onuki1725b202023-08-24 22:17:56 +0000321 shTestJavaDataTag = dependencyTag{name: "javaData"}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700322)
323
324var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}}
325
326func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) {
327 s.ShBinary.DepsMutator(ctx)
328
329 ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...)
330 ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...),
331 shTestDataLibsTag, s.testProperties.Data_libs...)
Liz Kammer3bf97bd2022-04-26 09:38:20 -0400332 if ctx.Target().Os.Class == android.Host && len(ctx.Config().Targets[android.Android]) > 0 {
Jaewoong Jung642916f2020-10-09 17:25:15 -0700333 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700334 ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...)
335 ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...),
336 shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...)
Makoto Onuki1725b202023-08-24 22:17:56 +0000337
338 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
339 ctx.AddVariationDependencies(javaDataVariation, shTestJavaDataTag, s.testProperties.Java_data...)
340
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700341 } else if ctx.Target().Os.Class != android.Host {
342 if len(s.testProperties.Data_device_bins) > 0 {
343 ctx.PropertyErrorf("data_device_bins", "only available for host modules")
344 }
345 if len(s.testProperties.Data_device_libs) > 0 {
346 ctx.PropertyErrorf("data_device_libs", "only available for host modules")
347 }
Makoto Onuki1725b202023-08-24 22:17:56 +0000348 if len(s.testProperties.Java_data) > 0 {
349 ctx.PropertyErrorf("Java_data", "only available for host modules")
350 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700351 }
352}
353
354func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) {
355 if _, exists := s.dataModules[relPath]; exists {
356 ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s",
357 relPath, s.dataModules[relPath].String(), path.String())
358 return
359 }
360 s.dataModules[relPath] = path
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800361 s.data = append(s.data, android.DataPath{SrcPath: path})
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700362}
363
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700364func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700365 s.ShBinary.generateAndroidBuildActions(ctx)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800366
367 expandedData := android.PathsForModuleSrc(ctx, s.testProperties.Data)
368 // Emulate the data property for java_data dependencies.
369 for _, javaData := range ctx.GetDirectDepsWithTag(shTestJavaDataTag) {
370 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
371 }
372 for _, d := range expandedData {
373 s.data = append(s.data, android.DataPath{SrcPath: d})
374 }
375
Colin Cross7c7c1142019-07-29 16:46:49 -0700376 testDir := "nativetest"
377 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
378 testDir = "nativetest64"
379 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700380 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700381 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
382 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
383 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
384 }
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700385 if s.SubDir() != "" {
386 // Don't add the module name to the installation path if sub_dir is specified for backward
387 // compatibility.
388 s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir())
389 } else {
390 s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name())
391 }
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 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800440 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700441 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 })
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800456
457 installedData := ctx.InstallTestData(s.installDir, s.data)
458 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath, installedData...)
459
Aditya Choudhary9b593522023-10-06 19:54:58 +0000460 ctx.SetProvider(testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700461}
462
Colin Cross7c7c1142019-07-29 16:46:49 -0700463func (s *ShTest) InstallInData() bool {
464 return true
465}
466
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700467func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
468 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700469 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700470 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500471 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700472 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700473 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700474 s.customAndroidMkEntries(entries)
Colin Crossc68db4b2021-11-11 18:59:15 -0800475 entries.SetPath("LOCAL_MODULE_PATH", s.installDir)
Liz Kammer57f5b332020-11-24 12:42:58 -0800476 entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700477 if s.testConfig != nil {
478 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700479 }
yangbill22bafec2022-02-11 18:06:07 +0800480 if s.testProperties.Data_bins != nil {
481 entries.AddStrings("LOCAL_TEST_DATA_BINS", s.testProperties.Data_bins...)
482 }
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900483 entries.SetBoolIfTrue("LOCAL_COMPATIBILITY_PER_TESTCASE_DIRECTORY", Bool(s.testProperties.Per_testcase_directory))
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800484
485 s.testProperties.Test_options.SetAndroidMkEntries(entries)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700486 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700487 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900488 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800489}
490
Colin Cross8ff10582023-12-07 13:10:56 -0800491func initShBinaryModule(s *ShBinary) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800492 s.AddProperties(&s.properties)
493}
494
Patrice Arrudae1034192019-03-11 13:20:17 -0700495// sh_binary is for a shell script or batch file to be installed as an
496// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700497func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800498 module := &ShBinary{}
Colin Cross8ff10582023-12-07 13:10:56 -0800499 initShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700500 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800501 return module
502}
503
Patrice Arrudae1034192019-03-11 13:20:17 -0700504// sh_binary_host is for a shell script to be installed as an executable binary
505// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700506func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800507 module := &ShBinary{}
Colin Cross8ff10582023-12-07 13:10:56 -0800508 initShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700509 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800510 return module
511}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800512
Jaewoong Jung61a83682019-07-01 09:08:50 -0700513// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700514func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800515 module := &ShTest{}
Colin Cross8ff10582023-12-07 13:10:56 -0800516 initShBinaryModule(&module.ShBinary)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800517 module.AddProperties(&module.testProperties)
518
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700519 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800520 return module
521}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700522
523// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700524func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700525 module := &ShTest{}
Colin Cross8ff10582023-12-07 13:10:56 -0800526 initShBinaryModule(&module.ShBinary)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700527 module.AddProperties(&module.testProperties)
Julien Desprez06f13992021-06-28 13:41:43 -0700528 // Default sh_test_host to unit_tests = true
529 if module.testProperties.Test_options.Unit_test == nil {
530 module.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
531 }
Jaewoong Jung61a83682019-07-01 09:08:50 -0700532
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700533 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700534 return module
535}
frankfengc5b87492020-06-03 10:28:47 -0700536
537var Bool = proptools.Bool
Rob Seymour925aa092021-08-10 20:42:03 +0000538
539var _ snapshot.RelativeInstallPath = (*ShBinary)(nil)