Search code examples
c++bazelbazel-cpp

How can I remove the first directory from the include path in bazel?


I have the following project structure that I am trying to move to Bazel from another build system:

MyProject/
├─ WORKSPACE.bazel
├─ app/
│  ├─ BUILD.bazel
│  ├─ main.cpp
├─ lib/
│  ├─ BUILD.bazel
│  ├─ lib1/
│  │  ├─ lib1.hpp
│  │  ├─ lib1.cpp
│  ├─ lib2/
│  │  ├─ lib2.hpp
│  │  ├─ lib2.cpp
├─ etc/
├─ test/

I have managed to get the project to compile but have had to change the include paths and the rest of my team is not happy with this, though they do enjoy the improved build performance. I'm looking to return the include paths back to what they were before, currently I have the project compiling with:

#include "lib/lib1/lib1.hpp"
#include "lib/lib2/lib2.hpp"

in main.cpp but the rest of the team really wants to go back to:

#include "lib1/lib1.hpp"
#include "lib2/lib2.hpp"

and my BUILD files to move to Bazel haven't required anything crazy either:

app/BUILD.bazel

load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
    name = "MyApp",
    srcs = ["main.cpp"],
    copts = [
        ...,
    ],
    linkopts = [
        ...,
    ],
    deps = [
        "//lib:lib1",
        "//lib:lib2",
    ],
)

lib/BUILD.bazel

load("@rules_cc//cc:defs.bzl", "cc_library")

cc_library(
    name = "lib1",
    hdrs = glob(["lib1/**/*.hpp"]),
    srcs = glob(["lib1/**/*.cpp"]),
    copts = [
        ...,
    ],
    linkopts = [
        ...,
    ],
    visibility = [
        "//app:__pkg__",
        "//test:__pkg__",
    ],
)

cc_library(
    name = "lib2",
    hdrs = glob(["lib2/**/*.hpp"]),
    srcs = glob(["lib2/**/*.cpp"]),
    copts = [
        ...,
    ],
    linkopts = [
        ...,
    ],
    visibility = [
        "//app:__pkg__",
        "//test:__pkg__",
    ],
)

Is this not possible using Bazel or is there something that I've missed?


Solution

  • Setting the includes on the library to "." worked:

    load("@rules_cc//cc:defs.bzl", "cc_library")
    
    cc_library(
        name = "lib1",
        hdrs = glob(["lib1/**/*.hpp"]),
        srcs = glob(["lib1/**/*.cpp"]),
        includes = ["."],
        copts = [
            ...,
        ],
        linkopts = [
            ...,
        ],
        visibility = [
            "//app:__pkg__",
            "//test:__pkg__",
        ],
    )
    
    cc_library(
        name = "lib2",
        hdrs = glob(["lib2/**/*.hpp"]),
        srcs = glob(["lib2/**/*.cpp"]),
        includes = ["."],
        copts = [
            ...,
        ],
        linkopts = [
            ...,
        ],
        visibility = [
            "//app:__pkg__",
            "//test:__pkg__",
        ],
    )