1

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 Answers1

0

@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

ahumesky
  • 4,203
  • 8
  • 12
  • "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" --> I initially tried with a Gitlab tar.gz download link out there but got the same error. "Also, putting absolute paths in your workspace file (or really anywhere) makes your set up not portable" --> Yes I am aware of that. That's just for testing purposes. – Indramani Arora Feb 24 '22 at 05:19
  • "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 :)" --> My end goal is to integrate this cmake based application in a bazel environment. So, it seemed easy for me to wrap the whole thing with bazel rather than writing bazel recipes from scratch. If there is a better way of doing that, please let me know. – Indramani Arora Feb 24 '22 at 05:23
  • FYI, I was able to build the example given in [rules_foreign_cc](https://bazelbuild.github.io/rules_foreign_cc/0.4.0/index.html) on the same system – Indramani Arora Feb 24 '22 at 05:29
  • "Is there something in the CC environment variable" --> this is the content of `CC` environment variable: `x86_64--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--linux` – Indramani Arora Feb 24 '22 at 06:40
  • It looks like your `CC` environment variable got set to something weird. The `` looks especially suspicious. You might try clearing `CC`, unless you know you need that for something else. – ahumesky Feb 24 '22 at 16:22
  • so what should `CC` be ideally set to? – Indramani Arora Feb 24 '22 at 16:39
  • Just now I tried keeping `CC` as `x86_64--linux-gcc -march=x86-64 --sysroot=/home/ubuntu/sdks/sysroots/x86-64-generic--linux` which gave me the same error. Keeping it as blank gives this error -> `/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/rules_foreign_cc/toolchains/BUILD.bazel:91:22: @platforms//os:windows is not a valid configuration key for @rules_foreign_cc//toolchains:built_make` – Indramani Arora Feb 24 '22 at 17:03
  • You can try clearing the `CC` variable just for one invocation of bazel with `CC="" bazel build //:`. If that fixes it, then you'll have to find how or where CC is getting set to that value. E.g. could be int your bashrc – ahumesky Feb 25 '22 at 04:29
  • As I said earlier, Keeping it as blank gives this error --> `/home/ubuntu/.cache/bazel/_bazel_ubuntu/312d04fd6758f8908769adc1bc8469ac/external/rules_foreign_cc/toolchains/BUILD.bazel:91:22: @platforms//os:windows is not a valid configuration key for @rules_foreign_cc//toolchains:built_make` – Indramani Arora Feb 25 '22 at 05:05
  • If I try by removing `--sysroot` option in `CC`, then I get this additional error --> `Auto-Configuration Error: Cannot find gcc or CC (x86_64--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); either correct your path or set the CC environment variable` – Indramani Arora Feb 25 '22 at 05:20
  • I would guess that setting CC to empty is the right thing to do (or at least better). The error `@platforms//os:windows is not a valid configuration key for @rules_foreign_cc//toolchains:built_make` sounds like there might be some bug in rules_foreign_cc that you're running into. You might consider filing a github issue with rules_foreign_cc – ahumesky Feb 25 '22 at 23:14
  • Yes, bazel 5.0.0 does not give this error when `CC` is blank, but I am getting some CMake errors now, seems that CMake now is not able to find to find my custom toolchain packages. – Indramani Arora Feb 27 '22 at 16:52
  • I'm not super familiar with CMake, so unfortunately I'm not sure how to help there! You might try reaching out to bazel-discuss: https://groups.google.com/g/bazel-discuss – ahumesky Feb 28 '22 at 20:19