I'm trying to build a hello_world cc_grpc_library using bazel 6.0.0 on Ubuntu 22.04, but I'm unable to do so.
Source tree:
WORKSPACE
MODULE.bazel
BUILD
helloworld.proto
.bazelrc
.bazelversion
My WORKSPACE file is empty. WORKSPACE.bzlmod does not exist.
MODULE.bazel contains:
module(name = "helloworld", version = "1.0")
bazel_dep(name = "grpc", version = "1.47.0", repo_name = "com_github_grpc_grpc")
BUILD contains:
load("#rules_proto//proto:defs.bzl", "proto_library")
load("#com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
load("#com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
load("#com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library")
grpc_deps()
grpc_extra_deps()
proto_library(
name = "helloworld_proto",
srcs = ["helloworld.proto"],
)
cc_proto_library(
name = "helloworld_cc_proto",
deps = [":helloworld_proto"],
)
cc_grpc_library(
name = "helloworld_cc_grpc",
srcs = [":helloworld_proto"],
grpc_only = True,
deps = [":helloworld_cc_proto"],
)
helloworld.proto is a copy of gRPC's helloworld.proto
When I type bazel build :helloworld_cc_grpc I get the following error and don't know how to solve it:
...external/grpc~1.47.0/bazel/grpc_deps.bzl", line 23, column 11, in grpc_bind_deps
native.bind(
Error: no native function or rule 'bind'
Available attributes: aar_import, action_listener, alias, android_binary, android_device, android_device_script_fixture, android_host_service_fixture, android_instrumentation_test, android_library, android_local_test, android_sdk, android_tools_defaults_jar, apple_cc_toolchain, available_xcodes, cc_binary, cc_host_toolchain_alias, cc_import, cc_libc_top_alias, cc_library, cc_proto_library, cc_shared_library, cc_shared_library_permissions, cc_test, cc_toolchain, cc_toolchain_alias, cc_toolchain_suite, config_feature_flag, config_setting, constraint_setting, constraint_value, environment, existing_rule, existing_rules, exports_files, extra_action, fdo_prefetch_hints, fdo_profile, filegroup, genquery, genrule, glob, j2objc_library, java_binary, java_import, java_library, java_lite_proto_library, java_package_configuration, java_plugin, java_plugins_flag_alias, java_proto_library, java_runtime, java_test, java_toolchain, label_flag, label_setting, objc_import, objc_library, package, package_group, package_name, platform, propeller_optimize, proto_lang_toolchain, proto_library, py_binary, py_library, py_runtime, py_test, repository_name, sh_binary, sh_library, sh_test, subpackages, test_suite, toolchain, toolchain_type, xcode_config, xcode_config_alias, xcode_version
bazel version output:
Bazelisk version: v1.15.0
Build label: 6.0.0
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Mon Dec 19 15:52:35 2022 (1671465155)
Build timestamp: 1671465155
Build timestamp as int: 1671465155
I've also found this gRPC github issue but I'm not sure what to do with it.
How to build gRPC's hello world example using bzlmod to manage external dependencies?
I've tried building it with bazel's cc_grpc_library but that wraps actual dependency (gRPC) into yet another layer as it is visible from the comment on the provided link. Either way it wasn't building but I don't recall actual error.
gRPC doesn't yet support Bazel module so it can break but in your case, it looks weird to call grpc_deps() and grpc_extra_deps() functions in BUILD file because those are expected to be called in WORKSPACE file. If it turns out that module doesn't work yet, you may want to use it without module. Please take a look at this example.
I'm using bazel/gazelle to pull in some external git repositories.
For example:
go_repository(
name = "com_github_pkg_errors",
importpath = "github.com/pkg/errors",
sum = "h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=",
version = "v0.8.1",
)
My go file would just import github.com/pkg/errors like normal.
Problem comes when I run go vet on my file. It would complain that no such package exists under GOROOT or GOPATH. This repo is located in my bazel cache. ~/.cache/bazel/.../external/com_github_pkg_errors
How can I resolve this?
You have to specify the dependency in go.mod
require (
github.com/pkg/errors v0.8.1
)
In the BUILD.bazel file of your project, specify go_library with dep
go_library(
name = "project_lib",
srcs = [],
importpath = "github.com/pkg/errors",
visibility = ["//visibility:public"],
deps = [
"#com_github_pkg_errors",
],
)
I'd like to use google protobuf (master branch) + bazel v4.2.1 to set up communication between C++ and python parts of my program. I've got the following WORKSPACE file:
load("#bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "com_google_protobuf",
strip_prefix = "protobuf-master",
urls = ["https://github.com/protocolbuffers/protobuf/archive/master.zip"],
)
load("#com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
protobuf_deps()
and the BUILD file:
load("#com_google_protobuf//:protobuf.bzl", "cc_proto_library")
load("#com_google_protobuf//:protobuf.bzl", "py_proto_library")
cc_proto_library(
name = "container_cc_proto",
srcs = ["container.proto"],
visibility = ["//visibility:public"],
deps = ["#com_google_protobuf//:any_proto",],
)
py_proto_library(
name = "container_py_proto",
srcs = ["container.proto"],
visibility = ["//visibility:public"],
deps = ["#com_google_protobuf//:any_proto",],
)
The container.proto tries to include "google/protobuf/any.proto". On my command bazel build //:container_cc_proto I see the following error:
ERROR: /home/*user*/sandbox/bypass/BUILD:31:17: no such target '#com_google_protobuf//:any_proto_genproto': target 'any_proto_genproto' not declared in package '' defined by /home/*some path*/external/com_google_protobuf/BUILD and referenced by '//:container_cc_proto_genproto'
Could anyone please tell me how to include any.proto correctly, or any other known workarounds. Thanks!
I am building my C++ project using Bazel on macOS, and I added the gRPC as one of the deps
FYI, I successfully built with same settings on Linux, however, it fails for me on macOS.
WORKSPACE
git_repository(
name = "com_github_grpc_grpc",
remote = "https://github.com/grpc/grpc",
tag = "v1.20.1"
)
load("#com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()
and the build file for my module
a.BUILD
cc_library(
name = "s12",
srcs = glob(
["**/*.cc"],
),
hdrs = glob(
["**/*.h",],
),
deps = [
"#com_github_grpc_grpc//:grpc++",
"#com_google_protobuf//:protobuf",
],
)
and in linking stage I got following error:
[libprotobuf ERROR external/com_github_sc_protobuf/src/google/protobuf/descriptor_database.cc:58] File already exists in database: google/protobuf/descriptor.proto
[libprotobuf FATAL external/com_github_sc_protobuf/src/google/protobuf/descriptor.cc:1370] CHECK failed: GeneratedDatabase()->Add(encoded_file_descriptor, size):
libc++abi.dylib: terminating with uncaught exception of type google::protobuf::FatalException: CHECK failed: GeneratedDatabase()->Add(encoded_file_descriptor, size):
external/bazel_tools/tools/test/test-setup.sh: line 310: 63268 Abort trap: 6 "${TEST_PATH}" "$#" 2>&1
Apparently, some file is included multiple times, however, I couldn't figure out why.
I'm new to Bazel and learning its build work, currently I am trying to do with bazel build from a Maven project, please advise me how to make it working, thanks.
Here is the WORKSPACE file I am trying to define:
maven_jar(
name = "junit",
artifact = "junit:junit:3.8.1",
)
maven_jar(
name = "log4j1",
artifact = "org.apache.logging.log4j:log4j-core:2.6.2",
)
maven_jar(
name = "log4j2",
artifact = "org.apache.logging.log4j:log4j-api:2.6.2",
)
.....
Here is the BUILD file I am trying to define:
package(default_visibility = ["//visibility:public"])
java_binary(
name = "everything",
srcs = glob(["src/main/java/**/*.java"]),
resources = glob(["src/main/resources/**"]),
main_class = "src/main/java/com/test/test/test/App",
deps = [
"#junit//jar",
"#log4j1//jar",
"#log4j2//jar",
"#jackson//jar",
"#jsonsimple//jar",
"#commonsdbutils//jar",
"#commons//jar",
"#guava//jar",
"#poi//jar"],
)
Here is I got Bazel build results:
mbp:bazel_test me$ bazel build //:everything
INFO: Analysed target //:everything (1 packages loaded).
INFO: Found 1 target...
ERROR: /Users/me/git/test/test/BUILD:4:1: Building everything-class.jar (104 source files) failed (Exit 1)
src/main/java/com/test/test/test/Testapp.java:13: error: cannot find symbol
import org.apache.poi.ss.usermodel.Cell;
^
symbol: class Cell
location: program package org.apache.poi.ss.usermodel
.....
Target //:everything failed to build
I see two problems here:
The main_class attribute should use dots instead of slashes:
java_binary(
name = "everything",
srcs = glob(["src/main/java/**/*.java"]),
main_class = "src.main.java.com.test.test.test.App",
...
There's no org.apache.poi.ss.usermodel.Cell class in the poi jar.
Find the jar's name:
$ bazel query --output=build #poi//jar
...
java_import(
name = "jar",
visibility = ["//visibility:public"],
jars = ["#poi//jar:poi-ooxml-3.9.jar"],
srcjar = "#poi//jar:poi-ooxml-3.9-sources.jar",
)
The package's location is $(bazel info output_base)/external/poi, and now we know (from the srcs attribute in the bazel query output above) that the file is called poi-ooxml-3.9.jar:
$ unzip -l $(bazel info output_base)/external/poi/jar/poi-ooxml-3.9.jar | grep "org.apache.poi.ss.usermodel"
0 2012-11-26 17:14 org/apache/poi/ss/usermodel/
2970 2012-11-26 17:14 org/apache/poi/ss/usermodel/WorkbookFactory.class