forked from bazel-contrib/rules_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_bazel.bzl
More file actions
109 lines (88 loc) · 3.48 KB
/
Copy pathcommon_bazel.bzl
File metadata and controls
109 lines (88 loc) · 3.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Copyright 2022 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Common functions that are specific to Bazel rule implementation"""
load("@bazel_skylib//lib:paths.bzl", "paths")
load(":common.bzl", "is_bool")
load(":providers.bzl", "PyCcLinkParamsProvider")
load(":py_internal.bzl", "py_internal")
# TODO: Load cc_common from rules_cc
_cc_common = cc_common
# TODO: Load CcInfo from rules_cc
_CcInfo = CcInfo
_py_builtins = py_internal
def collect_cc_info(ctx, extra_deps = []):
"""Collect C++ information from dependencies for Bazel.
Args:
ctx: Rule ctx; must have `deps` attribute.
extra_deps: list of Target to also collect C+ information from.
Returns:
CcInfo provider of merged information.
"""
deps = ctx.attr.deps
if extra_deps:
deps = list(deps)
deps.extend(extra_deps)
cc_infos = []
for dep in deps:
if _CcInfo in dep:
cc_infos.append(dep[_CcInfo])
if PyCcLinkParamsProvider in dep:
cc_infos.append(dep[PyCcLinkParamsProvider].cc_info)
return _cc_common.merge_cc_infos(cc_infos = cc_infos)
def maybe_precompile(ctx, srcs):
"""Computes all the outputs (maybe precompiled) from the input srcs.
See create_binary_semantics_struct for details about this function.
Args:
ctx: Rule ctx.
srcs: List of Files; the inputs to maybe precompile.
Returns:
List of Files; the desired output files derived from the input sources.
"""
_ = ctx # @unused
# Precompilation isn't implemented yet, so just return srcs as-is
return srcs
def get_imports(ctx):
"""Gets the imports from a rule's `imports` attribute.
See create_binary_semantics_struct for details about this function.
Args:
ctx: Rule ctx.
Returns:
List of strings.
"""
prefix = "{}/{}".format(
ctx.workspace_name,
_py_builtins.get_label_repo_runfiles_path(ctx.label),
)
result = []
for import_str in ctx.attr.imports:
import_str = ctx.expand_make_variables("imports", import_str, {})
if import_str.startswith("/"):
continue
# To prevent "escaping" out of the runfiles tree, we normalize
# the path and ensure it doesn't have up-level references.
import_path = paths.normalize("{}/{}".format(prefix, import_str))
if import_path.startswith("../") or import_path == "..":
fail("Path '{}' references a path above the execution root".format(
import_str,
))
result.append(import_path)
return result
def convert_legacy_create_init_to_int(kwargs):
"""Convert "legacy_create_init" key to int, in-place.
Args:
kwargs: The kwargs to modify. The key "legacy_create_init", if present
and bool, will be converted to its integer value, in place.
"""
if is_bool(kwargs.get("legacy_create_init")):
kwargs["legacy_create_init"] = 1 if kwargs["legacy_create_init"] else 0