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 "Base" module type for building Python program. |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "path/filepath" |
| 22 | "regexp" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 23 | "strings" |
| 24 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 25 | "android/soong/bazel" |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 26 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 27 | "github.com/google/blueprint" |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 28 | "github.com/google/blueprint/proptools" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 29 | |
| 30 | "android/soong/android" |
| 31 | ) |
| 32 | |
| 33 | func init() { |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 34 | registerPythonMutators(android.InitRegistrationContext) |
| 35 | } |
| 36 | |
| 37 | func registerPythonMutators(ctx android.RegistrationContext) { |
| 38 | ctx.PreDepsMutators(RegisterPythonPreDepsMutators) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 41 | // Exported to support other packages using Python modules in tests. |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 42 | func RegisterPythonPreDepsMutators(ctx android.RegisterMutatorsContext) { |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 43 | ctx.BottomUp("python_version", versionSplitMutator()).Parallel() |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 46 | // the version-specific properties that apply to python modules. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 47 | type VersionProperties struct { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 48 | // whether the module is required to be built with this version. |
| 49 | // Defaults to true for Python 3, and false otherwise. |
Liz Kammer | 59c0eae | 2021-09-17 17:48:05 -0400 | [diff] [blame] | 50 | Enabled *bool |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 51 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 52 | // list of source files specific to this Python version. |
| 53 | // Using the syntax ":module", srcs may reference the outputs of other modules that produce source files, |
| 54 | // e.g. genrule or filegroup. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 55 | Srcs []string `android:"path,arch_variant"` |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 56 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 57 | // list of source files that should not be used to build the Python module for this version. |
| 58 | // This is most useful to remove files that are not common to all Python versions. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 59 | Exclude_srcs []string `android:"path,arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 60 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 61 | // list of the Python libraries used only for this Python version. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 62 | Libs []string `android:"arch_variant"` |
| 63 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 64 | // whether the binary is required to be built with embedded launcher for this version, defaults to false. |
Liz Kammer | 59c0eae | 2021-09-17 17:48:05 -0400 | [diff] [blame] | 65 | Embedded_launcher *bool // TODO(b/174041232): Remove this property |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 68 | // properties that apply to all python modules |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 69 | type BaseProperties struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 70 | // the package path prefix within the output artifact at which to place the source/data |
| 71 | // files of the current module. |
| 72 | // eg. Pkg_path = "a/b/c"; Other packages can reference this module by using |
| 73 | // (from a.b.c import ...) statement. |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 74 | // if left unspecified, all the source/data files path is unchanged within zip file. |
Liz Kammer | 59c0eae | 2021-09-17 17:48:05 -0400 | [diff] [blame] | 75 | Pkg_path *string |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 76 | |
| 77 | // true, if the Python module is used internally, eg, Python std libs. |
Liz Kammer | 59c0eae | 2021-09-17 17:48:05 -0400 | [diff] [blame] | 78 | Is_internal *bool |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 79 | |
| 80 | // list of source (.py) files compatible both with Python2 and Python3 used to compile the |
| 81 | // Python module. |
| 82 | // srcs may reference the outputs of other modules that produce source files like genrule |
| 83 | // or filegroup using the syntax ":module". |
| 84 | // Srcs has to be non-empty. |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 85 | Srcs []string `android:"path,arch_variant"` |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 86 | |
| 87 | // list of source files that should not be used to build the C/C++ module. |
| 88 | // This is most useful in the arch/multilib variants to remove non-common files |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 89 | Exclude_srcs []string `android:"path,arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 90 | |
| 91 | // list of files or filegroup modules that provide data that should be installed alongside |
| 92 | // the test. the file extension can be arbitrary except for (.py). |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 93 | Data []string `android:"path,arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 94 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 95 | // list of java modules that provide data that should be installed alongside the test. |
| 96 | Java_data []string |
| 97 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 98 | // list of the Python libraries compatible both with Python2 and Python3. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 99 | Libs []string `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 100 | |
| 101 | Version struct { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 102 | // Python2-specific properties, including whether Python2 is supported for this module |
| 103 | // and version-specific sources, exclusions and dependencies. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 104 | Py2 VersionProperties `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 105 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 106 | // Python3-specific properties, including whether Python3 is supported for this module |
| 107 | // and version-specific sources, exclusions and dependencies. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 108 | Py3 VersionProperties `android:"arch_variant"` |
| 109 | } `android:"arch_variant"` |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 110 | |
| 111 | // the actual version each module uses after variations created. |
| 112 | // this property name is hidden from users' perspectives, and soong will populate it during |
| 113 | // runtime. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 114 | Actual_version string `blueprint:"mutated"` |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 115 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 116 | // whether the module is required to be built with actual_version. |
| 117 | // this is set by the python version mutator based on version-specific properties |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 118 | Enabled *bool `blueprint:"mutated"` |
| 119 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 120 | // whether the binary is required to be built with embedded launcher for this actual_version. |
| 121 | // this is set by the python version mutator based on version-specific properties |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 122 | Embedded_launcher *bool `blueprint:"mutated"` |
Cole Faust | 43ac21f | 2022-09-19 11:19:52 -0700 | [diff] [blame] | 123 | |
| 124 | Proto struct { |
| 125 | // Whether generated python protos should include the pkg_path in |
| 126 | // their import statements. This is a temporary flag to help transition to |
| 127 | // the new behavior where this is always true. It will be removed after all |
| 128 | // usages of protos with pkg_path have been updated. The default is currently |
| 129 | // false. |
| 130 | Respect_pkg_path *bool |
| 131 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 132 | } |
| 133 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 134 | type baseAttributes struct { |
| 135 | // TODO(b/200311466): Probably not translate b/c Bazel has no good equiv |
| 136 | //Pkg_path bazel.StringAttribute |
| 137 | // TODO: Related to Pkg_bath and similarLy gated |
| 138 | //Is_internal bazel.BoolAttribute |
| 139 | // Combines Srcs and Exclude_srcs |
| 140 | Srcs bazel.LabelListAttribute |
| 141 | Deps bazel.LabelListAttribute |
| 142 | // Combines Data and Java_data (invariant) |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame] | 143 | Data bazel.LabelListAttribute |
| 144 | Imports bazel.StringListAttribute |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 147 | // Used to store files of current module after expanding dependencies |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 148 | type pathMapping struct { |
| 149 | dest string |
| 150 | src android.Path |
| 151 | } |
| 152 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 153 | type Module struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 154 | android.ModuleBase |
Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 155 | android.DefaultableModuleBase |
Jingwen Chen | 13b9b42 | 2021-03-08 07:32:28 -0500 | [diff] [blame] | 156 | android.BazelModuleBase |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 157 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 158 | properties BaseProperties |
| 159 | protoProperties android.ProtoProperties |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 160 | |
| 161 | // initialize before calling Init |
| 162 | hod android.HostOrDeviceSupported |
| 163 | multilib android.Multilib |
| 164 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 165 | // interface used to bootstrap .par executable when embedded_launcher is true |
| 166 | // this should be set by Python modules which are runnable, e.g. binaries and tests |
| 167 | // bootstrapper might be nil (e.g. Python library module). |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 168 | bootstrapper bootstrapper |
| 169 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 170 | // interface that implements functions required for installation |
| 171 | // this should be set by Python modules which are runnable, e.g. binaries and tests |
| 172 | // installer might be nil (e.g. Python library module). |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 173 | installer installer |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 174 | |
| 175 | // the Python files of current module after expanding source dependencies. |
| 176 | // pathMapping: <dest: runfile_path, src: source_path> |
| 177 | srcsPathMappings []pathMapping |
| 178 | |
| 179 | // the data files of current module after expanding source dependencies. |
| 180 | // pathMapping: <dest: runfile_path, src: source_path> |
| 181 | dataPathMappings []pathMapping |
| 182 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 183 | // the zip filepath for zipping current module source/data files. |
| 184 | srcsZip android.Path |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 185 | |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 186 | // dependency modules' zip filepath for zipping current module source/data files. |
| 187 | depsSrcsZips android.Paths |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 188 | |
| 189 | // (.intermediate) module output path as installation source. |
| 190 | installSource android.OptionalPath |
| 191 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 192 | // Map to ensure sub-part of the AndroidMk for this module is only added once |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 193 | subAndroidMkOnce map[subAndroidMkProvider]bool |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 196 | // newModule generates new Python base module |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 197 | func newModule(hod android.HostOrDeviceSupported, multilib android.Multilib) *Module { |
| 198 | return &Module{ |
| 199 | hod: hod, |
| 200 | multilib: multilib, |
| 201 | } |
| 202 | } |
| 203 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 204 | func (m *Module) makeArchVariantBaseAttributes(ctx android.TopDownMutatorContext) baseAttributes { |
| 205 | var attrs baseAttributes |
| 206 | archVariantBaseProps := m.GetArchVariantProperties(ctx, &BaseProperties{}) |
| 207 | for axis, configToProps := range archVariantBaseProps { |
| 208 | for config, props := range configToProps { |
| 209 | if baseProps, ok := props.(*BaseProperties); ok { |
| 210 | attrs.Srcs.SetSelectValue(axis, config, |
| 211 | android.BazelLabelForModuleSrcExcludes(ctx, baseProps.Srcs, baseProps.Exclude_srcs)) |
| 212 | attrs.Deps.SetSelectValue(axis, config, |
| 213 | android.BazelLabelForModuleDeps(ctx, baseProps.Libs)) |
| 214 | data := android.BazelLabelForModuleSrc(ctx, baseProps.Data) |
| 215 | data.Append(android.BazelLabelForModuleSrc(ctx, baseProps.Java_data)) |
| 216 | attrs.Data.SetSelectValue(axis, config, data) |
| 217 | } |
| 218 | } |
| 219 | } |
Cole Faust | 53b6209 | 2022-05-12 15:37:02 -0700 | [diff] [blame] | 220 | |
| 221 | partitionedSrcs := bazel.PartitionLabelListAttribute(ctx, &attrs.Srcs, bazel.LabelPartitions{ |
| 222 | "proto": android.ProtoSrcLabelPartition, |
| 223 | "py": bazel.LabelPartition{Keep_remainder: true}, |
| 224 | }) |
| 225 | attrs.Srcs = partitionedSrcs["py"] |
| 226 | |
| 227 | if !partitionedSrcs["proto"].IsEmpty() { |
| 228 | protoInfo, _ := android.Bp2buildProtoProperties(ctx, &m.ModuleBase, partitionedSrcs["proto"]) |
| 229 | protoLabel := bazel.Label{Label: ":" + protoInfo.Name} |
| 230 | |
| 231 | pyProtoLibraryName := m.Name() + "_py_proto" |
| 232 | ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{ |
| 233 | Rule_class: "py_proto_library", |
| 234 | Bzl_load_location: "//build/bazel/rules/python:py_proto.bzl", |
| 235 | }, android.CommonAttributes{ |
| 236 | Name: pyProtoLibraryName, |
| 237 | }, &bazelPythonProtoLibraryAttributes{ |
| 238 | Deps: bazel.MakeSingleLabelListAttribute(protoLabel), |
| 239 | }) |
| 240 | |
| 241 | attrs.Deps.Add(bazel.MakeLabelAttribute(":" + pyProtoLibraryName)) |
| 242 | } |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame] | 243 | |
| 244 | // Bazel normally requires `import path.from.top.of.tree` statements in |
| 245 | // python code, but with soong you can directly import modules from libraries. |
| 246 | // Add "imports" attributes to the bazel library so it matches soong's behavior. |
| 247 | imports := "." |
| 248 | if m.properties.Pkg_path != nil { |
| 249 | // TODO(b/215119317) This is a hack to handle the fact that we don't convert |
| 250 | // pkg_path properly right now. If the folder structure that contains this |
| 251 | // Android.bp file matches pkg_path, we can set imports to an appropriate |
| 252 | // number of ../..s to emulate moving the files under a pkg_path folder. |
| 253 | pkg_path := filepath.Clean(*m.properties.Pkg_path) |
| 254 | if strings.HasPrefix(pkg_path, "/") { |
| 255 | ctx.ModuleErrorf("pkg_path cannot start with a /: %s", pkg_path) |
| 256 | } |
| 257 | |
| 258 | if !strings.HasSuffix(ctx.ModuleDir(), "/"+pkg_path) && ctx.ModuleDir() != pkg_path { |
| 259 | ctx.ModuleErrorf("Currently, bp2build only supports pkg_paths that are the same as the folders the Android.bp file is in. pkg_path: %s, module directory: %s", pkg_path, ctx.ModuleDir()) |
| 260 | } |
| 261 | numFolders := strings.Count(pkg_path, "/") + 1 |
| 262 | dots := make([]string, numFolders) |
| 263 | for i := 0; i < numFolders; i++ { |
| 264 | dots[i] = ".." |
| 265 | } |
| 266 | imports = strings.Join(dots, "/") |
| 267 | } |
| 268 | attrs.Imports = bazel.MakeStringListAttribute([]string{imports}) |
| 269 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 270 | return attrs |
| 271 | } |
| 272 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 273 | // bootstrapper interface should be implemented for runnable modules, e.g. binary and test |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 274 | type bootstrapper interface { |
| 275 | bootstrapperProps() []interface{} |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 276 | bootstrap(ctx android.ModuleContext, ActualVersion string, embeddedLauncher bool, |
| 277 | srcsPathMappings []pathMapping, srcsZip android.Path, |
| 278 | depsSrcsZips android.Paths) android.OptionalPath |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 279 | |
| 280 | autorun() bool |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 283 | // installer interface should be implemented for installable modules, e.g. binary and test |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 284 | type installer interface { |
| 285 | install(ctx android.ModuleContext, path android.Path) |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 286 | setAndroidMkSharedLibs(sharedLibs []string) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 289 | // interface implemented by Python modules to provide source and data mappings and zip to python |
| 290 | // modules that depend on it |
| 291 | type pythonDependency interface { |
| 292 | getSrcsPathMappings() []pathMapping |
| 293 | getDataPathMappings() []pathMapping |
| 294 | getSrcsZip() android.Path |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 297 | // getSrcsPathMappings gets this module's path mapping of src source path : runfiles destination |
| 298 | func (p *Module) getSrcsPathMappings() []pathMapping { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 299 | return p.srcsPathMappings |
| 300 | } |
| 301 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 302 | // getSrcsPathMappings gets this module's path mapping of data source path : runfiles destination |
| 303 | func (p *Module) getDataPathMappings() []pathMapping { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 304 | return p.dataPathMappings |
| 305 | } |
| 306 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 307 | // getSrcsZip returns the filepath where the current module's source/data files are zipped. |
| 308 | func (p *Module) getSrcsZip() android.Path { |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 309 | return p.srcsZip |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 312 | var _ pythonDependency = (*Module)(nil) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 313 | |
Liz Kammer | d8dceb0 | 2020-11-24 08:36:14 -0800 | [diff] [blame] | 314 | var _ android.AndroidMkEntriesProvider = (*Module)(nil) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 315 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 316 | func (p *Module) init(additionalProps ...interface{}) android.Module { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 317 | p.AddProperties(&p.properties, &p.protoProperties) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 318 | |
| 319 | // Add additional properties for bootstrapping/installation |
| 320 | // This is currently tied to the bootstrapper interface; |
| 321 | // however, these are a combination of properties for the installation and bootstrapping of a module |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 322 | if p.bootstrapper != nil { |
| 323 | p.AddProperties(p.bootstrapper.bootstrapperProps()...) |
| 324 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 325 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 326 | android.InitAndroidArchModule(p, p.hod, p.multilib) |
Nan Zhang | a3fc4ba | 2017-07-20 17:43:37 -0700 | [diff] [blame] | 327 | android.InitDefaultableModule(p) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 328 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 329 | return p |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 332 | // Python-specific tag to transfer information on the purpose of a dependency. |
| 333 | // This is used when adding a dependency on a module, which can later be accessed when visiting |
| 334 | // dependencies. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 335 | type dependencyTag struct { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 336 | blueprint.BaseDependencyTag |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 337 | name string |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 340 | // Python-specific tag that indicates that installed files of this module should depend on installed |
| 341 | // files of the dependency |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 342 | type installDependencyTag struct { |
| 343 | blueprint.BaseDependencyTag |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 344 | // embedding this struct provides the installation dependency requirement |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 345 | android.InstallAlwaysNeededDependencyTag |
| 346 | name string |
| 347 | } |
| 348 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 349 | var ( |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 350 | pythonLibTag = dependencyTag{name: "pythonLib"} |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 351 | javaDataTag = dependencyTag{name: "javaData"} |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 352 | launcherTag = dependencyTag{name: "launcher"} |
Colin Cross | e9fe294 | 2020-11-10 18:12:15 -0800 | [diff] [blame] | 353 | launcherSharedLibTag = installDependencyTag{name: "launcherSharedLib"} |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 354 | pathComponentRegexp = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_-]*$`) |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 355 | pyExt = ".py" |
| 356 | protoExt = ".proto" |
| 357 | pyVersion2 = "PY2" |
| 358 | pyVersion3 = "PY3" |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 359 | internalPath = "internal" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 360 | ) |
| 361 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 362 | // versionSplitMutator creates version variants for modules and appends the version-specific |
| 363 | // properties for a given variant to the properties in the variant module |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 364 | func versionSplitMutator() func(android.BottomUpMutatorContext) { |
| 365 | return func(mctx android.BottomUpMutatorContext) { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 366 | if base, ok := mctx.Module().(*Module); ok { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 367 | versionNames := []string{} |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 368 | // collect version specific properties, so that we can merge version-specific properties |
| 369 | // into the module's overall properties |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 370 | versionProps := []VersionProperties{} |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 371 | // PY3 is first so that we alias the PY3 variant rather than PY2 if both |
| 372 | // are available |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 373 | if proptools.BoolDefault(base.properties.Version.Py3.Enabled, true) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 374 | versionNames = append(versionNames, pyVersion3) |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 375 | versionProps = append(versionProps, base.properties.Version.Py3) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 376 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 377 | if proptools.BoolDefault(base.properties.Version.Py2.Enabled, false) { |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 378 | versionNames = append(versionNames, pyVersion2) |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 379 | versionProps = append(versionProps, base.properties.Version.Py2) |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 380 | } |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 381 | modules := mctx.CreateLocalVariations(versionNames...) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 382 | // Alias module to the first variant |
Liz Kammer | dd849a8 | 2020-06-12 16:38:45 -0700 | [diff] [blame] | 383 | if len(versionNames) > 0 { |
| 384 | mctx.AliasVariation(versionNames[0]) |
| 385 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 386 | for i, v := range versionNames { |
| 387 | // set the actual version for Python module. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 388 | modules[i].(*Module).properties.Actual_version = v |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 389 | // append versioned properties for the Python module to the overall properties |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 390 | err := proptools.AppendMatchingProperties([]interface{}{&modules[i].(*Module).properties}, &versionProps[i], nil) |
| 391 | if err != nil { |
| 392 | panic(err) |
| 393 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 399 | // HostToolPath returns a path if appropriate such that this module can be used as a host tool, |
| 400 | // fulfilling HostToolProvider interface. |
Nan Zhang | b8bdacf | 2017-12-06 15:13:10 -0800 | [diff] [blame] | 401 | func (p *Module) HostToolPath() android.OptionalPath { |
Rob Seymour | 925aa09 | 2021-08-10 20:42:03 +0000 | [diff] [blame] | 402 | if p.installer != nil { |
| 403 | if bin, ok := p.installer.(*binaryDecorator); ok { |
| 404 | // TODO: This should only be set when building host binaries -- tests built for device would be |
| 405 | // setting this incorrectly. |
| 406 | return android.OptionalPathForPath(bin.path) |
| 407 | } |
Nan Zhang | b8bdacf | 2017-12-06 15:13:10 -0800 | [diff] [blame] | 408 | } |
Rob Seymour | 925aa09 | 2021-08-10 20:42:03 +0000 | [diff] [blame] | 409 | |
| 410 | return android.OptionalPath{} |
| 411 | |
Nan Zhang | b8bdacf | 2017-12-06 15:13:10 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 414 | // OutputFiles returns output files based on given tag, returns an error if tag is unsupported. |
Liz Kammer | e0070ee | 2020-06-22 11:52:59 -0700 | [diff] [blame] | 415 | func (p *Module) OutputFiles(tag string) (android.Paths, error) { |
| 416 | switch tag { |
| 417 | case "": |
| 418 | if outputFile := p.installSource; outputFile.Valid() { |
| 419 | return android.Paths{outputFile.Path()}, nil |
| 420 | } |
| 421 | return android.Paths{}, nil |
| 422 | default: |
| 423 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 424 | } |
| 425 | } |
| 426 | |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 427 | func (p *Module) isEmbeddedLauncherEnabled() bool { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 428 | return p.installer != nil && Bool(p.properties.Embedded_launcher) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 431 | func anyHasExt(paths []string, ext string) bool { |
| 432 | for _, p := range paths { |
| 433 | if filepath.Ext(p) == ext { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 434 | return true |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | return false |
| 439 | } |
| 440 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 441 | func (p *Module) anySrcHasExt(ctx android.BottomUpMutatorContext, ext string) bool { |
| 442 | return anyHasExt(p.properties.Srcs, ext) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 445 | // DepsMutator mutates dependencies for this module: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 446 | // - handles proto dependencies, |
| 447 | // - if required, specifies launcher and adds launcher dependencies, |
| 448 | // - applies python version mutations to Python dependencies |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 449 | func (p *Module) DepsMutator(ctx android.BottomUpMutatorContext) { |
Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 450 | android.ProtoDeps(ctx, &p.protoProperties) |
| 451 | |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 452 | versionVariation := []blueprint.Variation{ |
| 453 | {"python_version", p.properties.Actual_version}, |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 454 | } |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 455 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 456 | // If sources contain a proto file, add dependency on libprotobuf-python |
| 457 | if p.anySrcHasExt(ctx, protoExt) && p.Name() != "libprotobuf-python" { |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 458 | ctx.AddVariationDependencies(versionVariation, pythonLibTag, "libprotobuf-python") |
| 459 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 460 | |
| 461 | // Add python library dependencies for this python version variation |
Colin Cross | e20113d | 2020-11-22 19:37:44 -0800 | [diff] [blame] | 462 | ctx.AddVariationDependencies(versionVariation, pythonLibTag, android.LastUniqueStrings(p.properties.Libs)...) |
Liz Kammer | 7e93e5b | 2020-10-30 15:44:09 -0700 | [diff] [blame] | 463 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 464 | // If this module will be installed and has an embedded launcher, we need to add dependencies for: |
| 465 | // * standard library |
| 466 | // * launcher |
| 467 | // * shared dependencies of the launcher |
| 468 | if p.installer != nil && p.isEmbeddedLauncherEnabled() { |
| 469 | var stdLib string |
| 470 | var launcherModule string |
| 471 | // Add launcher shared lib dependencies. Ideally, these should be |
| 472 | // derived from the `shared_libs` property of the launcher. However, we |
| 473 | // cannot read the property at this stage and it will be too late to add |
| 474 | // dependencies later. |
| 475 | launcherSharedLibDeps := []string{ |
| 476 | "libsqlite", |
| 477 | } |
| 478 | // Add launcher-specific dependencies for bionic |
| 479 | if ctx.Target().Os.Bionic() { |
| 480 | launcherSharedLibDeps = append(launcherSharedLibDeps, "libc", "libdl", "libm") |
| 481 | } |
Colin Cross | 4111c52 | 2022-03-08 11:56:27 -0800 | [diff] [blame] | 482 | if ctx.Target().Os == android.LinuxMusl && !ctx.Config().HostStaticBinaries() { |
| 483 | launcherSharedLibDeps = append(launcherSharedLibDeps, "libc_musl") |
| 484 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 485 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 486 | switch p.properties.Actual_version { |
| 487 | case pyVersion2: |
| 488 | stdLib = "py2-stdlib" |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 489 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 490 | launcherModule = "py2-launcher" |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 491 | if p.bootstrapper.autorun() { |
| 492 | launcherModule = "py2-launcher-autorun" |
| 493 | } |
Colin Cross | 4111c52 | 2022-03-08 11:56:27 -0800 | [diff] [blame] | 494 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 495 | launcherSharedLibDeps = append(launcherSharedLibDeps, "libc++") |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 496 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 497 | case pyVersion3: |
| 498 | stdLib = "py3-stdlib" |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 499 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 500 | launcherModule = "py3-launcher" |
Dan Willemsen | 8d4d7be | 2019-11-04 19:21:04 -0800 | [diff] [blame] | 501 | if p.bootstrapper.autorun() { |
| 502 | launcherModule = "py3-launcher-autorun" |
| 503 | } |
Colin Cross | 4111c52 | 2022-03-08 11:56:27 -0800 | [diff] [blame] | 504 | if ctx.Config().HostStaticBinaries() && ctx.Target().Os == android.LinuxMusl { |
| 505 | launcherModule += "-static" |
| 506 | } |
Dan Willemsen | 8d4d7be | 2019-11-04 19:21:04 -0800 | [diff] [blame] | 507 | |
Dan Willemsen | d7a1dee | 2020-01-20 22:08:20 -0800 | [diff] [blame] | 508 | if ctx.Device() { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 509 | launcherSharedLibDeps = append(launcherSharedLibDeps, "liblog") |
Dan Willemsen | d7a1dee | 2020-01-20 22:08:20 -0800 | [diff] [blame] | 510 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 511 | default: |
| 512 | panic(fmt.Errorf("unknown Python Actual_version: %q for module: %q.", |
| 513 | p.properties.Actual_version, ctx.ModuleName())) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 514 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 515 | ctx.AddVariationDependencies(versionVariation, pythonLibTag, stdLib) |
| 516 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherTag, launcherModule) |
| 517 | ctx.AddFarVariationDependencies(ctx.Target().Variations(), launcherSharedLibTag, launcherSharedLibDeps...) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 518 | } |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 519 | |
| 520 | // Emulate the data property for java_data but with the arch variation overridden to "common" |
| 521 | // so that it can point to java modules. |
| 522 | javaDataVariation := []blueprint.Variation{{"arch", android.Common.String()}} |
| 523 | ctx.AddVariationDependencies(javaDataVariation, javaDataTag, p.properties.Java_data...) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 526 | func (p *Module) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 527 | p.generatePythonBuildActions(ctx) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 528 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 529 | // Only Python binary and test modules have non-empty bootstrapper. |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 530 | if p.bootstrapper != nil { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 531 | // if the module is being installed, we need to collect all transitive dependencies to embed in |
| 532 | // the final par |
| 533 | p.collectPathsFromTransitiveDeps(ctx) |
| 534 | // bootstrap the module, including resolving main file, getting launcher path, and |
| 535 | // registering actions to build the par file |
| 536 | // bootstrap returns the binary output path |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 537 | p.installSource = p.bootstrapper.bootstrap(ctx, p.properties.Actual_version, |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 538 | p.isEmbeddedLauncherEnabled(), p.srcsPathMappings, p.srcsZip, p.depsSrcsZips) |
Nan Zhang | 5323f8e | 2017-05-10 13:37:54 -0700 | [diff] [blame] | 539 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 540 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 541 | // Only Python binary and test modules have non-empty installer. |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 542 | if p.installer != nil { |
| 543 | var sharedLibs []string |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 544 | // if embedded launcher is enabled, we need to collect the shared library depenendencies of the |
| 545 | // launcher |
Colin Cross | 0e44615 | 2021-05-03 13:35:32 -0700 | [diff] [blame] | 546 | for _, dep := range ctx.GetDirectDepsWithTag(launcherSharedLibTag) { |
| 547 | sharedLibs = append(sharedLibs, ctx.OtherModuleName(dep)) |
| 548 | } |
| 549 | |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 550 | p.installer.setAndroidMkSharedLibs(sharedLibs) |
| 551 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 552 | // Install the par file from installSource |
Logan Chien | 02880e4 | 2018-11-06 17:30:35 +0800 | [diff] [blame] | 553 | if p.installSource.Valid() { |
| 554 | p.installer.install(ctx, p.installSource.Path()) |
| 555 | } |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 556 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 559 | // generatePythonBuildActions performs build actions common to all Python modules |
| 560 | func (p *Module) generatePythonBuildActions(ctx android.ModuleContext) { |
| 561 | expandedSrcs := android.PathsForModuleSrcExcludes(ctx, p.properties.Srcs, p.properties.Exclude_srcs) |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 562 | requiresSrcs := true |
| 563 | if p.bootstrapper != nil && !p.bootstrapper.autorun() { |
| 564 | requiresSrcs = false |
| 565 | } |
| 566 | if len(expandedSrcs) == 0 && requiresSrcs { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 567 | ctx.ModuleErrorf("doesn't have any source files!") |
| 568 | } |
| 569 | |
| 570 | // expand data files from "data" property. |
Colin Cross | 8a49795 | 2019-03-05 22:25:09 -0800 | [diff] [blame] | 571 | expandedData := android.PathsForModuleSrc(ctx, p.properties.Data) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 572 | |
Colin Cross | 1bc6393 | 2020-11-22 20:12:45 -0800 | [diff] [blame] | 573 | // Emulate the data property for java_data dependencies. |
| 574 | for _, javaData := range ctx.GetDirectDepsWithTag(javaDataTag) { |
| 575 | expandedData = append(expandedData, android.OutputFilesForModule(ctx, javaData, "")...) |
| 576 | } |
| 577 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 578 | // Validate pkg_path property |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 579 | pkgPath := String(p.properties.Pkg_path) |
| 580 | if pkgPath != "" { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 581 | // TODO: export validation from android/paths.go handling to replace this duplicated functionality |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 582 | pkgPath = filepath.Clean(String(p.properties.Pkg_path)) |
| 583 | if pkgPath == ".." || strings.HasPrefix(pkgPath, "../") || |
| 584 | strings.HasPrefix(pkgPath, "/") { |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 585 | ctx.PropertyErrorf("pkg_path", |
| 586 | "%q must be a relative path contained in par file.", |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 587 | String(p.properties.Pkg_path)) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 588 | return |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 589 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 590 | } |
| 591 | // If property Is_internal is set, prepend pkgPath with internalPath |
| 592 | if proptools.BoolDefault(p.properties.Is_internal, false) { |
| 593 | pkgPath = filepath.Join(internalPath, pkgPath) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 594 | } |
| 595 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 596 | // generate src:destination path mappings for this module |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 597 | p.genModulePathMappings(ctx, pkgPath, expandedSrcs, expandedData) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 598 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 599 | // generate the zipfile of all source and data files |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 600 | p.srcsZip = p.createSrcsZip(ctx, pkgPath) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 603 | func isValidPythonPath(path string) error { |
| 604 | identifiers := strings.Split(strings.TrimSuffix(path, filepath.Ext(path)), "/") |
| 605 | for _, token := range identifiers { |
| 606 | if !pathComponentRegexp.MatchString(token) { |
| 607 | return fmt.Errorf("the path %q contains invalid subpath %q. "+ |
| 608 | "Subpaths must be at least one character long. "+ |
| 609 | "The first character must an underscore or letter. "+ |
| 610 | "Following characters may be any of: letter, digit, underscore, hyphen.", |
| 611 | path, token) |
| 612 | } |
| 613 | } |
| 614 | return nil |
| 615 | } |
| 616 | |
| 617 | // For this module, generate unique pathMappings: <dest: runfiles_path, src: source_path> |
| 618 | // for python/data files expanded from properties. |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 619 | func (p *Module) genModulePathMappings(ctx android.ModuleContext, pkgPath string, |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 620 | expandedSrcs, expandedData android.Paths) { |
| 621 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 622 | // check current module duplicates. |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 623 | destToPySrcs := make(map[string]string) |
| 624 | destToPyData := make(map[string]string) |
| 625 | |
| 626 | for _, s := range expandedSrcs { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 627 | if s.Ext() != pyExt && s.Ext() != protoExt { |
| 628 | ctx.PropertyErrorf("srcs", "found non (.py|.proto) file: %q!", s.String()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 629 | continue |
| 630 | } |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 631 | runfilesPath := filepath.Join(pkgPath, s.Rel()) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 632 | if err := isValidPythonPath(runfilesPath); err != nil { |
| 633 | ctx.PropertyErrorf("srcs", err.Error()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 634 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 635 | if !checkForDuplicateOutputPath(ctx, destToPySrcs, runfilesPath, s.String(), p.Name(), p.Name()) { |
| 636 | p.srcsPathMappings = append(p.srcsPathMappings, pathMapping{dest: runfilesPath, src: s}) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 637 | } |
| 638 | } |
| 639 | |
| 640 | for _, d := range expandedData { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 641 | if d.Ext() == pyExt || d.Ext() == protoExt { |
| 642 | ctx.PropertyErrorf("data", "found (.py|.proto) file: %q!", d.String()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 643 | continue |
| 644 | } |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 645 | runfilesPath := filepath.Join(pkgPath, d.Rel()) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 646 | if !checkForDuplicateOutputPath(ctx, destToPyData, runfilesPath, d.String(), p.Name(), p.Name()) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 647 | p.dataPathMappings = append(p.dataPathMappings, |
| 648 | pathMapping{dest: runfilesPath, src: d}) |
| 649 | } |
| 650 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 651 | } |
| 652 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 653 | // createSrcsZip registers build actions to zip current module's sources and data. |
Nan Zhang | 1db8540 | 2017-12-18 13:20:23 -0800 | [diff] [blame] | 654 | func (p *Module) createSrcsZip(ctx android.ModuleContext, pkgPath string) android.Path { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 655 | relativeRootMap := make(map[string]android.Paths) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 656 | pathMappings := append(p.srcsPathMappings, p.dataPathMappings...) |
| 657 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 658 | var protoSrcs android.Paths |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 659 | // "srcs" or "data" properties may contain filegroup so it might happen that |
| 660 | // the root directory for each source path is different. |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 661 | for _, path := range pathMappings { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 662 | // handle proto sources separately |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 663 | if path.src.Ext() == protoExt { |
| 664 | protoSrcs = append(protoSrcs, path.src) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 665 | } else { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 666 | var relativeRoot string |
| 667 | relativeRoot = strings.TrimSuffix(path.src.String(), path.src.Rel()) |
| 668 | if v, found := relativeRootMap[relativeRoot]; found { |
| 669 | relativeRootMap[relativeRoot] = append(v, path.src) |
| 670 | } else { |
| 671 | relativeRootMap[relativeRoot] = android.Paths{path.src} |
| 672 | } |
| 673 | } |
| 674 | } |
| 675 | var zips android.Paths |
| 676 | if len(protoSrcs) > 0 { |
Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 677 | protoFlags := android.GetProtoFlags(ctx, &p.protoProperties) |
| 678 | protoFlags.OutTypeFlag = "--python_out" |
| 679 | |
Cole Faust | 02ad401 | 2022-10-11 18:12:29 +0000 | [diff] [blame] | 680 | protosRespectPkgPath := proptools.BoolDefault(p.properties.Proto.Respect_pkg_path, true) |
Cole Faust | 43ac21f | 2022-09-19 11:19:52 -0700 | [diff] [blame] | 681 | pkgPathForProtos := pkgPath |
| 682 | if pkgPathForProtos != "" && protosRespectPkgPath { |
| 683 | pkgPathStagingDir := android.PathForModuleGen(ctx, "protos_staged_for_pkg_path") |
| 684 | rule := android.NewRuleBuilder(pctx, ctx) |
| 685 | var stagedProtoSrcs android.Paths |
| 686 | for _, srcFile := range protoSrcs { |
| 687 | stagedProtoSrc := pkgPathStagingDir.Join(ctx, pkgPath, srcFile.Rel()) |
| 688 | rule.Command().Text("mkdir -p").Flag(filepath.Base(stagedProtoSrc.String())) |
| 689 | rule.Command().Text("cp -f").Input(srcFile).Output(stagedProtoSrc) |
| 690 | stagedProtoSrcs = append(stagedProtoSrcs, stagedProtoSrc) |
| 691 | } |
| 692 | rule.Build("stage_protos_for_pkg_path", "Stage protos for pkg_path") |
| 693 | protoSrcs = stagedProtoSrcs |
| 694 | pkgPathForProtos = "" |
| 695 | } |
| 696 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 697 | for _, srcFile := range protoSrcs { |
Cole Faust | 43ac21f | 2022-09-19 11:19:52 -0700 | [diff] [blame] | 698 | zip := genProto(ctx, srcFile, protoFlags, pkgPathForProtos) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 699 | zips = append(zips, zip) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 703 | if len(relativeRootMap) > 0 { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 704 | // in order to keep stable order of soong_zip params, we sort the keys here. |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 705 | roots := android.SortedStringKeys(relativeRootMap) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 706 | |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame] | 707 | // Use -symlinks=false so that the symlinks in the bazel output directory are followed |
| 708 | parArgs := []string{"-symlinks=false"} |
Nan Zhang | f0c4e43 | 2018-05-22 14:50:18 -0700 | [diff] [blame] | 709 | if pkgPath != "" { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 710 | // use package path as path prefix |
Nan Zhang | f0c4e43 | 2018-05-22 14:50:18 -0700 | [diff] [blame] | 711 | parArgs = append(parArgs, `-P `+pkgPath) |
| 712 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 713 | paths := android.Paths{} |
| 714 | for _, root := range roots { |
| 715 | // specify relative root of file in following -f arguments |
| 716 | parArgs = append(parArgs, `-C `+root) |
| 717 | for _, path := range relativeRootMap[root] { |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 718 | parArgs = append(parArgs, `-f `+path.String()) |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 719 | paths = append(paths, path) |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | origSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".py.srcszip") |
| 724 | ctx.Build(pctx, android.BuildParams{ |
| 725 | Rule: zip, |
| 726 | Description: "python library archive", |
| 727 | Output: origSrcsZip, |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 728 | // as zip rule does not use $in, there is no real need to distinguish between Inputs and Implicits |
| 729 | Implicits: paths, |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 730 | Args: map[string]string{ |
| 731 | "args": strings.Join(parArgs, " "), |
| 732 | }, |
| 733 | }) |
| 734 | zips = append(zips, origSrcsZip) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 735 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 736 | // we may have multiple zips due to separate handling of proto source files |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 737 | if len(zips) == 1 { |
| 738 | return zips[0] |
| 739 | } else { |
| 740 | combinedSrcsZip := android.PathForModuleOut(ctx, ctx.ModuleName()+".srcszip") |
| 741 | ctx.Build(pctx, android.BuildParams{ |
| 742 | Rule: combineZip, |
| 743 | Description: "combine python library archive", |
| 744 | Output: combinedSrcsZip, |
| 745 | Inputs: zips, |
| 746 | }) |
| 747 | return combinedSrcsZip |
| 748 | } |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 749 | } |
| 750 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 751 | // isPythonLibModule returns whether the given module is a Python library Module or not |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 752 | func isPythonLibModule(module blueprint.Module) bool { |
| 753 | if m, ok := module.(*Module); ok { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 754 | return m.isLibrary() |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 755 | } |
| 756 | return false |
| 757 | } |
| 758 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 759 | // This is distinguished by the fact that Python libraries are not installable, while other Python |
| 760 | // modules are. |
| 761 | func (p *Module) isLibrary() bool { |
| 762 | // Python library has no bootstrapper or installer |
| 763 | return p.bootstrapper == nil && p.installer == nil |
| 764 | } |
| 765 | |
| 766 | func (p *Module) isBinary() bool { |
| 767 | _, ok := p.bootstrapper.(*binaryDecorator) |
| 768 | return ok |
| 769 | } |
| 770 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 771 | // collectPathsFromTransitiveDeps checks for source/data files for duplicate paths |
| 772 | // for module and its transitive dependencies and collects list of data/source file |
| 773 | // zips for transitive dependencies. |
| 774 | func (p *Module) collectPathsFromTransitiveDeps(ctx android.ModuleContext) { |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 775 | // fetch <runfiles_path, source_path> pairs from "src" and "data" properties to |
| 776 | // check duplicates. |
| 777 | destToPySrcs := make(map[string]string) |
| 778 | destToPyData := make(map[string]string) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 779 | for _, path := range p.srcsPathMappings { |
| 780 | destToPySrcs[path.dest] = path.src.String() |
| 781 | } |
| 782 | for _, path := range p.dataPathMappings { |
| 783 | destToPyData[path.dest] = path.src.String() |
| 784 | } |
| 785 | |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 786 | seen := make(map[android.Module]bool) |
| 787 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 788 | // visit all its dependencies in depth first. |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 789 | ctx.WalkDeps(func(child, parent android.Module) bool { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 790 | // we only collect dependencies tagged as python library deps |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 791 | if ctx.OtherModuleDependencyTag(child) != pythonLibTag { |
| 792 | return false |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 793 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 794 | if seen[child] { |
| 795 | return false |
| 796 | } |
| 797 | seen[child] = true |
Nan Zhang | b8fa197 | 2017-12-22 16:12:00 -0800 | [diff] [blame] | 798 | // Python modules only can depend on Python libraries. |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 799 | if !isPythonLibModule(child) { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 800 | ctx.PropertyErrorf("libs", |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 801 | "the dependency %q of module %q is not Python library!", |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | d75507f | 2021-08-20 21:02:43 +0000 | [diff] [blame] | 802 | ctx.OtherModuleName(child), ctx.ModuleName()) |
Nan Zhang | d4e641b | 2017-07-12 12:55:28 -0700 | [diff] [blame] | 803 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 804 | // collect source and data paths, checking that there are no duplicate output file conflicts |
| 805 | if dep, ok := child.(pythonDependency); ok { |
| 806 | srcs := dep.getSrcsPathMappings() |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 807 | for _, path := range srcs { |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 808 | checkForDuplicateOutputPath(ctx, destToPySrcs, |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 809 | path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 810 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 811 | data := dep.getDataPathMappings() |
| 812 | for _, path := range data { |
| 813 | checkForDuplicateOutputPath(ctx, destToPyData, |
| 814 | path.dest, path.src.String(), ctx.ModuleName(), ctx.OtherModuleName(child)) |
| 815 | } |
| 816 | p.depsSrcsZips = append(p.depsSrcsZips, dep.getSrcsZip()) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 817 | } |
Colin Cross | 6b75360 | 2018-06-21 13:03:07 -0700 | [diff] [blame] | 818 | return true |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 819 | }) |
| 820 | } |
| 821 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 822 | // chckForDuplicateOutputPath checks whether outputPath has already been included in map m, which |
| 823 | // would result in two files being placed in the same location. |
| 824 | // If there is a duplicate path, an error is thrown and true is returned |
| 825 | // Otherwise, outputPath: srcPath is added to m and returns false |
| 826 | func checkForDuplicateOutputPath(ctx android.ModuleContext, m map[string]string, outputPath, srcPath, curModule, otherModule string) bool { |
| 827 | if oldSrcPath, found := m[outputPath]; found { |
Nan Zhang | bea0975 | 2018-05-31 12:49:33 -0700 | [diff] [blame] | 828 | ctx.ModuleErrorf("found two files to be placed at the same location within zip %q."+ |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 829 | " First file: in module %s at path %q."+ |
| 830 | " Second file: in module %s at path %q.", |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 831 | outputPath, curModule, oldSrcPath, otherModule, srcPath) |
| 832 | return true |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 833 | } |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 834 | m[outputPath] = srcPath |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 835 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 836 | return false |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 837 | } |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 838 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 839 | // InstallInData returns true as Python is not supported in the system partition |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 840 | func (p *Module) InstallInData() bool { |
| 841 | return true |
| 842 | } |
| 843 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 844 | func (p *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) { |
| 845 | if p.isLibrary() { |
| 846 | pythonLibBp2Build(ctx, p) |
| 847 | } else if p.isBinary() { |
| 848 | pythonBinaryBp2Build(ctx, p) |
| 849 | } |
| 850 | } |
| 851 | |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 852 | var Bool = proptools.Bool |
Dan Willemsen | 6ca390f | 2019-02-14 23:17:08 -0800 | [diff] [blame] | 853 | var BoolDefault = proptools.BoolDefault |
Nan Zhang | ea568a4 | 2017-11-08 21:20:04 -0800 | [diff] [blame] | 854 | var String = proptools.String |