blob: d2eede65da581b08c53df5766e769cb5a58d5087 [file] [log] [blame]
Dan Willemsenb0552672019-01-25 16:04:11 -08001// Copyright 2019 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Jaewoong Jung4b79e982020-06-01 10:45:49 -070015package sh
Dan Willemsenb0552672019-01-25 16:04:11 -080016
17import (
18 "fmt"
Colin Cross7c7c1142019-07-29 16:46:49 -070019 "path/filepath"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070020 "sort"
Julien Desprez9e7fc142019-03-08 11:07:05 -080021 "strings"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070022
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"
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +000027 "android/soong/bazel"
Jaewoong Jung6e0eee52020-05-29 16:15:32 -070028 "android/soong/cc"
Rob Seymour925aa092021-08-10 20:42:03 +000029 "android/soong/snapshot"
frankfengc5b87492020-06-03 10:28:47 -070030 "android/soong/tradefed"
Dan Willemsenb0552672019-01-25 16:04:11 -080031)
32
33// sh_binary is for shell scripts (and batch files) that are installed as
34// executable files into .../bin/
35//
36// Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary
37// instead.
38
Jaewoong Jung4b79e982020-06-01 10:45:49 -070039var pctx = android.NewPackageContext("android/soong/sh")
40
Dan Willemsenb0552672019-01-25 16:04:11 -080041func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070042 pctx.Import("android/soong/android")
43
Paul Duffin56fb8ee2021-03-08 15:05:52 +000044 registerShBuildComponents(android.InitRegistrationContext)
Dan Willemsenb0552672019-01-25 16:04:11 -080045}
46
Paul Duffin56fb8ee2021-03-08 15:05:52 +000047func registerShBuildComponents(ctx android.RegistrationContext) {
48 ctx.RegisterModuleType("sh_binary", ShBinaryFactory)
49 ctx.RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
50 ctx.RegisterModuleType("sh_test", ShTestFactory)
51 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
52}
53
54// Test fixture preparer that will register most sh build components.
55//
56// Singletons and mutators should only be added here if they are needed for a majority of sh
57// module types, otherwise they should be added under a separate preparer to allow them to be
58// selected only when needed to reduce test execution time.
59//
60// Module types do not have much of an overhead unless they are used so this should include as many
61// module types as possible. The exceptions are those module types that require mutators and/or
62// singletons in order to function in which case they should be kept together in a separate
63// preparer.
64var PrepareForTestWithShBuildComponents = android.GroupFixturePreparers(
65 android.FixtureRegisterWithContext(registerShBuildComponents),
66)
67
Dan Willemsenb0552672019-01-25 16:04:11 -080068type shBinaryProperties struct {
69 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080070 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080071
72 // optional subdirectory under which this file is installed into
73 Sub_dir *string `android:"arch_variant"`
74
75 // optional name for the installed file. If unspecified, name of the module is used as the file name
76 Filename *string `android:"arch_variant"`
77
78 // when set to true, and filename property is not set, the name for the installed file
79 // is the same as the file name of the source file.
80 Filename_from_src *bool `android:"arch_variant"`
81
82 // Whether this module is directly installable to one of the partitions. Default: true.
83 Installable *bool
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -070084
85 // install symlinks to the binary
86 Symlinks []string `android:"arch_variant"`
Colin Crosscc83efb2020-08-21 14:25:33 -070087
88 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070089 // On device without a dedicated recovery partition, the module is only
90 // available after switching root into
91 // /first_stage_ramdisk. To expose the module before switching root, install
92 // the recovery variant instead.
Colin Crosscc83efb2020-08-21 14:25:33 -070093 Ramdisk_available *bool
94
Yifan Hong60e0cfb2020-10-21 15:17:56 -070095 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070096 // On device without a dedicated recovery partition, the module is only
97 // available after switching root into
98 // /first_stage_ramdisk. To expose the module before switching root, install
99 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700100 Vendor_ramdisk_available *bool
101
Colin Crosscc83efb2020-08-21 14:25:33 -0700102 // Make this module available when building for recovery.
103 Recovery_available *bool
Dan Willemsenb0552672019-01-25 16:04:11 -0800104}
105
Julien Desprez9e7fc142019-03-08 11:07:05 -0800106type TestProperties struct {
107 // list of compatibility suites (for example "cts", "vts") that the module should be
108 // installed into.
109 Test_suites []string `android:"arch_variant"`
110
111 // the name of the test configuration (for example "AndroidTest.xml") that should be
112 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -0700113 Test_config *string `android:"path,arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700114
115 // list of files or filegroup modules that provide data that should be installed alongside
116 // the test.
117 Data []string `android:"path,arch_variant"`
frankfengc5b87492020-06-03 10:28:47 -0700118
119 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
120 // with root permission.
121 Require_root *bool
122
123 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
124 // should be installed with the module.
125 Test_config_template *string `android:"path,arch_variant"`
126
127 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
128 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
129 // explicitly.
130 Auto_gen_config *bool
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700131
132 // list of binary modules that should be installed alongside the test
133 Data_bins []string `android:"path,arch_variant"`
134
135 // list of library modules that should be installed alongside the test
136 Data_libs []string `android:"path,arch_variant"`
137
138 // list of device binary modules that should be installed alongside the test.
139 // Only available for host sh_test modules.
140 Data_device_bins []string `android:"path,arch_variant"`
141
142 // list of device library modules that should be installed alongside the test.
143 // Only available for host sh_test modules.
144 Data_device_libs []string `android:"path,arch_variant"`
Dan Shib40deac2021-05-24 12:04:54 -0700145
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900146 // Install the test into a folder named for the module in all test suites.
147 Per_testcase_directory *bool
148
Dan Shib40deac2021-05-24 12:04:54 -0700149 // Test options.
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800150 Test_options android.CommonTestOptions
Julien Desprez9e7fc142019-03-08 11:07:05 -0800151}
152
Dan Willemsenb0552672019-01-25 16:04:11 -0800153type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700154 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500155 android.BazelModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -0800156
157 properties shBinaryProperties
158
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700159 sourceFilePath android.Path
160 outputFilePath android.OutputPath
161 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800162}
163
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700164var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700165
Julien Desprez9e7fc142019-03-08 11:07:05 -0800166type ShTest struct {
167 ShBinary
168
169 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700170
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700171 installDir android.InstallPath
172
frankfengc5b87492020-06-03 10:28:47 -0700173 data android.Paths
174 testConfig android.Path
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700175
176 dataModules map[string]android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800177}
178
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700179func (s *ShBinary) HostToolPath() android.OptionalPath {
180 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700181}
182
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700183func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800184}
185
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700186func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800187 return s.outputFilePath
188}
189
190func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700191 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800192}
193
Rob Seymour925aa092021-08-10 20:42:03 +0000194func (s *ShBinary) RelativeInstallPath() string {
195 return s.SubDir()
196}
Dan Willemsenb0552672019-01-25 16:04:11 -0800197func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700198 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800199}
200
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700201func (s *ShBinary) Symlinks() []string {
202 return s.properties.Symlinks
203}
204
Colin Crosscc83efb2020-08-21 14:25:33 -0700205var _ android.ImageInterface = (*ShBinary)(nil)
206
207func (s *ShBinary) ImageMutatorBegin(ctx android.BaseModuleContext) {}
208
209func (s *ShBinary) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
210 return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk()
211}
212
213func (s *ShBinary) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
214 return proptools.Bool(s.properties.Ramdisk_available) || s.ModuleBase.InstallInRamdisk()
215}
216
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700217func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
218 return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.ModuleBase.InstallInVendorRamdisk()
219}
220
Inseob Kim08758f02021-04-08 21:13:22 +0900221func (s *ShBinary) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
222 return false
223}
224
Colin Crosscc83efb2020-08-21 14:25:33 -0700225func (s *ShBinary) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
226 return proptools.Bool(s.properties.Recovery_available) || s.ModuleBase.InstallInRecovery()
227}
228
229func (s *ShBinary) ExtraImageVariations(ctx android.BaseModuleContext) []string {
230 return nil
231}
232
233func (s *ShBinary) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
234}
235
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700236func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500237 if s.properties.Src == nil {
238 ctx.PropertyErrorf("src", "missing prebuilt source file")
239 }
240
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700241 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
242 filename := proptools.String(s.properties.Filename)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800243 filenameFromSrc := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800244 if filename == "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800245 if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800246 filename = s.sourceFilePath.Base()
247 } else {
248 filename = ctx.ModuleName()
249 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800250 } else if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800251 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
252 return
253 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700254 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800255
256 // This ensures that outputFilePath has the correct name for others to
257 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700258 ctx.Build(pctx, android.BuildParams{
259 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800260 Output: s.outputFilePath,
261 Input: s.sourceFilePath,
262 })
263}
264
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700265func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700266 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700267 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000268 if !s.Installable() {
269 s.SkipInstall()
270 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700271 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
Colin Cross94bf5182021-11-09 17:21:14 -0800272 for _, symlink := range s.Symlinks() {
273 ctx.InstallSymlink(installDir, symlink, s.installedFile)
274 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700275}
276
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700277func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
278 return []android.AndroidMkEntries{android.AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800279 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700280 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500281 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700282 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700283 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700284 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700285 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000286 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !s.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700287 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800288 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900289 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800290}
291
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700292func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700293 entries.SetString("LOCAL_MODULE_SUFFIX", "")
294 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700295 if len(s.properties.Symlinks) > 0 {
296 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
297 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700298}
299
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700300type dependencyTag struct {
301 blueprint.BaseDependencyTag
302 name string
303}
304
305var (
306 shTestDataBinsTag = dependencyTag{name: "dataBins"}
307 shTestDataLibsTag = dependencyTag{name: "dataLibs"}
308 shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
309 shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"}
310)
311
312var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}}
313
314func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) {
315 s.ShBinary.DepsMutator(ctx)
316
317 ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...)
318 ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...),
319 shTestDataLibsTag, s.testProperties.Data_libs...)
Liz Kammer3bf97bd2022-04-26 09:38:20 -0400320 if ctx.Target().Os.Class == android.Host && len(ctx.Config().Targets[android.Android]) > 0 {
Jaewoong Jung642916f2020-10-09 17:25:15 -0700321 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700322 ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...)
323 ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...),
324 shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...)
325 } else if ctx.Target().Os.Class != android.Host {
326 if len(s.testProperties.Data_device_bins) > 0 {
327 ctx.PropertyErrorf("data_device_bins", "only available for host modules")
328 }
329 if len(s.testProperties.Data_device_libs) > 0 {
330 ctx.PropertyErrorf("data_device_libs", "only available for host modules")
331 }
332 }
333}
334
335func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) {
336 if _, exists := s.dataModules[relPath]; exists {
337 ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s",
338 relPath, s.dataModules[relPath].String(), path.String())
339 return
340 }
341 s.dataModules[relPath] = path
342}
343
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700344func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700345 s.ShBinary.generateAndroidBuildActions(ctx)
346 testDir := "nativetest"
347 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
348 testDir = "nativetest64"
349 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700350 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700351 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
352 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
353 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
354 }
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700355 if s.SubDir() != "" {
356 // Don't add the module name to the installation path if sub_dir is specified for backward
357 // compatibility.
358 s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir())
359 } else {
360 s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name())
361 }
362 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700363
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700364 s.data = android.PathsForModuleSrc(ctx, s.testProperties.Data)
frankfengc5b87492020-06-03 10:28:47 -0700365
366 var configs []tradefed.Config
367 if Bool(s.testProperties.Require_root) {
368 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
369 } else {
370 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
371 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
372 }
frankfengbe6ae772020-09-28 13:22:57 -0700373 if len(s.testProperties.Data_device_bins) > 0 {
374 moduleName := s.Name()
375 remoteDir := "/data/local/tests/unrestricted/" + moduleName + "/"
376 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
377 for _, bin := range s.testProperties.Data_device_bins {
378 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: remoteDir + bin})
379 }
380 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options})
381 }
Cole Faust21680542022-12-07 18:18:37 -0800382 s.testConfig = tradefed.AutoGenTestConfig(ctx, tradefed.AutoGenTestConfigOptions{
383 TestConfigProp: s.testProperties.Test_config,
384 TestConfigTemplateProp: s.testProperties.Test_config_template,
385 TestSuites: s.testProperties.Test_suites,
386 Config: configs,
387 AutoGenConfig: s.testProperties.Auto_gen_config,
388 OutputFileName: s.outputFilePath.Base(),
389 DeviceTemplate: "${ShellTestConfigTemplate}",
390 HostTemplate: "${ShellTestConfigTemplate}",
391 })
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700392
393 s.dataModules = make(map[string]android.Path)
394 ctx.VisitDirectDeps(func(dep android.Module) {
395 depTag := ctx.OtherModuleDependencyTag(dep)
396 switch depTag {
397 case shTestDataBinsTag, shTestDataDeviceBinsTag:
Anton Hansson2f6422c2020-12-31 13:42:10 +0000398 path := android.OutputFileForModule(ctx, dep, "")
399 s.addToDataModules(ctx, path.Base(), path)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700400 case shTestDataLibsTag, shTestDataDeviceLibsTag:
401 if cc, isCc := dep.(*cc.Module); isCc {
402 // Copy to an intermediate output directory to append "lib[64]" to the path,
403 // so that it's compatible with the default rpath values.
404 var relPath string
405 if cc.Arch().ArchType.Multilib == "lib64" {
406 relPath = filepath.Join("lib64", cc.OutputFile().Path().Base())
407 } else {
408 relPath = filepath.Join("lib", cc.OutputFile().Path().Base())
409 }
410 if _, exist := s.dataModules[relPath]; exist {
411 return
412 }
413 relocatedLib := android.PathForModuleOut(ctx, "relocated", relPath)
414 ctx.Build(pctx, android.BuildParams{
415 Rule: android.Cp,
416 Input: cc.OutputFile().Path(),
417 Output: relocatedLib,
418 })
419 s.addToDataModules(ctx, relPath, relocatedLib)
420 return
421 }
422 property := "data_libs"
423 if depTag == shTestDataDeviceBinsTag {
424 property = "data_device_libs"
425 }
426 ctx.PropertyErrorf(property, "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
427 }
428 })
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700429}
430
Colin Cross7c7c1142019-07-29 16:46:49 -0700431func (s *ShTest) InstallInData() bool {
432 return true
433}
434
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700435func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
436 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700437 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700438 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500439 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700440 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700441 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700442 s.customAndroidMkEntries(entries)
Colin Crossc68db4b2021-11-11 18:59:15 -0800443 entries.SetPath("LOCAL_MODULE_PATH", s.installDir)
Liz Kammer57f5b332020-11-24 12:42:58 -0800444 entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700445 if s.testConfig != nil {
446 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700447 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700448 for _, d := range s.data {
449 rel := d.Rel()
450 path := d.String()
451 if !strings.HasSuffix(path, rel) {
452 panic(fmt.Errorf("path %q does not end with %q", path, rel))
453 }
454 path = strings.TrimSuffix(path, rel)
455 entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700456 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700457 relPaths := make([]string, 0)
458 for relPath, _ := range s.dataModules {
459 relPaths = append(relPaths, relPath)
460 }
461 sort.Strings(relPaths)
462 for _, relPath := range relPaths {
463 dir := strings.TrimSuffix(s.dataModules[relPath].String(), relPath)
464 entries.AddStrings("LOCAL_TEST_DATA", dir+":"+relPath)
465 }
yangbill22bafec2022-02-11 18:06:07 +0800466 if s.testProperties.Data_bins != nil {
467 entries.AddStrings("LOCAL_TEST_DATA_BINS", s.testProperties.Data_bins...)
468 }
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900469 entries.SetBoolIfTrue("LOCAL_COMPATIBILITY_PER_TESTCASE_DIRECTORY", Bool(s.testProperties.Per_testcase_directory))
Zhenhuang Wang0ac5a432022-08-12 18:49:20 +0800470
471 s.testProperties.Test_options.SetAndroidMkEntries(entries)
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700472 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700473 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900474 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800475}
476
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400477func initShBinaryModule(s *ShBinary, useBazel bool) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800478 s.AddProperties(&s.properties)
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400479 if useBazel {
480 android.InitBazelModule(s)
481 }
Dan Willemsenb0552672019-01-25 16:04:11 -0800482}
483
Patrice Arrudae1034192019-03-11 13:20:17 -0700484// sh_binary is for a shell script or batch file to be installed as an
485// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700486func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800487 module := &ShBinary{}
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400488 initShBinaryModule(module, true)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700489 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800490 return module
491}
492
Patrice Arrudae1034192019-03-11 13:20:17 -0700493// sh_binary_host is for a shell script to be installed as an executable binary
494// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700495func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800496 module := &ShBinary{}
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400497 initShBinaryModule(module, true)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700498 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800499 return module
500}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800501
Jaewoong Jung61a83682019-07-01 09:08:50 -0700502// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700503func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800504 module := &ShTest{}
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400505 initShBinaryModule(&module.ShBinary, false)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800506 module.AddProperties(&module.testProperties)
507
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700508 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800509 return module
510}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700511
512// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700513func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700514 module := &ShTest{}
Liz Kammer9b2ca5c2023-04-28 17:39:24 -0400515 initShBinaryModule(&module.ShBinary, false)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700516 module.AddProperties(&module.testProperties)
Julien Desprez06f13992021-06-28 13:41:43 -0700517 // Default sh_test_host to unit_tests = true
518 if module.testProperties.Test_options.Unit_test == nil {
519 module.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
520 }
Jaewoong Jung61a83682019-07-01 09:08:50 -0700521
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700522 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700523 return module
524}
frankfengc5b87492020-06-03 10:28:47 -0700525
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000526type bazelShBinaryAttributes struct {
Yu Liub3024552021-11-23 14:53:41 -0800527 Srcs bazel.LabelListAttribute
Liz Kammer48c6ead2021-12-21 15:36:11 -0500528 Filename *string
529 Sub_dir *string
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000530 // Bazel also supports the attributes below, but (so far) these are not required for Bionic
531 // deps
532 // data
533 // args
534 // compatible_with
535 // deprecation
536 // distribs
537 // env
538 // exec_compatible_with
539 // exec_properties
540 // features
541 // licenses
542 // output_licenses
543 // restricted_to
544 // tags
545 // target_compatible_with
546 // testonly
547 // toolchains
548 // visibility
549}
550
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400551func (m *ShBinary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -0400552 srcs := bazel.MakeLabelListAttribute(
553 android.BazelLabelForModuleSrc(ctx, []string{*m.properties.Src}))
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000554
Liz Kammer48c6ead2021-12-21 15:36:11 -0500555 var filename *string
Yu Liub3024552021-11-23 14:53:41 -0800556 if m.properties.Filename != nil {
Liz Kammer48c6ead2021-12-21 15:36:11 -0500557 filename = m.properties.Filename
Yu Liub3024552021-11-23 14:53:41 -0800558 }
559
Liz Kammer48c6ead2021-12-21 15:36:11 -0500560 var subDir *string
Yu Liub3024552021-11-23 14:53:41 -0800561 if m.properties.Sub_dir != nil {
Liz Kammer48c6ead2021-12-21 15:36:11 -0500562 subDir = m.properties.Sub_dir
Yu Liub3024552021-11-23 14:53:41 -0800563 }
564
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000565 attrs := &bazelShBinaryAttributes{
Yu Liub3024552021-11-23 14:53:41 -0800566 Srcs: srcs,
567 Filename: filename,
568 Sub_dir: subDir,
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000569 }
570
Liz Kammerfc46bc12021-02-19 11:06:17 -0500571 props := bazel.BazelTargetModuleProperties{
Yu Liub3024552021-11-23 14:53:41 -0800572 Rule_class: "sh_binary",
573 Bzl_load_location: "//build/bazel/rules:sh_binary.bzl",
Liz Kammerfc46bc12021-02-19 11:06:17 -0500574 }
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000575
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000576 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000577}
578
frankfengc5b87492020-06-03 10:28:47 -0700579var Bool = proptools.Bool
Rob Seymour925aa092021-08-10 20:42:03 +0000580
581var _ snapshot.RelativeInstallPath = (*ShBinary)(nil)