blob: 8bb351783ff64f43daf70b4f7ad3ad15a7b5c530 [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"
19 "io"
Julien Desprez9e7fc142019-03-08 11:07:05 -080020 "strings"
Dan Willemsenb0552672019-01-25 16:04:11 -080021)
22
23// sh_binary is for shell scripts (and batch files) that are installed as
24// executable files into .../bin/
25//
26// Do not use them for prebuilt C/C++/etc files. Use cc_prebuilt_binary
27// instead.
28
29func init() {
30 RegisterModuleType("sh_binary", ShBinaryFactory)
31 RegisterModuleType("sh_binary_host", ShBinaryHostFactory)
Julien Desprez9e7fc142019-03-08 11:07:05 -080032 RegisterModuleType("sh_test", ShTestFactory)
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"`
61}
62
Dan Willemsenb0552672019-01-25 16:04:11 -080063type ShBinary struct {
64 ModuleBase
65
66 properties shBinaryProperties
67
68 sourceFilePath Path
69 outputFilePath OutputPath
70}
71
Julien Desprez9e7fc142019-03-08 11:07:05 -080072type ShTest struct {
73 ShBinary
74
75 testProperties TestProperties
76}
77
Dan Willemsenb0552672019-01-25 16:04:11 -080078func (s *ShBinary) DepsMutator(ctx BottomUpMutatorContext) {
79 if s.properties.Src == nil {
80 ctx.PropertyErrorf("src", "missing prebuilt source file")
81 }
Dan Willemsenb0552672019-01-25 16:04:11 -080082}
83
84func (s *ShBinary) SourceFilePath(ctx ModuleContext) Path {
85 return ctx.ExpandSource(String(s.properties.Src), "src")
86}
87
88func (s *ShBinary) OutputFile() OutputPath {
89 return s.outputFilePath
90}
91
92func (s *ShBinary) SubDir() string {
93 return String(s.properties.Sub_dir)
94}
95
96func (s *ShBinary) Installable() bool {
97 return s.properties.Installable == nil || Bool(s.properties.Installable)
98}
99
100func (s *ShBinary) GenerateAndroidBuildActions(ctx ModuleContext) {
101 s.sourceFilePath = ctx.ExpandSource(String(s.properties.Src), "src")
102 filename := String(s.properties.Filename)
103 filename_from_src := Bool(s.properties.Filename_from_src)
104 if filename == "" {
105 if filename_from_src {
106 filename = s.sourceFilePath.Base()
107 } else {
108 filename = ctx.ModuleName()
109 }
110 } else if filename_from_src {
111 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
112 return
113 }
114 s.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
115
116 // This ensures that outputFilePath has the correct name for others to
117 // use, as the source file may have a different name.
118 ctx.Build(pctx, BuildParams{
119 Rule: CpExecutable,
120 Output: s.outputFilePath,
121 Input: s.sourceFilePath,
122 })
123}
124
125func (s *ShBinary) AndroidMk() AndroidMkData {
126 return AndroidMkData{
127 Class: "EXECUTABLES",
128 OutputFile: OptionalPathForPath(s.outputFilePath),
129 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
130 Extra: []AndroidMkExtraFunc{
131 func(w io.Writer, outputFile Path) {
132 fmt.Fprintln(w, "LOCAL_MODULE_RELATIVE_PATH :=", String(s.properties.Sub_dir))
133 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX :=")
134 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", s.outputFilePath.Rel())
135 },
136 },
137 }
138}
139
Julien Desprez9e7fc142019-03-08 11:07:05 -0800140func (s *ShTest) AndroidMk() AndroidMkData {
141 data := s.ShBinary.AndroidMk()
142 data.Extra = append(data.Extra, func(w io.Writer, outputFile Path) {
143 fmt.Fprintln(w, "LOCAL_COMPATIBILITY_SUITE :=",
144 strings.Join(s.testProperties.Test_suites, " "))
145 fmt.Fprintln(w, "LOCAL_TEST_CONFIG :=", String(s.testProperties.Test_config))
146 })
147 return data
148}
149
Dan Willemsenb0552672019-01-25 16:04:11 -0800150func InitShBinaryModule(s *ShBinary) {
151 s.AddProperties(&s.properties)
152}
153
Patrice Arrudae1034192019-03-11 13:20:17 -0700154// sh_binary is for a shell script or batch file to be installed as an
155// executable binary to <partition>/bin.
Dan Willemsenb0552672019-01-25 16:04:11 -0800156func ShBinaryFactory() Module {
157 module := &ShBinary{}
158 InitShBinaryModule(module)
159 InitAndroidArchModule(module, HostAndDeviceSupported, MultilibFirst)
160 return module
161}
162
Patrice Arrudae1034192019-03-11 13:20:17 -0700163// sh_binary_host is for a shell script to be installed as an executable binary
164// to $(HOST_OUT)/bin.
Dan Willemsenb0552672019-01-25 16:04:11 -0800165func ShBinaryHostFactory() Module {
166 module := &ShBinary{}
167 InitShBinaryModule(module)
168 InitAndroidArchModule(module, HostSupported, MultilibFirst)
169 return module
170}
Julien Desprez9e7fc142019-03-08 11:07:05 -0800171
172func ShTestFactory() Module {
173 module := &ShTest{}
174 InitShBinaryModule(&module.ShBinary)
175 module.AddProperties(&module.testProperties)
176
177 InitAndroidArchModule(module, HostAndDeviceSupported, MultilibFirst)
178 return module
179}