| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 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 |  | 
|  | 15 | package python | 
|  | 16 |  | 
|  | 17 | // This file contains the module types for building Python binary. | 
|  | 18 |  | 
|  | 19 | import ( | 
|  | 20 | "fmt" | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 21 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 22 | "android/soong/android" | 
|  | 23 | ) | 
|  | 24 |  | 
|  | 25 | func init() { | 
|  | 26 | android.RegisterModuleType("python_binary_host", PythonBinaryHostFactory) | 
|  | 27 | } | 
|  | 28 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 29 | type BinaryProperties struct { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 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. | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 34 | Main *string `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 35 |  | 
|  | 36 | // set the name of the output binary. | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 37 | Stem *string `android:"arch_variant"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 38 |  | 
|  | 39 | // append to the name of the output binary. | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 40 | Suffix *string `android:"arch_variant"` | 
| Nan Zhang | c9c6cb7 | 2017-11-03 16:54:05 -0700 | [diff] [blame] | 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"` | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 47 | type binaryDecorator struct { | 
|  | 48 | binaryProperties BinaryProperties | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 49 |  | 
| Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 50 | *pythonInstaller | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 51 | } | 
|  | 52 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 53 | type IntermPathProvider interface { | 
|  | 54 | IntermPathForModuleOut() android.OptionalPath | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 55 | } | 
|  | 56 |  | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 57 | var ( | 
|  | 58 | stubTemplateHost = "build/soong/python/scripts/stub_template_host.txt" | 
|  | 59 | ) | 
|  | 60 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 61 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { | 
|  | 62 | module := newModule(hod, android.MultilibFirst) | 
| Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 63 | decorator := &binaryDecorator{pythonInstaller: NewPythonInstaller("bin", "")} | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 64 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 65 | module.bootstrapper = decorator | 
|  | 66 | module.installer = decorator | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 67 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 68 | return module, decorator | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 69 | } | 
|  | 70 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 71 | func PythonBinaryHostFactory() android.Module { | 
|  | 72 | module, _ := NewBinary(android.HostSupportedNoCross) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 73 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 74 | return module.Init() | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | func (binary *binaryDecorator) bootstrapperProps() []interface{} { | 
|  | 78 | return []interface{}{&binary.binaryProperties} | 
|  | 79 | } | 
|  | 80 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 81 | func (binary *binaryDecorator) bootstrap(ctx android.ModuleContext, actualVersion string, | 
|  | 82 | embeddedLauncher bool, srcsPathMappings []pathMapping, srcsZip android.Path, | 
|  | 83 | depsSrcsZips android.Paths) android.OptionalPath { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 84 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 85 | main := binary.getPyMainFile(ctx, srcsPathMappings) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 86 |  | 
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 87 | var launcherPath android.OptionalPath | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 88 | if embeddedLauncher { | 
| Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 89 | ctx.VisitDirectDepsWithTag(launcherTag, func(m android.Module) { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 90 | if provider, ok := m.(IntermPathProvider); ok { | 
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 91 | if launcherPath.Valid() { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 92 | panic(fmt.Errorf("launcher path was found before: %q", | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 93 | launcherPath)) | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 94 | } | 
| Nan Zhang | cba97e6 | 2018-09-26 15:14:10 -0700 | [diff] [blame] | 95 | launcherPath = provider.IntermPathForModuleOut() | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 96 | } | 
|  | 97 | }) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 100 | binFile := registerBuildActionForParFile(ctx, embeddedLauncher, launcherPath, | 
|  | 101 | binary.getHostInterpreterName(ctx, actualVersion), | 
|  | 102 | main, binary.getStem(ctx), append(android.Paths{srcsZip}, depsSrcsZips...)) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 103 |  | 
| Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 104 | return android.OptionalPathForPath(binFile) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 107 | // get host interpreter name. | 
|  | 108 | func (binary *binaryDecorator) getHostInterpreterName(ctx android.ModuleContext, | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 109 | actualVersion string) string { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 110 | var interp string | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 111 | switch actualVersion { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 112 | case pyVersion2: | 
| Dan Willemsen | 7d1681a | 2017-09-25 13:47:40 -0700 | [diff] [blame] | 113 | interp = "python2.7" | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 114 | case pyVersion3: | 
|  | 115 | interp = "python3" | 
|  | 116 | default: | 
|  | 117 | panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.", | 
| Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 118 | actualVersion, ctx.ModuleName())) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 119 | } | 
|  | 120 |  | 
|  | 121 | return interp | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | // find main program path within runfiles tree. | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 125 | func (binary *binaryDecorator) getPyMainFile(ctx android.ModuleContext, | 
|  | 126 | srcsPathMappings []pathMapping) string { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 127 | var main string | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 128 | if String(binary.binaryProperties.Main) == "" { | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 129 | main = ctx.ModuleName() + pyExt | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 130 | } else { | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 131 | main = String(binary.binaryProperties.Main) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
| Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 134 | for _, path := range srcsPathMappings { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 135 | 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 Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 144 | func (binary *binaryDecorator) getStem(ctx android.ModuleContext) string { | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 145 | stem := ctx.ModuleName() | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 146 | if String(binary.binaryProperties.Stem) != "" { | 
|  | 147 | stem = String(binary.binaryProperties.Stem) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
| Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 150 | return stem + String(binary.binaryProperties.Suffix) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 151 | } |