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