| 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" | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 21 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 22 | "android/soong/android" | 
|  | 23 | ) | 
|  | 24 |  | 
|  | 25 | func init() { | 
|  | 26 | android.RegisterModuleType("python_binary_host", PythonBinaryHostFactory) | 
|  | 27 | } | 
|  | 28 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 29 | type BinaryProperties struct { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 30 | // the name of the source file that is the main entry point of the program. | 
|  | 31 | // this file must also be listed in srcs. | 
|  | 32 | // If left unspecified, module name is used instead. | 
|  | 33 | // If name doesn’t match any filename in srcs, main must be specified. | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 34 | Main *string `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 35 |  | 
|  | 36 | // set the name of the output binary. | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 37 | Stem *string `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | // append to the name of the output binary. | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 40 | Suffix *string `android:"arch_variant"` | 
| Nan Zhang | c9c6cb7 | 2017-11-03 16:54:05 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | // list of compatibility suites (for example "cts", "vts") that the module should be | 
|  | 43 | // installed into. | 
|  | 44 | Test_suites []string `android:"arch_variant"` | 
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 45 |  | 
|  | 46 | // whether to use `main` when starting the executable. The default is true, when set to | 
|  | 47 | // false it will act much like the normal `python` executable, but with the sources and | 
|  | 48 | // libraries automatically included in the PYTHONPATH. | 
|  | 49 | Autorun *bool `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 50 | } | 
|  | 51 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 52 | type binaryDecorator struct { | 
|  | 53 | binaryProperties BinaryProperties | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 54 |  | 
| Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 55 | *pythonInstaller | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 56 | } | 
|  | 57 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 58 | type IntermPathProvider interface { | 
|  | 59 | IntermPathForModuleOut() android.OptionalPath | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 62 | var ( | 
|  | 63 | stubTemplateHost = "build/soong/python/scripts/stub_template_host.txt" | 
|  | 64 | ) | 
|  | 65 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 66 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { | 
|  | 67 | module := newModule(hod, android.MultilibFirst) | 
| Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 68 | decorator := &binaryDecorator{pythonInstaller: NewPythonInstaller("bin", "")} | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 69 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 70 | module.bootstrapper = decorator | 
|  | 71 | module.installer = decorator | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 72 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 73 | return module, decorator | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 76 | func PythonBinaryHostFactory() android.Module { | 
|  | 77 | module, _ := NewBinary(android.HostSupportedNoCross) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 78 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 79 | return module.Init() | 
|  | 80 | } | 
|  | 81 |  | 
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 82 | func (binary *binaryDecorator) autorun() bool { | 
|  | 83 | return BoolDefault(binary.binaryProperties.Autorun, true) | 
|  | 84 | } | 
|  | 85 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 86 | func (binary *binaryDecorator) bootstrapperProps() []interface{} { | 
|  | 87 | return []interface{}{&binary.binaryProperties} | 
|  | 88 | } | 
|  | 89 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 90 | func (binary *binaryDecorator) bootstrap(ctx android.ModuleContext, actualVersion string, | 
|  | 91 | embeddedLauncher bool, srcsPathMappings []pathMapping, srcsZip android.Path, | 
|  | 92 | depsSrcsZips android.Paths) android.OptionalPath { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 93 |  | 
| Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 94 | main := "" | 
|  | 95 | if binary.autorun() { | 
|  | 96 | main = binary.getPyMainFile(ctx, srcsPathMappings) | 
|  | 97 | } | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 98 |  | 
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 99 | var launcherPath android.OptionalPath | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 100 | if embeddedLauncher { | 
| Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 101 | ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 102 | if provider, ok := m.(IntermPathProvider); ok { | 
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 103 | if launcherPath.Valid() { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 104 | panic(fmt.Errorf("launcher path was found before: %q", | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 105 | launcherPath)) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 106 | } | 
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 107 | launcherPath = provider.IntermPathForModuleOut() | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 108 | } | 
|  | 109 | }) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 112 | binFile := registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath, | 
|  | 113 | binary.getHostInterpreterName(ctx, actualVersion), | 
|  | 114 | main, binary.getStem(ctx), append(android.Paths{srcsZip}, depsSrcsZips...)) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 115 |  | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 116 | return android.OptionalPathForPath(binFile) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 119 | // get host interpreter name. | 
|  | 120 | func (binary *binaryDecorator) getHostInterpreterName(ctx android.ModuleContext, | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 121 | actualVersion string) string { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 122 | var interp string | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 123 | switch actualVersion { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 124 | case pyVersion2: | 
| Dan Willemsen | 7d1681a | 2017-09-25 13:47:40 -0700 | [diff] [blame] | 125 | interp = "python2.7" | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 126 | case pyVersion3: | 
|  | 127 | interp = "python3" | 
|  | 128 | default: | 
|  | 129 | panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.", | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 130 | actualVersion, ctx.ModuleName())) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
|  | 133 | return interp | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | // find main program path within runfiles tree. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 137 | func (binary *binaryDecorator) getPyMainFile(ctx android.ModuleContext, | 
|  | 138 | srcsPathMappings []pathMapping) string { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 139 | var main string | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 140 | if String(binary.binaryProperties.Main) == "" { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 141 | main = ctx.ModuleName() + pyExt | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 142 | } else { | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 143 | main = String(binary.binaryProperties.Main) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 144 | } | 
|  | 145 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 146 | for _, path := range srcsPathMappings { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 147 | if main == path.src.Rel() { | 
|  | 148 | return path.dest | 
|  | 149 | } | 
|  | 150 | } | 
|  | 151 | ctx.PropertyErrorf("main", "%q is not listed in srcs.", main) | 
|  | 152 |  | 
|  | 153 | return "" | 
|  | 154 | } | 
|  | 155 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 156 | func (binary *binaryDecorator) getStem(ctx android.ModuleContext) string { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 157 | stem := ctx.ModuleName() | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 158 | if String(binary.binaryProperties.Stem) != "" { | 
|  | 159 | stem = String(binary.binaryProperties.Stem) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 160 | } | 
|  | 161 |  | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 162 | return stem + String(binary.binaryProperties.Suffix) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 163 | } |