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?
In principle you need a
cc_library
rule whosehdrs
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 yourWORKSPACE
file, point the rule'spath
at the SDK's directory and write a simplebuild_file_content
for it.Example:
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 userepository_ctx.which
to find binaries on thePATH
.if you need to do registry queries, use
repository_ctx.execute
withreg.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
: