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"` |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 50 | |
| 51 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml |
| 52 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true |
| 53 | // explicitly. |
| 54 | Auto_gen_config *bool |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 57 | type binaryDecorator struct { |
| 58 | binaryProperties BinaryProperties |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 59 | |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 60 | *pythonInstaller |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 63 | type IntermPathProvider interface { |
| 64 | IntermPathForModuleOut() android.OptionalPath |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 67 | var ( |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 68 | StubTemplateHost = "build/soong/python/scripts/stub_template_host.txt" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 69 | ) |
| 70 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 71 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
| 72 | module := newModule(hod, android.MultilibFirst) |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 73 | decorator := &binaryDecorator{pythonInstaller: NewPythonInstaller("bin", "")} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 74 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 75 | module.bootstrapper = decorator |
| 76 | module.installer = decorator |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 77 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 78 | return module, decorator |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 81 | func PythonBinaryHostFactory() android.Module { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 82 | module, _ := NewBinary(android.HostSupported) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 83 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 84 | return module.init() |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 87 | func (binary *binaryDecorator) autorun() bool { |
| 88 | return BoolDefault(binary.binaryProperties.Autorun, true) |
| 89 | } |
| 90 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 91 | func (binary *binaryDecorator) bootstrapperProps() []interface{} { |
| 92 | return []interface{}{&binary.binaryProperties} |
| 93 | } |
| 94 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 95 | func (binary *binaryDecorator) bootstrap(ctx android.ModuleContext, actualVersion string, |
| 96 | embeddedLauncher bool, srcsPathMappings []pathMapping, srcsZip android.Path, |
| 97 | depsSrcsZips android.Paths) android.OptionalPath { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 98 | |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 99 | main := "" |
| 100 | if binary.autorun() { |
| 101 | main = binary.getPyMainFile(ctx, srcsPathMappings) |
| 102 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 103 | |
Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 104 | var launcherPath android.OptionalPath |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 105 | if embeddedLauncher { |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 106 | ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 107 | if provider, ok := m.(IntermPathProvider); ok { |
Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 108 | if launcherPath.Valid() { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 109 | panic(fmt.Errorf("launcher path was found before: %q", |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 110 | launcherPath)) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 111 | } |
Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 112 | launcherPath = provider.IntermPathForModuleOut() |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 113 | } |
| 114 | }) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 117 | binFile := registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath, |
| 118 | binary.getHostInterpreterName(ctx, actualVersion), |
| 119 | main, binary.getStem(ctx), append(android.Paths{srcsZip}, depsSrcsZips...)) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 120 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 121 | return android.OptionalPathForPath(binFile) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 124 | // get host interpreter name. |
| 125 | func (binary *binaryDecorator) getHostInterpreterName(ctx android.ModuleContext, |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 126 | actualVersion string) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 127 | var interp string |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 128 | switch actualVersion { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 129 | case pyVersion2: |
Dan Willemsen | 7d1681a | 2017-09-25 13:47:40 -0700 | [diff] [blame] | 130 | interp = "python2.7" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 131 | case pyVersion3: |
| 132 | interp = "python3" |
| 133 | default: |
| 134 | panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.", |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 135 | actualVersion, ctx.ModuleName())) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | return interp |
| 139 | } |
| 140 | |
| 141 | // find main program path within runfiles tree. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 142 | func (binary *binaryDecorator) getPyMainFile(ctx android.ModuleContext, |
| 143 | srcsPathMappings []pathMapping) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 144 | var main string |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 145 | if String(binary.binaryProperties.Main) == "" { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 146 | main = ctx.ModuleName() + pyExt |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 147 | } else { |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 148 | main = String(binary.binaryProperties.Main) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 151 | for _, path := range srcsPathMappings { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 152 | if main == path.src.Rel() { |
| 153 | return path.dest |
| 154 | } |
| 155 | } |
| 156 | ctx.PropertyErrorf("main", "%q is not listed in srcs.", main) |
| 157 | |
| 158 | return "" |
| 159 | } |
| 160 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 161 | func (binary *binaryDecorator) getStem(ctx android.ModuleContext) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 162 | stem := ctx.ModuleName() |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 163 | if String(binary.binaryProperties.Stem) != "" { |
| 164 | stem = String(binary.binaryProperties.Stem) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 167 | return stem + String(binary.binaryProperties.Suffix) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 168 | } |