bazelbuild / rules_cc

C++ Rules for Bazel
https://bazel.build
Apache License 2.0
176 stars 88 forks source link

rules_cc issue while using for cross compilation. #220

Closed dommatib closed 5 months ago

dommatib commented 6 months ago

Description of the problem: I'm using rules_cc to cross compile the c++ code. while i configure the bazel workspace with rules_cc repo, it fails to download. Below is the Example: Workspace file: load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive( name = "rules_cc", urls = ["https://github.com/bazelbuild/rules_cc/releases/download/0.0.9/rules_cc-0.0.9.tar.gz"], sha256 = "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf", strip_prefix = "rules_cc-0.0.9", )

load("@rules_cc//cc:configs.bzl", "cc_autoconf")

cc_autoconf(name = "local_config_cc")

load("@bazel_toolchains//rules:envs.bzl", "bazel_toolchain")

bazel_toolchain( name = "x86_64-linux", exec_compatible_with = [ "@platforms//os:linux", "@platforms//cpu:x86_64",
], target_compatible_with = [ "@platforms//os:linux", "@platforms//cpu:x86_64", ], toolchain = ":cc-compiler-x86_64-linux", toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", )

bazel_toolchain( name = "x86_64-windows", exec_compatible_with = [ "@platforms//os:linux", "@platforms//cpu:x86_64", ], target_compatible_with = [ "@platforms//os:windows", "@platforms//cpu:x86_64", ], toolchain = ":cc-compiler-x86_64-windows", toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", )

cc_toolchain_config( name = "cc-compiler-x86_64-linux", cpu = "k8", compiler = "gcc", host_system_name = "x86_64-unknown-linux-gnu", target_system_name = "x86_64-unknown-linux-gnu", target_libc = "glibc_2.24", )

cc_toolchain_config( name = "cc-compiler-x86_64-windows", cpu= "k8", compiler = "msvc-cl", host_system_name = "x86_64-unknown-linux-gnu", target_system_name = "x86_64-unknown-windows", target_libc = "msvcrt", ) BUILD FILE:

load("@rules_cc//cc:cc_toolchain_config.bzl", "cc_toolchain_config")

exports_files(["cc_toolchain_config.bzl"])

cc_library( name = "hello_greet", srcs = ["hello_greet.cc"], hdrs = ["hello_greet.h"], )

cc_shared_library( name = "hello_greet_shared", deps = [":hello_greet"], )

cc_binary( name = "hello_world_linux", srcs = ["hello_world.cc"], deps = [":hello_greet", ":hello_greet_shared"], toolchains = ["@linux_toolchain"], )

cc_binary( name = "hello_world_windows", srcs = ["hello_world.cc"], deps = [":hello_greet", ":hello_greet_shared"], toolchains = ["@windows_toolchain"], )

cc_toolchain_config( name = "linux_toolchain", toolchain_identifier = "linux", compiler = "gcc", target_libc = "glibc", target_cpu = "k8", )

cc_toolchain_config( name = "windows_toolchain", toolchain_identifier = "windows", compiler = "cl.exe", target_libc = "msvcrt", target_cpu = "x64_windows", ) bazel.rc file build:windows --copt=-DWIN32 build:windows --cxxopt=-std=c++14

build:linux --copt=-DLINUX build:linux --cxxopt=-std=c++14

build:windows --cpu=x64_windows build:linux --cpu=x64_linux

build:windows --compilation_mode=dbg build:windows --compilation_mode=opt

build:linux --compilation_mode=dbg build:linux --compilation_mode=opt

build:dbg --copt=-g build:dbg -c dbg build:opt --copt=-O3 build:opt --copt=-DNDEBUG build:opt -c opt

Translation units to Build:

hello_greet.h

ifndef LIB_HELLO_GREETH

define LIB_HELLO_GREETH

include

std::string get_greet(const std::string &thing);

endif

hello_greet.cc

include "hello_greet.h"

include

std::string get_greet(const std::string& world) { return "Hello " + world; }

hello_world.cc

include "hello_greet.h"

include

include

int main(int argc, char** argv) { std::cout << get_greet("world") << std::endl; return 0; }

Configured the WORKSPACE file with rules_cc this should fetch the code from git and the build file is configured for cross compiled translation units (Linux & Windows) by loading rules_cc and set for cross compilation for windows and linux

ERROR: Error computing the main repository mapping: Every .bzl file must have a corresponding package, but '@@rules_cc//cc:configs.bzl' does not have one. Please create a BUILD file in the same or any parent directory. Note that this BUILD file does not need to do anything except exist.

The above error indicates ruless_cc//cc:configs.bzlis missing since the git download didn't happen because the same rules_cc is not downloaded . As per the above error the downloaded rules_cc should be under external/rules_cc/cc/empty build file (to be created ) Manually downloaded the repo and kept under same path external/rules_cc/cc/build Already build file exists but its still asking to create one BUILD file and add below package and export _files for configs.bzl as its missing from the repo package(default_visibility = ["//visibility:public"]) exports_files(["configs.bzl"])

Bazel version used: bazel version 7.0.0 Debain version used: Debian GNU/Linux rodete

Let me know if you need any more information

comius commented 5 months ago

Bazel 7.0.0 has by default enabled bzlmod and from your example it seems you're using WORKSPACE file, which is not read. You can disable bzlmod with --nobzlmod or even better, write a MODULE.bazel with dependency to rules_cc.