abseil / abseil-cpp

Abseil Common Libraries (C++)
https://abseil.io
Apache License 2.0
14.99k stars 2.62k forks source link

[Bug]: Compilation failure due to missing bazel platforms support #1573

Open gfrankliu opened 11 months ago

gfrankliu commented 11 months ago

Describe the issue

bazel is migrating to use the new "platforms" to support multi architectures: https://bazel.build/concepts/platforms

As more and more people start to use the new bazel and the platforms rules, we will see more compilation errors such as: https://github.com/abseil/abseil-cpp/issues/1210 https://github.com/abseil/abseil-cpp/issues/1227

Can abseil update to support that?

Steps to reproduce the problem

Use bazel "platforms" to cross compile for non-x86 target, you will see the errors.

What version of Abseil are you using?

latest as of Nov 19, 2023.

What operating system and version are you using?

Host Debian 12 am64, target arm64.

What compiler and version are you using?

gcc cross compiler.

What build system are you using?

bazel 6.4.0

Additional context

No response

derekmauro commented 11 months ago

Steps to reproduce the problem

Use bazel "platforms" to cross compile for non-x86 target, you will see the errors.

I don't doubt that something doesn't work. Can you give us an example command?

gfrankliu commented 11 months ago

See my steps in https://github.com/aspect-build/gcc-toolchain/issues/155

gfrankliu commented 11 months ago

To reproduce, use the cross compiler from aspect-build gcc-toolchain which uses the new bazel platforms.

git clone https://github.com/abseil/abseil-cpp.git
cd abseil-cpp/
cat >> WORKSPACE << EOF

# aspect-build
http_archive(
    name = "aspect_gcc_toolchain",
    sha256 = "3341394b1376fb96a87ac3ca01c582f7f18e7dc5e16e8cf40880a31dd7ac0e1e",
    strip_prefix = "gcc-toolchain-0.4.2",
    url = "https://github.com/aspect-build/gcc-toolchain/archive/refs/tags/0.4.2.tar.gz",
)

load("@aspect_gcc_toolchain//toolchain:defs.bzl", "gcc_register_toolchain", "ARCHS")

gcc_register_toolchain(
    name = "gcc_toolchain_aarch64",
    sysroot_variant = "aarch64",
    target_arch = ARCHS.aarch64,
)

gcc_register_toolchain(
    name = "gcc_toolchain_x86_64",
    sysroot_variant = "x86_64",
    target_arch = ARCHS.x86_64,
)
EOF

bazel build //absl/random --incompatible_enable_cc_toolchain_resolution --platforms=@aspect_gcc_toolchain//platforms:aarch64_linux --cxxopt=-std=c++17 --host_cxxopt=-std=c++17

You should see the errors because the cpu type was not properly selected.

zhzhzoo-autra commented 9 months ago

+1 for this, the problem is in absl/copts/configure_copts.bzl

# ABSL_RANDOM_RANDEN_COPTS blaze copts flags which are required by each
# environment to build an accelerated RandenHwAes library.
ABSL_RANDOM_RANDEN_COPTS = select({
    # APPLE
    ":cpu_darwin_x86_64": ABSL_RANDOM_HWAES_X64_FLAGS,
    ":cpu_darwin": ABSL_RANDOM_HWAES_X64_FLAGS,
    ":cpu_x64_windows_msvc": ABSL_RANDOM_HWAES_MSVC_X64_FLAGS,
    ":cpu_x64_windows": ABSL_RANDOM_HWAES_MSVC_X64_FLAGS,
    ":cpu_k8": ABSL_RANDOM_HWAES_X64_FLAGS,
    ":cpu_ppc": ["-mcrypto"],
    ":cpu_aarch64": ABSL_RANDOM_HWAES_ARM64_FLAGS,

    # Supported by default or unsupported.
    "//conditions:default": [], 
})

if using platforms this should be written:

select({
    "@platforms//cpu:k8": ABSL_RANDOM_HWAES_X64_FLAGS,
    "@platforms//cpu:ppc": ["-mcrypto"],
    "@platforms//cpu:aarch64": ABSL_RANDOM_HWAES_ARM64_FLAGS,
    # blabla...

    # Supported by default or unsupported.
    "//conditions:default": [], 
})
zhzhzoo-autra commented 9 months ago

See https://bazel.build/concepts/platforms#select

derekmauro commented 9 months ago

+1 for this, the problem is in absl/copts/configure_copts.bzl

# ABSL_RANDOM_RANDEN_COPTS blaze copts flags which are required by each
# environment to build an accelerated RandenHwAes library.
ABSL_RANDOM_RANDEN_COPTS = select({
    # APPLE
    ":cpu_darwin_x86_64": ABSL_RANDOM_HWAES_X64_FLAGS,
    ":cpu_darwin": ABSL_RANDOM_HWAES_X64_FLAGS,
    ":cpu_x64_windows_msvc": ABSL_RANDOM_HWAES_MSVC_X64_FLAGS,
    ":cpu_x64_windows": ABSL_RANDOM_HWAES_MSVC_X64_FLAGS,
    ":cpu_k8": ABSL_RANDOM_HWAES_X64_FLAGS,
    ":cpu_ppc": ["-mcrypto"],
    ":cpu_aarch64": ABSL_RANDOM_HWAES_ARM64_FLAGS,

    # Supported by default or unsupported.
    "//conditions:default": [], 
})

if using platforms this should be written:

select({
    "@platforms//cpu:k8": ABSL_RANDOM_HWAES_X64_FLAGS,
    "@platforms//cpu:ppc": ["-mcrypto"],
    "@platforms//cpu:aarch64": ABSL_RANDOM_HWAES_ARM64_FLAGS,
    # blabla...

    # Supported by default or unsupported.
    "//conditions:default": [], 
})

The proposed solution doesn't support MSVC. It makes the assumption that all compilers for that CPU support the GCC-style flags.

fmeum commented 9 months ago

The correct solution is to use the settings in this file instead of OS settings (plus @platforms//cpu constraints if necessary): https://github.com/bazelbuild/rules_cc/blob/main/cc/compiler/BUILD

derekmauro commented 9 months ago

The correct solution is to use the settings in this file instead of OS settings (plus @platforms//cpu constraints if necessary): https://github.com/bazelbuild/rules_cc/blob/main/cc/compiler/BUILD

Yes, that is part of the solution. How do I get the cross product of compiler and cpu?

fmeum commented 9 months ago

Yes, that is part of the solution. How do I get the cross product of compiler and cpu?

One option would be to use skylib's selects.config_setting_group to define selectable targets for each combination in the cross product.

1e100 commented 6 months ago

Yeah, nobody can build absl-cpp with a recent Bazel without this. And nobody could for quite a while. Is this thing even alive?