blob: 4135dfef992519602567dfcc6aea4cc3e214e7a5 [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"`
Nan Zhangdb0b9a32017-02-27 10:12:13 -080045}
46
Nan Zhangd4e641b2017-07-12 12:55:28 -070047type binaryDecorator struct {
48 binaryProperties BinaryProperties
Nan Zhangdb0b9a32017-02-27 10:12:13 -080049
Nan Zhangd9ec5e72017-12-01 20:00:31 +000050 *pythonInstaller
Nan Zhangdb0b9a32017-02-27 10:12:13 -080051}
52
Nan Zhangd4e641b2017-07-12 12:55:28 -070053type IntermPathProvider interface {
54 IntermPathForModuleOut() android.OptionalPath
Nan Zhang5323f8e2017-05-10 13:37:54 -070055}
56
Nan Zhangdb0b9a32017-02-27 10:12:13 -080057var (
58 stubTemplateHost = "build/soong/python/scripts/stub_template_host.txt"
59)
60
Nan Zhangd4e641b2017-07-12 12:55:28 -070061func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
62 module := newModule(hod, android.MultilibFirst)
Nan Zhangd9ec5e72017-12-01 20:00:31 +000063 decorator := &binaryDecorator{pythonInstaller: NewPythonInstaller("bin", "")}
Nan Zhangdb0b9a32017-02-27 10:12:13 -080064
Nan Zhangd4e641b2017-07-12 12:55:28 -070065 module.bootstrapper = decorator
66 module.installer = decorator
Nan Zhang5323f8e2017-05-10 13:37:54 -070067
Nan Zhangd4e641b2017-07-12 12:55:28 -070068 return module, decorator
Nan Zhangdb0b9a32017-02-27 10:12:13 -080069}
70
Nan Zhangd4e641b2017-07-12 12:55:28 -070071func PythonBinaryHostFactory() android.Module {
72 module, _ := NewBinary(android.HostSupportedNoCross)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080073
Nan Zhangd4e641b2017-07-12 12:55:28 -070074 return module.Init()
75}
76
77func (binary *binaryDecorator) bootstrapperProps() []interface{} {
78 return []interface{}{&binary.binaryProperties}
79}
80
Nan Zhang1db85402017-12-18 13:20:23 -080081func (binary *binaryDecorator) bootstrap(ctx android.ModuleContext, actualVersion string,
82 embeddedLauncher bool, srcsPathMappings []pathMapping, srcsZip android.Path,
83 depsSrcsZips android.Paths) android.OptionalPath {
Nan Zhangdb0b9a32017-02-27 10:12:13 -080084
Nan Zhangd4e641b2017-07-12 12:55:28 -070085 main := binary.getPyMainFile(ctx, srcsPathMappings)
Nan Zhangd4e641b2017-07-12 12:55:28 -070086
Nan Zhang1db85402017-12-18 13:20:23 -080087 var launcherPath android.Path
88 if embeddedLauncher {
Colin Crossee6143c2017-12-30 17:54:27 -080089 ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) {
Nan Zhangd4e641b2017-07-12 12:55:28 -070090 if provider, ok := m.(IntermPathProvider); ok {
Nan Zhang1db85402017-12-18 13:20:23 -080091 if launcherPath != nil {
Nan Zhangd4e641b2017-07-12 12:55:28 -070092 panic(fmt.Errorf("launcher path was found before: %q",
Nan Zhang1db85402017-12-18 13:20:23 -080093 launcherPath))
Nan Zhangd4e641b2017-07-12 12:55:28 -070094 }
Nan Zhang1db85402017-12-18 13:20:23 -080095 launcherPath = provider.IntermPathForModuleOut().Path()
Nan Zhangd4e641b2017-07-12 12:55:28 -070096 }
97 })
Nan Zhangdb0b9a32017-02-27 10:12:13 -080098 }
99
Nan Zhang1db85402017-12-18 13:20:23 -0800100 binFile := registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath,
101 binary.getHostInterpreterName(ctx, actualVersion),
102 main, binary.getStem(ctx), append(android.Paths{srcsZip}, depsSrcsZips...))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800103
Nan Zhang5323f8e2017-05-10 13:37:54 -0700104 return android.OptionalPathForPath(binFile)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800105}
106
Nan Zhangd4e641b2017-07-12 12:55:28 -0700107// get host interpreter name.
108func (binary *binaryDecorator) getHostInterpreterName(ctx android.ModuleContext,
Nan Zhang1db85402017-12-18 13:20:23 -0800109 actualVersion string) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800110 var interp string
Nan Zhang1db85402017-12-18 13:20:23 -0800111 switch actualVersion {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800112 case pyVersion2:
Dan Willemsen7d1681a2017-09-25 13:47:40 -0700113 interp = "python2.7"
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800114 case pyVersion3:
115 interp = "python3"
116 default:
117 panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.",
Nan Zhang1db85402017-12-18 13:20:23 -0800118 actualVersion, ctx.ModuleName()))
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800119 }
120
121 return interp
122}
123
124// find main program path within runfiles tree.
Nan Zhangd4e641b2017-07-12 12:55:28 -0700125func (binary *binaryDecorator) getPyMainFile(ctx android.ModuleContext,
126 srcsPathMappings []pathMapping) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800127 var main string
Nan Zhangea568a42017-11-08 21:20:04 -0800128 if String(binary.binaryProperties.Main) == "" {
Nan Zhangd4e641b2017-07-12 12:55:28 -0700129 main = ctx.ModuleName() + pyExt
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800130 } else {
Nan Zhangea568a42017-11-08 21:20:04 -0800131 main = String(binary.binaryProperties.Main)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800132 }
133
Nan Zhangd4e641b2017-07-12 12:55:28 -0700134 for _, path := range srcsPathMappings {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800135 if main == path.src.Rel() {
136 return path.dest
137 }
138 }
139 ctx.PropertyErrorf("main", "%q is not listed in srcs.", main)
140
141 return ""
142}
143
Nan Zhangd4e641b2017-07-12 12:55:28 -0700144func (binary *binaryDecorator) getStem(ctx android.ModuleContext) string {
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800145 stem := ctx.ModuleName()
Nan Zhangea568a42017-11-08 21:20:04 -0800146 if String(binary.binaryProperties.Stem) != "" {
147 stem = String(binary.binaryProperties.Stem)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800148 }
149
Nan Zhangea568a42017-11-08 21:20:04 -0800150 return stem + String(binary.binaryProperties.Suffix)
Nan Zhangdb0b9a32017-02-27 10:12:13 -0800151}