| 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" | 
|  | 25 | ) | 
|  | 26 |  | 
|  | 27 | func init() { | 
| Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 28 | registerPythonBinaryComponents(android.InitRegistrationContext) | 
| Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 29 | } | 
|  | 30 |  | 
| Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 31 | func registerPythonBinaryComponents(ctx android.RegistrationContext) { | 
|  | 32 | ctx.RegisterModuleType("python_binary_host", PythonBinaryHostFactory) | 
|  | 33 | } | 
|  | 34 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 35 | type BinaryProperties struct { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 36 | // 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 Faust | d82f036 | 2023-04-12 17:32:19 -0700 | [diff] [blame] | 40 | Main *string | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 41 |  | 
|  | 42 | // set the name of the output binary. | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 43 | Stem *string `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 44 |  | 
|  | 45 | // append to the name of the output binary. | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 46 | Suffix *string `android:"arch_variant"` | 
| Nan Zhang | c9c6cb7 | 2017-11-03 16:54:05 -0700 | [diff] [blame] | 47 |  | 
|  | 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 Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 51 |  | 
|  | 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 Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 56 |  | 
|  | 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 Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 61 | } | 
|  | 62 |  | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 63 | type PythonBinaryModule struct { | 
|  | 64 | PythonLibraryModule | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 65 | binaryProperties BinaryProperties | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 66 |  | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 67 | // (.intermediate) module output path as installation source. | 
|  | 68 | installSource android.Path | 
|  | 69 |  | 
|  | 70 | // Final installation path. | 
|  | 71 | installedDest android.Path | 
|  | 72 |  | 
|  | 73 | androidMkSharedLibs []string | 
| LaMont Jones | afe7baf | 2024-01-09 22:47:39 +0000 | [diff] [blame] | 74 |  | 
|  | 75 | // Aconfig files for all transitive deps.  Also exposed via TransitiveDeclarationsInfo | 
|  | 76 | mergedAconfigFiles map[string]android.Paths | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 79 | var _ android.AndroidMkEntriesProvider = (*PythonBinaryModule)(nil) | 
|  | 80 | var _ android.Module = (*PythonBinaryModule)(nil) | 
|  | 81 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 82 | type IntermPathProvider interface { | 
|  | 83 | IntermPathForModuleOut() android.OptionalPath | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 86 | func NewBinary(hod android.HostOrDeviceSupported) *PythonBinaryModule { | 
|  | 87 | return &PythonBinaryModule{ | 
|  | 88 | PythonLibraryModule: *newModule(hod, android.MultilibFirst), | 
|  | 89 | } | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 92 | func PythonBinaryHostFactory() android.Module { | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 93 | return NewBinary(android.HostSupported).init() | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 96 | func (p *PythonBinaryModule) init() android.Module { | 
|  | 97 | p.AddProperties(&p.properties, &p.protoProperties) | 
|  | 98 | p.AddProperties(&p.binaryProperties) | 
|  | 99 | android.InitAndroidArchModule(p, p.hod, p.multilib) | 
|  | 100 | android.InitDefaultableModule(p) | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 101 | return p | 
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 104 | func (p *PythonBinaryModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 105 | p.PythonLibraryModule.GenerateAndroidBuildActions(ctx) | 
|  | 106 | p.buildBinary(ctx) | 
|  | 107 | p.installedDest = ctx.InstallFile(installDir(ctx, "bin", "", ""), | 
|  | 108 | p.installSource.Base(), p.installSource) | 
| LaMont Jones | afe7baf | 2024-01-09 22:47:39 +0000 | [diff] [blame] | 109 | android.CollectDependencyAconfigFiles(ctx, &p.mergedAconfigFiles) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 112 | func (p *PythonBinaryModule) buildBinary(ctx android.ModuleContext) { | 
| Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 113 | embeddedLauncher := p.isEmbeddedLauncherEnabled() | 
|  | 114 | depsSrcsZips := p.collectPathsFromTransitiveDeps(ctx, embeddedLauncher) | 
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 115 | main := "" | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 116 | if p.autorun() { | 
|  | 117 | main = p.getPyMainFile(ctx, p.srcsPathMappings) | 
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 118 | } | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 119 |  | 
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 120 | var launcherPath android.OptionalPath | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 121 | if embeddedLauncher { | 
| Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 122 | ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 123 | if provider, ok := m.(IntermPathProvider); ok { | 
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 124 | if launcherPath.Valid() { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 125 | panic(fmt.Errorf("launcher path was found before: %q", | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 126 | launcherPath)) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 127 | } | 
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 128 | launcherPath = provider.IntermPathForModuleOut() | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 129 | } | 
|  | 130 | }) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 131 | } | 
| Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 132 | srcsZips := make(android.Paths, 0, len(depsSrcsZips)+1) | 
|  | 133 | if embeddedLauncher { | 
|  | 134 | srcsZips = append(srcsZips, p.precompiledSrcsZip) | 
|  | 135 | } else { | 
|  | 136 | srcsZips = append(srcsZips, p.srcsZip) | 
|  | 137 | } | 
|  | 138 | srcsZips = append(srcsZips, depsSrcsZips...) | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 139 | p.installSource = registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath, | 
|  | 140 | p.getHostInterpreterName(ctx, p.properties.Actual_version), | 
| Cole Faust | 5c503d1 | 2023-01-24 11:48:08 -0800 | [diff] [blame] | 141 | main, p.getStem(ctx), srcsZips) | 
| Cole Faust | 909d237 | 2023-02-13 23:17:40 +0000 | [diff] [blame] | 142 |  | 
|  | 143 | var sharedLibs []string | 
|  | 144 | // if embedded launcher is enabled, we need to collect the shared library dependencies of the | 
|  | 145 | // launcher | 
|  | 146 | for _, dep := range ctx.GetDirectDepsWithTag(launcherSharedLibTag) { | 
|  | 147 | sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep)) | 
|  | 148 | } | 
|  | 149 | p.androidMkSharedLibs = sharedLibs | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
|  | 152 | func (p *PythonBinaryModule) AndroidMkEntries() []android.AndroidMkEntries { | 
|  | 153 | entries := android.AndroidMkEntries{OutputFile: android.OptionalPathForPath(p.installSource)} | 
|  | 154 |  | 
|  | 155 | entries.Class = "EXECUTABLES" | 
|  | 156 |  | 
|  | 157 | entries.ExtraEntries = append(entries.ExtraEntries, | 
|  | 158 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { | 
|  | 159 | entries.AddCompatibilityTestSuites(p.binaryProperties.Test_suites...) | 
|  | 160 | }) | 
|  | 161 |  | 
|  | 162 | entries.Required = append(entries.Required, "libc++") | 
|  | 163 | entries.ExtraEntries = append(entries.ExtraEntries, | 
|  | 164 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { | 
|  | 165 | path, file := filepath.Split(p.installedDest.String()) | 
|  | 166 | stem := strings.TrimSuffix(file, filepath.Ext(file)) | 
|  | 167 |  | 
|  | 168 | entries.SetString("LOCAL_MODULE_SUFFIX", filepath.Ext(file)) | 
|  | 169 | entries.SetString("LOCAL_MODULE_PATH", path) | 
|  | 170 | entries.SetString("LOCAL_MODULE_STEM", stem) | 
|  | 171 | entries.AddStrings("LOCAL_SHARED_LIBRARIES", p.androidMkSharedLibs...) | 
|  | 172 | entries.SetBool("LOCAL_CHECK_ELF_FILES", false) | 
| LaMont Jones | afe7baf | 2024-01-09 22:47:39 +0000 | [diff] [blame] | 173 | android.SetAconfigFileMkEntries(&p.ModuleBase, entries, p.mergedAconfigFiles) | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 174 | }) | 
|  | 175 |  | 
|  | 176 | return []android.AndroidMkEntries{entries} | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | func (p *PythonBinaryModule) DepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 180 | p.PythonLibraryModule.DepsMutator(ctx) | 
|  | 181 |  | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 182 | if p.isEmbeddedLauncherEnabled() { | 
| Cole Faust | 909d237 | 2023-02-13 23:17:40 +0000 | [diff] [blame] | 183 | p.AddDepsOnPythonLauncherAndStdlib(ctx, pythonLibTag, launcherTag, launcherSharedLibTag, p.autorun(), ctx.Target()) | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 184 | } | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | // HostToolPath returns a path if appropriate such that this module can be used as a host tool, | 
|  | 188 | // fulfilling the android.HostToolProvider interface. | 
|  | 189 | func (p *PythonBinaryModule) HostToolPath() android.OptionalPath { | 
|  | 190 | // TODO: This should only be set when building host binaries -- tests built for device would be | 
|  | 191 | // setting this incorrectly. | 
|  | 192 | return android.OptionalPathForPath(p.installedDest) | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | // OutputFiles returns output files based on given tag, returns an error if tag is unsupported. | 
|  | 196 | func (p *PythonBinaryModule) OutputFiles(tag string) (android.Paths, error) { | 
|  | 197 | switch tag { | 
|  | 198 | case "": | 
|  | 199 | return android.Paths{p.installSource}, nil | 
|  | 200 | default: | 
|  | 201 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) | 
|  | 202 | } | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | func (p *PythonBinaryModule) isEmbeddedLauncherEnabled() bool { | 
| Charlie Boutier | 578066b | 2024-04-11 16:10:00 +0000 | [diff] [blame] | 206 | return Bool(p.properties.Embedded_launcher) | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
|  | 209 | func (b *PythonBinaryModule) autorun() bool { | 
|  | 210 | return BoolDefault(b.binaryProperties.Autorun, true) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 211 | } | 
|  | 212 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 213 | // get host interpreter name. | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 214 | func (p *PythonBinaryModule) getHostInterpreterName(ctx android.ModuleContext, | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 215 | actualVersion string) string { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 216 | var interp string | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 217 | switch actualVersion { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 218 | case pyVersion2: | 
| Dan Willemsen | 7d1681a | 2017-09-25 13:47:40 -0700 | [diff] [blame] | 219 | interp = "python2.7" | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 220 | case pyVersion3: | 
|  | 221 | interp = "python3" | 
|  | 222 | default: | 
|  | 223 | panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.", | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 224 | actualVersion, ctx.ModuleName())) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
|  | 227 | return interp | 
|  | 228 | } | 
|  | 229 |  | 
|  | 230 | // find main program path within runfiles tree. | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 231 | func (p *PythonBinaryModule) getPyMainFile(ctx android.ModuleContext, | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 232 | srcsPathMappings []pathMapping) string { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 233 | var main string | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 234 | if String(p.binaryProperties.Main) == "" { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 235 | main = ctx.ModuleName() + pyExt | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 236 | } else { | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 237 | main = String(p.binaryProperties.Main) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 238 | } | 
|  | 239 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 240 | for _, path := range srcsPathMappings { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 241 | if main == path.src.Rel() { | 
|  | 242 | return path.dest | 
|  | 243 | } | 
|  | 244 | } | 
|  | 245 | ctx.PropertyErrorf("main", "%q is not listed in srcs.", main) | 
|  | 246 |  | 
|  | 247 | return "" | 
|  | 248 | } | 
|  | 249 |  | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 250 | func (p *PythonBinaryModule) getStem(ctx android.ModuleContext) string { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 251 | stem := ctx.ModuleName() | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 252 | if String(p.binaryProperties.Stem) != "" { | 
|  | 253 | stem = String(p.binaryProperties.Stem) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 254 | } | 
|  | 255 |  | 
| Cole Faust | 4d247e6 | 2023-01-23 10:14:58 -0800 | [diff] [blame] | 256 | return stem + String(p.binaryProperties.Suffix) | 
|  | 257 | } | 
|  | 258 |  | 
|  | 259 | func installDir(ctx android.ModuleContext, dir, dir64, relative string) android.InstallPath { | 
|  | 260 | if ctx.Arch().ArchType.Multilib == "lib64" && dir64 != "" { | 
|  | 261 | dir = dir64 | 
|  | 262 | } | 
|  | 263 | if !ctx.Host() && ctx.Config().HasMultilibConflict(ctx.Arch().ArchType) { | 
|  | 264 | dir = filepath.Join(dir, ctx.Arch().ArchType.String()) | 
|  | 265 | } | 
|  | 266 | return android.PathForModuleInstall(ctx, dir, relative) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 267 | } |