Home > OS >  Bazel: submodule in project does not use bazel , pushing to git doesnt include locally added BUILD f
Bazel: submodule in project does not use bazel , pushing to git doesnt include locally added BUILD f

Time:02-10

I am just starting to learn how to use bazel following this tutorial

One thing I am unsure how to do is how to use a submodule, from my own repo for example. where I do not use bazel. The repo just has some c/h files that I need to pull in. To make things work locally I added a BUILD file in folder pulled in from submodules. However after I commit and push my changes the BUILD file is obviously not there. How do I add the c/h files from the submodule folder to my build. Bazel seems to be looking for a BUILD folder in that directory and there will be none, if for example someone else clones this repo.

currently my "main" Directory has this build file:

cc_library(
  name = "uCShell-main",
  srcs = ["main.c"],
  deps = ["//uCShell:uCShell-lib" ],
)

cc_binary(
   name = "uCShell-bin",
   deps = [":uCShell-main" , "//uCShell:uCShell-lib" ]
)

and the folder with the pulled in submodule has this locally added BUILD file:

cc_library(
    name = "uCShell-lib",
    srcs = glob(["*.c"]),
    hdrs = glob(["*.h"]),
    visibility = ["//visibility:public"],
)

This works and compiles just fine. However do correct any issues or misuse of Bazel you see here, I'd like to learn. Ultimately to reiterate the issue, when I push this project the locally added BUILD file will not be in the project because it is not in the original submodule. So how can I inlcude the c/h files in that submodule in order to build my main. And I would like to leave it as a submodule. Ultimately I can just add a BUILD file for the submodule's repo, but would like to find a better way, for example what if this was not my repo where I can just add a BUILD file.

CodePudding user response:

If you use new_local_repository with a relative path to import the submodule, you can set build_file or build_file_content to add the BUILD file. You should be able to use that same BUILD file as-is.

Because it will be in a different external repository, you'll need to access it via the corresponding label.

For example, if you put the BUILD file at build/BUILD.my_lib.bazel and this in WORKSPACE:

new_local_repository(
    name = "my_lib",
    path = "submodules/my_lib",
    build_file = "@//build:BUILD.my_lib.bazel",
)

then you can put @my_lib//:uCShell-lib in deps of any other target and it will all work.

  • Related