在 Intel 机器上构建 Apple Silicon 二进制文件

Build Apple Silicon binary on Intel machine

如何在 macOS 11 (Intel) 上编译 C 项目以在 Silicon 上运行?

我当前的构建脚本非常简单:

./configure
make
sudo make install

我试过将 --host--target 标志与 aarch64-apple-darwinarm-apple-darwin 一起使用,但没有成功。

二进制总是默认为x86_64:

> file foobar.so
foobar.so: Mach-O 64-bit bundle x86_64

更新: 当指定 --host 时,似乎找不到 cc 和 gcc。

checking for arm-apple-darwin-cc... no
checking for arm-apple-darwin-gcc... no

我在 this page 上找到了使用它的提示:

-target arm64-apple-macos11

当我 运行 这个来自我的 mac:

clang++ main.cpp -target arm64-apple-macos11

生成的 a.out 二进制文件列为:

% file a.out
a.out: Mach-O 64-bit executable arm64

我安装了 XCode 12.2。

我面前没有 Arm Mac,所以我假设这可行。

先生Curious 也对与 M1 的交叉编译感到好奇。一个意想不到的解决方案是 Zig。它的目标是成为交叉编译 C 等的最佳方式;它很容易从 Linux.

瞄准 M1

几周前有一系列关于交叉编译到 M1 的流:Part 1 shows how to use Zig as a cross-compiler in existing makefiles, and in Part 3他们成功地演示了在 Linux 上为 M1 编译 Redis。

强烈推荐。

我们最终解决了这个问题,并能够在 GitHub Actions 的 x86-64 机器上编译 darwin-arm64debian-aarch64 二进制文件。

我们为 arm64 预编译了所有依赖项,并静态和动态链接它们。

export RELAY_DEPS_PATH=./build-deps/arm64
export PKG_CONFIG_PATH=./build-deps/arm64/lib/pkgconfig

cd ./relay-deps
TARGET=./build-deps make install

cd ./relay
phpize
./configure CFLAGS='-target arm64-apple-macos' \
  --host=aarch64-apple-darwin \
  --enable-relay-jemalloc-prefix
  [snip...]

make

# Dynamically linked binary
cc --target=arm64-apple-darwin \
  ${wl}-flat_namespace ${wl}-undefined ${wl}suppress \
  -o .libs/relay.so -bundle .libs/*.o \
  -L$RELAY_DEPS_PATH/lib -lhiredis -ljemalloc_pic [snip...]

# re-link to standard paths
./relay-deps/utils/macos/relink.sh .libs/relay.so /usr/local/lib
cp .libs/relay.so modules/relay.so

# Build a statically linked shared object
cc --target=arm64-apple-darwin \
  ${wl}-flat_namespace ${wl}-undefined ${wl}suppress \
  -o .libs/relay-static.so -bundle .libs/*.o \
  $RELAY_DEPS_PATH/lib/libhiredis.a \
  $RELAY_DEPS_PATH/lib/libjemalloc_pic.a \
  [snip...]

relink.sh:

#!/bin/bash
set -e

printUsage() {
    echo "[=11=] <shared-object> <prefix>"
    exit 1
}

if [[ ! -f "" || -z "" ]]; then
    printUsage
    exit 1
fi

INFILE=
PREFIX=

links=(libjemalloc libhiredis [snip...])

if [ -z "$PREFIX" ]; then
    PREFIX=libs
fi

for link in ${links[@]}; do
    FROM=$(otool -L "$INFILE"|grep $link|awk '{print }')
    FILE=$(basename -- "$FROM")
    TO="$PREFIX/$FILE"

    echo "$FROM -> $TO"
    install_name_tool -change "$FROM" "$TO" ""
done