Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package python |
| 16 | |
| 17 | // This file contains the module types for building Python binary. |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 21 | "path/filepath" |
| 22 | "strings" |
| 23 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 24 | "android/soong/android" |
Yu Liu | e98f706 | 2025-01-17 22:52:43 +0000 | [diff] [blame] | 25 | "android/soong/cc" |
Cole Faust | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 26 | |
Yu Liu | e98f706 | 2025-01-17 22:52:43 +0000 | [diff] [blame] | 27 | "github.com/google/blueprint" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Yu Liu | e98f706 | 2025-01-17 22:52:43 +0000 | [diff] [blame] | 30 | type PythonBinaryInfo struct{} |
| 31 | |
| 32 | var PythonBinaryInfoProvider = blueprint.NewProvider[PythonBinaryInfo]() |
| 33 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 34 | func init() { |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 35 | registerPythonBinaryComponents(android.InitRegistrationContext) |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 36 | } |
| 37 | |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 38 | func registerPythonBinaryComponents(ctx android.RegistrationContext) { |
| 39 | ctx.RegisterModuleType("python_binary_host", PythonBinaryHostFactory) |
| 40 | } |
| 41 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 42 | type BinaryProperties struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 43 | // the name of the source file that is the main entry point of the program. |
| 44 | // this file must also be listed in srcs. |
| 45 | // If left unspecified, module name is used instead. |
| 46 | // If name doesn’t match any filename in srcs, main must be specified. |
Cole Faust | d82f036 | 2023-04-12 17:32:19 -0700 | [diff] [blame] | 47 | Main *string |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 48 | |
| 49 | // set the name of the output binary. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 50 | Stem *string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 51 | |
| 52 | // append to the name of the output binary. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 53 | Suffix *string `android:"arch_variant"` |
Nan Zhang | c9c6cb7 | 2017-11-03 16:54:05 -0700 | [diff] [blame] | 54 | |
| 55 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 56 | // installed into. |
| 57 | Test_suites []string `android:"arch_variant"` |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 58 | |
| 59 | // whether to use `main` when starting the executable. The default is true, when set to |
| 60 | // false it will act much like the normal `python` executable, but with the sources and |
| 61 | // libraries automatically included in the PYTHONPATH. |
| 62 | Autorun *bool `android:"arch_variant"` |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 63 | |
| 64 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml |
| 65 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true |
| 66 | // explicitly. |
| 67 | Auto_gen_config *bool |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 70 | type PythonBinaryModule struct { |
| 71 | PythonLibraryModule |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 72 | binaryProperties BinaryProperties |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 73 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 74 | // (.intermediate) module output path as installation source. |
| 75 | installSource android.Path |
| 76 | |
| 77 | // Final installation path. |
| 78 | installedDest android.Path |
| 79 | |
| 80 | androidMkSharedLibs []string |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 83 | var _ android.AndroidMkEntriesProvider = (*PythonBinaryModule)(nil) |
| 84 | var _ android.Module = (*PythonBinaryModule)(nil) |
| 85 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 86 | type IntermPathProvider interface { |
| 87 | IntermPathForModuleOut() android.OptionalPath |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 90 | func NewBinary(hod android.HostOrDeviceSupported) *PythonBinaryModule { |
| 91 | return &PythonBinaryModule{ |
| 92 | PythonLibraryModule: *newModule(hod, android.MultilibFirst), |
| 93 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 96 | func PythonBinaryHostFactory() android.Module { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 97 | return NewBinary(android.HostSupported).init() |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 100 | func (p *PythonBinaryModule) init() android.Module { |
| 101 | p.AddProperties(&p.properties, &p.protoProperties) |
| 102 | p.AddProperties(&p.binaryProperties) |
| 103 | android.InitAndroidArchModule(p, p.hod, p.multilib) |
| 104 | android.InitDefaultableModule(p) |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 105 | return p |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 108 | func (p *PythonBinaryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 109 | p.PythonLibraryModule.GenerateAndroidBuildActions(ctx) |
| 110 | p.buildBinary(ctx) |
| 111 | p.installedDest = ctx.InstallFile(installDir(ctx, "bin", "", ""), |
| 112 | p.installSource.Base(), p.installSource) |
Yu Liu | e98f706 | 2025-01-17 22:52:43 +0000 | [diff] [blame] | 113 | |
| 114 | android.SetProvider(ctx, PythonBinaryInfoProvider, PythonBinaryInfo{}) |
| 115 | |
mrziwang | ca3ea1b | 2024-06-18 15:32:09 -0700 | [diff] [blame] | 116 | ctx.SetOutputFiles(android.Paths{p.installSource}, "") |
Bill Yang | 3257153 | 2025-02-18 09:52:38 +0000 | [diff] [blame^] | 117 | |
| 118 | moduleInfoJSON := ctx.ModuleInfoJSON() |
| 119 | moduleInfoJSON.Class = []string{"EXECUTABLES"} |
| 120 | moduleInfoJSON.Dependencies = append(moduleInfoJSON.Dependencies, p.androidMkSharedLibs...) |
| 121 | moduleInfoJSON.SharedLibs = append(moduleInfoJSON.SharedLibs, p.androidMkSharedLibs...) |
| 122 | moduleInfoJSON.SystemSharedLibs = []string{"none"} |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 125 | func (p *PythonBinaryModule) buildBinary(ctx android.ModuleContext) { |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 126 | embeddedLauncher := p.isEmbeddedLauncherEnabled() |
| 127 | depsSrcsZips := p.collectPathsFromTransitiveDeps(ctx, embeddedLauncher) |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 128 | main := "" |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 129 | if p.autorun() { |
| 130 | main = p.getPyMainFile(ctx, p.srcsPathMappings) |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 131 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 132 | |
Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 133 | var launcherPath android.OptionalPath |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 134 | if embeddedLauncher { |
Yu Liu | e98f706 | 2025-01-17 22:52:43 +0000 | [diff] [blame] | 135 | ctx.VisitDirectDepsProxyWithTag(launcherTag, func(m android.ModuleProxy) { |
| 136 | if provider, ok := android.OtherModuleProvider(ctx, m, cc.LinkableInfoProvider); ok { |
Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 137 | if launcherPath.Valid() { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 138 | panic(fmt.Errorf("launcher path was found before: %q", |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 139 | launcherPath)) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 140 | } |
Yu Liu | e98f706 | 2025-01-17 22:52:43 +0000 | [diff] [blame] | 141 | launcherPath = provider.OutputFile |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 142 | } |
| 143 | }) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 144 | } |
Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 145 | srcsZips := make(android.Paths, 0, len(depsSrcsZips)+1) |
| 146 | if embeddedLauncher { |
| 147 | srcsZips = append(srcsZips, p.precompiledSrcsZip) |
| 148 | } else { |
| 149 | srcsZips = append(srcsZips, p.srcsZip) |
| 150 | } |
| 151 | srcsZips = append(srcsZips, depsSrcsZips...) |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 152 | p.installSource = registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath, |
Cole Faust | d85c677 | 2025-01-31 10:54:46 -0800 | [diff] [blame] | 153 | "python3", main, p.getStem(ctx), srcsZips) |
Cole Faust | 909d237 | 2023-02-13 23:17:40 +0000 | [diff] [blame] | 154 | |
| 155 | var sharedLibs []string |
| 156 | // if embedded launcher is enabled, we need to collect the shared library dependencies of the |
| 157 | // launcher |
Yu Liu | e98f706 | 2025-01-17 22:52:43 +0000 | [diff] [blame] | 158 | for _, dep := range ctx.GetDirectDepsProxyWithTag(launcherSharedLibTag) { |
Cole Faust | 909d237 | 2023-02-13 23:17:40 +0000 | [diff] [blame] | 159 | sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep)) |
| 160 | } |
| 161 | p.androidMkSharedLibs = sharedLibs |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | func (p *PythonBinaryModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 165 | entries := android.AndroidMkEntries{OutputFile: android.OptionalPathForPath(p.installSource)} |
| 166 | |
| 167 | entries.Class = "EXECUTABLES" |
| 168 | |
| 169 | entries.ExtraEntries = append(entries.ExtraEntries, |
| 170 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 171 | entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...) |
| 172 | }) |
| 173 | |
| 174 | entries.Required = append(entries.Required, "libc++") |
| 175 | entries.ExtraEntries = append(entries.ExtraEntries, |
| 176 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 177 | path, file := filepath.Split(p.installedDest.String()) |
| 178 | stem := strings.TrimSuffix(file, filepath.Ext(file)) |
| 179 | |
| 180 | entries.SetString("LOCAL_MODULE_SUFFIX", filepath.Ext(file)) |
| 181 | entries.SetString("LOCAL_MODULE_PATH", path) |
| 182 | entries.SetString("LOCAL_MODULE_STEM", stem) |
| 183 | entries.AddStrings("LOCAL_SHARED_LIBRARIES", p.androidMkSharedLibs...) |
| 184 | entries.SetBool("LOCAL_CHECK_ELF_FILES", false) |
| 185 | }) |
| 186 | |
| 187 | return []android.AndroidMkEntries{entries} |
| 188 | } |
| 189 | |
| 190 | func (p *PythonBinaryModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 191 | p.PythonLibraryModule.DepsMutator(ctx) |
| 192 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 193 | if p.isEmbeddedLauncherEnabled() { |
Cole Faust | 909d237 | 2023-02-13 23:17:40 +0000 | [diff] [blame] | 194 | p.AddDepsOnPythonLauncherAndStdlib(ctx, pythonLibTag, launcherTag, launcherSharedLibTag, p.autorun(), ctx.Target()) |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | // HostToolPath returns a path if appropriate such that this module can be used as a host tool, |
| 199 | // fulfilling the android.HostToolProvider interface. |
| 200 | func (p *PythonBinaryModule) HostToolPath() android.OptionalPath { |
| 201 | // TODO: This should only be set when building host binaries -- tests built for device would be |
| 202 | // setting this incorrectly. |
| 203 | return android.OptionalPathForPath(p.installedDest) |
| 204 | } |
| 205 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 206 | func (p *PythonBinaryModule) isEmbeddedLauncherEnabled() bool { |
Cole Faust | f09101e | 2024-04-18 18:33:15 +0000 | [diff] [blame] | 207 | return BoolDefault(p.properties.Embedded_launcher, true) |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | func (b *PythonBinaryModule) autorun() bool { |
| 211 | return BoolDefault(b.binaryProperties.Autorun, true) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 214 | // find main program path within runfiles tree. |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 215 | func (p *PythonBinaryModule) getPyMainFile(ctx android.ModuleContext, |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 216 | srcsPathMappings []pathMapping) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 217 | var main string |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 218 | if String(p.binaryProperties.Main) == "" { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 219 | main = ctx.ModuleName() + pyExt |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 220 | } else { |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 221 | main = String(p.binaryProperties.Main) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 224 | for _, path := range srcsPathMappings { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 225 | if main == path.src.Rel() { |
| 226 | return path.dest |
| 227 | } |
| 228 | } |
| 229 | ctx.PropertyErrorf("main", "%q is not listed in srcs.", main) |
| 230 | |
| 231 | return "" |
| 232 | } |
| 233 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 234 | func (p *PythonBinaryModule) getStem(ctx android.ModuleContext) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 235 | stem := ctx.ModuleName() |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 236 | if String(p.binaryProperties.Stem) != "" { |
| 237 | stem = String(p.binaryProperties.Stem) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 240 | return stem + String(p.binaryProperties.Suffix) |
| 241 | } |
| 242 | |
| 243 | func installDir(ctx android.ModuleContext, dir, dir64, relative string) android.InstallPath { |
| 244 | if ctx.Arch().ArchType.Multilib == "lib64" && dir64 != "" { |
| 245 | dir = dir64 |
| 246 | } |
| 247 | if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) { |
| 248 | dir = filepath.Join(dir, ctx.Arch().ArchType.String()) |
| 249 | } |
| 250 | return android.PathForModuleInstall(ctx, dir, relative) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 251 | } |