From a60f5974e0d9807805f91ca17d5b8b55634a27cb Mon Sep 17 00:00:00 2001 From: Martin Medler Date: Mon, 15 Jan 2024 21:43:00 +0100 Subject: [PATCH] Make aspect MSVC compatible --- src/aspect/dwyu.bzl | 7 +++++-- src/aspect/test/dwyu_test.bzl | 7 +++++++ test/aspect/defines/BUILD | 6 +++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/aspect/dwyu.bzl b/src/aspect/dwyu.bzl index 02c51560..9ad56245 100644 --- a/src/aspect/dwyu.bzl +++ b/src/aspect/dwyu.bzl @@ -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 diff --git a/src/aspect/test/dwyu_test.bzl b/src/aspect/test/dwyu_test.bzl index 885568bd..a2de10cf 100644 --- a/src/aspect/test/dwyu_test.bzl +++ b/src/aspect/test/dwyu_test.bzl @@ -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) diff --git a/test/aspect/defines/BUILD b/test/aspect/defines/BUILD index 92cff0aa..2a60ed62 100644 --- a/test/aspect/defines/BUILD +++ b/test/aspect/defines/BUILD @@ -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"],