Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update LibrariesToLinkCollector.java for .dll suffix stripping #21404

Closed
wants to merge 2 commits into from
Closed

Update LibrariesToLinkCollector.java for .dll suffix stripping #21404

wants to merge 2 commits into from

Conversation

vvviktor
Copy link
Contributor

Fixes #19696

Allow automatic linkage of DLL libraries when GNU toolchain used in Windows. .dll suffix has to be stripped before passing library name to ld.exe with -l option.

This case was successfully tested :

Workspace structure:

D:.
│   .bazelrc
│   BUILD
│   MODULE.bazel
│   MODULE.bazel.lock
│   WORKSPACE
│
├───Main
│       BUILD
│       main.cpp
│       math.cpp
│       math.h
│       math_dll_interface.cpp
│       math_dll_interface.h
│       math_import_defs.h
│
└───toolchain
        BUILD
        toolchain_config.bzl

BUILD file:

# //Main/BUILD

DLL_HDRS = ["math_import_defs.h", "math_dll_interface.h"]

cc_binary(
    name = "sum_numbers_mingw",
    srcs = ["main.cpp"],
    deps = [":math_d_shared"]
)

cc_import(
    name = "math_d_shared",
    hdrs = DLL_HDRS,
    shared_library = ":libmath_d.dll"
)

cc_binary(
    name = "libmath_d.dll",
    srcs = ["math_dll_interface.cpp"] + DLL_HDRS,
    deps = [":math"],
    defines = ["MATH_DLL"],
    linkshared = 1
)

cc_library(
    name = "math",
    srcs = ["math.cpp"],
    hdrs = ["math.h"],
    copts = ["-std=c++17"]
)

Without patch applied bazel build //main:sum_numbers_mingw --verbose_failures fails with error:
C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmath_d.dll: No such file or directory
Then after patch was applied it builds all targets as expected.

This approach also works fine with patch applied:

# //Main/BUILD

DLL_HDRS = ["math_import_defs.h", "math_dll_interface.h"]

cc_binary(
    name = "sum_numbers_mingw",
    srcs = ["main.cpp"] + DLL_HDRS,
    dynamic_deps = [":math_d_shared"]
)

cc_shared_library(
    name = "math_d_shared",
    shared_lib_name = "libmath_d.dll",
    deps = [":math_dll_interface"]
)

cc_library(
    name = "math_dll_interface",
    srcs = ["math_dll_interface.cpp"],
    hdrs = DLL_HDRS,
    deps = [":math"],
    defines = ["MATH_DLL"]
)

cc_library(
    name = "math",
    srcs = ["math.cpp"],
    hdrs = ["math.h"],
    copts = ["-std=c++17"]
)

@vvviktor vvviktor requested a review from lberki as a code owner February 19, 2024 00:20
@github-actions github-actions bot added team-Rules-CPP Issues for C++ rules awaiting-review PR is awaiting review from an assigned reviewer labels Feb 19, 2024
Copy link
Member

@meteorcloudy meteorcloudy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@meteorcloudy meteorcloudy added awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally and removed awaiting-review PR is awaiting review from an assigned reviewer labels Feb 28, 2024
@fmeum
Copy link
Collaborator

fmeum commented Feb 28, 2024

@bazel-io fork 7.1.0

@github-actions github-actions bot removed the awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally label Feb 29, 2024
bazel-io pushed a commit to bazel-io/bazel that referenced this pull request Feb 29, 2024
Fixes bazelbuild#19696

Allow automatic linkage of DLL libraries when GNU toolchain used in Windows. .dll suffix has to be stripped before passing library name to `ld.exe` with `-l` option.

This [case](https://github.com/vvviktor/bazel_sandbox.git) was successfully tested :

Workspace structure:
```
D:.
│   .bazelrc
│   BUILD
│   MODULE.bazel
│   MODULE.bazel.lock
│   WORKSPACE
│
├───Main
│       BUILD
│       main.cpp
│       math.cpp
│       math.h
│       math_dll_interface.cpp
│       math_dll_interface.h
│       math_import_defs.h
│
└───toolchain
        BUILD
        toolchain_config.bzl
```

BUILD file:
```
# //Main/BUILD

DLL_HDRS = ["math_import_defs.h", "math_dll_interface.h"]

cc_binary(
    name = "sum_numbers_mingw",
    srcs = ["main.cpp"],
    deps = [":math_d_shared"]
)

cc_import(
    name = "math_d_shared",
    hdrs = DLL_HDRS,
    shared_library = ":libmath_d.dll"
)

cc_binary(
    name = "libmath_d.dll",
    srcs = ["math_dll_interface.cpp"] + DLL_HDRS,
    deps = [":math"],
    defines = ["MATH_DLL"],
    linkshared = 1
)

cc_library(
    name = "math",
    srcs = ["math.cpp"],
    hdrs = ["math.h"],
    copts = ["-std=c++17"]
)
```
Without patch applied `bazel build //main:sum_numbers_mingw --verbose_failures` fails with error:
`C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmath_d.dll: No such file or directory`
Then after patch was applied it builds all targets as expected.

This approach also works fine with patch applied:
```
# //Main/BUILD

DLL_HDRS = ["math_import_defs.h", "math_dll_interface.h"]

cc_binary(
    name = "sum_numbers_mingw",
    srcs = ["main.cpp"] + DLL_HDRS,
    dynamic_deps = [":math_d_shared"]
)

cc_shared_library(
    name = "math_d_shared",
    shared_lib_name = "libmath_d.dll",
    deps = [":math_dll_interface"]
)

cc_library(
    name = "math_dll_interface",
    srcs = ["math_dll_interface.cpp"],
    hdrs = DLL_HDRS,
    deps = [":math"],
    defines = ["MATH_DLL"]
)

cc_library(
    name = "math",
    srcs = ["math.cpp"],
    hdrs = ["math.h"],
    copts = ["-std=c++17"]
)
```

Closes bazelbuild#21404.

PiperOrigin-RevId: 611401823
Change-Id: I98fbfb245acdd2dac41d6a56b5f74059dc53a082
github-merge-queue bot pushed a commit that referenced this pull request Mar 4, 2024
#21524)

Fixes #19696

Allow automatic linkage of DLL libraries when GNU toolchain used in
Windows. .dll suffix has to be stripped before passing library name to
`ld.exe` with `-l` option.

This [case](https://github.com/vvviktor/bazel_sandbox.git) was
successfully tested :

Workspace structure:
```
D:.
│   .bazelrc
│   BUILD
│   MODULE.bazel
│   MODULE.bazel.lock
│   WORKSPACE
│
├───Main
│       BUILD
│       main.cpp
│       math.cpp
│       math.h
│       math_dll_interface.cpp
│       math_dll_interface.h
│       math_import_defs.h
│
└───toolchain
        BUILD
        toolchain_config.bzl
```

BUILD file:
```
# //Main/BUILD

DLL_HDRS = ["math_import_defs.h", "math_dll_interface.h"]

cc_binary(
    name = "sum_numbers_mingw",
    srcs = ["main.cpp"],
    deps = [":math_d_shared"]
)

cc_import(
    name = "math_d_shared",
    hdrs = DLL_HDRS,
    shared_library = ":libmath_d.dll"
)

cc_binary(
    name = "libmath_d.dll",
    srcs = ["math_dll_interface.cpp"] + DLL_HDRS,
    deps = [":math"],
    defines = ["MATH_DLL"],
    linkshared = 1
)

cc_library(
    name = "math",
    srcs = ["math.cpp"],
    hdrs = ["math.h"],
    copts = ["-std=c++17"]
)
```
Without patch applied `bazel build //main:sum_numbers_mingw
--verbose_failures` fails with error:

`C:/msys64/ucrt64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe:
cannot find -lmath_d.dll: No such file or directory`
Then after patch was applied it builds all targets as expected.

This approach also works fine with patch applied:
```
# //Main/BUILD

DLL_HDRS = ["math_import_defs.h", "math_dll_interface.h"]

cc_binary(
    name = "sum_numbers_mingw",
    srcs = ["main.cpp"] + DLL_HDRS,
    dynamic_deps = [":math_d_shared"]
)

cc_shared_library(
    name = "math_d_shared",
    shared_lib_name = "libmath_d.dll",
    deps = [":math_dll_interface"]
)

cc_library(
    name = "math_dll_interface",
    srcs = ["math_dll_interface.cpp"],
    hdrs = DLL_HDRS,
    deps = [":math"],
    defines = ["MATH_DLL"]
)

cc_library(
    name = "math",
    srcs = ["math.cpp"],
    hdrs = ["math.h"],
    copts = ["-std=c++17"]
)
```

Closes #21404.

Commit
e2837db

PiperOrigin-RevId: 611401823
Change-Id: I98fbfb245acdd2dac41d6a56b5f74059dc53a082

Co-authored-by: Viktor Kustov <[email protected]>
Co-authored-by: Yun Peng <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-Rules-CPP Issues for C++ rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Linking fails for library name contains .dll suffix
3 participants