[7.2.0] Add layering_check support for macOS (#22572)
There were 2 things with the previous implementation that needed to be
improved here:
1. Apple Clang has a bug where it doesn't pass module compiler flags to
the underlying -cc1 invocation, so we have to manually pass them
directly to that invocation with -Xclang
2. The previous search script was too aggressive and slow for macOS. The
macOS SDK has tons of files that aren't headers, and tons of symlinks
pointing to other files within the SDK. This adds a fork in the script
to run a version that works with Apple SDKs. The time difference on my
machine is 41s->6s. 6s is still pretty long so if desired we can put
this behavior behind an env var for users to opt in with.
I've added a hermetic version of this to the apple_support toolchain,
but similar to the Linux setup here the modulemap file includes absolute
paths.
Closes #22259.
This reverts commit 1f1b4fd37bacf5fc90bd06403b63dbb54e84db3b.
Closes #22475.
PiperOrigin-RevId: 637969838
Change-Id: I7d4940a820e3741836239493222ba8d06c4d70e4
Commit
https://github.com/bazelbuild/bazel/commit/6abdd2a882e31095fdd4eb557f755040188562a9
Co-authored-by: Keith Smiley <keithbsmiley@gmail.com>
diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml
index 78e9995..cd43f97 100644
--- a/.bazelci/presubmit.yml
+++ b/.bazelci/presubmit.yml
@@ -215,7 +215,6 @@
# To run any of the following test in presubmit, just comment out the corresponding line.
# TODO(pcloudy): Disable the android tests after enabling them on Apple Silicon platform.
- "-//src/test/shell/bazel:bazel_bootstrap_distfile_test"
- - "-//src/test/shell/bazel:bazel_bootstrap_distfile_tar_test"
# - "-//src/test/shell/bazel/android:android_integration_test"
# - "-//src/test/shell/bazel/android:android_integration_test_with_head_android_tools"
- "-//src/test/shell/bazel:bazel_proto_library_test"
diff --git a/MODULE.bazel b/MODULE.bazel
index 514c7af..5336c36 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -56,6 +56,14 @@
path = "./third_party/googleapis",
)
+single_version_override(
+ module_name = "grpc",
+ patch_strip = 1,
+ patches = [
+ "//third_party/grpc:00_disable_layering_check.patch",
+ ],
+)
+
# The following Bazel modules are not direct dependencies for building Bazel,
# but are required for visibility from DIST_ARCHIVE_REPOS in repositories.bzl
bazel_dep(name = "apple_support", version = "1.8.1")
diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock
index 26fec2a..b4c5a31 100644
--- a/MODULE.bazel.lock
+++ b/MODULE.bazel.lock
@@ -1,6 +1,6 @@
{
"lockFileVersion": 6,
- "moduleFileHash": "e6dc6d8e38926c6071c69d5c9d37de5491100ec7a1246a774e7a7bb4035b064f",
+ "moduleFileHash": "ebb77f16d8f0a7ce4dfb1163ac6552073681a6fd506bd703e11bd435fa5badf5",
"flags": {
"cmdRegistries": [
"https://bcr.bazel.build/"
@@ -39,7 +39,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 72,
+ "line": 80,
"column": 22
},
"imports": {
@@ -170,7 +170,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 73,
+ "line": 81,
"column": 14
}
},
@@ -185,7 +185,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 199,
+ "line": 207,
"column": 19
}
},
@@ -200,7 +200,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 199,
+ "line": 207,
"column": 19
}
},
@@ -215,7 +215,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 199,
+ "line": 207,
"column": 19
}
},
@@ -230,7 +230,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 199,
+ "line": 207,
"column": 19
}
},
@@ -245,7 +245,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 199,
+ "line": 207,
"column": 19
}
},
@@ -260,7 +260,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 199,
+ "line": 207,
"column": 19
}
},
@@ -275,7 +275,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 199,
+ "line": 207,
"column": 19
}
},
@@ -290,7 +290,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 199,
+ "line": 207,
"column": 19
}
},
@@ -305,7 +305,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 199,
+ "line": 207,
"column": 19
}
},
@@ -333,7 +333,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 339,
+ "line": 347,
"column": 22
}
}
@@ -347,7 +347,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 220,
+ "line": 228,
"column": 32
},
"imports": {
@@ -387,7 +387,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 254,
+ "line": 262,
"column": 23
},
"imports": {},
@@ -401,7 +401,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 255,
+ "line": 263,
"column": 17
}
}
@@ -415,7 +415,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 257,
+ "line": 265,
"column": 20
},
"imports": {
@@ -433,7 +433,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 258,
+ "line": 266,
"column": 10
}
}
@@ -447,7 +447,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 269,
+ "line": 277,
"column": 33
},
"imports": {
@@ -478,7 +478,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 290,
+ "line": 298,
"column": 29
},
"imports": {
@@ -495,7 +495,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 293,
+ "line": 301,
"column": 20
},
"imports": {
@@ -514,7 +514,7 @@
"devDependency": false,
"location": {
"file": "@@//:MODULE.bazel",
- "line": 294,
+ "line": 302,
"column": 12
}
}
@@ -528,7 +528,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 306,
+ "line": 314,
"column": 32
},
"imports": {
@@ -547,7 +547,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 314,
+ "line": 322,
"column": 31
},
"imports": {
@@ -564,7 +564,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 317,
+ "line": 325,
"column": 48
},
"imports": {
@@ -581,7 +581,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 361,
+ "line": 369,
"column": 35
},
"imports": {
@@ -598,7 +598,7 @@
"usingModule": "<root>",
"location": {
"file": "@@//:MODULE.bazel",
- "line": 364,
+ "line": 372,
"column": 42
},
"imports": {
@@ -858,7 +858,14 @@
"remote_patches": {
"https://bcr.bazel.build/modules/grpc/1.48.1.bcr.1/patches/adopt_bzlmod.patch": "sha256-iMrebRKNKLNqVtRX+4eRZ63QcBr2t8Zo/ZvBPjVnyw8="
},
- "remote_patch_strip": 1
+ "remote_patch_strip": 1,
+ "patches": [
+ "@@//third_party/grpc:00_disable_layering_check.patch"
+ ],
+ "patch_cmds": [],
+ "patch_args": [
+ "-p1"
+ ]
}
}
},
@@ -2918,7 +2925,7 @@
"general": {
"bzlTransitiveDigest": "kGK7vQSUnYTiX5+yCN3M8Mdy5PtT3d9zz8C7MpetGCk=",
"recordedFileInputs": {
- "@@//MODULE.bazel": "e6dc6d8e38926c6071c69d5c9d37de5491100ec7a1246a774e7a7bb4035b064f",
+ "@@//MODULE.bazel": "ebb77f16d8f0a7ce4dfb1163ac6552073681a6fd506bd703e11bd435fa5badf5",
"@@//src/test/tools/bzlmod/MODULE.bazel.lock": "25e5660da3f92a92d560a450ef453fb64629795bf24613286c52f2da529adfd1"
},
"recordedDirentsInputs": {},
diff --git a/src/test/shell/bazel/bazel_layering_check_test.sh b/src/test/shell/bazel/bazel_layering_check_test.sh
index 4515493..341f821 100755
--- a/src/test/shell/bazel/bazel_layering_check_test.sh
+++ b/src/test/shell/bazel/bazel_layering_check_test.sh
@@ -144,11 +144,6 @@
}
function test_bazel_layering_check() {
- if is_darwin; then
- echo "This test doesn't run on Darwin. Skipping."
- return
- fi
-
local -r clang_tool=$(which clang)
if [[ ! -x ${clang_tool:-/usr/bin/clang_tool} ]]; then
echo "clang not installed. Skipping test."
diff --git a/tools/cpp/generate_system_module_map.sh b/tools/cpp/generate_system_module_map.sh
index abbaec2..deb52c2 100755
--- a/tools/cpp/generate_system_module_map.sh
+++ b/tools/cpp/generate_system_module_map.sh
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
# Copyright 2020 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,10 +17,19 @@
echo 'module "crosstool" [system] {'
-for dir in $@; do
- find -L "${dir}" -type f 2>/dev/null | LANG=C sort | uniq | while read header; do
- echo " textual header \"${header}\""
+if [[ "$OSTYPE" == darwin* ]]; then
+ for dir in $@; do
+ find "$dir" -type f \( -name "*.h" -o -name "*.def" -o -path "*/c++/*" \) \
+ | LANG=C sort -u | while read -r header; do
+ echo " textual header \"${header}\""
+ done
done
-done
+else
+ for dir in $@; do
+ find -L "${dir}" -type f 2>/dev/null | LANG=C sort -u | while read -r header; do
+ echo " textual header \"${header}\""
+ done
+ done
+fi
echo "}"
diff --git a/tools/cpp/unix_cc_configure.bzl b/tools/cpp/unix_cc_configure.bzl
index d123a3d..2184487 100644
--- a/tools/cpp/unix_cc_configure.bzl
+++ b/tools/cpp/unix_cc_configure.bzl
@@ -534,7 +534,7 @@
["/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk"],
)
- generate_modulemap = is_clang and not darwin
+ generate_modulemap = is_clang
if generate_modulemap:
repository_ctx.file("module.modulemap", _generate_system_module_map(
repository_ctx,
diff --git a/tools/cpp/unix_cc_toolchain_config.bzl b/tools/cpp/unix_cc_toolchain_config.bzl
index b186d5a..1b1b021 100644
--- a/tools/cpp/unix_cc_toolchain_config.bzl
+++ b/tools/cpp/unix_cc_toolchain_config.bzl
@@ -36,7 +36,7 @@
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
return xcode_config.minimum_os_for_platform_type(platform_type)
-def layering_check_features(compiler):
+def layering_check_features(compiler, is_macos):
if compiler != "clang":
return []
return [
@@ -53,8 +53,10 @@
],
flag_groups = [
flag_group(
- flags = [
+ # macOS requires -Xclang because of a bug in Apple Clang
+ flags = (["-Xclang"] if is_macos else []) + [
"-fmodule-name=%{module_name}",
+ ] + (["-Xclang"] if is_macos else []) + [
"-fmodule-map-file=%{module_map_file}",
],
),
@@ -86,7 +88,7 @@
]),
flag_group(
iterate_over = "dependent_module_map_files",
- flags = [
+ flags = (["-Xclang"] if is_macos else []) + [
"-fmodule-map-file=%{dependent_module_map_files}",
],
),
@@ -1484,7 +1486,7 @@
unfiltered_compile_flags_feature,
treat_warnings_as_errors_feature,
archive_param_file_feature,
- ] + layering_check_features(ctx.attr.compiler)
+ ] + layering_check_features(ctx.attr.compiler, is_macos = False)
else:
# macOS artifact name patterns differ from the defaults only for dynamic
# libraries.
@@ -1525,7 +1527,7 @@
treat_warnings_as_errors_feature,
archive_param_file_feature,
generate_linkmap_feature,
- ]
+ ] + layering_check_features(ctx.attr.compiler, is_macos = True)
parse_headers_action_configs, parse_headers_features = parse_headers_support(
parse_headers_tool_path = ctx.attr.tool_paths.get("parse_headers"),