Recently I wanted to migrate all my projects to bazel and finally I am building them with command setting c++ standard manually for different operations systems:
bazel build //... --cxxopt=-std=c++17 --compilation_mode opt
bazel build //... --cxxopt=/std:c++17 --compilation_mode opt
I would set it in .bzl file but setting standard is different for linux and windows, so I will anyway need to override it with --cxxopt=/std:c++17 for MSVC.
I tried to add global variable to .bzl file and load it for all projects, e.g.:
# variables.bzl
COPTS = ["-std=c++17"]
This works (but as I mentioned before it will anyway require changing for different platforms). Then I've tried:
# variables.bzl
COPTS = select({
"//tools/cc_target_os:windows": ["/std:c++17"],
"//conditions:default": ["-std=c++17"],
})
But it lead to error:
ERROR: path/to/project/BUILD:2:1: no such package 'tools/cc_target_os': BUILD file not found on package path and referenced by '//project:smth'
Is it possible to set c++ flags to all projects depending on platform (compiler)? Unfortunately, I could not find any working example. Could you please help me?
Use @bazel_tools//src/conditions:windows instead.
# variables.bzl
COPTS = select({
"@bazel_tools//src/conditions:windows": ["/std:c++17"],
"//conditions:default": ["-std=c++17"],
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With