Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use select to properly detect whether I am building C++ code in Windows or Linux?

Tags:

c++

bazel

I am writing a sample C++ project that uses Bazel to serve as an example idiom for other collaborators to follow.

Here is the repository: https://github.com/thinlizzy/bazelexample

I am interested to know if I am doing it 'right', more specifically about this file: https://github.com/thinlizzy/bazelexample/blob/38cc07931e58ff5a888dd6a83456970f76d7e5b3/demo/BUILD when regarding to pick particular implementations.

cc_library(
    name = "demo",
    srcs = ["demo.cpp"],
    deps = [
        "//example:frontend",
    ],
)

cc_binary(
    name = "main_win",
    deps = [
        ":demo",
        "//example:impl_win",
    ],
)

cc_binary(
    name = "main_linux",
    deps = [
        ":demo",
        "//example:impl_linux",
    ],
)

Is this following a correct/expected idiom for Bazel projects? I am doing this way already for other projects, by concentrating all the platform-specific dependencies in separate targets and then the binaries just depend on them.

Someone in bazel-discuss list told me to use select, instead, but my attempts failed to 'detect' the operating system. I'm sure I did something wrong, but the lack of info and examples don't tell me much how to use it properly.

like image 981
thinlizzy Avatar asked Feb 19 '18 06:02

thinlizzy


1 Answers

@bazel_tools contains predefined platform conditions:

$ bazel query @bazel_tools//src/conditions:all
@bazel_tools//src/conditions:windows_msys
@bazel_tools//src/conditions:windows_msvc
@bazel_tools//src/conditions:windows
@bazel_tools//src/conditions:remote
@bazel_tools//src/conditions:host_windows_msys
@bazel_tools//src/conditions:host_windows_msvc
@bazel_tools//src/conditions:host_windows
@bazel_tools//src/conditions:freebsd
@bazel_tools//src/conditions:darwin_x86_64
@bazel_tools//src/conditions:darwin

You can use them directly in the BUILD file:

cc_library(
  name = "impl",
  srcs = ["Implementation.cpp"] + select({
    "@bazel_tools//src/conditions:windows": ["ImplementationWin.cpp"],
    "@bazel_tools//src/conditions:darwin": ["ImplementationMacOS.cpp"],
     "//conditions:default": ["ImplementationLinux.cpp"],
  }),
  # .. same for hdrs and data
)

cc_binary(
  name = "demo",
  deps = [":impl"],
)

See the documentation for select for details on the syntax.

like image 168
Jin Avatar answered Sep 29 '22 23:09

Jin