conditional binding in WORKSPACE

1,285 views
Skip to first unread message

Diego

unread,
May 4, 2017, 11:00:00 AM5/4/17
to bazel-discuss
To fit some of my use cases for multiplatform, I wanted to do something like this in my WORKSPACE:

if linux then

bind(
    name = "freeimage",
    actual = "@linux_freeimage//:freeimage",
)

else if win32 then

bind(
    name = "freeimage",
    actual = "@win32_freeimage//:freeimage",
)

end if


Is there a way to detect the current OS/platform and to do such conditional binding?

Thanks!

--
https://github.com/thinlizzy/ - C++11 and C++14 open source wrappers and libraries for various purposes (XML, image, webserver... and even a MTG implementation)
http://baudejogos.net - o único site brasileiro decente de jogos

John Cater

unread,
May 4, 2017, 11:11:00 AM5/4/17
to Diego, bazel-discuss
Currently, there's nothing you can do in the WORKSPACE, but you could define freeimage_linux and freeimage_windows, and use config_setting and select to choose between them in your BUILD files.

In the near future, I'm working on implementing https://github.com/bazelbuild/bazel/issues/2219, which will get us closer to what you want (but not all the way there).

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAA5%3Di_HTKNhi%2Bz4XMJ7FOPt5eny8jXxSmFw_b6w4aV6OsaFLmA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Diego

unread,
May 5, 2017, 8:46:11 AM5/5/17
to John Cater, bazel-discuss
I gonna wait for your implementation then. I don't feel comfortable polluting all my BUILD files with select() statements. That definitely does not scale when more platforms show up.
As a workaround I can just comment/uncomment my WORKSPACE files to change my external bindings when it is time to compile to another platform. Or I can have separate workspace files (WORKSPACE.windows, WORKSPACE.linux, etc) and then copy them over when I have to rebuild the sources.


On Thu, May 4, 2017 at 11:10 AM, John Cater <jca...@google.com> wrote:
Currently, there's nothing you can do in the WORKSPACE, but you could define freeimage_linux and freeimage_windows, and use config_setting and select to choose between them in your BUILD files.

In the near future, I'm working on implementing https://github.com/bazelbuild/bazel/issues/2219, which will get us closer to what you want (but not all the way there).

On Thu, May 4, 2017 at 11:00 AM Diego <jose....@gmail.com> wrote:
To fit some of my use cases for multiplatform, I wanted to do something like this in my WORKSPACE:

if linux then

bind(
    name = "freeimage",
    actual = "@linux_freeimage//:freeimage",
)

else if win32 then

bind(
    name = "freeimage",
    actual = "@win32_freeimage//:freeimage",
)

end if


Is there a way to detect the current OS/platform and to do such conditional binding?

Thanks!

--
https://github.com/thinlizzy/ - C++11 and C++14 open source wrappers and libraries for various purposes (XML, image, webserver... and even a MTG implementation)
http://baudejogos.net - o único site brasileiro decente de jogos

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.

Brian Silverman

unread,
May 6, 2017, 11:11:12 PM5/6/17
to Diego, John Cater, bazel-discuss
You could also create a filegroup/cc_library/whatever for freeimage which has deps/srcs/whatever as a select that gets the one for the current platform. I've been doing that for various things and it works fine.

Diego

unread,
May 6, 2017, 11:39:21 PM5/6/17
to Brian Silverman, bazel-discuss, John Cater
If I can do it in the WORKSPACE, that'll be awesome. Do you have an example of that?

Thanks!

Diego

unread,
Jun 1, 2017, 12:10:28 AM6/1/17
to Brian Silverman, bazel-discuss, John Cater
hello? :)

Marcel Hlopko

unread,
Jun 1, 2017, 4:29:20 AM6/1/17
to Diego, Brian Silverman, bazel-discuss, John Cater
Hi Diego,

What Brian suggested is using filegroup in the BUILD files that fill hide the select, and other rules will only depend on the filegroup. John's platforms work is in progress but will take some time, and I'm not sure it will be sufficient to what you'd want.

Cheers,

hello? :)

To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.



--
https://github.com/thinlizzy/ - C++11 and C++14 open source wrappers and libraries for various purposes (XML, image, webserver... and even a MTG implementation)
http://baudejogos.net - o único site brasileiro decente de jogos

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.



--
https://github.com/thinlizzy/ - C++11 and C++14 open source wrappers and libraries for various purposes (XML, image, webserver... and even a MTG implementation)
http://baudejogos.net - o único site brasileiro decente de jogos

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAA5%3Di_GRxtcH6UPegtik8_GN%3DKUmg1fqq7%2BWfOW39SJ_GMXUdA%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.
--
-- 
Marcel Hlopko | Software Engineer | hlo...@google.com | 

Google Germany GmbH | Erika-Mann-Str. 33  | 80636 München | Germany | Geschäftsführer: Matthew Scott Sucherman, Paul Terence Manicle | Registergericht und -nummer: Hamburg, HRB 86891

abergmeier...@gmail.com

unread,
Jun 8, 2017, 4:44:09 AM6/8/17
to bazel-discuss, jca...@google.com, jose....@gmail.com
That is basically the bullet we had to bite. We have a whole bunch of aliases in our root BUILD file :(
> To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.

Diego

unread,
Sep 3, 2017, 3:52:19 PM9/3/17
to abergmeier...@gmail.com, bazel-discuss, John Cater
ressuscitating this thread, I tried to do something in Bazel for Windows, but the config_setting did not detect the windows crosstool:

config_setting(
    name = "windows",
    values = {
        "crosstool_top": "//crosstools/windows",
    },
)

cc_library(
name = "die-tk",
deps = select({
":windows": [":die-tk-base",":die-tk-win32"],
"//conditions:default": [":die-tk-base",":die-tk-linux"],
}),
)

I've pasted the config_setting example from https://docs.bazel.build/versions/master/be/functions.html#select_example . Is that example accurate and/or did I miss something?

Thanks!
http://baudejogos.net - o único site brasileiro decente de jogos!!

Dmitry Lomov

unread,
Sep 4, 2017, 8:21:01 AM9/4/17
to Diego, abergmeier...@gmail.com, bazel-discuss, John Cater
On Sun, Sep 3, 2017 at 9:52 PM, Diego <jose....@gmail.com> wrote:
ressuscitating this thread, I tried to do something in Bazel for Windows, but the config_setting did not detect the windows crosstool:

config_setting(
    name = "windows",
    values = {
        "crosstool_top": "//crosstools/windows",
    },
)

cc_library(
name = "die-tk",
deps = select({
":windows": [":die-tk-base",":die-tk-win32"],
"//conditions:default": [":die-tk-base",":die-tk-linux"],
}),
)

I've pasted the config_setting example from https://docs.bazel.build/versions/master/be/functions.html#select_example . Is that example accurate and/or did I miss something?

This is not a good example - sorry! Here is how you detect Windows:

config_setting(
   name = "windows_msvc",
   values = {"cpu": "x64_windows_msvc"},
}


Thanks!
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAA5%3Di_FzbtDHn%3Ded0XeQwPHKeqf_rdomi8jeGZP4GKNrF-2Hbg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Google Germany GmbH
Erika-Mann-Straße 33, 80636 München, Germany

Diego

unread,
Sep 4, 2017, 1:35:14 PM9/4/17
to Dmitry Lomov, abergmeier...@gmail.com, bazel-discuss, John Cater
it still did not work. is there a way to query the "current cpu" or crosstool used?

thanks!

On Mon, Sep 4, 2017 at 8:20 AM, Dmitry Lomov <dsl...@google.com> wrote:
On Sun, Sep 3, 2017 at 9:52 PM, Diego <jose....@gmail.com> wrote:
ressuscitating this thread, I tried to do something in Bazel for Windows, but the config_setting did not detect the windows crosstool:

config_setting(
    name = "windows",
    values = {
        "crosstool_top": "//crosstools/windows",
    },
)

cc_library(
name = "die-tk",
deps = select({
":windows": [":die-tk-base",":die-tk-win32"],
"//conditions:default": [":die-tk-base",":die-tk-linux"],
}),
)

I've pasted the config_setting example from https://docs.bazel.build/versions/master/be/functions.html#select_example . Is that example accurate and/or did I miss something?

This is not a good example - sorry! Here is how you detect Windows:

config_setting(
   name = "windows_msvc",
   values = {"cpu": "x64_windows_msvc"},
}


Thanks!

Diego

unread,
Nov 27, 2017, 11:24:33 PM11/27/17
to Dmitry Lomov, abergmeier...@gmail.com, bazel-discuss, John Cater
FYI, my current workaround was to rebuild my WORKSPACE file every time when I want to build to a different platform.
My repository has the following set of files: WORKSPACE.common, WORKSPACE.win32, WORKSPACE.linux. The common file has the configs/dependencies/repositories that are platform independent. Both win32 and linux files have their respective dependencies and then they use bind() to normalize the names of the repositories.

For example, to work with win32 builds, I first issue the following command: COPY WORKSPACE.common+WORKSPACE.win32 WORKSPACE

I think regenerating the WORKSPACE file is a good compromise to keep my BUILD files free of that select() hassle and I no longer need to worry about to make config_setting() to work.

What do you think?


László Csomor

unread,
Dec 7, 2017, 6:47:01 AM12/7/17
to Diego, Dmitry Lomov, abergmeier...@gmail.com, bazel-discuss, John Cater
+dslomov: ping



--
László Csomor | Software Engineer | laszlo...@google.com


Google Germany GmbH | Erika-Mann-Str. 33 | 80636 München | Germany
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg
Geschäftsführer: Paul Manicle, Halimah DeLaine Prado

Diego

unread,
Feb 19, 2018, 1:57:36 AM2/19/18
to László Csomor, Dmitry Lomov, abergmeier...@gmail.com, bazel-discuss, John Cater
Hello, I've noticed that issue  2219 has been closed. Does it mean that I can do what I originally wanted now? Or do we have something similar, at least?

Thanks!

Dmitry Lomov

unread,
Feb 19, 2018, 10:11:00 AM2/19/18
to Diego, László Csomor, abergmeier...@gmail.com, bazel-discuss, John Cater
Toolchain information is still not available in WORKSPACE file, not will it be available for the foreseeable future. 

WORKSPACE file processing happens conceptually very early - even before any BUILD files are loaded.

We very much recommend *not* to overwrite WORKSPACE files and instead use one WORKSPACE file to define toolchains for all platforms and then make use of toolchain resolution mechanism in BUILD files. 



For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages