blob: 52c5a9a33d1da0f929610d18104cddaa77e2a673 [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"
20)
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)
31}
32
33type shBinaryProperties struct {
34 // Source file of this prebuilt.
Colin Cross27b922f2019-03-04 22:35:41 -080035 Src *string `android:"path,arch_variant"`
Dan Willemsenb0552672019-01-25 16:04:11 -080036
37 // optional subdirectory under which this file is installed into
38 Sub_dir *string `android:"arch_variant"`
39
40 // optional name for the installed file. If unspecified, name of the module is used as the file name
41 Filename *string `android:"arch_variant"`
42
43 // when set to true, and filename property is not set, the name for the installed file
44 // is the same as the file name of the source file.
45 Filename_from_src *bool `android:"arch_variant"`
46
47 // Whether this module is directly installable to one of the partitions. Default: true.
48 Installable *bool
49}
50
51type ShBinary struct {
52 ModuleBase
53
54 properties shBinaryProperties
55
56 sourceFilePath Path
57 outputFilePath OutputPath
58}
59
60func (s *ShBinary) DepsMutator(ctx BottomUpMutatorContext) {
61 if s.properties.Src == nil {
62 ctx.PropertyErrorf("src", "missing prebuilt source file")
63 }
Dan Willemsenb0552672019-01-25 16:04:11 -080064}
65
66func (s *ShBinary) SourceFilePath(ctx ModuleContext) Path {
67 return ctx.ExpandSource(String(s.properties.Src), "src")
68}
69
70func (s *ShBinary) OutputFile() OutputPath {
71 return s.outputFilePath
72}
73
74func (s *ShBinary) SubDir() string {
75 return String(s.properties.Sub_dir)
76}
77
78func (s *ShBinary) Installable() bool {
79 return s.properties.Installable == nil || Bool(s.properties.Installable)
80}
81
82func (s *ShBinary) GenerateAndroidBuildActions(ctx ModuleContext) {
83 s.sourceFilePath = ctx.ExpandSource(String(s.properties.Src), "src")
84 filename := String(s.properties.Filename)
85 filename_from_src := Bool(s.properties.Filename_from_src)
86 if filename == "" {
87 if filename_from_src {
88 filename = s.sourceFilePath.Base()
89 } else {
90 filename = ctx.ModuleName()
91 }
92 } else if filename_from_src {
93 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
94 return
95 }
96 s.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
97
98 // This ensures that outputFilePath has the correct name for others to
99 // use, as the source file may have a different name.
100 ctx.Build(pctx, BuildParams{
101 Rule: CpExecutable,
102 Output: s.outputFilePath,
103 Input: s.sourceFilePath,
104 })
105}
106
107func (s *ShBinary) AndroidMk() AndroidMkData {
108 return AndroidMkData{
109 Class: "EXECUTABLES",
110 OutputFile: OptionalPathForPath(s.outputFilePath),
111 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
112 Extra: []AndroidMkExtraFunc{
113 func(w io.Writer, outputFile Path) {
114 fmt.Fprintln(w, "LOCAL_MODULE_RELATIVE_PATH :=", String(s.properties.Sub_dir))
115 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX :=")
116 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", s.outputFilePath.Rel())
117 },
118 },
119 }
120}
121
122func InitShBinaryModule(s *ShBinary) {
123 s.AddProperties(&s.properties)
124}
125
Patrice Arrudae1034192019-03-11 13:20:17 -0700126// sh_binary is for a shell script or batch file to be installed as an
127// executable binary to <partition>/bin.
Dan Willemsenb0552672019-01-25 16:04:11 -0800128func ShBinaryFactory() Module {
129 module := &ShBinary{}
130 InitShBinaryModule(module)
131 InitAndroidArchModule(module, HostAndDeviceSupported, MultilibFirst)
132 return module
133}
134
Patrice Arrudae1034192019-03-11 13:20:17 -0700135// sh_binary_host is for a shell script to be installed as an executable binary
136// to $(HOST_OUT)/bin.
Dan Willemsenb0552672019-01-25 16:04:11 -0800137func ShBinaryHostFactory() Module {
138 module := &ShBinary{}
139 InitShBinaryModule(module)
140 InitAndroidArchModule(module, HostSupported, MultilibFirst)
141 return module
142}