blob: 37e825309214fe994d479787f9e9f4a2f39b6ee1 [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.
73 Test_config *string `android:"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
Jaewoong Jung2d4f1a22020-06-10 17:23:46 -0700110 installDir android.InstallPath
111
frankfengc5b87492020-06-03 10:28:47 -0700112 data android.Paths
113 testConfig android.Path
Julien Desprez9e7fc142019-03-08 11:07:05 -0800114}
115
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700116func (s *ShBinary) HostToolPath() android.OptionalPath {
117 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700118}
119
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700120func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800121 if s.properties.Src == nil {
122 ctx.PropertyErrorf("src", "missing prebuilt source file")
123 }
Dan Willemsenb0552672019-01-25 16:04:11 -0800124}
125
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700126func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800127 return s.outputFilePath
128}
129
130func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700131 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800132}
133
134func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700135 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800136}
137
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700138func (s *ShBinary) Symlinks() []string {
139 return s.properties.Symlinks
140}
141
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700142func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
143 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
144 filename := proptools.String(s.properties.Filename)
145 filename_from_src := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800146 if filename == "" {
147 if filename_from_src {
148 filename = s.sourceFilePath.Base()
149 } else {
150 filename = ctx.ModuleName()
151 }
152 } else if filename_from_src {
153 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
154 return
155 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700156 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800157
158 // This ensures that outputFilePath has the correct name for others to
159 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700160 ctx.Build(pctx, android.BuildParams{
161 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800162 Output: s.outputFilePath,
163 Input: s.sourceFilePath,
164 })
165}
166
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700167func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700168 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700169 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Colin Cross7c7c1142019-07-29 16:46:49 -0700170 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
171}
172
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700173func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
174 return []android.AndroidMkEntries{android.AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800175 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700176 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Dan Willemsenb0552672019-01-25 16:04:11 -0800177 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700178 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
179 func(entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700180 s.customAndroidMkEntries(entries)
181 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800182 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900183 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800184}
185
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700186func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
187 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700188 entries.SetString("LOCAL_MODULE_SUFFIX", "")
189 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700190 if len(s.properties.Symlinks) > 0 {
191 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
192 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700193}
194
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700195func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700196 s.ShBinary.generateAndroidBuildActions(ctx)
197 testDir := "nativetest"
198 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
199 testDir = "nativetest64"
200 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700201 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700202 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
203 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
204 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
205 }
Jaewoong Jung2d4f1a22020-06-10 17:23:46 -0700206 s.installDir = android.PathForModuleInstall(ctx, testDir, proptools.String(s.properties.Sub_dir), s.Name())
207 s.installedFile = ctx.InstallExecutable(s.installDir, s.outputFilePath.Base(), s.outputFilePath)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700208
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700209 s.data = android.PathsForModuleSrc(ctx, s.testProperties.Data)
frankfengc5b87492020-06-03 10:28:47 -0700210
211 var configs []tradefed.Config
212 if Bool(s.testProperties.Require_root) {
213 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", nil})
214 } else {
215 options := []tradefed.Option{{Name: "force-root", Value: "false"}}
216 configs = append(configs, tradefed.Object{"target_preparer", "com.android.tradefed.targetprep.RootTargetPreparer", options})
217 }
218 s.testConfig = tradefed.AutoGenShellTestConfig(ctx, s.testProperties.Test_config,
219 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 -0700220}
221
Colin Cross7c7c1142019-07-29 16:46:49 -0700222func (s *ShTest) InstallInData() bool {
223 return true
224}
225
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700226func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
227 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700228 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700229 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700230 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700231 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
232 func(entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700233 s.customAndroidMkEntries(entries)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700234
Jaewoong Jung2d4f1a22020-06-10 17:23:46 -0700235 entries.SetPath("LOCAL_MODULE_PATH", s.installDir.ToMakePath())
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700236 entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", s.testProperties.Test_suites...)
frankfengc5b87492020-06-03 10:28:47 -0700237 if s.testProperties.Test_config != nil {
238 entries.SetString("LOCAL_TEST_CONFIG", proptools.String(s.testProperties.Test_config))
239 } else {
240 if s.testConfig != nil {
241 entries.SetString("LOCAL_FULL_TEST_CONFIG", s.testConfig.String())
242 }
243 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700244 for _, d := range s.data {
245 rel := d.Rel()
246 path := d.String()
247 if !strings.HasSuffix(path, rel) {
248 panic(fmt.Errorf("path %q does not end with %q", path, rel))
249 }
250 path = strings.TrimSuffix(path, rel)
251 entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700252 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700253 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700254 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900255 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800256}
257
Dan Willemsenb0552672019-01-25 16:04:11 -0800258func InitShBinaryModule(s *ShBinary) {
259 s.AddProperties(&s.properties)
260}
261
Patrice Arrudae1034192019-03-11 13:20:17 -0700262// sh_binary is for a shell script or batch file to be installed as an
263// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700264func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800265 module := &ShBinary{}
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700266 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
267 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
Jiyong Park6ac3cac2019-11-19 12:57:57 +0900268 })
Dan Willemsenb0552672019-01-25 16:04:11 -0800269 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700270 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800271 return module
272}
273
Patrice Arrudae1034192019-03-11 13:20:17 -0700274// sh_binary_host is for a shell script to be installed as an executable binary
275// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700276func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800277 module := &ShBinary{}
278 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700279 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800280 return module
281}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800282
Jaewoong Jung61a83682019-07-01 09:08:50 -0700283// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700284func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800285 module := &ShTest{}
286 InitShBinaryModule(&module.ShBinary)
287 module.AddProperties(&module.testProperties)
288
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700289 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800290 return module
291}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700292
293// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700294func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700295 module := &ShTest{}
296 InitShBinaryModule(&module.ShBinary)
297 module.AddProperties(&module.testProperties)
298
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700299 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700300 return module
301}
frankfengc5b87492020-06-03 10:28:47 -0700302
303var Bool = proptools.Bool