使用Bazel构建LLVM

时间:2018-07-30 00:36:23

标签: llvm bazel

我有一个当前正在使用CMake的项目,我想切换到Bazel。主要依赖项是LLVM,我使用它来生成LLVM IR。环顾四周,似乎没有太多指导,因为只有TensorFlow似乎使用了Bazel的LLVM(据我所知,它会自动生成其配置)。我发现还有一个thread on bazel-discuss讨论了类似的问题,尽管我尝试复制它的尝试失败了。

目前,我最好的表现是这样的(fetcher.bzl):

def _impl(ctx):
    # Download LLVM master
    ctx.download_and_extract(url = "https://github.com/llvm-mirror/llvm/archive/master.zip")

    # Run `cmake llvm-master` to generate configuration.
    ctx.execute(["cmake", "llvm-master"])

    # The bazel-discuss thread says to delete llvm-master, but I've
    # found that only generated files are pulled out of master, so all
    # the non-generated ones get dropped if I delete this.
    # ctx.execute(["rm", "-r", "llvm-master"])

    # Generate a BUILD file for the LLVM dependency.
    ctx.file('BUILD', """
# Build a library with all the LLVM code in it.
cc_library(
    name = "lib",
    srcs = glob(["**/*.cpp"]),
    hdrs = glob(["**/*.h"]),

    # Include the x86 target and all include files.
    # Add those under llvm-master/... as well because only built files
    # seem to appear under include/...
    copts = [
        "-Ilib/Target/X86",
        "-Iinclude",
        "-Illvm-master/lib/Target/X86",
        "-Illvm-master/include",
    ],

    # Include here as well, not sure whether this or copts is
    # actually doing the work.
    includes = [
        "include",
        "llvm-master/include",
    ],
    visibility = ["//visibility:public"],
    # Currently picking up some gtest targets, I have that dependency
    # already, so just link it here until I filter those out.
    deps = [
        "@gtest//:gtest_main",
    ],
)
""")

    # Generate an empty workspace file
    ctx.file('WORKSPACE', '')

get_llvm = repository_rule(implementation = _impl)

然后我的WORKSPACE文件如下所示:

load(":fetcher.bzl", "get_llvm")

git_repository(
    name = "gflags",
    commit = "46f73f88b18aee341538c0dfc22b1710a6abedef", # 2.2.1
    remote = "https://github.com/gflags/gflags.git",
)

new_http_archive(
    name = "gtest",
    url = "https://github.com/google/googletest/archive/release-1.8.0.zip",
    sha256 = "f3ed3b58511efd272eb074a3a6d6fb79d7c2e6a0e374323d1e6bcbcc1ef141bf",
    build_file = "gtest.BUILD",
    strip_prefix = "googletest-release-1.8.0",
)

get_llvm(name = "llvm")

然后我将使用bazel build @llvm//:lib --verbose_failures运行它。

我会不断从丢失的头文件中得到错误。最终,我发现运行cmake llvm-master会在当前目录中生成许多头文件,但似乎将未生成的头文件保留在llvm-master/中。我在llvm-master/下添加了相同的包含目录,这似乎捕获了很多文件。但是,当前看来tblgen尚未运行,并且我仍然缺少编译所需的关键标头。我当前的错误是:

In file included from external/llvm/llvm-master/include/llvm/CodeGen/MachineOperand.h:18:0,
                 from external/llvm/llvm-master/include/llvm/CodeGen/MachineInstr.h:24,
                 from external/llvm/llvm-master/include/llvm/CodeGen/MachineBasicBlock.h:22,
                 from external/llvm/llvm-master/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h:20,
                 from external/llvm/llvm-master/include/llvm/CodeGen/GlobalISel/ConstantFoldingMIRBuilder.h:13,
                 from external/llvm/llvm-master/unittests/CodeGen/GlobalISel/PatternMatchTest.cpp:10:
external/llvm/llvm-master/include/llvm/IR/Intrinsics.h:42:38: fatal error: llvm/IR/IntrinsicEnums.inc: No such file or directory

尝试特别查找此文件,我看不到任何IntrinsicEnums.incIntrinsicEnums.hIntrinsicEnums.dt。我确实看到了很多Instrinsics*.td,所以也许其中一个会生成此特定文件?

似乎tblgen应该将*.td文件转换为*.h*.cpp文件(如果我误会了,请纠正我)。但是,这似乎没有运行。我看到在Tensorflow的项目中,他们有一个gentbl() BUILD宏,尽管复制它对Tensorflow的其余构建基础结构有太多依赖性,但对我来说复制它并不实际。

如果没有Tensorflow系统那么大而复杂的东西,有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

我已张贴到llvm-dev邮件列表here,并得到了一些有趣的答复。 LLVM绝对不是设计来支​​持Bazel的,而且做得也不是特别好。从理论上讲,使用Ninja输出所有编译命令,然后从Bazel中使用它们,似乎是可行的。这可能很困难,并且需要一个单独的工具来输出由Bazel运行的Skylark代码。

对于我正在从事的项目规模而言,这似乎非常复杂,因此我的解决方法是从releases.llvm.org下载预构建的二进制文件。这包括所有必需的头文件,库和工具二进制文件。我可以使用Bazel为我的自定义编程语言创建一个简单但功能强大的工具链。

简单的示例(有限但重点突出):https://github.com/dgp1130/llvm-bazel-foolang

完整的示例(较复杂且关注较少):https://github.com/dgp1130/sanity-lang