Til*_*ill 15 c macos arm autotools apple-silicon
如何在 macOS 11(英特尔)上编译 C 项目以在 ?Silicon 上工作?
我当前的构建脚本非常简单:
./configure
make
sudo make install
Run Code Online (Sandbox Code Playgroud)
我已经使用了尝试--host和--target标志有aarch64-apple-darwin和arm-apple-darwin没有任何的运气。
二进制文件始终默认为x86_64:
> file foobar.so
foobar.so: Mach-O 64-bit bundle x86_64
Run Code Online (Sandbox Code Playgroud)
更新:
似乎在--host指定时找不到 cc 和 gcc 。
checking for arm-apple-darwin-cc... no
checking for arm-apple-darwin-gcc... no
Run Code Online (Sandbox Code Playgroud)
我在这个页面上找到了一个使用这个的提示:
-target arm64-apple-macos11
Run Code Online (Sandbox Code Playgroud)
当我从我的 mac 运行它时:
clang++ main.cpp -target arm64-apple-macos11
Run Code Online (Sandbox Code Playgroud)
生成的 a.out 二进制文件被列为:
% file a.out
a.out: Mach-O 64-bit executable arm64
Run Code Online (Sandbox Code Playgroud)
我安装了 XCode 12.2。
我面前没有 Arm Mac,所以我假设这可行。
我们最终解决了这个问题,并能够在 GitHub Actions 的 x86-64 机器上编译darwin-arm64和二进制文件。debian-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...]
Run Code Online (Sandbox Code Playgroud)
这relink.sh:
#!/bin/bash
set -e
printUsage() {
echo "$0 <shared-object> <prefix>"
exit 1
}
if [[ ! -f "$1" || -z "$2" ]]; then
printUsage
exit 1
fi
INFILE=$1
PREFIX=$2
links=(libjemalloc libhiredis [snip...])
if [ -z "$PREFIX" ]; then
PREFIX=libs
fi
for link in ${links[@]}; do
FROM=$(otool -L "$INFILE"|grep $link|awk '{print $1}')
FILE=$(basename -- "$FROM")
TO="$PREFIX/$FILE"
echo "$FROM -> $TO"
install_name_tool -change "$FROM" "$TO" "$1"
done
Run Code Online (Sandbox Code Playgroud)