在 Mojave 下编译 SpiderMonkey

自从 macOS 升级到 Mojave(10.14) 之后,编译 SpiderMonkey 变得有点不同。

有一点比较重要的变化是 libstdc++ 被替换成 libc++,这个修改是从 OS X 10.9 就开始有的修改,所以直接调用 ./configure 有这样的 warning 和报错:

checking whether the C++ compiler (/usr/bin/clang++  -fno-common -fno-rtti  -lobjc) actually is a C++ compiler... no
configure: error: /usr/bin/clang++  -fno-common -fno-rtti  -lobjc failed to compile and link a simple C++ source.
------ config.log ------
; return 0; }
configure:7837: checking for 64-bit OS
configure:7846: /usr/bin/clang -c  -std=gnu99 -Qunused-arguments  conftest.c 1>&5
configure:8119: checking for -framework ExceptionHandling
configure:8129: /usr/bin/clang -o conftest  -std=gnu99 -fno-common -Qunused-arguments   -lobjc -framework ExceptionHandling conftest.c  1>&5
configure:8151: checking for -dead_strip option to ld
configure:8162: /usr/bin/clang -o conftest  -std=gnu99 -fno-common -Qunused-arguments   -lobjc -Wl,-dead_strip conftest.c  1>&5
configure:9034: checking for valid debug flags
configure:9045: /usr/bin/clang -c  -std=gnu99 -fno-common -g -Qunused-arguments  conftest.c 1>&5
configure:9134: checking whether the C++ compiler (/usr/bin/clang++  -fno-common -fno-rtti  -lobjc) actually is a C++ compiler
configure:9153: /usr/bin/clang++ -o conftest  -fno-common -fno-rtti -Qunused-arguments   -lobjc conftest.C  1>&5
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated]
warning: include path for stdlibc++ headers not found; pass '-std=libc++' on the command line to use the libc++ standard library instead [-Wstdlibcxx-not-found]
configure:9147:10: fatal error: 'new' file not found
#include <new>
         ^~~~~
1 warning and 1 error generated.
configure: failed program was:
#line 9146 "configure"
#include "confdefs.h"
#include <new>
int main() {
int *foo = new int;
; return 0; }
configure: error: /usr/bin/clang++  -fno-common -fno-rtti  -lobjc failed to compile and link a simple C++ source.

很明显,报错原因是因为标准库 new 找不到,原因也在于 libstdc++ 被替换了,但是天杀的报错信息写错了。应该加入的 flag 是 -stdlib=libc++ 而不是 -std=libc++,这一点很坑爹。

但是加上了这个 flag 仍然不能编译,因为又有报错:

clang: error: invalid deployment target for -stdlib=libc++ (requires OS X 10.7 or later)

想了半天,我的系统不是 10.14 吗,后来才知道是因为要设置 deployment target,很坑爹,所以最后让我编译成功的命令是:

$ CXXFLAGS="-stdlib=libc++ -mmacosx-version-min=10.7"

写篇 blog 庆祝一下。