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 | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 31 | type PythonBinaryBaseProperties 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. |
| 36 | Main string |
| 37 | |
| 38 | // set the name of the output binary. |
| 39 | Stem string |
| 40 | |
| 41 | // append to the name of the output binary. |
| 42 | Suffix string |
| 43 | } |
| 44 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 45 | type pythonBinaryBase struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 46 | pythonBaseModule |
| 47 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 48 | binaryProperties PythonBinaryBaseProperties |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 49 | |
| 50 | // soong_zip arguments from all its dependencies. |
| 51 | depsParSpecs []parSpec |
| 52 | |
| 53 | // Python runfiles paths from all its dependencies. |
| 54 | depsPyRunfiles []string |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 57 | type PythonBinaryHost struct { |
| 58 | pythonBinaryBase |
| 59 | } |
| 60 | |
| 61 | var _ PythonSubModule = (*PythonBinaryHost)(nil) |
| 62 | |
| 63 | type pythonBinaryHostDecorator struct { |
| 64 | pythonDecorator |
| 65 | } |
| 66 | |
| 67 | func (p *pythonBinaryHostDecorator) install(ctx android.ModuleContext, file android.Path) { |
| 68 | p.pythonDecorator.baseInstaller.install(ctx, file) |
| 69 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 70 | |
| 71 | var ( |
| 72 | stubTemplateHost = "build/soong/python/scripts/stub_template_host.txt" |
| 73 | ) |
| 74 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame^] | 75 | func PythonBinaryHostFactory() android.Module { |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 76 | decorator := &pythonBinaryHostDecorator{ |
| 77 | pythonDecorator: pythonDecorator{baseInstaller: NewPythonInstaller("bin")}} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 78 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 79 | module := &PythonBinaryHost{} |
| 80 | module.pythonBaseModule.installer = decorator |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame^] | 81 | module.AddProperties(&module.binaryProperties) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 82 | |
| 83 | return InitPythonBaseModule(&module.pythonBinaryBase.pythonBaseModule, |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame^] | 84 | &module.pythonBinaryBase, android.HostSupportedNoCross) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 87 | func (p *pythonBinaryBase) GeneratePythonBuildActions(ctx android.ModuleContext) android.OptionalPath { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 88 | p.pythonBaseModule.GeneratePythonBuildActions(ctx) |
| 89 | |
| 90 | // no Python source file for compiling par file. |
| 91 | if len(p.pythonBaseModule.srcsPathMappings) == 0 && len(p.depsPyRunfiles) == 0 { |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 92 | return android.OptionalPath{} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // the runfiles packages needs to be populated with "__init__.py". |
| 96 | newPyPkgs := []string{} |
| 97 | // the set to de-duplicate the new Python packages above. |
| 98 | newPyPkgSet := make(map[string]bool) |
| 99 | // the runfiles dirs have been treated as packages. |
| 100 | existingPyPkgSet := make(map[string]bool) |
| 101 | |
| 102 | wholePyRunfiles := []string{} |
| 103 | for _, path := range p.pythonBaseModule.srcsPathMappings { |
| 104 | wholePyRunfiles = append(wholePyRunfiles, path.dest) |
| 105 | } |
| 106 | wholePyRunfiles = append(wholePyRunfiles, p.depsPyRunfiles...) |
| 107 | |
| 108 | // find all the runfiles dirs which have been treated as packages. |
| 109 | for _, path := range wholePyRunfiles { |
| 110 | if filepath.Base(path) != initFileName { |
| 111 | continue |
| 112 | } |
| 113 | existingPyPkg := PathBeforeLastSlash(path) |
| 114 | if _, found := existingPyPkgSet[existingPyPkg]; found { |
| 115 | panic(fmt.Errorf("found init file path duplicates: %q for module: %q.", |
| 116 | path, ctx.ModuleName())) |
| 117 | } else { |
| 118 | existingPyPkgSet[existingPyPkg] = true |
| 119 | } |
| 120 | parentPath := PathBeforeLastSlash(existingPyPkg) |
| 121 | populateNewPyPkgs(parentPath, existingPyPkgSet, newPyPkgSet, &newPyPkgs) |
| 122 | } |
| 123 | |
| 124 | // create new packages under runfiles tree. |
| 125 | for _, path := range wholePyRunfiles { |
| 126 | if filepath.Base(path) == initFileName { |
| 127 | continue |
| 128 | } |
| 129 | parentPath := PathBeforeLastSlash(path) |
| 130 | populateNewPyPkgs(parentPath, existingPyPkgSet, newPyPkgSet, &newPyPkgs) |
| 131 | } |
| 132 | |
| 133 | main := p.getPyMainFile(ctx) |
| 134 | if main == "" { |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 135 | return android.OptionalPath{} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 136 | } |
| 137 | interp := p.getInterpreter(ctx) |
| 138 | if interp == "" { |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 139 | return android.OptionalPath{} |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // we need remove "runfiles/" suffix since stub script starts |
| 143 | // searching for main file in each sub-dir of "runfiles" directory tree. |
| 144 | binFile := registerBuildActionForParFile(ctx, p.getInterpreter(ctx), |
| 145 | strings.TrimPrefix(main, runFiles+"/"), p.getStem(ctx), |
| 146 | newPyPkgs, append(p.depsParSpecs, p.pythonBaseModule.parSpec)) |
| 147 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 148 | return android.OptionalPathForPath(binFile) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // get interpreter path. |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 152 | func (p *pythonBinaryBase) getInterpreter(ctx android.ModuleContext) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 153 | var interp string |
| 154 | switch p.pythonBaseModule.properties.ActualVersion { |
| 155 | case pyVersion2: |
| 156 | interp = "python2" |
| 157 | case pyVersion3: |
| 158 | interp = "python3" |
| 159 | default: |
| 160 | panic(fmt.Errorf("unknown Python actualVersion: %q for module: %q.", |
| 161 | p.properties.ActualVersion, ctx.ModuleName())) |
| 162 | } |
| 163 | |
| 164 | return interp |
| 165 | } |
| 166 | |
| 167 | // find main program path within runfiles tree. |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 168 | func (p *pythonBinaryBase) getPyMainFile(ctx android.ModuleContext) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 169 | var main string |
| 170 | if p.binaryProperties.Main == "" { |
| 171 | main = p.BaseModuleName() + pyExt |
| 172 | } else { |
| 173 | main = p.binaryProperties.Main |
| 174 | } |
| 175 | |
| 176 | for _, path := range p.pythonBaseModule.srcsPathMappings { |
| 177 | if main == path.src.Rel() { |
| 178 | return path.dest |
| 179 | } |
| 180 | } |
| 181 | ctx.PropertyErrorf("main", "%q is not listed in srcs.", main) |
| 182 | |
| 183 | return "" |
| 184 | } |
| 185 | |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 186 | func (p *pythonBinaryBase) getStem(ctx android.ModuleContext) string { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 187 | stem := ctx.ModuleName() |
| 188 | if p.binaryProperties.Stem != "" { |
| 189 | stem = p.binaryProperties.Stem |
| 190 | } |
| 191 | |
| 192 | return stem + p.binaryProperties.Suffix |
| 193 | } |
| 194 | |
| 195 | // Sets the given directory and all its ancestor directories as Python packages. |
| 196 | func populateNewPyPkgs(pkgPath string, existingPyPkgSet, |
| 197 | newPyPkgSet map[string]bool, newPyPkgs *[]string) { |
| 198 | for pkgPath != "" { |
| 199 | if _, found := existingPyPkgSet[pkgPath]; found { |
| 200 | break |
| 201 | } |
| 202 | if _, found := newPyPkgSet[pkgPath]; !found { |
| 203 | newPyPkgSet[pkgPath] = true |
| 204 | *newPyPkgs = append(*newPyPkgs, pkgPath) |
| 205 | // Gets its ancestor directory by trimming last slash. |
| 206 | pkgPath = PathBeforeLastSlash(pkgPath) |
| 207 | } else { |
| 208 | break |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | // filepath.Dir("abc") -> "." and filepath.Dir("/abc") -> "/". However, |
| 214 | // the PathBeforeLastSlash() will return "" for both cases above. |
| 215 | func PathBeforeLastSlash(path string) string { |
| 216 | if idx := strings.LastIndex(path, "/"); idx != -1 { |
| 217 | return path[:idx] |
| 218 | } |
| 219 | return "" |
| 220 | } |