blob: ba0c8be7d89382d431b2bff77e06071fa5cf1ac7 [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
15package android
16
17import (
18 "fmt"
Julien Desprez9e7fc142019-03-08 11:07:05 -080019 "strings"
Dan Willemsenb0552672019-01-25 16:04:11 -080020)
21
22// sh_binary is for shell scripts (and batch files) that are installed as
23// executable files into .../bin/
24//
25// Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary
26// instead.
27
28func init() {
29 RegisterModuleType("sh_binary", ShBinaryFactory)
30 RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
Julien Desprez9e7fc142019-03-08 11:07:05 -080031 RegisterModuleType("sh_test", ShTestFactory)
Jaewoong Jung61a83682019-07-01 09:08:50 -070032 RegisterModuleType("sh_test_host", ShTestHostFactory)
Dan Willemsenb0552672019-01-25 16:04:11 -080033}
34
35type shBinaryProperties struct {
36 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080037 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080038
39 // optional subdirectory under which this file is installed into
40 Sub_dir *string `android:"arch_variant"`
41
42 // optional name for the installed file. If unspecified, name of the module is used as the file name
43 Filename *string `android:"arch_variant"`
44
45 // when set to true, and filename property is not set, the name for the installed file
46 // is the same as the file name of the source file.
47 Filename_from_src *bool `android:"arch_variant"`
48
49 // Whether this module is directly installable to one of the partitions. Default: true.
50 Installable *bool
51}
52
Julien Desprez9e7fc142019-03-08 11:07:05 -080053type TestProperties struct {
54 // list of compatibility suites (for example "cts", "vts") that the module should be
55 // installed into.
56 Test_suites []string `android:"arch_variant"`
57
58 // the name of the test configuration (for example "AndroidTest.xml") that should be
59 // installed with the module.
60 Test_config *string `android:"arch_variant"`
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070061
62 // list of files or filegroup modules that provide data that should be installed alongside
63 // the test.
64 Data []string `android:"path,arch_variant"`
Julien Desprez9e7fc142019-03-08 11:07:05 -080065}
66
Dan Willemsenb0552672019-01-25 16:04:11 -080067type ShBinary struct {
68 ModuleBase
69
70 properties shBinaryProperties
71
72 sourceFilePath Path
73 outputFilePath OutputPath
74}
75
Julien Desprez9e7fc142019-03-08 11:07:05 -080076type ShTest struct {
77 ShBinary
78
79 testProperties TestProperties
Jaewoong Jung8eaeb092019-05-16 14:58:29 -070080
81 data Paths
Julien Desprez9e7fc142019-03-08 11:07:05 -080082}
83
Dan Willemsenb0552672019-01-25 16:04:11 -080084func (s *ShBinary) DepsMutator(ctx BottomUpMutatorContext) {
85 if s.properties.Src == nil {
86 ctx.PropertyErrorf("src", "missing prebuilt source file")
87 }
Dan Willemsenb0552672019-01-25 16:04:11 -080088}
89
90func (s *ShBinary) SourceFilePath(ctx ModuleContext) Path {
Colin Cross8a497952019-03-05 22:25:09 -080091 return PathForModuleSrc(ctx, String(s.properties.Src))
Dan Willemsenb0552672019-01-25 16:04:11 -080092}
93
94func (s *ShBinary) OutputFile() OutputPath {
95 return s.outputFilePath
96}
97
98func (s *ShBinary) SubDir() string {
99 return String(s.properties.Sub_dir)
100}
101
102func (s *ShBinary) Installable() bool {
103 return s.properties.Installable == nil || Bool(s.properties.Installable)
104}
105
106func (s *ShBinary) GenerateAndroidBuildActions(ctx ModuleContext) {
Colin Cross8a497952019-03-05 22:25:09 -0800107 s.sourceFilePath = PathForModuleSrc(ctx, String(s.properties.Src))
Dan Willemsenb0552672019-01-25 16:04:11 -0800108 filename := String(s.properties.Filename)
109 filename_from_src := Bool(s.properties.Filename_from_src)
110 if filename == "" {
111 if filename_from_src {
112 filename = s.sourceFilePath.Base()
113 } else {
114 filename = ctx.ModuleName()
115 }
116 } else if filename_from_src {
117 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
118 return
119 }
120 s.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
121
122 // This ensures that outputFilePath has the correct name for others to
123 // use, as the source file may have a different name.
124 ctx.Build(pctx, BuildParams{
125 Rule: CpExecutable,
126 Output: s.outputFilePath,
127 Input: s.sourceFilePath,
128 })
129}
130
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700131func (s *ShBinary) AndroidMkEntries() AndroidMkEntries {
132 return AndroidMkEntries{
Dan Willemsenb0552672019-01-25 16:04:11 -0800133 Class: "EXECUTABLES",
134 OutputFile: OptionalPathForPath(s.outputFilePath),
135 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700136 ExtraEntries: []AndroidMkExtraEntriesFunc{
137 func(entries *AndroidMkEntries) {
138 s.customAndroidMkEntries(entries)
139 },
Dan Willemsenb0552672019-01-25 16:04:11 -0800140 },
141 }
142}
143
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700144func (s *ShBinary) customAndroidMkEntries(entries *AndroidMkEntries) {
145 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", String(s.properties.Sub_dir))
146 entries.SetString("LOCAL_MODULE_SUFFIX", "")
147 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
148}
149
150func (s *ShTest) GenerateAndroidBuildActions(ctx ModuleContext) {
151 s.ShBinary.GenerateAndroidBuildActions(ctx)
152
153 s.data = PathsForModuleSrc(ctx, s.testProperties.Data)
154}
155
156func (s *ShTest) AndroidMkEntries() AndroidMkEntries {
157 return AndroidMkEntries{
158 Class: "NATIVE_TESTS",
159 OutputFile: OptionalPathForPath(s.outputFilePath),
160 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700161 ExtraEntries: []AndroidMkExtraEntriesFunc{
162 func(entries *AndroidMkEntries) {
163 s.customAndroidMkEntries(entries)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700164
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700165 entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", s.testProperties.Test_suites...)
166 entries.SetString("LOCAL_TEST_CONFIG", String(s.testProperties.Test_config))
167 for _, d := range s.data {
168 rel := d.Rel()
169 path := d.String()
170 if !strings.HasSuffix(path, rel) {
171 panic(fmt.Errorf("path %q does not end with %q", path, rel))
172 }
173 path = strings.TrimSuffix(path, rel)
174 entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel)
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700175 }
Jaewoong Junge0dc8df2019-08-27 17:33:16 -0700176 },
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700177 },
178 }
Julien Desprez9e7fc142019-03-08 11:07:05 -0800179}
180
Dan Willemsenb0552672019-01-25 16:04:11 -0800181func InitShBinaryModule(s *ShBinary) {
182 s.AddProperties(&s.properties)
183}
184
Patrice Arrudae1034192019-03-11 13:20:17 -0700185// sh_binary is for a shell script or batch file to be installed as an
186// executable binary to <partition>/bin.
Dan Willemsenb0552672019-01-25 16:04:11 -0800187func ShBinaryFactory() Module {
188 module := &ShBinary{}
189 InitShBinaryModule(module)
190 InitAndroidArchModule(module, HostAndDeviceSupported, MultilibFirst)
191 return module
192}
193
Patrice Arrudae1034192019-03-11 13:20:17 -0700194// sh_binary_host is for a shell script to be installed as an executable binary
195// to $(HOST_OUT)/bin.
Dan Willemsenb0552672019-01-25 16:04:11 -0800196func ShBinaryHostFactory() Module {
197 module := &ShBinary{}
198 InitShBinaryModule(module)
199 InitAndroidArchModule(module, HostSupported, MultilibFirst)
200 return module
201}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800202
Jaewoong Jung61a83682019-07-01 09:08:50 -0700203// sh_test defines a shell script based test module.
Julien Desprez9e7fc142019-03-08 11:07:05 -0800204func ShTestFactory() Module {
205 module := &ShTest{}
206 InitShBinaryModule(&module.ShBinary)
207 module.AddProperties(&module.testProperties)
208
209 InitAndroidArchModule(module, HostAndDeviceSupported, MultilibFirst)
210 return module
211}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700212
213// sh_test_host defines a shell script based test module that runs on a host.
214func ShTestHostFactory() Module {
215 module := &ShTest{}
216 InitShBinaryModule(&module.ShBinary)
217 module.AddProperties(&module.testProperties)
218
219 InitAndroidArchModule(module, HostSupported, MultilibFirst)
220 return module
221}