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" |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 23 | "android/soong/bazel" |
| 24 | |
| 25 | "github.com/google/blueprint/proptools" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | func init() { |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 29 | registerPythonBinaryComponents(android.InitRegistrationContext) |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 32 | func registerPythonBinaryComponents(ctx android.RegistrationContext) { |
| 33 | ctx.RegisterModuleType("python_binary_host", PythonBinaryHostFactory) |
| 34 | } |
| 35 | |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 36 | type bazelPythonBinaryAttributes struct { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 37 | Main *string |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 38 | Srcs bazel.LabelListAttribute |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 39 | Deps bazel.LabelListAttribute |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 40 | Python_version *string |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame^] | 41 | Imports bazel.StringListAttribute |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 44 | func pythonBinaryBp2Build(ctx android.TopDownMutatorContext, m *Module) { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 45 | var main *string |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 46 | for _, propIntf := range m.GetProperties() { |
| 47 | if props, ok := propIntf.(*BinaryProperties); ok { |
| 48 | // main is optional. |
| 49 | if props.Main != nil { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 50 | main = props.Main |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 51 | break |
| 52 | } |
| 53 | } |
| 54 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 55 | |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 56 | // TODO(b/182306917): this doesn't fully handle all nested props versioned |
| 57 | // by the python version, which would have been handled by the version split |
| 58 | // mutator. This is sufficient for very simple python_binary_host modules |
| 59 | // under Bionic. |
| 60 | py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, false) |
| 61 | py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false) |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 62 | var python_version *string |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 63 | if py3Enabled && py2Enabled { |
| 64 | panic(fmt.Errorf( |
| 65 | "error for '%s' module: bp2build's python_binary_host converter does not support "+ |
| 66 | "converting a module that is enabled for both Python 2 and 3 at the same time.", m.Name())) |
| 67 | } else if py2Enabled { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 68 | python_version = &pyVersion2 |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 69 | } else { |
| 70 | // do nothing, since python_version defaults to PY3. |
| 71 | } |
| 72 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 73 | baseAttrs := m.makeArchVariantBaseAttributes(ctx) |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 74 | attrs := &bazelPythonBinaryAttributes{ |
| 75 | Main: main, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 76 | Srcs: baseAttrs.Srcs, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 77 | Deps: baseAttrs.Deps, |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 78 | Python_version: python_version, |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame^] | 79 | Imports: baseAttrs.Imports, |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | props := bazel.BazelTargetModuleProperties{ |
| 83 | // Use the native py_binary rule. |
| 84 | Rule_class: "py_binary", |
| 85 | } |
| 86 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 87 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{ |
| 88 | Name: m.Name(), |
| 89 | Data: baseAttrs.Data, |
| 90 | }, attrs) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 93 | type BinaryProperties struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 94 | // the name of the source file that is the main entry point of the program. |
| 95 | // this file must also be listed in srcs. |
| 96 | // If left unspecified, module name is used instead. |
| 97 | // 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] | 98 | Main *string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 99 | |
| 100 | // set the name of the output binary. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 101 | Stem *string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 102 | |
| 103 | // append to the name of the output binary. |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 104 | Suffix *string `android:"arch_variant"` |
Nan Zhang | c9c6cb7 | 2017-11-03 16:54:05 -0700 | [diff] [blame] | 105 | |
| 106 | // list of compatibility suites (for example "cts", "vts") that the module should be |
| 107 | // installed into. |
| 108 | Test_suites []string `android:"arch_variant"` |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 109 | |
| 110 | // whether to use `main` when starting the executable. The default is true, when set to |
| 111 | // false it will act much like the normal `python` executable, but with the sources and |
| 112 | // libraries automatically included in the PYTHONPATH. |
| 113 | Autorun *bool `android:"arch_variant"` |
Dan Shi | 6ffaaa8 | 2019-09-26 11:41:36 -0700 | [diff] [blame] | 114 | |
| 115 | // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml |
| 116 | // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true |
| 117 | // explicitly. |
| 118 | Auto_gen_config *bool |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 121 | type binaryDecorator struct { |
| 122 | binaryProperties BinaryProperties |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 123 | |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 124 | *pythonInstaller |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 127 | type IntermPathProvider interface { |
| 128 | IntermPathForModuleOut() android.OptionalPath |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 131 | var ( |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 132 | StubTemplateHost = "build/soong/python/scripts/stub_template_host.txt" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 133 | ) |
| 134 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 135 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
| 136 | module := newModule(hod, android.MultilibFirst) |
Rupert Shuttleworth | ffc4cc4 | 2021-11-03 09:07:26 +0000 | [diff] [blame] | 137 | decorator := &binaryDecorator{pythonInstaller: NewPythonInstaller("bin", "")} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 138 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 139 | module.bootstrapper = decorator |
| 140 | module.installer = decorator |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 141 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 142 | return module, decorator |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 145 | func PythonBinaryHostFactory() android.Module { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 146 | module, _ := NewBinary(android.HostSupported) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 147 | |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 148 | android.InitBazelModule(module) |
| 149 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 150 | return module.init() |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 153 | func (binary *binaryDecorator) autorun() bool { |
| 154 | return BoolDefault(binary.binaryProperties.Autorun, true) |
| 155 | } |
| 156 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 157 | func (binary *binaryDecorator) bootstrapperProps() []interface{} { |
| 158 | return []interface{}{&binary.binaryProperties} |
| 159 | } |
| 160 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 161 | func (binary *binaryDecorator) bootstrap(ctx android.ModuleContext, actualVersion string, |
| 162 | embeddedLauncher bool, srcsPathMappings []pathMapping, srcsZip android.Path, |
| 163 | depsSrcsZips android.Paths) android.OptionalPath { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 164 | |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 165 | main := "" |
| 166 | if binary.autorun() { |
| 167 | main = binary.getPyMainFile(ctx, srcsPathMappings) |
| 168 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 169 | |
Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 170 | var launcherPath android.OptionalPath |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 171 | if embeddedLauncher { |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 172 | ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 173 | if provider, ok := m.(IntermPathProvider); ok { |
Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 174 | if launcherPath.Valid() { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 175 | panic(fmt.Errorf("launcher path was found before: %q", |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 176 | launcherPath)) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 177 | } |
Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 178 | launcherPath = provider.IntermPathForModuleOut() |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 179 | } |
| 180 | }) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 183 | binFile := registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath, |
| 184 | binary.getHostInterpreterName(ctx, actualVersion), |
| 185 | main, binary.getStem(ctx), append(android.Paths{srcsZip}, depsSrcsZips...)) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 186 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 187 | return android.OptionalPathForPath(binFile) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 190 | // get host interpreter name. |
| 191 | func (binary *binaryDecorator) getHostInterpreterName(ctx android.ModuleContext, |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 192 | actualVersion string) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 193 | var interp string |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 194 | switch actualVersion { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 195 | case pyVersion2: |
Dan Willemsen | 7d1681a | 2017-09-25 13:47:40 -0700 | [diff] [blame] | 196 | interp = "python2.7" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 197 | case pyVersion3: |
| 198 | interp = "python3" |
| 199 | default: |
| 200 | panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.", |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 201 | actualVersion, ctx.ModuleName())) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | return interp |
| 205 | } |
| 206 | |
| 207 | // find main program path within runfiles tree. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 208 | func (binary *binaryDecorator) getPyMainFile(ctx android.ModuleContext, |
| 209 | srcsPathMappings []pathMapping) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 210 | var main string |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 211 | if String(binary.binaryProperties.Main) == "" { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 212 | main = ctx.ModuleName() + pyExt |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 213 | } else { |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 214 | main = String(binary.binaryProperties.Main) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 217 | for _, path := range srcsPathMappings { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 218 | if main == path.src.Rel() { |
| 219 | return path.dest |
| 220 | } |
| 221 | } |
| 222 | ctx.PropertyErrorf("main", "%q is not listed in srcs.", main) |
| 223 | |
| 224 | return "" |
| 225 | } |
| 226 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 227 | func (binary *binaryDecorator) getStem(ctx android.ModuleContext) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 228 | stem := ctx.ModuleName() |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 229 | if String(binary.binaryProperties.Stem) != "" { |
| 230 | stem = String(binary.binaryProperties.Stem) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 231 | } |
| 232 | |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 233 | return stem + String(binary.binaryProperties.Suffix) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 234 | } |