Bazel environment variables in build rules

邮差的信 提交于 2019-12-03 11:19:46

问题


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?


回答1:


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",
    ],
)


来源:https://stackoverflow.com/questions/50013431/bazel-environment-variables-in-build-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!