1// 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"
21
22	"android/soong/android"
23)
24
25func init() {
26	android.RegisterModuleType("python_binary_host", PythonBinaryHostFactory)
27}
28
29type BinaryProperties struct {
30	// 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.
34	Main *string `android:"arch_variant"`
35
36	// set the name of the output binary.
37	Stem *string `android:"arch_variant"`
38
39	// append to the name of the output binary.
40	Suffix *string `android:"arch_variant"`
41
42	// list of compatibility suites (for example "cts", "vts") that the module should be
43	// installed into.
44	Test_suites []string `android:"arch_variant"`
45
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"`
50
51	// Flag to indicate whether or not to create test config automatically. If AndroidTest.xml
52	// doesn't exist next to the Android.bp, this attribute doesn't need to be set to true
53	// explicitly.
54	Auto_gen_config *bool
55}
56
57type binaryDecorator struct {
58	binaryProperties BinaryProperties
59
60	*pythonInstaller
61}
62
63type IntermPathProvider interface {
64	IntermPathForModuleOut() android.OptionalPath
65}
66
67var (
68	StubTemplateHost = "build/soong/python/scripts/stub_template_host.txt"
69)
70
71func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
72	module := newModule(hod, android.MultilibFirst)
73	decorator := &binaryDecorator{pythonInstaller: NewPythonInstaller("bin", "")}
74
75	module.bootstrapper = decorator
76	module.installer = decorator
77
78	return module, decorator
79}
80
81func PythonBinaryHostFactory() android.Module {
82	module, _ := NewBinary(android.HostSupportedNoCross)
83
84	return module.Init()
85}
86
87func (binary *binaryDecorator) autorun() bool {
88	return BoolDefault(binary.binaryProperties.Autorun, true)
89}
90
91func (binary *binaryDecorator) bootstrapperProps() []interface{} {
92	return []interface{}{&binary.binaryProperties}
93}
94
95func (binary *binaryDecorator) bootstrap(ctx android.ModuleContext, actualVersion string,
96	embeddedLauncher bool, srcsPathMappings []pathMapping, srcsZip android.Path,
97	depsSrcsZips android.Paths) android.OptionalPath {
98
99	main := ""
100	if binary.autorun() {
101		main = binary.getPyMainFile(ctx, srcsPathMappings)
102	}
103
104	var launcherPath android.OptionalPath
105	if embeddedLauncher {
106		ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) {
107			if provider, ok := m.(IntermPathProvider); ok {
108				if launcherPath.Valid() {
109					panic(fmt.Errorf("launcher path was found before: %q",
110						launcherPath))
111				}
112				launcherPath = provider.IntermPathForModuleOut()
113			}
114		})
115	}
116
117	binFile := registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath,
118		binary.getHostInterpreterName(ctx, actualVersion),
119		main, binary.getStem(ctx), append(android.Paths{srcsZip}, depsSrcsZips...))
120
121	return android.OptionalPathForPath(binFile)
122}
123
124// get host interpreter name.
125func (binary *binaryDecorator) getHostInterpreterName(ctx android.ModuleContext,
126	actualVersion string) string {
127	var interp string
128	switch actualVersion {
129	case pyVersion2:
130		interp = "python2.7"
131	case pyVersion3:
132		interp = "python3"
133	default:
134		panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.",
135			actualVersion, ctx.ModuleName()))
136	}
137
138	return interp
139}
140
141// find main program path within runfiles tree.
142func (binary *binaryDecorator) getPyMainFile(ctx android.ModuleContext,
143	srcsPathMappings []pathMapping) string {
144	var main string
145	if String(binary.binaryProperties.Main) == "" {
146		main = ctx.ModuleName() + pyExt
147	} else {
148		main = String(binary.binaryProperties.Main)
149	}
150
151	for _, path := range srcsPathMappings {
152		if main == path.src.Rel() {
153			return path.dest
154		}
155	}
156	ctx.PropertyErrorf("main", "%q is not listed in srcs.", main)
157
158	return ""
159}
160
161func (binary *binaryDecorator) getStem(ctx android.ModuleContext) string {
162	stem := ctx.ModuleName()
163	if String(binary.binaryProperties.Stem) != "" {
164		stem = String(binary.binaryProperties.Stem)
165	}
166
167	return stem + String(binary.binaryProperties.Suffix)
168}
169