blob: 2dcd7162c780807ff1814a879e5f46f798197927 [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"
Dan Willemsenb0552672019-01-25 16:04:11 -080025)
26
27// sh_binary is for shell scripts (and batch files) that are installed as
28// executable files into .../bin/
29//
30// Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary
31// instead.
32
Jaewoong Jung4b79e982020-06-01 10:45:49 -070033var pctx = android.NewPackageContext("android/soong/sh")
34
Dan Willemsenb0552672019-01-25 16:04:11 -080035func init() {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070036 pctx.Import("android/soong/android")
37
38 android.RegisterModuleType("sh_binary", ShBinaryFactory)
39 android.RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
40 android.RegisterModuleType("sh_test", ShTestFactory)
41 android.RegisterModuleType("sh_test_host", ShTestHostFactory)
Dan Willemsenb0552672019-01-25 16:04:11 -080042}
43
44type shBinaryProperties struct {
45 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080046 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080047
48 // optional subdirectory under which this file is installed into
49 Sub_dir *string `android:"arch_variant"`
50
51 // optional name for the installed file. If unspecified, name of the module is used as the file name
52 Filename *string `android:"arch_variant"`
53
54 // when set to true, and filename property is not set, the name for the installed file
55 // is the same as the file name of the source file.
56 Filename_from_src *bool `android:"arch_variant"`
57
58 // Whether this module is directly installable to one of the partitions. Default: true.
59 Installable *bool
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -070060
61 // install symlinks to the binary
62 Symlinks []string `android:"arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080063}
64
Julien Desprez9e7fc142019-03-08 11:07:05 -080065type TestProperties struct {
66 // list of compatibility suites (for example "cts", "vts") that the module should be
67 // installed into.
68 Test_suites []string `android:"arch_variant"`
69
70 // the name of the test configuration (for example "AndroidTest.xml") that should be
71 // installed with the module.
72 Test_config *string `android:"arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070073
74 // list of files or filegroup modules that provide data that should be installed alongside
75 // the test.
76 Data []string `android:"path,arch_variant"`
Julien Desprez9e7fc142019-03-08 11:07:05 -080077}
78
Dan Willemsenb0552672019-01-25 16:04:11 -080079type ShBinary struct {
Jaewoong Jung4b79e982020-06-01 10:45:49 -070080 android.ModuleBase
Dan Willemsenb0552672019-01-25 16:04:11 -080081
82 properties shBinaryProperties
83
Jaewoong Jung4b79e982020-06-01 10:45:49 -070084 sourceFilePath android.Path
85 outputFilePath android.OutputPath
86 installedFile android.InstallPath
Dan Willemsenb0552672019-01-25 16:04:11 -080087}
88
Jaewoong Jung4b79e982020-06-01 10:45:49 -070089var _ android.HostToolProvider = (*ShBinary)(nil)
Colin Cross7c7c1142019-07-29 16:46:49 -070090
Julien Desprez9e7fc142019-03-08 11:07:05 -080091type ShTest struct {
92 ShBinary
93
94 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070095
Jaewoong Jung4b79e982020-06-01 10:45:49 -070096 data android.Paths
Julien Desprez9e7fc142019-03-08 11:07:05 -080097}
98
Jaewoong Jung4b79e982020-06-01 10:45:49 -070099func (s *ShBinary) HostToolPath() android.OptionalPath {
100 return android.OptionalPathForPath(s.installedFile)
Colin Cross7c7c1142019-07-29 16:46:49 -0700101}
102
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700103func (s *ShBinary) DepsMutator(ctx android.BottomUpMutatorContext) {
Dan Willemsenb0552672019-01-25 16:04:11 -0800104 if s.properties.Src == nil {
105 ctx.PropertyErrorf("src", "missing prebuilt source file")
106 }
Dan Willemsenb0552672019-01-25 16:04:11 -0800107}
108
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700109func (s *ShBinary) OutputFile() android.OutputPath {
Dan Willemsenb0552672019-01-25 16:04:11 -0800110 return s.outputFilePath
111}
112
113func (s *ShBinary) SubDir() string {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700114 return proptools.String(s.properties.Sub_dir)
Dan Willemsenb0552672019-01-25 16:04:11 -0800115}
116
117func (s *ShBinary) Installable() bool {
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700118 return s.properties.Installable == nil || proptools.Bool(s.properties.Installable)
Dan Willemsenb0552672019-01-25 16:04:11 -0800119}
120
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700121func (s *ShBinary) Symlinks() []string {
122 return s.properties.Symlinks
123}
124
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700125func (s *ShBinary) generateAndroidBuildActions(ctx android.ModuleContext) {
126 s.sourceFilePath = android.PathForModuleSrc(ctx, proptools.String(s.properties.Src))
127 filename := proptools.String(s.properties.Filename)
128 filename_from_src := proptools.Bool(s.properties.Filename_from_src)
Dan Willemsenb0552672019-01-25 16:04:11 -0800129 if filename == "" {
130 if filename_from_src {
131 filename = s.sourceFilePath.Base()
132 } else {
133 filename = ctx.ModuleName()
134 }
135 } else if filename_from_src {
136 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
137 return
138 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700139 s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
Dan Willemsenb0552672019-01-25 16:04:11 -0800140
141 // This ensures that outputFilePath has the correct name for others to
142 // use, as the source file may have a different name.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700143 ctx.Build(pctx, android.BuildParams{
144 Rule: android.CpExecutable,
Dan Willemsenb0552672019-01-25 16:04:11 -0800145 Output: s.outputFilePath,
146 Input: s.sourceFilePath,
147 })
148}
149
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700150func (s *ShBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700151 s.generateAndroidBuildActions(ctx)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700152 installDir := android.PathForModuleInstall(ctx, "bin", proptools.String(s.properties.Sub_dir))
Colin Cross7c7c1142019-07-29 16:46:49 -0700153 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
154}
155
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700156func (s *ShBinary) AndroidMkEntries() []android.AndroidMkEntries {
157 return []android.AndroidMkEntries{android.AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800158 Class: "EXECUTABLES",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700159 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Dan Willemsenb0552672019-01-25 16:04:11 -0800160 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700161 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
162 func(entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700163 s.customAndroidMkEntries(entries)
164 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800165 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900166 }}
Dan Willemsenb0552672019-01-25 16:04:11 -0800167}
168
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700169func (s *ShBinary) customAndroidMkEntries(entries *android.AndroidMkEntries) {
170 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", proptools.String(s.properties.Sub_dir))
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700171 entries.SetString("LOCAL_MODULE_SUFFIX", "")
172 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
Rashed Abdel-Tawab6a341312019-10-04 20:38:01 -0700173 if len(s.properties.Symlinks) > 0 {
174 entries.SetString("LOCAL_MODULE_SYMLINKS", strings.Join(s.properties.Symlinks, " "))
175 }
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700176}
177
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700178func (s *ShTest) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Colin Cross7c7c1142019-07-29 16:46:49 -0700179 s.ShBinary.generateAndroidBuildActions(ctx)
180 testDir := "nativetest"
181 if ctx.Target().Arch.ArchType.Multilib == "lib64" {
182 testDir = "nativetest64"
183 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700184 if ctx.Target().NativeBridge == android.NativeBridgeEnabled {
Colin Cross7c7c1142019-07-29 16:46:49 -0700185 testDir = filepath.Join(testDir, ctx.Target().NativeBridgeRelativePath)
186 } else if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
187 testDir = filepath.Join(testDir, ctx.Arch().ArchType.String())
188 }
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700189 installDir := android.PathForModuleInstall(ctx, testDir, proptools.String(s.properties.Sub_dir))
Colin Cross7c7c1142019-07-29 16:46:49 -0700190 s.installedFile = ctx.InstallExecutable(installDir, s.outputFilePath.Base(), s.outputFilePath)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700191
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700192 s.data = android.PathsForModuleSrc(ctx, s.testProperties.Data)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700193}
194
Colin Cross7c7c1142019-07-29 16:46:49 -0700195func (s *ShTest) InstallInData() bool {
196 return true
197}
198
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700199func (s *ShTest) AndroidMkEntries() []android.AndroidMkEntries {
200 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700201 Class: "NATIVE_TESTS",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700202 OutputFile: android.OptionalPathForPath(s.outputFilePath),
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700203 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700204 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
205 func(entries *android.AndroidMkEntries) {
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700206 s.customAndroidMkEntries(entries)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700207
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700208 entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", s.testProperties.Test_suites...)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700209 entries.SetString("LOCAL_TEST_CONFIG", proptools.String(s.testProperties.Test_config))
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700210 for _, d := range s.data {
211 rel := d.Rel()
212 path := d.String()
213 if !strings.HasSuffix(path, rel) {
214 panic(fmt.Errorf("path %q does not end with %q", path, rel))
215 }
216 path = strings.TrimSuffix(path, rel)
217 entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700218 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700219 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700220 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900221 }}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800222}
223
Dan Willemsenb0552672019-01-25 16:04:11 -0800224func InitShBinaryModule(s *ShBinary) {
225 s.AddProperties(&s.properties)
226}
227
Patrice Arrudae1034192019-03-11 13:20:17 -0700228// sh_binary is for a shell script or batch file to be installed as an
229// executable binary to <partition>/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700230func ShBinaryFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800231 module := &ShBinary{}
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700232 module.Prefer32(func(ctx android.BaseModuleContext, base *android.ModuleBase, class android.OsClass) bool {
233 return class == android.Device && ctx.Config().DevicePrefer32BitExecutables()
Jiyong Park6ac3cac2019-11-19 12:57:57 +0900234 })
Dan Willemsenb0552672019-01-25 16:04:11 -0800235 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700236 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800237 return module
238}
239
Patrice Arrudae1034192019-03-11 13:20:17 -0700240// sh_binary_host is for a shell script to be installed as an executable binary
241// to $(HOST_OUT)/bin.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700242func ShBinaryHostFactory() android.Module {
Dan Willemsenb0552672019-01-25 16:04:11 -0800243 module := &ShBinary{}
244 InitShBinaryModule(module)
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700245 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Dan Willemsenb0552672019-01-25 16:04:11 -0800246 return module
247}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800248
Jaewoong Jung61a83682019-07-01 09:08:50 -0700249// sh_test defines a shell script based test module.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700250func ShTestFactory() android.Module {
Julien Desprez9e7fc142019-03-08 11:07:05 -0800251 module := &ShTest{}
252 InitShBinaryModule(&module.ShBinary)
253 module.AddProperties(&module.testProperties)
254
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700255 android.InitAndroidArchModule(module, android.HostAndDeviceSupported, android.MultilibFirst)
Julien Desprez9e7fc142019-03-08 11:07:05 -0800256 return module
257}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700258
259// sh_test_host defines a shell script based test module that runs on a host.
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700260func ShTestHostFactory() android.Module {
Jaewoong Jung61a83682019-07-01 09:08:50 -0700261 module := &ShTest{}
262 InitShBinaryModule(&module.ShBinary)
263 module.AddProperties(&module.testProperties)
264
Jaewoong Jung4b79e982020-06-01 10:45:49 -0700265 android.InitAndroidArchModule(module, android.HostSupported, android.MultilibFirst)
Jaewoong Jung61a83682019-07-01 09:08:50 -0700266 return module
267}