Using Rules Foreign CC to Build AWS C++ SDK with Bazel - bazel

Is there a way to do this? I'm trying to build parts of the AWS SDK (s3 and rds) to use in my Bazel project. I've heard that rules_foreign_cc can be used to integrate CMake projects with Bazel.

tensorflow-io contains bazel build files here:
WORKSPACE
BUILD file
Might be slightly cleaner to adopt these vs using rules_foreign_cc. Note that above links refer to a specific hash in the repo - the files might have changed upstream.

load("#rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
rules_foreign_cc_dependencies()
AWS_BUILD = """\
filegroup(
name = "sdk",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
"""
new_git_repository(
name = "aws_sdk",
build_file_content = _ALL_CONTENT,
commit = "2550901e1011e0ee1dc1bae44b42e1a2c6947c24",
recursive_init_submodules = True,
remote = "https://github.com/aws/aws-sdk-cpp",
shallow_since = "1628277923 +0000",
)
Then you can reference the binaries from within the BUILD file. Just build the aws-sdk using cmake.
load("#rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
cmake(
name = "aws_sdk",
cache_entries = {
"CMAKE_BUILD_TYPE": "Release",
"BUILD_ONLY": "s3",
"BUILD_SHARED_LIBS": "ON",
"ENABLE_TESTING": "OFF",
},
install = True,
lib_source = "#aws_sdk//:sdk",
out_shared_libs = [
"libaws-cpp-sdk-core.so",
"libaws-cpp-sdk-s3.so",
]
)

Related

Missing required module in ios_dynamic_framework [Bazel]

I am trying to use Bazel to create distributable framework and I have BUILD file like this:
ios_dynamic_framework(
name = "TheFramework",
bundle_name = "TheFramework",
...
deps = [
"TheFrameworkLib",
]
)
swift_library(
name = "TheFrameworkLib",
module_name: "TheFramework"
srcs = [
# publicly available
],
deps = [
":InternalLib"
],
)
swift_library(
name = "InternalLib",
srcs = [
# internal files
],
)
It builds successfully, and I can see by the size of the framework that InternalLib is also included in the binary. However, when I import this TheFramework.framework in the new iOS project (without Bazel), I am getting error: Missing required module "InternalLib". Any ideas how this can work? I guess I am missing some flags?

Deploy py_binary without container bazel

I am attempting to create a py_binary in bazel and then copy that to a remote machine. At the moment I was doing a dummy app to make sure the pipeline would work. I cannot use a docker image as I need lower level control of the hardware than docker can provide.
My goal is to do the following:
build the software with bazel
create a tar package that contains the py_binary
copy and run that binary on another computer not connected to bazel
To do this I made a simple binary(that for context just makes some RPC calls to a server I am working on as a side project) and the build files is as follows:
py_binary(
name="rosecore_cli",
srcs=glob([
"src/*.py"
]),
deps = [
"//rosecore/proto:project_py_pb2_grpc",
"//rosecore/proto:project_py_pb2"
]
)
pkg_files(
name = "binary",
srcs = [
":rosecore_cli",
],
prefix = "/usr/share/rosecore/bin",
)
pkg_filegroup(
name = "arch",
srcs = [
":binary",
],
visibility = ["//visibility:public"],
)
pkg_tar(
name = "rosecore_tar",
srcs = [
":arch"
],
include_runfiles=True
)
When I build, copy the tar file and extract it I get the following error:
AssertionError: Cannot find .runfiles directory for ./usr/share/rosecore/bin/rosecore_cli
Any help would be appreciated :)

`Bazel run` won't find static file dependency in bazel-bin when running nodejs_image

I am trying to get a rules docker nodejs_image to run using bazel.
My command is
bazel run :image.binary
Here is my rule:
load("#npm//#bazel/typescript:index.bzl", "ts_project")
load("#io_bazel_rules_docker//nodejs:image.bzl", "nodejs_image")
ts_project(
name = "typescript_build",
srcs = glob([
"src/**/*",
]),
allow_js = True,
out_dir = "build",
deps = ["#npm//:node_modules"],
)
nodejs_image(
name = "image",
data = [
":package.json",
":typescript_build",
"#npm//:node_modules",
],
entry_point = "build/app.js",
)
Basically, I need the package.json file because it includes some important configuration information when Node executes. If I call bazel build :image and then grab/run that image, everything works fine. But if I call bazel run :image it will basically work except that it can't find the package.json.
When I check the bazel-bin/ folder, I've noticed that the package.json isn't included, but the built typescript and node_modules are. I'm guessing because I'm not running any prior rules on the package.json, it doesn't get added to the bin, but I really don't know how to work around this.
So, basically, it seems that if you just use a rule like copy_to_bin or js_library, I think their purpose is to help put static files into your bazel-bin.
https://bazelbuild.github.io/rules_nodejs/Built-ins.html#copy_to_bin
ts_project(
name = "typescript_build",
srcs = glob([
"src/**/*",
]),
allow_js = True,
out_dir = "build",
deps = ["#npm//:node_modules"],
)
js_library(
name = "library",
srcs = ["package.json"],
deps = [":typescript_build"],
)
nodejs_image(
name = "image",
data = [
":library",
"#npm//:node_modules",
],
entry_point = "build/app.js",
)

Build Bazel package from multiple repos

I am trying to build OpenCV with external modules, but haven't been able to come up with a clean solution.
I have successfully built OpenCV using CMake (rules_foreign_cc). But to be able to build with external modules I need to download a separate repo (opencv_contrib) and then build using the path to the second repo as a build argument.
Is there a way I can do this without having to create a git repo of my own that combines these two git repos?
# WORKSPACE file
http_archive(
name = "rules_foreign_cc",
strip_prefix = "rules_foreign_cc-4010620160e0df4d894b61496d3d3b6fc8323212",
sha256 = "07e3414cc841b1f4d16e5231eb818e5c5e03e2045827f5306a55709e5045c7fd",
url = "https://github.com/bazelbuild/rules_foreign_cc/archive/4010620160e0df4d894b61496d3d3b6fc8323212.zip",
)
load("#rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")
rules_foreign_cc_dependencies()
http_archive(
name = "opencv_contrib",
build_file="//:opencv_contrib.BUILD",
sha256 = "9f52fd3114ac464cb4c9a2a6a485c729a223afb57b9c24848484e55cef0b5c2a",
urls = ["https://github.com/opencv/opencv_contrib/archive/refs/tags/4.5.2.tar.gz"],
strip_prefix = "opencv_contrib-4.5.2",
)
http_archive(
name = "opencv",
sha256="ae258ed50aa039279c3d36afdea5c6ecf762515836b27871a8957c610d0424f8",
build_file="//:opencv.BUILD",
urls = ["https://github.com/opencv/opencv/archive/refs/tags/4.5.2.tar.gz"],
strip_prefix = "opencv-4.5.2",
)
# opencv_contrib.BUILD file
filegroup(
name = "modules",
srcs = glob(["modules/**"]),
visibility = ["//visibility:public"],
)
# opencv.BUILD file
load("#rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
filegroup(
name = "srcs",
srcs = glob(["**"]),
visibility = ["//visibility:public"],
)
cmake(
name = "opencv",
generate_args = ["-GNinja"],
additional_inputs=["#opencv_contrib//:modules"],
cache_entries = {
"BUILD_SHARED_LIBS": "OFF",
"BUILD_opencv_world": "ON",
"OPENCV_EXTRA_MODULES_PATH": "$$EXT_BUILD_ROOT$$/external/opencv_contrib/modules",
},
lib_source = ":srcs",
out_static_libs = ["libopencv_world.a"],
)
You can use git_repository rule in your WORKSPACE file to have bazel automatically clone a git repo before building.
Once you add it to the WORKSPACE file, then you can reference build targets of the remote repo inside your own repo.
Check this repo for an example.

How to specify groupid, artifact and version directly in dependencies section of BUILD file using Bazel?

How to specify groupid, artifact and version directly in dependencies section of BUILD file using Bazel?
I am trying to convert a simple gradle project to bazel project. Can't really use generate_workspace since I have a gradle project (not maven).
I am wondering if there is just a easier way to specify GAV in the dependencies itself in the BUILD file so it would look something like this
java_binary(
name = "HelloWorld",
srcs = glob(["src/main/java/**/*.java"]),
resources = glob(["src/main/resources/**"]),
deps = ["com.fasterxml.jackson.core:jackson-core:2.8.8"],
main_class = "com.hello.sample.Foo"
)
Have you tried using maven_jar() directly?
In WORKSPACE:
maven_jar(
name = "com_google_guava_guava",
artifact = "com.google.guava:guava:18.0",
sha1 = "cce0823396aa693798f8882e64213b1772032b09",
)
In BUILD:
java_binary(
name = "HelloWorld",
srcs = glob(["src/main/java/**/*.java"]),
resources = glob(["src/main/resources/**"]),
deps = ["#com_google_guava_guava//jar"],
main_class = "com.hello.sample.Foo"
)
See https://docs.bazel.build/versions/master/be/workspace.html#maven_jar

Resources