Skip to content

Commit 69897b6

Browse files
Mizuxcopybara-github
authored andcommitted
fix dynamic library .pyd support
* should address the example listed in #17117 * related to bazelbuild/rules_cc#501 * needed to fix pybind/pybind11_bazel#124 Closes #27434. PiperOrigin-RevId: 826451549 Change-Id: I79872e654fdc7c73a5e05f3befdc734bd82c4f07
1 parent 0635b5c commit 69897b6

File tree

8 files changed

+26
-19
lines changed

8 files changed

+26
-19
lines changed

src/main/cpp/blaze_util_windows.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,8 @@ std::unique_ptr<blaze_util::Path> GetProcessCWD(int pid) {
466466
}
467467

468468
bool IsSharedLibrary(const string& filename) {
469-
return blaze_util::ends_with(filename, ".dll");
469+
return (blaze_util::ends_with(filename, ".dll") ||
470+
blaze_util::ends_with(filename, ".pyd"));
470471
}
471472

472473
string GetSystemJavabase() {

src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
public enum ArtifactCategory {
2424
STATIC_LIBRARY("lib", ".a", ".lib"),
2525
ALWAYSLINK_STATIC_LIBRARY("lib", ".lo", ".lo.lib"),
26-
DYNAMIC_LIBRARY("lib", ".so", ".dylib", ".dll", ".wasm"),
26+
DYNAMIC_LIBRARY("lib", ".so", ".dylib", ".dll", ".pyd", ".wasm"),
2727
EXECUTABLE("", "", ".exe", ".wasm"),
2828
INTERFACE_LIBRARY("lib", ".ifso", ".tbd", ".if.lib", ".lib"),
2929
PIC_FILE("", ".pic"),

src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,8 @@ def _create_transitive_linking_actions(
317317
# entries during linking process.
318318
for libs in precompiled_files[:]:
319319
for artifact in libs:
320-
if _matches([".so", ".dylib", ".dll", ".ifso", ".tbd", ".lib", ".dll.a"], artifact.basename) or cc_helper.is_valid_shared_library_artifact(artifact):
320+
if (_matches([".so", ".dylib", ".dll", ".pyd", ".ifso", ".tbd", ".lib", ".dll.a"], artifact.basename) or
321+
cc_helper.is_valid_shared_library_artifact(artifact)):
321322
library_to_link = cc_common.create_library_to_link(
322323
actions = ctx.actions,
323324
feature_configuration = feature_configuration,
@@ -463,7 +464,9 @@ def cc_binary_impl(ctx, additional_linkopts, force_linkstatic = False):
463464
# the target name.
464465
# This is no longer necessary, the toolchain can figure out the correct file extensions.
465466
target_name = ctx.label.name
466-
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))
467+
has_legacy_link_shared_name = (_is_link_shared(ctx) and
468+
(_matches([".so", ".dylib", ".dll", ".pyd"], target_name) or
469+
cc_helper.is_valid_shared_library_name(target_name)))
467470
binary = None
468471
is_dbg_build = (cc_toolchain._cpp_configuration.compilation_mode() == "dbg")
469472
if has_legacy_link_shared_name:

src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def _is_valid_shared_library_name(shared_library_name):
130130
if (shared_library_name.endswith(".so") or
131131
shared_library_name.endswith(".dll") or
132132
shared_library_name.endswith(".dylib") or
133+
shared_library_name.endswith(".pyd") or
133134
shared_library_name.endswith(".wasm")):
134135
return True
135136

@@ -549,7 +550,7 @@ def _get_toolchain_global_make_variables(cc_toolchain):
549550
result["CROSSTOOLTOP"] = cc_toolchain._crosstool_top_path
550551
return result
551552

552-
_SHARED_LIBRARY_EXTENSIONS = ["so", "dll", "dylib", "wasm"]
553+
_SHARED_LIBRARY_EXTENSIONS = ["so", "dll", "dylib", "pyd", "wasm"]
553554

554555
def _is_valid_shared_library_artifact(shared_library):
555556
if (shared_library.extension in _SHARED_LIBRARY_EXTENSIONS):

src/main/starlark/builtins_bzl/common/cc/cc_helper_internal.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ _ARCHIVE = [".a", ".lib"]
103103
_PIC_ARCHIVE = [".pic.a"]
104104
_ALWAYSLINK_LIBRARY = [".lo"]
105105
_ALWAYSLINK_PIC_LIBRARY = [".pic.lo"]
106-
_SHARED_LIBRARY = [".so", ".dylib", ".dll", ".wasm"]
106+
_SHARED_LIBRARY = [".so", ".dylib", ".dll", ".pyd", ".wasm"]
107107
_INTERFACE_SHARED_LIBRARY = [".ifso", ".tbd", ".lib", ".dll.a"]
108108
_OBJECT_FILE = [".o", ".obj"]
109109
_PIC_OBJECT_FILE = [".pic.o"]
@@ -177,7 +177,7 @@ _ArtifactCategoryInfo, _unused_new_aci = provider(
177177
_artifact_categories = [
178178
_ArtifactCategoryInfo("STATIC_LIBRARY", "lib", ".a", ".lib"),
179179
_ArtifactCategoryInfo("ALWAYSLINK_STATIC_LIBRARY", "lib", ".lo", ".lo.lib"),
180-
_ArtifactCategoryInfo("DYNAMIC_LIBRARY", "lib", ".so", ".dylib", ".dll", ".wasm"),
180+
_ArtifactCategoryInfo("DYNAMIC_LIBRARY", "lib", ".so", ".dylib", ".dll", ".pyd", ".wasm"),
181181
_ArtifactCategoryInfo("EXECUTABLE", "", "", ".exe", ".wasm"),
182182
_ArtifactCategoryInfo("INTERFACE_LIBRARY", "lib", ".ifso", ".tbd", ".if.lib", ".lib"),
183183
_ArtifactCategoryInfo("PIC_FILE", "", ".pic"),

src/main/starlark/builtins_bzl/common/cc/cc_import.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _perform_error_checks(
4242

4343
if (shared_library_artifact != None and
4444
not cc_helper.is_valid_shared_library_artifact(shared_library_artifact)):
45-
fail("'shared_library' does not produce any cc_import shared_library files (expected .so, .dylib or .dll)")
45+
fail("'shared_library' does not produce any cc_import shared_library files (expected .so, .dylib, .dll or .pyd)")
4646

4747
def _create_archive_action(
4848
ctx,
@@ -340,7 +340,8 @@ binary that depends on it during runtime.
340340
<p> Permitted file types:
341341
<code>.so</code>,
342342
<code>.dll</code>
343-
or <code>.dylib</code>
343+
<code>.dylib</code>,
344+
or <code>.pyd</code>
344345
</p>"""),
345346
"interface_library": attr.label(
346347
allow_single_file = [".ifso", ".tbd", ".lib", ".so", ".dylib"],

src/main/starlark/builtins_bzl/common/cc/link/create_libraries_to_link_values.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,12 @@ def _add_dynamic_library_to_link(
287287
# -l:libfoo.so.1 -> libfoo.so.1
288288
has_compatible_name = (
289289
name.startswith("lib") or
290-
(not name.endswith(".so") and not name.endswith(".dylib") and not name.endswith(".dll"))
290+
(not name.endswith(".so") and not name.endswith(".dylib") and
291+
not name.endswith(".dll") and not name.endswith(".pyd"))
291292
)
292293
if shared_library and has_compatible_name:
293294
lib_name = name.removeprefix("lib").removesuffix(".so").removesuffix(".dylib") \
294-
.removesuffix(".dll")
295+
.removesuffix(".dll").removesuffix(".pyd")
295296
libraries_to_link_values.append(
296297
_NamedLibraryInfo(
297298
type = _TYPE.DYNAMIC_LIBRARY,

third_party/BUILD

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -512,15 +512,15 @@ alias(
512512

513513
UNNECESSARY_DYNAMIC_LIBRARIES = select({
514514
"//src/conditions:windows": "*.so *.jnilib",
515-
"//src/conditions:darwin": "*.so *.dll",
516-
"//src/conditions:linux_x86_64": "*.jnilib *.dll",
517-
"//src/conditions:linux_s390x": "*.jnilib *.dll",
515+
"//src/conditions:darwin": "*.so *.dll *.pyd",
516+
"//src/conditions:linux_x86_64": "*.jnilib *.dll *.pyd",
517+
"//src/conditions:linux_s390x": "*.jnilib *.dll *.pyd",
518518
# The .so file is an x86/s390x one, so we can just remove it if the CPU is not x86/s390x
519-
"//src/conditions:arm": "*.so *.jnilib *.dll",
520-
"//src/conditions:linux_aarch64": "*.so *.jnilib *.dll",
521-
"//src/conditions:linux_ppc": "*.so *.jnilib *.dll",
522-
"//src/conditions:freebsd": "*.so *.jnilib *.dll",
523-
"//src/conditions:openbsd": "*.so *.jnilib *.dll",
519+
"//src/conditions:arm": "*.so *.jnilib *.dll *.pyd",
520+
"//src/conditions:linux_aarch64": "*.so *.jnilib *.dll *.pyd",
521+
"//src/conditions:linux_ppc": "*.so *.jnilib *.dll *.pyd",
522+
"//src/conditions:freebsd": "*.so *.jnilib *.dll *.pyd",
523+
"//src/conditions:openbsd": "*.so *.jnilib *.dll *.pyd",
524524
# Default is to play it safe -- better have a big binary than a slow binary
525525
# The empty string means nothing is to be removed from the library;
526526
# the rule command tests for the empty string explictly to avoid

0 commit comments

Comments
 (0)