blob: 2855aa040405a1e38ab1b99bb117c044444b86df [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 Jung8eaeb092019-05-16 14:58:29 -0700136 AddCustomEntries: func(name, prefix, moduleDir string, entries *AndroidMkEntries) {
137 s.customAndroidMkEntries(entries)
Dan Willemsenb0552672019-01-25 16:04:11 -0800138 },
139 }
140}
141
Jaewoong Jung8eaeb092019-05-16 14:58:29 -0700142func (s *ShBinary) customAndroidMkEntries(entries *AndroidMkEntries) {
143 entries.SetString("LOCAL_MODULE_RELATIVE_PATH", String(s.properties.Sub_dir))
144 entries.SetString("LOCAL_MODULE_SUFFIX", "")
145 entries.SetString("LOCAL_MODULE_STEM", s.outputFilePath.Rel())
146}
147
148func (s *ShTest) GenerateAndroidBuildActions(ctx ModuleContext) {
149 s.ShBinary.GenerateAndroidBuildActions(ctx)
150
151 s.data = PathsForModuleSrc(ctx, s.testProperties.Data)
152}
153
154func (s *ShTest) AndroidMkEntries() AndroidMkEntries {
155 return AndroidMkEntries{
156 Class: "NATIVE_TESTS",
157 OutputFile: OptionalPathForPath(s.outputFilePath),
158 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
159 AddCustomEntries: func(name, prefix, moduleDir string, entries *AndroidMkEntries) {
160 s.customAndroidMkEntries(entries)
161
162 entries.AddStrings("LOCAL_COMPATIBILITY_SUITE", s.testProperties.Test_suites...)
163 entries.SetString("LOCAL_TEST_CONFIG", String(s.testProperties.Test_config))
164 for _, d := range s.data {
165 rel := d.Rel()
166 path := d.String()
167 if !strings.HasSuffix(path, rel) {
168 panic(fmt.Errorf("path %q does not end with %q", path, rel))
169 }
170 path = strings.TrimSuffix(path, rel)
171 entries.AddStrings("LOCAL_TEST_DATA", path+":"+rel)
172 }
173 },
174 }
Julien Desprez9e7fc142019-03-08 11:07:05 -0800175}
176
Dan Willemsenb0552672019-01-25 16:04:11 -0800177func InitShBinaryModule(s *ShBinary) {
178 s.AddProperties(&s.properties)
179}
180
Patrice Arrudae1034192019-03-11 13:20:17 -0700181// sh_binary is for a shell script or batch file to be installed as an
182// executable binary to <partition>/bin.
Dan Willemsenb0552672019-01-25 16:04:11 -0800183func ShBinaryFactory() Module {
184 module := &ShBinary{}
185 InitShBinaryModule(module)
186 InitAndroidArchModule(module, HostAndDeviceSupported, MultilibFirst)
187 return module
188}
189
Patrice Arrudae1034192019-03-11 13:20:17 -0700190// sh_binary_host is for a shell script to be installed as an executable binary
191// to $(HOST_OUT)/bin.
Dan Willemsenb0552672019-01-25 16:04:11 -0800192func ShBinaryHostFactory() Module {
193 module := &ShBinary{}
194 InitShBinaryModule(module)
195 InitAndroidArchModule(module, HostSupported, MultilibFirst)
196 return module
197}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800198
Jaewoong Jung61a83682019-07-01 09:08:50 -0700199// sh_test defines a shell script based test module.
Julien Desprez9e7fc142019-03-08 11:07:05 -0800200func ShTestFactory() Module {
201 module := &ShTest{}
202 InitShBinaryModule(&module.ShBinary)
203 module.AddProperties(&module.testProperties)
204
205 InitAndroidArchModule(module, HostAndDeviceSupported, MultilibFirst)
206 return module
207}
Jaewoong Jung61a83682019-07-01 09:08:50 -0700208
209// sh_test_host defines a shell script based test module that runs on a host.
210func ShTestHostFactory() Module {
211 module := &ShTest{}
212 InitShBinaryModule(&module.ShBinary)
213 module.AddProperties(&module.testProperties)
214
215 InitAndroidArchModule(module, HostSupported, MultilibFirst)
216 return module
217}