bazel rules_foreign_cc - //:<target> depends on @local_config_cc//:cc-compiler-k8 in repository @local_config_cc which failed to fetch

Viewed 695

I am an absolute beginner to bazel. I am trying to build a CMake based application using bazel through rules_foreign_cc

General Info:

  • OS: Ubuntu 20.04 LTS
  • Bazel version: Tried with both 3.4.0 and 5.0.0
  • CMake version: 3.19.5

My directory structure looks like this

ROOT
|-----BUILD
|-----WORKSPACE
|-----<my_cmake_based_app>-<commit_id>.tar.gz

<my_cmake_based_app> has a CMakeLists.txt at its root.

WORKSPACE file looks like this:

workspace(name = "rules_foreign_cc_usage_example")

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

# Rule repository, note that it's recommended to use a pinned commit to a released version of the rules
http_archive(
    name = "rules_foreign_cc",
    # TODO: Get the latest sha256 value from a bazel debug message or the latest 
    #       release on the releases page: https://github.com/bazelbuild/rules_foreign_cc/releases
    #
    # sha256 = "...",
    strip_prefix = "rules_foreign_cc-0.7.0",
    url = "https://github.com/bazelbuild/rules_foreign_cc/archive/0.7.0.tar.gz",
)

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

# This sets up some common toolchains for building targets. For more details, please see
# https://github.com/bazelbuild/rules_foreign_cc/tree/main/docs#rules_foreign_cc_dependencies
rules_foreign_cc_dependencies()

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

http_archive(
    name = "<target>",
    strip_prefix = "<my_cmake_based_app>-<commit_id>",
    build_file_content = _ALL_CONTENT,
    url = "file:///<absolute/path/to>/<my_cmake_based_app>-<commit_id>.tar.gz",
)

My BUILD file looks like this:

load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")

cmake(
    name = "<target>",
    cache_entries = {
        "CMAKE_C_FLAGS": "-fPIC",
    },
    lib_source = "@<target>//:all_srcs",
    out_shared_libs = ["lib<target>.so"],
)

For starting the build. I use the following command:

bazel build //:<target>

Error log looks like this:

DEBUG: Rule 'rules_foreign_cc' indicated that a canonical reproducible form can be obtained by modifying arguments sha256 = "1df78c7d7eed2dc21b8b325a2853c31933a81e7b780f9a59a5d078be9008b13a"
DEBUG: Repository rules_foreign_cc instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule http_archive defined at:
  /home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
INFO: Repository local_config_cc instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule cc_autoconf defined at:
  /home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/cc_configure.bzl:143:30: in <toplevel>
ERROR: An error occurred during the fetch of repository 'local_config_cc':
   Traceback (most recent call last):
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/cc_configure.bzl", line 125
                configure_unix_toolchain(<3 more arguments>)
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 352, in configure_unix_toolchain
                _find_generic(repository_ctx, <3 more arguments>)
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 314, in _find_generic
                repository_ctx.which(result)
Program argument of which() may not contains a / or a \ ('x86_64-<name_of_sdk>-linux-gcc  -march=x86-64 -mtune=generic -mfpmath=sse -msse4.2 -m64 -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/ubuntu/sdks/sysroots/x86-64-generic-<name_of_sdk>-linux' given)
INFO: Repository cmake-3.22.0-linux-x86_64 instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule http_archive defined at:
  /home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
INFO: Repository <target> instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule http_archive defined at:
  /home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/build_defs/repo/http.bzl:336:31: in <toplevel>
ERROR: /home/ubuntu/<ROOT>/BUILD:3:6: //:<target> depends on @local_config_cc//:cc-compiler-k8 in repository @local_config_cc which failed to fetch. no such package '@local_config_cc//': Traceback (most recent call last):
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/cc_configure.bzl", line 125
                configure_unix_toolchain(<3 more arguments>)
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 352, in configure_unix_toolchain
                _find_generic(repository_ctx, <3 more arguments>)
        File "/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/bazel_tools/tools/cpp/unix_cc_configure.bzl", line 314, in _find_generic
                repository_ctx.which(result)
Program argument of which() may not contains a / or a \ ('x86_64-<name_of_sdk>-linux-gcc  -march=x86-64 -mtune=generic -mfpmath=sse -msse4.2 -m64 -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/ubuntu/sdks/sysroots/x86-64-generic-<name_of_sdk>-linux' given)
ERROR: Analysis of target '//:<target>' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.127s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)

Some of my obvious questions are:

  • I have no idea from where this @local_config_cc is coming from.
  • Am I missing out on something to add in my WORKSPACE / BUILD files? OR
  • Is my directory structure a complete mess?

As a side note, since this is the 1st time I am posting something over stackoverflow, I would highly appreciate any tips so that I could ask questions in a better way in the future.

Thanks!

EDIT: FYI, I was able to build the example given in rules_foreign_cc on my existing system.

EDIT: FYI, The system I am using is an AWS EC2 Instance

1 Answers

@local_config_cc is part of Bazel's automatic C++ toolchain set up. It looks like something is going wrong between Bazel and the C++ toolchain on your system, because this is the error:

Program argument of which() may not contains a / or a \ ('x86_64-<name_of_sdk>-linux-gcc  -march=x86-64 -mtune=generic -mfpmath=sse -msse4.2 -m64 -fstack-protector-strong  -O2 -D_FORTIFY_SOURCE=2 -Wformat -Wformat-security -Werror=format-security --sysroot=/home/ubuntu/sdks/sysroots/x86-64-generic-<name_of_sdk>-linux' given)

So the argument to which looks like some command, whereas it should be just the name of a c++ tool like "gcc". Bazel will look at environment variables to find these tools too. Is there something in the CC environment variable?

If not, it might be useful to try to build something simple first, e.g.:

main.c:

#include <stdio.h>
int main() {
  printf("hello world\n");
  return 0;
}

BUILD:

cc_binary(
  name = "main",
  srcs = ["main.c"],
)

then:

$ bazel run main
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:main (35 packages loaded, 146 targets configured).
INFO: Found 1 target...
Target //:main up-to-date:
  bazel-bin/main
INFO: Elapsed time: 3.657s, Critical Path: 0.03s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
hello world

Commenting more generally on your other questions, it's somewhat unusual to have a tar.gz archive in your workspace and then reference that archive in an http_archive rule in your WORKSPACE file[1]. If you have specific reasons or other context for doing that, it might be helpful to talk about those. Otherwise, you're basically just using Bazel to untar an archive and run make on the contents, which you could do in a two-line shell script :) Typically your build files and workspace file would be a part of your code repository itself.

[1] Also, putting absolute paths in your workspace file (or really anywhere) makes your set up not portable

Related