【发布时间】:2023-01-20 03:25:00
【问题描述】:
我有一个代码 sn-p,它支持一些基于编译平台的宏。
例如,如果支持_mm_crc32_u32,则定义宏 A。
对于 cmake,check_cxx_source_compiles 似乎符合我的要求。我想知道 bazel 是否也支持类似的功能?
【问题讨论】:
-
这是你的usecase的一个例子
-
@SG_Bazel 传递标志不是问题,我想知道的是如何配置基于平台/编译器/等的选项?
我有一个代码 sn-p,它支持一些基于编译平台的宏。
例如,如果支持_mm_crc32_u32,则定义宏 A。
对于 cmake,check_cxx_source_compiles 似乎符合我的要求。我想知道 bazel 是否也支持类似的功能?
【问题讨论】:
您可以使用select、Bazel platforms和Configurable 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,
)
【讨论】: