Can't use make variables in cc_library hdrs
Attempts to substitute make variables in a label, such as cc_library's hdrs, will result in an error ('target names may not contain '$'). While this is not a bug per se it would be very useful to be able to predicate file paths on $(TARGET_CPU) (e.g. 'arch/$(TARGET_CPU)/include/arch-variant-include.h') without the use of select().
Hello can you paste here the BUILD file part concerned? Le mar. 14 avr. 2015 à 01:47, kayasoze [email protected] a écrit :
Attempts to substitute make variables in a label, such as cc_library's hdrs, will result in an error ('target names may not contain '$'). While this is not a bug per se it would be very useful to be able to predicate file paths on $(TARGET_CPU) (e.g. 'arch/$(TARGET_CPU)/include/arch-variant-include.h') without the use of select().
— Reply to this email directly or view it on GitHub https://github.com/google/bazel/issues/120.
Sorry my bad misread you. Le mar. 14 avr. 2015 à 08:19, Damien Martin-guillerez [email protected] a écrit :
Hello can you paste here the BUILD file part concerned? Le mar. 14 avr. 2015 à 01:47, kayasoze [email protected] a écrit :
Attempts to substitute make variables in a label, such as cc_library's hdrs, will result in an error ('target names may not contain '$'). While this is not a bug per se it would be very useful to be able to predicate file paths on $(TARGET_CPU) (e.g. 'arch/$(TARGET_CPU)/include/arch-variant-include.h') without the use of select().
— Reply to this email directly or view it on GitHub https://github.com/google/bazel/issues/120.
You will need to use select() for that: http://bazel.io/docs/build-encyclopedia.html#select
On Tue, Apr 14, 2015 at 8:20 AM, Damien Martin-Guillerez < [email protected]> wrote:
Sorry my bad misread you. Le mar. 14 avr. 2015 à 08:19, Damien Martin-guillerez <[email protected]
a écrit :
Hello can you paste here the BUILD file part concerned? Le mar. 14 avr. 2015 à 01:47, kayasoze [email protected] a écrit :
Attempts to substitute make variables in a label, such as cc_library's hdrs, will result in an error ('target names may not contain '$'). While this is not a bug per se it would be very useful to be able to predicate file paths on $(TARGET_CPU) (e.g. 'arch/$(TARGET_CPU)/include/arch-variant-include.h') without the use of select().
— Reply to this email directly or view it on GitHub https://github.com/google/bazel/issues/120.
— Reply to this email directly or view it on GitHub https://github.com/google/bazel/issues/120#issuecomment-92636766.
It feels a bit arbitrary: in some cases, I can use make variable substitution, but in others I must use select(), e.g. for pathname generation.
I'd like to encapsulate all of the platform specific path generation in one place for use in various attributes, like copts and includes, but because I'd like to use a mere path (for string concatenations) file groups do not suffice. What is the best practice here? Are the creation of string objects, and their concatenation, a supported mechanism in BUILD files? select() does not seem compatible with string concatenation: unsupported operand type(s) for +: 'select' and 'string'.
We have a love/hate relationship with make variables, so it's not that surprising. ;-)
On Tue, Apr 14, 2015 at 6:41 PM, kayasoze [email protected] wrote:
It feels a bit arbitrary: in some cases, I can use make variable substitution, but in others I must use select(), e.g. for pathname generation.
I'd like to encapsulate all of the platform specific path generation in one place for use in various attributes, like copts and includes, but because I'd like to use a mere path (for string concatenations) file groups do not suffice. What is the best practice here? Are the creation of string objects, and their concatenation, a supported mechanism in BUILD files? select() does not seem compatible with string concatenation: unsupported operand type(s) for +: 'select' and 'string'.
String concatenation is generally supported, but not in combination with select() - this is due to how our code is structured, and because select() is a fairly recent addition. We have an upcoming change that allows some combinations with select(), but I'm not sure if it allows string concatenation. It certainly allows list concatenation.
It might help if you could give an example of what you're trying to do and what you'd like it to look like.
— Reply to this email directly or view it on GitHub https://github.com/google/bazel/issues/120#issuecomment-92969072.
In the example below, I'd like to use hdrs to declare a set of header files which are predicated on the toolchain I'm currently using (here, C_COMPILER is used as a proxy for the toolchain, but it could well be TARGET_CPU or ABI). However, the use of select() for hdrs creates a lot of redundancy, as for each toolchain (of which there are many), I must include the same list of header files, differing only by path prefix. It would be nice if hdrs allowed make variable substitution, or some other mechanism was provided to generate a label that included a make variable.
cc_library(
name = "gflags",
visibility = ["//visibility:public"],
# hdrs = [
# "generated/$(C_COMPILER)/include/foo.h",
# "generated/$(C_COMPILER)/include/bar.h"
# ],
includes = ["generated/$(C_COMPILER)/include"],
srcs = [
"src/gflags.cc",
"src/gflags_completions.cc",
"src/gflags_reporting.cc",
],
copts = [
"-I%s/generated/$(C_COMPILER)/private_include" % PACKAGE_NAME,
"-I%s/generated/$(C_COMPILER)/include/gflags" % PACKAGE_NAME
],
linkopts = select({
":darwin": [],
"//conditions:default": ["-pthread"],
}),
)
Are the 'generated/$(C_COMPILER)/include/...' files checked in?
If not, you don't need to write your rules like that - blaze will automatically put all generated files into a directory that has a prefix depending on the compiler (or C++ toolchain in general), so it can build any number of these.
If yes, then you can combine select either with glob() or, if you don't like glob, with list comprehensions.
On Mon, Apr 20, 2015 at 11:40 PM, kayasoze [email protected] wrote:
In the example below, I'd like to use hdrs to declare a set of header files which are predicated on the toolchain I'm currently using (here, C_COMPILER is used as a proxy for the toolchain, but it could well be TARGET_CPU or ABI). However, the use of select() for hdrs creates a lot of redundancy, as for each toolchain (of which there are many), I must include the same list of header files, differing only by path prefix. It would be nice if hdrs allowed make variable substitution, or some other mechanism was provided to generate a label that included a make variable.
cc_library( name = "gflags", visibility = ["//visibility:public"],
hdrs = [
"generated/$(C_COMPILER)/include/foo.h",
"generated/$(C_COMPILER)/include/bar.h"
],
includes = ["generated/$(C_COMPILER)/include"], srcs = [ "src/gflags.cc", "src/gflags_completions.cc", "src/gflags_reporting.cc", ], copts = [ "-I%s/generated/$(C_COMPILER)/private_include" % PACKAGE_NAME, "-I%s/generated/$(C_COMPILER)/include/gflags" % PACKAGE_NAME ], linkopts = select({ ":darwin": [], "//conditions:default": ["-pthread"], }), )
— Reply to this email directly or view it on GitHub https://github.com/google/bazel/issues/120#issuecomment-94576054.
Closing as obsolete. Please ping if it is still relevant
Unfortunately the include files are checked in. They're generated by configure manually. I could generate them at build time, but I was trying to convert various dependencies into pure Bazel, with no autotools-based steps.
I could use select(), but it would result in code which manually duplicated C_COMPILER, which seems a bit silly.
We don't currently plan to support a shorter syntax than select(). I'm not sure this is common enough to warrant special support from Bazel.