1

This is an example in my actual WORKSPACE to enable TUT (Template Unit Test) to be seen by my projects, whether I am testing them on Windows or Linux.

new_local_repository(
    name = "win32_tut",
    path = "/d/diego/progs/c++/lib/tut/",
    build_file_content = """
cc_library(
    name = "tut",
    srcs = glob([
        "tut/*.hpp",
    ]),
    hdrs = glob([
        "*.h",
    ]),
    visibility = ["//visibility:public"],
)
""",
)

new_local_repository(
    name = "linux_tut",
    path = "/usr/include/",
    build_file_content = """
cc_library(
    name = "tut",
    srcs = glob([
        "tut/*.hpp",
    ]),
    hdrs = glob([
        "tut.h",
    ]),
    visibility = ["//visibility:public"],
)
""",
)

new_local_repository(
    name = "tut",
    path = ".",
    build_file_content = """
cc_library(
    name = "tut",
    deps = select({
        "@bazel_tools//src/conditions:windows": ["@win32_tut//:tut"],
        "//conditions:default": ["@linux_tut//:tut"],
    }),
    visibility = ["//visibility:public"],
)
""",
)

All my cc_test rules are successfully depending on "@tut//:tut". That is working for me, but it looks like I am abusing the WORKSPACE file. Is there a better way to achieve this?

thinlizzy
  • 668
  • 8
  • 13

1 Answers1

1

Your solution looks good to me.

it looks like I am abusing the WORKSPACE file

Alternatively, create a custom repository rule that detects the OS [1], creates a symlink to the d:/diego/progs/c++/lib/tut or /usr/include directory, and creates a BUILD file with just one cc_library, globbing all files under the symlink.

See an example for a custom repository.


[1] e.g. repository_ctx.os.name.lower().startswith("windows")

László
  • 3,973
  • 1
  • 13
  • 26
  • I am creating a new local repo with a meaningless path only to merge the two platform specific repos. That looks like exploiting to me. – thinlizzy Apr 11 '18 at 04:02
  • your updated answer seems to be the right thing to do here, but I have no experience with skylark at all. where can I find examples of custom repository rules? thanks :) – thinlizzy Apr 21 '18 at 22:53