aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/py
diff options
context:
space:
mode:
authorGravatar Yun Peng <pcloudy@google.com>2017-09-20 10:25:48 +0200
committerGravatar László Csomor <laszlocsomor@google.com>2017-09-20 11:59:04 +0200
commit481657d6149b3c68b893ad8221ac1bccccf1060d (patch)
treedb33e89dbfd5f9df16143de5553cbaf1ab638159 /src/test/py
parente28b772d85a463e4fe154767d0d82c0fb9e63c3d (diff)
Windows: Make dynamic libraries available to binary at runtime
When copy_dynamic_libraries_to_binary is enabled, we copy the shared libraries required by the binary to the binary's directory. Bazel will throw errors if there are confilct actions generating the same artifacts. Change-Id: I09a5a599ca0ec7a67efd49d5aa89481450fa4e90 PiperOrigin-RevId: 169364498
Diffstat (limited to 'src/test/py')
-rw-r--r--src/test/py/bazel/bazel_windows_dynamic_link_test.py103
1 files changed, 102 insertions, 1 deletions
diff --git a/src/test/py/bazel/bazel_windows_dynamic_link_test.py b/src/test/py/bazel/bazel_windows_dynamic_link_test.py
index 33440cd033..1093037ad0 100644
--- a/src/test/py/bazel/bazel_windows_dynamic_link_test.py
+++ b/src/test/py/bazel/bazel_windows_dynamic_link_test.py
@@ -23,6 +23,7 @@ class BazelWindowsDynamicLinkTest(test_base.TestBase):
self.ScratchFile('WORKSPACE')
self.ScratchFile('BUILD', [
'package(',
+ ' default_visibility = ["//visibility:public"],',
' features=["windows_export_all_symbols"]',
')',
'',
@@ -49,6 +50,7 @@ class BazelWindowsDynamicLinkTest(test_base.TestBase):
' linkstatic = 0,',
')',
])
+
self.ScratchFile('a.cc', [
'#include <stdio.h>',
'#include "a.h"',
@@ -58,6 +60,7 @@ class BazelWindowsDynamicLinkTest(test_base.TestBase):
' printf("Hello A, %d\\n", a);',
'}',
])
+
self.ScratchFile('b.cc', [
'#include <stdio.h>',
'#include "a.h"',
@@ -88,7 +91,7 @@ class BazelWindowsDynamicLinkTest(test_base.TestBase):
self.ScratchFile('b.h',
[line.replace('%{name}', 'B') for line in header_temp])
- self.ScratchFile('c.cc', [
+ c_cc_content = [
'#include <stdio.h>',
'#include "a.h"',
'#include "b.h"',
@@ -103,7 +106,21 @@ class BazelWindowsDynamicLinkTest(test_base.TestBase):
' hello_C();',
' return 0;',
'}',
+ ]
+
+ self.ScratchFile('c.cc', c_cc_content)
+
+ self.ScratchFile('lib/BUILD', [
+ 'cc_library(',
+ ' name = "A",',
+ ' srcs = ["dummy.cc"],',
+ ' features = ["windows_export_all_symbols"],',
+ ' visibility = ["//visibility:public"],',
+ ')',
])
+ self.ScratchFile('lib/dummy.cc', ['void dummy() {}'])
+
+ self.ScratchFile('main/main.cc', c_cc_content)
def getBazelInfo(self, info_key):
exit_code, stdout, stderr = self.RunBazel(['info', info_key])
@@ -188,6 +205,90 @@ class BazelWindowsDynamicLinkTest(test_base.TestBase):
# c_exe
self.assertTrue(os.path.exists(os.path.join(bazel_bin, 'C.exe')))
+ def testBuildCcBinaryFromDifferentPackage(self):
+ self.createProjectFiles()
+ self.ScratchFile('main/BUILD', [
+ 'cc_binary(',
+ ' name = "main",',
+ ' srcs = ["main.cc"],',
+ ' deps = ["//:B"],',
+ ' linkstatic = 0,'
+ ')',
+ ])
+ bazel_bin = self.getBazelInfo('bazel-bin')
+
+ # We dynamically link to msvcrt by setting USE_DYNAMIC_CRT=1
+ exit_code, _, stderr = self.RunBazel(
+ ['build', '//main:main', '--action_env=USE_DYNAMIC_CRT=1'])
+ self.AssertExitCode(exit_code, 0, stderr)
+
+ # Test if libA.so and libB.so are copied to the directory of main.exe
+ main_bin = os.path.join(bazel_bin, 'main/main.exe')
+ self.assertTrue(os.path.exists(main_bin))
+ self.assertTrue(os.path.exists(os.path.join(bazel_bin, 'main/libA.so')))
+ self.assertTrue(os.path.exists(os.path.join(bazel_bin, 'main/libB.so')))
+
+ # Run the binary to see if it runs successfully
+ exit_code, stdout, stderr = self.RunProgram([main_bin])
+ self.AssertExitCode(exit_code, 0, stderr)
+ self.assertEqual(['Hello A, 1', 'Hello A, 2', 'Hello B', 'Hello C'], stdout)
+
+ def testBuildCcBinaryDependsOnConflictDLLs(self):
+ self.createProjectFiles()
+ self.ScratchFile(
+ 'main/BUILD',
+ [
+ 'cc_binary(',
+ ' name = "main",',
+ ' srcs = ["main.cc"],',
+ ' deps = ["//:B", "//lib:A"],', # Transitively depends on //:A
+ ' linkstatic = 0,'
+ ')',
+ ])
+
+ # //main:main depends on both //lib:A and //:A,
+ # their dlls are both called libA.so,
+ # so there should be a conflict error
+ exit_code, _, stderr = self.RunBazel(['build', '//main:main'])
+ self.AssertExitCode(exit_code, 1, stderr)
+ self.assertIn(
+ 'ERROR: file \'main/libA.so\' is generated by these conflicting '
+ 'actions:',
+ ''.join(stderr))
+
+ def testBuildDifferentCcBinariesDependOnConflictDLLs(self):
+ self.createProjectFiles()
+ self.ScratchFile(
+ 'main/BUILD',
+ [
+ 'cc_binary(',
+ ' name = "main",',
+ ' srcs = ["main.cc"],',
+ ' deps = ["//:B"],', # Transitively depends on //:A
+ ' linkstatic = 0,'
+ ')',
+ '',
+ 'cc_binary(',
+ ' name = "other_main",',
+ ' srcs = ["other_main.cc"],',
+ ' deps = ["//lib:A"],',
+ ' linkstatic = 0,'
+ ')',
+ ])
+ self.ScratchFile('main/other_main.cc', ['int main() {return 0;}'])
+
+ # Building //main:main should succeed
+ exit_code, _, stderr = self.RunBazel(['build', '//main:main'])
+ self.AssertExitCode(exit_code, 0, stderr)
+
+ # Building //main:other_main after //main:main should fail
+ exit_code, _, stderr = self.RunBazel(['build', '//main:other_main'])
+ self.AssertExitCode(exit_code, 1, stderr)
+ self.assertIn(
+ 'ERROR: file \'main/libA.so\' is generated by these conflicting '
+ 'actions:',
+ ''.join(stderr))
+
if __name__ == '__main__':
unittest.main()