Skip to content

Commit 0419322

Browse files
committed
examples/basic: hack rules_cc
1 parent 6fee932 commit 0419322

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

examples/basic/MODULE.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
bazel_dep(name = "platforms", version = "1.0.0")
2+
bazel_dep(name = "rules_cc", version = "0.2.9")
3+
git_override(
4+
module_name = "rules_cc",
5+
commit = "d9f1178c796ee3a83a7bcaf6445601ce617b8fde",
6+
patch_strip = 1,
7+
patches = ["//:patches/rules_cc.patch"],
8+
remote = "https://github.com/bazelbuild/rules_cc.git",
9+
)
210
bazel_dep(name = "rules_python", version = "1.6.3")
311
bazel_dep(name = "pybind11_bazel")
412
local_path_override(
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
diff --git a/cc/common/cc_helper.bzl b/cc/common/cc_helper.bzl
2+
index 8444d4f..91bbed1 100644
3+
--- a/cc/common/cc_helper.bzl
4+
+++ b/cc/common/cc_helper.bzl
5+
@@ -68,6 +68,7 @@ def _libraries_from_linking_context(linking_context):
6+
def _is_valid_shared_library_name(shared_library_name):
7+
if (shared_library_name.endswith(".so") or
8+
shared_library_name.endswith(".dll") or
9+
+ shared_library_name.endswith(".pyd") or
10+
shared_library_name.endswith(".dylib") or
11+
shared_library_name.endswith(".wasm")):
12+
return True
13+
@@ -512,7 +513,7 @@ def _get_toolchain_global_make_variables(cc_toolchain):
14+
result["CROSSTOOLTOP"] = cc_toolchain._crosstool_top_path
15+
return result
16+
17+
-_SHARED_LIBRARY_EXTENSIONS = ["so", "dll", "dylib", "wasm"]
18+
+_SHARED_LIBRARY_EXTENSIONS = ["so", "dll", "pyd", "dylib", "wasm"]
19+
20+
def _is_valid_shared_library_artifact(shared_library):
21+
if (shared_library.extension in _SHARED_LIBRARY_EXTENSIONS):
22+
diff --git a/cc/common/cc_helper_internal.bzl b/cc/common/cc_helper_internal.bzl
23+
index 5309491..ef41e11 100644
24+
--- a/cc/common/cc_helper_internal.bzl
25+
+++ b/cc/common/cc_helper_internal.bzl
26+
@@ -96,7 +96,7 @@ _ARCHIVE = [".a", ".lib"]
27+
_PIC_ARCHIVE = [".pic.a"]
28+
_ALWAYSLINK_LIBRARY = [".lo"]
29+
_ALWAYSLINK_PIC_LIBRARY = [".pic.lo"]
30+
-_SHARED_LIBRARY = [".so", ".dylib", ".dll", ".wasm"]
31+
+_SHARED_LIBRARY = [".so", ".dylib", ".dll", ".pyd", ".wasm"]
32+
_INTERFACE_SHARED_LIBRARY = [".ifso", ".tbd", ".lib", ".dll.a"]
33+
_OBJECT_FILE = [".o", ".obj"]
34+
_PIC_OBJECT_FILE = [".pic.o"]
35+
@@ -170,7 +170,7 @@ _ArtifactCategoryInfo, _unused_new_aci = provider(
36+
_artifact_categories = [
37+
_ArtifactCategoryInfo("STATIC_LIBRARY", "lib", ".a", ".lib"),
38+
_ArtifactCategoryInfo("ALWAYSLINK_STATIC_LIBRARY", "lib", ".lo", ".lo.lib"),
39+
- _ArtifactCategoryInfo("DYNAMIC_LIBRARY", "lib", ".so", ".dylib", ".dll", ".wasm"),
40+
+ _ArtifactCategoryInfo("DYNAMIC_LIBRARY", "lib", ".so", ".dylib", ".dll", ".pyd", ".wasm"),
41+
_ArtifactCategoryInfo("EXECUTABLE", "", "", ".exe", ".wasm"),
42+
_ArtifactCategoryInfo("INTERFACE_LIBRARY", "lib", ".ifso", ".tbd", ".if.lib", ".lib"),
43+
_ArtifactCategoryInfo("PIC_FILE", "", ".pic"),
44+
diff --git a/cc/private/link/create_libraries_to_link_values.bzl b/cc/private/link/create_libraries_to_link_values.bzl
45+
index 3adf203..7a833e8 100644
46+
--- a/cc/private/link/create_libraries_to_link_values.bzl
47+
+++ b/cc/private/link/create_libraries_to_link_values.bzl
48+
@@ -286,11 +286,12 @@ def _add_dynamic_library_to_link(
49+
# -l:libfoo.so.1 -> libfoo.so.1
50+
has_compatible_name = (
51+
name.startswith("lib") or
52+
- (not name.endswith(".so") and not name.endswith(".dylib") and not name.endswith(".dll"))
53+
+ (not name.endswith(".so") and not name.endswith(".dylib") and \
54+
+ not name.endswith(".dll") and not name.endswith(".pyd"))
55+
)
56+
if shared_library and has_compatible_name:
57+
lib_name = name.removeprefix("lib").removesuffix(".so").removesuffix(".dylib") \
58+
- .removesuffix(".dll")
59+
+ .removesuffix(".dll").removesuffix(".pyd")
60+
libraries_to_link_values.append(
61+
_NamedLibraryInfo(
62+
type = _TYPE.DYNAMIC_LIBRARY,
63+
diff --git a/cc/private/rules_impl/cc_binary.bzl b/cc/private/rules_impl/cc_binary.bzl
64+
index 8b3f430..f1d0943 100644
65+
--- a/cc/private/rules_impl/cc_binary.bzl
66+
+++ b/cc/private/rules_impl/cc_binary.bzl
67+
@@ -331,7 +331,7 @@ def _create_transitive_linking_actions(
68+
# entries during linking process.
69+
for libs in precompiled_files[:]:
70+
for artifact in libs:
71+
- if _matches([".so", ".dylib", ".dll", ".ifso", ".tbd", ".lib", ".dll.a"], artifact.basename) or cc_helper.is_valid_shared_library_artifact(artifact):
72+
+ if _matches([".so", ".dylib", ".dll", ".pyd", ".ifso", ".tbd", ".lib", ".dll.a"], artifact.basename) or cc_helper.is_valid_shared_library_artifact(artifact):
73+
library_to_link = cc_common.create_library_to_link(
74+
actions = ctx.actions,
75+
feature_configuration = feature_configuration,
76+
@@ -477,7 +477,7 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
77+
# the target name.
78+
# This is no longer necessary, the toolchain can figure out the correct file extensions.
79+
target_name = ctx.label.name
80+
- has_legacy_link_shared_name = _is_link_shared(ctx) and (_matches([".so", ".dylib", ".dll"], target_name) or cc_helper.is_valid_shared_library_name(target_name))
81+
+ has_legacy_link_shared_name = _is_link_shared(ctx) and (_matches([".so", ".dylib", ".dll", ".pyd"], target_name) or cc_helper.is_valid_shared_library_name(target_name))
82+
binary = None
83+
is_dbg_build = (cc_toolchain._cpp_configuration.compilation_mode() == "dbg")
84+
if has_legacy_link_shared_name:
85+
diff --git a/cc/private/toolchain/windows_cc_toolchain_config.bzl b/cc/private/toolchain/windows_cc_toolchain_config.bzl
86+
index 5d0d40f..38a67aa 100644
87+
--- a/cc/private/toolchain/windows_cc_toolchain_config.bzl
88+
+++ b/cc/private/toolchain/windows_cc_toolchain_config.bzl
89+
@@ -119,6 +119,11 @@ def _impl(ctx):
90+
prefix = "",
91+
extension = ".dll",
92+
),
93+
+ artifact_name_pattern(
94+
+ category_name = "dynamic_library",
95+
+ prefix = "",
96+
+ extension = ".pyd",
97+
+ ),
98+
artifact_name_pattern(
99+
category_name = "interface_library",
100+
prefix = "",

0 commit comments

Comments
 (0)