Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bazel environment variables in build rules

I want to refer to a DirectX SDK in the BUILD file. The problem is that (as far as I understand) Bazel supports passing environment variables only through --action_env=DXSDK_DIR argument for Bazel and it is meant to be used in actions, which must be defined in a plugin (.bzl file).

Is there any easier way to refer to the environment variable by using it as Make variable (includes = [ "$(DXSDK_DIR)/Include" ]) or do I need to write a plugin?

like image 553
shybovycha Avatar asked Apr 25 '18 03:04

shybovycha


1 Answers

In principle you need a cc_library rule whose hdrs attribute globs the DirectX headers. For that you need to pretend that the DX SDK is part of your source tree. Bazel offers "repository rules" for that purpose.

1. Create a repository rule for the DirectX SDK

Depending on whether the SDK's location is known or needs to be discovered, you have two options.

a. Fixed SDK location

You can use this approach if you don't need to read any environment variables, run any binaries, or query the registry to find where the SDK is. This is the case if everyone who builds your rules will install the SDK to the same location.

Just add a new_local_repository rule to your WORKSPACE file, point the rule's path at the SDK's directory and write a simple build_file_content for it.

Example:

new_local_repository(
    name = "directx_sdk",
    path = "c:/program files/directx/sdk/includes",
    build_file_contents = """
cc_library(
    name = "sdk",
    hdrs = glob(["**/*.h"]),
    visibility = ["//visibility:public"],
)
""")

This rule creates the @directx_sdk repository with one rule in its root package, @directx_sdk//:sdk.

b. SDK discovery

You need to follow this approach if you need to read environment variables, run binaries, or query the registry to find where the SDK is.

Instead of using a new_local_repository rule, you need to implement your own. More info and examples are here.

Key points:

  • if your repository rule needs to read environment variables, add them to the list repository_rule(environ), e.g. repository_rule(..., environ = ["DXSDK_DIR"])

  • if you need to run some binaries that tell you where the SDK is, use repository_ctx.execute. You can use repository_ctx.which to find binaries on the PATH.

  • if you need to do registry queries, use repository_ctx.execute with reg.exe /query <args>

2. Depend on the SDK's cc_library

In your project, just depend on the SDK's library as if it was an ordinary cc_library:

cc_library(
    name = "render",
    ...
    deps = [
        ...
        "@directx_sdk//:sdk",
    ],
)
like image 191
László Avatar answered Sep 21 '22 06:09

László