blob: 3f82560c0c54766d4fbf4e9096f98f86289ecc94 [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 (
Colin Cross7c7c1142019-07-29 16:46:49 -070018 "path/filepath"
Julien Desprez9e7fc142019-03-08 11:07:05 -080019 "strings"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070020
Aditya Choudhary9b593522023-10-06 19:54:58 +000021 "android/soong/testing"
Kiyoung Kim37693d02024-04-04 09:56:15 +090022
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"
frankfengc5b87492020-06-03 10:28:47 -070028 "android/soong/tradefed"
Dan Willemsenb0552672019-01-25 16:04:11 -080029)
30
31// sh_binary is for shell scripts (and batch files) that are installed as
32// executable files into .../bin/
33//
34// Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary
35// instead.
36
Jaewoong Jung4b79e982020-06-01 10:45:49 -070037var pctx = android.NewPackageContext("android/soong/sh")
38
Dan Willemsenb0552672019-01-25 16:04:11 -080039func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070040 pctx.Import("android/soong/android")
41
Paul Duffin56fb8ee2021-03-08 15:05:52 +000042 registerShBuildComponents(android.InitRegistrationContext)
Dan Willemsenb0552672019-01-25 16:04:11 -080043}
44
Paul Duffin56fb8ee2021-03-08 15:05:52 +000045func registerShBuildComponents(ctx android.RegistrationContext) {
46 ctx.RegisterModuleType("sh_binary", ShBinaryFactory)
47 ctx.RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
48 ctx.RegisterModuleType("sh_test", ShTestFactory)
49 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
50}
51
52// Test fixture preparer that will register most sh build components.
53//
54// Singletons and mutators should only be added here if they are needed for a majority of sh
55// module types, otherwise they should be added under a separate preparer to allow them to be
56// selected only when needed to reduce test execution time.
57//
58// Module types do not have much of an overhead unless they are used so this should include as many
59// module types as possible. The exceptions are those module types that require mutators and/or
60// singletons in order to function in which case they should be kept together in a separate
61// preparer.
62var PrepareForTestWithShBuildComponents = android.GroupFixturePreparers(
63 android.FixtureRegisterWithContext(registerShBuildComponents),
64)
65
Dan Willemsenb0552672019-01-25 16:04:11 -080066type shBinaryProperties struct {
67 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080068 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080069
70 // optional subdirectory under which this file is installed into
71 Sub_dir *string `android:"arch_variant"`
72
73 // optional name for the installed file. If unspecified, name of the module is used as the file name
74 Filename *string `android:"arch_variant"`
75
76 // when set to true, and filename property is not set, the name for the installed file
77 // is the same as the file name of the source file.
78 Filename_from_src *bool `android:"arch_variant"`
79
80 // Whether this module is directly installable to one of the partitions. Default: true.
81 Installable *bool
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -070082
83 // install symlinks to the binary
84 Symlinks []string `android:"arch_variant"`
Colin Crosscc83efb2020-08-21 14:25:33 -070085
86 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070087 // On device without a dedicated recovery partition, the module is only
88 // available after switching root into
89 // /first_stage_ramdisk. To expose the module before switching root, install
90 // the recovery variant instead.
Colin Crosscc83efb2020-08-21 14:25:33 -070091 Ramdisk_available *bool
92
Yifan Hong60e0cfb2020-10-21 15:17:56 -070093 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070094 // On device without a dedicated recovery partition, the module is only
95 // available after switching root into
96 // /first_stage_ramdisk. To expose the module before switching root, install
97 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -070098 Vendor_ramdisk_available *bool
99
Colin Crosscc83efb2020-08-21 14:25:33 -0700100 // Make this module available when building for recovery.
101 Recovery_available *bool
Jihoon Kang71825162024-06-11 23:24:13 +0000102
103 // The name of the image this module is built for
104 ImageVariation string `blueprint:"mutated"`
105
106 // Suffix for the name of Android.mk entries generated by this module
107 SubName string `blueprint:"mutated"`
Dan Willemsenb0552672019-01-25 16:04:11 -0800108}
109
Julien Desprez9e7fc142019-03-08 11:07:05 -0800110type TestProperties struct {
111 // list of compatibility suites (for example "cts", "vts") that the module should be
112 // installed into.
113 Test_suites []string `android:"arch_variant"`
114
115 // the name of the test configuration (for example "AndroidTest.xml") that should be
116 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -0700117 Test_config *string `android:"path,arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700118
119 // list of files or filegroup modules that provide data that should be installed alongside
120 // the test.
121 Data []string `android:"path,arch_variant"`
frankfengc5b87492020-06-03 10:28:47 -0700122
123 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
124 // with root permission.
125 Require_root *bool
126
127 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
128 // should be installed with the module.
129 Test_config_template *string `android:"path,arch_variant"`
130
131 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
132 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
133 // explicitly.
134 Auto_gen_config *bool
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700135
136 // list of binary modules that should be installed alongside the test
137 Data_bins []string `android:"path,arch_variant"`
138
139 // list of library modules that should be installed alongside the test
140 Data_libs []string `android:"path,arch_variant"`
141
142 // list of device binary modules that should be installed alongside the test.
143 // Only available for host sh_test modules.
144 Data_device_bins []string `android:"path,arch_variant"`
145
146 // list of device library modules that should be installed alongside the test.
147 // Only available for host sh_test modules.
148 Data_device_libs []string `android:"path,arch_variant"`
Dan Shib40deac2021-05-24 12:04:54 -0700149
Makoto Onuki1725b202023-08-24 22:17:56 +0000150 // list of java modules that provide data that should be installed alongside the test.
151 Java_data []string
152
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900153 // Install the test into a folder named for the module in all test suites.
154 Per_testcase_directory *bool
155
Dan Shib40deac2021-05-24 12:04:54 -0700156 // Test options.
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800157 Test_options android.CommonTestOptions
Julien Desprez9e7fc142019-03-08 11:07:05 -0800158}
159
Dan Willemsenb0552672019-01-25 16:04:11 -0800160type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700161 android.ModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -0800162
163 properties shBinaryProperties
164
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700165 sourceFilePath android.Path
166 outputFilePath android.OutputPath
167 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800168}
169
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700170var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700171
Julien Desprez9e7fc142019-03-08 11:07:05 -0800172type ShTest struct {
173 ShBinary
174
175 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700176
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700177 installDir android.InstallPath
178
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800179 data []android.DataPath
frankfengc5b87492020-06-03 10:28:47 -0700180 testConfig android.Path
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700181
182 dataModules map[string]android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800183}
184
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700185func (s *ShBinary) HostToolPath() android.OptionalPath {
186 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700187}
188
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700189func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800190}
191
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700192func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800193 return s.outputFilePath
194}
195
196func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700197 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800198}
199
Rob Seymour925aa092021-08-10 20:42:03 +0000200func (s *ShBinary) RelativeInstallPath() string {
201 return s.SubDir()
202}
Dan Willemsenb0552672019-01-25 16:04:11 -0800203func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700204 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800205}
206
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700207func (s *ShBinary) Symlinks() []string {
208 return s.properties.Symlinks
209}
210
Colin Crosscc83efb2020-08-21 14:25:33 -0700211var _ android.ImageInterface = (*ShBinary)(nil)
212
213func (s *ShBinary) ImageMutatorBegin(ctx android.BaseModuleContext) {}
214
215func (s *ShBinary) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000216 return !s.InstallInRecovery() && !s.InstallInRamdisk() && !s.InstallInVendorRamdisk() && !s.ModuleBase.InstallInVendor()
Colin Crosscc83efb2020-08-21 14:25:33 -0700217}
218
219func (s *ShBinary) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000220 return proptools.Bool(s.properties.Ramdisk_available) || s.InstallInRamdisk()
Colin Crosscc83efb2020-08-21 14:25:33 -0700221}
222
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700223func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000224 return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.InstallInVendorRamdisk()
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700225}
226
Inseob Kim08758f02021-04-08 21:13:22 +0900227func (s *ShBinary) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
228 return false
229}
230
Colin Crosscc83efb2020-08-21 14:25:33 -0700231func (s *ShBinary) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000232 return proptools.Bool(s.properties.Recovery_available) || s.InstallInRecovery()
Colin Crosscc83efb2020-08-21 14:25:33 -0700233}
234
235func (s *ShBinary) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Jihoon Kang71825162024-06-11 23:24:13 +0000236 extraVariations := []string{}
237 if s.InstallInProduct() {
238 extraVariations = append(extraVariations, cc.ProductVariation)
239 }
240 if s.InstallInVendor() {
241 extraVariations = append(extraVariations, cc.VendorVariation)
242 }
243 return extraVariations
Colin Crosscc83efb2020-08-21 14:25:33 -0700244}
245
246func (s *ShBinary) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
Jihoon Kang71825162024-06-11 23:24:13 +0000247 if m, ok := module.(*ShBinary); ok {
248 m.properties.ImageVariation = variation
249 }
250}
251
252// Overrides ModuleBase.InstallInRamdisk() so that the install rule respects
253// Ramdisk_available property for ramdisk variant
254func (s *ShBinary) InstallInRamdisk() bool {
255 return s.ModuleBase.InstallInRamdisk() ||
256 (proptools.Bool(s.properties.Ramdisk_available) && s.properties.ImageVariation == android.RamdiskVariation)
257}
258
259// Overrides ModuleBase.InstallInVendorRamdisk() so that the install rule respects
260// Vendor_ramdisk_available property for vendor ramdisk variant
261func (s *ShBinary) InstallInVendorRamdisk() bool {
262 return s.ModuleBase.InstallInVendorRamdisk() ||
263 (proptools.Bool(s.properties.Vendor_ramdisk_available) && s.properties.ImageVariation == android.VendorRamdiskVariation)
264}
265
266// Overrides ModuleBase.InstallInRecovery() so that the install rule respects
267// Recovery_available property for recovery variant
268func (s *ShBinary) InstallInRecovery() bool {
269 return s.ModuleBase.InstallInRecovery() ||
270 (proptools.Bool(s.properties.Recovery_available) && s.properties.ImageVariation == android.RecoveryVariation)
Colin Crosscc83efb2020-08-21 14:25:33 -0700271}
272
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700273func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500274 if s.properties.Src == nil {
275 ctx.PropertyErrorf("src", "missing prebuilt source file")
276 }
277
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700278 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
279 filename := proptools.String(s.properties.Filename)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800280 filenameFromSrc := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800281 if filename == "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800282 if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800283 filename = s.sourceFilePath.Base()
284 } else {
285 filename = ctx.ModuleName()
286 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800287 } else if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800288 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
289 return
290 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700291 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800292
293 // This ensures that outputFilePath has the correct name for others to
294 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700295 ctx.Build(pctx, android.BuildParams{
296 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800297 Output: s.outputFilePath,
298 Input: s.sourceFilePath,
299 })
Jihoon Kang71825162024-06-11 23:24:13 +0000300
301 s.properties.SubName = s.GetSubname(ctx)
302
Colin Cross40213022023-12-13 15:19:49 -0800303 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: []string{s.sourceFilePath.String()}})
mrziwang4f58b5f2024-06-10 12:58:40 -0700304
305 ctx.SetOutputFiles(android.Paths{s.outputFilePath}, "")
Dan Willemsenb0552672019-01-25 16:04:11 -0800306}
307
Jihoon Kang71825162024-06-11 23:24:13 +0000308func (s *ShBinary) GetSubname(ctx android.ModuleContext) string {
309 ret := ""
310 if s.properties.ImageVariation != "" {
311 if s.properties.ImageVariation != cc.VendorVariation {
312 ret = "." + s.properties.ImageVariation
313 }
314 }
315 return ret
316}
317
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700318func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700319 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700320 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000321 if !s.Installable() {
322 s.SkipInstall()
323 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700324 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
Colin Cross94bf5182021-11-09 17:21:14 -0800325 for _, symlink := range s.Symlinks() {
326 ctx.InstallSymlink(installDir, symlink, s.installedFile)
327 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700328}
329
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700330func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
Jihoon Kang71825162024-06-11 23:24:13 +0000331 return []android.AndroidMkEntries{{
Dan Willemsenb0552672019-01-25 16:04:11 -0800332 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700333 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500334 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700335 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700336 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700337 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700338 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000339 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !s.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700340 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800341 },
Jihoon Kang71825162024-06-11 23:24:13 +0000342 SubName: s.properties.SubName,
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900343 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800344}
345
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700346func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700347 entries.SetString("LOCAL_MODULE_SUFFIX", "")
348 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700349 if len(s.properties.Symlinks) > 0 {
350 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
351 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700352}
353
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700354type dependencyTag struct {
355 blueprint.BaseDependencyTag
356 name string
357}
358
359var (
360 shTestDataBinsTag = dependencyTag{name: "dataBins"}
361 shTestDataLibsTag = dependencyTag{name: "dataLibs"}
362 shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
363 shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"}
Makoto Onuki1725b202023-08-24 22:17:56 +0000364 shTestJavaDataTag = dependencyTag{name: "javaData"}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700365)
366
367var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}}
368
369func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) {
370 s.ShBinary.DepsMutator(ctx)
371
372 ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...)
373 ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...),
374 shTestDataLibsTag, s.testProperties.Data_libs...)
Liz Kammer3bf97bd2022-04-26 09:38:20 -0400375 if ctx.Target().Os.Class == android.Host && len(ctx.Config().Targets[android.Android]) > 0 {
Jaewoong Jung642916f2020-10-09 17:25:15 -0700376 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700377 ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...)
378 ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...),
379 shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...)
Makoto Onuki1725b202023-08-24 22:17:56 +0000380
381 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
382 ctx.AddVariationDependencies(javaDataVariation, shTestJavaDataTag, s.testProperties.Java_data...)
383
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700384 } else if ctx.Target().Os.Class != android.Host {
385 if len(s.testProperties.Data_device_bins) > 0 {
386 ctx.PropertyErrorf("data_device_bins", "only available for host modules")
387 }
388 if len(s.testProperties.Data_device_libs) > 0 {
389 ctx.PropertyErrorf("data_device_libs", "only available for host modules")
390 }
Makoto Onuki1725b202023-08-24 22:17:56 +0000391 if len(s.testProperties.Java_data) > 0 {
392 ctx.PropertyErrorf("Java_data", "only available for host modules")
393 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700394 }
395}
396
397func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) {
398 if _, exists := s.dataModules[relPath]; exists {
399 ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s",
400 relPath, s.dataModules[relPath].String(), path.String())
401 return
402 }
403 s.dataModules[relPath] = path
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800404 s.data = append(s.data, android.DataPath{SrcPath: path})
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700405}
406
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700407func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700408 s.ShBinary.generateAndroidBuildActions(ctx)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800409
410 expandedData := android.PathsForModuleSrc(ctx, s.testProperties.Data)
411 // Emulate the data property for java_data dependencies.
412 for _, javaData := range ctx.GetDirectDepsWithTag(shTestJavaDataTag) {
413 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
414 }
415 for _, d := range expandedData {
416 s.data = append(s.data, android.DataPath{SrcPath: d})
417 }
418
Colin Cross7c7c1142019-07-29 16:46:49 -0700419 testDir := "nativetest"
420 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
421 testDir = "nativetest64"
422 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700423 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700424 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
425 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
426 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
427 }
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700428 if s.SubDir() != "" {
429 // Don't add the module name to the installation path if sub_dir is specified for backward
430 // compatibility.
431 s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir())
432 } else {
433 s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name())
434 }
frankfengc5b87492020-06-03 10:28:47 -0700435
436 var configs []tradefed.Config
437 if Bool(s.testProperties.Require_root) {
438 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
439 } else {
440 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
441 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
442 }
frankfengbe6ae772020-09-28 13:22:57 -0700443 if len(s.testProperties.Data_device_bins) > 0 {
444 moduleName := s.Name()
445 remoteDir := "/data/local/tests/unrestricted/" + moduleName + "/"
446 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
447 for _, bin := range s.testProperties.Data_device_bins {
448 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: remoteDir + bin})
449 }
450 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options})
451 }
Cole Faust21680542022-12-07 18:18:37 -0800452 s.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
453 TestConfigProp: s.testProperties.Test_config,
454 TestConfigTemplateProp: s.testProperties.Test_config_template,
455 TestSuites: s.testProperties.Test_suites,
456 Config: configs,
457 AutoGenConfig: s.testProperties.Auto_gen_config,
458 OutputFileName: s.outputFilePath.Base(),
459 DeviceTemplate: "${ShellTestConfigTemplate}",
460 HostTemplate: "${ShellTestConfigTemplate}",
461 })
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700462
463 s.dataModules = make(map[string]android.Path)
464 ctx.VisitDirectDeps(func(dep android.Module) {
465 depTag := ctx.OtherModuleDependencyTag(dep)
466 switch depTag {
467 case shTestDataBinsTag, shTestDataDeviceBinsTag:
Anton Hansson2f6422c2020-12-31 13:42:10 +0000468 path := android.OutputFileForModule(ctx, dep, "")
469 s.addToDataModules(ctx, path.Base(), path)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700470 case shTestDataLibsTag, shTestDataDeviceLibsTag:
471 if cc, isCc := dep.(*cc.Module); isCc {
472 // Copy to an intermediate output directory to append "lib[64]" to the path,
473 // so that it's compatible with the default rpath values.
474 var relPath string
475 if cc.Arch().ArchType.Multilib == "lib64" {
476 relPath = filepath.Join("lib64", cc.OutputFile().Path().Base())
477 } else {
478 relPath = filepath.Join("lib", cc.OutputFile().Path().Base())
479 }
480 if _, exist := s.dataModules[relPath]; exist {
481 return
482 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800483 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700484 ctx.Build(pctx, android.BuildParams{
485 Rule: android.Cp,
486 Input: cc.OutputFile().Path(),
487 Output: relocatedLib,
488 })
489 s.addToDataModules(ctx, relPath, relocatedLib)
490 return
491 }
492 property := "data_libs"
493 if depTag == shTestDataDeviceBinsTag {
494 property = "data_device_libs"
495 }
496 ctx.PropertyErrorf(property, "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
497 }
498 })
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800499
500 installedData := ctx.InstallTestData(s.installDir, s.data)
501 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath, installedData...)
502
Colin Cross40213022023-12-13 15:19:49 -0800503 android.SetProvider(ctx, testing.TestModuleProviderKey, testing.TestModuleProviderData{})
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700504}
505
Colin Cross7c7c1142019-07-29 16:46:49 -0700506func (s *ShTest) InstallInData() bool {
507 return true
508}
509
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700510func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
511 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700512 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700513 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500514 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700515 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700516 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700517 s.customAndroidMkEntries(entries)
Colin Crossc68db4b2021-11-11 18:59:15 -0800518 entries.SetPath("LOCAL_MODULE_PATH", s.installDir)
Liz Kammer57f5b332020-11-24 12:42:58 -0800519 entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700520 if s.testConfig != nil {
521 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700522 }
yangbill22bafec2022-02-11 18:06:07 +0800523 if s.testProperties.Data_bins != nil {
524 entries.AddStrings("LOCAL_TEST_DATA_BINS", s.testProperties.Data_bins...)
525 }
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900526 entries.SetBoolIfTrue("LOCAL_COMPATIBILITY_PER_TESTCASE_DIRECTORY", Bool(s.testProperties.Per_testcase_directory))
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800527
528 s.testProperties.Test_options.SetAndroidMkEntries(entries)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700529 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700530 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900531 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800532}
533
Colin Cross8ff10582023-12-07 13:10:56 -0800534func initShBinaryModule(s *ShBinary) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800535 s.AddProperties(&s.properties)
536}
537
Patrice Arrudae1034192019-03-11 13:20:17 -0700538// sh_binary is for a shell script or batch file to be installed as an
539// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700540func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800541 module := &ShBinary{}
Colin Cross8ff10582023-12-07 13:10:56 -0800542 initShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700543 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800544 return module
545}
546
Patrice Arrudae1034192019-03-11 13:20:17 -0700547// sh_binary_host is for a shell script to be installed as an executable binary
548// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700549func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800550 module := &ShBinary{}
Colin Cross8ff10582023-12-07 13:10:56 -0800551 initShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700552 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800553 return module
554}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800555
Jaewoong Jung61a83682019-07-01 09:08:50 -0700556// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700557func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800558 module := &ShTest{}
Colin Cross8ff10582023-12-07 13:10:56 -0800559 initShBinaryModule(&module.ShBinary)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800560 module.AddProperties(&module.testProperties)
561
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700562 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800563 return module
564}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700565
566// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700567func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700568 module := &ShTest{}
Colin Cross8ff10582023-12-07 13:10:56 -0800569 initShBinaryModule(&module.ShBinary)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700570 module.AddProperties(&module.testProperties)
Julien Desprez06f13992021-06-28 13:41:43 -0700571 // Default sh_test_host to unit_tests = true
572 if module.testProperties.Test_options.Unit_test == nil {
573 module.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
574 }
Jaewoong Jung61a83682019-07-01 09:08:50 -0700575
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700576 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700577 return module
578}
frankfengc5b87492020-06-03 10:28:47 -0700579
580var Bool = proptools.Bool