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 | "path/filepath" |
| 22 | "strings" |
| 23 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
| 27 | func init() { |
| 28 | android.RegisterModuleType("python_binary_host", PythonBinaryHostFactory) |
| 29 | } |
| 30 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 31 | type BinaryProperties struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 32 | // the name of the source file that is the main entry point of the program. |
| 33 | // this file must also be listed in srcs. |
| 34 | // If left unspecified, module name is used instead. |
| 35 | // If name doesn’t match any filename in srcs, main must be specified. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 36 | Main string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 37 | |
| 38 | // set the name of the output binary. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 39 | Stem string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 40 | |
| 41 | // append to the name of the output binary. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 42 | Suffix string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 45 | type binaryDecorator struct { |
| 46 | binaryProperties BinaryProperties |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 47 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 48 | baseInstaller *pythonInstaller |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 51 | type IntermPathProvider interface { |
| 52 | IntermPathForModuleOut() android.OptionalPath |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 55 | func (binary *binaryDecorator) install(ctx android.ModuleContext, file android.Path) { |
| 56 | binary.baseInstaller.install(ctx, file) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 57 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 58 | |
| 59 | var ( |
| 60 | stubTemplateHost = "build/soong/python/scripts/stub_template_host.txt" |
| 61 | ) |
| 62 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 63 | func NewBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
| 64 | module := newModule(hod, android.MultilibFirst) |
| 65 | decorator := &binaryDecorator{baseInstaller: NewPythonInstaller("bin")} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 66 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 67 | module.bootstrapper = decorator |
| 68 | module.installer = decorator |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 69 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 70 | return module, decorator |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 73 | func PythonBinaryHostFactory() android.Module { |
| 74 | module, _ := NewBinary(android.HostSupportedNoCross) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 75 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 76 | return module.Init() |
| 77 | } |
| 78 | |
| 79 | func (binary *binaryDecorator) bootstrapperProps() []interface{} { |
| 80 | return []interface{}{&binary.binaryProperties} |
| 81 | } |
| 82 | |
| 83 | func (binary *binaryDecorator) bootstrap(ctx android.ModuleContext, actual_version string, |
| 84 | embedded_launcher bool, srcsPathMappings []pathMapping, parSpec parSpec, |
| 85 | depsPyRunfiles []string, depsParSpecs []parSpec) android.OptionalPath { |
| 86 | // no Python source file for compiling .par file. |
| 87 | if len(srcsPathMappings) == 0 { |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 88 | return android.OptionalPath{} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // the runfiles packages needs to be populated with "__init__.py". |
| 92 | newPyPkgs := []string{} |
| 93 | // the set to de-duplicate the new Python packages above. |
| 94 | newPyPkgSet := make(map[string]bool) |
| 95 | // the runfiles dirs have been treated as packages. |
| 96 | existingPyPkgSet := make(map[string]bool) |
| 97 | |
| 98 | wholePyRunfiles := []string{} |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 99 | for _, path := range srcsPathMappings { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 100 | wholePyRunfiles = append(wholePyRunfiles, path.dest) |
| 101 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 102 | wholePyRunfiles = append(wholePyRunfiles, depsPyRunfiles...) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 103 | |
| 104 | // find all the runfiles dirs which have been treated as packages. |
| 105 | for _, path := range wholePyRunfiles { |
| 106 | if filepath.Base(path) != initFileName { |
| 107 | continue |
| 108 | } |
| 109 | existingPyPkg := PathBeforeLastSlash(path) |
| 110 | if _, found := existingPyPkgSet[existingPyPkg]; found { |
| 111 | panic(fmt.Errorf("found init file path duplicates: %q for module: %q.", |
| 112 | path, ctx.ModuleName())) |
| 113 | } else { |
| 114 | existingPyPkgSet[existingPyPkg] = true |
| 115 | } |
| 116 | parentPath := PathBeforeLastSlash(existingPyPkg) |
| 117 | populateNewPyPkgs(parentPath, existingPyPkgSet, newPyPkgSet, &newPyPkgs) |
| 118 | } |
| 119 | |
| 120 | // create new packages under runfiles tree. |
| 121 | for _, path := range wholePyRunfiles { |
| 122 | if filepath.Base(path) == initFileName { |
| 123 | continue |
| 124 | } |
| 125 | parentPath := PathBeforeLastSlash(path) |
| 126 | populateNewPyPkgs(parentPath, existingPyPkgSet, newPyPkgSet, &newPyPkgs) |
| 127 | } |
| 128 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 129 | main := binary.getPyMainFile(ctx, srcsPathMappings) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 130 | if main == "" { |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 131 | return android.OptionalPath{} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 132 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 133 | |
| 134 | var launcher_path android.Path |
| 135 | if embedded_launcher { |
Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 136 | ctx.VisitDirectDeps(func(m android.Module) { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 137 | if ctx.OtherModuleDependencyTag(m) != launcherTag { |
| 138 | return |
| 139 | } |
| 140 | if provider, ok := m.(IntermPathProvider); ok { |
| 141 | if launcher_path != nil { |
| 142 | panic(fmt.Errorf("launcher path was found before: %q", |
| 143 | launcher_path)) |
| 144 | } |
| 145 | launcher_path = provider.IntermPathForModuleOut().Path() |
| 146 | } |
| 147 | }) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 150 | binFile := registerBuildActionForParFile(ctx, embedded_launcher, launcher_path, |
| 151 | binary.getHostInterpreterName(ctx, actual_version), |
| 152 | main, binary.getStem(ctx), newPyPkgs, append(depsParSpecs, parSpec)) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 153 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 154 | return android.OptionalPathForPath(binFile) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 157 | // get host interpreter name. |
| 158 | func (binary *binaryDecorator) getHostInterpreterName(ctx android.ModuleContext, |
| 159 | actual_version string) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 160 | var interp string |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 161 | switch actual_version { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 162 | case pyVersion2: |
Dan Willemsen | 7d1681a | 2017-09-25 13:47:40 -0700 | [diff] [blame] | 163 | interp = "python2.7" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 164 | case pyVersion3: |
| 165 | interp = "python3" |
| 166 | default: |
| 167 | panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.", |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 168 | actual_version, ctx.ModuleName())) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | return interp |
| 172 | } |
| 173 | |
| 174 | // find main program path within runfiles tree. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 175 | func (binary *binaryDecorator) getPyMainFile(ctx android.ModuleContext, |
| 176 | srcsPathMappings []pathMapping) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 177 | var main string |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 178 | if binary.binaryProperties.Main == "" { |
| 179 | main = ctx.ModuleName() + pyExt |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 180 | } else { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 181 | main = binary.binaryProperties.Main |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 184 | for _, path := range srcsPathMappings { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 185 | if main == path.src.Rel() { |
| 186 | return path.dest |
| 187 | } |
| 188 | } |
| 189 | ctx.PropertyErrorf("main", "%q is not listed in srcs.", main) |
| 190 | |
| 191 | return "" |
| 192 | } |
| 193 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 194 | func (binary *binaryDecorator) getStem(ctx android.ModuleContext) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 195 | stem := ctx.ModuleName() |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 196 | if binary.binaryProperties.Stem != "" { |
| 197 | stem = binary.binaryProperties.Stem |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 200 | return stem + binary.binaryProperties.Suffix |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // Sets the given directory and all its ancestor directories as Python packages. |
| 204 | func populateNewPyPkgs(pkgPath string, existingPyPkgSet, |
| 205 | newPyPkgSet map[string]bool, newPyPkgs *[]string) { |
| 206 | for pkgPath != "" { |
| 207 | if _, found := existingPyPkgSet[pkgPath]; found { |
| 208 | break |
| 209 | } |
| 210 | if _, found := newPyPkgSet[pkgPath]; !found { |
| 211 | newPyPkgSet[pkgPath] = true |
| 212 | *newPyPkgs = append(*newPyPkgs, pkgPath) |
| 213 | // Gets its ancestor directory by trimming last slash. |
| 214 | pkgPath = PathBeforeLastSlash(pkgPath) |
| 215 | } else { |
| 216 | break |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // filepath.Dir("abc") -> "." and filepath.Dir("/abc") -> "/". However, |
| 222 | // the PathBeforeLastSlash() will return "" for both cases above. |
| 223 | func PathBeforeLastSlash(path string) string { |
| 224 | if idx := strings.LastIndex(path, "/"); idx != -1 { |
| 225 | return path[:idx] |
| 226 | } |
| 227 | return "" |
| 228 | } |