blob: bf97b88f83a07d505706e7e3d20f6b67f377e994 [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)
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +000045
46 android.RegisterBp2BuildMutator("sh_binary", ShBinaryBp2Build)
Dan Willemsenb0552672019-01-25 16:04:11 -080047}
48
Paul Duffin56fb8ee2021-03-08 15:05:52 +000049func registerShBuildComponents(ctx android.RegistrationContext) {
50 ctx.RegisterModuleType("sh_binary", ShBinaryFactory)
51 ctx.RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
52 ctx.RegisterModuleType("sh_test", ShTestFactory)
53 ctx.RegisterModuleType("sh_test_host", ShTestHostFactory)
54}
55
56// Test fixture preparer that will register most sh build components.
57//
58// Singletons and mutators should only be added here if they are needed for a majority of sh
59// module types, otherwise they should be added under a separate preparer to allow them to be
60// selected only when needed to reduce test execution time.
61//
62// Module types do not have much of an overhead unless they are used so this should include as many
63// module types as possible. The exceptions are those module types that require mutators and/or
64// singletons in order to function in which case they should be kept together in a separate
65// preparer.
66var PrepareForTestWithShBuildComponents = android.GroupFixturePreparers(
67 android.FixtureRegisterWithContext(registerShBuildComponents),
68)
69
Dan Willemsenb0552672019-01-25 16:04:11 -080070type shBinaryProperties struct {
71 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080072 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080073
74 // optional subdirectory under which this file is installed into
75 Sub_dir *string `android:"arch_variant"`
76
77 // optional name for the installed file. If unspecified, name of the module is used as the file name
78 Filename *string `android:"arch_variant"`
79
80 // when set to true, and filename property is not set, the name for the installed file
81 // is the same as the file name of the source file.
82 Filename_from_src *bool `android:"arch_variant"`
83
84 // Whether this module is directly installable to one of the partitions. Default: true.
85 Installable *bool
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -070086
87 // install symlinks to the binary
88 Symlinks []string `android:"arch_variant"`
Colin Crosscc83efb2020-08-21 14:25:33 -070089
90 // Make this module available when building for ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070091 // On device without a dedicated recovery partition, the module is only
92 // available after switching root into
93 // /first_stage_ramdisk. To expose the module before switching root, install
94 // the recovery variant instead.
Colin Crosscc83efb2020-08-21 14:25:33 -070095 Ramdisk_available *bool
96
Yifan Hong60e0cfb2020-10-21 15:17:56 -070097 // Make this module available when building for vendor ramdisk.
Yifan Hong39143a92020-10-26 12:43:12 -070098 // On device without a dedicated recovery partition, the module is only
99 // available after switching root into
100 // /first_stage_ramdisk. To expose the module before switching root, install
101 // the recovery variant instead.
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700102 Vendor_ramdisk_available *bool
103
Colin Crosscc83efb2020-08-21 14:25:33 -0700104 // Make this module available when building for recovery.
105 Recovery_available *bool
Dan Willemsenb0552672019-01-25 16:04:11 -0800106}
107
Dan Shib40deac2021-05-24 12:04:54 -0700108// Test option struct.
109type TestOptions struct {
110 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
111 Unit_test *bool
112}
113
Julien Desprez9e7fc142019-03-08 11:07:05 -0800114type TestProperties struct {
115 // list of compatibility suites (for example "cts", "vts") that the module should be
116 // installed into.
117 Test_suites []string `android:"arch_variant"`
118
119 // the name of the test configuration (for example "AndroidTest.xml") that should be
120 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -0700121 Test_config *string `android:"path,arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700122
123 // list of files or filegroup modules that provide data that should be installed alongside
124 // the test.
125 Data []string `android:"path,arch_variant"`
frankfengc5b87492020-06-03 10:28:47 -0700126
127 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
128 // with root permission.
129 Require_root *bool
130
131 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
132 // should be installed with the module.
133 Test_config_template *string `android:"path,arch_variant"`
134
135 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
136 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
137 // explicitly.
138 Auto_gen_config *bool
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700139
140 // list of binary modules that should be installed alongside the test
141 Data_bins []string `android:"path,arch_variant"`
142
143 // list of library modules that should be installed alongside the test
144 Data_libs []string `android:"path,arch_variant"`
145
146 // list of device binary modules that should be installed alongside the test.
147 // Only available for host sh_test modules.
148 Data_device_bins []string `android:"path,arch_variant"`
149
150 // list of device library modules that should be installed alongside the test.
151 // Only available for host sh_test modules.
152 Data_device_libs []string `android:"path,arch_variant"`
Dan Shib40deac2021-05-24 12:04:54 -0700153
154 // Test options.
155 Test_options TestOptions
Julien Desprez9e7fc142019-03-08 11:07:05 -0800156}
157
Dan Willemsenb0552672019-01-25 16:04:11 -0800158type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700159 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500160 android.BazelModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -0800161
162 properties shBinaryProperties
163
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700164 sourceFilePath android.Path
165 outputFilePath android.OutputPath
166 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800167}
168
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700169var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700170
Colin Cross7e07e202021-11-08 12:37:57 -0800171func (s *ShBinary) InstallBypassMake() bool {
172 return true
173}
174
Julien Desprez9e7fc142019-03-08 11:07:05 -0800175type ShTest struct {
176 ShBinary
177
178 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700179
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700180 installDir android.InstallPath
181
frankfengc5b87492020-06-03 10:28:47 -0700182 data android.Paths
183 testConfig android.Path
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700184
185 dataModules map[string]android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800186}
187
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700188func (s *ShBinary) HostToolPath() android.OptionalPath {
189 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700190}
191
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700192func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800193}
194
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700195func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800196 return s.outputFilePath
197}
198
199func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700200 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800201}
202
Rob Seymour925aa092021-08-10 20:42:03 +0000203func (s *ShBinary) RelativeInstallPath() string {
204 return s.SubDir()
205}
Dan Willemsenb0552672019-01-25 16:04:11 -0800206func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700207 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800208}
209
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700210func (s *ShBinary) Symlinks() []string {
211 return s.properties.Symlinks
212}
213
Colin Crosscc83efb2020-08-21 14:25:33 -0700214var _ android.ImageInterface = (*ShBinary)(nil)
215
216func (s *ShBinary) ImageMutatorBegin(ctx android.BaseModuleContext) {}
217
218func (s *ShBinary) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
219 return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk()
220}
221
222func (s *ShBinary) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
223 return proptools.Bool(s.properties.Ramdisk_available) || s.ModuleBase.InstallInRamdisk()
224}
225
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700226func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
227 return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.ModuleBase.InstallInVendorRamdisk()
228}
229
Inseob Kim08758f02021-04-08 21:13:22 +0900230func (s *ShBinary) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
231 return false
232}
233
Colin Crosscc83efb2020-08-21 14:25:33 -0700234func (s *ShBinary) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
235 return proptools.Bool(s.properties.Recovery_available) || s.ModuleBase.InstallInRecovery()
236}
237
238func (s *ShBinary) ExtraImageVariations(ctx android.BaseModuleContext) []string {
239 return nil
240}
241
242func (s *ShBinary) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
243}
244
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700245func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500246 if s.properties.Src == nil {
247 ctx.PropertyErrorf("src", "missing prebuilt source file")
248 }
249
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700250 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
251 filename := proptools.String(s.properties.Filename)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800252 filenameFromSrc := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800253 if filename == "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800254 if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800255 filename = s.sourceFilePath.Base()
256 } else {
257 filename = ctx.ModuleName()
258 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800259 } else if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800260 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
261 return
262 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700263 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800264
265 // This ensures that outputFilePath has the correct name for others to
266 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700267 ctx.Build(pctx, android.BuildParams{
268 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800269 Output: s.outputFilePath,
270 Input: s.sourceFilePath,
271 })
272}
273
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700274func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700275 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700276 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Colin Cross7c7c1142019-07-29 16:46:49 -0700277 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
278}
279
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700280func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
281 return []android.AndroidMkEntries{android.AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800282 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700283 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Dan Willemsenb0552672019-01-25 16:04:11 -0800284 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700285 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700286 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700287 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700288 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700289 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800290 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900291 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800292}
293
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700294func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700295 entries.SetString("LOCAL_MODULE_SUFFIX", "")
296 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700297 if len(s.properties.Symlinks) > 0 {
298 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
299 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700300}
301
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700302type dependencyTag struct {
303 blueprint.BaseDependencyTag
304 name string
305}
306
307var (
308 shTestDataBinsTag = dependencyTag{name: "dataBins"}
309 shTestDataLibsTag = dependencyTag{name: "dataLibs"}
310 shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
311 shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"}
312)
313
314var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}}
315
316func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) {
317 s.ShBinary.DepsMutator(ctx)
318
319 ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...)
320 ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...),
321 shTestDataLibsTag, s.testProperties.Data_libs...)
Liz Kammer356f7d42021-01-26 09:18:53 -0500322 if (ctx.Target().Os.Class == android.Host || ctx.BazelConversionMode()) && len(ctx.Config().Targets[android.Android]) > 0 {
Jaewoong Jung642916f2020-10-09 17:25:15 -0700323 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700324 ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...)
325 ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...),
326 shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...)
327 } else if ctx.Target().Os.Class != android.Host {
328 if len(s.testProperties.Data_device_bins) > 0 {
329 ctx.PropertyErrorf("data_device_bins", "only available for host modules")
330 }
331 if len(s.testProperties.Data_device_libs) > 0 {
332 ctx.PropertyErrorf("data_device_libs", "only available for host modules")
333 }
334 }
335}
336
337func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) {
338 if _, exists := s.dataModules[relPath]; exists {
339 ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s",
340 relPath, s.dataModules[relPath].String(), path.String())
341 return
342 }
343 s.dataModules[relPath] = path
344}
345
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700346func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700347 s.ShBinary.generateAndroidBuildActions(ctx)
348 testDir := "nativetest"
349 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
350 testDir = "nativetest64"
351 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700352 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700353 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
354 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
355 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
356 }
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700357 if s.SubDir() != "" {
358 // Don't add the module name to the installation path if sub_dir is specified for backward
359 // compatibility.
360 s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir())
361 } else {
362 s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name())
363 }
364 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700365
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700366 s.data = android.PathsForModuleSrc(ctx, s.testProperties.Data)
frankfengc5b87492020-06-03 10:28:47 -0700367
368 var configs []tradefed.Config
369 if Bool(s.testProperties.Require_root) {
370 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
371 } else {
372 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
373 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
374 }
frankfengbe6ae772020-09-28 13:22:57 -0700375 if len(s.testProperties.Data_device_bins) > 0 {
376 moduleName := s.Name()
377 remoteDir := "/data/local/tests/unrestricted/" + moduleName + "/"
378 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
379 for _, bin := range s.testProperties.Data_device_bins {
380 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: remoteDir + bin})
381 }
382 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options})
383 }
frankfengc5b87492020-06-03 10:28:47 -0700384 s.testConfig = tradefed.AutoGenShellTestConfig(ctx, s.testProperties.Test_config,
385 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 -0700386
387 s.dataModules = make(map[string]android.Path)
388 ctx.VisitDirectDeps(func(dep android.Module) {
389 depTag := ctx.OtherModuleDependencyTag(dep)
390 switch depTag {
391 case shTestDataBinsTag, shTestDataDeviceBinsTag:
Anton Hansson2f6422c2020-12-31 13:42:10 +0000392 path := android.OutputFileForModule(ctx, dep, "")
393 s.addToDataModules(ctx, path.Base(), path)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700394 case shTestDataLibsTag, shTestDataDeviceLibsTag:
395 if cc, isCc := dep.(*cc.Module); isCc {
396 // Copy to an intermediate output directory to append "lib[64]" to the path,
397 // so that it's compatible with the default rpath values.
398 var relPath string
399 if cc.Arch().ArchType.Multilib == "lib64" {
400 relPath = filepath.Join("lib64", cc.OutputFile().Path().Base())
401 } else {
402 relPath = filepath.Join("lib", cc.OutputFile().Path().Base())
403 }
404 if _, exist := s.dataModules[relPath]; exist {
405 return
406 }
407 relocatedLib := android.PathForModuleOut(ctx, "relocated", relPath)
408 ctx.Build(pctx, android.BuildParams{
409 Rule: android.Cp,
410 Input: cc.OutputFile().Path(),
411 Output: relocatedLib,
412 })
413 s.addToDataModules(ctx, relPath, relocatedLib)
414 return
415 }
416 property := "data_libs"
417 if depTag == shTestDataDeviceBinsTag {
418 property = "data_device_libs"
419 }
420 ctx.PropertyErrorf(property, "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
421 }
422 })
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700423}
424
Colin Cross7c7c1142019-07-29 16:46:49 -0700425func (s *ShTest) InstallInData() bool {
426 return true
427}
428
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700429func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
430 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700431 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700432 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700433 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700434 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700435 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700436 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700437 entries.SetPath("LOCAL_MODULE_PATH", s.installDir.ToMakePath())
Liz Kammer57f5b332020-11-24 12:42:58 -0800438 entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700439 if s.testConfig != nil {
440 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700441 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700442 for _, d := range s.data {
443 rel := d.Rel()
444 path := d.String()
445 if !strings.HasSuffix(path, rel) {
446 panic(fmt.Errorf("path %q does not end with %q", path, rel))
447 }
448 path = strings.TrimSuffix(path, rel)
449 entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700450 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700451 relPaths := make([]string, 0)
452 for relPath, _ := range s.dataModules {
453 relPaths = append(relPaths, relPath)
454 }
455 sort.Strings(relPaths)
456 for _, relPath := range relPaths {
457 dir := strings.TrimSuffix(s.dataModules[relPath].String(), relPath)
458 entries.AddStrings("LOCAL_TEST_DATA", dir+":"+relPath)
459 }
Dan Shib40deac2021-05-24 12:04:54 -0700460 if Bool(s.testProperties.Test_options.Unit_test) {
461 entries.SetBool("LOCAL_IS_UNIT_TEST", true)
462 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700463 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700464 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900465 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800466}
467
Dan Willemsenb0552672019-01-25 16:04:11 -0800468func InitShBinaryModule(s *ShBinary) {
469 s.AddProperties(&s.properties)
Liz Kammerea6666f2021-02-17 10:17:28 -0500470 android.InitBazelModule(s)
Dan Willemsenb0552672019-01-25 16:04:11 -0800471}
472
Patrice Arrudae1034192019-03-11 13:20:17 -0700473// sh_binary is for a shell script or batch file to be installed as an
474// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700475func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800476 module := &ShBinary{}
477 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700478 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800479 return module
480}
481
Patrice Arrudae1034192019-03-11 13:20:17 -0700482// sh_binary_host is for a shell script to be installed as an executable binary
483// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700484func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800485 module := &ShBinary{}
486 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700487 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800488 return module
489}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800490
Jaewoong Jung61a83682019-07-01 09:08:50 -0700491// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700492func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800493 module := &ShTest{}
494 InitShBinaryModule(&module.ShBinary)
495 module.AddProperties(&module.testProperties)
496
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700497 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800498 return module
499}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700500
501// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700502func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700503 module := &ShTest{}
504 InitShBinaryModule(&module.ShBinary)
505 module.AddProperties(&module.testProperties)
Julien Desprez06f13992021-06-28 13:41:43 -0700506 // Default sh_test_host to unit_tests = true
507 if module.testProperties.Test_options.Unit_test == nil {
508 module.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
509 }
Jaewoong Jung61a83682019-07-01 09:08:50 -0700510
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700511 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700512 return module
513}
frankfengc5b87492020-06-03 10:28:47 -0700514
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000515type bazelShBinaryAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400516 Srcs bazel.LabelListAttribute
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000517 // Bazel also supports the attributes below, but (so far) these are not required for Bionic
518 // deps
519 // data
520 // args
521 // compatible_with
522 // deprecation
523 // distribs
524 // env
525 // exec_compatible_with
526 // exec_properties
527 // features
528 // licenses
529 // output_licenses
530 // restricted_to
531 // tags
532 // target_compatible_with
533 // testonly
534 // toolchains
535 // visibility
536}
537
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000538func ShBinaryBp2Build(ctx android.TopDownMutatorContext) {
539 m, ok := ctx.Module().(*ShBinary)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500540 if !ok || !m.ConvertWithBp2build(ctx) {
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000541 return
542 }
543
Jingwen Chen07027912021-03-15 06:02:43 -0400544 srcs := bazel.MakeLabelListAttribute(
545 android.BazelLabelForModuleSrc(ctx, []string{*m.properties.Src}))
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000546
547 attrs := &bazelShBinaryAttributes{
548 Srcs: srcs,
549 }
550
Liz Kammerfc46bc12021-02-19 11:06:17 -0500551 props := bazel.BazelTargetModuleProperties{
552 Rule_class: "sh_binary",
553 }
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000554
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000555 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000556}
557
frankfengc5b87492020-06-03 10:28:47 -0700558var Bool = proptools.Bool
Rob Seymour925aa092021-08-10 20:42:03 +0000559
560var _ snapshot.RelativeInstallPath = (*ShBinary)(nil)