Skip to content

Commit 026b300

Browse files
authored
refactor: consolidate py_executable_bazel, common_bazel (bazel-contrib#2523)
This furthers the work of removing the artificial split of code that stemmed from when the implementation was part of Bazel itself. Summary of changes: * Move most of `py_executable_bazel.bzl` into `py_executable.bzl` * Move most of `common_bazel.bzl` into `common.bzl` * Create `precompile.bzl` for the precompile helpers. This is to avoid a circular dependency between common.bzl and attributes.bzl. Work towards bazel-contrib#2522
1 parent be950f9 commit 026b300

11 files changed

+843
-891
lines changed

python/private/BUILD.bazel

+21-33
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,18 @@ bzl_library(
104104
deps = [":py_internal_bzl"],
105105
)
106106

107-
bzl_library(
108-
name = "common_bazel_bzl",
109-
srcs = ["common_bazel.bzl"],
110-
deps = [
111-
":attributes_bzl",
112-
":common_bzl",
113-
":py_cc_link_params_info_bzl",
114-
":py_internal_bzl",
115-
":py_interpreter_program_bzl",
116-
":toolchain_types_bzl",
117-
"@bazel_skylib//lib:paths",
118-
],
119-
)
120-
121107
bzl_library(
122108
name = "common_bzl",
123109
srcs = ["common.bzl"],
124110
deps = [
125111
":cc_helper_bzl",
112+
":py_cc_link_params_info_bzl",
126113
":py_info_bzl",
127114
":py_internal_bzl",
128115
":reexports_bzl",
129116
":rules_cc_srcs_bzl",
130117
":semantics_bzl",
118+
"@bazel_skylib//lib:paths",
131119
],
132120
)
133121

@@ -199,6 +187,18 @@ bzl_library(
199187
srcs = ["normalize_name.bzl"],
200188
)
201189

190+
bzl_library(
191+
name = "precompile_bzl",
192+
srcs = ["precompile.bzl"],
193+
deps = [
194+
":attributes_bzl",
195+
":py_internal_bzl",
196+
":py_interpreter_program_bzl",
197+
":toolchain_types_bzl",
198+
"@bazel_skylib//lib:paths",
199+
],
200+
)
201+
202202
bzl_library(
203203
name = "python_bzl",
204204
srcs = ["python.bzl"],
@@ -265,8 +265,8 @@ bzl_library(
265265
name = "py_binary_macro_bzl",
266266
srcs = ["py_binary_macro.bzl"],
267267
deps = [
268-
":common_bzl",
269268
":py_binary_rule_bzl",
269+
":py_executable_bzl",
270270
],
271271
)
272272

@@ -275,7 +275,7 @@ bzl_library(
275275
srcs = ["py_binary_rule.bzl"],
276276
deps = [
277277
":attributes_bzl",
278-
":py_executable_bazel_bzl",
278+
":py_executable_bzl",
279279
":semantics_bzl",
280280
"@bazel_skylib//lib:dicts",
281281
],
@@ -343,20 +343,6 @@ bzl_library(
343343
],
344344
)
345345

346-
bzl_library(
347-
name = "py_executable_bazel_bzl",
348-
srcs = ["py_executable_bazel.bzl"],
349-
deps = [
350-
":attributes_bzl",
351-
":common_bazel_bzl",
352-
":common_bzl",
353-
":py_executable_bzl",
354-
":py_internal_bzl",
355-
":py_runtime_info_bzl",
356-
":semantics_bzl",
357-
],
358-
)
359-
360346
bzl_library(
361347
name = "py_executable_bzl",
362348
srcs = ["py_executable.bzl"],
@@ -365,6 +351,7 @@ bzl_library(
365351
":cc_helper_bzl",
366352
":common_bzl",
367353
":flags_bzl",
354+
":precompile_bzl",
368355
":py_cc_link_params_info_bzl",
369356
":py_executable_info_bzl",
370357
":py_info_bzl",
@@ -373,6 +360,7 @@ bzl_library(
373360
":rules_cc_srcs_bzl",
374361
":toolchain_types_bzl",
375362
"@bazel_skylib//lib:dicts",
363+
"@bazel_skylib//lib:paths",
376364
"@bazel_skylib//lib:structs",
377365
"@bazel_skylib//rules:common_settings",
378366
],
@@ -431,8 +419,8 @@ bzl_library(
431419
name = "py_library_rule_bzl",
432420
srcs = ["py_library_rule.bzl"],
433421
deps = [
434-
":common_bazel_bzl",
435422
":common_bzl",
423+
":precompile_bzl",
436424
":py_library_bzl",
437425
],
438426
)
@@ -508,7 +496,7 @@ bzl_library(
508496
name = "py_test_macro_bzl",
509497
srcs = ["py_test_macro.bzl"],
510498
deps = [
511-
":common_bazel_bzl",
499+
":py_executable_bzl",
512500
":py_test_rule_bzl",
513501
],
514502
)
@@ -519,7 +507,7 @@ bzl_library(
519507
deps = [
520508
":attributes_bzl",
521509
":common_bzl",
522-
":py_executable_bazel_bzl",
510+
":py_executable_bzl",
523511
":semantics_bzl",
524512
"@bazel_skylib//lib:dicts",
525513
],

python/private/common.bzl

+60-1
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
"""Various things common to Bazel and Google rule implementations."""
14+
"""Various things common to rule implementations."""
1515

16+
load("@bazel_skylib//lib:paths.bzl", "paths")
17+
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
18+
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
1619
load(":cc_helper.bzl", "cc_helper")
20+
load(":py_cc_link_params_info.bzl", "PyCcLinkParamsInfo")
1721
load(":py_info.bzl", "PyInfo", "PyInfoBuilder")
1822
load(":py_internal.bzl", "py_internal")
1923
load(":reexports.bzl", "BuiltinPyInfo")
@@ -262,6 +266,30 @@ def filter_to_py_srcs(srcs):
262266
# as a valid extension.
263267
return [f for f in srcs if f.extension == "py"]
264268

269+
def collect_cc_info(ctx, extra_deps = []):
270+
"""Collect C++ information from dependencies for Bazel.
271+
272+
Args:
273+
ctx: Rule ctx; must have `deps` attribute.
274+
extra_deps: list of Target to also collect C+ information from.
275+
276+
Returns:
277+
CcInfo provider of merged information.
278+
"""
279+
deps = ctx.attr.deps
280+
if extra_deps:
281+
deps = list(deps)
282+
deps.extend(extra_deps)
283+
cc_infos = []
284+
for dep in deps:
285+
if CcInfo in dep:
286+
cc_infos.append(dep[CcInfo])
287+
288+
if PyCcLinkParamsInfo in dep:
289+
cc_infos.append(dep[PyCcLinkParamsInfo].cc_info)
290+
291+
return cc_common.merge_cc_infos(cc_infos = cc_infos)
292+
265293
def collect_imports(ctx, semantics):
266294
"""Collect the direct and transitive `imports` strings.
267295
@@ -280,6 +308,37 @@ def collect_imports(ctx, semantics):
280308
transitive.append(dep[BuiltinPyInfo].imports)
281309
return depset(direct = semantics.get_imports(ctx), transitive = transitive)
282310

311+
def get_imports(ctx):
312+
"""Gets the imports from a rule's `imports` attribute.
313+
314+
See create_binary_semantics_struct for details about this function.
315+
316+
Args:
317+
ctx: Rule ctx.
318+
319+
Returns:
320+
List of strings.
321+
"""
322+
prefix = "{}/{}".format(
323+
ctx.workspace_name,
324+
py_internal.get_label_repo_runfiles_path(ctx.label),
325+
)
326+
result = []
327+
for import_str in ctx.attr.imports:
328+
import_str = ctx.expand_make_variables("imports", import_str, {})
329+
if import_str.startswith("/"):
330+
continue
331+
332+
# To prevent "escaping" out of the runfiles tree, we normalize
333+
# the path and ensure it doesn't have up-level references.
334+
import_path = paths.normalize("{}/{}".format(prefix, import_str))
335+
if import_path.startswith("../") or import_path == "..":
336+
fail("Path '{}' references a path above the execution root".format(
337+
import_str,
338+
))
339+
result.append(import_path)
340+
return result
341+
283342
def collect_runfiles(ctx, files = depset()):
284343
"""Collects the necessary files from the rule's context.
285344

python/private/common_bazel.bzl renamed to python/private/precompile.bzl

-73
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,12 @@
1313
# limitations under the License.
1414
"""Common functions that are specific to Bazel rule implementation"""
1515

16-
load("@bazel_skylib//lib:paths.bzl", "paths")
1716
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
18-
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
19-
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
2017
load(":attributes.bzl", "PrecompileAttr", "PrecompileInvalidationModeAttr", "PrecompileSourceRetentionAttr")
21-
load(":common.bzl", "is_bool")
2218
load(":flags.bzl", "PrecompileFlag")
23-
load(":py_cc_link_params_info.bzl", "PyCcLinkParamsInfo")
24-
load(":py_internal.bzl", "py_internal")
2519
load(":py_interpreter_program.bzl", "PyInterpreterProgramInfo")
2620
load(":toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "TARGET_TOOLCHAIN_TYPE")
2721

28-
_py_builtins = py_internal
29-
30-
def collect_cc_info(ctx, extra_deps = []):
31-
"""Collect C++ information from dependencies for Bazel.
32-
33-
Args:
34-
ctx: Rule ctx; must have `deps` attribute.
35-
extra_deps: list of Target to also collect C+ information from.
36-
37-
Returns:
38-
CcInfo provider of merged information.
39-
"""
40-
deps = ctx.attr.deps
41-
if extra_deps:
42-
deps = list(deps)
43-
deps.extend(extra_deps)
44-
cc_infos = []
45-
for dep in deps:
46-
if CcInfo in dep:
47-
cc_infos.append(dep[CcInfo])
48-
49-
if PyCcLinkParamsInfo in dep:
50-
cc_infos.append(dep[PyCcLinkParamsInfo].cc_info)
51-
52-
return cc_common.merge_cc_infos(cc_infos = cc_infos)
53-
5422
def maybe_precompile(ctx, srcs):
5523
"""Computes all the outputs (maybe precompiled) from the input srcs.
5624
@@ -237,44 +205,3 @@ def _precompile(ctx, src, *, use_pycache):
237205
toolchain = EXEC_TOOLS_TOOLCHAIN_TYPE,
238206
)
239207
return pyc
240-
241-
def get_imports(ctx):
242-
"""Gets the imports from a rule's `imports` attribute.
243-
244-
See create_binary_semantics_struct for details about this function.
245-
246-
Args:
247-
ctx: Rule ctx.
248-
249-
Returns:
250-
List of strings.
251-
"""
252-
prefix = "{}/{}".format(
253-
ctx.workspace_name,
254-
_py_builtins.get_label_repo_runfiles_path(ctx.label),
255-
)
256-
result = []
257-
for import_str in ctx.attr.imports:
258-
import_str = ctx.expand_make_variables("imports", import_str, {})
259-
if import_str.startswith("/"):
260-
continue
261-
262-
# To prevent "escaping" out of the runfiles tree, we normalize
263-
# the path and ensure it doesn't have up-level references.
264-
import_path = paths.normalize("{}/{}".format(prefix, import_str))
265-
if import_path.startswith("../") or import_path == "..":
266-
fail("Path '{}' references a path above the execution root".format(
267-
import_str,
268-
))
269-
result.append(import_path)
270-
return result
271-
272-
def convert_legacy_create_init_to_int(kwargs):
273-
"""Convert "legacy_create_init" key to int, in-place.
274-
275-
Args:
276-
kwargs: The kwargs to modify. The key "legacy_create_init", if present
277-
and bool, will be converted to its integer value, in place.
278-
"""
279-
if is_bool(kwargs.get("legacy_create_init")):
280-
kwargs["legacy_create_init"] = 1 if kwargs["legacy_create_init"] else 0

python/private/py_binary_macro.bzl

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# limitations under the License.
1414
"""Implementation of macro-half of py_binary rule."""
1515

16-
load(":common_bazel.bzl", "convert_legacy_create_init_to_int")
1716
load(":py_binary_rule.bzl", py_binary_rule = "py_binary")
17+
load(":py_executable.bzl", "convert_legacy_create_init_to_int")
1818

1919
def py_binary(**kwargs):
2020
convert_legacy_create_init_to_int(kwargs)

python/private/py_binary_rule.bzl

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
load("@bazel_skylib//lib:dicts.bzl", "dicts")
1717
load(":attributes.bzl", "AGNOSTIC_BINARY_ATTRS")
1818
load(
19-
":py_executable_bazel.bzl",
19+
":py_executable.bzl",
2020
"create_executable_rule",
21-
"py_executable_bazel_impl",
21+
"py_executable_impl",
2222
)
2323

2424
_PY_TEST_ATTRS = {
@@ -39,7 +39,7 @@ _PY_TEST_ATTRS = {
3939
}
4040

4141
def _py_binary_impl(ctx):
42-
return py_executable_bazel_impl(
42+
return py_executable_impl(
4343
ctx = ctx,
4444
is_test = False,
4545
inherited_environment = [],

0 commit comments

Comments
 (0)