blob: 7f5a4261f4d5356a38483171cacd819a58a2ecfb [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 (
Ronald Braunsteinf424c9a2024-10-22 01:41:20 +000018 "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
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070022 "github.com/google/blueprint"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070023 "github.com/google/blueprint/proptools"
24
25 "android/soong/android"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070026 "android/soong/cc"
frankfengc5b87492020-06-03 10:28:47 -070027 "android/soong/tradefed"
Dan Willemsenb0552672019-01-25 16:04:11 -080028)
29
30// sh_binary is for shell scripts (and batch files) that are installed as
31// executable files into .../bin/
32//
33// Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary
34// instead.
35
Jaewoong Jung4b79e982020-06-01 10:45:49 -070036var pctx = android.NewPackageContext("android/soong/sh")
37
Dan Willemsenb0552672019-01-25 16:04:11 -080038func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070039 pctx.Import("android/soong/android")
40
Paul Duffin56fb8ee2021-03-08 15:05:52 +000041 registerShBuildComponents(android.InitRegistrationContext)
Dan Willemsenb0552672019-01-25 16:04:11 -080042}
43
Paul Duffin56fb8ee2021-03-08 15:05:52 +000044func registerShBuildComponents(ctx android.RegistrationContext) {
45 ctx.RegisterModuleType("sh_binary", ShBinaryFactory)
46 ctx.RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
47 ctx.RegisterModuleType("sh_test", ShTestFactory)
48 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
Ronald Braunstein98204082024-10-29 11:09:00 -070049 ctx.RegisterModuleType("sh_defaults", ShDefaultsFactory)
Paul Duffin56fb8ee2021-03-08 15:05:52 +000050}
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
Cole Faust65cb40a2024-10-21 15:41:42 -0700123 // same as data, but adds dependencies using the device's os variation and the common
124 // architecture's variation. Can be used to add a module built for device to the data of a
125 // host test.
126 Device_common_data []string `android:"path_device_common"`
127
128 // same as data, but adds dependencies using the device's os variation and the device's first
129 // architecture's variation. Can be used to add a module built for device to the data of a
130 // host test.
131 Device_first_data []string `android:"path_device_first"`
132
frankfengc5b87492020-06-03 10:28:47 -0700133 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
134 // with root permission.
135 Require_root *bool
136
137 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
138 // should be installed with the module.
139 Test_config_template *string `android:"path,arch_variant"`
140
141 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
142 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
143 // explicitly.
144 Auto_gen_config *bool
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700145
146 // list of binary modules that should be installed alongside the test
147 Data_bins []string `android:"path,arch_variant"`
148
149 // list of library modules that should be installed alongside the test
150 Data_libs []string `android:"path,arch_variant"`
151
152 // list of device binary modules that should be installed alongside the test.
153 // Only available for host sh_test modules.
154 Data_device_bins []string `android:"path,arch_variant"`
155
156 // list of device library modules that should be installed alongside the test.
157 // Only available for host sh_test modules.
158 Data_device_libs []string `android:"path,arch_variant"`
Dan Shib40deac2021-05-24 12:04:54 -0700159
Makoto Onuki1725b202023-08-24 22:17:56 +0000160 // list of java modules that provide data that should be installed alongside the test.
161 Java_data []string
162
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900163 // Install the test into a folder named for the module in all test suites.
164 Per_testcase_directory *bool
165
Dan Shib40deac2021-05-24 12:04:54 -0700166 // Test options.
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800167 Test_options android.CommonTestOptions
Ronald Braunsteinf424c9a2024-10-22 01:41:20 +0000168
169 // a list of extra test configuration files that should be installed with the module.
170 Extra_test_configs []string `android:"path,arch_variant"`
Julien Desprez9e7fc142019-03-08 11:07:05 -0800171}
172
Dan Willemsenb0552672019-01-25 16:04:11 -0800173type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700174 android.ModuleBase
Ronald Braunstein98204082024-10-29 11:09:00 -0700175 android.DefaultableModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -0800176
177 properties shBinaryProperties
178
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700179 sourceFilePath android.Path
180 outputFilePath android.OutputPath
181 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800182}
183
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700184var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700185
Julien Desprez9e7fc142019-03-08 11:07:05 -0800186type ShTest struct {
187 ShBinary
188
189 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700190
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700191 installDir android.InstallPath
192
Ronald Braunsteinf424c9a2024-10-22 01:41:20 +0000193 data []android.DataPath
194 testConfig android.Path
195 extraTestConfigs android.Paths
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700196
197 dataModules map[string]android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800198}
199
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700200func (s *ShBinary) HostToolPath() android.OptionalPath {
201 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700202}
203
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700204func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800205}
206
Cole Faust4e9f5922024-11-13 16:09:23 -0800207func (s *ShBinary) OutputFile() android.Path {
Dan Willemsenb0552672019-01-25 16:04:11 -0800208 return s.outputFilePath
209}
210
211func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700212 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800213}
214
Rob Seymour925aa092021-08-10 20:42:03 +0000215func (s *ShBinary) RelativeInstallPath() string {
216 return s.SubDir()
217}
Dan Willemsenb0552672019-01-25 16:04:11 -0800218func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700219 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800220}
221
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700222func (s *ShBinary) Symlinks() []string {
223 return s.properties.Symlinks
224}
225
Colin Crosscc83efb2020-08-21 14:25:33 -0700226var _ android.ImageInterface = (*ShBinary)(nil)
227
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700228func (s *ShBinary) ImageMutatorBegin(ctx android.ImageInterfaceContext) {}
Colin Crosscc83efb2020-08-21 14:25:33 -0700229
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700230func (s *ShBinary) VendorVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang47e91842024-06-19 00:51:16 +0000231 return s.InstallInVendor()
232}
233
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700234func (s *ShBinary) ProductVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang47e91842024-06-19 00:51:16 +0000235 return s.InstallInProduct()
236}
237
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700238func (s *ShBinary) CoreVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000239 return !s.InstallInRecovery() && !s.InstallInRamdisk() && !s.InstallInVendorRamdisk() && !s.ModuleBase.InstallInVendor()
Colin Crosscc83efb2020-08-21 14:25:33 -0700240}
241
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700242func (s *ShBinary) RamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000243 return proptools.Bool(s.properties.Ramdisk_available) || s.InstallInRamdisk()
Colin Crosscc83efb2020-08-21 14:25:33 -0700244}
245
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700246func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000247 return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.InstallInVendorRamdisk()
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700248}
249
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700250func (s *ShBinary) DebugRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool {
Inseob Kim08758f02021-04-08 21:13:22 +0900251 return false
252}
253
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700254func (s *ShBinary) RecoveryVariantNeeded(ctx android.ImageInterfaceContext) bool {
Jihoon Kang71825162024-06-11 23:24:13 +0000255 return proptools.Bool(s.properties.Recovery_available) || s.InstallInRecovery()
Colin Crosscc83efb2020-08-21 14:25:33 -0700256}
257
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700258func (s *ShBinary) ExtraImageVariations(ctx android.ImageInterfaceContext) []string {
Jihoon Kang47e91842024-06-19 00:51:16 +0000259 return nil
Colin Crosscc83efb2020-08-21 14:25:33 -0700260}
261
Cole Faustfa6e0fd2024-10-15 15:22:57 -0700262func (s *ShBinary) SetImageVariation(ctx android.ImageInterfaceContext, variation string) {
Jihoon Kang7583e832024-06-13 21:25:45 +0000263 s.properties.ImageVariation = variation
Jihoon Kang71825162024-06-11 23:24:13 +0000264}
265
266// Overrides ModuleBase.InstallInRamdisk() so that the install rule respects
267// Ramdisk_available property for ramdisk variant
268func (s *ShBinary) InstallInRamdisk() bool {
269 return s.ModuleBase.InstallInRamdisk() ||
270 (proptools.Bool(s.properties.Ramdisk_available) && s.properties.ImageVariation == android.RamdiskVariation)
271}
272
273// Overrides ModuleBase.InstallInVendorRamdisk() so that the install rule respects
274// Vendor_ramdisk_available property for vendor ramdisk variant
275func (s *ShBinary) InstallInVendorRamdisk() bool {
276 return s.ModuleBase.InstallInVendorRamdisk() ||
277 (proptools.Bool(s.properties.Vendor_ramdisk_available) && s.properties.ImageVariation == android.VendorRamdiskVariation)
278}
279
280// Overrides ModuleBase.InstallInRecovery() so that the install rule respects
281// Recovery_available property for recovery variant
282func (s *ShBinary) InstallInRecovery() bool {
283 return s.ModuleBase.InstallInRecovery() ||
284 (proptools.Bool(s.properties.Recovery_available) && s.properties.ImageVariation == android.RecoveryVariation)
Colin Crosscc83efb2020-08-21 14:25:33 -0700285}
286
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700287func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500288 if s.properties.Src == nil {
289 ctx.PropertyErrorf("src", "missing prebuilt source file")
290 }
291
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700292 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
293 filename := proptools.String(s.properties.Filename)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800294 filenameFromSrc := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800295 if filename == "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800296 if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800297 filename = s.sourceFilePath.Base()
298 } else {
299 filename = ctx.ModuleName()
300 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800301 } else if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800302 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
303 return
304 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700305 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800306
307 // This ensures that outputFilePath has the correct name for others to
308 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700309 ctx.Build(pctx, android.BuildParams{
310 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800311 Output: s.outputFilePath,
312 Input: s.sourceFilePath,
313 })
Jihoon Kang71825162024-06-11 23:24:13 +0000314
315 s.properties.SubName = s.GetSubname(ctx)
316
Colin Cross40213022023-12-13 15:19:49 -0800317 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: []string{s.sourceFilePath.String()}})
mrziwang4f58b5f2024-06-10 12:58:40 -0700318
319 ctx.SetOutputFiles(android.Paths{s.outputFilePath}, "")
Dan Willemsenb0552672019-01-25 16:04:11 -0800320}
321
Jihoon Kang71825162024-06-11 23:24:13 +0000322func (s *ShBinary) GetSubname(ctx android.ModuleContext) string {
323 ret := ""
324 if s.properties.ImageVariation != "" {
Jihoon Kang47e91842024-06-19 00:51:16 +0000325 if s.properties.ImageVariation != android.VendorVariation {
Jihoon Kang71825162024-06-11 23:24:13 +0000326 ret = "." + s.properties.ImageVariation
327 }
328 }
329 return ret
330}
331
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700332func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700333 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700334 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000335 if !s.Installable() {
336 s.SkipInstall()
337 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700338 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
Colin Cross94bf5182021-11-09 17:21:14 -0800339 for _, symlink := range s.Symlinks() {
340 ctx.InstallSymlink(installDir, symlink, s.installedFile)
341 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700342}
343
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700344func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
Jihoon Kang71825162024-06-11 23:24:13 +0000345 return []android.AndroidMkEntries{{
Dan Willemsenb0552672019-01-25 16:04:11 -0800346 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700347 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500348 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700349 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700350 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700351 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700352 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000353 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !s.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700354 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800355 },
Jihoon Kang71825162024-06-11 23:24:13 +0000356 SubName: s.properties.SubName,
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900357 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800358}
359
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700360func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700361 entries.SetString("LOCAL_MODULE_SUFFIX", "")
362 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700363 if len(s.properties.Symlinks) > 0 {
364 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
365 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700366}
367
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700368type dependencyTag struct {
369 blueprint.BaseDependencyTag
370 name string
371}
372
373var (
374 shTestDataBinsTag = dependencyTag{name: "dataBins"}
375 shTestDataLibsTag = dependencyTag{name: "dataLibs"}
376 shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
377 shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"}
Makoto Onuki1725b202023-08-24 22:17:56 +0000378 shTestJavaDataTag = dependencyTag{name: "javaData"}
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700379)
380
381var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}}
382
383func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) {
384 s.ShBinary.DepsMutator(ctx)
385
386 ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...)
387 ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...),
388 shTestDataLibsTag, s.testProperties.Data_libs...)
Liz Kammer3bf97bd2022-04-26 09:38:20 -0400389 if ctx.Target().Os.Class == android.Host && len(ctx.Config().Targets[android.Android]) > 0 {
Jaewoong Jung642916f2020-10-09 17:25:15 -0700390 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700391 ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...)
392 ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...),
393 shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...)
Makoto Onuki1725b202023-08-24 22:17:56 +0000394
395 javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}}
396 ctx.AddVariationDependencies(javaDataVariation, shTestJavaDataTag, s.testProperties.Java_data...)
397
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700398 } else if ctx.Target().Os.Class != android.Host {
399 if len(s.testProperties.Data_device_bins) > 0 {
400 ctx.PropertyErrorf("data_device_bins", "only available for host modules")
401 }
402 if len(s.testProperties.Data_device_libs) > 0 {
403 ctx.PropertyErrorf("data_device_libs", "only available for host modules")
404 }
Makoto Onuki1725b202023-08-24 22:17:56 +0000405 if len(s.testProperties.Java_data) > 0 {
406 ctx.PropertyErrorf("Java_data", "only available for host modules")
407 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700408 }
409}
410
411func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) {
412 if _, exists := s.dataModules[relPath]; exists {
413 ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s",
414 relPath, s.dataModules[relPath].String(), path.String())
415 return
416 }
417 s.dataModules[relPath] = path
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800418 s.data = append(s.data, android.DataPath{SrcPath: path})
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700419}
420
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700421func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700422 s.ShBinary.generateAndroidBuildActions(ctx)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800423
424 expandedData := android.PathsForModuleSrc(ctx, s.testProperties.Data)
Cole Faust65cb40a2024-10-21 15:41:42 -0700425 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, s.testProperties.Device_common_data)...)
426 expandedData = append(expandedData, android.PathsForModuleSrc(ctx, s.testProperties.Device_first_data)...)
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800427 // Emulate the data property for java_data dependencies.
428 for _, javaData := range ctx.GetDirectDepsWithTag(shTestJavaDataTag) {
429 expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...)
430 }
431 for _, d := range expandedData {
432 s.data = append(s.data, android.DataPath{SrcPath: d})
433 }
434
Colin Cross7c7c1142019-07-29 16:46:49 -0700435 testDir := "nativetest"
436 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
437 testDir = "nativetest64"
438 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700439 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700440 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
441 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
442 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
443 }
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700444 if s.SubDir() != "" {
445 // Don't add the module name to the installation path if sub_dir is specified for backward
446 // compatibility.
447 s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir())
448 } else {
449 s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name())
450 }
frankfengc5b87492020-06-03 10:28:47 -0700451
452 var configs []tradefed.Config
453 if Bool(s.testProperties.Require_root) {
454 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
455 } else {
456 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
457 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
458 }
frankfengbe6ae772020-09-28 13:22:57 -0700459 if len(s.testProperties.Data_device_bins) > 0 {
460 moduleName := s.Name()
461 remoteDir := "/data/local/tests/unrestricted/" + moduleName + "/"
462 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
463 for _, bin := range s.testProperties.Data_device_bins {
464 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: remoteDir + bin})
465 }
466 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options})
467 }
Cole Faust21680542022-12-07 18:18:37 -0800468 s.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
469 TestConfigProp: s.testProperties.Test_config,
470 TestConfigTemplateProp: s.testProperties.Test_config_template,
471 TestSuites: s.testProperties.Test_suites,
472 Config: configs,
473 AutoGenConfig: s.testProperties.Auto_gen_config,
474 OutputFileName: s.outputFilePath.Base(),
475 DeviceTemplate: "${ShellTestConfigTemplate}",
476 HostTemplate: "${ShellTestConfigTemplate}",
477 })
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700478
Ronald Braunsteinf424c9a2024-10-22 01:41:20 +0000479 s.extraTestConfigs = android.PathsForModuleSrc(ctx, s.testProperties.Extra_test_configs)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700480 s.dataModules = make(map[string]android.Path)
481 ctx.VisitDirectDeps(func(dep android.Module) {
482 depTag := ctx.OtherModuleDependencyTag(dep)
483 switch depTag {
484 case shTestDataBinsTag, shTestDataDeviceBinsTag:
Anton Hansson2f6422c2020-12-31 13:42:10 +0000485 path := android.OutputFileForModule(ctx, dep, "")
486 s.addToDataModules(ctx, path.Base(), path)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700487 case shTestDataLibsTag, shTestDataDeviceLibsTag:
488 if cc, isCc := dep.(*cc.Module); isCc {
489 // Copy to an intermediate output directory to append "lib[64]" to the path,
490 // so that it's compatible with the default rpath values.
491 var relPath string
492 if cc.Arch().ArchType.Multilib == "lib64" {
493 relPath = filepath.Join("lib64", cc.OutputFile().Path().Base())
494 } else {
495 relPath = filepath.Join("lib", cc.OutputFile().Path().Base())
496 }
497 if _, exist := s.dataModules[relPath]; exist {
498 return
499 }
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800500 relocatedLib := android.PathForModuleOut(ctx, "relocated").Join(ctx, relPath)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700501 ctx.Build(pctx, android.BuildParams{
502 Rule: android.Cp,
503 Input: cc.OutputFile().Path(),
504 Output: relocatedLib,
505 })
506 s.addToDataModules(ctx, relPath, relocatedLib)
507 return
508 }
509 property := "data_libs"
510 if depTag == shTestDataDeviceBinsTag {
511 property = "data_device_libs"
512 }
513 ctx.PropertyErrorf(property, "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
514 }
515 })
Colin Cross5c1d5fb2023-11-15 12:39:40 -0800516
517 installedData := ctx.InstallTestData(s.installDir, s.data)
518 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath, installedData...)
Ronald Braunsteinf424c9a2024-10-22 01:41:20 +0000519
520 mkEntries := s.AndroidMkEntries()[0]
521 android.SetProvider(ctx, tradefed.BaseTestProviderKey, tradefed.BaseTestProviderData{
522 TestcaseRelDataFiles: addArch(ctx.Arch().ArchType.String(), installedData.Paths()),
523 OutputFile: s.outputFilePath,
524 TestConfig: s.testConfig,
525 TestSuites: s.testProperties.Test_suites,
526 IsHost: false,
527 IsUnitTest: Bool(s.testProperties.Test_options.Unit_test),
528 MkInclude: mkEntries.Include,
529 MkAppClass: mkEntries.Class,
530 InstallDir: s.installDir,
531 })
532}
533
534func addArch(archType string, paths android.Paths) []string {
535 archRelPaths := []string{}
536 for _, p := range paths {
537 archRelPaths = append(archRelPaths, fmt.Sprintf("%s/%s", archType, p.Rel()))
538 }
539 return archRelPaths
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700540}
541
Colin Cross7c7c1142019-07-29 16:46:49 -0700542func (s *ShTest) InstallInData() bool {
543 return true
544}
545
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700546func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
547 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700548 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700549 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500550 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700551 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700552 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700553 s.customAndroidMkEntries(entries)
Colin Crossc68db4b2021-11-11 18:59:15 -0800554 entries.SetPath("LOCAL_MODULE_PATH", s.installDir)
Liz Kammer57f5b332020-11-24 12:42:58 -0800555 entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700556 if s.testConfig != nil {
557 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700558 }
yangbill22bafec2022-02-11 18:06:07 +0800559 if s.testProperties.Data_bins != nil {
560 entries.AddStrings("LOCAL_TEST_DATA_BINS", s.testProperties.Data_bins...)
561 }
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900562 entries.SetBoolIfTrue("LOCAL_COMPATIBILITY_PER_TESTCASE_DIRECTORY", Bool(s.testProperties.Per_testcase_directory))
Ronald Braunsteinf424c9a2024-10-22 01:41:20 +0000563 if len(s.extraTestConfigs) > 0 {
564 entries.AddStrings("LOCAL_EXTRA_FULL_TEST_CONFIGS", s.extraTestConfigs.Strings()...)
565 }
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800566
567 s.testProperties.Test_options.SetAndroidMkEntries(entries)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700568 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700569 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900570 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800571}
572
Colin Cross8ff10582023-12-07 13:10:56 -0800573func initShBinaryModule(s *ShBinary) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800574 s.AddProperties(&s.properties)
575}
576
Patrice Arrudae1034192019-03-11 13:20:17 -0700577// sh_binary is for a shell script or batch file to be installed as an
578// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700579func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800580 module := &ShBinary{}
Colin Cross8ff10582023-12-07 13:10:56 -0800581 initShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700582 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Ronald Braunstein98204082024-10-29 11:09:00 -0700583 android.InitDefaultableModule(module)
Dan Willemsenb0552672019-01-25 16:04:11 -0800584 return module
585}
586
Patrice Arrudae1034192019-03-11 13:20:17 -0700587// sh_binary_host is for a shell script to be installed as an executable binary
588// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700589func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800590 module := &ShBinary{}
Colin Cross8ff10582023-12-07 13:10:56 -0800591 initShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700592 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Ronald Braunstein98204082024-10-29 11:09:00 -0700593 android.InitDefaultableModule(module)
Dan Willemsenb0552672019-01-25 16:04:11 -0800594 return module
595}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800596
Jaewoong Jung61a83682019-07-01 09:08:50 -0700597// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700598func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800599 module := &ShTest{}
Colin Cross8ff10582023-12-07 13:10:56 -0800600 initShBinaryModule(&module.ShBinary)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800601 module.AddProperties(&module.testProperties)
602
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700603 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Ronald Braunstein98204082024-10-29 11:09:00 -0700604 android.InitDefaultableModule(module)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800605 return module
606}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700607
608// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700609func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700610 module := &ShTest{}
Colin Cross8ff10582023-12-07 13:10:56 -0800611 initShBinaryModule(&module.ShBinary)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700612 module.AddProperties(&module.testProperties)
Julien Desprez06f13992021-06-28 13:41:43 -0700613 // Default sh_test_host to unit_tests = true
614 if module.testProperties.Test_options.Unit_test == nil {
615 module.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
616 }
Jaewoong Jung61a83682019-07-01 09:08:50 -0700617
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700618 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Ronald Braunstein98204082024-10-29 11:09:00 -0700619 android.InitDefaultableModule(module)
620 return module
621}
622
623type ShDefaults struct {
624 android.ModuleBase
625 android.DefaultsModuleBase
626}
627
628func ShDefaultsFactory() android.Module {
629 module := &ShDefaults{}
630
631 module.AddProperties(&shBinaryProperties{}, &TestProperties{})
632 android.InitDefaultsModule(module)
633
Jaewoong Jung61a83682019-07-01 09:08:50 -0700634 return module
635}
frankfengc5b87492020-06-03 10:28:47 -0700636
637var Bool = proptools.Bool