Bazel environment variables in build rules

后端 未结 1 1284
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 21:27

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 --a

相关标签:
1条回答
  • 2021-02-04 22:08

    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",
        ],
    )
    
    0 讨论(0)
提交回复
热议问题