Skip to content

Commit

Permalink
Fix copy shared libraries when target label contains subdirectories
Browse files Browse the repository at this point in the history
Fixes bazelbuild#12448

Closes bazelbuild#12449.

PiperOrigin-RevId: 343109568
  • Loading branch information
hollste authored and copybara-github committed Nov 18, 2020
1 parent f581436 commit ec54013
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1049,23 +1049,26 @@ private static Packager createIntermediateDwpPackagers(
private static NestedSet<Artifact> createDynamicLibrariesCopyActions(
RuleContext ruleContext, Iterable<Artifact> dynamicLibrariesForRuntime) {
NestedSetBuilder<Artifact> result = NestedSetBuilder.stableOrder();
for (Artifact target : dynamicLibrariesForRuntime) {
for (Artifact lib : dynamicLibrariesForRuntime) {
// If the binary and the DLL don't belong to the same package or the DLL is a source file,
// we should copy the DLL to the binary's directory.
if (!ruleContext
.getLabel()
.getPackageIdentifier()
.equals(target.getOwner().getPackageIdentifier())
|| target.isSourceArtifact()) {
.equals(lib.getOwner().getPackageIdentifier())
|| lib.isSourceArtifact()) {
String targetName = ruleContext.getTarget().getName();
PathFragment targetSubDir = PathFragment.create(targetName).getParentDirectory();
// SymlinkAction on file is actually copy on Windows.
Artifact copy = ruleContext.getBinArtifact(target.getFilename());
ruleContext.registerAction(SymlinkAction.toArtifact(
ruleContext.getActionOwner(), target, copy, "Copying Execution Dynamic Library"));
Artifact copy = ruleContext.getBinArtifact(targetSubDir.getRelative(lib.getFilename()));
ruleContext.registerAction(
SymlinkAction.toArtifact(
ruleContext.getActionOwner(), lib, copy, "Copying Execution Dynamic Library"));
result.add(copy);
} else {
// If the target is already in the same directory as the binary, we don't need to copy it,
// If the library is already in the same directory as the binary, we don't need to copy it,
// but we still add it the result.
result.add(target);
result.add(lib);
}
}
return result.build();
Expand Down
24 changes: 21 additions & 3 deletions src/test/py/bazel/bazel_windows_cpp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,11 +683,25 @@ def testCopyDLLAsSource(self):
'cc_import(',
' name = "a_import",',
' shared_library = "A.dll",',
' visibility = ["//:__subpackages__"],',
')',
''
'filegroup(',
' name = "bin_src",',
' srcs = ["bin.cc"],',
' visibility = ["//:__subpackages__"],',
')',
'',
'cc_binary(',
' name = "bin",',
' srcs = ["bin.cc"],',
' srcs = ["//:bin_src"],',
' deps = ["//:a_import"],',
')',
])
self.ScratchFile('package/BUILD', [
'cc_binary(',
' name = "dir1/dir2/bin",',
' srcs = ["//:bin_src"],',
' deps = ["//:a_import"],',
')',
])
Expand All @@ -700,15 +714,19 @@ def testCopyDLLAsSource(self):
exit_code, _, stderr = self.RunBazel([
'build',
'//:bin',
'//package:dir1/dir2/bin',
])
self.AssertExitCode(exit_code, 0, stderr)

bazel_bin = self.getBazelInfo('bazel-bin')
a_dll = os.path.join(bazel_bin, 'A.dll')
# Even though A.dll is in the same package as bin.exe, but it still should
# Even though A.dll is in the same package as bin.exe, it still should
# be copied to the output directory of bin.exe.
a_dll = os.path.join(bazel_bin, 'A.dll')
self.assertTrue(os.path.exists(a_dll))

nested_a_dll = os.path.join(bazel_bin, 'package/dir1/dir2/A.dll')
self.assertTrue(os.path.exists(nested_a_dll))

def testCppErrorShouldBeVisible(self):
self.CreateWorkspaceWithDefaultRepos('WORKSPACE')
self.ScratchFile('BUILD', [
Expand Down

0 comments on commit ec54013

Please sign in to comment.