bazel-contrib / rules_foreign_cc

Build rules for interfacing with "foreign" (non-Bazel) build systems (CMake, configure-make, GNU Make, boost, ninja, Meson)
https://bazel-contrib.github.io/rules_foreign_cc
Apache License 2.0
680 stars 249 forks source link

Add ffmpeg example #1295

Open mering opened 1 month ago

mering commented 1 month ago

This does not build with the configured hermetic toolchain.

Instructions

(Optional) Run in container

Create container:

docker run -it --rm -v $(pwd):/work -w /work debian:12

Setup container:

apt update && apt install -y curl libxml2 python3
curl -LSs "https://github.com/bazelbuild/bazelisk/releases/download/v1.20.0/bazelisk-linux-amd64" -o "/usr/bin/bazelisk" && chmod +x "/usr/bin/bazelisk" && ln -s "/usr/bin/bazelisk" "/usr/bin/bazel"
useradd --shell /bin/bash --create-home builder
su builder

Build

cd examples/third_party
USE_BAZEL_VERSION=7.x bazelisk build --verbose_failures @ffmpeg
voxeljorge commented 1 month ago

I've also run into this, while trying to build ffmpeg with rules_foreign_cc

I'm able to get a successful build by letting bazel just select the local c compiler but ideally I would like to use a hermetic llvm + sysroot.

voxeljorge commented 1 month ago

my build fails with errors like this:

gcc: error: unrecognized command line option '--target=x86_64-unknown-linux-gnu'
gcc: error: unrecognized command line option '-fcolor-diagnostics'
gcc: error: unrecognized command line option '-Wthread-safety'
gcc: error: unrecognized command line option '-Wself-assign'

in config.log

voxeljorge commented 1 month ago

Seems like this bug is probably related: https://trac.ffmpeg.org/ticket/9310

voxeljorge commented 1 month ago

Adding "--cc=$$EXT_BUILD_ROOT/$(CC)" to configure_options fixed my build. I suspect that might work for this PR as well.

mering commented 1 month ago

Adding "--cc=$$EXT_BUILD_ROOT/$(CC)" to configure_options fixed my build. I suspect that might work for this PR as well.

@voxeljorge We have already been setting the --cc option but I like using the CC environment variable instead of the hard-coded path. I updated my example accordingly.

Unfortunately, the build still doesn't work (see PR description for instructions on how to reproduce the different errors). The error for building ffmpeg is Host compiler lacks C11 support, even when setting --host-cc=$$EXT_BUILD_ROOT/$(CC).

voxeljorge commented 1 month ago

I found that I had similar errors because not only is configure not using CC, it is also not using CFLAGS or any of the other env vars. I ended up writing a wrapper configure_wrapper which would read the env vars and set --cc, --extral-cflags etc

mering commented 1 month ago

I found that I had similar errors because not only is configure not using CC, it is also not using CFLAGS or any of the other env vars. I ended up writing a wrapper configure_wrapper which would read the env vars and set --cc, --extral-cflags etc

@voxeljorge Could you share your wrapper?

I noticed that my build was missing the --sysroot as CFLAGS. I updated my branch accordingly but the hardcoded paths are ugly. Is this available in some makevar or similar?

libvpx and libdav1d are still not building in my branch. Do these codec libraries build for you?

mering commented 1 month ago

I created two fixes for libdav1d with meson: #1302 #1303

voxeljorge commented 1 month ago

I don't build libvpx or libdav1d at all. The configure wrapper is trivial:

#!/bin/bash

# ffmpeg ignores flags like CC/CXX and CFLAGS: https://trac.ffmpeg.org/ticket/9310
# so we have a wrapper which assigns them correctly
exec "$(dirname "$0")"/configure \
    --cc="$CC" \
    --cxx="$CXX" \
    --extra-cflags="$CFLAGS" \
    --extra-cxxflags="$CXXFLAGS" \
    --extra-ldflags="$LDFLAGS" \
    "$@"

The wrapper can be installed a couple of ways, you could inject it during the repository creation time what I did was copy the sources with something like this:

load("@aspect_bazel_lib//lib:copy_to_directory.bzl", "copy_to_directory")

copy_to_directory(
    name = "src",
    srcs = [
        "configure_wrapper",
        "@ffmpeg_7_0_sources",
    ],
    include_external_repositories = ["ffmpeg_7_0_sources"],
)

configure_make(
    name = "ffmpeg",
    lib_source = "//third_party/ffmpeg:src",
    configure_command = "src/configure_wrapper",
)

I do have ffmpeg building with a limited set of codecs currently, even with a modern llvm and sysroot.