blob: 99c62591650838705bd624b42e1c878de2abf16b [file] [log] [blame]
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001// 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
15package python
16
17// This file contains the module types for building Python binary.
18
19import (
20 "fmt"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080021
Nan Zhangdb0b9a32017-02-27 10:12:13 -080022 "android/soong/android"
Jingwen Chen13b9b422021-03-08 07:32:28 -050023 "android/soong/bazel"
24
25 "github.com/google/blueprint/proptools"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080026)
27
28func init() {
Paul Duffind0890452021-03-17 21:57:08 +000029 registerPythonBinaryComponents(android.InitRegistrationContext)
Jingwen Chen13b9b422021-03-08 07:32:28 -050030}
31
Paul Duffind0890452021-03-17 21:57:08 +000032func registerPythonBinaryComponents(ctx android.RegistrationContext) {
33 ctx.RegisterModuleType("python_binary_host", PythonBinaryHostFactory)
34}
35
Jingwen Chen13b9b422021-03-08 07:32:28 -050036type bazelPythonBinaryAttributes struct {
Liz Kammer46fb7ab2021-12-01 10:09:34 -050037 Main *string
Jingwen Chen07027912021-03-15 06:02:43 -040038 Srcs bazel.LabelListAttribute
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000039 Deps bazel.LabelListAttribute
Liz Kammer46fb7ab2021-12-01 10:09:34 -050040 Python_version *string
Jingwen Chen13b9b422021-03-08 07:32:28 -050041}
42
Liz Kammerbe46fcc2021-11-01 15:32:43 -040043func pythonBinaryBp2Build(ctx android.TopDownMutatorContext, m *Module) {
Liz Kammer46fb7ab2021-12-01 10:09:34 -050044 var main *string
Jingwen Chen13b9b422021-03-08 07:32:28 -050045 for _, propIntf := range m.GetProperties() {
46 if props, ok := propIntf.(*BinaryProperties); ok {
47 // main is optional.
48 if props.Main != nil {
Liz Kammer46fb7ab2021-12-01 10:09:34 -050049 main = props.Main
Jingwen Chen13b9b422021-03-08 07:32:28 -050050 break
51 }
52 }
53 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +000054
Jingwen Chen13b9b422021-03-08 07:32:28 -050055 // TODO(b/182306917): this doesn't fully handle all nested props versioned
56 // by the python version, which would have been handled by the version split
57 // mutator. This is sufficient for very simple python_binary_host modules
58 // under Bionic.
59 py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, false)
60 py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
Liz Kammer46fb7ab2021-12-01 10:09:34 -050061 var python_version *string
Jingwen Chen13b9b422021-03-08 07:32:28 -050062 if py3Enabled && py2Enabled {
63 panic(fmt.Errorf(
64 "error for '%s' module: bp2build's python_binary_host converter does not support "+
65 "converting a module that is enabled for both Python 2 and 3 at the same time.", m.Name()))
66 } else if py2Enabled {
Liz Kammer46fb7ab2021-12-01 10:09:34 -050067 python_version = &pyVersion2
Jingwen Chen13b9b422021-03-08 07:32:28 -050068 } else {
69 // do nothing, since python_version defaults to PY3.
70 }
71
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +000072 baseAttrs := m.makeArchVariantBaseAttributes(ctx)
Jingwen Chen13b9b422021-03-08 07:32:28 -050073 attrs := &bazelPythonBinaryAttributes{
74 Main: main,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +000075 Srcs: baseAttrs.Srcs,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +000076 Deps: baseAttrs.Deps,
Jingwen Chen13b9b422021-03-08 07:32:28 -050077 Python_version: python_version,
78 }
79
80 props := bazel.BazelTargetModuleProperties{
81 // Use the native py_binary rule.
82 Rule_class: "py_binary",
83 }
84
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +000085 ctx.CreateBazelTargetModule(props, android.CommonAttributes{
86 Name: m.Name(),
87 Data: baseAttrs.Data,
88 }, attrs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080089}
90
Nan Zhangd4e641b2017-07-12 12:55:28 -070091type BinaryProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080092 // the name of the source file that is the main entry point of the program.
93 // this file must also be listed in srcs.
94 // If left unspecified, module name is used instead.
95 // If name doesn’t match any filename in srcs, main must be specified.
Nan Zhangea568a42017-11-08 21:20:04 -080096 Main *string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080097
98 // set the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -080099 Stem *string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800100
101 // append to the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -0800102 Suffix *string `android:"arch_variant"`
Nan Zhangc9c6cb72017-11-03 16:54:05 -0700103
104 // list of compatibility suites (for example "cts", "vts") that the module should be
105 // installed into.
106 Test_suites []string `android:"arch_variant"`
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800107
108 // whether to use `main` when starting the executable. The default is true, when set to
109 // false it will act much like the normal `python` executable, but with the sources and
110 // libraries automatically included in the PYTHONPATH.
111 Autorun *bool `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700112
113 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
114 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
115 // explicitly.
116 Auto_gen_config *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800117}
118
Nan Zhangd4e641b2017-07-12 12:55:28 -0700119type binaryDecorator struct {
120 binaryProperties BinaryProperties
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800121
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000122 *pythonInstaller
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800123}
124
Nan Zhangd4e641b2017-07-12 12:55:28 -0700125type IntermPathProvider interface {
126 IntermPathForModuleOut() android.OptionalPath
Nan Zhang5323f8e2017-05-10 13:37:54 -0700127}
128
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800129var (
Liz Kammerdd849a82020-06-12 16:38:45 -0700130 StubTemplateHost = "build/soong/python/scripts/stub_template_host.txt"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800131)
132
Nan Zhangd4e641b2017-07-12 12:55:28 -0700133func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
134 module := newModule(hod, android.MultilibFirst)
Rupert Shuttleworthffc4cc42021-11-03 09:07:26 +0000135 decorator := &binaryDecorator{pythonInstaller: NewPythonInstaller("bin", "")}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800136
Nan Zhangd4e641b2017-07-12 12:55:28 -0700137 module.bootstrapper = decorator
138 module.installer = decorator
Nan Zhang5323f8e2017-05-10 13:37:54 -0700139
Nan Zhangd4e641b2017-07-12 12:55:28 -0700140 return module, decorator
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800141}
142
Nan Zhangd4e641b2017-07-12 12:55:28 -0700143func PythonBinaryHostFactory() android.Module {
Jiyong Park1613e552020-09-14 19:43:17 +0900144 module, _ := NewBinary(android.HostSupported)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800145
Jingwen Chen13b9b422021-03-08 07:32:28 -0500146 android.InitBazelModule(module)
147
Liz Kammerd737d022020-11-16 15:42:51 -0800148 return module.init()
Nan Zhangd4e641b2017-07-12 12:55:28 -0700149}
150
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800151func (binary *binaryDecorator) autorun() bool {
152 return BoolDefault(binary.binaryProperties.Autorun, true)
153}
154
Nan Zhangd4e641b2017-07-12 12:55:28 -0700155func (binary *binaryDecorator) bootstrapperProps() []interface{} {
156 return []interface{}{&binary.binaryProperties}
157}
158
Nan Zhang1db85402017-12-18 13:20:23 -0800159func (binary *binaryDecorator) bootstrap(ctx android.ModuleContext, actualVersion string,
160 embeddedLauncher bool, srcsPathMappings []pathMapping, srcsZip android.Path,
161 depsSrcsZips android.Paths) android.OptionalPath {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800162
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800163 main := ""
164 if binary.autorun() {
165 main = binary.getPyMainFile(ctx, srcsPathMappings)
166 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700167
Nan Zhangcba97e62018-09-26 15:14:10 -0700168 var launcherPath android.OptionalPath
Nan Zhang1db85402017-12-18 13:20:23 -0800169 if embeddedLauncher {
Colin Crossee6143c2017-12-30 17:54:27 -0800170 ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700171 if provider, ok := m.(IntermPathProvider); ok {
Nan Zhangcba97e62018-09-26 15:14:10 -0700172 if launcherPath.Valid() {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700173 panic(fmt.Errorf("launcher path was found before: %q",
Nan Zhang1db85402017-12-18 13:20:23 -0800174 launcherPath))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700175 }
Nan Zhangcba97e62018-09-26 15:14:10 -0700176 launcherPath = provider.IntermPathForModuleOut()
Nan Zhangd4e641b2017-07-12 12:55:28 -0700177 }
178 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800179 }
180
Nan Zhang1db85402017-12-18 13:20:23 -0800181 binFile := registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath,
182 binary.getHostInterpreterName(ctx, actualVersion),
183 main, binary.getStem(ctx), append(android.Paths{srcsZip}, depsSrcsZips...))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800184
Nan Zhang5323f8e2017-05-10 13:37:54 -0700185 return android.OptionalPathForPath(binFile)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800186}
187
Nan Zhangd4e641b2017-07-12 12:55:28 -0700188// get host interpreter name.
189func (binary *binaryDecorator) getHostInterpreterName(ctx android.ModuleContext,
Nan Zhang1db85402017-12-18 13:20:23 -0800190 actualVersion string) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800191 var interp string
Nan Zhang1db85402017-12-18 13:20:23 -0800192 switch actualVersion {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800193 case pyVersion2:
Dan Willemsen7d1681a2017-09-25 13:47:40 -0700194 interp = "python2.7"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800195 case pyVersion3:
196 interp = "python3"
197 default:
198 panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.",
Nan Zhang1db85402017-12-18 13:20:23 -0800199 actualVersion, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800200 }
201
202 return interp
203}
204
205// find main program path within runfiles tree.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700206func (binary *binaryDecorator) getPyMainFile(ctx android.ModuleContext,
207 srcsPathMappings []pathMapping) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800208 var main string
Nan Zhangea568a42017-11-08 21:20:04 -0800209 if String(binary.binaryProperties.Main) == "" {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700210 main = ctx.ModuleName() + pyExt
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800211 } else {
Nan Zhangea568a42017-11-08 21:20:04 -0800212 main = String(binary.binaryProperties.Main)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800213 }
214
Nan Zhangd4e641b2017-07-12 12:55:28 -0700215 for _, path := range srcsPathMappings {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800216 if main == path.src.Rel() {
217 return path.dest
218 }
219 }
220 ctx.PropertyErrorf("main", "%q is not listed in srcs.", main)
221
222 return ""
223}
224
Nan Zhangd4e641b2017-07-12 12:55:28 -0700225func (binary *binaryDecorator) getStem(ctx android.ModuleContext) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800226 stem := ctx.ModuleName()
Nan Zhangea568a42017-11-08 21:20:04 -0800227 if String(binary.binaryProperties.Stem) != "" {
228 stem = String(binary.binaryProperties.Stem)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800229 }
230
Nan Zhangea568a42017-11-08 21:20:04 -0800231 return stem + String(binary.binaryProperties.Suffix)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800232}