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 library. |
| 18 | |
| 19 | import ( |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 20 | "fmt" |
| 21 | |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 22 | "android/soong/android" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 23 | "android/soong/bazel" |
| 24 | "github.com/google/blueprint/proptools" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | func init() { |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 28 | registerPythonLibraryComponents(android.InitRegistrationContext) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 29 | android.RegisterBp2BuildMutator("python_library_host", PythonLibraryHostBp2Build) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 30 | android.RegisterBp2BuildMutator("python_library", PythonLibraryBp2Build) |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | func registerPythonLibraryComponents(ctx android.RegistrationContext) { |
| 34 | ctx.RegisterModuleType("python_library_host", PythonLibraryHostFactory) |
| 35 | ctx.RegisterModuleType("python_library", PythonLibraryFactory) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 38 | func PythonLibraryHostFactory() android.Module { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 39 | module := newModule(android.HostSupported, android.MultilibFirst) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 40 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 41 | android.InitBazelModule(module) |
| 42 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 43 | return module.init() |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 44 | } |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 45 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 46 | type bazelPythonLibraryAttributes struct { |
| 47 | Srcs bazel.LabelListAttribute |
| 48 | Data bazel.LabelListAttribute |
| 49 | Srcs_version string |
| 50 | } |
| 51 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 52 | func PythonLibraryHostBp2Build(ctx android.TopDownMutatorContext) { |
| 53 | pythonLibBp2Build(ctx, "python_library_host") |
| 54 | } |
| 55 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 56 | func PythonLibraryBp2Build(ctx android.TopDownMutatorContext) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 57 | pythonLibBp2Build(ctx, "python_library") |
| 58 | } |
| 59 | |
| 60 | func pythonLibBp2Build(ctx android.TopDownMutatorContext, modType string) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 61 | m, ok := ctx.Module().(*Module) |
| 62 | if !ok || !m.ConvertWithBp2build(ctx) { |
| 63 | return |
| 64 | } |
| 65 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 66 | // a Module can be something other than a `modType` |
| 67 | if ctx.ModuleType() != modType { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 68 | return |
| 69 | } |
| 70 | |
| 71 | // TODO(b/182306917): this doesn't fully handle all nested props versioned |
| 72 | // by the python version, which would have been handled by the version split |
| 73 | // mutator. This is sufficient for very simple python_library modules under |
| 74 | // Bionic. |
| 75 | py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, true) |
| 76 | py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false) |
| 77 | var python_version string |
| 78 | if py2Enabled && !py3Enabled { |
| 79 | python_version = "PY2" |
| 80 | } else if !py2Enabled && py3Enabled { |
| 81 | python_version = "PY3" |
| 82 | } else if !py2Enabled && !py3Enabled { |
| 83 | panic(fmt.Errorf( |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 84 | "error for '%s' module: bp2build's %s converter doesn't understand having "+ |
| 85 | "neither py2 nor py3 enabled", m.Name(), modType)) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 86 | } else { |
| 87 | // do nothing, since python_version defaults to PY2ANDPY3 |
| 88 | } |
| 89 | |
| 90 | srcs := android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs) |
| 91 | data := android.BazelLabelForModuleSrc(ctx, m.properties.Data) |
| 92 | |
| 93 | attrs := &bazelPythonLibraryAttributes{ |
| 94 | Srcs: bazel.MakeLabelListAttribute(srcs), |
| 95 | Data: bazel.MakeLabelListAttribute(data), |
| 96 | Srcs_version: python_version, |
| 97 | } |
| 98 | |
| 99 | props := bazel.BazelTargetModuleProperties{ |
| 100 | // Use the native py_library rule. |
| 101 | Rule_class: "py_library", |
| 102 | } |
| 103 | |
| 104 | ctx.CreateBazelTargetModule(m.Name(), props, attrs) |
| 105 | } |
| 106 | |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 107 | func PythonLibraryFactory() android.Module { |
| 108 | module := newModule(android.HostAndDeviceSupported, android.MultilibBoth) |
| 109 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 110 | android.InitBazelModule(module) |
| 111 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 112 | return module.init() |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 113 | } |