chandlerc updated this revision to Diff 383634.
chandlerc marked 5 inline comments as done.
chandlerc edited the summary of this revision.
chandlerc added a reviewer: rnk.
chandlerc added a comment.

Major update to better fix some of the issues here. No longer requires any 
changes outside of Bazel.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D112399/new/

https://reviews.llvm.org/D112399

Files:
  utils/bazel/.bazelrc
  utils/bazel/llvm-project-overlay/clang/BUILD.bazel
  utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h
  utils/bazel/llvm-project-overlay/clang/unittests/BUILD.bazel
  utils/bazel/llvm-project-overlay/llvm/cc_plugin_library.bzl
  utils/bazel/llvm-project-overlay/llvm/config.bzl
  utils/bazel/llvm-project-overlay/llvm/include/llvm/Config/config.h

Index: utils/bazel/llvm-project-overlay/llvm/include/llvm/Config/config.h
===================================================================
--- utils/bazel/llvm-project-overlay/llvm/include/llvm/Config/config.h
+++ utils/bazel/llvm-project-overlay/llvm/include/llvm/Config/config.h
@@ -352,10 +352,10 @@
 #define HAVE_STD_IS_TRIVIALLY_COPYABLE 1
 
 /* Define to a function implementing stricmp */
-/* stricmp defined in Bazel */
+/* stricmp defined conditionally below. */
 
 /* Define to a function implementing strdup */
-/* strdup defined in Bazel */
+/* strdup defined conditionally below. */
 
 /* Whether GlobalISel rule coverage is being collected */
 #define LLVM_GISEL_COV_ENABLED 0
@@ -368,4 +368,17 @@
 
 /* HAVE_PROC_PID_RUSAGE defined in Bazel */
 
+/* Directly provide definitions here behind platform preprocessor definitions.
+ * The preprocessor conditions are sufficient to handle all of the configuration
+ * on platforms targeted by Bazel, and defining these here more faithfully
+ * matches how the users of this header expect things to work with CMake.
+ * FIXME: We should consider moving other platform defines to use this technique
+ * as well.
+ */
+
+#ifdef _WIN32
+#define stricmp _stricmp
+#define strdup _strdup
+#endif
+
 #endif
Index: utils/bazel/llvm-project-overlay/llvm/config.bzl
===================================================================
--- utils/bazel/llvm-project-overlay/llvm/config.bzl
+++ utils/bazel/llvm-project-overlay/llvm/config.bzl
@@ -57,9 +57,15 @@
 ]
 
 win32_defines = [
-    # MSVC specific
-    "stricmp=_stricmp",
-    "strdup=_strdup",
+    # Windows system library specific defines.
+    "_CRT_SECURE_NO_DEPRECATE",
+    "_CRT_SECURE_NO_WARNINGS",
+    "_CRT_NONSTDC_NO_DEPRECATE",
+    "_CRT_NONSTDC_NO_WARNINGS",
+    "_SCL_SECURE_NO_DEPRECATE",
+    "_SCL_SECURE_NO_WARNINGS",
+    "UNICODE",
+    "_UNICODE",
 
     # LLVM features
     r'LTDL_SHLIB_EXT=\".dll\"',
Index: utils/bazel/llvm-project-overlay/llvm/cc_plugin_library.bzl
===================================================================
--- utils/bazel/llvm-project-overlay/llvm/cc_plugin_library.bzl
+++ utils/bazel/llvm-project-overlay/llvm/cc_plugin_library.bzl
@@ -4,51 +4,72 @@
 
 """A macro to produce a loadable plugin library for the target OS.
 
-This macro produces a `cc_binary` rule with the name `name + "_impl"`. It
-forces the rule to statically link in its dependencies but to be linked as a
-shared "plugin" library. It then creates binary aliases to `.so`, `.dylib`
-,and `.dll` suffixed names for use on various platforms and selects between
-these into a filegroup with the exact name passed to the macro.
+This macro produces a set of platform-specific `cc_binary` rules, by appending
+the platform suffix (`.dll`, `.dylib`, or `.so`) to the provided `name`. It then
+connects these to a `cc_import` rule with `name` exactly and `hdrs` that can be
+used by other Bazel rules to depend on the plugin library.
+
+The `srcs` attribute for the `cc_binary` rules is `srcs + hdrs`. Other explicit
+arguments are passed to all of the rules where they apply, and can be used to
+configure generic aspects of all generated rules such as `testonly`. Lastly,
+`kwargs` is expanded into all the `cc_binary` rules.
 """
 
-load("@rules_cc//cc:defs.bzl", "cc_binary")
-load(":binary_alias.bzl", "binary_alias")
+load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_import", "cc_library")
 
-def cc_plugin_library(name, **kwargs):
+def cc_plugin_library(name, srcs, hdrs, include_prefix = None, strip_include_prefix = None, alwayslink = False, features = [], tags = [], testonly = False, **kwargs):
     # Neither the name of the plugin binary nor tags on whether it is built are
-    # configurable. Instead, we build a `cc_binary` that implements the plugin
-    # library using a `_impl` suffix. Bazel will use appropriate flags to cause
-    # this file to be a plugin library regardless of its name. We then create
-    # binary aliases in the different possible platform names, and select
-    # between these different names into a filegroup. The macro's name becomes
-    # the filegroup name and it contains exactly one target that is the target
-    # platform suffixed plugin library.
+    # configurable. Instead, we build a `cc_binary` with each name and
+    # selectively depend on them based on platform.
     #
     # All-in-all, this is a pretty poor workaround. I think this is part of the
     # Bazel issue: https://github.com/bazelbuild/bazel/issues/7538
-    cc_binary(
-        name = name + "_impl",
-        linkshared = True,
-        linkstatic = True,
-        **kwargs
-    )
-    binary_alias(
-        name = name + ".so",
-        binary = ":" + name + "_impl",
-    )
-    binary_alias(
-        name = name + ".dll",
-        binary = ":" + name + "_impl",
-    )
-    binary_alias(
-        name = name + ".dylib",
-        binary = ":" + name + "_impl",
-    )
+    so_name = name + ".so"
+    dll_name = name + ".dll"
+    dylib_name = name + ".dylib"
+    interface_output_name = name + "_interface_output"
+    import_name = name + "_import"
+    for impl_name in [dll_name, dylib_name, so_name]:
+        cc_binary(
+            name = impl_name,
+            srcs = srcs + hdrs,
+            linkshared = True,
+            linkstatic = True,
+            features = features,
+            tags = ["manual"] + tags,
+            testonly = testonly,
+            **kwargs
+        )
     native.filegroup(
-        name = name,
+        name = interface_output_name,
         srcs = select({
-            "@bazel_tools//src/conditions:windows": [":" + name + ".dll"],
-            "@bazel_tools//src/conditions:darwin": [":" + name + ".dylib"],
-            "//conditions:default": [":" + name + ".so"],
+            "@bazel_tools//src/conditions:windows": [":" + dll_name],
+            "@bazel_tools//src/conditions:darwin": [":" + dylib_name],
+            "//conditions:default": [":" + so_name],
+        }),
+        output_group = "interface_library",
+    )
+    cc_import(
+        name = import_name,
+        interface_library = ":" + interface_output_name,
+        shared_library = select({
+            "@bazel_tools//src/conditions:windows": ":" + dll_name,
+            "@bazel_tools//src/conditions:darwin": ":" + dylib_name,
+            "//conditions:default": ":" + so_name,
         }),
+        alwayslink = alwayslink,
+        features = features,
+        tags = tags,
+        testonly = testonly,
+    )
+    cc_library(
+        name = name,
+        hdrs = hdrs,
+        include_prefix = include_prefix,
+        strip_include_prefix = strip_include_prefix,
+        deps = [":" + import_name],
+        alwayslink = alwayslink,
+        features = features,
+        tags = tags,
+        testonly = testonly,
     )
Index: utils/bazel/llvm-project-overlay/clang/unittests/BUILD.bazel
===================================================================
--- utils/bazel/llvm-project-overlay/clang/unittests/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/clang/unittests/BUILD.bazel
@@ -480,8 +480,15 @@
     ) + [
         "libclang/TestUtils.h",
     ],
+    args = select({
+        "@bazel_tools//src/conditions:windows": [
+            # Need to disable the VFS tests that don't use Windows friendly paths.
+            "--gtest_filter=-*VirtualFileOverlay*",
+        ],
+        "//conditions:default": [],
+    }),
     deps = [
-        "//clang:c-bindings",
+        "//clang:libclang",
         "//llvm:Support",
         "//llvm:gtest",
         "//llvm:gtest_main",
Index: utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h
===================================================================
--- utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h
+++ utils/bazel/llvm-project-overlay/clang/include/clang/Config/config.h
@@ -75,7 +75,7 @@
 /* #undef CLANG_HAVE_LIBXML */
 
 /* Define if we have sys/resource.h (rlimits) */
-#define CLANG_HAVE_RLIMITS 1
+/* CLANG_HAVE_RLIMITS defined in Bazel */
 
 /* The LLVM product name and version */
 #define BACKEND_PACKAGE_STRING "LLVM 12.0.0git"
Index: utils/bazel/llvm-project-overlay/clang/BUILD.bazel
===================================================================
--- utils/bazel/llvm-project-overlay/clang/BUILD.bazel
+++ utils/bazel/llvm-project-overlay/clang/BUILD.bazel
@@ -357,6 +357,13 @@
         "include/clang/Basic/Version.inc",
         "include/clang/Config/config.h",
     ],
+    defines = select({
+        "@bazel_tools//src/conditions:windows": [],
+        "//conditions:default": [
+            # Clang-specific define on non-Windows platforms.
+            "CLANG_HAVE_RLIMITS=1",
+        ],
+    }),
     includes = ["include"],
     deps = [
         # We rely on the LLVM config library to provide configuration defines.
@@ -1313,6 +1320,10 @@
         # directly #including "Tools.h".
         "lib/Driver",
     ],
+    linkopts = select({
+        "@bazel_tools//src/conditions:windows": ["version.lib"],
+        "//conditions:default": [],
+    }),
     textual_hdrs = glob([
         "include/clang/Driver/*.def",
     ]),
@@ -1732,12 +1743,13 @@
 )
 
 cc_library(
-    name = "libclang_library",
+    name = "libclang_static",
     srcs = glob([
         "tools/libclang/*.cpp",
         "tools/libclang/*.h",
     ]),
     hdrs = glob(["include/clang-c/*.h"]),
+    defines = ["CINDEX_NO_EXPORTS"],
     deps = [
         ":arc_migrate",
         ":ast",
@@ -1758,18 +1770,36 @@
     ],
 )
 
-cc_library(
-    name = "c-bindings",
+cc_plugin_library(
+    name = "libclang",
+    srcs = glob([
+        "tools/libclang/*.cpp",
+        "tools/libclang/*.h",
+    ]),
     hdrs = glob(["include/clang-c/*.h"]),
+    copts = select({
+        "@bazel_tools//src/conditions:windows": ["-D_CINDEX_LIB_"],
+        "//conditions:default": [],
+    }),
+    strip_include_prefix = "include",
     deps = [
-        ":libclang_library",
+        ":arc_migrate",
+        ":ast",
+        ":basic",
+        ":codegen",
+        ":config",
+        ":driver",
+        ":frontend",
+        ":index",
+        ":lex",
+        ":rewrite",
+        ":sema",
+        ":tooling",
+        "//llvm:BitstreamReader",
+        "//llvm:FrontendOpenMP",
+        "//llvm:Support",
+        "//llvm:config",
     ],
-    alwayslink = 1,
-)
-
-cc_plugin_library(
-    name = "libclang",
-    deps = [":c-bindings"],
 )
 
 filegroup(
@@ -1795,21 +1825,20 @@
         "tools/c-index-test/c-index-test.c",
         "tools/c-index-test/core_main.cpp",
     ],
-    copts = [
-        "-Wno-uninitialized",
-    ],
+    copts = select({
+        "@bazel_tools//src/conditions:windows": [],
+        "//conditions:default": ["-Wno-uninitialized"],
+    }),
     stamp = 0,
     deps = [
         ":ast",
         ":basic",
-        ":c-bindings",
         ":codegen",
         ":config",
         ":frontend",
         ":index",
         ":lex",
-        ":parse",
-        ":sema",
+        ":libclang",
         ":serialization",
         "//llvm:Core",
         "//llvm:MC",
@@ -1837,11 +1866,14 @@
     name = "c-arcmt-test",
     testonly = 1,
     srcs = ["tools/c-arcmt-test/c-arcmt-test.c"],
-    copts = ["-std=gnu99"],
+    copts = select({
+        "@bazel_tools//src/conditions:windows": [],
+        "//conditions:default": ["-std=gnu99"],
+    }),
     stamp = 0,
     deps = [
-        ":c-bindings",
         ":codegen",
+        ":libclang",
         "//llvm:MC",
         "//llvm:Support",
     ],
Index: utils/bazel/.bazelrc
===================================================================
--- utils/bazel/.bazelrc
+++ utils/bazel/.bazelrc
@@ -72,8 +72,16 @@
 # Generic Windows flags common to both MSVC and Clang.
 ###############################################################################
 
-# Yay for security warnings. Boo for non-standard.
-build:windows --copt=/D_CRT_SECURE_NO_WARNINGS --host_copt=/D_CRT_SECURE_NO_WARNINGS
+# C++14 standard version is required.
+build:windows --cxxopt=/std:c++14 --host_cxxopt=/std:c++14
+
+# Other generic dialect flags.
+build:windows --copt=/Zc:strictStrings --host_copt=/Zc:strictStrings
+build:windows --copt=/Oi --host_copt=/Oi
+build:windows --cxxopt=/Zc:rvalueCast --host_cxxopt=/Zc:rvalueCast
+
+# Use the more flexible bigobj format for C++ files that have lots of symbols.
+build:windows --cxxopt=/bigobj --host_cxxopt=/bigobj
 
 ###############################################################################
 # Windows specific flags for building with MSVC.
@@ -107,9 +115,6 @@
 # Switch from MSVC to the `clang-cl` compiler.
 build:clang-cl --compiler=clang-cl
 
-# C++14 standard version is required.
-build:clang-cl --cxxopt=/std:c++14 --host_cxxopt=/std:c++14
-
 # Use Clang's internal warning flags instead of the ones that sometimes map
 # through to MSVC's flags.
 build:clang-cl --copt=/clang:-Wall --host_copt=/clang:-Wall
@@ -121,6 +126,10 @@
 # There appears to be an unused constant in GoogleTest on Windows.
 build:clang-cl --copt=/clang:-Wno-unused-const-variable --host_copt=/clang:-Wno-unused-const-variable
 
+# Disable some warnings hit even with `clang-cl` in Clang's own code.
+build:clang-cl --copt=/clang:-Wno-inconsistent-dllimport --host_copt=/clang:-Wno-inconsistent-dllimport
+build:clang-cl --cxxopt=/clang:-Wno-c++11-narrowing --host_cxxopt=/clang:-Wno-c++11-narrowing
+
 ###############################################################################
 
 ###############################################################################
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to