Skip to content

Commit

Permalink
Enable compiler_param_file in Windows MSVC toolchain
Browse files Browse the repository at this point in the history
Fixes bazelbuild#5163

Closes bazelbuild#17135.

PiperOrigin-RevId: 501212729
Change-Id: I6b4371a5521230c4c89f92321933b578bf5c3051
  • Loading branch information
fmeum authored and copybara-github committed Jan 11, 2023
1 parent 4c57def commit d47a410
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 35 deletions.
107 changes: 72 additions & 35 deletions src/test/py/bazel/bazel_windows_cpp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def testBuildCcBinaryFromDifferentPackage(self):
' name = "main",',
' srcs = ["main.cc"],',
' deps = ["//:B"],',
' linkstatic = 0,'
' linkstatic = 0,',
')',
])
bazel_bin = self.getBazelInfo('bazel-bin')
Expand Down Expand Up @@ -247,7 +247,7 @@ def testBuildCcBinaryDependsOnConflictDLLs(self):
' name = "main",',
' srcs = ["main.cc"],',
' deps = ["//:B", "//lib:A"],', # Transitively depends on //:A
' linkstatic = 0,'
' linkstatic = 0,',
')',
])
bazel_bin = self.getBazelInfo('bazel-bin')
Expand Down Expand Up @@ -278,14 +278,14 @@ def testBuildDifferentCcBinariesDependOnConflictDLLs(self):
' name = "main",',
' srcs = ["main.cc"],',
' deps = ["//:B"],', # Transitively depends on //:A
' linkstatic = 0,'
' linkstatic = 0,',
')',
'',
'cc_binary(',
' name = "other_main",',
' srcs = ["other_main.cc"],',
' deps = ["//lib:A"],',
' linkstatic = 0,'
' linkstatic = 0,',
')',
])
bazel_bin = self.getBazelInfo('bazel-bin')
Expand Down Expand Up @@ -386,24 +386,32 @@ def testDynamicLinkingMSVCRT(self):
# By default, it should link to msvcrt dynamically.
exit_code, _, stderr = self.RunBazel(
['build', '//:A', '--output_groups=dynamic_library', '-s'])
paramfile = os.path.join(bazel_output,
'x64_windows-fastbuild/bin/A_0.dll-2.params')
compile_params = os.path.join(
bazel_output, 'x64_windows-fastbuild/bin/_objs/A/a.obj.params'
)
link_params = os.path.join(
bazel_output, 'x64_windows-fastbuild/bin/A_0.dll-2.params'
)
self.AssertExitCode(exit_code, 0, stderr)
self.assertIn('/MD', ''.join(stderr))
self.AssertFileContentContains(paramfile, '/DEFAULTLIB:msvcrt.lib')
self.assertNotIn('/MT', ''.join(stderr))
self.AssertFileContentNotContains(paramfile, '/DEFAULTLIB:libcmt.lib')
self.AssertFileContentContains(compile_params, '/MD')
self.AssertFileContentContains(link_params, '/DEFAULTLIB:msvcrt.lib')
self.AssertFileContentNotContains(compile_params, '/MT')
self.AssertFileContentNotContains(link_params, '/DEFAULTLIB:libcmt.lib')

# Test build in debug mode.
exit_code, _, stderr = self.RunBazel(
['build', '-c', 'dbg', '//:A', '--output_groups=dynamic_library', '-s'])
paramfile = os.path.join(bazel_output,
'x64_windows-dbg/bin/A_0.dll-2.params')
compile_params = os.path.join(
bazel_output, 'x64_windows-dbg/bin/_objs/A/a.obj.params'
)
link_params = os.path.join(
bazel_output, 'x64_windows-dbg/bin/A_0.dll-2.params'
)
self.AssertExitCode(exit_code, 0, stderr)
self.assertIn('/MDd', ''.join(stderr))
self.AssertFileContentContains(paramfile, '/DEFAULTLIB:msvcrtd.lib')
self.assertNotIn('/MTd', ''.join(stderr))
self.AssertFileContentNotContains(paramfile, '/DEFAULTLIB:libcmtd.lib')
self.AssertFileContentContains(compile_params, '/MDd')
self.AssertFileContentContains(link_params, '/DEFAULTLIB:msvcrtd.lib')
self.AssertFileContentNotContains(compile_params, '/MTd')
self.AssertFileContentNotContains(link_params, '/DEFAULTLIB:libcmtd.lib')

def testStaticLinkingMSVCRT(self):
self.createProjectFiles()
Expand All @@ -414,26 +422,34 @@ def testStaticLinkingMSVCRT(self):
'build', '//:A', '--output_groups=dynamic_library',
'--features=static_link_msvcrt', '-s'
])
paramfile = os.path.join(bazel_output,
'x64_windows-fastbuild/bin/A_0.dll-2.params')
compile_params = os.path.join(
bazel_output, 'x64_windows-fastbuild/bin/_objs/A/a.obj.params'
)
link_params = os.path.join(
bazel_output, 'x64_windows-fastbuild/bin/A_0.dll-2.params'
)
self.AssertExitCode(exit_code, 0, stderr)
self.assertNotIn('/MD', ''.join(stderr))
self.AssertFileContentNotContains(paramfile, '/DEFAULTLIB:msvcrt.lib')
self.assertIn('/MT', ''.join(stderr))
self.AssertFileContentContains(paramfile, '/DEFAULTLIB:libcmt.lib')
self.AssertFileContentNotContains(compile_params, '/MD')
self.AssertFileContentNotContains(link_params, '/DEFAULTLIB:msvcrt.lib')
self.AssertFileContentContains(compile_params, '/MT')
self.AssertFileContentContains(link_params, '/DEFAULTLIB:libcmt.lib')

# Test build in debug mode.
exit_code, _, stderr = self.RunBazel([
'build', '-c', 'dbg', '//:A', '--output_groups=dynamic_library',
'--features=static_link_msvcrt', '-s'
])
paramfile = os.path.join(bazel_output,
'x64_windows-dbg/bin/A_0.dll-2.params')
compile_params = os.path.join(
bazel_output, 'x64_windows-dbg/bin/_objs/A/a.obj.params'
)
link_params = os.path.join(
bazel_output, 'x64_windows-dbg/bin/A_0.dll-2.params'
)
self.AssertExitCode(exit_code, 0, stderr)
self.assertNotIn('/MDd', ''.join(stderr))
self.AssertFileContentNotContains(paramfile, '/DEFAULTLIB:msvcrtd.lib')
self.assertIn('/MTd', ''.join(stderr))
self.AssertFileContentContains(paramfile, '/DEFAULTLIB:libcmtd.lib')
self.AssertFileContentNotContains(compile_params, '/MDd')
self.AssertFileContentNotContains(link_params, '/DEFAULTLIB:msvcrtd.lib')
self.AssertFileContentContains(compile_params, '/MTd')
self.AssertFileContentContains(link_params, '/DEFAULTLIB:libcmtd.lib')

def testBuildSharedLibraryFromCcBinaryWithStaticLink(self):
self.createProjectFiles()
Expand All @@ -444,8 +460,8 @@ def testBuildSharedLibraryFromCcBinaryWithStaticLink(self):
' name = "main.dll",',
' srcs = ["main.cc"],',
' deps = ["//:B"],', # Transitively depends on //:A
' linkstatic = 1,'
' linkshared = 1,'
' linkstatic = 1,',
' linkshared = 1,',
' features=["windows_export_all_symbols"]',
')',
])
Expand Down Expand Up @@ -479,8 +495,8 @@ def testBuildSharedLibraryFromCcBinaryWithDynamicLink(self):
' name = "main.dll",',
' srcs = ["main.cc"],',
' deps = ["//:B"],', # Transitively depends on //:A
' linkstatic = 0,'
' linkshared = 1,'
' linkstatic = 0,',
' linkshared = 1,',
' features=["windows_export_all_symbols"]',
')',
'',
Expand Down Expand Up @@ -528,8 +544,8 @@ def testGetDefFileOfSharedLibraryFromCcBinary(self):
' name = "main.dll",',
' srcs = ["main.cc"],',
' deps = ["//:B"],', # Transitively depends on //:A
' linkstatic = 1,'
' linkshared = 1,'
' linkstatic = 1,',
' linkshared = 1,',
')',
])
bazel_bin = self.getBazelInfo('bazel-bin')
Expand Down Expand Up @@ -681,7 +697,7 @@ def testCopyDLLAsSource(self):
' shared_library = "A.dll",',
' visibility = ["//:__subpackages__"],',
')',
''
'',
'filegroup(',
' name = "bin_src",',
' srcs = ["bin.cc"],',
Expand Down Expand Up @@ -1071,6 +1087,27 @@ def testBuildArm64CppBinaryWithMsvcCLAndCpuArm64Windows(self):
self.AssertExitCode(exit_code, 0, stderr)
self.assertIn('arm64\\cl.exe', ''.join(stderr))

def testLongCompileCommandLines(self):
self.CreateWorkspaceWithDefaultRepos('WORKSPACE')
self.ScratchFile(
'BUILD',
[
'cc_binary(',
' name = "long",',
' srcs = ["long.cc"],',
# Creates a command that is longer than 32767 characters, which is
# the maximum length of a command line on Windows.
' includes = [str(i) + 450 * "a" for i in range(120)],',
')',
],
)
self.ScratchFile('long.cc', ['int main() { return 0; }'])

exit_code, _, stderr = self.RunBazel(
['build', '--verbose_failures', '//:long']
)
self.AssertExitCode(exit_code, 0, stderr)


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions tools/cpp/windows_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ def _impl(ctx):

compiler_param_file_feature = feature(
name = "compiler_param_file",
enabled = True,
)

copy_dynamic_libraries_to_binary_feature = feature(
Expand Down

0 comments on commit d47a410

Please sign in to comment.