【问题标题】:How to build a cross-compiler for i386-apple-darwin target from x86_64-apple-darwin host?如何从 x86_64-apple-darwin 主机为 i386-apple-darwin 目标构建交叉编译器?
【发布时间】:2013-03-17 23:43:18
【问题描述】:

问题概述

我正在尝试学习如何构建一个交叉编译器,特别是 GNU gcc 4.7.2 的一个 64 位 Intel OS X 主机,它以 i386-apple-darwin(32 位 Intel)为目标。但是,我一直陷入同样的​​错误:

...
checking for i386-apple-darwin-gcc... /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include
checking for suffix of object files... configure: error: in `/Users/alexpreynolds/Developer/third-party/build-gcc/i386-apple-darwin/libgcc':
configure: error: cannot compute suffix of object files: cannot compile
See `config.log' for more details.
make[1]: *** [configure-target-libgcc] Error 1
make: *** [all] Error 2

构建过程

我首先构建了一个 GNU binutils 版本,它以 i386-apple-darwin 为目标,GNU gcc 构建文件夹可以访问它:

$ cd third-party
$ wget http://ftp.gnu.org/gnu/binutils/binutils-2.23.2.tar.bz2
$ tar xjvf binutils-2.23.2.tar.bz2
$ mkdir i386-apple-darwin
$ mkdir build-binutils && cd build-binutils
$ ../binutils-2.23.2/configure --target=i386-apple-darwin --prefix="/Users/alexpreynolds/Developer/third-party/i386-apple-gcc"
$ make all && make install
...

然后我尝试构建gcc的目标版本如下:

$ cd ..
$ pwd
/Users/alexpreynolds/Developer/third-party
$ wget http://ftp.gnu.org/pub/gnu/gcc/gcc-4.7.2/gcc-4.7.2.tar.bz2
$ tar xvjf gcc-4.7.2.tar.bz2
$ cd gcc-4.7.2
$ ./contrib/download_prerequisites
... /* this grabs GMP, MPC and MPFR dependencies */
$ cd ..
$ mkdir build-gcc && cd build-gcc
$ ../gcc-4.7.2/configure --target=i386-apple-darwin --prefix="/Users/alexpreynolds/Developer/Bedops/bedops/third-party/i386-apple-gcc"
$ make
...

编译大约 30 分钟后,它卡在上面显示的错误消息中。

错误详情

为了追查错误的来源,这里是build-gcc/i386-apple-darwin/libgcc/config.log的sn-p:

configure:3344: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include    -V >&5
xgcc: error: unrecognized command line option '-V'
xgcc: fatal error: no input files
compilation terminated.
configure:3355: $? = 1
configure:3344: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include    -qversion >&5
xgcc: error: unrecognized command line option '-qversion'
xgcc: fatal error: no input files
compilation terminated.
configure:3355: $? = 1
configure:3371: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include    -o conftest -g -O2   conftest.c  >&5
Assembler messages:
Fatal error: invalid listing option `r'
configure:3374: $? = 1
configure:3562: checking for suffix of object files
configure:3584: /Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/xgcc -B/Users/alexpreynolds/Developer/third-party/build-gcc/./gcc/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/bin/ -B/Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/lib/ -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/include -isystem /Users/alexpreynolds/Developer/third-party/i386-apple-gcc/i386-apple-darwin/sys-include    -c -g -O2  conftest.c >&5
Assembler messages:
Fatal error: invalid listing option `r'
configure:3588: $? = 1
configure: failed program was:
| /* confdefs.h */
| #define PACKAGE_NAME "GNU C Runtime Library"
| #define PACKAGE_TARNAME "libgcc"
| #define PACKAGE_VERSION "1.0"
| #define PACKAGE_STRING "GNU C Runtime Library 1.0"
| #define PACKAGE_BUGREPORT ""
| #define PACKAGE_URL "http://www.gnu.org/software/libgcc/"
| /* end confdefs.h.  */
|
| int
| main ()
| {
|
|   ;
|   return 0;
| }
configure:3602: error: in `/Users/alexpreynolds/Developer/third-party/build-gcc/i386-apple-darwin/libgcc':
configure:3605: error: cannot compute suffix of object files: cannot compile
See `config.log' for more details.

我正在引导的编译器

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin12/4.7.2/lto-wrapper
Target: x86_64-apple-darwin12
Configured with: ../gcc-4.7.2/configure --prefix=/opt/local --build=x86_64-apple-darwin12 --enable-languages=c,c++,objc,obj-c++,lto,fortran,java --libdir=/opt/local/lib/gcc47 --includedir=/opt/local/include/gcc47 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-4.7 --with-libiconv-prefix=/opt/local --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-4.7 --with-gxx-include-dir=/opt/local/include/gcc47/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --with-ppl=/opt/local --with-cloog=/opt/local --enable-cloog-backend=isl --disable-cloog-version-check --enable-stage1-checking --enable-lto --enable-libstdcxx-time --with-as=/opt/local/bin/as --with-ld=/opt/local/bin/ld --with-ar=/opt/local/bin/ar --with-bugurl=https://trac.macports.org/newticket --disable-ppl-version-check --with-pkgversion='MacPorts gcc47 4.7.2_2+universal'
Thread model: posix
gcc version 4.7.2 (MacPorts gcc47 4.7.2_2+universal) 

问题

在设置coreutilsgcc 的构建环境时我是否遗漏了一个步骤,从而导致此错误?

在尝试解决此错误时,我还能排除哪些故障?我在 Google 上搜索此错误消息的结果并不多。


变化

我将构建脚本更改如下:

#!/bin/bash -x

WORK_DIR=$PWD
INSTALL_DIR=$WORK_DIR/gcc-4.7.2-darwin
MIN_OSX_VERSION=10.5

if [ -d "$INSTALL_DIR" ]; then
    echo "Directory $INSTALL_DIR already exists";
    exit 1
fi

BINUTILS=binutils-2.23.2
TARBALL_BINUTILS=$BINUTILS.tar.bz2
URL_BINUTILS=http://ftp.gnu.org/gnu/binutils/$TARBALL_BINUTILS
SOURCE_BINUTILS_DIR=$WORK_DIR/$BINUTILS
BUILD_BINUTILS_DIR=$WORK_DIR/build-$BINUTILS

GCC=gcc-4.7.2
TARBALL_GCC=$GCC.tar.bz2
URL_GCC=http://ftp.gnu.org/pub/gnu/gcc/$GCC/$TARBALL_GCC
SOURCE_GCC_DIR=$WORK_DIR/$GCC
BUILD_GCC_DIR=$WORK_DIR/build-$GCC

curl -O $URL_BINUTILS
mkdir -p $SOURCE_BINUTILS_DIR
tar -xjvf $TARBALL_BINUTILS
rm $TARBALL_BINUTILS
mkdir -p $BUILD_BINUTILS_DIR
cd $BUILD_BINUTILS_DIR
$SOURCE_BINUTILS_DIR/configure --prefix=$INSTALL_DIR
make -j $(sysctl -n hw.availcpu) all CFLAGS='-Wno-error' && make install

find $INSTALL_DIR -name '*strip*' -delete

cd $WORK_DIR

curl -O $URL_GCC
mkdir -p $SOURCE_GCC_DIR
tar -xvjf $TARBALL_GCC
rm $TARBALL_GCC
cd $SOURCE_GCC_DIR
sed -i '' -e 's/wget/curl -O/g' $SOURCE_GCC_DIR/contrib/download_prerequisites
$SOURCE_GCC_DIR/contrib/download_prerequisites
cd $WORK_DIR
mkdir -p $BUILD_GCC_DIR
cd $BUILD_GCC_DIR
$SOURCE_GCC_DIR/configure --prefix=$INSTALL_DIR --enable-languages=c,c++
make -j $(sysctl -n hw.availcpu) CFLAGS="-mmacosx-version-min=$MIN_OSX_VERSION" && make install

find $INSTALL_DIR -name '*.a' -print0 | xargs -0 -n1 ranlib

rm -Rf $BUILD_GCC_DIR
rm -Rf $BUILD_BINUTILS_DIR
rm -Rf $SOURCE_GCC_DIR
rm -Rf $SOURCE_BINUTILS_DIR

ranlib 步骤是必要的,因为在构建库的过程中出现了许多错误。

要构建i386 二进制文件,我必须使用我的Makefile 执行以下操作:

ARCH                 = i386
...
GCCDIR               = ${PARTY3}/gcc-4.7.2-darwin
GCCBINDIR            = ${GCCDIR}/bin
GCCLIBDIR            = ${GCCDIR}/lib
GCCINCDIR            = ${GCCDIR}/include
...
LIBCPP               = libgcc_eh.a
LIBCXX               = libstdc++.a
...
INCLUDES             = -I${GCCINCDIR} ...
...
CC                   = ${GCCBINDIR}/gcc
CXX                  = ${GCCBINDIR}/g++
...
GCCCPPLIBPATH        = ${GCCLIBDIR}/gcc/x86_64-apple-darwin12.3.0/4.7.2/${ARCH}/${LIBCPP}
GCCCXXLIBPATH        = ${GCCLIBDIR}/${ARCH}/${LIBCXX}
...
FLAGS                = ${SFLAGS} ${NONSTATICFLAGS} ${OBJS} -L${GCCLIBDIR} ...
...
run: dependencies
    mkdir -p ${BIN} && ${CXX} -o ${BIN}/${PROG}_${ARCH} ${FLAGS} -static-libgcc -mmacosx-version-min=${MIN_OSX_VERSION} -arch ${ARCH} ... ${GCCCPPLIBPATH} ${GCCCXXLIBPATH} ${SOURCE1}

Makefile 更改摘要:

  1. 我必须添加 -static-libgcc 标志并省略 -static-libstdc++,这没有任何效果(除了导致构建失败,直到我在静态库上运行 ranlib,它修复了构建失败但对-static-libstdc++没有影响)。

  2. 我在run 目标中添加了${GCCCPPLIBPATH}${GCCCXXLIBPATH},它们分别指向静态C anc C++ 库libgcc_eh.alibstdc++.a

这使我能够编译一个工作的i386(32 位)二进制文件,但是相同的 GCC 安装未能构建一个工作的 x86_64(64 位)二进制文件。

【问题讨论】:

  • 我还没有构建编译器的目标,但是这些选项 xgcc 失败了,看起来像是用于例如IBM 的xlc,即配置脚本是否对它使用的编译器感到困惑?我将通过追溯产生错误的配置行开始调查,并尝试了解该配置行是如何生成的。

标签: macos gcc cross-compiling gnu darwin


【解决方案1】:

稍微考虑一下之后,我想到到目前为止最简单的解决方案是为 x86_64 编译 GCC,然后在您希望为 i386 构建时传递-m32。由于某种原因,这还不足以满足您的需求吗?以这种方式构建非常简单,并且不需要下面描述的任何疯狂。执行以下一系列事件会产生一个工作编译器,该编译器将在请求时输出 i386 二进制文件:

WORK_DIR=$PWD
INSTALL_DIR=$WORK_DIR/install

curl -O http://ftp.gnu.org/gnu/binutils/binutils-2.23.2.tar.bz2
tar xjvf binutils-2.23.2.tar.bz2
mkdir build-binutils && cd build-binutils
../binutils-2.23.2/configure --prefix=$INSTALL_DIR
make -j $(sysctl -n hw.availcpu) all CFLAGS='-Wno-error' && make install

find $INSTALL_DIR -name '*strip*' -delete

cd $WORK_DIR

curl -O http://ftp.gnu.org/pub/gnu/gcc/gcc-4.7.2/gcc-4.7.2.tar.bz2
tar xvjf gcc-4.7.2.tar.bz2
cd gcc-4.7.2
sed -i '' -e 's/wget/curl -O/g' ./contrib/download_prerequisites
./contrib/download_prerequisites
cd ..
mkdir build-gcc && cd build-gcc
../gcc-4.7.2/configure --prefix=$INSTALL_DIR --enable-languages=c,c++
make -j $(sysctl -n hw.availcpu) && make install

完成后,您可以看到它的运行情况,如下所示:

mrowe@falcon:~/tmp$ cat > test.cpp
#include <iostream>

int main(int argc, char **argv) {
    std::cerr << "Built with GCC 4.7" << std::endl;
    return 0;
}

^D
mrowe@falcon:~/tmp$ ./install/bin/g++ -o test test.cpp 
mrowe@falcon:~/tmp$ file test
test: Mach-O 64-bit executable x86_64
mrowe@falcon:~/tmp$ ./install/bin/g++ -m32 -static-libgcc -static-libstdc++ -o test test.cpp 
mrowe@falcon:~/tmp$ file test
test: Mach-O executable i386
mrowe@falcon:~/tmp$ otool -L ./test
./test:
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1192.0.0)
mrowe@falcon:~/tmp$ ./test 
Built with GCC 4.7
mrowe@falcon:~/tmp$ 

如果您看到如下警告:

ignoring file …/4.7.2/i386/libgcc.a, file was built for archive which is not the architecture being linked (i386)

您可以通过在静态库上运行 ranlib 来修复它们:

find $INSTALL_DIR -name '*.a' -print0 | xargs -0 -n1 ranlib

以下是我试图帮助您沿着您尝试遵循的路径的尝试。我认为这不是理想的方法,而且充满了问题。

您报告看到的错误是由于您的 GCC 构建尝试使用 binutils 中的 GNU 汇编器,而生成的程序集打算使用 Apple cctools 中的 GNU 汇编器进行汇编。由于 Apple 的汇编器在多年前与上游的 GNU 汇编器分道扬镳,因此两者支持的汇编方言有所不同。

我能找到的用于构建 cctools 的所有说明都已过时,因此我进行了试验,直到我得到了一些有用的东西。我做了一些接近以下的事情:

curl -O http://opensource.apple.com/tarballs/cctools/cctools-829.tar.gz
tar xvzf cctools-829.tar.gz
cd cctools-829

然后,您需要修补两个文件才能在不依赖 LLVM 的情况下进行构建:

diff -ru cctools-829/libstuff/lto.c cctools-829.modified/libstuff/lto.c
--- cctools-829/libstuff/lto.c  1969-12-31 16:00:00.000000000 -0800
+++ cctools-829.modified/libstuff/lto.c 2013-04-05 03:18:34.000000000 -0700
@@ -376,4 +376,18 @@
        lto_dispose(mod);
 }

+#else 
+
+#include <stdlib.h>
+
+__private_extern__
+int
+is_llvm_bitcode(
+                struct ofile *ofile,
+                char *addr,
+                size_t size)
+{
+    return 0;
+}
+
 #endif /* LTO_SUPPORT */
diff -ru cctools-829/misc/libtool.c cctools-829.modified/misc/libtool.c
--- cctools-829/misc/libtool.c  1969-12-31 16:00:00.000000000 -0800
+++ cctools-829.modified/misc/libtool.c 2013-04-05 03:20:21.000000000 -0700
@@ -1369,8 +1369,11 @@
                    }
                }
                 }
-                else if(ofiles[i].arch_type == OFILE_Mach_O ||
-                        ofiles[i].arch_type == OFILE_LLVM_BITCODE){
+                else if(ofiles[i].arch_type == OFILE_Mach_O
+#ifdef LTO_SUPPORT
+                            || ofiles[i].arch_type == OFILE_LLVM_BITCODE
+#endif
+                            ){
                if(cmd_flags.ranlib == TRUE){
                    error("for architecture: %s file: %s is not an "
                          "archive (no processing done on this file)",

然后构建:

make install USE_DEPENDENCY_FILE=NO BUILD_DYLIBS=NO LTO= TRIE= DSTROOT=../build-cctools

并手动安装必要的部件:

cd ..
cp build-cctools/usr/bin/as i386-apple-gcc/bin/i386-apple-darwin-as
cp build-cctools/usr/bin/as i386-apple-gcc/i386-apple-darwin/bin/as
cp build-cctools/usr/bin/lipo i386-apple-gcc/bin/i386-apple-darwin-lipo
cp build-cctools/usr/bin/lipo i386-apple-gcc/i386-apple-darwin/bin/lipo
cp build-cctools/usr/bin/strip i386-apple-gcc/bin/i386-apple-darwin-strip
cp build-cctools/usr/bin/strip i386-apple-gcc/i386-apple-darwin/bin/strip

如果我们再次尝试构建 GCC,我们会发现构建过程被打乱了,因为我们新构建的汇编程序无法以 x86_64 为目标。由于这似乎与您要实现的目标无关,您可以将 --disable-multilib 参数添加到 GCC 的配置脚本中以防止它尝试这样做。

此时我在尝试构建 libgcc 时遇到了错误。这些错误是因为找不到合适的链接器,所以我想现在你需要download 并构建 ld64。

而在这一点上,我已经没有时间再进一步了。祝你好运!

【讨论】:

  • 传递 -m32 是不够的,因为我正在尝试构建 GNU 的 C 和 C++ 库的静态 32 位版本,我可以将其链接到 32 位二进制文​​件。 -m32 选项不会这样做。
  • 也许我理解错了,但是在构建之后,我最终得到了 $PREFIX/lib/i386/libstdc++.a,这似乎是 GNU C++ 库的 32 位版本。所有其他 GCC 支持库也是为 32 位和 64 位构建的。
  • 我会试一试你的方法,看看我是否可以复制它。感谢您添加所有这些细节。
  • 你的补丁文件不完整吗?尝试应用时出现以下错误:patching file misc/libtool.c - patch unexpectedly ends in middle of line - Hunk #1 FAILED at 1369. - 1 out of 1 hunk FAILED -- saving rejects to file misc/libtool.c.rej
  • 我还尝试了第二次单独的尝试,我使用了llvm-gcc42 并将CFLAGS='-m32' 添加到./configure 语句中。 make 步骤在编译大约 20 分钟后失败。
猜你喜欢
  • 2021-04-29
  • 2016-03-09
  • 2013-09-26
  • 2022-06-17
  • 2012-03-31
  • 1970-01-01
  • 2020-07-16
  • 2015-04-24
  • 1970-01-01
相关资源
最近更新 更多