blob: 1ae557a8d922abf89ffa3666e1130fa8254cf33e [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"
frankfengc5b87492020-06-03 10:28:47 -070029 "android/soong/tradefed"
Dan Willemsenb0552672019-01-25 16:04:11 -080030)
31
32// sh_binary is for shell scripts (and batch files) that are installed as
33// executable files into .../bin/
34//
35// Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary
36// instead.
37
Jaewoong Jung4b79e982020-06-01 10:45:49 -070038var pctx = android.NewPackageContext("android/soong/sh")
39
Dan Willemsenb0552672019-01-25 16:04:11 -080040func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070041 pctx.Import("android/soong/android")
42
Paul Duffin56fb8ee2021-03-08 15:05:52 +000043 registerShBuildComponents(android.InitRegistrationContext)
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +000044
45 android.RegisterBp2BuildMutator("sh_binary", ShBinaryBp2Build)
Dan Willemsenb0552672019-01-25 16:04:11 -080046}
47
Paul Duffin56fb8ee2021-03-08 15:05:52 +000048func registerShBuildComponents(ctx android.RegistrationContext) {
49 ctx.RegisterModuleType("sh_binary", ShBinaryFactory)
50 ctx.RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
51 ctx.RegisterModuleType("sh_test", ShTestFactory)
52 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
53}
54
55// Test fixture preparer that will register most sh build components.
56//
57// Singletons and mutators should only be added here if they are needed for a majority of sh
58// module types, otherwise they should be added under a separate preparer to allow them to be
59// selected only when needed to reduce test execution time.
60//
61// Module types do not have much of an overhead unless they are used so this should include as many
62// module types as possible. The exceptions are those module types that require mutators and/or
63// singletons in order to function in which case they should be kept together in a separate
64// preparer.
65var PrepareForTestWithShBuildComponents = android.GroupFixturePreparers(
66 android.FixtureRegisterWithContext(registerShBuildComponents),
67)
68
Dan Willemsenb0552672019-01-25 16:04:11 -080069type shBinaryProperties struct {
70 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080071 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080072
73 // optional subdirectory under which this file is installed into
74 Sub_dir *string `android:"arch_variant"`
75
76 // optional name for the installed file. If unspecified, name of the module is used as the file name
77 Filename *string `android:"arch_variant"`
78
79 // when set to true, and filename property is not set, the name for the installed file
80 // is the same as the file name of the source file.
81 Filename_from_src *bool `android:"arch_variant"`
82
83 // Whether this module is directly installable to one of the partitions. Default: true.
84 Installable *bool
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -070085
86 // install symlinks to the binary
87 Symlinks []string `android:"arch_variant"`
Colin Crosscc83efb2020-08-21 14:25:33 -070088
89 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070090 // On device without a dedicated recovery partition, the module is only
91 // available after switching root into
92 // /first_stage_ramdisk. To expose the module before switching root, install
93 // the recovery variant instead.
Colin Crosscc83efb2020-08-21 14:25:33 -070094 Ramdisk_available *bool
95
Yifan Hong60e0cfb2020-10-21 15:17:56 -070096 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070097 // On device without a dedicated recovery partition, the module is only
98 // available after switching root into
99 // /first_stage_ramdisk. To expose the module before switching root, install
100 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700101 Vendor_ramdisk_available *bool
102
Colin Crosscc83efb2020-08-21 14:25:33 -0700103 // Make this module available when building for recovery.
104 Recovery_available *bool
Dan Willemsenb0552672019-01-25 16:04:11 -0800105}
106
Julien Desprez9e7fc142019-03-08 11:07:05 -0800107type TestProperties struct {
108 // list of compatibility suites (for example "cts", "vts") that the module should be
109 // installed into.
110 Test_suites []string `android:"arch_variant"`
111
112 // the name of the test configuration (for example "AndroidTest.xml") that should be
113 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -0700114 Test_config *string `android:"path,arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700115
116 // list of files or filegroup modules that provide data that should be installed alongside
117 // the test.
118 Data []string `android:"path,arch_variant"`
frankfengc5b87492020-06-03 10:28:47 -0700119
120 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
121 // with root permission.
122 Require_root *bool
123
124 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
125 // should be installed with the module.
126 Test_config_template *string `android:"path,arch_variant"`
127
128 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
129 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
130 // explicitly.
131 Auto_gen_config *bool
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700132
133 // list of binary modules that should be installed alongside the test
134 Data_bins []string `android:"path,arch_variant"`
135
136 // list of library modules that should be installed alongside the test
137 Data_libs []string `android:"path,arch_variant"`
138
139 // list of device binary modules that should be installed alongside the test.
140 // Only available for host sh_test modules.
141 Data_device_bins []string `android:"path,arch_variant"`
142
143 // list of device library modules that should be installed alongside the test.
144 // Only available for host sh_test modules.
145 Data_device_libs []string `android:"path,arch_variant"`
Julien Desprez9e7fc142019-03-08 11:07:05 -0800146}
147
Dan Willemsenb0552672019-01-25 16:04:11 -0800148type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700149 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500150 android.BazelModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -0800151
152 properties shBinaryProperties
153
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700154 sourceFilePath android.Path
155 outputFilePath android.OutputPath
156 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800157}
158
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700159var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700160
Julien Desprez9e7fc142019-03-08 11:07:05 -0800161type ShTest struct {
162 ShBinary
163
164 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700165
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700166 installDir android.InstallPath
167
frankfengc5b87492020-06-03 10:28:47 -0700168 data android.Paths
169 testConfig android.Path
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700170
171 dataModules map[string]android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800172}
173
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700174func (s *ShBinary) HostToolPath() android.OptionalPath {
175 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700176}
177
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700178func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800179}
180
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700181func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800182 return s.outputFilePath
183}
184
185func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700186 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800187}
188
189func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700190 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800191}
192
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700193func (s *ShBinary) Symlinks() []string {
194 return s.properties.Symlinks
195}
196
Colin Crosscc83efb2020-08-21 14:25:33 -0700197var _ android.ImageInterface = (*ShBinary)(nil)
198
199func (s *ShBinary) ImageMutatorBegin(ctx android.BaseModuleContext) {}
200
201func (s *ShBinary) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
202 return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk()
203}
204
205func (s *ShBinary) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
206 return proptools.Bool(s.properties.Ramdisk_available) || s.ModuleBase.InstallInRamdisk()
207}
208
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700209func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
210 return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.ModuleBase.InstallInVendorRamdisk()
211}
212
Colin Crosscc83efb2020-08-21 14:25:33 -0700213func (s *ShBinary) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
214 return proptools.Bool(s.properties.Recovery_available) || s.ModuleBase.InstallInRecovery()
215}
216
217func (s *ShBinary) ExtraImageVariations(ctx android.BaseModuleContext) []string {
218 return nil
219}
220
221func (s *ShBinary) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
222}
223
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700224func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500225 if s.properties.Src == nil {
226 ctx.PropertyErrorf("src", "missing prebuilt source file")
227 }
228
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700229 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
230 filename := proptools.String(s.properties.Filename)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800231 filenameFromSrc := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800232 if filename == "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800233 if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800234 filename = s.sourceFilePath.Base()
235 } else {
236 filename = ctx.ModuleName()
237 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800238 } else if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800239 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
240 return
241 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700242 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800243
244 // This ensures that outputFilePath has the correct name for others to
245 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700246 ctx.Build(pctx, android.BuildParams{
247 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800248 Output: s.outputFilePath,
249 Input: s.sourceFilePath,
250 })
251}
252
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700253func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700254 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700255 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Colin Cross7c7c1142019-07-29 16:46:49 -0700256 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
257}
258
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700259func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
260 return []android.AndroidMkEntries{android.AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800261 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700262 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Dan Willemsenb0552672019-01-25 16:04:11 -0800263 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700264 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700265 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700266 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700267 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700268 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800269 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900270 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800271}
272
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700273func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700274 entries.SetString("LOCAL_MODULE_SUFFIX", "")
275 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700276 if len(s.properties.Symlinks) > 0 {
277 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
278 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700279}
280
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700281type dependencyTag struct {
282 blueprint.BaseDependencyTag
283 name string
284}
285
286var (
287 shTestDataBinsTag = dependencyTag{name: "dataBins"}
288 shTestDataLibsTag = dependencyTag{name: "dataLibs"}
289 shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
290 shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"}
291)
292
293var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}}
294
295func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) {
296 s.ShBinary.DepsMutator(ctx)
297
298 ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...)
299 ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...),
300 shTestDataLibsTag, s.testProperties.Data_libs...)
Liz Kammer356f7d42021-01-26 09:18:53 -0500301 if (ctx.Target().Os.Class == android.Host || ctx.BazelConversionMode()) && len(ctx.Config().Targets[android.Android]) > 0 {
Jaewoong Jung642916f2020-10-09 17:25:15 -0700302 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700303 ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...)
304 ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...),
305 shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...)
306 } else if ctx.Target().Os.Class != android.Host {
307 if len(s.testProperties.Data_device_bins) > 0 {
308 ctx.PropertyErrorf("data_device_bins", "only available for host modules")
309 }
310 if len(s.testProperties.Data_device_libs) > 0 {
311 ctx.PropertyErrorf("data_device_libs", "only available for host modules")
312 }
313 }
314}
315
316func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) {
317 if _, exists := s.dataModules[relPath]; exists {
318 ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s",
319 relPath, s.dataModules[relPath].String(), path.String())
320 return
321 }
322 s.dataModules[relPath] = path
323}
324
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700325func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700326 s.ShBinary.generateAndroidBuildActions(ctx)
327 testDir := "nativetest"
328 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
329 testDir = "nativetest64"
330 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700331 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700332 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
333 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
334 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
335 }
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700336 if s.SubDir() != "" {
337 // Don't add the module name to the installation path if sub_dir is specified for backward
338 // compatibility.
339 s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir())
340 } else {
341 s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name())
342 }
343 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700344
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700345 s.data = android.PathsForModuleSrc(ctx, s.testProperties.Data)
frankfengc5b87492020-06-03 10:28:47 -0700346
347 var configs []tradefed.Config
348 if Bool(s.testProperties.Require_root) {
349 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
350 } else {
351 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
352 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
353 }
frankfengbe6ae772020-09-28 13:22:57 -0700354 if len(s.testProperties.Data_device_bins) > 0 {
355 moduleName := s.Name()
356 remoteDir := "/data/local/tests/unrestricted/" + moduleName + "/"
357 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
358 for _, bin := range s.testProperties.Data_device_bins {
359 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: remoteDir + bin})
360 }
361 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options})
362 }
frankfengc5b87492020-06-03 10:28:47 -0700363 s.testConfig = tradefed.AutoGenShellTestConfig(ctx, s.testProperties.Test_config,
364 s.testProperties.Test_config_template, s.testProperties.Test_suites, configs, s.testProperties.Auto_gen_config, s.outputFilePath.Base())
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700365
366 s.dataModules = make(map[string]android.Path)
367 ctx.VisitDirectDeps(func(dep android.Module) {
368 depTag := ctx.OtherModuleDependencyTag(dep)
369 switch depTag {
370 case shTestDataBinsTag, shTestDataDeviceBinsTag:
Anton Hansson2f6422c2020-12-31 13:42:10 +0000371 path := android.OutputFileForModule(ctx, dep, "")
372 s.addToDataModules(ctx, path.Base(), path)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700373 case shTestDataLibsTag, shTestDataDeviceLibsTag:
374 if cc, isCc := dep.(*cc.Module); isCc {
375 // Copy to an intermediate output directory to append "lib[64]" to the path,
376 // so that it's compatible with the default rpath values.
377 var relPath string
378 if cc.Arch().ArchType.Multilib == "lib64" {
379 relPath = filepath.Join("lib64", cc.OutputFile().Path().Base())
380 } else {
381 relPath = filepath.Join("lib", cc.OutputFile().Path().Base())
382 }
383 if _, exist := s.dataModules[relPath]; exist {
384 return
385 }
386 relocatedLib := android.PathForModuleOut(ctx, "relocated", relPath)
387 ctx.Build(pctx, android.BuildParams{
388 Rule: android.Cp,
389 Input: cc.OutputFile().Path(),
390 Output: relocatedLib,
391 })
392 s.addToDataModules(ctx, relPath, relocatedLib)
393 return
394 }
395 property := "data_libs"
396 if depTag == shTestDataDeviceBinsTag {
397 property = "data_device_libs"
398 }
399 ctx.PropertyErrorf(property, "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
400 }
401 })
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700402}
403
Colin Cross7c7c1142019-07-29 16:46:49 -0700404func (s *ShTest) InstallInData() bool {
405 return true
406}
407
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700408func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
409 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700410 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700411 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700412 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700413 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700414 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700415 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700416 entries.SetPath("LOCAL_MODULE_PATH", s.installDir.ToMakePath())
Liz Kammer57f5b332020-11-24 12:42:58 -0800417 entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700418 if s.testConfig != nil {
419 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700420 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700421 for _, d := range s.data {
422 rel := d.Rel()
423 path := d.String()
424 if !strings.HasSuffix(path, rel) {
425 panic(fmt.Errorf("path %q does not end with %q", path, rel))
426 }
427 path = strings.TrimSuffix(path, rel)
428 entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700429 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700430 relPaths := make([]string, 0)
431 for relPath, _ := range s.dataModules {
432 relPaths = append(relPaths, relPath)
433 }
434 sort.Strings(relPaths)
435 for _, relPath := range relPaths {
436 dir := strings.TrimSuffix(s.dataModules[relPath].String(), relPath)
437 entries.AddStrings("LOCAL_TEST_DATA", dir+":"+relPath)
438 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700439 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700440 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900441 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800442}
443
Dan Willemsenb0552672019-01-25 16:04:11 -0800444func InitShBinaryModule(s *ShBinary) {
445 s.AddProperties(&s.properties)
Liz Kammerea6666f2021-02-17 10:17:28 -0500446 android.InitBazelModule(s)
Dan Willemsenb0552672019-01-25 16:04:11 -0800447}
448
Patrice Arrudae1034192019-03-11 13:20:17 -0700449// sh_binary is for a shell script or batch file to be installed as an
450// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700451func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800452 module := &ShBinary{}
453 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700454 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800455 return module
456}
457
Patrice Arrudae1034192019-03-11 13:20:17 -0700458// sh_binary_host is for a shell script to be installed as an executable binary
459// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700460func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800461 module := &ShBinary{}
462 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700463 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800464 return module
465}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800466
Jaewoong Jung61a83682019-07-01 09:08:50 -0700467// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700468func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800469 module := &ShTest{}
470 InitShBinaryModule(&module.ShBinary)
471 module.AddProperties(&module.testProperties)
472
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700473 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800474 return module
475}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700476
477// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700478func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700479 module := &ShTest{}
480 InitShBinaryModule(&module.ShBinary)
481 module.AddProperties(&module.testProperties)
482
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700483 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700484 return module
485}
frankfengc5b87492020-06-03 10:28:47 -0700486
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000487type bazelShBinaryAttributes struct {
488 Srcs bazel.LabelList
489 // Bazel also supports the attributes below, but (so far) these are not required for Bionic
490 // deps
491 // data
492 // args
493 // compatible_with
494 // deprecation
495 // distribs
496 // env
497 // exec_compatible_with
498 // exec_properties
499 // features
500 // licenses
501 // output_licenses
502 // restricted_to
503 // tags
504 // target_compatible_with
505 // testonly
506 // toolchains
507 // visibility
508}
509
510type bazelShBinary struct {
511 android.BazelTargetModuleBase
512 bazelShBinaryAttributes
513}
514
515func BazelShBinaryFactory() android.Module {
516 module := &bazelShBinary{}
517 module.AddProperties(&module.bazelShBinaryAttributes)
518 android.InitBazelTargetModule(module)
519 return module
520}
521
522func ShBinaryBp2Build(ctx android.TopDownMutatorContext) {
523 m, ok := ctx.Module().(*ShBinary)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500524 if !ok || !m.ConvertWithBp2build(ctx) {
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000525 return
526 }
527
528 srcs := android.BazelLabelForModuleSrc(ctx, []string{*m.properties.Src})
529
530 attrs := &bazelShBinaryAttributes{
531 Srcs: srcs,
532 }
533
Liz Kammerfc46bc12021-02-19 11:06:17 -0500534 props := bazel.BazelTargetModuleProperties{
535 Rule_class: "sh_binary",
536 }
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000537
Liz Kammerfc46bc12021-02-19 11:06:17 -0500538 ctx.CreateBazelTargetModule(BazelShBinaryFactory, m.Name(), props, attrs)
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000539}
540
541func (m *bazelShBinary) Name() string {
542 return m.BaseModuleName()
543}
544
545func (m *bazelShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
546
frankfengc5b87492020-06-03 10:28:47 -0700547var Bool = proptools.Bool