macos_bundle: Make "apple_product_type.kernel_extension" produce a valid kext.

This is the rollback of commit 20bfdc67dc1fc32ffebbda7088ba49ee17e3e182, which in turn rolled back the original commit 19a401c38e30ebc0879925a5caedcbe43de0028f.

This was originally rolled back because breaks external go_rules (https://github.com/bazelbuild/bazel/issues/6283)

This is now safe as rules_go has been updated and released.
Full downstream test run: https://buildkite.com/bazel/bazel-with-downstream-projects-bazel/builds/551

PiperOrigin-RevId: 219678937
diff --git a/tools/osx/crosstool/CROSSTOOL.tpl b/tools/osx/crosstool/CROSSTOOL.tpl
index eacd267..6c617b1 100644
--- a/tools/osx/crosstool/CROSSTOOL.tpl
+++ b/tools/osx/crosstool/CROSSTOOL.tpl
@@ -70,7 +70,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   objcopy_embed_flag: "-I"
   objcopy_embed_flag: "binary"
@@ -121,6 +120,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -256,6 +272,21 @@
     }
   }
   feature {
+    name: "kernel_extension"
+    flag_set {
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-nostdlib"
+        flag: "-lkmod"
+        flag: "-lkmodc++"
+        flag: "-lcc_kext"
+        flag: "-Xlinker"
+        flag: "-kext"
+      }
+    }
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -1024,6 +1055,9 @@
         flag: "-framework"
         flag: "Foundation"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
     }
   }
   feature {
@@ -1401,10 +1435,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "x86_64"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -1416,6 +1446,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "x86_64"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"
@@ -1708,7 +1747,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   linker_flag: "-target"
   linker_flag: "x86_64-apple-ios"
@@ -1758,6 +1796,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -1902,6 +1957,9 @@
     }
   }
   feature {
+    name: "kernel_extension"
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -3054,10 +3112,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "x86_64"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -3069,6 +3123,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "x86_64"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"
@@ -3361,7 +3424,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   linker_flag: "-target"
   linker_flag: "i386-apple-watchos"
@@ -3411,6 +3473,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -3555,6 +3634,9 @@
     }
   }
   feature {
+    name: "kernel_extension"
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -4709,10 +4791,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "i386"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -4724,6 +4802,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "i386"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"
@@ -5015,7 +5102,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   objcopy_embed_flag: "-I"
   objcopy_embed_flag: "binary"
@@ -5061,6 +5147,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -5205,6 +5308,9 @@
     }
   }
   feature {
+    name: "kernel_extension"
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -6359,10 +6465,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "x86_64"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -6374,6 +6476,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "x86_64"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"
@@ -6665,7 +6776,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   objcopy_embed_flag: "-I"
   objcopy_embed_flag: "binary"
@@ -6711,6 +6821,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -6855,6 +6982,9 @@
     }
   }
   feature {
+    name: "kernel_extension"
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -7997,10 +8127,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "<architecture>"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -8012,6 +8138,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "<architecture>"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"
@@ -8304,7 +8439,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   linker_flag: "-target"
   linker_flag: "x86_64-apple-tvos"
@@ -8355,6 +8489,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -8499,6 +8650,9 @@
     }
   }
   feature {
+    name: "kernel_extension"
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -9678,10 +9832,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "x86_64"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -9693,6 +9843,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "x86_64"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"
@@ -9988,7 +10147,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   linker_flag: "-target"
   linker_flag: "i386-apple-ios"
@@ -10038,6 +10196,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -10182,6 +10357,9 @@
     }
   }
   feature {
+    name: "kernel_extension"
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -11334,10 +11512,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "i386"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -11349,6 +11523,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "i386"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"
@@ -11641,7 +11824,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   linker_flag: "-target"
   linker_flag: "armv7-apple-ios"
@@ -11691,6 +11873,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -11835,6 +12034,9 @@
     }
   }
   feature {
+    name: "kernel_extension"
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -12975,10 +13177,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "armv7"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -12990,6 +13188,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "armv7"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"
@@ -13282,7 +13489,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   linker_flag: "-target"
   linker_flag: "armv7-apple-watchos"
@@ -13332,6 +13538,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -13476,6 +13699,9 @@
     }
   }
   feature {
+    name: "kernel_extension"
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -14618,10 +14844,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "armv7k"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -14633,6 +14855,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "armv7k"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"
@@ -14925,7 +15156,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   linker_flag: "-target"
   linker_flag: "arm64-apple-tvos"
@@ -14976,6 +15206,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -15120,6 +15367,9 @@
     }
   }
   feature {
+    name: "kernel_extension"
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -16287,10 +16537,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "arm64"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -16302,6 +16548,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "arm64"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"
@@ -16597,7 +16852,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   linker_flag: "-target"
   linker_flag: "arm64-apple-ios"
@@ -16647,6 +16901,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -16791,6 +17062,9 @@
     }
   }
   feature {
+    name: "kernel_extension"
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -17931,10 +18205,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "arm64"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -17946,6 +18216,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "arm64"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"
@@ -18239,7 +18518,6 @@
   compiler_flag: "-fno-omit-frame-pointer"
   cxx_flag: "-std=c++11"
   linker_flag: "-headerpad_max_install_names"
-  linker_flag: "-lc++"
   linker_flag: "-no-canonical-prefixes"
   objcopy_embed_flag: "-I"
   objcopy_embed_flag: "binary"
@@ -18289,6 +18567,23 @@
     name: "dbg"
   }
   feature {
+    name: "link_libc++"
+    flag_set {
+      action: "c++-link-executable"
+      action: "c++-link-dynamic-library"
+      action: "c++-link-nodeps-dynamic-library"
+      action: "objc-executable"
+      action: "objc++-executable"
+      flag_group {
+        flag: "-lc++"
+      }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    enabled: true
+  }
+  feature {
     name: "compile_all_modules"
   }
   feature {
@@ -18433,6 +18728,9 @@
     }
   }
   feature {
+    name: "kernel_extension"
+  }
+  feature {
     name: "linkstamps"
     flag_set {
       action: "c++-link-executable"
@@ -19573,10 +19871,6 @@
     }
     flag_set {
       flag_group {
-        flag: "-arch"
-        flag: "<architecture>"
-      }
-      flag_group {
         flag: "-Xlinker"
         flag: "-objc_abi_version"
         flag: "-Xlinker"
@@ -19588,6 +19882,15 @@
         flag: "-fobjc-link-runtime"
         flag: "-ObjC"
       }
+      with_feature {
+        not_feature: "kernel_extension"
+      }
+    }
+    flag_set {
+      flag_group {
+        flag: "-arch"
+        flag: "<architecture>"
+      }
       flag_group {
         flag: "-framework"
         flag: "%{framework_names}"