blob: afcc53a651bcb5a187f1fef1529efda4d7ed6bd8 [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 android.RegisterBp2BuildMutator("python_binary_host", PythonBinaryBp2Build)
31}
32
Paul Duffind0890452021-03-17 21:57:08 +000033func registerPythonBinaryComponents(ctx android.RegistrationContext) {
34 ctx.RegisterModuleType("python_binary_host", PythonBinaryHostFactory)
35}
36
Jingwen Chen13b9b422021-03-08 07:32:28 -050037type bazelPythonBinaryAttributes struct {
38 Main string
Jingwen Chen07027912021-03-15 06:02:43 -040039 Srcs bazel.LabelListAttribute
40 Data bazel.LabelListAttribute
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000041 Deps bazel.LabelListAttribute
Jingwen Chen13b9b422021-03-08 07:32:28 -050042 Python_version string
43}
44
Jingwen Chen13b9b422021-03-08 07:32:28 -050045func PythonBinaryBp2Build(ctx android.TopDownMutatorContext) {
46 m, ok := ctx.Module().(*Module)
Jingwen Chen12b4c272021-03-10 02:05:59 -050047 if !ok || !m.ConvertWithBp2build(ctx) {
Jingwen Chen13b9b422021-03-08 07:32:28 -050048 return
49 }
50
51 // a Module can be something other than a python_binary_host
52 if ctx.ModuleType() != "python_binary_host" {
53 return
54 }
55
56 var main string
57 for _, propIntf := range m.GetProperties() {
58 if props, ok := propIntf.(*BinaryProperties); ok {
59 // main is optional.
60 if props.Main != nil {
61 main = *props.Main
62 break
63 }
64 }
65 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +000066
Jingwen Chen13b9b422021-03-08 07:32:28 -050067 // TODO(b/182306917): this doesn't fully handle all nested props versioned
68 // by the python version, which would have been handled by the version split
69 // mutator. This is sufficient for very simple python_binary_host modules
70 // under Bionic.
71 py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, false)
72 py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
73 var python_version string
74 if py3Enabled && py2Enabled {
75 panic(fmt.Errorf(
76 "error for '%s' module: bp2build's python_binary_host converter does not support "+
77 "converting a module that is enabled for both Python 2 and 3 at the same time.", m.Name()))
78 } else if py2Enabled {
79 python_version = "PY2"
80 } else {
81 // do nothing, since python_version defaults to PY3.
82 }
83
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +000084 baseAttrs := m.makeArchVariantBaseAttributes(ctx)
Jingwen Chen13b9b422021-03-08 07:32:28 -050085 attrs := &bazelPythonBinaryAttributes{
86 Main: main,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +000087 Srcs: baseAttrs.Srcs,
88 Data: baseAttrs.Data,
89 Deps: baseAttrs.Deps,
Jingwen Chen13b9b422021-03-08 07:32:28 -050090 Python_version: python_version,
91 }
92
93 props := bazel.BazelTargetModuleProperties{
94 // Use the native py_binary rule.
95 Rule_class: "py_binary",
96 }
97
Liz Kammer2ada09a2021-08-11 00:17:36 -040098 ctx.CreateBazelTargetModule(m.Name(), props, attrs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080099}
100
Nan Zhangd4e641b2017-07-12 12:55:28 -0700101type BinaryProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800102 // the name of the source file that is the main entry point of the program.
103 // this file must also be listed in srcs.
104 // If left unspecified, module name is used instead.
105 // If name doesn’t match any filename in srcs, main must be specified.
Nan Zhangea568a42017-11-08 21:20:04 -0800106 Main *string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800107
108 // set the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -0800109 Stem *string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800110
111 // append to the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -0800112 Suffix *string `android:"arch_variant"`
Nan Zhangc9c6cb72017-11-03 16:54:05 -0700113
114 // list of compatibility suites (for example "cts", "vts") that the module should be
115 // installed into.
116 Test_suites []string `android:"arch_variant"`
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800117
118 // whether to use `main` when starting the executable. The default is true, when set to
119 // false it will act much like the normal `python` executable, but with the sources and
120 // libraries automatically included in the PYTHONPATH.
121 Autorun *bool `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700122
123 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
124 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
125 // explicitly.
126 Auto_gen_config *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800127}
128
Nan Zhangd4e641b2017-07-12 12:55:28 -0700129type binaryDecorator struct {
130 binaryProperties BinaryProperties
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800131
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000132 *pythonInstaller
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800133}
134
Nan Zhangd4e641b2017-07-12 12:55:28 -0700135type IntermPathProvider interface {
136 IntermPathForModuleOut() android.OptionalPath
Nan Zhang5323f8e2017-05-10 13:37:54 -0700137}
138
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800139var (
Liz Kammerdd849a82020-06-12 16:38:45 -0700140 StubTemplateHost = "build/soong/python/scripts/stub_template_host.txt"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800141)
142
Nan Zhangd4e641b2017-07-12 12:55:28 -0700143func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
144 module := newModule(hod, android.MultilibFirst)
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000145 decorator := &binaryDecorator{pythonInstaller: NewPythonInstaller("bin", "")}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800146
Nan Zhangd4e641b2017-07-12 12:55:28 -0700147 module.bootstrapper = decorator
148 module.installer = decorator
Nan Zhang5323f8e2017-05-10 13:37:54 -0700149
Nan Zhangd4e641b2017-07-12 12:55:28 -0700150 return module, decorator
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800151}
152
Nan Zhangd4e641b2017-07-12 12:55:28 -0700153func PythonBinaryHostFactory() android.Module {
Jiyong Park1613e552020-09-14 19:43:17 +0900154 module, _ := NewBinary(android.HostSupported)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800155
Jingwen Chen13b9b422021-03-08 07:32:28 -0500156 android.InitBazelModule(module)
157
Liz Kammerd737d022020-11-16 15:42:51 -0800158 return module.init()
Nan Zhangd4e641b2017-07-12 12:55:28 -0700159}
160
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800161func (binary *binaryDecorator) autorun() bool {
162 return BoolDefault(binary.binaryProperties.Autorun, true)
163}
164
Nan Zhangd4e641b2017-07-12 12:55:28 -0700165func (binary *binaryDecorator) bootstrapperProps() []interface{} {
166 return []interface{}{&binary.binaryProperties}
167}
168
Nan Zhang1db85402017-12-18 13:20:23 -0800169func (binary *binaryDecorator) bootstrap(ctx android.ModuleContext, actualVersion string,
170 embeddedLauncher bool, srcsPathMappings []pathMapping, srcsZip android.Path,
171 depsSrcsZips android.Paths) android.OptionalPath {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800172
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800173 main := ""
174 if binary.autorun() {
175 main = binary.getPyMainFile(ctx, srcsPathMappings)
176 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700177
Nan Zhangcba97e62018-09-26 15:14:10 -0700178 var launcherPath android.OptionalPath
Nan Zhang1db85402017-12-18 13:20:23 -0800179 if embeddedLauncher {
Colin Crossee6143c2017-12-30 17:54:27 -0800180 ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700181 if provider, ok := m.(IntermPathProvider); ok {
Nan Zhangcba97e62018-09-26 15:14:10 -0700182 if launcherPath.Valid() {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700183 panic(fmt.Errorf("launcher path was found before: %q",
Nan Zhang1db85402017-12-18 13:20:23 -0800184 launcherPath))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700185 }
Nan Zhangcba97e62018-09-26 15:14:10 -0700186 launcherPath = provider.IntermPathForModuleOut()
Nan Zhangd4e641b2017-07-12 12:55:28 -0700187 }
188 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800189 }
190
Nan Zhang1db85402017-12-18 13:20:23 -0800191 binFile := registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath,
192 binary.getHostInterpreterName(ctx, actualVersion),
193 main, binary.getStem(ctx), append(android.Paths{srcsZip}, depsSrcsZips...))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800194
Nan Zhang5323f8e2017-05-10 13:37:54 -0700195 return android.OptionalPathForPath(binFile)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800196}
197
Nan Zhangd4e641b2017-07-12 12:55:28 -0700198// get host interpreter name.
199func (binary *binaryDecorator) getHostInterpreterName(ctx android.ModuleContext,
Nan Zhang1db85402017-12-18 13:20:23 -0800200 actualVersion string) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800201 var interp string
Nan Zhang1db85402017-12-18 13:20:23 -0800202 switch actualVersion {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800203 case pyVersion2:
Dan Willemsen7d1681a2017-09-25 13:47:40 -0700204 interp = "python2.7"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800205 case pyVersion3:
206 interp = "python3"
207 default:
208 panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.",
Nan Zhang1db85402017-12-18 13:20:23 -0800209 actualVersion, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800210 }
211
212 return interp
213}
214
215// find main program path within runfiles tree.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700216func (binary *binaryDecorator) getPyMainFile(ctx android.ModuleContext,
217 srcsPathMappings []pathMapping) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800218 var main string
Nan Zhangea568a42017-11-08 21:20:04 -0800219 if String(binary.binaryProperties.Main) == "" {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700220 main = ctx.ModuleName() + pyExt
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800221 } else {
Nan Zhangea568a42017-11-08 21:20:04 -0800222 main = String(binary.binaryProperties.Main)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800223 }
224
Nan Zhangd4e641b2017-07-12 12:55:28 -0700225 for _, path := range srcsPathMappings {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800226 if main == path.src.Rel() {
227 return path.dest
228 }
229 }
230 ctx.PropertyErrorf("main", "%q is not listed in srcs.", main)
231
232 return ""
233}
234
Nan Zhangd4e641b2017-07-12 12:55:28 -0700235func (binary *binaryDecorator) getStem(ctx android.ModuleContext) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800236 stem := ctx.ModuleName()
Nan Zhangea568a42017-11-08 21:20:04 -0800237 if String(binary.binaryProperties.Stem) != "" {
238 stem = String(binary.binaryProperties.Stem)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800239 }
240
Nan Zhangea568a42017-11-08 21:20:04 -0800241 return stem + String(binary.binaryProperties.Suffix)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800242}