bazelbuild / rules_foreign_cc

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

Linker flags not getting applied #1106

Open chetgnegy opened 10 months ago

chetgnegy commented 10 months ago

I am trying to build a module from the Juce library. It builds successfully on the command line using the following

cmake . -B cmake-build -DJUCE_BUILD_EXAMPLES=ON -DJUCE_BUILD_EXTRAS=ON
cmake --build cmake-build --target AudioPluginHost

This also builds correctly (but fails because the output is a folder.

genrule(
    name = "PluginHostGen",
    outs = ["AudioPluginHost"],
    srcs = [":all_srcs"],
    cmd = """ \
        cmake ThirdParty/Juce/JUCE -B cmake-build -DJUCE_BUILD_EXAMPLES=ON -DJUCE_BUILD_EXTRAS=ON
        cmake --build cmake-build --target AudioPluginHost
    """,
)

However, this fails:

cmake (
    name = "PluginHost",
    cache_entries = {
        "JUCE_BUILD_EXAMPLES":"ON",
        "JUCE_BUILD_EXTRAS":"ON",
    },
    linkopts = [
        "-lm",
        "-ldl",
        "-lpthread",
    ],
    lib_source = ":all_srcs",
    working_directory = "JUCE",
    targets = ["AudioPluginHost"],
    out_binaries = ["PluginHost"],
)

Producing this error:

/usr/bin/ld:
  CMakeFiles/juceaide.dir/__/__/__/modules/juce_gui_basics/juce_gui_basics.cpp.o:
  undefined reference to symbol 'exp@@GLIBC_2.29'

  /usr/bin/ld: /lib/x86_64-linux-gnu/libm.so.6: error adding symbols: DSO
  missing from command line

  collect2: error: ld returned 1 exit status

  make[2]: *** [extras/Build/juceaide/CMakeFiles/juceaide.dir/build.make:194:
  extras/Build/juceaide/juceaide_artefacts/Debug/juceaide] Error 1

  make[1]: *** [CMakeFiles/Makefile2:131:
  extras/Build/juceaide/CMakeFiles/juceaide.dir/all] Error 2

  make: *** [Makefile:136: all] Error 2

To reproduce this, you can pull the JUCE git repo and try executing any of these from the root folder, I'm using the 7.0.7 release of Juce. I'm also using rules_foreign_cc 0.9.0 and cmake 3.22.1 on Ubuntu 22.04 https://github.com/juce-framework/JUCE

chetgnegy commented 10 months ago

After dissecting the build_script.sh file a bit, I found the problem is related to bazel not using the "-B cmake-build" arg, which makes no sense to me, but I'm making progress.