blob: b9201177dbc528c6c8e792399e352c02564628c3 [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"
Liz Kammer46fb7ab2021-12-01 10:09:34 -050024
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000025 "github.com/google/blueprint/proptools"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080026)
27
28func init() {
Paul Duffind0890452021-03-17 21:57:08 +000029 registerPythonLibraryComponents(android.InitRegistrationContext)
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000030 android.RegisterBp2BuildMutator("python_library_host", PythonLibraryHostBp2Build)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000031 android.RegisterBp2BuildMutator("python_library", PythonLibraryBp2Build)
Paul Duffind0890452021-03-17 21:57:08 +000032}
33
34func registerPythonLibraryComponents(ctx android.RegistrationContext) {
35 ctx.RegisterModuleType("python_library_host", PythonLibraryHostFactory)
36 ctx.RegisterModuleType("python_library", PythonLibraryFactory)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080037}
38
Colin Cross36242852017-06-23 15:06:31 -070039func PythonLibraryHostFactory() android.Module {
Jiyong Park1613e552020-09-14 19:43:17 +090040 module := newModule(android.HostSupported, android.MultilibFirst)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080041
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000042 android.InitBazelModule(module)
43
Liz Kammerd737d022020-11-16 15:42:51 -080044 return module.init()
Nan Zhangdb0b9a32017-02-27 10:12:13 -080045}
Nan Zhangd9ec5e72017-12-01 20:00:31 +000046
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000047type bazelPythonLibraryAttributes struct {
48 Srcs bazel.LabelListAttribute
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000049 Deps bazel.LabelListAttribute
Liz Kammer46fb7ab2021-12-01 10:09:34 -050050 Srcs_version *string
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000051}
52
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000053func PythonLibraryHostBp2Build(ctx android.TopDownMutatorContext) {
54 pythonLibBp2Build(ctx, "python_library_host")
55}
56
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000057func PythonLibraryBp2Build(ctx android.TopDownMutatorContext) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000058 pythonLibBp2Build(ctx, "python_library")
59}
60
61func pythonLibBp2Build(ctx android.TopDownMutatorContext, modType string) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000062 m, ok := ctx.Module().(*Module)
63 if !ok || !m.ConvertWithBp2build(ctx) {
64 return
65 }
66
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000067 // a Module can be something other than a `modType`
68 if ctx.ModuleType() != modType {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000069 return
70 }
71
72 // TODO(b/182306917): this doesn't fully handle all nested props versioned
73 // by the python version, which would have been handled by the version split
74 // mutator. This is sufficient for very simple python_library modules under
75 // Bionic.
76 py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, true)
77 py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
Liz Kammer46fb7ab2021-12-01 10:09:34 -050078 var python_version *string
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000079 if py2Enabled && !py3Enabled {
Liz Kammer46fb7ab2021-12-01 10:09:34 -050080 python_version = &pyVersion2
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000081 } else if !py2Enabled && py3Enabled {
Liz Kammer46fb7ab2021-12-01 10:09:34 -050082 python_version = &pyVersion3
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000083 } else if !py2Enabled && !py3Enabled {
84 panic(fmt.Errorf(
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000085 "error for '%s' module: bp2build's %s converter doesn't understand having "+
86 "neither py2 nor py3 enabled", m.Name(), modType))
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000087 } else {
88 // do nothing, since python_version defaults to PY2ANDPY3
89 }
90
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +000091 baseAttrs := m.makeArchVariantBaseAttributes(ctx)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000092 attrs := &bazelPythonLibraryAttributes{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +000093 Srcs: baseAttrs.Srcs,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +000094 Deps: baseAttrs.Deps,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000095 Srcs_version: python_version,
96 }
97
98 props := bazel.BazelTargetModuleProperties{
99 // Use the native py_library rule.
100 Rule_class: "py_library",
101 }
102
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000103 ctx.CreateBazelTargetModule(props, android.CommonAttributes{
104 Name: m.Name(),
105 Data: baseAttrs.Data,
106 }, attrs)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000107}
108
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000109func PythonLibraryFactory() android.Module {
110 module := newModule(android.HostAndDeviceSupported, android.MultilibBoth)
111
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000112 android.InitBazelModule(module)
113
Liz Kammerd737d022020-11-16 15:42:51 -0800114 return module.init()
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000115}