blob: 4de01443df507bd414926a6c3467b4eeb0fd110d [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
Dan Shib40deac2021-05-24 12:04:54 -0700106// Test option struct.
107type TestOptions struct {
108 // If the test is a hostside(no device required) unittest that shall be run during presubmit check.
109 Unit_test *bool
110}
111
Julien Desprez9e7fc142019-03-08 11:07:05 -0800112type TestProperties struct {
113 // list of compatibility suites (for example "cts", "vts") that the module should be
114 // installed into.
115 Test_suites []string `android:"arch_variant"`
116
117 // the name of the test configuration (for example "AndroidTest.xml") that should be
118 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -0700119 Test_config *string `android:"path,arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700120
121 // list of files or filegroup modules that provide data that should be installed alongside
122 // the test.
123 Data []string `android:"path,arch_variant"`
frankfengc5b87492020-06-03 10:28:47 -0700124
125 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
126 // with root permission.
127 Require_root *bool
128
129 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
130 // should be installed with the module.
131 Test_config_template *string `android:"path,arch_variant"`
132
133 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
134 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
135 // explicitly.
136 Auto_gen_config *bool
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700137
138 // list of binary modules that should be installed alongside the test
139 Data_bins []string `android:"path,arch_variant"`
140
141 // list of library modules that should be installed alongside the test
142 Data_libs []string `android:"path,arch_variant"`
143
144 // list of device binary modules that should be installed alongside the test.
145 // Only available for host sh_test modules.
146 Data_device_bins []string `android:"path,arch_variant"`
147
148 // list of device library modules that should be installed alongside the test.
149 // Only available for host sh_test modules.
150 Data_device_libs []string `android:"path,arch_variant"`
Dan Shib40deac2021-05-24 12:04:54 -0700151
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900152 // Install the test into a folder named for the module in all test suites.
153 Per_testcase_directory *bool
154
Dan Shib40deac2021-05-24 12:04:54 -0700155 // Test options.
156 Test_options TestOptions
Julien Desprez9e7fc142019-03-08 11:07:05 -0800157}
158
Dan Willemsenb0552672019-01-25 16:04:11 -0800159type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700160 android.ModuleBase
Liz Kammerea6666f2021-02-17 10:17:28 -0500161 android.BazelModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -0800162
163 properties shBinaryProperties
164
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700165 sourceFilePath android.Path
166 outputFilePath android.OutputPath
167 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800168}
169
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700170var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700171
Julien Desprez9e7fc142019-03-08 11:07:05 -0800172type ShTest struct {
173 ShBinary
174
175 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700176
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700177 installDir android.InstallPath
178
frankfengc5b87492020-06-03 10:28:47 -0700179 data android.Paths
180 testConfig android.Path
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700181
182 dataModules map[string]android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800183}
184
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700185func (s *ShBinary) HostToolPath() android.OptionalPath {
186 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700187}
188
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700189func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800190}
191
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700192func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800193 return s.outputFilePath
194}
195
196func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700197 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800198}
199
Rob Seymour925aa092021-08-10 20:42:03 +0000200func (s *ShBinary) RelativeInstallPath() string {
201 return s.SubDir()
202}
Dan Willemsenb0552672019-01-25 16:04:11 -0800203func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700204 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800205}
206
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700207func (s *ShBinary) Symlinks() []string {
208 return s.properties.Symlinks
209}
210
Colin Crosscc83efb2020-08-21 14:25:33 -0700211var _ android.ImageInterface = (*ShBinary)(nil)
212
213func (s *ShBinary) ImageMutatorBegin(ctx android.BaseModuleContext) {}
214
215func (s *ShBinary) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
216 return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk()
217}
218
219func (s *ShBinary) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
220 return proptools.Bool(s.properties.Ramdisk_available) || s.ModuleBase.InstallInRamdisk()
221}
222
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700223func (s *ShBinary) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
224 return proptools.Bool(s.properties.Vendor_ramdisk_available) || s.ModuleBase.InstallInVendorRamdisk()
225}
226
Inseob Kim08758f02021-04-08 21:13:22 +0900227func (s *ShBinary) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
228 return false
229}
230
Colin Crosscc83efb2020-08-21 14:25:33 -0700231func (s *ShBinary) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
232 return proptools.Bool(s.properties.Recovery_available) || s.ModuleBase.InstallInRecovery()
233}
234
235func (s *ShBinary) ExtraImageVariations(ctx android.BaseModuleContext) []string {
236 return nil
237}
238
239func (s *ShBinary) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
240}
241
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700242func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
Liz Kammer356f7d42021-01-26 09:18:53 -0500243 if s.properties.Src == nil {
244 ctx.PropertyErrorf("src", "missing prebuilt source file")
245 }
246
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700247 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
248 filename := proptools.String(s.properties.Filename)
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800249 filenameFromSrc := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800250 if filename == "" {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800251 if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800252 filename = s.sourceFilePath.Base()
253 } else {
254 filename = ctx.ModuleName()
255 }
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800256 } else if filenameFromSrc {
Dan Willemsenb0552672019-01-25 16:04:11 -0800257 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
258 return
259 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700260 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800261
262 // This ensures that outputFilePath has the correct name for others to
263 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700264 ctx.Build(pctx, android.BuildParams{
265 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800266 Output: s.outputFilePath,
267 Input: s.sourceFilePath,
268 })
269}
270
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700271func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700272 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700273 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000274 if !s.Installable() {
275 s.SkipInstall()
276 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700277 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
Colin Cross94bf5182021-11-09 17:21:14 -0800278 for _, symlink := range s.Symlinks() {
279 ctx.InstallSymlink(installDir, symlink, s.installedFile)
280 }
Colin Cross7c7c1142019-07-29 16:46:49 -0700281}
282
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700283func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
284 return []android.AndroidMkEntries{android.AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800285 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700286 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500287 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700288 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700289 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700290 s.customAndroidMkEntries(entries)
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700291 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Sundong Ahna5c5b9c2022-01-12 03:29:17 +0000292 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !s.Installable())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700293 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800294 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900295 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800296}
297
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700298func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700299 entries.SetString("LOCAL_MODULE_SUFFIX", "")
300 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700301 if len(s.properties.Symlinks) > 0 {
302 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
303 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700304}
305
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700306type dependencyTag struct {
307 blueprint.BaseDependencyTag
308 name string
309}
310
311var (
312 shTestDataBinsTag = dependencyTag{name: "dataBins"}
313 shTestDataLibsTag = dependencyTag{name: "dataLibs"}
314 shTestDataDeviceBinsTag = dependencyTag{name: "dataDeviceBins"}
315 shTestDataDeviceLibsTag = dependencyTag{name: "dataDeviceLibs"}
316)
317
318var sharedLibVariations = []blueprint.Variation{{Mutator: "link", Variation: "shared"}}
319
320func (s *ShTest) DepsMutator(ctx android.BottomUpMutatorContext) {
321 s.ShBinary.DepsMutator(ctx)
322
323 ctx.AddFarVariationDependencies(ctx.Target().Variations(), shTestDataBinsTag, s.testProperties.Data_bins...)
324 ctx.AddFarVariationDependencies(append(ctx.Target().Variations(), sharedLibVariations...),
325 shTestDataLibsTag, s.testProperties.Data_libs...)
Liz Kammer3bf97bd2022-04-26 09:38:20 -0400326 if ctx.Target().Os.Class == android.Host && len(ctx.Config().Targets[android.Android]) > 0 {
Jaewoong Jung642916f2020-10-09 17:25:15 -0700327 deviceVariations := ctx.Config().AndroidFirstDeviceTarget.Variations()
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700328 ctx.AddFarVariationDependencies(deviceVariations, shTestDataDeviceBinsTag, s.testProperties.Data_device_bins...)
329 ctx.AddFarVariationDependencies(append(deviceVariations, sharedLibVariations...),
330 shTestDataDeviceLibsTag, s.testProperties.Data_device_libs...)
331 } else if ctx.Target().Os.Class != android.Host {
332 if len(s.testProperties.Data_device_bins) > 0 {
333 ctx.PropertyErrorf("data_device_bins", "only available for host modules")
334 }
335 if len(s.testProperties.Data_device_libs) > 0 {
336 ctx.PropertyErrorf("data_device_libs", "only available for host modules")
337 }
338 }
339}
340
341func (s *ShTest) addToDataModules(ctx android.ModuleContext, relPath string, path android.Path) {
342 if _, exists := s.dataModules[relPath]; exists {
343 ctx.ModuleErrorf("data modules have a conflicting installation path, %v - %s, %s",
344 relPath, s.dataModules[relPath].String(), path.String())
345 return
346 }
347 s.dataModules[relPath] = path
348}
349
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700350func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700351 s.ShBinary.generateAndroidBuildActions(ctx)
352 testDir := "nativetest"
353 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
354 testDir = "nativetest64"
355 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700356 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700357 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
358 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
359 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
360 }
Jaewoong Jung4aedc862020-06-10 17:23:46 -0700361 if s.SubDir() != "" {
362 // Don't add the module name to the installation path if sub_dir is specified for backward
363 // compatibility.
364 s.installDir = android.PathForModuleInstall(ctx, testDir, s.SubDir())
365 } else {
366 s.installDir = android.PathForModuleInstall(ctx, testDir, s.Name())
367 }
368 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700369
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700370 s.data = android.PathsForModuleSrc(ctx, s.testProperties.Data)
frankfengc5b87492020-06-03 10:28:47 -0700371
372 var configs []tradefed.Config
373 if Bool(s.testProperties.Require_root) {
374 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
375 } else {
376 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
377 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
378 }
frankfengbe6ae772020-09-28 13:22:57 -0700379 if len(s.testProperties.Data_device_bins) > 0 {
380 moduleName := s.Name()
381 remoteDir := "/data/local/tests/unrestricted/" + moduleName + "/"
382 options := []tradefed.Option{{Name: "cleanup", Value: "true"}}
383 for _, bin := range s.testProperties.Data_device_bins {
384 options = append(options, tradefed.Option{Name: "push-file", Key: bin, Value: remoteDir + bin})
385 }
386 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.PushFilePreparer", options})
387 }
frankfengc5b87492020-06-03 10:28:47 -0700388 s.testConfig = tradefed.AutoGenShellTestConfig(ctx, s.testProperties.Test_config,
389 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 -0700390
391 s.dataModules = make(map[string]android.Path)
392 ctx.VisitDirectDeps(func(dep android.Module) {
393 depTag := ctx.OtherModuleDependencyTag(dep)
394 switch depTag {
395 case shTestDataBinsTag, shTestDataDeviceBinsTag:
Anton Hansson2f6422c2020-12-31 13:42:10 +0000396 path := android.OutputFileForModule(ctx, dep, "")
397 s.addToDataModules(ctx, path.Base(), path)
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700398 case shTestDataLibsTag, shTestDataDeviceLibsTag:
399 if cc, isCc := dep.(*cc.Module); isCc {
400 // Copy to an intermediate output directory to append "lib[64]" to the path,
401 // so that it's compatible with the default rpath values.
402 var relPath string
403 if cc.Arch().ArchType.Multilib == "lib64" {
404 relPath = filepath.Join("lib64", cc.OutputFile().Path().Base())
405 } else {
406 relPath = filepath.Join("lib", cc.OutputFile().Path().Base())
407 }
408 if _, exist := s.dataModules[relPath]; exist {
409 return
410 }
411 relocatedLib := android.PathForModuleOut(ctx, "relocated", relPath)
412 ctx.Build(pctx, android.BuildParams{
413 Rule: android.Cp,
414 Input: cc.OutputFile().Path(),
415 Output: relocatedLib,
416 })
417 s.addToDataModules(ctx, relPath, relocatedLib)
418 return
419 }
420 property := "data_libs"
421 if depTag == shTestDataDeviceBinsTag {
422 property = "data_device_libs"
423 }
424 ctx.PropertyErrorf(property, "%q of type %q is not supported", dep.Name(), ctx.OtherModuleType(dep))
425 }
426 })
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700427}
428
Colin Cross7c7c1142019-07-29 16:46:49 -0700429func (s *ShTest) InstallInData() bool {
430 return true
431}
432
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700433func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
434 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700435 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700436 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Ivan Lozanod06cc742021-11-12 13:27:58 -0500437 Include: "$(BUILD_SYSTEM)/soong_cc_rust_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700438 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700439 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700440 s.customAndroidMkEntries(entries)
Colin Crossc68db4b2021-11-11 18:59:15 -0800441 entries.SetPath("LOCAL_MODULE_PATH", s.installDir)
Liz Kammer57f5b332020-11-24 12:42:58 -0800442 entries.AddCompatibilityTestSuites(s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700443 if s.testConfig != nil {
444 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700445 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700446 for _, d := range s.data {
447 rel := d.Rel()
448 path := d.String()
449 if !strings.HasSuffix(path, rel) {
450 panic(fmt.Errorf("path %q does not end with %q", path, rel))
451 }
452 path = strings.TrimSuffix(path, rel)
453 entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700454 }
Jaewoong Jung6e0eee52020-05-29 16:15:32 -0700455 relPaths := make([]string, 0)
456 for relPath, _ := range s.dataModules {
457 relPaths = append(relPaths, relPath)
458 }
459 sort.Strings(relPaths)
460 for _, relPath := range relPaths {
461 dir := strings.TrimSuffix(s.dataModules[relPath].String(), relPath)
462 entries.AddStrings("LOCAL_TEST_DATA", dir+":"+relPath)
463 }
yangbill22bafec2022-02-11 18:06:07 +0800464 if s.testProperties.Data_bins != nil {
465 entries.AddStrings("LOCAL_TEST_DATA_BINS", s.testProperties.Data_bins...)
466 }
Dan Shib40deac2021-05-24 12:04:54 -0700467 if Bool(s.testProperties.Test_options.Unit_test) {
468 entries.SetBool("LOCAL_IS_UNIT_TEST", true)
469 }
Jooyung Han3ae4cca2022-02-22 16:33:24 +0900470 entries.SetBoolIfTrue("LOCAL_COMPATIBILITY_PER_TESTCASE_DIRECTORY", Bool(s.testProperties.Per_testcase_directory))
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700471 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700472 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900473 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800474}
475
Dan Willemsenb0552672019-01-25 16:04:11 -0800476func InitShBinaryModule(s *ShBinary) {
477 s.AddProperties(&s.properties)
Liz Kammerea6666f2021-02-17 10:17:28 -0500478 android.InitBazelModule(s)
Dan Willemsenb0552672019-01-25 16:04:11 -0800479}
480
Patrice Arrudae1034192019-03-11 13:20:17 -0700481// sh_binary is for a shell script or batch file to be installed as an
482// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700483func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800484 module := &ShBinary{}
485 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700486 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800487 return module
488}
489
Patrice Arrudae1034192019-03-11 13:20:17 -0700490// sh_binary_host is for a shell script to be installed as an executable binary
491// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700492func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800493 module := &ShBinary{}
494 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700495 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800496 return module
497}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800498
Jaewoong Jung61a83682019-07-01 09:08:50 -0700499// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700500func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800501 module := &ShTest{}
502 InitShBinaryModule(&module.ShBinary)
503 module.AddProperties(&module.testProperties)
504
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700505 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800506 return module
507}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700508
509// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700510func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700511 module := &ShTest{}
512 InitShBinaryModule(&module.ShBinary)
513 module.AddProperties(&module.testProperties)
Julien Desprez06f13992021-06-28 13:41:43 -0700514 // Default sh_test_host to unit_tests = true
515 if module.testProperties.Test_options.Unit_test == nil {
516 module.testProperties.Test_options.Unit_test = proptools.BoolPtr(true)
517 }
Jaewoong Jung61a83682019-07-01 09:08:50 -0700518
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700519 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700520 return module
521}
frankfengc5b87492020-06-03 10:28:47 -0700522
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000523type bazelShBinaryAttributes struct {
Yu Liub3024552021-11-23 14:53:41 -0800524 Srcs bazel.LabelListAttribute
Liz Kammer48c6ead2021-12-21 15:36:11 -0500525 Filename *string
526 Sub_dir *string
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000527 // Bazel also supports the attributes below, but (so far) these are not required for Bionic
528 // deps
529 // data
530 // args
531 // compatible_with
532 // deprecation
533 // distribs
534 // env
535 // exec_compatible_with
536 // exec_properties
537 // features
538 // licenses
539 // output_licenses
540 // restricted_to
541 // tags
542 // target_compatible_with
543 // testonly
544 // toolchains
545 // visibility
546}
547
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400548func (m *ShBinary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Jingwen Chen07027912021-03-15 06:02:43 -0400549 srcs := bazel.MakeLabelListAttribute(
550 android.BazelLabelForModuleSrc(ctx, []string{*m.properties.Src}))
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000551
Liz Kammer48c6ead2021-12-21 15:36:11 -0500552 var filename *string
Yu Liub3024552021-11-23 14:53:41 -0800553 if m.properties.Filename != nil {
Liz Kammer48c6ead2021-12-21 15:36:11 -0500554 filename = m.properties.Filename
Yu Liub3024552021-11-23 14:53:41 -0800555 }
556
Liz Kammer48c6ead2021-12-21 15:36:11 -0500557 var subDir *string
Yu Liub3024552021-11-23 14:53:41 -0800558 if m.properties.Sub_dir != nil {
Liz Kammer48c6ead2021-12-21 15:36:11 -0500559 subDir = m.properties.Sub_dir
Yu Liub3024552021-11-23 14:53:41 -0800560 }
561
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000562 attrs := &bazelShBinaryAttributes{
Yu Liub3024552021-11-23 14:53:41 -0800563 Srcs: srcs,
564 Filename: filename,
565 Sub_dir: subDir,
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000566 }
567
Liz Kammerfc46bc12021-02-19 11:06:17 -0500568 props := bazel.BazelTargetModuleProperties{
Yu Liub3024552021-11-23 14:53:41 -0800569 Rule_class: "sh_binary",
570 Bzl_load_location: "//build/bazel/rules:sh_binary.bzl",
Liz Kammerfc46bc12021-02-19 11:06:17 -0500571 }
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000572
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000573 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Rupert Shuttlewortha1a56e82021-02-09 10:59:06 +0000574}
575
frankfengc5b87492020-06-03 10:28:47 -0700576var Bool = proptools.Bool
Rob Seymour925aa092021-08-10 20:42:03 +0000577
578var _ snapshot.RelativeInstallPath = (*ShBinary)(nil)