bazel-contrib / rules_foreign_cc

Build rules for interfacing with "foreign" (non-Bazel) build systems (CMake, configure-make, GNU Make, boost, ninja, Meson)
https://bazel-contrib.github.io/rules_foreign_cc
Apache License 2.0
679 stars 249 forks source link

Default rules_foreign_cc_dependencies call fails - `no such package '@@rules_foreign_cc//toolchains'` #1232

Closed xgqt closed 4 months ago

xgqt commented 4 months ago

Hi!

I wanted to build a custom curl binary using rules_foreign_cc. The default setup fails for me on the toolchain registration step but in a very weird way - Bazel claims that @@rules_foreign_cc//toolchains does not exist.

If I do not register the TCs it passes but all required TCs are built by Bazel.

Issue

The default rules_foreign_cc_dependencies() call fails for me.

As a workaround I had to do:

rules_foreign_cc_dependencies(
    register_preinstalled_tools = False,
    register_toolchains = False,
)

Error

The whole error log is (when called bazel build "//vendor-curl"):

ERROR: /home/xy/real_tests/foreign/vendor-curl/BUILD.bazel:7:15: While resolving toolchains for target //vendor-curl:vendor_curl_foreign (5c86453): invalid registered toolchain '@rules_foreign_cc//toolchains:built_cmake_toolchain': no such package '@@rules_foreign_cc//toolchains': The repository '@@rules_foreign_cc' could not be resolved: Repository '@@rules_foreign_cc' is not defined
ERROR: Analysis of target '//vendor-curl:vendor-curl' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.163s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
FAILED: 
    Fetching repository @@local_config_sh; starting
    Fetching repository @@ninja_1.12.0_toolchains; starting
    Fetching repository @@rules_foreign_cc_framework_toolchains; starting
    Fetching repository @@cmake_3.23.2_toolchains; starting

Setup

I have composed a minimal setup to reproduce this:

bazel/starlark/vendors.bzl :

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

_vendors = {
    "curl": {
        "version": "8.8.0",
        "integrity": "sha256-6y8X79aAE5Rf/eHLDKYb9GcJ24b0AMduhEY670U+104=",
    },
}

def _repo(domain, organization, project):
    return "https://%s/%s/%s" % (domain, organization, project)

def _github(repo, version):
    return "%s/archive/refs/tags/%s.tar.gz" % (repo, version)

def vendors_http_archives():
    """
    Set up http_archives for vendored components.
    """

    build_file_content = """filegroup(
        name = "all",
        srcs = glob(["**"]),
        visibility = ["//visibility:public"],
    )"""

    curl_version = "curl-" + _vendors["curl"]["version"].replace(".", "_")

    http_archive(
        name = "vendor_curl",
        build_file_content = build_file_content,
        integrity = _vendors["curl"]["integrity"],
        strip_prefix = "curl-" + curl_version,
        urls = [
            _github(
                repo = _repo("github.com", "curl", "curl"),
                version = curl_version,
            ),
        ],
    )

vendor-curl/BUILD.bazel :

load("@rules_foreign_cc//foreign_cc:configure.bzl", "configure_make")

package(default_visibility = ["//visibility:public"])

configure_make(
    name = "vendor_curl_foreign",
    autoreconf = True,
    autoreconf_options = [
        "-f",
        "-i",
    ],
    configure_in_place = True,
    configure_options = [
        "--disable-manual",
        "--disable-shared",
        "--enable-static",
        "--with-openssl",
    ],
    lib_source = "@vendor_curl//:all",
    out_binaries = [
        "curl",
    ],
)

genrule(
    name = "vendor-curl",
    srcs = [
        ":vendor_curl_foreign",
    ],
    outs = [
        "curl",
    ],
    cmd = """
    (
        mkdir -p ./rule_tmpdir
        cp -r $(SRCS) ./rule_tmpdir
        strip ./rule_tmpdir/curl -o $(@D)/curl
        rm -r ./rule_tmpdir
    )
    """,
)

top-level BUILD.bazel :

empty

top-level MODULE.bazel :

module(name = "foreign")

bazel_dep(name = "rules_foreign_cc", version = "0.11.1")

top-level WORKSPACE.bazel :

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

rules_foreign_cc_dependencies()

load("//:bazel/starlark/vendors.bzl", "vendors_http_archives")

vendors_http_archives()

Environment

This machine runs stable Gentoo Linux but I doubt that this has anything to do with the issue.

Bazel version:

Bazelisk version: development
Build label: 7.2.1
Build target: @@//src/main/java/com/google/devtools/build/lib/bazel:BazelServer
Build time: Tue Jun 25 15:53:05 2024 (1719330785)
Build timestamp: 1719330785
Build timestamp as int: 1719330785
jsharpe commented 4 months ago

You don't need to call rules_foreign_cc_dependencies if you're using bzlmod... Just remove that from your WORKSPACE.bazel file.

xgqt commented 4 months ago

You don't need to call rules_foreign_cc_dependencies if you're using bzlmod... Just remove that from your WORKSPACE.bazel file.

@jsharpe ok, but how would I then register my own toolchains?

xgqt commented 4 months ago

You don't need to call rules_foreign_cc_dependencies if you're using bzlmod... Just remove that from your WORKSPACE.bazel file.

@jsharpe ok, but how would I then register my own toolchains?

Ok, so when I use MODULE.bazel I already have all the toolchains registered.

"@rules_foreign_cc//toolchains:all",

But when I want to sue the preinstalled TC like so:

...
    toolchains = [
        "@rules_foreign_cc//toolchains:preinstalled_autoconf_toolchain",
        "@rules_foreign_cc//toolchains:preinstalled_automake_toolchain",
        "@rules_foreign_cc//toolchains:preinstalled_m4_toolchain",
        "@rules_foreign_cc//toolchains:preinstalled_make_toolchain",
        "@rules_foreign_cc//toolchains:preinstalled_pkgconfig_toolchain",
    ],
...

then Im getting:

'@@rules_foreign_cc~//toolchains:preinstalled_autoconf_toolchain' does not have mandatory providers: 'TemplateVariableInfo'
xgqt commented 4 months ago

Ok, I solved the prebuilt usage issue:


configure_make_variant(
    name = "vendor_curl_foreign",
    lib_source = "@vendor_curl//:all",
    out_binaries = [
        "curl",
    ],
    toolchain = "@rules_foreign_cc//toolchains:preinstalled_pkgconfig_toolchain",
    ...
)