blob: a3acb347d20c2a4476628aba3bc57284bbf78a80 [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"
Yu Liue98f7062025-01-17 22:52:43 +000025 "android/soong/cc"
26 "github.com/google/blueprint"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080027)
28
Yu Liue98f7062025-01-17 22:52:43 +000029type PythonBinaryInfo struct{}
30
31var PythonBinaryInfoProvider = blueprint.NewProvider[PythonBinaryInfo]()
32
Nan Zhangdb0b9a32017-02-27 10:12:13 -080033func init() {
Paul Duffind0890452021-03-17 21:57:08 +000034 registerPythonBinaryComponents(android.InitRegistrationContext)
Jingwen Chen13b9b422021-03-08 07:32:28 -050035}
36
Paul Duffind0890452021-03-17 21:57:08 +000037func registerPythonBinaryComponents(ctx android.RegistrationContext) {
38 ctx.RegisterModuleType("python_binary_host", PythonBinaryHostFactory)
39}
40
Nan Zhangd4e641b2017-07-12 12:55:28 -070041type BinaryProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080042 // the name of the source file that is the main entry point of the program.
43 // this file must also be listed in srcs.
44 // If left unspecified, module name is used instead.
45 // If name doesn’t match any filename in srcs, main must be specified.
Cole Faustd82f0362023-04-12 17:32:19 -070046 Main *string
Nan Zhangdb0b9a32017-02-27 10:12:13 -080047
48 // set the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -080049 Stem *string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080050
51 // append to the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -080052 Suffix *string `android:"arch_variant"`
Nan Zhangc9c6cb72017-11-03 16:54:05 -070053
54 // list of compatibility suites (for example "cts", "vts") that the module should be
55 // installed into.
56 Test_suites []string `android:"arch_variant"`
Dan Willemsen6ca390f2019-02-14 23:17:08 -080057
58 // whether to use `main` when starting the executable. The default is true, when set to
59 // false it will act much like the normal `python` executable, but with the sources and
60 // libraries automatically included in the PYTHONPATH.
61 Autorun *bool `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -070062
63 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
64 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
65 // explicitly.
66 Auto_gen_config *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -080067}
68
Cole Faust4d247e62023-01-23 10:14:58 -080069type PythonBinaryModule struct {
70 PythonLibraryModule
Nan Zhangd4e641b2017-07-12 12:55:28 -070071 binaryProperties BinaryProperties
Nan Zhangdb0b9a32017-02-27 10:12:13 -080072
Cole Faust4d247e62023-01-23 10:14:58 -080073 // (.intermediate) module output path as installation source.
74 installSource android.Path
75
76 // Final installation path.
77 installedDest android.Path
78
79 androidMkSharedLibs []string
Nan Zhangdb0b9a32017-02-27 10:12:13 -080080}
81
Cole Faust4d247e62023-01-23 10:14:58 -080082var _ android.AndroidMkEntriesProvider = (*PythonBinaryModule)(nil)
83var _ android.Module = (*PythonBinaryModule)(nil)
84
Nan Zhangd4e641b2017-07-12 12:55:28 -070085type IntermPathProvider interface {
86 IntermPathForModuleOut() android.OptionalPath
Nan Zhang5323f8e2017-05-10 13:37:54 -070087}
88
Cole Faust4d247e62023-01-23 10:14:58 -080089func NewBinary(hod android.HostOrDeviceSupported) *PythonBinaryModule {
90 return &PythonBinaryModule{
91 PythonLibraryModule: *newModule(hod, android.MultilibFirst),
92 }
Nan Zhangdb0b9a32017-02-27 10:12:13 -080093}
94
Nan Zhangd4e641b2017-07-12 12:55:28 -070095func PythonBinaryHostFactory() android.Module {
Cole Faust4d247e62023-01-23 10:14:58 -080096 return NewBinary(android.HostSupported).init()
Nan Zhangd4e641b2017-07-12 12:55:28 -070097}
98
Cole Faust4d247e62023-01-23 10:14:58 -080099func (p *PythonBinaryModule) init() android.Module {
100 p.AddProperties(&p.properties, &p.protoProperties)
101 p.AddProperties(&p.binaryProperties)
102 android.InitAndroidArchModule(p, p.hod, p.multilib)
103 android.InitDefaultableModule(p)
Cole Faust4d247e62023-01-23 10:14:58 -0800104 return p
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800105}
106
Cole Faust4d247e62023-01-23 10:14:58 -0800107func (p *PythonBinaryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
108 p.PythonLibraryModule.GenerateAndroidBuildActions(ctx)
109 p.buildBinary(ctx)
110 p.installedDest = ctx.InstallFile(installDir(ctx, "bin", "", ""),
111 p.installSource.Base(), p.installSource)
Yu Liue98f7062025-01-17 22:52:43 +0000112
113 android.SetProvider(ctx, PythonBinaryInfoProvider, PythonBinaryInfo{})
114
mrziwangca3ea1b2024-06-18 15:32:09 -0700115 ctx.SetOutputFiles(android.Paths{p.installSource}, "")
Nan Zhangd4e641b2017-07-12 12:55:28 -0700116}
117
Cole Faust4d247e62023-01-23 10:14:58 -0800118func (p *PythonBinaryModule) buildBinary(ctx android.ModuleContext) {
Cole Faust5c503d12023-01-24 11:48:08 -0800119 embeddedLauncher := p.isEmbeddedLauncherEnabled()
120 depsSrcsZips := p.collectPathsFromTransitiveDeps(ctx, embeddedLauncher)
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800121 main := ""
Cole Faust4d247e62023-01-23 10:14:58 -0800122 if p.autorun() {
123 main = p.getPyMainFile(ctx, p.srcsPathMappings)
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800124 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700125
Nan Zhangcba97e62018-09-26 15:14:10 -0700126 var launcherPath android.OptionalPath
Nan Zhang1db85402017-12-18 13:20:23 -0800127 if embeddedLauncher {
Yu Liue98f7062025-01-17 22:52:43 +0000128 ctx.VisitDirectDepsProxyWithTag(launcherTag, func(m android.ModuleProxy) {
129 if provider, ok := android.OtherModuleProvider(ctx, m, cc.LinkableInfoProvider); ok {
Nan Zhangcba97e62018-09-26 15:14:10 -0700130 if launcherPath.Valid() {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700131 panic(fmt.Errorf("launcher path was found before: %q",
Nan Zhang1db85402017-12-18 13:20:23 -0800132 launcherPath))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700133 }
Yu Liue98f7062025-01-17 22:52:43 +0000134 launcherPath = provider.OutputFile
Nan Zhangd4e641b2017-07-12 12:55:28 -0700135 }
136 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800137 }
Cole Faust5c503d12023-01-24 11:48:08 -0800138 srcsZips := make(android.Paths, 0, len(depsSrcsZips)+1)
139 if embeddedLauncher {
140 srcsZips = append(srcsZips, p.precompiledSrcsZip)
141 } else {
142 srcsZips = append(srcsZips, p.srcsZip)
143 }
144 srcsZips = append(srcsZips, depsSrcsZips...)
Cole Faust4d247e62023-01-23 10:14:58 -0800145 p.installSource = registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath,
146 p.getHostInterpreterName(ctx, p.properties.Actual_version),
Cole Faust5c503d12023-01-24 11:48:08 -0800147 main, p.getStem(ctx), srcsZips)
Cole Faust909d2372023-02-13 23:17:40 +0000148
149 var sharedLibs []string
150 // if embedded launcher is enabled, we need to collect the shared library dependencies of the
151 // launcher
Yu Liue98f7062025-01-17 22:52:43 +0000152 for _, dep := range ctx.GetDirectDepsProxyWithTag(launcherSharedLibTag) {
Cole Faust909d2372023-02-13 23:17:40 +0000153 sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep))
154 }
155 p.androidMkSharedLibs = sharedLibs
Cole Faust4d247e62023-01-23 10:14:58 -0800156}
157
158func (p *PythonBinaryModule) AndroidMkEntries() []android.AndroidMkEntries {
159 entries := android.AndroidMkEntries{OutputFile: android.OptionalPathForPath(p.installSource)}
160
161 entries.Class = "EXECUTABLES"
162
163 entries.ExtraEntries = append(entries.ExtraEntries,
164 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
165 entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...)
166 })
167
168 entries.Required = append(entries.Required, "libc++")
169 entries.ExtraEntries = append(entries.ExtraEntries,
170 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
171 path, file := filepath.Split(p.installedDest.String())
172 stem := strings.TrimSuffix(file, filepath.Ext(file))
173
174 entries.SetString("LOCAL_MODULE_SUFFIX", filepath.Ext(file))
175 entries.SetString("LOCAL_MODULE_PATH", path)
176 entries.SetString("LOCAL_MODULE_STEM", stem)
177 entries.AddStrings("LOCAL_SHARED_LIBRARIES", p.androidMkSharedLibs...)
178 entries.SetBool("LOCAL_CHECK_ELF_FILES", false)
179 })
180
181 return []android.AndroidMkEntries{entries}
182}
183
184func (p *PythonBinaryModule) DepsMutator(ctx android.BottomUpMutatorContext) {
185 p.PythonLibraryModule.DepsMutator(ctx)
186
Cole Faust4d247e62023-01-23 10:14:58 -0800187 if p.isEmbeddedLauncherEnabled() {
Cole Faust909d2372023-02-13 23:17:40 +0000188 p.AddDepsOnPythonLauncherAndStdlib(ctx, pythonLibTag, launcherTag, launcherSharedLibTag, p.autorun(), ctx.Target())
Cole Faust4d247e62023-01-23 10:14:58 -0800189 }
190}
191
192// HostToolPath returns a path if appropriate such that this module can be used as a host tool,
193// fulfilling the android.HostToolProvider interface.
194func (p *PythonBinaryModule) HostToolPath() android.OptionalPath {
195 // TODO: This should only be set when building host binaries -- tests built for device would be
196 // setting this incorrectly.
197 return android.OptionalPathForPath(p.installedDest)
198}
199
Cole Faust4d247e62023-01-23 10:14:58 -0800200func (p *PythonBinaryModule) isEmbeddedLauncherEnabled() bool {
Cole Faustf09101e2024-04-18 18:33:15 +0000201 return BoolDefault(p.properties.Embedded_launcher, true)
Cole Faust4d247e62023-01-23 10:14:58 -0800202}
203
204func (b *PythonBinaryModule) autorun() bool {
205 return BoolDefault(b.binaryProperties.Autorun, true)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800206}
207
Nan Zhangd4e641b2017-07-12 12:55:28 -0700208// get host interpreter name.
Cole Faust4d247e62023-01-23 10:14:58 -0800209func (p *PythonBinaryModule) getHostInterpreterName(ctx android.ModuleContext,
Nan Zhang1db85402017-12-18 13:20:23 -0800210 actualVersion string) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800211 var interp string
Nan Zhang1db85402017-12-18 13:20:23 -0800212 switch actualVersion {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800213 case pyVersion2:
Dan Willemsen7d1681a2017-09-25 13:47:40 -0700214 interp = "python2.7"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800215 case pyVersion3:
216 interp = "python3"
217 default:
218 panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.",
Nan Zhang1db85402017-12-18 13:20:23 -0800219 actualVersion, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800220 }
221
222 return interp
223}
224
225// find main program path within runfiles tree.
Cole Faust4d247e62023-01-23 10:14:58 -0800226func (p *PythonBinaryModule) getPyMainFile(ctx android.ModuleContext,
Nan Zhangd4e641b2017-07-12 12:55:28 -0700227 srcsPathMappings []pathMapping) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800228 var main string
Cole Faust4d247e62023-01-23 10:14:58 -0800229 if String(p.binaryProperties.Main) == "" {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700230 main = ctx.ModuleName() + pyExt
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800231 } else {
Cole Faust4d247e62023-01-23 10:14:58 -0800232 main = String(p.binaryProperties.Main)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800233 }
234
Nan Zhangd4e641b2017-07-12 12:55:28 -0700235 for _, path := range srcsPathMappings {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800236 if main == path.src.Rel() {
237 return path.dest
238 }
239 }
240 ctx.PropertyErrorf("main", "%q is not listed in srcs.", main)
241
242 return ""
243}
244
Cole Faust4d247e62023-01-23 10:14:58 -0800245func (p *PythonBinaryModule) getStem(ctx android.ModuleContext) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800246 stem := ctx.ModuleName()
Cole Faust4d247e62023-01-23 10:14:58 -0800247 if String(p.binaryProperties.Stem) != "" {
248 stem = String(p.binaryProperties.Stem)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800249 }
250
Cole Faust4d247e62023-01-23 10:14:58 -0800251 return stem + String(p.binaryProperties.Suffix)
252}
253
254func installDir(ctx android.ModuleContext, dir, dir64, relative string) android.InstallPath {
255 if ctx.Arch().ArchType.Multilib == "lib64" && dir64 != "" {
256 dir = dir64
257 }
258 if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) {
259 dir = filepath.Join(dir, ctx.Arch().ArchType.String())
260 }
261 return android.PathForModuleInstall(ctx, dir, relative)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800262}