blob: 6061ad4f7f8aeeee5089344f39e8abb90170bc1f [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
39 Srcs bazel.LabelList
40 Data bazel.LabelList
41 Python_version string
42}
43
44type bazelPythonBinary struct {
45 android.BazelTargetModuleBase
46 bazelPythonBinaryAttributes
47}
48
49func BazelPythonBinaryFactory() android.Module {
50 module := &bazelPythonBinary{}
51 module.AddProperties(&module.bazelPythonBinaryAttributes)
52 android.InitBazelTargetModule(module)
53 return module
54}
55
56func (m *bazelPythonBinary) Name() string {
57 return m.BaseModuleName()
58}
59
60func (m *bazelPythonBinary) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
61
62func PythonBinaryBp2Build(ctx android.TopDownMutatorContext) {
63 m, ok := ctx.Module().(*Module)
64 if !ok || !m.ConvertWithBp2build() {
65 return
66 }
67
68 // a Module can be something other than a python_binary_host
69 if ctx.ModuleType() != "python_binary_host" {
70 return
71 }
72
73 var main string
74 for _, propIntf := range m.GetProperties() {
75 if props, ok := propIntf.(*BinaryProperties); ok {
76 // main is optional.
77 if props.Main != nil {
78 main = *props.Main
79 break
80 }
81 }
82 }
83 // TODO(b/182306917): this doesn't fully handle all nested props versioned
84 // by the python version, which would have been handled by the version split
85 // mutator. This is sufficient for very simple python_binary_host modules
86 // under Bionic.
87 py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, false)
88 py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
89 var python_version string
90 if py3Enabled && py2Enabled {
91 panic(fmt.Errorf(
92 "error for '%s' module: bp2build's python_binary_host converter does not support "+
93 "converting a module that is enabled for both Python 2 and 3 at the same time.", m.Name()))
94 } else if py2Enabled {
95 python_version = "PY2"
96 } else {
97 // do nothing, since python_version defaults to PY3.
98 }
99
100 attrs := &bazelPythonBinaryAttributes{
101 Main: main,
102 Srcs: android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs),
103 Data: android.BazelLabelForModuleSrc(ctx, m.properties.Data),
104 Python_version: python_version,
105 }
106
107 props := bazel.BazelTargetModuleProperties{
108 // Use the native py_binary rule.
109 Rule_class: "py_binary",
110 }
111
112 ctx.CreateBazelTargetModule(BazelPythonBinaryFactory, m.Name(), props, attrs)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800113}
114
Nan Zhangd4e641b2017-07-12 12:55:28 -0700115type BinaryProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800116 // the name of the source file that is the main entry point of the program.
117 // this file must also be listed in srcs.
118 // If left unspecified, module name is used instead.
119 // If name doesn’t match any filename in srcs, main must be specified.
Nan Zhangea568a42017-11-08 21:20:04 -0800120 Main *string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800121
122 // set the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -0800123 Stem *string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800124
125 // append to the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -0800126 Suffix *string `android:"arch_variant"`
Nan Zhangc9c6cb72017-11-03 16:54:05 -0700127
128 // list of compatibility suites (for example "cts", "vts") that the module should be
129 // installed into.
130 Test_suites []string `android:"arch_variant"`
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800131
132 // whether to use `main` when starting the executable. The default is true, when set to
133 // false it will act much like the normal `python` executable, but with the sources and
134 // libraries automatically included in the PYTHONPATH.
135 Autorun *bool `android:"arch_variant"`
Dan Shi6ffaaa82019-09-26 11:41:36 -0700136
137 // Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
138 // doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
139 // explicitly.
140 Auto_gen_config *bool
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800141}
142
Nan Zhangd4e641b2017-07-12 12:55:28 -0700143type binaryDecorator struct {
144 binaryProperties BinaryProperties
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800145
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000146 *pythonInstaller
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800147}
148
Nan Zhangd4e641b2017-07-12 12:55:28 -0700149type IntermPathProvider interface {
150 IntermPathForModuleOut() android.OptionalPath
Nan Zhang5323f8e2017-05-10 13:37:54 -0700151}
152
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800153var (
Liz Kammerdd849a82020-06-12 16:38:45 -0700154 StubTemplateHost = "build/soong/python/scripts/stub_template_host.txt"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800155)
156
Nan Zhangd4e641b2017-07-12 12:55:28 -0700157func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
158 module := newModule(hod, android.MultilibFirst)
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000159 decorator := &binaryDecorator{pythonInstaller: NewPythonInstaller("bin", "")}
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800160
Nan Zhangd4e641b2017-07-12 12:55:28 -0700161 module.bootstrapper = decorator
162 module.installer = decorator
Nan Zhang5323f8e2017-05-10 13:37:54 -0700163
Nan Zhangd4e641b2017-07-12 12:55:28 -0700164 return module, decorator
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800165}
166
Nan Zhangd4e641b2017-07-12 12:55:28 -0700167func PythonBinaryHostFactory() android.Module {
Jiyong Park1613e552020-09-14 19:43:17 +0900168 module, _ := NewBinary(android.HostSupported)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800169
Jingwen Chen13b9b422021-03-08 07:32:28 -0500170 android.InitBazelModule(module)
171
Liz Kammerd737d022020-11-16 15:42:51 -0800172 return module.init()
Nan Zhangd4e641b2017-07-12 12:55:28 -0700173}
174
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800175func (binary *binaryDecorator) autorun() bool {
176 return BoolDefault(binary.binaryProperties.Autorun, true)
177}
178
Nan Zhangd4e641b2017-07-12 12:55:28 -0700179func (binary *binaryDecorator) bootstrapperProps() []interface{} {
180 return []interface{}{&binary.binaryProperties}
181}
182
Nan Zhang1db85402017-12-18 13:20:23 -0800183func (binary *binaryDecorator) bootstrap(ctx android.ModuleContext, actualVersion string,
184 embeddedLauncher bool, srcsPathMappings []pathMapping, srcsZip android.Path,
185 depsSrcsZips android.Paths) android.OptionalPath {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800186
Dan Willemsen6ca390f2019-02-14 23:17:08 -0800187 main := ""
188 if binary.autorun() {
189 main = binary.getPyMainFile(ctx, srcsPathMappings)
190 }
Nan Zhangd4e641b2017-07-12 12:55:28 -0700191
Nan Zhangcba97e62018-09-26 15:14:10 -0700192 var launcherPath android.OptionalPath
Nan Zhang1db85402017-12-18 13:20:23 -0800193 if embeddedLauncher {
Colin Crossee6143c2017-12-30 17:54:27 -0800194 ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700195 if provider, ok := m.(IntermPathProvider); ok {
Nan Zhangcba97e62018-09-26 15:14:10 -0700196 if launcherPath.Valid() {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700197 panic(fmt.Errorf("launcher path was found before: %q",
Nan Zhang1db85402017-12-18 13:20:23 -0800198 launcherPath))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700199 }
Nan Zhangcba97e62018-09-26 15:14:10 -0700200 launcherPath = provider.IntermPathForModuleOut()
Nan Zhangd4e641b2017-07-12 12:55:28 -0700201 }
202 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800203 }
204
Nan Zhang1db85402017-12-18 13:20:23 -0800205 binFile := registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath,
206 binary.getHostInterpreterName(ctx, actualVersion),
207 main, binary.getStem(ctx), append(android.Paths{srcsZip}, depsSrcsZips...))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800208
Nan Zhang5323f8e2017-05-10 13:37:54 -0700209 return android.OptionalPathForPath(binFile)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800210}
211
Nan Zhangd4e641b2017-07-12 12:55:28 -0700212// get host interpreter name.
213func (binary *binaryDecorator) getHostInterpreterName(ctx android.ModuleContext,
Nan Zhang1db85402017-12-18 13:20:23 -0800214 actualVersion string) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800215 var interp string
Nan Zhang1db85402017-12-18 13:20:23 -0800216 switch actualVersion {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800217 case pyVersion2:
Dan Willemsen7d1681a2017-09-25 13:47:40 -0700218 interp = "python2.7"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800219 case pyVersion3:
220 interp = "python3"
221 default:
222 panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.",
Nan Zhang1db85402017-12-18 13:20:23 -0800223 actualVersion, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800224 }
225
226 return interp
227}
228
229// find main program path within runfiles tree.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700230func (binary *binaryDecorator) getPyMainFile(ctx android.ModuleContext,
231 srcsPathMappings []pathMapping) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800232 var main string
Nan Zhangea568a42017-11-08 21:20:04 -0800233 if String(binary.binaryProperties.Main) == "" {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700234 main = ctx.ModuleName() + pyExt
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800235 } else {
Nan Zhangea568a42017-11-08 21:20:04 -0800236 main = String(binary.binaryProperties.Main)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800237 }
238
Nan Zhangd4e641b2017-07-12 12:55:28 -0700239 for _, path := range srcsPathMappings {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800240 if main == path.src.Rel() {
241 return path.dest
242 }
243 }
244 ctx.PropertyErrorf("main", "%q is not listed in srcs.", main)
245
246 return ""
247}
248
Nan Zhangd4e641b2017-07-12 12:55:28 -0700249func (binary *binaryDecorator) getStem(ctx android.ModuleContext) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800250 stem := ctx.ModuleName()
Nan Zhangea568a42017-11-08 21:20:04 -0800251 if String(binary.binaryProperties.Stem) != "" {
252 stem = String(binary.binaryProperties.Stem)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800253 }
254
Nan Zhangea568a42017-11-08 21:20:04 -0800255 return stem + String(binary.binaryProperties.Suffix)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800256}