Skip to content

Commit 31c8156

Browse files
committed
Use custom absolute path checking function
1 parent 23d5348 commit 31c8156

File tree

7 files changed

+58
-5
lines changed

7 files changed

+58
-5
lines changed

cc/common/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ bzl_library(
1919
srcs = glob(["*.bzl"]),
2020
visibility = ["//visibility:public"],
2121
deps = [
22+
"//cc/private:paths_bzl",
2223
"//cc/private/rules_impl:native_bzl",
2324
],
2425
)
@@ -29,6 +30,7 @@ bzl_library(
2930
visibility = ["//visibility:public"],
3031
deps = [
3132
":visibility_bzl",
33+
"//cc/private:paths_bzl",
3234
"//cc/private/rules_impl:objc_common",
3335
],
3436
)

cc/common/cc_helper.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# limitations under the License.
1414
"""Utility functions for C++ rules."""
1515

16-
load("@bazel_skylib//lib:paths.bzl", "paths")
1716
load("//cc:find_cc_toolchain.bzl", "CC_TOOLCHAIN_TYPE")
17+
load("//cc/private:paths.bzl", "is_path_absolute")
1818
load("//cc/private/rules_impl:objc_common.bzl", "objc_common")
1919
load(":cc_common.bzl", "cc_common")
2020
load(
@@ -808,7 +808,7 @@ def _include_dirs(ctx, additional_make_variable_substitutions):
808808
package_source_root = _package_source_root(ctx.label.workspace_name, package, sibling_repository_layout)
809809
for include in ctx.attr.includes:
810810
includes_attr = _expand(ctx, include, additional_make_variable_substitutions)
811-
if paths.is_absolute(includes_attr):
811+
if is_path_absolute(includes_attr):
812812
continue
813813
includes_path = get_relative_path(package_exec_path, includes_attr)
814814
if not sibling_repository_layout and path_contains_up_level_references(includes_path):

cc/common/cc_helper_internal.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Only use those within C++ implementation. The others need to go through cc_commo
1919
"""
2020

2121
load("@bazel_skylib//lib:paths.bzl", "paths")
22+
load("//cc/private:paths.bzl", "is_path_absolute")
2223

2324
# LINT.IfChange(forked_exports)
2425

@@ -198,7 +199,7 @@ def repository_exec_path(repository, sibling_repository_layout):
198199
# LINT.ThenChange(https://github.com/bazelbuild/bazel/blob/master/src/main/starlark/builtins_bzl/common/cc/cc_helper_internal.bzl:forked_exports)
199200

200201
def get_relative_path(path_a, path_b):
201-
if paths.is_absolute(path_b):
202+
if is_path_absolute(path_b):
202203
return path_b
203204
return paths.normalize(paths.join(path_a, path_b))
204205

cc/private/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,22 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
16+
1517
filegroup(
1618
name = "srcs",
1719
srcs = glob([
1820
"**/BUILD",
21+
"*.bzl",
1922
]) + [
2023
"//cc/private/rules_impl:srcs",
2124
"//cc/private/toolchain:srcs",
2225
],
2326
visibility = ["//visibility:public"],
2427
)
28+
29+
bzl_library(
30+
name = "paths_bzl",
31+
srcs = ["paths.bzl"],
32+
visibility = ["//cc:__subpackages__"],
33+
)

cc/private/paths.bzl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Copyright 2025 The Bazel Authors. 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+
15+
"""Paths utility functions."""
16+
17+
def is_path_absolute(path):
18+
"""Checks whether a path is absolute or not.
19+
20+
Note that is does not take the execution platform into account.
21+
22+
This was implemented to replace skylib's paths.is_absolute, which
23+
only checks for the presence of a colon character in the second position
24+
of the paths
25+
26+
Args:
27+
path: A path (as a string).
28+
29+
Returns:
30+
`True` if `path` is an absolute path.
31+
"""
32+
33+
if path.startswith("/"):
34+
return True
35+
36+
# Check for DOS-style absolute paths for Windows
37+
return len(path) >= 3 and \
38+
path[0].isalpha() and \
39+
path[1] == ":" and \
40+
path[2] in ("/", "\\")

cc/toolchains/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ bzl_library(
2222
"//cc:action_names_bzl",
2323
"//cc:cc_toolchain_config_lib_bzl",
2424
"//cc:find_cc_toolchain_bzl",
25+
"//cc/private:paths_bzl",
2526
"//cc/private/rules_impl:cc_flags_supplier_lib_bzl",
2627
"//cc/private/rules_impl:native_bzl",
2728
"//cc/toolchains/impl:toolchain_impl_rules",

cc/toolchains/args.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
# limitations under the License.
1414
"""All providers for rule-based bazel toolchain config."""
1515

16-
load("@bazel_skylib//lib:paths.bzl", "paths")
1716
load("@bazel_skylib//rules/directory:providers.bzl", "DirectoryInfo")
17+
load("//cc/private:paths.bzl", "is_path_absolute")
1818
load("//cc/toolchains/impl:args_utils.bzl", "validate_env_variables", "validate_nested_args")
1919
load(
2020
"//cc/toolchains/impl:collect.bzl",
@@ -51,7 +51,7 @@ def _cc_args_impl(ctx):
5151
)
5252

5353
for path in ctx.attr.allowlist_absolute_include_directories:
54-
if not paths.is_absolute(path):
54+
if not is_path_absolute(path):
5555
fail("`{}` is not an absolute paths".format(path))
5656

5757
nested = None

0 commit comments

Comments
 (0)