How to build a simple C++ demo using Eigen with Bazel?
Asked Answered
H

3

9

How can I use Eigen within a C++ project that is built using Bazel (version 0.25.2)? I like to fetch the Eigen dependency using http_archive or git_repository.

I've tried the following:

main.cpp

#include <iostream>
#include <Eigen/Dense>

using Eigen::MatrixXd;

int main() {
    MatrixXd m(2, 2);
    m(0, 0) = 3;
    m(1, 0) = 2.5;
    m(0, 1) = -1;
    m(1, 1) = m(1, 0) + m(0, 1);
    std::cout << m << std::endl;
}

WORKSPACE

workspace(name = "EigenDemo")

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

# Eigen
http_archive(
    name = "eigen",
    build_file = "//:eigen.BUILD",
    sha256 = "3a66f9bfce85aff39bc255d5a341f87336ec6f5911e8d816dd4a3fdc500f8acf",
    url = "https://bitbucket.org/eigen/eigen/get/c5e90d9.tar.gz",
)

BUILD

cc_binary(
    name = "EigenDemo",
    srcs = ["main.cpp"],
    deps = [
        "@eigen",
    ],
)

eigen.BUILD

# Copyright 2017 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

cc_library(
    name = "eigen",
    hdrs = glob(
        ["Eigen/**"],
        exclude = [
            "Eigen/src/OrderingMethods/Amd.h",
            "Eigen/src/SparseCholesky/**",
            "Eigen/Eigen",
            "Eigen/IterativeLinearSolvers",
            "Eigen/MetisSupport",
            "Eigen/Sparse",
            "Eigen/SparseCholesky",
            "Eigen/SparseLU",
        ],
    ),
    defines = [
        "EIGEN_MPL_ONLY",
        "EIGEN_NO_DEBUG",
    ],
    includes = ["."],
)

Error output:

INFO: Analysed target //:EigenTest (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /BazelDemos/EigenDemo/BUILD:1:1: C++ compilation of rule '//:EigenTest' failed (Exit 1) gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer '-std=c++0x' -MD -MF ... (remaining 40 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
main.cpp:2:10: fatal error: Eigen/Dense: No such file or directory
 #include <Eigen/Dense>
      ^~~~~~~~~~~~~
compilation terminated.
Target //:EigenTest failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.258s, Critical Path: 0.10s
INFO: 0 processes.
FAILED: Build did NOT complete successfully
Herzl answered 16/5, 2019 at 16:7 Comment(4)
Never used bazel, but the error message looks like the extra comma(s) behind "@eigen//:eigen" are the problem.Screwed
@chtz: I edited my post - the double comma happened accidently - I updated the error messageHerzl
I guess this requires somebody who actually knows bazel ... Maybe clearing the/some cache helps?Screwed
Did bazel clean --expunge_async && bazel build //...Herzl
H
4

strip_prefix is missing in the WORKSPACE file:

workspace(name = "EigenDemo")

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

# Eigen
http_archive(
    name = "eigen",
    build_file = "//:eigen.BUILD",
    sha256 = "3a66f9bfce85aff39bc255d5a341f87336ec6f5911e8d816dd4a3fdc500f8acf",
    url = "https://bitbucket.org/eigen/eigen/get/c5e90d9.tar.gz",
    strip_prefix="eigen-eigen-c5e90d9e764e"
)
Herzl answered 20/5, 2019 at 11:20 Comment(0)
C
0

Tensorflow no longer uses a separate eigen.BUILD file. Instead, you can put the following in your WORKSPACE file (without the need for a separate eigen.BUILD file):

http_archive(
    name = "com_gitlab_libeigen_eigen",
    sha256 = "0215c6593c4ee9f1f7f28238c4e8995584ebf3b556e9dbf933d84feb98d5b9ef",
    strip_prefix = "eigen-3.3.8",
    urls = [
        "https://gitlab.com/libeigen/eigen/-/archive/3.3.8/eigen-3.3.8.tar.bz2",
    ],
    build_file_content =
"""
# TODO(keir): Replace this with a better version, like from TensorFlow.
# See https://github.com/ceres-solver/ceres-solver/issues/337.
cc_library(
    name = 'eigen',
    srcs = [],
    includes = ['.'],
    hdrs = glob(['Eigen/**']),
    visibility = ['//visibility:public'],
)
"""
)

(As you can probably tell from the TODO, I ripped this from Ceres.)

Chopin answered 19/7, 2021 at 4:14 Comment(0)
E
0

I used the latest rules_foreign_cc. There is a new cmake rule. Here is what I have:

WORKPSPACE

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

http_archive(
    name = "rules_foreign_cc",
    sha256 = "6041f1374ff32ba711564374ad8e007aef77f71561a7ce784123b9b4b88614fc",
    strip_prefix = "rules_foreign_cc-0.8.0",
    url = "https://github.com/bazelbuild/rules_foreign_cc/archive/0.8.0.tar.gz",
)

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

# This sets up some common toolchains for building targets. For more details, please see
# https://bazelbuild.github.io/rules_foreign_cc/0.8.0/flatten.html#rules_foreign_cc_dependencies
rules_foreign_cc_dependencies()

_ALL_CONTENT = """\
filegroup(
    name = "all_srcs",
    srcs = glob(["**"]),
    visibility = ["//visibility:public"],
)
"""
http_archive(
  name="eigen",
  build_file_content = _ALL_CONTENT,
  strip_prefix = "eigen-3.4",
  urls=["https://gitlab.com/libeigen/eigen/-/archive/3.4/eigen-3.4.tar.bz2"],
  sha256 = "a6f7aaa7b19c289dfeb33281e1954f19bf2ba1c6cae2c182354d820f535abef8",
)

BUILD

load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
cmake(
    name = "eigen",
    cache_entries = {
        "CMAKE_C_FLAGS": "-fPIC",
    },
    lib_source = "@eigen//:all_srcs",
    out_headers_only = True,
    includes = ["eigen3/",],
    install = True,
)
cc_binary(
    name = "bazelt",
    srcs = [
        "main.cpp",
    ],
    visibility = ["//visibility:public"],
    deps = [":eigen",],
)

Note the "includes" parameter in the cmake rule. By default cmake rule will put the header files in the "include" directory in the cache. So all you need is the relative path to that. And the main.cpp

#include <iostream>
#include <Eigen/Dense>

int main()
{
  Eigen::MatrixXd m = Eigen::MatrixXd::Random(4,6);
  std::cout << m << '\n';
  return 0;
}
Endow answered 14/6, 2022 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.