blob: c255190638862ba3ed3162be3f5c93e97c360acc [file] [log] [blame]
Nan Zhangdb0b9a32017-02-27 10:12:13 -08001// 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
15package python
16
17// This file contains the module types for building Python library.
18
19import (
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000020 "fmt"
21
Nan Zhangdb0b9a32017-02-27 10:12:13 -080022 "android/soong/android"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000023 "android/soong/bazel"
24 "github.com/google/blueprint/proptools"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080025)
26
27func init() {
Paul Duffind0890452021-03-17 21:57:08 +000028 registerPythonLibraryComponents(android.InitRegistrationContext)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000029 android.RegisterBp2BuildMutator("python_library", PythonLibraryBp2Build)
Paul Duffind0890452021-03-17 21:57:08 +000030}
31
32func registerPythonLibraryComponents(ctx android.RegistrationContext) {
33 ctx.RegisterModuleType("python_library_host", PythonLibraryHostFactory)
34 ctx.RegisterModuleType("python_library", PythonLibraryFactory)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080035}
36
Colin Cross36242852017-06-23 15:06:31 -070037func PythonLibraryHostFactory() android.Module {
Jiyong Park1613e552020-09-14 19:43:17 +090038 module := newModule(android.HostSupported, android.MultilibFirst)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080039
Liz Kammerd737d022020-11-16 15:42:51 -080040 return module.init()
Nan Zhangdb0b9a32017-02-27 10:12:13 -080041}
Nan Zhangd9ec5e72017-12-01 20:00:31 +000042
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000043type bazelPythonLibraryAttributes struct {
44 Srcs bazel.LabelListAttribute
45 Data bazel.LabelListAttribute
46 Srcs_version string
47}
48
49func PythonLibraryBp2Build(ctx android.TopDownMutatorContext) {
50 m, ok := ctx.Module().(*Module)
51 if !ok || !m.ConvertWithBp2build(ctx) {
52 return
53 }
54
55 // a Module can be something other than a python_library
56 if ctx.ModuleType() != "python_library" {
57 return
58 }
59
60 // TODO(b/182306917): this doesn't fully handle all nested props versioned
61 // by the python version, which would have been handled by the version split
62 // mutator. This is sufficient for very simple python_library modules under
63 // Bionic.
64 py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, true)
65 py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
66 var python_version string
67 if py2Enabled && !py3Enabled {
68 python_version = "PY2"
69 } else if !py2Enabled && py3Enabled {
70 python_version = "PY3"
71 } else if !py2Enabled && !py3Enabled {
72 panic(fmt.Errorf(
73 "error for '%s' module: bp2build's python_library converter doesn't understand having "+
74 "neither py2 nor py3 enabled", m.Name()))
75 } else {
76 // do nothing, since python_version defaults to PY2ANDPY3
77 }
78
79 srcs := android.BazelLabelForModuleSrcExcludes(ctx, m.properties.Srcs, m.properties.Exclude_srcs)
80 data := android.BazelLabelForModuleSrc(ctx, m.properties.Data)
81
82 attrs := &bazelPythonLibraryAttributes{
83 Srcs: bazel.MakeLabelListAttribute(srcs),
84 Data: bazel.MakeLabelListAttribute(data),
85 Srcs_version: python_version,
86 }
87
88 props := bazel.BazelTargetModuleProperties{
89 // Use the native py_library rule.
90 Rule_class: "py_library",
91 }
92
93 ctx.CreateBazelTargetModule(m.Name(), props, attrs)
94}
95
Nan Zhangd9ec5e72017-12-01 20:00:31 +000096func PythonLibraryFactory() android.Module {
97 module := newModule(android.HostAndDeviceSupported, android.MultilibBoth)
98
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000099 android.InitBazelModule(module)
100
Liz Kammerd737d022020-11-16 15:42:51 -0800101 return module.init()
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000102}