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 ( |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 20 | "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] | 21 | "android/soong/bazel" |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 22 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func init() { |
Paul Duffin | d089045 | 2021-03-17 21:57:08 +0000 | [diff] [blame] | 27 | registerPythonLibraryComponents(android.InitRegistrationContext) |
| 28 | } |
| 29 | |
| 30 | func registerPythonLibraryComponents(ctx android.RegistrationContext) { |
| 31 | ctx.RegisterModuleType("python_library_host", PythonLibraryHostFactory) |
| 32 | ctx.RegisterModuleType("python_library", PythonLibraryFactory) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 35 | func PythonLibraryHostFactory() android.Module { |
Jiyong Park | 1613e55 | 2020-09-14 19:43:17 +0900 | [diff] [blame] | 36 | module := newModule(android.HostSupported, android.MultilibFirst) |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 37 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | dc212c0 | 2021-08-23 20:21:19 +0000 | [diff] [blame] | 38 | android.InitBazelModule(module) |
| 39 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 40 | return module.init() |
Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 41 | } |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 42 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 43 | type bazelPythonLibraryAttributes struct { |
| 44 | Srcs bazel.LabelListAttribute |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 560cb66 | 2021-08-26 20:13:29 +0000 | [diff] [blame] | 45 | Deps bazel.LabelListAttribute |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 46 | Imports bazel.StringListAttribute |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 47 | Srcs_version *string |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Cole Faust | 53b6209 | 2022-05-12 15:37:02 -0700 | [diff] [blame] | 50 | type bazelPythonProtoLibraryAttributes struct { |
| 51 | Deps bazel.LabelListAttribute |
| 52 | } |
| 53 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 54 | func pythonLibBp2Build(ctx android.TopDownMutatorContext, m *Module) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 55 | // TODO(b/182306917): this doesn't fully handle all nested props versioned |
| 56 | // by the python version, which would have been handled by the version split |
| 57 | // mutator. This is sufficient for very simple python_library modules under |
| 58 | // Bionic. |
| 59 | py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, true) |
| 60 | py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false) |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 61 | var python_version *string |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 62 | if py2Enabled && !py3Enabled { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 63 | python_version = &pyVersion2 |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 64 | } else if !py2Enabled && py3Enabled { |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 65 | python_version = &pyVersion3 |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 66 | } else if !py2Enabled && !py3Enabled { |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 67 | ctx.ModuleErrorf("bp2build converter doesn't understand having neither py2 nor py3 enabled") |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 68 | } else { |
| 69 | // do nothing, since python_version defaults to PY2ANDPY3 |
| 70 | } |
| 71 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 72 | baseAttrs := m.makeArchVariantBaseAttributes(ctx) |
Cole Faust | 53b6209 | 2022-05-12 15:37:02 -0700 | [diff] [blame] | 73 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 74 | attrs := &bazelPythonLibraryAttributes{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 75 | Srcs: baseAttrs.Srcs, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 19d399d | 2021-09-17 20:30:21 +0000 | [diff] [blame] | 76 | Deps: baseAttrs.Deps, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 77 | Srcs_version: python_version, |
Cole Faust | 0124336 | 2022-06-02 12:11:12 -0700 | [diff] [blame] | 78 | Imports: baseAttrs.Imports, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | props := bazel.BazelTargetModuleProperties{ |
Cole Faust | b09da7e | 2022-05-18 10:57:33 -0700 | [diff] [blame] | 82 | // Use the native py_library rule. |
| 83 | Rule_class: "py_library", |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 86 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{ |
| 87 | Name: m.Name(), |
| 88 | Data: baseAttrs.Data, |
| 89 | }, attrs) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 92 | func PythonLibraryFactory() android.Module { |
| 93 | module := newModule(android.HostAndDeviceSupported, android.MultilibBoth) |
| 94 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0fc781c | 2021-08-19 19:21:30 +0000 | [diff] [blame] | 95 | android.InitBazelModule(module) |
| 96 | |
Liz Kammer | d737d02 | 2020-11-16 15:42:51 -0800 | [diff] [blame] | 97 | return module.init() |
Nan Zhang | d9ec5e7 | 2017-12-01 20:00:31 +0000 | [diff] [blame] | 98 | } |