blob: ab0490ac14c785bcf829739926a092250cf802dc [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"
Julien Desprez9e7fc142019-03-08 11:07:05 -080020 "strings"
Jaewoong Jung4b79e982020-06-01 10:45:49 -070021
22 "github.com/google/blueprint/proptools"
23
24 "android/soong/android"
frankfengc5b87492020-06-03 10:28:47 -070025 "android/soong/tradefed"
Dan Willemsenb0552672019-01-25 16:04:11 -080026)
27
28// sh_binary is for shell scripts (and batch files) that are installed as
29// executable files into .../bin/
30//
31// Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary
32// instead.
33
Jaewoong Jung4b79e982020-06-01 10:45:49 -070034var pctx = android.NewPackageContext("android/soong/sh")
35
Dan Willemsenb0552672019-01-25 16:04:11 -080036func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070037 pctx.Import("android/soong/android")
38
39 android.RegisterModuleType("sh_binary", ShBinaryFactory)
40 android.RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
41 android.RegisterModuleType("sh_test", ShTestFactory)
42 android.RegisterModuleType("sh_test_host", ShTestHostFactory)
Dan Willemsenb0552672019-01-25 16:04:11 -080043}
44
45type shBinaryProperties struct {
46 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080047 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080048
49 // optional subdirectory under which this file is installed into
50 Sub_dir *string `android:"arch_variant"`
51
52 // optional name for the installed file. If unspecified, name of the module is used as the file name
53 Filename *string `android:"arch_variant"`
54
55 // when set to true, and filename property is not set, the name for the installed file
56 // is the same as the file name of the source file.
57 Filename_from_src *bool `android:"arch_variant"`
58
59 // Whether this module is directly installable to one of the partitions. Default: true.
60 Installable *bool
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -070061
62 // install symlinks to the binary
63 Symlinks []string `android:"arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080064}
65
Julien Desprez9e7fc142019-03-08 11:07:05 -080066type TestProperties struct {
67 // list of compatibility suites (for example "cts", "vts") that the module should be
68 // installed into.
69 Test_suites []string `android:"arch_variant"`
70
71 // the name of the test configuration (for example "AndroidTest.xml") that should be
72 // installed with the module.
Colin Crossa6384822020-06-09 15:09:22 -070073 Test_config *string `android:"path,arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070074
75 // list of files or filegroup modules that provide data that should be installed alongside
76 // the test.
77 Data []string `android:"path,arch_variant"`
frankfengc5b87492020-06-03 10:28:47 -070078
79 // Add RootTargetPreparer to auto generated test config. This guarantees the test to run
80 // with root permission.
81 Require_root *bool
82
83 // the name of the test configuration template (for example "AndroidTestTemplate.xml") that
84 // should be installed with the module.
85 Test_config_template *string `android:"path,arch_variant"`
86
87 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
88 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
89 // explicitly.
90 Auto_gen_config *bool
Julien Desprez9e7fc142019-03-08 11:07:05 -080091}
92
Dan Willemsenb0552672019-01-25 16:04:11 -080093type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070094 android.ModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -080095
96 properties shBinaryProperties
97
Jaewoong Jung4b79e982020-06-01 10:45:49 -070098 sourceFilePath android.Path
99 outputFilePath android.OutputPath
100 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800101}
102
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700103var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -0700104
Julien Desprez9e7fc142019-03-08 11:07:05 -0800105type ShTest struct {
106 ShBinary
107
108 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700109
frankfengc5b87492020-06-03 10:28:47 -0700110 data android.Paths
111 testConfig android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800112}
113
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700114func (s *ShBinary) HostToolPath() android.OptionalPath {
115 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700116}
117
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700118func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800119 if s.properties.Src == nil {
120 ctx.PropertyErrorf("src", "missing prebuilt source file")
121 }
Dan Willemsenb0552672019-01-25 16:04:11 -0800122}
123
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700124func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800125 return s.outputFilePath
126}
127
128func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700129 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800130}
131
132func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700133 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800134}
135
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700136func (s *ShBinary) Symlinks() []string {
137 return s.properties.Symlinks
138}
139
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700140func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
141 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
142 filename := proptools.String(s.properties.Filename)
143 filename_from_src := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800144 if filename == "" {
145 if filename_from_src {
146 filename = s.sourceFilePath.Base()
147 } else {
148 filename = ctx.ModuleName()
149 }
150 } else if filename_from_src {
151 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
152 return
153 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700154 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800155
156 // This ensures that outputFilePath has the correct name for others to
157 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700158 ctx.Build(pctx, android.BuildParams{
159 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800160 Output: s.outputFilePath,
161 Input: s.sourceFilePath,
162 })
163}
164
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700165func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700166 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700167 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Colin Cross7c7c1142019-07-29 16:46:49 -0700168 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
169}
170
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700171func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
172 return []android.AndroidMkEntries{android.AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800173 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700174 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Dan Willemsenb0552672019-01-25 16:04:11 -0800175 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700176 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
177 func(entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700178 s.customAndroidMkEntries(entries)
179 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800180 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900181 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800182}
183
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700184func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
185 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700186 entries.SetString("LOCAL_MODULE_SUFFIX", "")
187 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700188 if len(s.properties.Symlinks) > 0 {
189 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
190 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700191}
192
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700193func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700194 s.ShBinary.generateAndroidBuildActions(ctx)
195 testDir := "nativetest"
196 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
197 testDir = "nativetest64"
198 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700199 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700200 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
201 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
202 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
203 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700204 installDir := android.PathForModuleInstall(ctx, testDir, proptools.String(s.properties.Sub_dir))
Colin Cross7c7c1142019-07-29 16:46:49 -0700205 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700206
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700207 s.data = android.PathsForModuleSrc(ctx, s.testProperties.Data)
frankfengc5b87492020-06-03 10:28:47 -0700208
209 var configs []tradefed.Config
210 if Bool(s.testProperties.Require_root) {
211 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
212 } else {
213 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
214 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
215 }
216 s.testConfig = tradefed.AutoGenShellTestConfig(ctx, s.testProperties.Test_config,
217 s.testProperties.Test_config_template, s.testProperties.Test_suites, configs, s.testProperties.Auto_gen_config, s.outputFilePath.Base())
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700218}
219
Colin Cross7c7c1142019-07-29 16:46:49 -0700220func (s *ShTest) InstallInData() bool {
221 return true
222}
223
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700224func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
225 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700226 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700227 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700228 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700229 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
230 func(entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700231 s.customAndroidMkEntries(entries)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700232
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700233 entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", s.testProperties.Test_suites...)
Colin Crossa6384822020-06-09 15:09:22 -0700234 if s.testConfig != nil {
235 entries.SetPath("LOCAL_FULL_TEST_CONFIG", s.testConfig)
frankfengc5b87492020-06-03 10:28:47 -0700236 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700237 for _, d := range s.data {
238 rel := d.Rel()
239 path := d.String()
240 if !strings.HasSuffix(path, rel) {
241 panic(fmt.Errorf("path %q does not end with %q", path, rel))
242 }
243 path = strings.TrimSuffix(path, rel)
244 entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700245 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700246 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700247 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900248 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800249}
250
Dan Willemsenb0552672019-01-25 16:04:11 -0800251func InitShBinaryModule(s *ShBinary) {
252 s.AddProperties(&s.properties)
253}
254
Patrice Arrudae1034192019-03-11 13:20:17 -0700255// sh_binary is for a shell script or batch file to be installed as an
256// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700257func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800258 module := &ShBinary{}
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700259 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
260 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
Jiyong Park6ac3cac2019-11-19 12:57:57 +0900261 })
Dan Willemsenb0552672019-01-25 16:04:11 -0800262 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700263 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800264 return module
265}
266
Patrice Arrudae1034192019-03-11 13:20:17 -0700267// sh_binary_host is for a shell script to be installed as an executable binary
268// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700269func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800270 module := &ShBinary{}
271 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700272 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800273 return module
274}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800275
Jaewoong Jung61a83682019-07-01 09:08:50 -0700276// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700277func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800278 module := &ShTest{}
279 InitShBinaryModule(&module.ShBinary)
280 module.AddProperties(&module.testProperties)
281
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700282 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800283 return module
284}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700285
286// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700287func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700288 module := &ShTest{}
289 InitShBinaryModule(&module.ShBinary)
290 module.AddProperties(&module.testProperties)
291
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700292 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700293 return module
294}
frankfengc5b87492020-06-03 10:28:47 -0700295
296var Bool = proptools.Bool