How do I reference a system library used by an external dependency if copts doesn't allow system paths?
Asked Answered
S

1

10

For Bazel, I'm pulling in an external library from within my WORKSPACE:

new_http_archive(
      name = "imgui",
      build_file = "deps/BUILD.imgui",
      sha256 = "c457fdc19b4e3aa74deccf6a2d9bc51f0d470b3acd9cc095bf04df16459d6474",
      strip_prefix = 'imgui-1.62',
      url = "https://github.com/ocornut/imgui/archive/v1.62.tar.gz",
)

Inside BUILD.imgui, I'm attempting to build it:

cc_library(
 name = "imgui_sdl_opengl3",
 linkopts = ["-ldl", "-lGL", "-L/usr/lib/x86_64-linux-gnu", "-lSDL2", "-lSDL"],
 copts = ["-Iexamples/", "-D_REENTRANT",],
 includes = [".","examples/libs/gl3w"],
 hdrs = [
     "examples/imgui_impl_opengl3.h",
     "examples/libs/gl3w/GL/gl3w.h",
     "examples/imgui_impl_sdl.h",
     "examples/libs/gl3w/GL/glcorearb.h",
 ],
 srcs = [
     "examples/imgui_impl_opengl3.cpp",
     "examples/imgui_impl_sdl.cpp",
     "examples/libs/gl3w/GL/gl3w.c",
 ],
)

The problem is that it can't find #include <SDL.h>.
I tried adding it to copts:

 copts = ["-Iexamples/", "-D_REENTRANT", "-I/usr/include/SDL"],

But the error is:

The include path '/usr/include/SDL' references a path outside of the execution root.

Ok. Same if I try to add it to the includes argument of cc_library.

I tried this other trick I saw where you make the headers visible through another repo within Bazel editing the WORKSPACE as followed:

new_local_repository(
    name = "SDL",
    path = "/usr/include/SDL",
    build_file_content = """
package(
    default_visibility = [
        "//visibility:public",
    ],
)

cc_library(
    name = "headers",
    srcs = glob(["**/*.h"]),
)
""",

The problem is, if I reference that repo as a deps to the external library I'm trying to build, I get the following error:

external/imgui/examples/imgui_impl_sdl.cpp:38:10: error: 'SDL.h' file not found with <angled> include; use "quotes" instead
#include <SDL.h>
         ^~~~~~~
         "SDL.h"

Of course I can't change the header to that, because its not my header. It comes from the external library I pulled down.

What do I do?
I don't understand why I can't add system paths to copts (probably hermetic reasons). I don't know how to include paths and have them accessible as system headers. I tried different things with -isystem as as well, but saw the same errors.

Strain answered 18/7, 2018 at 17:43 Comment(1)
Using the external repo is the right direction. What exactly have you tried with -isystem? I thought adding it to the cc_library in the build_file_contents should suffice, is that not the case? Have you tried cc_library.includes?Promptitude
S
1

As László stated, you can set the includes to the current directory which will allow it to be detected as a system header when used as a dependency in other code:

cc_library(
    ... 
    includes = [
        ".",
    ],
)
Strain answered 1/6, 2020 at 15:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.