Converts wrapped_clang from a bash script to a C++ source file which is compiled as part of bazel's repository bootstrap. This should make crosstool's clang invocations faster. An added benefit of this is that wrapped_clang.cc supports the "DSYM_HINT" flags specified through the CROSSTOOL, so with this change, apple_binary gets support for the --apple_generate_dsym flag.

The dSYM generation issue has been flagged multiple times:
https://github.com/bazelbuild/bazel/issues/4312
https://github.com/bazelbuild/bazel/issues/3940
https://github.com/bazelbuild/bazel/issues/3372

RELNOTES: apple_binary can now generate dSYM outputs with the --apple_generate_dsym=true flag.
PiperOrigin-RevId: 184688215
diff --git a/tools/cpp/osx_cc_configure.bzl b/tools/cpp/osx_cc_configure.bzl
index 1c75c6e..8dd837b 100644
--- a/tools/cpp/osx_cc_configure.bzl
+++ b/tools/cpp/osx_cc_configure.bzl
@@ -73,17 +73,40 @@
         Label("@bazel_tools//tools/osx/crosstool:wrapped_ar.tpl"),
         "wrapped_ar")
     repository_ctx.symlink(
-        Label("@bazel_tools//tools/osx/crosstool:wrapped_clang.tpl"),
-        "wrapped_clang")
-    repository_ctx.symlink(
-        Label("@bazel_tools//tools/osx/crosstool:wrapped_clang_pp.tpl"),
-        "wrapped_clang_pp")
-    repository_ctx.symlink(
         Label("@bazel_tools//tools/osx/crosstool:BUILD.tpl"),
         "BUILD")
     repository_ctx.symlink(
         Label("@bazel_tools//tools/osx/crosstool:osx_archs.bzl"),
         "osx_archs.bzl")
+
+    wrapped_clang_src_path = str(repository_ctx.path(
+        Label("@bazel_tools//tools/osx/crosstool:wrapped_clang.cc")))
+    xcrun_result = repository_ctx.execute(["env", "-i", "xcrun", "clang", "-std=c++11", "-lc++",
+                                           "-o", "wrapped_clang", wrapped_clang_src_path], 30)
+    if (xcrun_result.return_code == 0):
+      repository_ctx.symlink("wrapped_clang", "wrapped_clang_pp")
+    else:
+      # If for some reason wrapped_clang couldn't be built, fall back to
+      # using the bash scripts that don't support dSYM generation. This is to
+      # avoid completely breaking a build. This should be removed after a whole
+      # release cycle to keep from increasing code maintenance, if we haven't
+      # received any issues as requested below.
+      error_msg = (
+          "return code {code}, stderr: {err}, stdout: {out}").format(
+              code=xcrun_result.return_code,
+              err=xcrun_result.stderr,
+              out=xcrun_result.stdout)
+      print("wrapped_clang failed to generate. This shouldn't cause " +
+            "problems, but please file an issue at " +
+            "https://github.com/bazelbuild/bazel/issues with the following:\n" +
+            error_msg)
+      repository_ctx.symlink(
+          Label("@bazel_tools//tools/osx/crosstool:wrapped_clang.tpl"),
+          "wrapped_clang")
+      repository_ctx.symlink(
+          Label("@bazel_tools//tools/osx/crosstool:wrapped_clang_pp.tpl"),
+          "wrapped_clang_pp")
+
     escaped_include_paths = _get_escaped_xcode_cxx_inc_directories(repository_ctx, cc, xcode_toolchains)
     escaped_cxx_include_directories = []
     for path in escaped_include_paths: