blob: 140f07af99c3d2b7847b59c6f4e41d1cb27514ef [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"
23)
24
25func init() {
26 android.RegisterModuleType("python_binary_host", PythonBinaryHostFactory)
27}
28
Nan Zhangd4e641b2017-07-12 12:55:28 -070029type BinaryProperties struct {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080030 // 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 Zhangea568a42017-11-08 21:20:04 -080034 Main *string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080035
36 // set the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -080037 Stem *string `android:"arch_variant"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080038
39 // append to the name of the output binary.
Nan Zhangea568a42017-11-08 21:20:04 -080040 Suffix *string `android:"arch_variant"`
Nan Zhangc9c6cb72017-11-03 16:54:05 -070041
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 Willemsen6ca390f2019-02-14 23:17:08 -080045
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"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080050}
51
Nan Zhangd4e641b2017-07-12 12:55:28 -070052type binaryDecorator struct {
53 binaryProperties BinaryProperties
Nan Zhangdb0b9a32017-02-27 10:12:13 -080054
Nan Zhangd9ec5e72017-12-01 20:00:31 +000055 *pythonInstaller
Nan Zhangdb0b9a32017-02-27 10:12:13 -080056}
57
Nan Zhangd4e641b2017-07-12 12:55:28 -070058type IntermPathProvider interface {
59 IntermPathForModuleOut() android.OptionalPath
Nan Zhang5323f8e2017-05-10 13:37:54 -070060}
61
Nan Zhangdb0b9a32017-02-27 10:12:13 -080062var (
63 stubTemplateHost = "build/soong/python/scripts/stub_template_host.txt"
64)
65
Nan Zhangd4e641b2017-07-12 12:55:28 -070066func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
67 module := newModule(hod, android.MultilibFirst)
Nan Zhangd9ec5e72017-12-01 20:00:31 +000068 decorator := &binaryDecorator{pythonInstaller: NewPythonInstaller("bin", "")}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080069
Nan Zhangd4e641b2017-07-12 12:55:28 -070070 module.bootstrapper = decorator
71 module.installer = decorator
Nan Zhang5323f8e2017-05-10 13:37:54 -070072
Nan Zhangd4e641b2017-07-12 12:55:28 -070073 return module, decorator
Nan Zhangdb0b9a32017-02-27 10:12:13 -080074}
75
Nan Zhangd4e641b2017-07-12 12:55:28 -070076func PythonBinaryHostFactory() android.Module {
77 module, _ := NewBinary(android.HostSupportedNoCross)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080078
Nan Zhangd4e641b2017-07-12 12:55:28 -070079 return module.Init()
80}
81
Dan Willemsen6ca390f2019-02-14 23:17:08 -080082func (binary *binaryDecorator) autorun() bool {
83 return BoolDefault(binary.binaryProperties.Autorun, true)
84}
85
Nan Zhangd4e641b2017-07-12 12:55:28 -070086func (binary *binaryDecorator) bootstrapperProps() []interface{} {
87 return []interface{}{&binary.binaryProperties}
88}
89
Nan Zhang1db85402017-12-18 13:20:23 -080090func (binary *binaryDecorator) bootstrap(ctx android.ModuleContext, actualVersion string,
91 embeddedLauncher bool, srcsPathMappings []pathMapping, srcsZip android.Path,
92 depsSrcsZips android.Paths) android.OptionalPath {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080093
Dan Willemsen6ca390f2019-02-14 23:17:08 -080094 main := ""
95 if binary.autorun() {
96 main = binary.getPyMainFile(ctx, srcsPathMappings)
97 }
Nan Zhangd4e641b2017-07-12 12:55:28 -070098
Nan Zhangcba97e62018-09-26 15:14:10 -070099 var launcherPath android.OptionalPath
Nan Zhang1db85402017-12-18 13:20:23 -0800100 if embeddedLauncher {
Colin Crossee6143c2017-12-30 17:54:27 -0800101 ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700102 if provider, ok := m.(IntermPathProvider); ok {
Nan Zhangcba97e62018-09-26 15:14:10 -0700103 if launcherPath.Valid() {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700104 panic(fmt.Errorf("launcher path was found before: %q",
Nan Zhang1db85402017-12-18 13:20:23 -0800105 launcherPath))
Nan Zhangd4e641b2017-07-12 12:55:28 -0700106 }
Nan Zhangcba97e62018-09-26 15:14:10 -0700107 launcherPath = provider.IntermPathForModuleOut()
Nan Zhangd4e641b2017-07-12 12:55:28 -0700108 }
109 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800110 }
111
Nan Zhang1db85402017-12-18 13:20:23 -0800112 binFile := registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath,
113 binary.getHostInterpreterName(ctx, actualVersion),
114 main, binary.getStem(ctx), append(android.Paths{srcsZip}, depsSrcsZips...))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800115
Nan Zhang5323f8e2017-05-10 13:37:54 -0700116 return android.OptionalPathForPath(binFile)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800117}
118
Nan Zhangd4e641b2017-07-12 12:55:28 -0700119// get host interpreter name.
120func (binary *binaryDecorator) getHostInterpreterName(ctx android.ModuleContext,
Nan Zhang1db85402017-12-18 13:20:23 -0800121 actualVersion string) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800122 var interp string
Nan Zhang1db85402017-12-18 13:20:23 -0800123 switch actualVersion {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800124 case pyVersion2:
Dan Willemsen7d1681a2017-09-25 13:47:40 -0700125 interp = "python2.7"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800126 case pyVersion3:
127 interp = "python3"
128 default:
129 panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.",
Nan Zhang1db85402017-12-18 13:20:23 -0800130 actualVersion, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800131 }
132
133 return interp
134}
135
136// find main program path within runfiles tree.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700137func (binary *binaryDecorator) getPyMainFile(ctx android.ModuleContext,
138 srcsPathMappings []pathMapping) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800139 var main string
Nan Zhangea568a42017-11-08 21:20:04 -0800140 if String(binary.binaryProperties.Main) == "" {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700141 main = ctx.ModuleName() + pyExt
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800142 } else {
Nan Zhangea568a42017-11-08 21:20:04 -0800143 main = String(binary.binaryProperties.Main)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800144 }
145
Nan Zhangd4e641b2017-07-12 12:55:28 -0700146 for _, path := range srcsPathMappings {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800147 if main == path.src.Rel() {
148 return path.dest
149 }
150 }
151 ctx.PropertyErrorf("main", "%q is not listed in srcs.", main)
152
153 return ""
154}
155
Nan Zhangd4e641b2017-07-12 12:55:28 -0700156func (binary *binaryDecorator) getStem(ctx android.ModuleContext) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800157 stem := ctx.ModuleName()
Nan Zhangea568a42017-11-08 21:20:04 -0800158 if String(binary.binaryProperties.Stem) != "" {
159 stem = String(binary.binaryProperties.Stem)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800160 }
161
Nan Zhangea568a42017-11-08 21:20:04 -0800162 return stem + String(binary.binaryProperties.Suffix)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800163}