blob: 5071b74cfe7be354acebb1f59a2cecb2dcdd124b [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 (
Cole Faustb09da7e2022-05-18 10:57:33 -070020 "path/filepath"
21 "strings"
22
Nan Zhangdb0b9a32017-02-27 10:12:13 -080023 "android/soong/android"
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000024 "android/soong/bazel"
Liz Kammer46fb7ab2021-12-01 10:09:34 -050025
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000026 "github.com/google/blueprint/proptools"
Nan Zhangdb0b9a32017-02-27 10:12:13 -080027)
28
29func init() {
Paul Duffind0890452021-03-17 21:57:08 +000030 registerPythonLibraryComponents(android.InitRegistrationContext)
31}
32
33func registerPythonLibraryComponents(ctx android.RegistrationContext) {
34 ctx.RegisterModuleType("python_library_host", PythonLibraryHostFactory)
35 ctx.RegisterModuleType("python_library", PythonLibraryFactory)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080036}
37
Colin Cross36242852017-06-23 15:06:31 -070038func PythonLibraryHostFactory() android.Module {
Jiyong Park1613e552020-09-14 19:43:17 +090039 module := newModule(android.HostSupported, android.MultilibFirst)
Nan Zhangdb0b9a32017-02-27 10:12:13 -080040
Alex Márquez Pérez Muñíz Díaz Púras Thaureauxdc212c02021-08-23 20:21:19 +000041 android.InitBazelModule(module)
42
Liz Kammerd737d022020-11-16 15:42:51 -080043 return module.init()
Nan Zhangdb0b9a32017-02-27 10:12:13 -080044}
Nan Zhangd9ec5e72017-12-01 20:00:31 +000045
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000046type bazelPythonLibraryAttributes struct {
47 Srcs bazel.LabelListAttribute
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux560cb662021-08-26 20:13:29 +000048 Deps bazel.LabelListAttribute
Cole Faustb09da7e2022-05-18 10:57:33 -070049 Imports bazel.StringListAttribute
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
Cole Faust53b62092022-05-12 15:37:02 -070053type bazelPythonProtoLibraryAttributes struct {
54 Deps bazel.LabelListAttribute
55}
56
Liz Kammerbe46fcc2021-11-01 15:32:43 -040057func pythonLibBp2Build(ctx android.TopDownMutatorContext, m *Module) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000058 // TODO(b/182306917): this doesn't fully handle all nested props versioned
59 // by the python version, which would have been handled by the version split
60 // mutator. This is sufficient for very simple python_library modules under
61 // Bionic.
62 py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, true)
63 py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false)
Liz Kammer46fb7ab2021-12-01 10:09:34 -050064 var python_version *string
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000065 if py2Enabled && !py3Enabled {
Liz Kammer46fb7ab2021-12-01 10:09:34 -050066 python_version = &pyVersion2
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000067 } else if !py2Enabled && py3Enabled {
Liz Kammer46fb7ab2021-12-01 10:09:34 -050068 python_version = &pyVersion3
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000069 } else if !py2Enabled && !py3Enabled {
Liz Kammerbe46fcc2021-11-01 15:32:43 -040070 ctx.ModuleErrorf("bp2build converter doesn't understand having neither py2 nor py3 enabled")
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +000071 } else {
72 // do nothing, since python_version defaults to PY2ANDPY3
73 }
74
Cole Faustb09da7e2022-05-18 10:57:33 -070075 // Bazel normally requires `import path.from.top.of.tree` statements in
76 // python code, but with soong you can directly import modules from libraries.
77 // Add "imports" attributes to the bazel library so it matches soong's behavior.
78 imports := "."
79 if m.properties.Pkg_path != nil {
80 // TODO(b/215119317) This is a hack to handle the fact that we don't convert
81 // pkg_path properly right now. If the folder structure that contains this
82 // Android.bp file matches pkg_path, we can set imports to an appropriate
83 // number of ../..s to emulate moving the files under a pkg_path folder.
84 pkg_path := filepath.Clean(*m.properties.Pkg_path)
85 if strings.HasPrefix(pkg_path, "/") {
86 ctx.ModuleErrorf("pkg_path cannot start with a /: %s", pkg_path)
87 return
88 }
89
90 if !strings.HasSuffix(ctx.ModuleDir(), "/"+pkg_path) && ctx.ModuleDir() != pkg_path {
91 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())
92 return
93 }
94 numFolders := strings.Count(pkg_path, "/") + 1
95 dots := make([]string, numFolders)
96 for i := 0; i < numFolders; i++ {
97 dots[i] = ".."
98 }
99 imports = strings.Join(dots, "/")
100 }
101
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000102 baseAttrs := m.makeArchVariantBaseAttributes(ctx)
Cole Faust53b62092022-05-12 15:37:02 -0700103
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000104 attrs := &bazelPythonLibraryAttributes{
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000105 Srcs: baseAttrs.Srcs,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux19d399d2021-09-17 20:30:21 +0000106 Deps: baseAttrs.Deps,
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000107 Srcs_version: python_version,
Cole Faustb09da7e2022-05-18 10:57:33 -0700108 Imports: bazel.MakeStringListAttribute([]string{imports}),
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000109 }
110
111 props := bazel.BazelTargetModuleProperties{
Cole Faustb09da7e2022-05-18 10:57:33 -0700112 // Use the native py_library rule.
113 Rule_class: "py_library",
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000114 }
115
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000116 ctx.CreateBazelTargetModule(props, android.CommonAttributes{
117 Name: m.Name(),
118 Data: baseAttrs.Data,
119 }, attrs)
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000120}
121
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000122func PythonLibraryFactory() android.Module {
123 module := newModule(android.HostAndDeviceSupported, android.MultilibBoth)
124
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0fc781c2021-08-19 19:21:30 +0000125 android.InitBazelModule(module)
126
Liz Kammerd737d022020-11-16 15:42:51 -0800127 return module.init()
Nan Zhangd9ec5e72017-12-01 20:00:31 +0000128}