The documentation (https://docs.bazel.build/configurable-attributes.html) offers the following example, which sadly doesn't work:
cc_library(
name = "my_lib",
deps = select(
{
"//tools/cc_target_os:android": [":android_deps"],
"//tools/cc_target_os:windows": [":windows_deps"],
},
no_match_error = "Please build with an Android or Windows toolchain",
),
)
Matchers such as "#platforms//os:macos" and "#platforms//os:windows" sadly only detect HOST platform but not TARGET platform. This breaks when cross-compiling on a different architecture.
I came up with an "android" matcher that works:
config_setting(
name = "android",
values = {"crosstool_top": "//external:android/crosstool"},
)
But cannot figure out a way to match windows, macos or linux TARGET toolchains.
Thanks!
I think that you are looking for platforms: https://docs.bazel.build/versions/master/platforms.html and the target_compatible_with attribute on cc_library.
I would suggest you want something like:
cc_library(
name = "my_lib_android",
deps = [":android_deps"],
target_compatible_with = [
"#platforms//os:android",
],
)
cc_library(
name = "my_lib_windows",
deps = [":windows_deps"],
target_compatible_with = [
"#platforms//os:windows",
],
)
The user of my_lib would then have to depend on either my_lib_android or my_lib_windows as appropriate.
Related
I have a Bazel build file that selects what files and defines to include in my library based on 2 possible backends (A and B). I use select to do this, but I also want a default behavior to let another variable set them (for example, platform). Since this would require nested selects, I can workaround it by having an intermediate filegroup target for the default behavior (platform based). But I'm not sure what to do about the defines which are just a list of strings. This is an example of how I configured the BUILD file:
filegroup(
name = "_backend_based_on_platform_srcs",
srcs = select({
"//darwin": _backend_a_srcs,
"//linux": _backend_b_srcs,
})
)
filegroup(
name = "headers",
srcs = glob([
"include/common/*.hpp",
]) + select({
"//backend_a": _backend_a_srcs,
"//backend_b": _backend_b_srcs,
"//backend_default": [":_backend_based_on_platform_srcs"],
}),
)
cc_library(
name = "custom_lib",
hdrs = [
":headers",
],
defines = select({
"//backend_a": _backend_a_defines,
"//backend_b": _backend_b_defines,
#"//backend_default": [":_backend_based_on_platform_defines"],
}),
includes = ["include"],
strip_include_prefix = "include",
visibility = ["//visibility:public"],
)
I'd like to have an intermediate target for the defines so that I can put a select in it, but I don't know if that exists. I tried using alias, but that can only take a string and not a list of strings (multiple define constants). Is there something else I can try to get this to work?
I'd like to do something like:
srcs = glob(["*.proto"]) - ["some.proto"],
That particular syntax isn't valid in Skylark. How do I go about accomplishing a list diff in Skylark?
glob provides a exclude attribute, e.g.:
glob(
[
".editorconfig",
".gitattributes",
"third_party/eigen-*/**",
],
exclude = ["devertexwahn/flatland/copy.bara.sky"],
),
I have a genrule that looks like the below. It basically runs a simple go template tool that gets a resource name, a json file and template and outputs the rendered file. I have a bunch of resources that all need to be in separate files and ultimately packaged into a container.
resources = ["foo", "bar"]
[genrule(
name = "generate_" + resource + "_config",
srcs = [
"//some:tool:config.tmpl",
"//some:json",
],
outs = [resource + ".hcl"],
cmd = "$(location //some/tool:template) -resource " + resource + " -json_path=$(location //some:json) -template=$(location //some/tool:config.tmpl) -out=$#",
tools = [
"/some/tool:template",
],
) for resource in resources]
The above will generate a few rules named generate_foo_config and generate_bar_config and the files output correctly. I however cannot figure out how to use each one without specifying them directly in a filegroup or pkg_tar rule without enumerating each one. I would like to be able to add a new thing to the resources variable, and have it automatically included in the filegroup or tar for use in a build rule later. Is this possible?
Use a list comprehension, like you've got creating the genrules. Something like this:
pkg_tar(
name = "all_resources",
srcs = [":generate_" + resource + "_config" for resource in resources],
)
You can also put the list in a variable in the BUILD file to use it multiple times:
all_resources = [":generate_" + resource + "_config" for resource in resources]
pkg_tar(
name = "all_resources",
srcs = all_resources,
)
filegroup(
name = "resources_filegroup",
srcs = all_resources,
)
I am trying to specify build conditions based on the os I'm running bazel from, so in my .bzl script I have a rule that makes all the simlinks from external sources and writes a BUILD file (with ctx.file), in which I'm declaring all the imports and libraries and in those I would like to add the select function. However, when I build I get this error message:
ERROR: no such package '#maya_repo//': Traceback (most recent call last):
File "/var/tmp/doNotRemove/mdilena_plugins/MayaMathNodes/src/maya.bzl", line 149
ctx.file("BUILD", _BUILD_STRUC.format(maya_...))
File "/var/tmp/doNotRemove/mdilena_plugins/MayaMathNodes/src/maya.bzl", line 149, in ctx.file
_BUILD_STRUC.format(maya_dir = maya_dir)
Invalid character '[' inside replacement field
so here's an example of my code and what I'm trying to achieve:
_BUILD_STRUC = \
"""
# Windows imports
cc_import(
name = "Foundation-win",
interface_library = "{maya_dir}/lib/Foundation.lib",
shared_library = "{maya_dir}/bin/Foundation.dll",
)
cc_import(
name = "OpenMaya-win",
interface_library = "{maya_dir}/lib/OpenMaya.lib",
shared_library = "{maya_dir}/bin/OpenMaya.dll",
)
# Linux imports
cc_import(
name = "Foundation-lnx",
shared_library = "{maya_dir}/bin/Foundation.so",
)
cc_import(
name = "OpenMaya-lnx",
shared_library = "{maya_dir}/bin/OpenMaya.so",
)
cc_library(
name = "Foundation",
deps = select({
"#bazel_tools//src/conditions:windows": [":Foundation-win"],
"//conditions:default": [":Foundation-lnx"],
}),
includes = ["{maya_dir}/include"],
visibility = ["//visibility:public"],
)
cc_library(
name = "OpenMaya",
deps = select({
"#bazel_tools//src/conditions:windows": [":OpenMaya-win"],
"//conditions:default": [":OpenMaya-lnx"],
}),
includes = ["{maya_dir}/include"],
visibility = ["//visibility:public"],
)
"""
def _impl(ctx):
maya_src = ctx.os.environ["MAYA_LOCATION"]
maya_ver = ctx.os.environ["MAYA_VERSION"]
maya_dir = "maya{}".format(maya_ver)
ctx.symlink(maya_src, maya_dir)
ctx.file("BUILD", _BUILD_STRUC.format(maya_dir=maya_dir))
link_maya = repository_rule(
implementation = _impl,
local = True,
environ = ["MAYA_LOCATION"],
)
does anyone have any idea why this is happening? I looked at select and configurable attributes docs and seems like that's the way to use it; I wonder if it's me doing something wrong or if there's a bug somewhere.
Thanks for any help!
EDIT:
looks like Bazel really doesn't like using select inside a ctx.file,
I'll leave the question open in case someone will be able to shed some
light on it. In the meantime I solved it by making all the cc_imports
and includes public from the linked repo, while leaving all the
cc_libraries with select to my plugin's BUILD file; from there I'm
able to use the condition and everything builds.
It looks like the error is coming from this line, specifically the call to string.format.
ctx.file("BUILD", _BUILD_STRUC.format(maya_dir=maya_dir))
string.format searches the template string for curly braces like {} or {key} and replaces them with positional or keyword arguments.
You're seeing this error because string.format is mistaking the dict argument to select within the template as something to replace because it starts with a curly brace. Escaping the braces within the template string by doubling them should fix the problem:
_BUILD_STRUC = \
"""
...
cc_library(
name = "Foundation",
deps = select({{
"#bazel_tools//src/conditions:windows": [":Foundation-win"],
"//conditions:default": [":Foundation-lnx"],
}}),
includes = ["{maya_dir}/include"],
visibility = ["//visibility:public"],
)
...
FYI, you might find repository_ctx.template easier to work with. It has slightly different semantics: it replaces strings literally, without looking for special characters like {, so escaping is not needed.
My android project contains some aar modules, which have their own AndroidManifest.xml. What should I do to have the aar's manifest to be merged into the final AndroidManifest.xml?
Thanks very much for any help!
My android_binary rule:
android_binary(
name="apk",
custom_package = "com.xtbc",
manifest_merger = "android",
manifest = "AndroidManifest.xml",
resource_files = glob(["res/**"], exclude=["res/.DS_Store"]),
assets = glob(["assets/**"], exclude=["assets/.DS_Store"]),
assets_dir = "assets",
multidex = "manual_main_dex",
main_dex_list = "mainDexList.txt",
dexopts = [
"--force-jumbo"
],
deps = [
":lib",
":base_lib",
":jni"
]
)
The :base_lib is a module (ie, an android_library rule):
android_library(
name = "base_lib",
srcs = glob(["base/src/**/*.java"]),
custom_package = "com.xtbc.base",
manifest = "base/AndroidManifest.xml",
resource_files = glob(["base/res/**"], exclude=["base/res/.DS_Store"]),
assets = glob(["base/assets/**"], exclude=["base/assets/.DS_Store"]),
assets_dir = "base/assets",
deps = [
"#androidsdk//com.android.support:support-annotations-23.0.1"
]
)
It has its own base/AndoridManifest.xml, what I want is that the :base_lib's AndroidManifest.xml will be merged into the final AndroidManifest.xml(ie, the :apk's AndroidManifest.xml).
I do not have enough stackoverflow reputation to respond to the comment chain, but it sounds like what you are after is the exports_manifest attribute of android_library.
The documentation at https://bazel.build/versions/master/docs/be/android.html#android_library.exports_manifest says that the default is 1, however, that documentation is based on source code changes that have not made it into a Bazel release yet. For now you will need to add exports_manifest = 1 onto your android_library. In the next Bazel release, this will no longer be necessary.
Also, regarding "AAR modules": If these are prebuilt .aar files, you will want to use the aar_import rule. It does not have an exports_manifest attribute, because it will always export by default. If these are Gradle Android library modules, then you can just use the android_library rule. If you were referring to the support libraries, #androidsdk//com.android.support:support-annotations-23.0.1 is actually a JAR, not an AAR.