blob: 16474282e48c5e599da37481f8518a45515e5ce0 [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
Dan Shib40deac2021-05-24 12:04:54 -0700107// Test option struct.
108type TestOptions struct {
109 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
110 Unit_test *bool
111}
112
Julien Desprez9e7fc142019-03-08 11:07:05 -0800113type TestProperties struct {
114 // list of compatibility suites (for example "cts", "vts") that the module should be
115 // installed into.
116 Test_suites []string `android:"arch_variant"`
117
118 // the name of the test configuration (for example "AndroidTest.xml") that should be
119 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -0700120 Test_config *string `android:"path,arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700121
122 // list of files or filegroup modules that provide data that should be installed alongside
123 // the test.
124 Data []string `android:"path,arch_variant"`
frankfengc5b87492020-06-03 10:28:47 -0700125
126 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
127 // with root permission.
128 Require_root *bool
129
130 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
131 // should be installed with the module.
132 Test_config_template *string `android:"path,arch_variant"`
133
134 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
135 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
136 // explicitly.
137 Auto_gen_config *bool
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700138
139 // list of binary modules that should be installed alongside the test
140 Data_bins []string `android:"path,arch_variant"`
141
142 // list of library modules that should be installed alongside the test
143 Data_libs []string `android:"path,arch_variant"`
144
145 // list of device binary modules that should be installed alongside the test.
146 // Only available for host sh_test modules.
147 Data_device_bins []string `android:"path,arch_variant"`
148
149 // list of device library modules that should be installed alongside the test.
150 // Only available for host sh_test modules.
151 Data_device_libs []string `android:"path,arch_variant"`
Dan Shib40deac2021-05-24 12:04:54 -0700152
153 // Test options.
154 Test_options TestOptions
Julien Desprez9e7fc142019-03-08 11:07:05 -0800155}
156
Dan Willemsenb0552672019-01-25 16:04:11 -0800157type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700158 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500159 android.BazelModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -0800160
161 properties shBinaryProperties
162
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700163 sourceFilePath android.Path
164 outputFilePath android.OutputPath
165 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800166}
167
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700168var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700169
Julien Desprez9e7fc142019-03-08 11:07:05 -0800170type ShTest struct {
171 ShBinary
172
173 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700174
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700175 installDir android.InstallPath
176
frankfengc5b87492020-06-03 10:28:47 -0700177 data android.Paths
178 testConfig android.Path
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700179
180 dataModules map[string]android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800181}
182
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700183func (s *ShBinary) HostToolPath() android.OptionalPath {
184 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700185}
186
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700187func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800188}
189
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700190func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800191 return s.outputFilePath
192}
193
194func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700195 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800196}
197
198func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700199 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800200}
201
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700202func (s *ShBinary) Symlinks() []string {
203 return s.properties.Symlinks
204}
205
Colin Crosscc83efb2020-08-21 14:25:33 -0700206var _ android.ImageInterface = (*ShBinary)(nil)
207
208func (s *ShBinary) ImageMutatorBegin(ctx android.BaseModuleContext) {}
209
210func (s *ShBinary) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
211 return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk()
212}
213
214func (s *ShBinary) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
215 return proptools.Bool(s.properties.Ramdisk_available) || s.ModuleBase.InstallInRamdisk()
216}
217
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700218func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
219 return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.ModuleBase.InstallInVendorRamdisk()
220}
221
Inseob Kim08758f02021-04-08 21:13:22 +0900222func (s *ShBinary) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
223 return false
224}
225
Colin Crosscc83efb2020-08-21 14:25:33 -0700226func (s *ShBinary) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
227 return proptools.Bool(s.properties.Recovery_available) || s.ModuleBase.InstallInRecovery()
228}
229
230func (s *ShBinary) ExtraImageVariations(ctx android.BaseModuleContext) []string {
231 return nil
232}
233
234func (s *ShBinary) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
235}
236
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700237func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500238 if s.properties.Src == nil {
239 ctx.PropertyErrorf("src", "missing prebuilt source file")
240 }
241
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700242 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
243 filename := proptools.String(s.properties.Filename)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800244 filenameFromSrc := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800245 if filename == "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800246 if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800247 filename = s.sourceFilePath.Base()
248 } else {
249 filename = ctx.ModuleName()
250 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800251 } else if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800252 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
253 return
254 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700255 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800256
257 // This ensures that outputFilePath has the correct name for others to
258 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700259 ctx.Build(pctx, android.BuildParams{
260 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800261 Output: s.outputFilePath,
262 Input: s.sourceFilePath,
263 })
264}
265
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700266func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700267 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700268 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Colin Cross7c7c1142019-07-29 16:46:49 -0700269 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
270}
271
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700272func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
273 return []android.AndroidMkEntries{android.AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800274 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700275 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Dan Willemsenb0552672019-01-25 16:04:11 -0800276 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700277 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700278 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700279 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700280 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700281 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800282 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900283 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800284}
285
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700286func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700287 entries.SetString("LOCAL_MODULE_SUFFIX", "")
288 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700289 if len(s.properties.Symlinks) > 0 {
290 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
291 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700292}
293
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700294type dependencyTag struct {
295 blueprint.BaseDependencyTag
296 name string
297}
298
299var (
300 shTestDataBinsTag = dependencyTag{name: "dataBins"}
301 shTestDataLibsTag = dependencyTag{name: "dataLibs"}
302 shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
303 shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"}
304)
305
306var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}}
307
308func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) {
309 s.ShBinary.DepsMutator(ctx)
310
311 ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...)
312 ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...),
313 shTestDataLibsTag, s.testProperties.Data_libs...)
Liz Kammer356f7d42021-01-26 09:18:53 -0500314 if (ctx.Target().Os.Class == android.Host || ctx.BazelConversionMode()) && len(ctx.Config().Targets[android.Android]) > 0 {
Jaewoong Jung642916f2020-10-09 17:25:15 -0700315 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700316 ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...)
317 ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...),
318 shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...)
319 } else if ctx.Target().Os.Class != android.Host {
320 if len(s.testProperties.Data_device_bins) > 0 {
321 ctx.PropertyErrorf("data_device_bins", "only available for host modules")
322 }
323 if len(s.testProperties.Data_device_libs) > 0 {
324 ctx.PropertyErrorf("data_device_libs", "only available for host modules")
325 }
326 }
327}
328
329func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) {
330 if _, exists := s.dataModules[relPath]; exists {
331 ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s",
332 relPath, s.dataModules[relPath].String(), path.String())
333 return
334 }
335 s.dataModules[relPath] = path
336}
337
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700338func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700339 s.ShBinary.generateAndroidBuildActions(ctx)
340 testDir := "nativetest"
341 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
342 testDir = "nativetest64"
343 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700344 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700345 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
346 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
347 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
348 }
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700349 if s.SubDir() != "" {
350 // Don't add the module name to the installation path if sub_dir is specified for backward
351 // compatibility.
352 s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir())
353 } else {
354 s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name())
355 }
356 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700357
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700358 s.data = android.PathsForModuleSrc(ctx, s.testProperties.Data)
frankfengc5b87492020-06-03 10:28:47 -0700359
360 var configs []tradefed.Config
361 if Bool(s.testProperties.Require_root) {
362 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
363 } else {
364 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
365 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
366 }
frankfengbe6ae772020-09-28 13:22:57 -0700367 if len(s.testProperties.Data_device_bins) > 0 {
368 moduleName := s.Name()
369 remoteDir := "/data/local/tests/unrestricted/" + moduleName + "/"
370 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
371 for _, bin := range s.testProperties.Data_device_bins {
372 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: remoteDir + bin})
373 }
374 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options})
375 }
frankfengc5b87492020-06-03 10:28:47 -0700376 s.testConfig = tradefed.AutoGenShellTestConfig(ctx, s.testProperties.Test_config,
377 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 -0700378
379 s.dataModules = make(map[string]android.Path)
380 ctx.VisitDirectDeps(func(dep android.Module) {
381 depTag := ctx.OtherModuleDependencyTag(dep)
382 switch depTag {
383 case shTestDataBinsTag, shTestDataDeviceBinsTag:
Anton Hansson2f6422c2020-12-31 13:42:10 +0000384 path := android.OutputFileForModule(ctx, dep, "")
385 s.addToDataModules(ctx, path.Base(), path)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700386 case shTestDataLibsTag, shTestDataDeviceLibsTag:
387 if cc, isCc := dep.(*cc.Module); isCc {
388 // Copy to an intermediate output directory to append "lib[64]" to the path,
389 // so that it's compatible with the default rpath values.
390 var relPath string
391 if cc.Arch().ArchType.Multilib == "lib64" {
392 relPath = filepath.Join("lib64", cc.OutputFile().Path().Base())
393 } else {
394 relPath = filepath.Join("lib", cc.OutputFile().Path().Base())
395 }
396 if _, exist := s.dataModules[relPath]; exist {
397 return
398 }
399 relocatedLib := android.PathForModuleOut(ctx, "relocated", relPath)
400 ctx.Build(pctx, android.BuildParams{
401 Rule: android.Cp,
402 Input: cc.OutputFile().Path(),
403 Output: relocatedLib,
404 })
405 s.addToDataModules(ctx, relPath, relocatedLib)
406 return
407 }
408 property := "data_libs"
409 if depTag == shTestDataDeviceBinsTag {
410 property = "data_device_libs"
411 }
412 ctx.PropertyErrorf(property, "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
413 }
414 })
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700415}
416
Colin Cross7c7c1142019-07-29 16:46:49 -0700417func (s *ShTest) InstallInData() bool {
418 return true
419}
420
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700421func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
422 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700423 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700424 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700425 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700426 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700427 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700428 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700429 entries.SetPath("LOCAL_MODULE_PATH", s.installDir.ToMakePath())
Liz Kammer57f5b332020-11-24 12:42:58 -0800430 entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700431 if s.testConfig != nil {
432 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700433 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700434 for _, d := range s.data {
435 rel := d.Rel()
436 path := d.String()
437 if !strings.HasSuffix(path, rel) {
438 panic(fmt.Errorf("path %q does not end with %q", path, rel))
439 }
440 path = strings.TrimSuffix(path, rel)
441 entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700442 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700443 relPaths := make([]string, 0)
444 for relPath, _ := range s.dataModules {
445 relPaths = append(relPaths, relPath)
446 }
447 sort.Strings(relPaths)
448 for _, relPath := range relPaths {
449 dir := strings.TrimSuffix(s.dataModules[relPath].String(), relPath)
450 entries.AddStrings("LOCAL_TEST_DATA", dir+":"+relPath)
451 }
Dan Shib40deac2021-05-24 12:04:54 -0700452 if Bool(s.testProperties.Test_options.Unit_test) {
453 entries.SetBool("LOCAL_IS_UNIT_TEST", true)
454 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700455 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700456 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900457 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800458}
459
Dan Willemsenb0552672019-01-25 16:04:11 -0800460func InitShBinaryModule(s *ShBinary) {
461 s.AddProperties(&s.properties)
Liz Kammerea6666f2021-02-17 10:17:28 -0500462 android.InitBazelModule(s)
Dan Willemsenb0552672019-01-25 16:04:11 -0800463}
464
Patrice Arrudae1034192019-03-11 13:20:17 -0700465// sh_binary is for a shell script or batch file to be installed as an
466// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700467func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800468 module := &ShBinary{}
469 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700470 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800471 return module
472}
473
Patrice Arrudae1034192019-03-11 13:20:17 -0700474// sh_binary_host is for a shell script to be installed as an executable binary
475// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700476func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800477 module := &ShBinary{}
478 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700479 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800480 return module
481}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800482
Jaewoong Jung61a83682019-07-01 09:08:50 -0700483// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700484func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800485 module := &ShTest{}
486 InitShBinaryModule(&module.ShBinary)
487 module.AddProperties(&module.testProperties)
488
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700489 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800490 return module
491}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700492
493// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700494func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700495 module := &ShTest{}
496 InitShBinaryModule(&module.ShBinary)
497 module.AddProperties(&module.testProperties)
Julien Desprez06f13992021-06-28 13:41:43 -0700498 // Default sh_test_host to unit_tests = true
499 if module.testProperties.Test_options.Unit_test == nil {
500 module.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
501 }
Jaewoong Jung61a83682019-07-01 09:08:50 -0700502
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700503 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700504 return module
505}
frankfengc5b87492020-06-03 10:28:47 -0700506
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000507type bazelShBinaryAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400508 Srcs bazel.LabelListAttribute
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000509 // Bazel also supports the attributes below, but (so far) these are not required for Bionic
510 // deps
511 // data
512 // args
513 // compatible_with
514 // deprecation
515 // distribs
516 // env
517 // exec_compatible_with
518 // exec_properties
519 // features
520 // licenses
521 // output_licenses
522 // restricted_to
523 // tags
524 // target_compatible_with
525 // testonly
526 // toolchains
527 // visibility
528}
529
530type bazelShBinary struct {
531 android.BazelTargetModuleBase
532 bazelShBinaryAttributes
533}
534
535func BazelShBinaryFactory() android.Module {
536 module := &bazelShBinary{}
537 module.AddProperties(&module.bazelShBinaryAttributes)
538 android.InitBazelTargetModule(module)
539 return module
540}
541
542func ShBinaryBp2Build(ctx android.TopDownMutatorContext) {
543 m, ok := ctx.Module().(*ShBinary)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500544 if !ok || !m.ConvertWithBp2build(ctx) {
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000545 return
546 }
547
Jingwen Chen07027912021-03-15 06:02:43 -0400548 srcs := bazel.MakeLabelListAttribute(
549 android.BazelLabelForModuleSrc(ctx, []string{*m.properties.Src}))
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000550
551 attrs := &bazelShBinaryAttributes{
552 Srcs: srcs,
553 }
554
Liz Kammerfc46bc12021-02-19 11:06:17 -0500555 props := bazel.BazelTargetModuleProperties{
556 Rule_class: "sh_binary",
557 }
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000558
Liz Kammerfc46bc12021-02-19 11:06:17 -0500559 ctx.CreateBazelTargetModule(BazelShBinaryFactory, m.Name(), props, attrs)
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000560}
561
562func (m *bazelShBinary) Name() string {
563 return m.BaseModuleName()
564}
565
566func (m *bazelShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
567
frankfengc5b87492020-06-03 10:28:47 -0700568var Bool = proptools.Bool