0

I have a code snippet which supports a few macros based on compilation platform. For example, if _mm_crc32_u32 is supported then define macro A.

For cmake, check_cxx_source_compiles seems to fit my requirement. I'm wondering whether bazel supports the similar functionality too?

Tinyden
  • 524
  • 4
  • 13
  • This is one example for your [usecase](https://stackoverflow.com/questions/69734500/using-macros-with-bazel-build) – SG_Bazel Jan 17 '23 at 09:56
  • @SG_Bazel Passing the flag is not an issue, what I want to know is how to configure the option based on platform/compiler/etc? – Tinyden Jan 17 '23 at 23:10

1 Answers1

1

You can make use of select, Bazel platforms and Configurable Build Attributes. Here is an example how I used it do different actions depending on the underlying OS - works similar for other build attributes:

native.genrule(
    name = "%s_ispc_gen" % name,
    srcs = srcs,
    outs = [name + ".o", generted_header_filename],
    cmd = select({
        "@platforms//os:linux": "$(location @ispc_linux_x86_64//:ispc) --target=avx2 --arch=x86-64 --pic $(locations %s) --header-outfile=$(location %s) -o $(location %s.o)" % (ispc_main_source_file, generted_header_filename, name),
        "@rules_ispc//:osx_arm64": "$(location @ispc_osx_x86_64//:ispc) --target=neon --target-os=macos --arch=aarch64 --pic $(locations %s) --header-outfile=$(location %s) -o $(location %s.o)" % (ispc_main_source_file, generted_header_filename, name),
        "@rules_ispc//:osx_x86_64": "$(location @ispc_osx_x86_64//:ispc) --target=sse2 --target-os=macos --arch=x86-64 --pic $(locations %s) --header-outfile=$(location %s) -o $(location %s.o)" % (ispc_main_source_file, generted_header_filename, name),
        "@platforms//os:windows": "$(location @ispc_windows_x86_64//:ispc) --target=avx2 --target-os=windows --arch=x86-64 $(locations %s) --header-outfile=$(location %s) -o $(location %s.o)" % (ispc_main_source_file, generted_header_filename, name),
    }),
    tools = select({
        "@platforms//os:linux": ["@ispc_linux_x86_64//:ispc"],
        "@platforms//os:osx": ["@ispc_osx_x86_64//:ispc"],
        "@platforms//os:windows": ["@ispc_windows_x86_64//:ispc"],
    }),
    target_compatible_with = target_compatible_with,
)
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90