bazel
bazel copied to clipboard
Select on host platform and execution platform
First of all, I apologize if I missed this info in the documentation.
I have a use case where I want my code-generator program to be compiled slightly differently for target platform, host platform, and execution platform by setting different defines.
This is a simplified example of what I'm trying to do. My target is aarch64, so that's straight forward. My host and execution platforms are the same x86 machine but the CODEGEN_TARGET
define value depends on whether it's building for host or execution platform.
config_setting(
name = "is_aarch64",
constraint_values = [
"@platforms//cpu:aarch64",
"@platforms//os:linux",
],
)
config_setting(
name = "is_x86_host",
constraint_values = [ <---------------------------------+
"@platforms//cpu:x86", |
"@platforms//os:linux", |
# How to define a host platform ? |
], +---- How to make the distinction between host platform and execution platform?
) |
|
config_setting( |
name = "is_x86_execution", |
constraint_values = [ <---------------------------------+
"@platforms//cpu:x86",
"@platforms//os:linux",
# How to define an execution platform ?
],
)
cc_library(
name = "codeGen",
hdrs = [
"include/codeGen.h",
],
defines = select({
":is_aarch64": ["CODEGEN_TARGET=aarch64"],
":is_x86_execution": ["CODEGEN_TARGET=aarch64"], # <----------------- how do I write a rule that applies to execution platform only?
":is_x86_host": ["CODEGEN_TARGET=x86"],
}),
includes = ["include"],
src = [
"codeGen.cc",
],
)
I'm probably approaching it the wrong way. Any guidance would be appreciated.
Hello @jasaw, Could you please provide more details with respect to the above query. Thanks
@sgowroji The question is how do I write a rule that applies to execution platform only, and another rule for host platform only? Just to clarify, bazel outputs the execution platform binaries to bazel-out/k8-opt-exec
directory, whereas the host platform binaries go to bazel-out/host
directory.
In my pseudo code above, in the cc_library
section, I want the defines
to select
different C macro based on what platform (host or execution) it is compiling it for. The problem that I'm facing is, I can't make the host/execution distinction in their respective config_setting
.
you seem to actually want to answer the question: if I'm building for execution, what is the target platform this tool will be used for.
afaik the execution platform doesn't know the target platform it's building for, the tool is assumed to be target agnostic...if you somehow need to change a tool's configuration due to the target platform you probably will need to use a transition somewhere higher up in the graph to change a config and use that as a select where you are selecting now.
you can also try to use cquery
and config
to diff the config of the tool when it's in exec and vs when it's in host, and see if you have any information available you can use to select on
@chancila Yes you're right. In this case, I need the knowledge of target platform already compiled into the tool for execution.
Your suggestion makes sense, but it's not clear to me how to define it in bazel. Would you be able to show me using the simple example above?
Thanks in advance.
I have a use case where I want my code-generator program to be compiled slightly differently for target platform, host platform, and execution platform by setting different defines.
I am confused by this: your tool (the code generator) can run on a given platform (which would be the exec), but needs to be built differently based on the target platform it will be used for?
We don't have explicit support for this case (and, honestly, it's a little confusing to me). My suggestion would be to either encode whatever changed behavior you need behind a flag, so that your rule can pass in the target-appropriate flag to the (unified) tool, or to have multiple different tools available and use a select to choose between them.
The major reason we don't encourage this use case is caching: you're going to re-built this tool for each use, instead of being able to cache it and re-use the same tool between different targets.
Closing this pending more information, feel free to re-open or re-file with more details.