blob: a5db2f6ef15401b8e719fa32a050c946c84bdc2f [file] [log] [blame]
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001// Copyright 2017 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 python
16
17// This file contains the module types for building Python binary.
18
19import (
20 "fmt"
Cole Faust4d247e62023-01-23 10:14:58 -080021 "path/filepath"
22 "strings"
23
Nan Zhangdb0b9a32017-02-27 10:12:13 -080024 "android/soong/android"
25)
26
27func init() {
Paul Duffind0890452021-03-17 21:57:08 +000028 registerPythonBinaryComponents(android.InitRegistrationContext)
Jingwen Chen13b9b422021-03-08 07:32:28 -050029}
30
Paul Duffind0890452021-03-17 21:57:08 +000031func registerPythonBinaryComponents(ctx android.RegistrationContext) {
32 ctx.RegisterModuleType("python_binary_host", PythonBinaryHostFactory)
33}
34
Nan Zhangd4e641b2017-07-12 12:55:28 -070035type BinaryProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080036 // the name of the source file that is the main entry point of the program.
37 // this file must also be listed in srcs.
38 // If left unspecified, module name is used instead.
39 // If name doesn’t match any filename in srcs, main must be specified.
Cole Faustd82f0362023-04-12 17:32:19 -070040 Main *string
Nan Zhangdb0b9a32017-02-27 10:12:13 -080041
42 // set the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -080043 Stem *string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080044
45 // append to the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -080046 Suffix *string `android:"arch_variant"`
Nan Zhangc9c6cb72017-11-03 16:54:05 -070047
48 // list of compatibility suites (for example "cts", "vts") that the module should be
49 // installed into.
50 Test_suites []string `android:"arch_variant"`
Dan Willemsen6ca390f2019-02-14 23:17:08 -080051
52 // whether to use `main` when starting the executable. The default is true, when set to
53 // false it will act much like the normal `python` executable, but with the sources and
54 // libraries automatically included in the PYTHONPATH.
55 Autorun *bool `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -070056
57 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
58 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
59 // explicitly.
60 Auto_gen_config *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -080061}
62
Cole Faust4d247e62023-01-23 10:14:58 -080063type PythonBinaryModule struct {
64 PythonLibraryModule
Nan Zhangd4e641b2017-07-12 12:55:28 -070065 binaryProperties BinaryProperties
Nan Zhangdb0b9a32017-02-27 10:12:13 -080066
Cole Faust4d247e62023-01-23 10:14:58 -080067 // (.intermediate) module output path as installation source.
68 installSource android.Path
69
70 // Final installation path.
71 installedDest android.Path
72
73 androidMkSharedLibs []string
Nan Zhangdb0b9a32017-02-27 10:12:13 -080074}
75
Cole Faust4d247e62023-01-23 10:14:58 -080076var _ android.AndroidMkEntriesProvider = (*PythonBinaryModule)(nil)
77var _ android.Module = (*PythonBinaryModule)(nil)
78
Nan Zhangd4e641b2017-07-12 12:55:28 -070079type IntermPathProvider interface {
80 IntermPathForModuleOut() android.OptionalPath
Nan Zhang5323f8e2017-05-10 13:37:54 -070081}
82
Cole Faust4d247e62023-01-23 10:14:58 -080083func NewBinary(hod android.HostOrDeviceSupported) *PythonBinaryModule {
84 return &PythonBinaryModule{
85 PythonLibraryModule: *newModule(hod, android.MultilibFirst),
86 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -080087}
88
Nan Zhangd4e641b2017-07-12 12:55:28 -070089func PythonBinaryHostFactory() android.Module {
Cole Faust4d247e62023-01-23 10:14:58 -080090 return NewBinary(android.HostSupported).init()
Nan Zhangd4e641b2017-07-12 12:55:28 -070091}
92
Cole Faust4d247e62023-01-23 10:14:58 -080093func (p *PythonBinaryModule) init() android.Module {
94 p.AddProperties(&p.properties, &p.protoProperties)
95 p.AddProperties(&p.binaryProperties)
96 android.InitAndroidArchModule(p, p.hod, p.multilib)
97 android.InitDefaultableModule(p)
98 android.InitBazelModule(p)
99 return p
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800100}
101
Cole Faust4d247e62023-01-23 10:14:58 -0800102func (p *PythonBinaryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
103 p.PythonLibraryModule.GenerateAndroidBuildActions(ctx)
104 p.buildBinary(ctx)
105 p.installedDest = ctx.InstallFile(installDir(ctx, "bin", "", ""),
106 p.installSource.Base(), p.installSource)
Nan Zhangd4e641b2017-07-12 12:55:28 -0700107}
108
Cole Faust4d247e62023-01-23 10:14:58 -0800109func (p *PythonBinaryModule) buildBinary(ctx android.ModuleContext) {
Cole Faust5c503d12023-01-24 11:48:08 -0800110 embeddedLauncher := p.isEmbeddedLauncherEnabled()
111 depsSrcsZips := p.collectPathsFromTransitiveDeps(ctx, embeddedLauncher)
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800112 main := ""
Cole Faust4d247e62023-01-23 10:14:58 -0800113 if p.autorun() {
114 main = p.getPyMainFile(ctx, p.srcsPathMappings)
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800115 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700116
Nan Zhangcba97e62018-09-26 15:14:10 -0700117 var launcherPath android.OptionalPath
Nan Zhang1db85402017-12-18 13:20:23 -0800118 if embeddedLauncher {
Colin Crossee6143c2017-12-30 17:54:27 -0800119 ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700120 if provider, ok := m.(IntermPathProvider); ok {
Nan Zhangcba97e62018-09-26 15:14:10 -0700121 if launcherPath.Valid() {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700122 panic(fmt.Errorf("launcher path was found before: %q",
Nan Zhang1db85402017-12-18 13:20:23 -0800123 launcherPath))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700124 }
Nan Zhangcba97e62018-09-26 15:14:10 -0700125 launcherPath = provider.IntermPathForModuleOut()
Nan Zhangd4e641b2017-07-12 12:55:28 -0700126 }
127 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800128 }
Cole Faust5c503d12023-01-24 11:48:08 -0800129 srcsZips := make(android.Paths, 0, len(depsSrcsZips)+1)
130 if embeddedLauncher {
131 srcsZips = append(srcsZips, p.precompiledSrcsZip)
132 } else {
133 srcsZips = append(srcsZips, p.srcsZip)
134 }
135 srcsZips = append(srcsZips, depsSrcsZips...)
Cole Faust4d247e62023-01-23 10:14:58 -0800136 p.installSource = registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath,
137 p.getHostInterpreterName(ctx, p.properties.Actual_version),
Cole Faust5c503d12023-01-24 11:48:08 -0800138 main, p.getStem(ctx), srcsZips)
Cole Faust909d2372023-02-13 23:17:40 +0000139
140 var sharedLibs []string
141 // if embedded launcher is enabled, we need to collect the shared library dependencies of the
142 // launcher
143 for _, dep := range ctx.GetDirectDepsWithTag(launcherSharedLibTag) {
144 sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep))
145 }
146 p.androidMkSharedLibs = sharedLibs
Cole Faust4d247e62023-01-23 10:14:58 -0800147}
148
149func (p *PythonBinaryModule) AndroidMkEntries() []android.AndroidMkEntries {
150 entries := android.AndroidMkEntries{OutputFile: android.OptionalPathForPath(p.installSource)}
151
152 entries.Class = "EXECUTABLES"
153
154 entries.ExtraEntries = append(entries.ExtraEntries,
155 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
156 entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...)
157 })
158
159 entries.Required = append(entries.Required, "libc++")
160 entries.ExtraEntries = append(entries.ExtraEntries,
161 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
162 path, file := filepath.Split(p.installedDest.String())
163 stem := strings.TrimSuffix(file, filepath.Ext(file))
164
165 entries.SetString("LOCAL_MODULE_SUFFIX", filepath.Ext(file))
166 entries.SetString("LOCAL_MODULE_PATH", path)
167 entries.SetString("LOCAL_MODULE_STEM", stem)
168 entries.AddStrings("LOCAL_SHARED_LIBRARIES", p.androidMkSharedLibs...)
169 entries.SetBool("LOCAL_CHECK_ELF_FILES", false)
170 })
171
172 return []android.AndroidMkEntries{entries}
173}
174
175func (p *PythonBinaryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
176 p.PythonLibraryModule.DepsMutator(ctx)
177
Cole Faust4d247e62023-01-23 10:14:58 -0800178 if p.isEmbeddedLauncherEnabled() {
Cole Faust909d2372023-02-13 23:17:40 +0000179 p.AddDepsOnPythonLauncherAndStdlib(ctx, pythonLibTag, launcherTag, launcherSharedLibTag, p.autorun(), ctx.Target())
Cole Faust4d247e62023-01-23 10:14:58 -0800180 }
181}
182
183// HostToolPath returns a path if appropriate such that this module can be used as a host tool,
184// fulfilling the android.HostToolProvider interface.
185func (p *PythonBinaryModule) HostToolPath() android.OptionalPath {
186 // TODO: This should only be set when building host binaries -- tests built for device would be
187 // setting this incorrectly.
188 return android.OptionalPathForPath(p.installedDest)
189}
190
191// OutputFiles returns output files based on given tag, returns an error if tag is unsupported.
192func (p *PythonBinaryModule) OutputFiles(tag string) (android.Paths, error) {
193 switch tag {
194 case "":
195 return android.Paths{p.installSource}, nil
196 default:
197 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
198 }
199}
200
201func (p *PythonBinaryModule) isEmbeddedLauncherEnabled() bool {
202 return Bool(p.properties.Embedded_launcher)
203}
204
205func (b *PythonBinaryModule) autorun() bool {
206 return BoolDefault(b.binaryProperties.Autorun, true)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800207}
208
Nan Zhangd4e641b2017-07-12 12:55:28 -0700209// get host interpreter name.
Cole Faust4d247e62023-01-23 10:14:58 -0800210func (p *PythonBinaryModule) getHostInterpreterName(ctx android.ModuleContext,
Nan Zhang1db85402017-12-18 13:20:23 -0800211 actualVersion string) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800212 var interp string
Nan Zhang1db85402017-12-18 13:20:23 -0800213 switch actualVersion {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800214 case pyVersion2:
Dan Willemsen7d1681a2017-09-25 13:47:40 -0700215 interp = "python2.7"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800216 case pyVersion3:
217 interp = "python3"
218 default:
219 panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.",
Nan Zhang1db85402017-12-18 13:20:23 -0800220 actualVersion, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800221 }
222
223 return interp
224}
225
226// find main program path within runfiles tree.
Cole Faust4d247e62023-01-23 10:14:58 -0800227func (p *PythonBinaryModule) getPyMainFile(ctx android.ModuleContext,
Nan Zhangd4e641b2017-07-12 12:55:28 -0700228 srcsPathMappings []pathMapping) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800229 var main string
Cole Faust4d247e62023-01-23 10:14:58 -0800230 if String(p.binaryProperties.Main) == "" {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700231 main = ctx.ModuleName() + pyExt
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800232 } else {
Cole Faust4d247e62023-01-23 10:14:58 -0800233 main = String(p.binaryProperties.Main)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800234 }
235
Nan Zhangd4e641b2017-07-12 12:55:28 -0700236 for _, path := range srcsPathMappings {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800237 if main == path.src.Rel() {
238 return path.dest
239 }
240 }
241 ctx.PropertyErrorf("main", "%q is not listed in srcs.", main)
242
243 return ""
244}
245
Cole Faust4d247e62023-01-23 10:14:58 -0800246func (p *PythonBinaryModule) getStem(ctx android.ModuleContext) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800247 stem := ctx.ModuleName()
Cole Faust4d247e62023-01-23 10:14:58 -0800248 if String(p.binaryProperties.Stem) != "" {
249 stem = String(p.binaryProperties.Stem)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800250 }
251
Cole Faust4d247e62023-01-23 10:14:58 -0800252 return stem + String(p.binaryProperties.Suffix)
253}
254
255func installDir(ctx android.ModuleContext, dir, dir64, relative string) android.InstallPath {
256 if ctx.Arch().ArchType.Multilib == "lib64" && dir64 != "" {
257 dir = dir64
258 }
259 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
260 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
261 }
262 return android.PathForModuleInstall(ctx, dir, relative)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800263}