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 15package python 16 17// This file contains the module types for building Python library. 18 19import ( 20 "android/soong/android" 21 "android/soong/bazel" 22 23 "github.com/google/blueprint/proptools" 24) 25 26func init() { 27 registerPythonLibraryComponents(android.InitRegistrationContext) 28} 29 30func registerPythonLibraryComponents(ctx android.RegistrationContext) { 31 ctx.RegisterModuleType("python_library_host", PythonLibraryHostFactory) 32 ctx.RegisterModuleType("python_library", PythonLibraryFactory) 33} 34 35func PythonLibraryHostFactory() android.Module { 36 module := newModule(android.HostSupported, android.MultilibFirst) 37 38 android.InitBazelModule(module) 39 40 return module.init() 41} 42 43type bazelPythonLibraryAttributes struct { 44 Srcs bazel.LabelListAttribute 45 Deps bazel.LabelListAttribute 46 Srcs_version *string 47} 48 49func pythonLibBp2Build(ctx android.TopDownMutatorContext, m *Module) { 50 // TODO(b/182306917): this doesn't fully handle all nested props versioned 51 // by the python version, which would have been handled by the version split 52 // mutator. This is sufficient for very simple python_library modules under 53 // Bionic. 54 py3Enabled := proptools.BoolDefault(m.properties.Version.Py3.Enabled, true) 55 py2Enabled := proptools.BoolDefault(m.properties.Version.Py2.Enabled, false) 56 var python_version *string 57 if py2Enabled && !py3Enabled { 58 python_version = &pyVersion2 59 } else if !py2Enabled && py3Enabled { 60 python_version = &pyVersion3 61 } else if !py2Enabled && !py3Enabled { 62 ctx.ModuleErrorf("bp2build converter doesn't understand having neither py2 nor py3 enabled") 63 } else { 64 // do nothing, since python_version defaults to PY2ANDPY3 65 } 66 67 baseAttrs := m.makeArchVariantBaseAttributes(ctx) 68 attrs := &bazelPythonLibraryAttributes{ 69 Srcs: baseAttrs.Srcs, 70 Deps: baseAttrs.Deps, 71 Srcs_version: python_version, 72 } 73 74 props := bazel.BazelTargetModuleProperties{ 75 Rule_class: "py_library", 76 Bzl_load_location: "//build/bazel/rules/python:library.bzl", 77 } 78 79 ctx.CreateBazelTargetModule(props, android.CommonAttributes{ 80 Name: m.Name(), 81 Data: baseAttrs.Data, 82 }, attrs) 83} 84 85func PythonLibraryFactory() android.Module { 86 module := newModule(android.HostAndDeviceSupported, android.MultilibBoth) 87 88 android.InitBazelModule(module) 89 90 return module.init() 91} 92