【问题标题】:How to cross-compile Coreutils or other GNU projects with clang/LLVM?如何使用 clang/LLVM 交叉编译 Coreutils 或其他 GNU 项目?
【发布时间】:2020-10-05 01:37:54
【问题描述】:

我非常需要为其他架构编译带有 llvm 的 Coreutils:arm/aarch64/mips/mips32/ppc/ppc32...

因为我安装了所有 gcc-cross 工具,如 mips-linux-gnupowerpc64-linux-gnu,并且如果我有一个像 test.c 这样的简单 C 程序

#include<stdio.h>
int main(){
    printf("hello!");
    return 0;
}

我可以把它编译成拱形,即

clang --target=mips64-linux-gnuabi64 test.c -o test-mips64
➜  tests file test-mips64 
test-mips64: ELF 64-bit MSB executable, MIPS, MIPS64 rel2 version 1 (SYSV), dynamically linked, interpreter /lib64/ld.so.1, BuildID[sha1]=7b33d55a0d08e6cd18d966341590dc351e346a78, for GNU/Linux 3.2.0, not stripped

我尝试以相同的方式编译Coreutils,尝试设置

export CC=clang
export CXX=clang++
CFLAGS = "--target=mips64-linux-gnuabi64"
./configure --host=mips64-linux-gnuabi64

但是,每次配置或制作时都会出错...

我应该如何设置配置?我可以为其他架构轻松编译带有 llvm 的 Coreuntils 吗?

【问题讨论】:

  • 在交叉编译方面,您需要做很多事情。我们需要更多信息来帮助您。至少描述您尝试过的内容以及遇到的错误。
  • 还有你在使用哪个系统?

标签: clang cross-compiling gnu-coreutils


【解决方案1】:

让命令行选项适合交叉编译有点棘手。假设您正在使用基于 Debian 的系统(如 Debian 或 Ubuntu),我让它可以使用以下命令。步骤如下。

  1. 安装gcc-mips64-linux-gnuabi64gcc-powerpc64-linux-gnu
  2. CFLAGS 选择正确的参数
    • -B/usr/mips64-linux-gnuabi64/bin/ 表示我们要在该目录中使用链接器 ld。对 powerpc 执行相同操作。
    • --target=mips64-linux-gnuabi64 表示我们的编译目标是什么。对 powerpc 执行相同操作。
    • -I/usr/mips64-linux-gnuabi64/include 包含头文件。对 powerpc 执行相同操作。
  3. 使用./configure --host=mips64-linux-gnuabi 配置mips64,使用./configure --host=powerpc64-linux-gnueabi 配置powerpc64。

下面是为 mips64 编译的命令:

make clean
CFLAGS="-B/usr/mips64-linux-gnuabi64/bin/ --target=mips64-linux-gnuabi64 -I/usr/mips64-linux-gnuabi64/include" \
    ./configure --host=mips64-linux-gnuabi
make

以及为 powerpc64 编译的命令:

make clean
CFLAGS="-B/usr/powerpc64-linux-gnu/bin/ --target=powerpc64-linux-gnueabi -I/usr/powerpc64-linux-gnu/include" \
    ./configure --host=powerpc64-linux-gnueabi
make

这是file ./src/ls 的输出,以证明它是一个powerpc64 可执行文件:

$ file ./src/ls
./src/ls: ELF 64-bit MSB executable, 64-bit PowerPC or cisco 7500, version 1 (SYSV), dynamically linked, interpreter /lib64/ld64.so.1, for GNU/Linux 3.2.0, BuildID[sha1]=97fe33981ca0112160f44a6fb678d6dc1b462114, not stripped

以下是一个 Dockerfile,可用于可重复地交叉编译 mips64 和 powerpc64 的 coreutils。

# Cross-compile GNU coreutils for mips64 and powerpc64 using clang.
# With help from https://medium.com/@wolfv/cross-compiling-arm-on-travis-using-clang-and-qemu-2b9702d7c6f3

FROM debian:buster

# Install compile-time dependencies.
RUN apt-get update \
    && apt-get install --yes \
        clang \
        curl \
        gcc-mips64-linux-gnuabi64 \
        gcc-powerpc64-linux-gnu \
        make \
        perl \
    && rm -rf /var/lib/apt/lists/*

# Download source code for release.
WORKDIR /tmp/coreutils
RUN curl -fsSL https://ftp.gnu.org/gnu/coreutils/coreutils-8.32.tar.xz \
    | tar xJ --strip-components 1

# Compile and install for mips64.
RUN CFLAGS="-B/usr/mips64-linux-gnuabi64/bin/ --target=mips64-linux-gnuabi64 -I/usr/mips64-linux-gnuabi64/include" \
        ./configure --host=mips64-linux-gnuabi --prefix=/opt/coreutils-mips \
    && make \
    && make install

# Compile and install for powerpc64.
RUN make clean \
    && CFLAGS="-B/usr/powerpc64-linux-gnu/bin/ --target=powerpc64-linux-gnueabi -I/usr/powerpc64-linux-gnu/include" \
        ./configure --host=powerpc64-linux-gnueabi --prefix=/opt/coreutils-powerpc64 \
    && make \
    && make install

# Keep only the compiled programs from the previous stage.
FROM debian:buster
COPY --from=0 /opt /opt

【讨论】:

    【解决方案2】:

    我目前正在使用 Python 开发一个简单的构建工具,可能会对您有所帮助。 不幸的是,目前仍然缺乏 clang 实现,但可以与 GCC 和 MSVC 配合使用。 基本上是混合 Json 参数文件来生成命令行构建。

    CppMagic

    【讨论】:

      猜你喜欢
      • 2014-07-19
      • 2014-01-10
      • 2019-12-11
      • 2020-08-29
      • 2014-03-10
      • 1970-01-01
      • 2011-12-20
      • 2015-12-24
      • 1970-01-01
      相关资源
      最近更新 更多