georgetheevilman
georgetheevilman

Reputation: 177

Glob not found in bazel

I am currently using Bazel to build a C++ project. Here is my WORKSPACE file:

cc_library(
name = "Boost",
srcs = glob(["/usr/local/lib/libboost*.so"]),
hdrs = glob(["/home/duttama/boost_1_55_0/**/*.h"]),
)


cc_library(
name = "OpenCV",
srcs = glob(["/usr/lib/x86_64-linux-gnu/libopencv*.so"]),
hdrs = glob(["/home/duttama/opencv-2.4.13/**/*.h"]),
)


cc_library(
name = "AffectivaSources",
srcs = glob(["/root/affdex-sdk/lib/*.so"]),
hdrs = glob(["/root/affdex-sdk/include/*.h"]),
)

And here is my BUILD file:

cc_binary(
name = "video-demo",
srcs = ["video-demo.cpp"],
deps = ["//lib:OpenCV",
            "//lib:AffectivaSources",
            "//lib:Boost",
],
)    

When I run it, I get this error:

ERROR: /root/sdk-samples/WORKSPACE:17:12: Traceback (most recent call last):
File "/root/sdk-samples/WORKSPACE", line 15
    cc_library(name = "AffectivaSources", srcs = ..."]), ..."]))
File "/root/sdk-samples/WORKSPACE", line 17, in cc_library
    glob
name 'glob' is not defined

My question is why is it not finding the glob function. I am not sure what else to specify in the WORKSPACE file or how to import this glob function.

Upvotes: 4

Views: 3511

Answers (1)

Jin
Jin

Reputation: 13533

The WORKSPACE file is not meant for cc_binary/cc_rules; it's for defining project-wide rules such as external dependencies, and the glob function is not available in WORKSPACE files. See https://docs.bazel.build/versions/master/be/workspace.html for WORKSPACE rules.

It seems like you're importing c++ dependencies into your project: check out the new_local_repository rule here. This allows you to point to a folder in your local system. It will require you to write BUILD files containing the respective cc_library rules for each external dependency.

For example, in your WORKSPACE file:

new_local_repository(
  name = "affectivalibrary",
  path = "/root/affdex-sdk/",
  build_file = "BUILD.affectiva",
)

and in your project, create a BUILD.affectiva:

cc_library(
  name = "aff",
  srcs = glob(["lib/*.so"]),
  hdrs = glob(["include/*.h"]),
)

Once you've done this, you can depend on them in your BUILD files with the syntax:

cc_binary(
  name = "video-demo",
  srcs = ["video-demo.cpp"],
  deps = ["@affectivalibrary//:aff", ...]
)

I'm also seeing that you are using absolute file paths in the srcs/hdrs attributes - glob patterns cannot be absolute. Please refer to the BUILD file target label syntax here: https://docs.bazel.build/versions/master/build-ref.html#lexi

If you're still confused, there's an example c++ project in the Bazel repository: https://github.com/bazelbuild/bazel/tree/master/examples/cpp

Upvotes: 2

Related Questions