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
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.
Depending on whether the SDK's location is known or needs to be discovered, you have two options.
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
.
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>
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",
],
)