Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bazel: submodule in project does not use bazel , pushing to git doesnt include locally added BUILD file in submodule, how do i build?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading