Merge pull request #144 from aranguyen:main

PiperOrigin-RevId: 475495525
Change-Id: Ieef564f6ceaf7df20a495e32ef88dbbb13c14eb9
diff --git a/.bcr/metadata.template.json b/.bcr/metadata.template.json
new file mode 100644
index 0000000..2a9e313
--- /dev/null
+++ b/.bcr/metadata.template.json
@@ -0,0 +1,6 @@
+{
+  "homepage": "https://github.com/bazelbuild/rules_cc"
+  "maintainers": [],
+  "versions": [],
+  "yanked_versions": {}
+}
diff --git a/.bcr/presubmit.yml b/.bcr/presubmit.yml
new file mode 100644
index 0000000..f01c9bf
--- /dev/null
+++ b/.bcr/presubmit.yml
@@ -0,0 +1,9 @@
+matrix:
+  platform: ["centos7", "debian10", "macos", "ubuntu2004", "windows"]
+tasks:
+  verify_targets:
+    name: "Verify build targets"
+    platform: ${{ platform }}
+    build_targets:
+      - "@rules_cc//cc/..."
+      - "@rules_cc//tools/runfiles"
diff --git a/.bcr/source.template.json b/.bcr/source.template.json
new file mode 100644
index 0000000..4f14819
--- /dev/null
+++ b/.bcr/source.template.json
@@ -0,0 +1,5 @@
+{
+  "integrity": "",
+  "strip_prefix": "{REPO}-{VERSION}",
+  "url": "https://github.com/{OWNER}/{REPO}/archive/refs/tags/{TAG}.tar.gz"
+}
diff --git a/CODEOWNERS b/CODEOWNERS
index a9dcf6d..85a388b 100644
--- a/CODEOWNERS
+++ b/CODEOWNERS
@@ -1 +1 @@
-* @oquenchil @c-mita @comius
+* @oquenchil @c-mita @comius @buildbreaker2021
diff --git a/MODULE.bazel b/MODULE.bazel
index 08b0c51..31b70f1 100644
--- a/MODULE.bazel
+++ b/MODULE.bazel
@@ -1,8 +1,7 @@
 module(
     name = "rules_cc",
     compatibility_level = 1,
-    toolchains_to_register = ["@local_config_cc_toolchains//:all"],
-    version = "0.0.1",
+    version = "0.0.3",
 )
 
 bazel_dep(name = "bazel_skylib", version = "1.0.3")
@@ -11,3 +10,5 @@
 cc_configure = use_extension("@rules_cc//cc:extensions.bzl", "cc_configure")
 
 use_repo(cc_configure, "local_config_cc_toolchains")
+
+register_toolchains("@local_config_cc_toolchains//:all")
diff --git a/cc/defs.bzl b/cc/defs.bzl
index e49c32b..17140c7 100644
--- a/cc/defs.bzl
+++ b/cc/defs.bzl
@@ -16,6 +16,7 @@
 
 load("//cc/private/rules_impl:cc_flags_supplier.bzl", _cc_flags_supplier = "cc_flags_supplier")
 load("//cc/private/rules_impl:compiler_flag.bzl", _compiler_flag = "compiler_flag")
+load("//cc/private/rules_impl:native.bzl", "NativeCcInfo", "NativeCcToolchainConfigInfo", "NativeDebugPackageInfo", "native_cc_common")
 
 _MIGRATION_TAG = "__CC_RULES_MIGRATION_DO_NOT_USE_WILL_BREAK__"
 
@@ -173,3 +174,11 @@
       **attrs: Rule attributes
     """
     _compiler_flag(**_add_tags(attrs))
+
+cc_common = native_cc_common
+
+CcInfo = NativeCcInfo
+
+CcToolchainConfigInfo = NativeCcToolchainConfigInfo
+
+DebugPackageInfo = NativeDebugPackageInfo
diff --git a/cc/find_cc_toolchain.bzl b/cc/find_cc_toolchain.bzl
index 05a6952..d2f2d9f 100644
--- a/cc/find_cc_toolchain.bzl
+++ b/cc/find_cc_toolchain.bzl
@@ -71,6 +71,10 @@
         if not CC_TOOLCHAIN_TYPE in ctx.toolchains:
             fail("In order to use find_cc_toolchain, your rule has to depend on C++ toolchain. See find_cc_toolchain.bzl docs for details.")
         toolchain_info = ctx.toolchains[CC_TOOLCHAIN_TYPE]
+        if toolchain_info == None:
+            # No cpp toolchain was found, so report an error.
+            fail("Unable to find a CC toolchain using toolchain resolution. Target: %s, Platform: %s, Exec platform: %s" %
+                 (ctx.label, ctx.fragments.platform.platform, ctx.fragments.platform.host_platform))
         if hasattr(toolchain_info, "cc_provider_in_toolchain") and hasattr(toolchain_info, "cc"):
             return toolchain_info.cc
         return toolchain_info
@@ -93,8 +97,7 @@
     """
     return find_cc_toolchain(ctx)
 
-# buildifier: disable=unused-variable
-def use_cc_toolchain(mandatory = True):
+def use_cc_toolchain(mandatory = False):
     """
     Helper to depend on the cc toolchain.
 
@@ -107,9 +110,8 @@
 
     Args:
       mandatory: Whether or not it should be an error if the toolchain cannot be resolved.
-        Currently ignored, this will be enabled when optional toolchain types are added.
 
     Returns:
       A list that can be used as the value for `rule.toolchains`.
     """
-    return [CC_TOOLCHAIN_TYPE]
+    return [config_common.toolchain_type(CC_TOOLCHAIN_TYPE, mandatory = mandatory)]
diff --git a/cc/private/rules_impl/BUILD b/cc/private/rules_impl/BUILD
index dc74dfe..3d79d9a 100644
--- a/cc/private/rules_impl/BUILD
+++ b/cc/private/rules_impl/BUILD
@@ -1,3 +1,5 @@
+load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
+
 package(default_visibility = ["//visibility:public"])
 
 licenses(["notice"])  # Apache 2.0
@@ -16,3 +18,9 @@
         "**/BUILD",
     ]),
 )
+
+bzl_library(
+    name = "native_bzl",
+    srcs = ["native.bzl"],
+    visibility = ["//cc:__pkg__"],
+)
diff --git a/cc/private/rules_impl/compiler_flag.bzl b/cc/private/rules_impl/compiler_flag.bzl
index 35674b2..ebbac94 100644
--- a/cc/private/rules_impl/compiler_flag.bzl
+++ b/cc/private/rules_impl/compiler_flag.bzl
@@ -14,7 +14,7 @@
 
 """Rule that allows select() to differentiate between compilers."""
 
-load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain")
+load("@bazel_tools//tools/cpp:toolchain_utils.bzl", "find_cpp_toolchain", "use_cpp_toolchain")
 
 def _compiler_flag_impl(ctx):
     toolchain = find_cpp_toolchain(ctx)
@@ -25,7 +25,5 @@
     attrs = {
         "_cc_toolchain": attr.label(default = Label("@bazel_tools//tools/cpp:current_cc_toolchain")),
     },
-    toolchains = [
-        "@bazel_tools//tools/cpp:toolchain_type",  # copybara-use-repo-external-label
-    ],
+    toolchains = use_cpp_toolchain(),
 )
diff --git a/cc/private/rules_impl/native.bzl b/cc/private/rules_impl/native.bzl
new file mode 100644
index 0000000..cce8c7f
--- /dev/null
+++ b/cc/private/rules_impl/native.bzl
@@ -0,0 +1,34 @@
+# Copyright 2022 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Redefine native symbols with a new name as a workaround for
+# exporting them in `//third_party/bazel_rules/rules_proto/proto:defs.bzl` with their original name.
+#
+# While we cannot force users to load these symbol due to the lack of a
+# allowlisting mechanism, we can still export them and tell users to
+# load it to make a future migration to pure Starlark easier.
+
+"""Lovely workaround to be able to expose native constants pretending to be Starlark."""
+
+# buildifier: disable=native-cc
+NativeCcInfo = CcInfo
+
+# buildifier: disable=native-cc
+NativeDebugPackageInfo = DebugPackageInfo
+
+# buildifier: disable=native-cc
+NativeCcToolchainConfigInfo = CcToolchainConfigInfo
+
+# buildifier: disable=native-cc
+native_cc_common = cc_common
diff --git a/cc/private/toolchain/unix_cc_configure.bzl b/cc/private/toolchain/unix_cc_configure.bzl
index a9c1cd4..0c936de 100644
--- a/cc/private/toolchain/unix_cc_configure.bzl
+++ b/cc/private/toolchain/unix_cc_configure.bzl
@@ -382,10 +382,8 @@
         False,
     ), ":")
 
-    bazel_linkopts = "-lstdc++:-lm"
-    bazel_linklibs = ""
-    if hasattr(repository_ctx, "flag_enabled") and repository_ctx.flag_enabled("incompatible_linkopts_to_linklibs"):
-        bazel_linkopts, bazel_linklibs = bazel_linklibs, bazel_linkopts
+    bazel_linklibs = "-lstdc++:-lm"
+    bazel_linkopts = ""
     link_opts = split_escaped(get_env_var(
         repository_ctx,
         "BAZEL_LINKOPTS",
diff --git a/tools/migration/BUILD b/tools/migration/BUILD
index 91c5d54..b1dfafb 100644
--- a/tools/migration/BUILD
+++ b/tools/migration/BUILD
@@ -22,7 +22,7 @@
 
 package(default_visibility = ["//visibility:public"])
 
-licenses(["notice"])  # Apache 2.0
+licenses(["notice"])
 
 py_binary(
     name = "legacy_fields_migrator",