Understanding how external dependencies and build overlays work

120 views
Skip to first unread message

Ajith Ramanathan

unread,
Jan 5, 2023, 3:19:07 PM1/5/23
to bazel-discuss
Hi.  I'm trying to work with some external dependencies, and I want to make sure that I correctly understand what's going on.  I apologise if these questions betray my ignorance or reveal the terribly unsystematic nature of my reading of the Bazel documentation.
Concretely, I'm trying to build some code that depends on imgui, which in turn depends on glfw.  So, I need to declare dependencies on both of them in my WORKSPACE.bazel:

http_archive(
    name = "imgui",
    build_file = "//third_party/imgui:imgui.BUILD.bazel",
    sha256 = "6d02a0079514d869e4b5f8f590f9060259385fcddd93a07ef21298b6a9610cbd",
    strip_prefix = "imgui-1.89.1",
    urls = [
        "https://github.com/ocornut/imgui/archive/refs/tags/v1.89.1.tar.gz",
        "https://github.com/ocornut/imgui/archive/refs/tags/v1.89.1.tar.gz",
    ],
)

and

http_archive(
    name = "glfw",
    build_file = "//third_party/glfw:glfw.BUILD.bazel",
    sha256 = "f30f42e05f11e5fc62483e513b0488d5bceeab7d9c5da0ffe2252ad81816c713",
    strip_prefix = "glfw-3.3.8",
    urls = [
        "https://github.com/glfw/glfw/archive/refs/tags/3.3.8.tar.gz",
        "https://github.com/glfw/glfw/archive/refs/tags/3.3.8.tar.gz",
    ],
)


Neither dependency uses Bazel, so I added build overlays under //third_party/{glfw,imgui}.  As I understand it, when, say, imgui, is built, the source is fetched and the file //third_party/imgui:imgui.BUILD.bazel is copied to the root of the source.   If I need to depend on imgui, I use the label @imgui rather that //third_party/imgui since the build file under third_party is not actually visible.  Is that right?  Now, I wanted things to be a little bit prettier in my code so I added another build file //third_party/imgui:BUILD.bazel that aliases the imgui build rule so that I can use the label //third_party/imgui in my code:

alias(
    name = "imgui",
    actual = "@imgui",
)


All good so far.

Question 1: I need to add a dependency on glfw to imgui.  I found that I could only do that via @imgui rather than //third_party/imgui.  //third_party/imgui works in the code that I'm writing though.  It is a minor nit, but I wonder why the discrepancy?
Question 2: Is there a way to only have one build file and still reference imgui as //third_party/imgui without the use of the alias rule?

Thanks, and happy New Year!

Brian Silverman

unread,
Jan 5, 2023, 3:56:44 PM1/5/23
to Ajith Ramanathan, bazel-discuss
Sounds like you've mostly got it. Responses to some specific things inline:

On Thu, Jan 5, 2023 at 12:19 PM Ajith Ramanathan <aji...@gmail.com> wrote:
Hi.  I'm trying to work with some external dependencies, and I want to make sure that I correctly understand what's going on.  I apologise if these questions betray my ignorance or reveal the terribly unsystematic nature of my reading of the Bazel documentation.
Concretely, I'm trying to build some code that depends on imgui, which in turn depends on glfw.  So, I need to declare dependencies on both of them in my WORKSPACE.bazel:

http_archive(
    name = "imgui",
    build_file = "//third_party/imgui:imgui.BUILD.bazel",
    sha256 = "6d02a0079514d869e4b5f8f590f9060259385fcddd93a07ef21298b6a9610cbd",
    strip_prefix = "imgui-1.89.1",
    urls = [
        "https://github.com/ocornut/imgui/archive/refs/tags/v1.89.1.tar.gz",
        "https://github.com/ocornut/imgui/archive/refs/tags/v1.89.1.tar.gz",
    ],
)

It's common to have a single-entry `urls` list, you don't need to duplicate these. 

and

http_archive(
    name = "glfw",
    build_file = "//third_party/glfw:glfw.BUILD.bazel",
    sha256 = "f30f42e05f11e5fc62483e513b0488d5bceeab7d9c5da0ffe2252ad81816c713",
    strip_prefix = "glfw-3.3.8",
    urls = [
        "https://github.com/glfw/glfw/archive/refs/tags/3.3.8.tar.gz",
        "https://github.com/glfw/glfw/archive/refs/tags/3.3.8.tar.gz",
    ],
)


Neither dependency uses Bazel, so I added build overlays under //third_party/{glfw,imgui}.  As I understand it, when, say, imgui, is built, the source is fetched and the file //third_party/imgui:imgui.BUILD.bazel is copied to the root of the source.   If I need to depend on imgui, I use the label @imgui rather that //third_party/imgui since the build file under third_party is not actually visible.  Is that right?

That's mostly correct. Technically you can still use //third_party/imgui:imgui.BUILD.bazel to refer to the file so it's not fully hidden, but nothing will use it as a BUILD file.
 
Now, I wanted things to be a little bit prettier in my code so I added another build file //third_party/imgui:BUILD.bazel that aliases the imgui build rule so that I can use the label //third_party/imgui in my code:

alias(
    name = "imgui",
    actual = "@imgui",
)


All good so far.

Question 1: I need to add a dependency on glfw to imgui.  I found that I could only do that via @imgui rather than //third_party/imgui.  //third_party/imgui works in the code that I'm writing though.  It is a minor nit, but I wonder why the discrepancy?

//third_party/imgui from the glfw repository refers to @glfw//third_party/imgui. @//third_party/imgui refers to the one in the main repository from anywhere.
 
Question 2: Is there a way to only have one build file and still reference imgui as //third_party/imgui without the use of the alias rule?

No. You could use macros and repository rules to hide the alias rule, but it's still going to be somewhere.

Ajith Ramanathan

unread,
Jan 5, 2023, 4:12:28 PM1/5/23
to Brian Silverman, bazel-discuss
Ah.  That makes things much clearer.  

Thanks for the correction and the information Brian!
Reply all
Reply to author
Forward
0 new messages