Allow the `enable_modules`, `module_map`, and `module_name` attributes of `objc_library` to be removed from the rule definition.

PiperOrigin-RevId: 808107939
Change-Id: I0fda7b4d4b967adc73fa53db218ea93b6f883e33
diff --git a/cc/private/rules_impl/objc_attrs.bzl b/cc/private/rules_impl/objc_attrs.bzl
index 0275b9c..d8a4b94 100644
--- a/cc/private/rules_impl/objc_attrs.bzl
+++ b/cc/private/rules_impl/objc_attrs.bzl
@@ -17,12 +17,13 @@
 load("//cc/common:cc_info.bzl", "CcInfo")
 load(":objc_semantics.bzl", "semantics")
 
-# buildifier: disable=unsorted-dict-items
-_COMPILING_RULE = {
-    "srcs": attr.label_list(
-        allow_files = True,
-        flags = ["DIRECT_COMPILE_TIME_INPUT"],
-        doc = """
+def _get_compiling_rule_attrs():
+    # buildifier: disable=unsorted-dict-items
+    attrs = {
+        "srcs": attr.label_list(
+            allow_files = True,
+            flags = ["DIRECT_COMPILE_TIME_INPUT"],
+            doc = """
 The list of C, C++, Objective-C, and Objective-C++ source and header
 files, and/or (`.s`, `.S`, or `.asm`) assembly source files, that are processed to create
 the library target.
@@ -34,20 +35,20 @@
 Additionally, precompiled .o files may be given as srcs.  Be careful to
 ensure consistency in the architecture of provided .o files and that of the
 build to avoid missing symbol linker errors.""",
-    ),
-    "non_arc_srcs": attr.label_list(
-        allow_files = True,
-        flags = ["DIRECT_COMPILE_TIME_INPUT"],
-        doc = """
+        ),
+        "non_arc_srcs": attr.label_list(
+            allow_files = True,
+            flags = ["DIRECT_COMPILE_TIME_INPUT"],
+            doc = """
 The list of Objective-C files that are processed to create the
 library target that DO NOT use Automatic Reference Counting (ARC).
 The files in this attribute are treated very similar to those in the
 srcs attribute, but are compiled without ARC enabled.""",
-    ),
-    "pch": attr.label(
-        allow_single_file = [".pch"],
-        flags = ["DIRECT_COMPILE_TIME_INPUT"],
-        doc = """
+        ),
+        "pch": attr.label(
+            allow_single_file = [".pch"],
+            flags = ["DIRECT_COMPILE_TIME_INPUT"],
+            doc = """
 Header file to prepend to every source file being compiled (both arc
 and non-arc).
 Use of pch files is actively discouraged in BUILD files, and this should be
@@ -55,31 +56,39 @@
 a build-speed enhancement, and instead is just a global dependency. From a build
 efficiency point of view you are actually better including what you need directly
 in your sources where you need it.""",
-    ),
-    "defines": attr.string_list(doc = """
+        ),
+        "defines": attr.string_list(doc = """
 Extra <code>-D</code> flags to pass to the compiler. They should be in
 the form <code>KEY=VALUE</code> or simply <code>KEY</code> and are
 passed not only to the compiler for this target (as <code>copts</code>
 are) but also to all <code>objc_</code> dependers of this target.
 Subject to <a href="${link make-variables}">"Make variable"</a> substitution and
 <a href="${link common-definitions#sh-tokenization}">Bourne shell tokenization</a>."""),
-    "enable_modules": attr.bool(doc = """
+        "linkopts": attr.string_list(doc = "Extra flags to pass to the linker."),
+        # How many rules use this in the depot?
+        "stamp": attr.bool(),
+    }
+
+    if semantics.is_enable_modules_attr_allowed:
+        attrs["enable_modules"] = attr.bool(doc = """
 Enables clang module support (via -fmodules).
 Setting this to 1 will allow you to @import system headers and other targets:
 @import UIKit;
-@import path_to_package_target;"""),
-    "linkopts": attr.string_list(doc = "Extra flags to pass to the linker."),
-    "module_map": attr.label(allow_files = [".modulemap"], doc = """
+@import path_to_package_target;""")
+
+    if semantics.is_module_map_attr_allowed:
+        attrs["module_map"] = attr.label(allow_files = [".modulemap"], doc = """
 custom Clang module map for this target. Use of a custom module map is discouraged. Most
 users should use module maps generated by Bazel.
 If specified, Bazel will not generate a module map for this target, but will pass the
-provided module map to the compiler."""),
-    "module_name": attr.string(doc = """
+provided module map to the compiler.""")
+
+    if semantics.is_module_name_attr_allowed:
+        attrs["module_name"] = attr.string(doc = """
 Sets the module name for this target. By default the module name is the target path with
-all special symbols replaced by _, e.g. //foo/baz:bar can be imported as foo_baz_bar."""),
-    # How many rules use this in the depot?
-    "stamp": attr.bool(),
-}
+all special symbols replaced by _, e.g. //foo/baz:bar can be imported as foo_baz_bar.""")
+
+    return attrs
 
 # buildifier: disable=unsorted-dict-items
 _COMPILE_DEPENDENCY_RULE = {
@@ -214,7 +223,7 @@
 common_attrs = struct(
     union = _union,
     ALWAYSLINK_RULE = _ALWAYSLINK_RULE,
-    COMPILING_RULE = _COMPILING_RULE,
+    COMPILING_RULE = _get_compiling_rule_attrs(),
     COMPILE_DEPENDENCY_RULE = _COMPILE_DEPENDENCY_RULE,
     COPTS_RULE = _COPTS_RULE,
     LICENSES = semantics.get_licenses_attr(),
diff --git a/cc/private/rules_impl/objc_semantics.bzl b/cc/private/rules_impl/objc_semantics.bzl
index 2d7d481..3683c7c 100644
--- a/cc/private/rules_impl/objc_semantics.bzl
+++ b/cc/private/rules_impl/objc_semantics.bzl
@@ -44,4 +44,7 @@
     get_repo = _get_repo,
     get_licenses_attr = _get_licenses_attr,
     apple_crosstool_transition = None,
+    is_enable_modules_attr_allowed = True,
+    is_module_map_attr_allowed = True,
+    is_module_name_attr_allowed = True,
 )