18

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.

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
thinlizzy
  • 668
  • 8
  • 13
  • Please insert code You want to fix directly here – bartop Feb 19 '18 at 06:59
  • as I said in the question, I am worried about this file: https://github.com/thinlizzy/bazelexample/blob/master/demo/BUILD - how can I use select there to have only one binary target, instead of two? – thinlizzy Feb 19 '18 at 18:13

3 Answers3

32

@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.

Jin
  • 12,748
  • 3
  • 36
  • 41
0

@Vertexwahn's answer caused some confusion on my end, so I hope this answer helps clarify a bit. While his answer does not directly tie into the question, it may be of use to others trying to build on entirely different platforms without file specific inclusions.

Here is a link to where I answered that particular question: How do I specify portable build configurations for different operating systems for Bazel?

  • this is great info! maybe you can extract this comment to a self-answered question in SO and link it to here – thinlizzy Mar 14 '22 at 14:50
  • What do you think I should title the question to benefit others in finding this info for this aspect of Bazel? It has been a while since I encountered and resolved this issue. @thinlizzy – J-B-Blankenship Apr 24 '22 at 22:41
  • maybe "specifying portable build configurations in .bazelrc" ? – thinlizzy Apr 27 '22 at 13:37
-1

Add a .bazelrc to your project. Add the lines build:vs2019 --cxxopt=/std:c++14 and build:gcc --cxxopt=-std=c++14. Build your code bazel build --config=msvc //... or bazel build --config=gcc //....

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90