Repository: bazelbuild/bazel
Issue: Building for arm64_32 fails
Labels: type: bug
P2
z-team-Apple

### Description of the problem / feature request:
Building a watch application for arm64_32 results in clang failing with 
`clang: error: invalid arch name '-arch <architecture>'`

### Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
Build any `watchos_application` target for arm64_32

### What operating system are you running Bazel on?
Macos 10.14.6

### What's the output of `bazel info release`?
release 2.0.0
###  Have you found anything relevant by searching the web?
Might be related to #7329

Issue comments:
Setting -arch to `"<architecture>"` for watchos_arm64_32 certainly seems to be the wrong thing to do. Thank you for the PR, I'll ping some potential reviewers.

Pull request review comments:

Patch that resolved the issue:
diff --git a/tools/osx/crosstool/cc_toolchain_config.bzl b/tools/osx/crosstool/cc_toolchain_config.bzl
index 12027dadf49442..495edef6d270e2 100644
--- a/tools/osx/crosstool/cc_toolchain_config.bzl
+++ b/tools/osx/crosstool/cc_toolchain_config.bzl
@@ -337,8 +337,7 @@ def _impl(ctx):
     else:
         cpp_header_parsing_action = None
 
-    if (ctx.attr.cpu == "armeabi-v7a" or
-        ctx.attr.cpu == "watchos_arm64_32"):
+    if (ctx.attr.cpu == "armeabi-v7a"):
         objc_compile_action = action_config(
             action_name = ACTION_NAMES.objc_compile,
             flag_sets = [
@@ -370,6 +369,38 @@ def _impl(ctx):
                 ),
             ],
         )
+    elif (ctx.attr.cpu == "watchos_arm64_32"):
+        objc_compile_action = action_config(
+            action_name = ACTION_NAMES.objc_compile,
+            flag_sets = [
+                flag_set(
+                    flag_groups = [flag_group(flags = ["-arch", "arm64_32"])],
+                ),
+            ],
+            implies = [
+                "compiler_input_flags",
+                "compiler_output_flags",
+                "objc_actions",
+                "apply_default_compiler_flags",
+                "apply_default_warnings",
+                "framework_paths",
+                "preprocessor_defines",
+                "include_system_dirs",
+                "version_min",
+                "objc_arc",
+                "no_objc_arc",
+                "apple_env",
+                "user_compile_flags",
+                "sysroot",
+                "unfiltered_compile_flags",
+            ],
+            tools = [
+                tool(
+                    path = "wrapped_clang",
+                    execution_requirements = ["requires-darwin"],
+                ),
+            ],
+        )
     elif (ctx.attr.cpu == "ios_arm64" or
           ctx.attr.cpu == "tvos_arm64"):
         objc_compile_action = action_config(
@@ -603,8 +634,7 @@ def _impl(ctx):
     else:
         objc_compile_action = None
 
-    if (ctx.attr.cpu == "armeabi-v7a" or
-        ctx.attr.cpu == "watchos_arm64_32"):
+    if (ctx.attr.cpu == "armeabi-v7a"):
         objcpp_executable_action = action_config(
             action_name = "objc++-executable",
             flag_sets = [
@@ -666,6 +696,68 @@ def _impl(ctx):
                 ),
             ],
         )
+    elif (ctx.attr.cpu == "watchos_arm64_32"):
+        objcpp_executable_action = action_config(
+            action_name = "objc++-executable",
+            flag_sets = [
+                flag_set(
+                    flag_groups = [
+                        flag_group(flags = ["-stdlib=libc++", "-std=gnu++11"]),
+                        flag_group(flags = ["-arch", "arm64_32"]),
+                        flag_group(
+                            flags = [
+                                "-Xlinker",
+                                "-objc_abi_version",
+                                "-Xlinker",
+                                "2",
+                                "-fobjc-link-runtime",
+                                "-ObjC",
+                            ],
+                        ),
+                        flag_group(
+                            flags = ["-framework", "%{framework_names}"],
+                            iterate_over = "framework_names",
+                        ),
+                        flag_group(
+                            flags = ["-weak_framework", "%{weak_framework_names}"],
+                            iterate_over = "weak_framework_names",
+                        ),
+                        flag_group(
+                            flags = ["-l%{library_names}"],
+                            iterate_over = "library_names",
+                        ),
+                        flag_group(flags = ["-filelist", "%{filelist}"]),
+                        flag_group(flags = ["-o", "%{linked_binary}"]),
+                        flag_group(
+                            flags = ["-force_load", "%{force_load_exec_paths}"],
+                            iterate_over = "force_load_exec_paths",
+                        ),
+                        flag_group(
+                            flags = ["%{dep_linkopts}"],
+                            iterate_over = "dep_linkopts",
+                        ),
+                        flag_group(
+                            flags = ["-Wl,%{attr_linkopts}"],
+                            iterate_over = "attr_linkopts",
+                        ),
+                    ],
+                ),
+            ],
+            implies = [
+                "include_system_dirs",
+                "framework_paths",
+                "version_min",
+                "strip_debug_symbols",
+                "apple_env",
+                "apply_implicit_frameworks",
+            ],
+            tools = [
+                tool(
+                    path = "wrapped_clang_pp",
+                    execution_requirements = ["requires-darwin"],
+                ),
+            ],
+        )
     elif (ctx.attr.cpu == "ios_arm64" or
           ctx.attr.cpu == "tvos_arm64"):
         objcpp_executable_action = action_config(
@@ -1250,8 +1342,7 @@ def _impl(ctx):
     else:
         cpp_compile_action = None
 
-    if (ctx.attr.cpu == "armeabi-v7a" or
-        ctx.attr.cpu == "watchos_arm64_32"):
+    if (ctx.attr.cpu == "armeabi-v7a"):
         objcpp_compile_action = action_config(
             action_name = ACTION_NAMES.objcpp_compile,
             flag_sets = [
@@ -1291,6 +1382,46 @@ def _impl(ctx):
                 ),
             ],
         )
+    elif (ctx.attr.cpu == "watchos_arm64_32"):
+        objcpp_compile_action = action_config(
+            action_name = ACTION_NAMES.objcpp_compile,
+            flag_sets = [
+                flag_set(
+                    flag_groups = [
+                        flag_group(
+                            flags = [
+                                "-arch",
+                                "arm64_32",
+                                "-stdlib=libc++",
+                                "-std=gnu++11",
+                            ],
+                        ),
+                    ],
+                ),
+            ],
+            implies = [
+                "compiler_input_flags",
+                "compiler_output_flags",
+                "apply_default_compiler_flags",
+                "apply_default_warnings",
+                "framework_paths",
+                "preprocessor_defines",
+                "include_system_dirs",
+                "version_min",
+                "objc_arc",
+                "no_objc_arc",
+                "apple_env",
+                "user_compile_flags",
+                "sysroot",
+                "unfiltered_compile_flags",
+            ],
+            tools = [
+                tool(
+                    path = "wrapped_clang",
+                    execution_requirements = ["requires-darwin"],
+                ),
+            ],
+        )
     elif (ctx.attr.cpu == "ios_arm64" or
           ctx.attr.cpu == "tvos_arm64"):
         objcpp_compile_action = action_config(
@@ -1663,8 +1794,7 @@ def _impl(ctx):
     else:
         preprocess_assemble_action = None
 
-    if (ctx.attr.cpu == "armeabi-v7a" or
-        ctx.attr.cpu == "watchos_arm64_32"):
+    if (ctx.attr.cpu == "armeabi-v7a"):
         objc_archive_action = action_config(
             action_name = "objc-archive",
             flag_sets = [
@@ -1695,6 +1825,37 @@ def _impl(ctx):
                 ),
             ],
         )
+    elif (ctx.attr.cpu == "watchos_arm64_32"):
+        objc_archive_action = action_config(
+            action_name = "objc-archive",
+            flag_sets = [
+                flag_set(
+                    flag_groups = [
+                        flag_group(
+                            flags = [
+                                "-no_warning_for_no_symbols",
+                                "-static",
+                                "-filelist",
+                                "%{obj_list_path}",
+                                "-arch_only",
+                                "arm64_32",
+                                "-syslibroot",
+                                "%{sdk_dir}",
+                                "-o",
+                                "%{archive_path}",
+                            ],
+                        ),
+                    ],
+                ),
+            ],
+            implies = ["apple_env"],
+            tools = [
+                tool(
+                    path = "libtool",
+                    execution_requirements = ["requires-darwin"],
+                ),
+            ],
+        )
     elif (ctx.attr.cpu == "ios_arm64" or
           ctx.attr.cpu == "tvos_arm64"):
         objc_archive_action = action_config(
@@ -1889,8 +2050,7 @@ def _impl(ctx):
     else:
         objc_archive_action = None
 
-    if (ctx.attr.cpu == "armeabi-v7a" or
-        ctx.attr.cpu == "watchos_arm64_32"):
+    if (ctx.attr.cpu == "armeabi-v7a"):
         objc_executable_action = action_config(
             action_name = "objc-executable",
             flag_sets = [
@@ -1956,6 +2116,72 @@ def _impl(ctx):
                 ),
             ],
         )
+    elif (ctx.attr.cpu == "watchos_arm64_32"):
+        objc_executable_action = action_config(
+            action_name = "objc-executable",
+            flag_sets = [
+                flag_set(
+                    flag_groups = [
+                        flag_group(
+                            flags = [
+                                "-Xlinker",
+                                "-objc_abi_version",
+                                "-Xlinker",
+                                "2",
+                                "-fobjc-link-runtime",
+                                "-ObjC",
+                            ],
+                        ),
+                    ],
+                    with_features = [with_feature_set(not_features = ["kernel_extension"])],
+                ),
+                flag_set(
+                    flag_groups = [
+                        flag_group(flags = ["-arch", "arm64_32"]),
+                        flag_group(
+                            flags = ["-framework", "%{framework_names}"],
+                            iterate_over = "framework_names",
+                        ),
+                        flag_group(
+                            flags = ["-weak_framework", "%{weak_framework_names}"],
+                            iterate_over = "weak_framework_names",
+                        ),
+                        flag_group(
+                            flags = ["-l%{library_names}"],
+                            iterate_over = "library_names",
+                        ),
+                        flag_group(flags = ["-filelist", "%{filelist}"]),
+                        flag_group(flags = ["-o", "%{linked_binary}"]),
+                        flag_group(
+                            flags = ["-force_load", "%{force_load_exec_paths}"],
+                            iterate_over = "force_load_exec_paths",
+                        ),
+                        flag_group(
+                            flags = ["%{dep_linkopts}"],
+                            iterate_over = "dep_linkopts",
+                        ),
+                        flag_group(
+                            flags = ["-Wl,%{attr_linkopts}"],
+                            iterate_over = "attr_linkopts",
+                        ),
+                    ],
+                ),
+            ],
+            implies = [
+                "include_system_dirs",
+                "framework_paths",
+                "version_min",
+                "strip_debug_symbols",
+                "apple_env",
+                "apply_implicit_frameworks",
+            ],
+            tools = [
+                tool(
+                    path = "wrapped_clang",
+                    execution_requirements = ["requires-darwin"],
+                ),
+            ],
+        )
     elif (ctx.attr.cpu == "ios_arm64" or
           ctx.attr.cpu == "tvos_arm64"):
         objc_executable_action = action_config(
@@ -2572,8 +2798,7 @@ def _impl(ctx):
     else:
         cpp_link_nodeps_dynamic_library_action = None
 
-    if (ctx.attr.cpu == "armeabi-v7a" or
-        ctx.attr.cpu == "watchos_arm64_32"):
+    if (ctx.attr.cpu == "armeabi-v7a"):
         objc_fully_link_action = action_config(
             action_name = "objc-fully-link",
             flag_sets = [
@@ -2614,6 +2839,47 @@ def _impl(ctx):
                 ),
             ],
         )
+    elif (ctx.attr.cpu == "watchos_arm64_32"):
+        objc_fully_link_action = action_config(
+            action_name = "objc-fully-link",
+            flag_sets = [
+                flag_set(
+                    flag_groups = [
+                        flag_group(
+                            flags = [
+                                "-no_warning_for_no_symbols",
+                                "-static",
+                                "-arch_only",
+                                "arm64_32",
+                                "-syslibroot",
+                                "%{sdk_dir}",
+                                "-o",
+                                "%{fully_linked_archive_path}",
+                            ],
+                        ),
+                        flag_group(
+                            flags = ["%{objc_library_exec_paths}"],
+                            iterate_over = "objc_library_exec_paths",
+                        ),
+                        flag_group(
+                            flags = ["%{cc_library_exec_paths}"],
+                            iterate_over = "cc_library_exec_paths",
+                        ),
+                        flag_group(
+                            flags = ["%{imported_library_exec_paths}"],
+                            iterate_over = "imported_library_exec_paths",
+                        ),
+                    ],
+                ),
+            ],
+            implies = ["apple_env"],
+            tools = [
+                tool(
+                    path = "libtool",
+                    execution_requirements = ["requires-darwin"],
+                ),
+            ],
+        )
     elif (ctx.attr.cpu == "ios_arm64" or
           ctx.attr.cpu == "tvos_arm64"):
         objc_fully_link_action = action_config(


Test patch: