blob: fde4ea4d905b849b3bb7c7e668cd9e739a0a3362 [file] [log] [blame]
# Copyright 2021 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/group_with_inputs.gni")
# Defines a python library
#
# NOTE: Currently does NOT support depending on other python_libraries.
# TODO(https://fxbug.dev/73542): Enable other python_libraries as deps when use
# case show up.
#
# Example
#
# ```
# python_library("lib") {
# library_name = "lib"
# sources = [
# "__init__.py",
# "lib.py",
# ]
# }
# ```
#
# Parameters
#
# library_name (optional)
# Name of the library, Python scripts can import this library by this name.
# Type: string
# Default: ${target_name}
#
# source_root (optional)
# Path to root of the package, where __init__.py is.
# Type: path
# Default: current directory
#
# sources (required)
# List of sources for this python library, relative to source_root.
# Type: list(path)
#
# library_deps (optional)
# List of targets for other python_library()s that this library depends on
# Type: list(target)
#
# Metadata
#
# library_info
# Exactly one scope including name of this library, path to its root and all
# of its sources (relative to root).
#
# library_info_barrier
# Empty for stopping metadata walks.
template("python_library") {
assert(defined(invoker.sources), "sources is required")
library_deps = []
if (defined(invoker.library_deps)) {
library_deps = invoker.library_deps
}
group_with_inputs(target_name) {
forward_variables_from(invoker,
[
"testonly",
"visibility",
])
library_name = target_name
if (defined(invoker.library_name)) {
library_name = invoker.library_name
}
# Seed with the library_deps, and then add any other deps as well
deps = library_deps
if (defined(invoker.deps)) {
deps += invoker.deps
}
source_root = "."
if (defined(invoker.source_root)) {
source_root = invoker.source_root
}
metadata = {
library_info = [
{
library_name = library_name
source_root = rebase_path(source_root, root_build_dir)
sources = invoker.sources
},
]
# Collect library_info from any libraries that are needed.
library_info_barrier = library_deps
}
# Add the sources from the invoker
if (defined(invoker.sources)) {
if (source_root == ".") {
# use the directory of the current target to locate the files
_source_root = get_label_info(":$target_name", "dir")
} else {
_source_root = source_root
}
sources = []
foreach(source, invoker.sources) {
sources += [ _source_root + "/" + source ]
}
}
}
}