blob: 9c0db73ba011ab83646f00b1b44991a422668e13 [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
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070021 "github.com/google/blueprint"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070022 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070025 "android/soong/cc"
frankfengc5b87492020-06-03 10:28:47 -070026 "android/soong/tradefed"
Dan Willemsenb0552672019-01-25 16:04:11 -080027)
28
29// sh_binary is for shell scripts (and batch files) that are installed as
30// executable files into .../bin/
31//
32// Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary
33// instead.
34
Jaewoong Jung4b79e982020-06-01 10:45:49 -070035var pctx = android.NewPackageContext("android/soong/sh")
36
Dan Willemsenb0552672019-01-25 16:04:11 -080037func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038 pctx.Import("android/soong/android")
39
Paul Duffin56fb8ee2021-03-08 15:05:52 +000040 registerShBuildComponents(android.InitRegistrationContext)
Dan Willemsenb0552672019-01-25 16:04:11 -080041}
42
Paul Duffin56fb8ee2021-03-08 15:05:52 +000043func registerShBuildComponents(ctx android.RegistrationContext) {
44 ctx.RegisterModuleType("sh_binary", ShBinaryFactory)
45 ctx.RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
46 ctx.RegisterModuleType("sh_test", ShTestFactory)
47 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
48}
49
50// Test fixture preparer that will register most sh build components.
51//
52// Singletons and mutators should only be added here if they are needed for a majority of sh
53// module types, otherwise they should be added under a separate preparer to allow them to be
54// selected only when needed to reduce test execution time.
55//
56// Module types do not have much of an overhead unless they are used so this should include as many
57// module types as possible. The exceptions are those module types that require mutators and/or
58// singletons in order to function in which case they should be kept together in a separate
59// preparer.
60var PrepareForTestWithShBuildComponents = android.GroupFixturePreparers(
61 android.FixtureRegisterWithContext(registerShBuildComponents),
62)
63
Dan Willemsenb0552672019-01-25 16:04:11 -080064type shBinaryProperties struct {
65 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080066 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080067
68 // optional subdirectory under which this file is installed into
69 Sub_dir *string `android:"arch_variant"`
70
71 // optional name for the installed file. If unspecified, name of the module is used as the file name
72 Filename *string `android:"arch_variant"`
73
74 // when set to true, and filename property is not set, the name for the installed file
75 // is the same as the file name of the source file.
76 Filename_from_src *bool `android:"arch_variant"`
77
78 // Whether this module is directly installable to one of the partitions. Default: true.
79 Installable *bool
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -070080
81 // install symlinks to the binary
82 Symlinks []string `android:"arch_variant"`
Colin Crosscc83efb2020-08-21 14:25:33 -070083
84 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070085 // On device without a dedicated recovery partition, the module is only
86 // available after switching root into
87 // /first_stage_ramdisk. To expose the module before switching root, install
88 // the recovery variant instead.
Colin Crosscc83efb2020-08-21 14:25:33 -070089 Ramdisk_available *bool
90
Yifan Hong60e0cfb2020-10-21 15:17:56 -070091 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070092 // On device without a dedicated recovery partition, the module is only
93 // available after switching root into
94 // /first_stage_ramdisk. To expose the module before switching root, install
95 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -070096 Vendor_ramdisk_available *bool
97
Colin Crosscc83efb2020-08-21 14:25:33 -070098 // Make this module available when building for recovery.
99 Recovery_available *bool
Jihoon Kang71825162024-06-11 23:24:13 +0000100
101 // The name of the image this module is built for
102 ImageVariation string `blueprint:"mutated"`
103
104 // Suffix for the name of Android.mk entries generated by this module
105 SubName string `blueprint:"mutated"`
Dan Willemsenb0552672019-01-25 16:04:11 -0800106}
107
Julien Desprez9e7fc142019-03-08 11:07:05 -0800108type TestProperties struct {
109 // list of compatibility suites (for example "cts", "vts") that the module should be
110 // installed into.
111 Test_suites []string `android:"arch_variant"`
112
113 // the name of the test configuration (for example "AndroidTest.xml") that should be
114 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -0700115 Test_config *string `android:"path,arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700116
117 // list of files or filegroup modules that provide data that should be installed alongside
118 // the test.
119 Data []string `android:"path,arch_variant"`
frankfengc5b87492020-06-03 10:28:47 -0700120
Cole Faust65cb40a2024-10-21 15:41:42 -0700121 // same as data, but adds dependencies using the device's os variation and the common
122 // architecture's variation. Can be used to add a module built for device to the data of a
123 // host test.
124 Device_common_data []string `android:"path_device_common"`
125
126 // same as data, but adds dependencies using the device's os variation and the device's first
127 // architecture's variation. Can be used to add a module built for device to the data of a
128 // host test.
129 Device_first_data []string `android:"path_device_first"`
130
frankfengc5b87492020-06-03 10:28:47 -0700131 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
132 // with root permission.
133 Require_root *bool
134
135 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
136 // should be installed with the module.
137 Test_config_template *string `android:"path,arch_variant"`
138
139 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
140 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
141 // explicitly.
142 Auto_gen_config *bool
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700143
144 // list of binary modules that should be installed alongside the test
145 Data_bins []string `android:"path,arch_variant"`
146
147 // list of library modules that should be installed alongside the test
148 Data_libs []string `android:"path,arch_variant"`
149
150 // list of device binary modules that should be installed alongside the test.
151 // Only available for host sh_test modules.
152 Data_device_bins []string `android:"path,arch_variant"`
153
154 // list of device library modules that should be installed alongside the test.
155 // Only available for host sh_test modules.
156 Data_device_libs []string `android:"path,arch_variant"`
Dan Shib40deac2021-05-24 12:04:54 -0700157
Makoto Onuki1725b202023-08-24 22:17:56 +0000158 // list of java modules that provide data that should be installed alongside the test.
159 Java_data []string
160
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900161 // Install the test into a folder named for the module in all test suites.
162 Per_testcase_directory *bool
163
Dan Shib40deac2021-05-24 12:04:54 -0700164 // Test options.
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800165 Test_options android.CommonTestOptions
Julien Desprez9e7fc142019-03-08 11:07:05 -0800166}
167
Dan Willemsenb0552672019-01-25 16:04:11 -0800168type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700169 android.ModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -0800170
171 properties shBinaryProperties
172
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700173 sourceFilePath android.Path
174 outputFilePath android.OutputPath
175 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800176}
177
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700178var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700179
Julien Desprez9e7fc142019-03-08 11:07:05 -0800180type ShTest struct {
181 ShBinary
182
183 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700184
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700185 installDir android.InstallPath
186
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800187 data []android.DataPath
frankfengc5b87492020-06-03 10:28:47 -0700188 testConfig android.Path
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700189
190 dataModules map[string]android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800191}
192
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700193func (s *ShBinary) HostToolPath() android.OptionalPath {
194 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700195}
196
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700197func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800198}
199
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700200func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800201 return s.outputFilePath
202}
203
204func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700205 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800206}
207
Rob Seymour925aa092021-08-10 20:42:03 +0000208func (s *ShBinary) RelativeInstallPath() string {
209 return s.SubDir()
210}
Dan Willemsenb0552672019-01-25 16:04:11 -0800211func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700212 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800213}
214
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700215func (s *ShBinary) Symlinks() []string {
216 return s.properties.Symlinks
217}
218
Colin Crosscc83efb2020-08-21 14:25:33 -0700219var _ android.ImageInterface = (*ShBinary)(nil)
220
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700221func (s *ShBinary) ImageMutatorBegin(ctx android.ImageInterfaceContext) {}
Colin Crosscc83efb2020-08-21 14:25:33 -0700222
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700223func (s *ShBinary) VendorVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang47e91842024-06-19 00:51:16 +0000224 return s.InstallInVendor()
225}
226
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700227func (s *ShBinary) ProductVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang47e91842024-06-19 00:51:16 +0000228 return s.InstallInProduct()
229}
230
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700231func (s *ShBinary) CoreVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000232 return !s.InstallInRecovery() && !s.InstallInRamdisk() && !s.InstallInVendorRamdisk() && !s.ModuleBase.InstallInVendor()
Colin Crosscc83efb2020-08-21 14:25:33 -0700233}
234
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700235func (s *ShBinary) RamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000236 return proptools.Bool(s.properties.Ramdisk_available) || s.InstallInRamdisk()
Colin Crosscc83efb2020-08-21 14:25:33 -0700237}
238
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700239func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000240 return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.InstallInVendorRamdisk()
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700241}
242
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700243func (s *ShBinary) DebugRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Inseob Kim08758f02021-04-08 21:13:22 +0900244 return false
245}
246
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700247func (s *ShBinary) RecoveryVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000248 return proptools.Bool(s.properties.Recovery_available) || s.InstallInRecovery()
Colin Crosscc83efb2020-08-21 14:25:33 -0700249}
250
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700251func (s *ShBinary) ExtraImageVariations(ctx android.ImageInterfaceContext) []string {
Jihoon Kang47e91842024-06-19 00:51:16 +0000252 return nil
Colin Crosscc83efb2020-08-21 14:25:33 -0700253}
254
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700255func (s *ShBinary) SetImageVariation(ctx android.ImageInterfaceContext, variation string) {
Jihoon Kang7583e832024-06-13 21:25:45 +0000256 s.properties.ImageVariation = variation
Jihoon Kang71825162024-06-11 23:24:13 +0000257}
258
259// Overrides ModuleBase.InstallInRamdisk() so that the install rule respects
260// Ramdisk_available property for ramdisk variant
261func (s *ShBinary) InstallInRamdisk() bool {
262 return s.ModuleBase.InstallInRamdisk() ||
263 (proptools.Bool(s.properties.Ramdisk_available) && s.properties.ImageVariation == android.RamdiskVariation)
264}
265
266// Overrides ModuleBase.InstallInVendorRamdisk() so that the install rule respects
267// Vendor_ramdisk_available property for vendor ramdisk variant
268func (s *ShBinary) InstallInVendorRamdisk() bool {
269 return s.ModuleBase.InstallInVendorRamdisk() ||
270 (proptools.Bool(s.properties.Vendor_ramdisk_available) && s.properties.ImageVariation == android.VendorRamdiskVariation)
271}
272
273// Overrides ModuleBase.InstallInRecovery() so that the install rule respects
274// Recovery_available property for recovery variant
275func (s *ShBinary) InstallInRecovery() bool {
276 return s.ModuleBase.InstallInRecovery() ||
277 (proptools.Bool(s.properties.Recovery_available) && s.properties.ImageVariation == android.RecoveryVariation)
Colin Crosscc83efb2020-08-21 14:25:33 -0700278}
279
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700280func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500281 if s.properties.Src == nil {
282 ctx.PropertyErrorf("src", "missing prebuilt source file")
283 }
284
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700285 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
286 filename := proptools.String(s.properties.Filename)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800287 filenameFromSrc := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800288 if filename == "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800289 if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800290 filename = s.sourceFilePath.Base()
291 } else {
292 filename = ctx.ModuleName()
293 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800294 } else if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800295 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
296 return
297 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700298 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800299
300 // This ensures that outputFilePath has the correct name for others to
301 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700302 ctx.Build(pctx, android.BuildParams{
303 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800304 Output: s.outputFilePath,
305 Input: s.sourceFilePath,
306 })
Jihoon Kang71825162024-06-11 23:24:13 +0000307
308 s.properties.SubName = s.GetSubname(ctx)
309
Colin Cross40213022023-12-13 15:19:49 -0800310 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: []string{s.sourceFilePath.String()}})
mrziwang4f58b5f2024-06-10 12:58:40 -0700311
312 ctx.SetOutputFiles(android.Paths{s.outputFilePath}, "")
Dan Willemsenb0552672019-01-25 16:04:11 -0800313}
314
Jihoon Kang71825162024-06-11 23:24:13 +0000315func (s *ShBinary) GetSubname(ctx android.ModuleContext) string {
316 ret := ""
317 if s.properties.ImageVariation != "" {
Jihoon Kang47e91842024-06-19 00:51:16 +0000318 if s.properties.ImageVariation != android.VendorVariation {
Jihoon Kang71825162024-06-11 23:24:13 +0000319 ret = "." + s.properties.ImageVariation
320 }
321 }
322 return ret
323}
324
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700325func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700326 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700327 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000328 if !s.Installable() {
329 s.SkipInstall()
330 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700331 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
Colin Cross94bf5182021-11-09 17:21:14 -0800332 for _, symlink := range s.Symlinks() {
333 ctx.InstallSymlink(installDir, symlink, s.installedFile)
334 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700335}
336
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700337func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
Jihoon Kang71825162024-06-11 23:24:13 +0000338 return []android.AndroidMkEntries{{
Dan Willemsenb0552672019-01-25 16:04:11 -0800339 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700340 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500341 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700342 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700343 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700344 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700345 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000346 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !s.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700347 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800348 },
Jihoon Kang71825162024-06-11 23:24:13 +0000349 SubName: s.properties.SubName,
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900350 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800351}
352
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700353func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700354 entries.SetString("LOCAL_MODULE_SUFFIX", "")
355 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700356 if len(s.properties.Symlinks) > 0 {
357 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
358 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700359}
360
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700361type dependencyTag struct {
362 blueprint.BaseDependencyTag
363 name string
364}
365
366var (
367 shTestDataBinsTag = dependencyTag{name: "dataBins"}
368 shTestDataLibsTag = dependencyTag{name: "dataLibs"}
369 shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
370 shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"}
Makoto Onuki1725b202023-08-24 22:17:56 +0000371 shTestJavaDataTag = dependencyTag{name: "javaData"}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700372)
373
374var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}}
375
376func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) {
377 s.ShBinary.DepsMutator(ctx)
378
379 ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...)
380 ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...),
381 shTestDataLibsTag, s.testProperties.Data_libs...)
Liz Kammer3bf97bd2022-04-26 09:38:20 -0400382 if ctx.Target().Os.Class == android.Host && len(ctx.Config().Targets[android.Android]) > 0 {
Jaewoong Jung642916f2020-10-09 17:25:15 -0700383 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700384 ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...)
385 ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...),
386 shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...)
Makoto Onuki1725b202023-08-24 22:17:56 +0000387
388 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
389 ctx.AddVariationDependencies(javaDataVariation, shTestJavaDataTag, s.testProperties.Java_data...)
390
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700391 } else if ctx.Target().Os.Class != android.Host {
392 if len(s.testProperties.Data_device_bins) > 0 {
393 ctx.PropertyErrorf("data_device_bins", "only available for host modules")
394 }
395 if len(s.testProperties.Data_device_libs) > 0 {
396 ctx.PropertyErrorf("data_device_libs", "only available for host modules")
397 }
Makoto Onuki1725b202023-08-24 22:17:56 +0000398 if len(s.testProperties.Java_data) > 0 {
399 ctx.PropertyErrorf("Java_data", "only available for host modules")
400 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700401 }
402}
403
404func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) {
405 if _, exists := s.dataModules[relPath]; exists {
406 ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s",
407 relPath, s.dataModules[relPath].String(), path.String())
408 return
409 }
410 s.dataModules[relPath] = path
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800411 s.data = append(s.data, android.DataPath{SrcPath: path})
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700412}
413
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700414func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700415 s.ShBinary.generateAndroidBuildActions(ctx)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800416
417 expandedData := android.PathsForModuleSrc(ctx, s.testProperties.Data)
Cole Faust65cb40a2024-10-21 15:41:42 -0700418 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, s.testProperties.Device_common_data)...)
419 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, s.testProperties.Device_first_data)...)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800420 // Emulate the data property for java_data dependencies.
421 for _, javaData := range ctx.GetDirectDepsWithTag(shTestJavaDataTag) {
422 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
423 }
424 for _, d := range expandedData {
425 s.data = append(s.data, android.DataPath{SrcPath: d})
426 }
427
Colin Cross7c7c1142019-07-29 16:46:49 -0700428 testDir := "nativetest"
429 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
430 testDir = "nativetest64"
431 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700432 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700433 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
434 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
435 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
436 }
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700437 if s.SubDir() != "" {
438 // Don't add the module name to the installation path if sub_dir is specified for backward
439 // compatibility.
440 s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir())
441 } else {
442 s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name())
443 }
frankfengc5b87492020-06-03 10:28:47 -0700444
445 var configs []tradefed.Config
446 if Bool(s.testProperties.Require_root) {
447 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
448 } else {
449 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
450 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
451 }
frankfengbe6ae772020-09-28 13:22:57 -0700452 if len(s.testProperties.Data_device_bins) > 0 {
453 moduleName := s.Name()
454 remoteDir := "/data/local/tests/unrestricted/" + moduleName + "/"
455 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
456 for _, bin := range s.testProperties.Data_device_bins {
457 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: remoteDir + bin})
458 }
459 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options})
460 }
Cole Faust21680542022-12-07 18:18:37 -0800461 s.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
462 TestConfigProp: s.testProperties.Test_config,
463 TestConfigTemplateProp: s.testProperties.Test_config_template,
464 TestSuites: s.testProperties.Test_suites,
465 Config: configs,
466 AutoGenConfig: s.testProperties.Auto_gen_config,
467 OutputFileName: s.outputFilePath.Base(),
468 DeviceTemplate: "${ShellTestConfigTemplate}",
469 HostTemplate: "${ShellTestConfigTemplate}",
470 })
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700471
472 s.dataModules = make(map[string]android.Path)
473 ctx.VisitDirectDeps(func(dep android.Module) {
474 depTag := ctx.OtherModuleDependencyTag(dep)
475 switch depTag {
476 case shTestDataBinsTag, shTestDataDeviceBinsTag:
Anton Hansson2f6422c2020-12-31 13:42:10 +0000477 path := android.OutputFileForModule(ctx, dep, "")
478 s.addToDataModules(ctx, path.Base(), path)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700479 case shTestDataLibsTag, shTestDataDeviceLibsTag:
480 if cc, isCc := dep.(*cc.Module); isCc {
481 // Copy to an intermediate output directory to append "lib[64]" to the path,
482 // so that it's compatible with the default rpath values.
483 var relPath string
484 if cc.Arch().ArchType.Multilib == "lib64" {
485 relPath = filepath.Join("lib64", cc.OutputFile().Path().Base())
486 } else {
487 relPath = filepath.Join("lib", cc.OutputFile().Path().Base())
488 }
489 if _, exist := s.dataModules[relPath]; exist {
490 return
491 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800492 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700493 ctx.Build(pctx, android.BuildParams{
494 Rule: android.Cp,
495 Input: cc.OutputFile().Path(),
496 Output: relocatedLib,
497 })
498 s.addToDataModules(ctx, relPath, relocatedLib)
499 return
500 }
501 property := "data_libs"
502 if depTag == shTestDataDeviceBinsTag {
503 property = "data_device_libs"
504 }
505 ctx.PropertyErrorf(property, "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
506 }
507 })
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800508
509 installedData := ctx.InstallTestData(s.installDir, s.data)
510 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath, installedData...)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700511}
512
Colin Cross7c7c1142019-07-29 16:46:49 -0700513func (s *ShTest) InstallInData() bool {
514 return true
515}
516
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700517func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
518 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700519 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700520 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500521 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700522 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700523 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700524 s.customAndroidMkEntries(entries)
Colin Crossc68db4b2021-11-11 18:59:15 -0800525 entries.SetPath("LOCAL_MODULE_PATH", s.installDir)
Liz Kammer57f5b332020-11-24 12:42:58 -0800526 entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700527 if s.testConfig != nil {
528 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700529 }
yangbill22bafec2022-02-11 18:06:07 +0800530 if s.testProperties.Data_bins != nil {
531 entries.AddStrings("LOCAL_TEST_DATA_BINS", s.testProperties.Data_bins...)
532 }
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900533 entries.SetBoolIfTrue("LOCAL_COMPATIBILITY_PER_TESTCASE_DIRECTORY", Bool(s.testProperties.Per_testcase_directory))
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800534
535 s.testProperties.Test_options.SetAndroidMkEntries(entries)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700536 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700537 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900538 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800539}
540
Colin Cross8ff10582023-12-07 13:10:56 -0800541func initShBinaryModule(s *ShBinary) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800542 s.AddProperties(&s.properties)
543}
544
Patrice Arrudae1034192019-03-11 13:20:17 -0700545// sh_binary is for a shell script or batch file to be installed as an
546// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700547func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800548 module := &ShBinary{}
Colin Cross8ff10582023-12-07 13:10:56 -0800549 initShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700550 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800551 return module
552}
553
Patrice Arrudae1034192019-03-11 13:20:17 -0700554// sh_binary_host is for a shell script to be installed as an executable binary
555// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700556func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800557 module := &ShBinary{}
Colin Cross8ff10582023-12-07 13:10:56 -0800558 initShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700559 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800560 return module
561}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800562
Jaewoong Jung61a83682019-07-01 09:08:50 -0700563// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700564func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800565 module := &ShTest{}
Colin Cross8ff10582023-12-07 13:10:56 -0800566 initShBinaryModule(&module.ShBinary)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800567 module.AddProperties(&module.testProperties)
568
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700569 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800570 return module
571}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700572
573// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700574func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700575 module := &ShTest{}
Colin Cross8ff10582023-12-07 13:10:56 -0800576 initShBinaryModule(&module.ShBinary)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700577 module.AddProperties(&module.testProperties)
Julien Desprez06f13992021-06-28 13:41:43 -0700578 // Default sh_test_host to unit_tests = true
579 if module.testProperties.Test_options.Unit_test == nil {
580 module.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
581 }
Jaewoong Jung61a83682019-07-01 09:08:50 -0700582
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700583 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700584 return module
585}
frankfengc5b87492020-06-03 10:28:47 -0700586
587var Bool = proptools.Bool