blob: 391519391f34db87eea4e3e1f21ba03f67fbeb4c [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.
35 Src *string `android:"arch_variant"`
36
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 }
64
65 // To support ":modulename" in src
66 ExtractSourceDeps(ctx, s.properties.Src)
67}
68
69func (s *ShBinary) SourceFilePath(ctx ModuleContext) Path {
70 return ctx.ExpandSource(String(s.properties.Src), "src")
71}
72
73func (s *ShBinary) OutputFile() OutputPath {
74 return s.outputFilePath
75}
76
77func (s *ShBinary) SubDir() string {
78 return String(s.properties.Sub_dir)
79}
80
81func (s *ShBinary) Installable() bool {
82 return s.properties.Installable == nil || Bool(s.properties.Installable)
83}
84
85func (s *ShBinary) GenerateAndroidBuildActions(ctx ModuleContext) {
86 s.sourceFilePath = ctx.ExpandSource(String(s.properties.Src), "src")
87 filename := String(s.properties.Filename)
88 filename_from_src := Bool(s.properties.Filename_from_src)
89 if filename == "" {
90 if filename_from_src {
91 filename = s.sourceFilePath.Base()
92 } else {
93 filename = ctx.ModuleName()
94 }
95 } else if filename_from_src {
96 ctx.PropertyErrorf("filename_from_src", "filename is set. filename_from_src can't be true")
97 return
98 }
99 s.outputFilePath = PathForModuleOut(ctx, filename).OutputPath
100
101 // This ensures that outputFilePath has the correct name for others to
102 // use, as the source file may have a different name.
103 ctx.Build(pctx, BuildParams{
104 Rule: CpExecutable,
105 Output: s.outputFilePath,
106 Input: s.sourceFilePath,
107 })
108}
109
110func (s *ShBinary) AndroidMk() AndroidMkData {
111 return AndroidMkData{
112 Class: "EXECUTABLES",
113 OutputFile: OptionalPathForPath(s.outputFilePath),
114 Include: "$(BUILD_SYSTEM)/soong_cc_prebuilt.mk",
115 Extra: []AndroidMkExtraFunc{
116 func(w io.Writer, outputFile Path) {
117 fmt.Fprintln(w, "LOCAL_MODULE_RELATIVE_PATH :=", String(s.properties.Sub_dir))
118 fmt.Fprintln(w, "LOCAL_MODULE_SUFFIX :=")
119 fmt.Fprintln(w, "LOCAL_MODULE_STEM :=", s.outputFilePath.Rel())
120 },
121 },
122 }
123}
124
125func InitShBinaryModule(s *ShBinary) {
126 s.AddProperties(&s.properties)
127}
128
129func ShBinaryFactory() Module {
130 module := &ShBinary{}
131 InitShBinaryModule(module)
132 InitAndroidArchModule(module, HostAndDeviceSupported, MultilibFirst)
133 return module
134}
135
136func ShBinaryHostFactory() Module {
137 module := &ShBinary{}
138 InitShBinaryModule(module)
139 InitAndroidArchModule(module, HostSupported, MultilibFirst)
140 return module
141}