Skip to content

Commit

Permalink
Make aspect MSVC compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
martis42 committed Jan 18, 2024
1 parent 3455428 commit a60f597
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/aspect/dwyu.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,15 @@ def extract_defines_from_compiler_flags(compiler_flags):
defines = {}

for cflag in compiler_flags:
if cflag.startswith("-U"):
# gcc/clang use '-U'. MSVC uses '/U'.
if cflag.startswith(("-U", "/U")):
undefine = cflag[2:]
undefine_name = undefine.split("=", 1)[0]
if undefine_name in defines.keys():
defines.pop(undefine_name)
if cflag.startswith("-D"):

# gcc/clang use '-D'. MSVC uses '/D'.
if cflag.startswith(("-D", "/D")):
define = cflag[2:]
define_name = define.split("=", 1)[0]
defines[define_name] = define
Expand Down
7 changes: 7 additions & 0 deletions src/aspect/test/dwyu_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ def _extract_defines_from_compiler_flags_test_impl(ctx):
extract_defines_from_compiler_flags(["-DFoo=BAR=42", "-DTick 42", '-DRiff "Raff"']),
)

# MSVC syntax
asserts.equals(
env,
["Bar", "FooBar=42"],
extract_defines_from_compiler_flags(["/DFoo", "/UFoo", "/DBar", "/DFooBar=42"]),
)

return unittest.end(env)

extract_defines_from_compiler_flags_test = unittest.make(_extract_defines_from_compiler_flags_test_impl)
Expand Down
6 changes: 5 additions & 1 deletion test/aspect/defines/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ cc_library(
cc_library(
name = "defines_from_bazel_target",
hdrs = ["defines_from_bazel_target.h"],
copts = ["-DSOME_COPT 42"],
copts = select({
# Strictly speaking Windows does not automatically equal MSVC, but does so in our integration tests
"@platforms//os:windows": ["/DSOME_COPT 42"],
"//conditions:default": ["-DSOME_COPT 42"],
}),
defines = ["SOME_DEFINE"],
local_defines = ["LOCAL_DEFINE"],
deps = ["//defines/support:lib_a"],
Expand Down

0 comments on commit a60f597

Please sign in to comment.