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
659 stars 241 forks source link

Issue with apache arrow build #879

Open fuhailin opened 2 years ago

fuhailin commented 2 years ago

Hi, I would like to build the apache arrow lib using the cmake rule. I have a wired issue with CXX_COMPILER_FLAGS but i'm confused how to configure it. I would like to know what is the right approach to set the ARROW_CXX_COMPILER_FLAGS in the cmake rule? thanks

WORKSPACE:

workspace(name = "arrow_build")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "rules_foreign_cc",
    sha256 = "bcd0c5f46a49b85b384906daae41d277b3dc0ff27c7c752cc51e43048a58ec83",
    strip_prefix = "rules_foreign_cc-0.7.1",
    urls = [
        "https://github.com/bazelbuild/rules_foreign_cc/archive/0.7.1.tar.gz",
    ],
)

load("@rules_foreign_cc//foreign_cc:repositories.bzl", "rules_foreign_cc_dependencies")

rules_foreign_cc_dependencies()

http_archive(
    name = "jemalloc",
    sha256 = "ed51b0b37098af4ca6ed31c22324635263f8ad6471889e0592a9c0dba9136aea",
    strip_prefix = "jemalloc-5.2.1",
    build_file = "//third_party:jemalloc.BUILD",
    urls = ["https://github.com/jemalloc/jemalloc/archive/5.2.1.tar.gz"],
)

ARROW_VERSION = "18e7cbf75f9b2cb58571a33f0dbd8b4ed954f23b"
http_archive(
    name = "com_github_apache_arrow",
    build_file = "//third_party:arrow.BUILD",
    strip_prefix = "arrow-" + ARROW_VERSION,
    urls = [
        "https://github.com/apache/arrow/archive/{}.zip".format(ARROW_VERSION),
    ],
)

third_party/arrow.BUILD

load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")

package(default_visibility = ["//visibility:public"])

filegroup(
    name = "all_srcs",
    srcs = glob(["**"]),
)

cmake(
    name = "arrow",
    build_args = [
        "-j `nproc`",
    ],
    cache_entries = {
        "CMAKE_BUILD_TYPE": "Release",
        "ARROW_BUILD_SHARED": "OFF",
        "ARROW_BUILD_STATIC": "ON",
        "ARROW_BUILD_TESTS": "OFF",
        "ARROW_JEMALLOC": "ON",
        "ARROW_JEMALLOC_INCLUDE_DIR": "$EXT_BUILD_DEPS/jemalloc",
    },
    lib_source = ":all_srcs",
    out_static_libs = ["libarrow.a"],
    working_directory = "cpp",
    deps = [
        "@jemalloc",
    ],
)

third_party/jemalloc.BUILD

load("@rules_foreign_cc//foreign_cc:defs.bzl", "configure_make")

package(default_visibility = ["//visibility:public"])

filegroup(
    name = "all_srcs",
    srcs = glob(["**"]),
)

configure_make(
    name = "jemalloc",
    args = [
        "-j `nproc`",
    ],
    autogen = True,
    configure_in_place = True,
    lib_source = ":all_srcs",
    linkopts = [
        "-pthread",
        "-ldl",
    ],
    out_shared_libs = select({
        "@platforms//os:macos": [
            "libjemalloc.dylib",
        ],
        "@platforms//os:linux": [
            "libjemalloc.so",
        ],
        "@platforms//os:windows": [
            "libjemalloc.dll",
        ],
    }),
    out_static_libs = [
        "libjemalloc.a",
    ],
)

BUILD

load("@bazel_skylib//rules:build_test.bzl", "build_test")

package(default_visibility = ["//visibility:public"])

build_test(
    name = "build_test_arrow",
    targets = [
        "@jemalloc",
        "@com_github_apache_arrow//:arrow",
    ],
)

I am going to build this target: bazel build build_test_arrow

error:

ERROR: /root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/external/com_github_apache_arrow/BUILD.bazel:10:6: Foreign Cc - CMake: Building a
rrow failed: (Exit 2): bash failed: error executing command /bin/bash -c bazel-out/k8-fastbuild/bin/external/com_github_apache_arrow/arrow_foreign_cc/wr
apper_build_script.sh

Use --sandbox_debug to see verbose messages from the sandbox
rules_foreign_cc: Build failed!
rules_foreign_cc: Keeping temp build directory and dependencies directory for debug.
rules_foreign_cc: Please note that the directories inside a sandbox are still cleaned unless you specify --sandbox_debug Bazel command line flag.
rules_foreign_cc: Printing build logs:
_____ BEGIN BUILD LOGS _____

Bazel external C/C++ Rules. Building library arrow

Environment:______________
BUILD_SCRIPT=bazel-out/k8-fastbuild/bin/external/com_github_apache_arrow/arrow_foreign_cc/build_script.sh
EXT_BUILD_ROOT=/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build
BUILD_LOG=bazel-out/k8-fastbuild/bin/external/com_github_apache_arrow/arrow_foreign_cc/CMake.log
PWD=/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build
BUILD_WRAPPER_SCRIPT=bazel-out/k8-fastbuild/bin/external/com_github_apache_arrow/arrow_foreign_cc/wrapper_build_script.sh
TMPDIR=/tmp
EXT_BUILD_DEPS=/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbu
ild/bin/external/com_github_apache_arrow/arrow.ext_build_deps
BUILD_TMPDIR=/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuil
d/bin/external/com_github_apache_arrow/arrow.build_tmpdir
SHLVL=2
INSTALLDIR=/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuild/
bin/external/com_github_apache_arrow/arrow
PATH=/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build:/root/.cache/bazelisk/downloa
ds/bazelbuild/bazel-5.0.0-linux-x86_64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
__________________________
+ /root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/external/cmake-3.21.2-linux-x86_
64/bin/cmake -DARROW_BUILD_SHARED=OFF -DARROW_BUILD_STATIC=ON -DARROW_BUILD_TESTS=OFF -DARROW_JEMALLOC=ON -DARROW_JEMALLOC_INCLUDE_DIR=/root/.cache/baze
l/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuild/bin/external/com_github_apac
he_arrow/arrow.ext_build_deps/jemalloc -DCMAKE_TOOLCHAIN_FILE=crosstool_bazel.cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/root/.cache/bazel
/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuild/bin/external/com_github_apach
e_arrow/arrow -DCMAKE_PREFIX_PATH=/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/
bazel-out/k8-fastbuild/bin/external/com_github_apache_arrow/arrow.ext_build_deps -DCMAKE_RANLIB= -DCMAKE_MAKE_PROGRAM=/root/.cache/bazel/_bazel_root/f8c
b4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-opt-exec-2B5CBBC6/bin/external/rules_foreign_cc/toolcha
ins/make/bin/make -G 'Unix Makefiles' /root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_bu
ild/external/com_github_apache_arrow/cpp
-- Building using CMake version: 3.21.2
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done

.............

[ 34%] Built target arrow_bundled_dependencies
[ 34%] Building CXX object src/arrow/CMakeFiles/arrow_objlib.dir/scalar.cc.o
In file included from /root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/external/com
_github_apache_arrow/cpp/src/arrow/config.cc:22:
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuild/bin/externa
l/com_github_apache_arrow/arrow.build_tmpdir/src/arrow/util/config.h:30:245: error: stray '\' in program
   30 | #define ARROW_CXX_COMPILER_FLAGS "-U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-
pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\\"redacted\\" -D__TIMESTAMP__=\\"redacted\\" -D__TIME__=\\"red
acted\\" -fdiagnostics-color=always -O3 -DNDEBUG"
      |
                                                                                                    ^
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/external/com_github_apache_arrow/c
pp/src/arrow/config.cc:42:5: note: in expansion of macro 'ARROW_CXX_COMPILER_FLAGS'
   42 |     ARROW_CXX_COMPILER_FLAGS,
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuild/bin/externa
l/com_github_apache_arrow/arrow.build_tmpdir/src/arrow/util/config.h:30:246: error: stray '\' in program
   30 | #define ARROW_CXX_COMPILER_FLAGS "-U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-
pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\\"redacted\\" -D__TIMESTAMP__=\\"redacted\\" -D__TIME__=\\"red
acted\\" -fdiagnostics-color=always -O3 -DNDEBUG"
      |
                                                                                                     ^
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/external/com_github_apache_arrow/c
pp/src/arrow/config.cc:42:5: note: in expansion of macro 'ARROW_CXX_COMPILER_FLAGS'
   42 |     ARROW_CXX_COMPILER_FLAGS,
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuild/bin/externa
l/com_github_apache_arrow/arrow.build_tmpdir/src/arrow/util/config.h:30:276: error: stray '\' in program
   30 | #define ARROW_CXX_COMPILER_FLAGS "-U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-
pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\\"redacted\\" -D__TIMESTAMP__=\\"redacted\\" -D__TIME__=\\"red
acted\\" -fdiagnostics-color=always -O3 -DNDEBUG"
      |
                                                                                                                                   ^
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/external/com_github_apache_arrow/c
pp/src/arrow/config.cc:42:5: note: in expansion of macro 'ARROW_CXX_COMPILER_FLAGS'
   42 |     ARROW_CXX_COMPILER_FLAGS,
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuild/bin/externa
l/com_github_apache_arrow/arrow.build_tmpdir/src/arrow/util/config.h:30:277: error: stray '\' in program
   30 | #define ARROW_CXX_COMPILER_FLAGS "-U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-
pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\\"redacted\\" -D__TIMESTAMP__=\\"redacted\\" -D__TIME__=\\"red
acted\\" -fdiagnostics-color=always -O3 -DNDEBUG"
      |
                                                                                                                                    ^
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/external/com_github_apache_arrow/c
pp/src/arrow/config.cc:42:5: note: in expansion of macro 'ARROW_CXX_COMPILER_FLAGS'
   42 |     ARROW_CXX_COMPILER_FLAGS,
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuild/bin/externa
l/com_github_apache_arrow/arrow.build_tmpdir/src/arrow/util/config.h:30:302: error: stray '\' in program
   30 | #define ARROW_CXX_COMPILER_FLAGS "-U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-
pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\\"redacted\\" -D__TIMESTAMP__=\\"redacted\\" -D__TIME__=\\"red
acted\\" -fdiagnostics-color=always -O3 -DNDEBUG"
      |

     ^
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/external/com_github_apache_arrow/c
pp/src/arrow/config.cc:42:5: note: in expansion of macro 'ARROW_CXX_COMPILER_FLAGS'
   42 |     ARROW_CXX_COMPILER_FLAGS,
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuild/bin/externa
l/com_github_apache_arrow/arrow.build_tmpdir/src/arrow/util/config.h:30:303: error: stray '\' in program
   30 | #define ARROW_CXX_COMPILER_FLAGS "-U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-
pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\\"redacted\\" -D__TIMESTAMP__=\\"redacted\\" -D__TIME__=\\"red
acted\\" -fdiagnostics-color=always -O3 -DNDEBUG"
      |

      ^
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/external/com_github_apache_arrow/c
pp/src/arrow/config.cc:42:5: note: in expansion of macro 'ARROW_CXX_COMPILER_FLAGS'
   42 |     ARROW_CXX_COMPILER_FLAGS,
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuild/bin/externa
l/com_github_apache_arrow/arrow.build_tmpdir/src/arrow/util/config.h:30:304: error: unable to find string literal operator 'operator""redacted' with 'co
nst char [272]', 'long unsigned int' arguments
   30 | #define ARROW_CXX_COMPILER_FLAGS "-U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-
pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\\"redacted\\" -D__TIMESTAMP__=\\"redacted\\" -D__TIME__=\\"red
acted\\" -fdiagnostics-color=always -O3 -DNDEBUG"
      |

       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/root/.cache/bazel/_bazel_root/f8cb4d7c2553e81df7cb8ef2585e305e/sandbox/processwrapper-sandbox/6/execroot/arrow_build/bazel-out/k8-fastbuild/bin/externa
l/com_github_apache_arrow/arrow.build_tmpdir/src/arrow/util/config.h:30:304: note: in definition of macro 'ARROW_CXX_COMPILER_FLAGS'
   30 | #define ARROW_CXX_COMPILER_FLAGS "-U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-
pointer -std=c++0x -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__=\\"redacted\\" -D__TIMESTAMP__=\\"redacted\\" -D__TIME__=\\"red
acted\\" -fdiagnostics-color=always -O3 -DNDEBUG"
      |

       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make[2]: *** [src/arrow/CMakeFiles/arrow_objlib.dir/build.make:398: src/arrow/CMakeFiles/arrow_objlib.dir/config.cc.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[1]: *** [CMakeFiles/Makefile2:798: src/arrow/CMakeFiles/arrow_objlib.dir/all] Error 2
make: *** [Makefile:146: all] Error 2
_____ END BUILD LOGS _____
stefan-novak-brt commented 2 years ago

Hi fuhailin, I came across this issue as well.

I have a hunch that it's due to an additional application of escaping characters when ARROW_CXX_COMPILER_FLAGS is interpolated in the Arrow build process: https://github.com/apache/arrow/blob/7a0ba80702ef63e63e346d2a9ca3137d8baca8bb/cpp/src/arrow/util/config.h.cmake#L30

Have you had any luck with this since?

stefan-novak-brt commented 2 years ago

Ah, ok -- so the template src/arrow/util/config.h.cmake is being generated via:

configure_file("src/arrow/util/config.h.cmake" "src/arrow/util/config.h" ESCAPE_QUOTES)

in https://github.com/apache/arrow/blob/apache-arrow-9.0.0/cpp/cmake_modules/ThirdpartyToolchain.cmake#L4874

That mucks up the handling of the special __DATE__, __TIMESTAMP__, and __TIME__ variables. Note: Bazel sets these to "redacted" to allow for reproducible builds.

stefan-novak-brt commented 2 years ago

I created an issue on the Apache Arrow Jira board: https://issues.apache.org/jira/browse/ARROW-17763

This issue seems to be related to an Arrow CMake project configuration, not an unexpected behavior in rules_foreign_cc. @fuhailin -- do you think we could close this issue and keep an eye on the Arrow Jira ticket?

stefan-novak-brt commented 2 years ago

@fuhailin : I was able to get Arrow compiling with Bazel.

This can be done by applying a path like this:

--- cpp/cmake_modules/ThirdpartyToolchain.cmake
+++ cpp/cmake_modules/ThirdpartyToolchain.cmake
@@ -4871,6 +4871,6 @@ message(STATUS "All bundled static libraries: ${ARROW_BUNDLED_STATIC_LIBS}")

 # Write out the package configurations.

-configure_file("src/arrow/util/config.h.cmake" "src/arrow/util/config.h" ESCAPE_QUOTES)
+configure_file("src/arrow/util/config.h.cmake" "src/arrow/util/config.h")
 install(FILES "${ARROW_BINARY_DIR}/src/arrow/util/config.h"
         DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/arrow/util")

when downloading and extracting Arrow source:

http_archive(
    name = "arrow",
    build_file = "//third_party/github.com/apache/arrow:BUILD.bazel",
    strip_prefix = "apache-arrow-9.0.0",
    patches = ["//third_party/github.com/apache/arrow:ThirdpartyToolchain.cmake.patch"],
    urls = [
        "https://github.com/apache/arrow/archive/refs/tags/apache-arrow-9.0.0.tar.gz"
    ],
)

I shared the same finding with the Jira ticket as well.

Good luck and let me know if there's anything else I can help with!

aakshintala commented 1 year ago

Hey @stefan-novak-brt, any chance you could share your WORKSPACE and BUILD files? I was using a hacked up version of https://github.com/tensorflow/io/blob/master/third_party/arrow.BUILD. My script worked fine for Arrow-8.0.0, but it's a real PITA to maintain as arrow's dependencies change... Would love to move to a rules_foreign_cc based solution instead. Thanks in advance!

aakshintala commented 1 year ago

Ah nevermind. I think the files provided by fuhailin coupled with your patch should be a good starting point.