[autoconf] 如何让 AC_TRY_LINK 打印原始出错信息?

安装 Ruby 的时候出错了,提示:

$ rbenv install -k 2.7.1 -vvv
...
checking for unistd.h... yes
checking minix/config.h usability... no
checking minix/config.h presence... no
checking for minix/config.h... no
checking whether it is safe to define __EXTENSIONS__... yes
checking for cd using physical directory... cd -P
checking whether CFLAGS is valid... yes
checking whether LDFLAGS is valid... no
configure: error: something wrong with LDFLAGS="-L/Users/*/.rbenv/versions/2.7.1/lib 
-L/usr/local/opt/readline -L/usr/local/opt/[email protected]/lib"
make: *** No targets specified and no makefile found.  Stop.

看了编译脚本,出错信息是由 config.ac 打印的:

AC_TRY_LINK([], [],
    [AC_MSG_RESULT(yes)],
    [
    cd .. && rm -fr tmp.$$.try_link
    AC_MSG_RESULT(no)
    AC_MSG_ERROR([something wrong with LDFLAGS="$LDFLAGS"])
    ]

这段代码创建了一个空的测试项目,然后试着编译,成功返回 yes,失败返回 no 并打印一句出错信息。

这句让人摸不着头脑的出错信息 something wrong with ... 是脚本编写者给的,并不是编译输出的原始信息。有没有办法让 AC_TRY_LINK 输出原始出错信息?


https://gnu.huihoo.org/autoconf-2.13/html_node/autoconf_43.html

  • Macro: AC_TRY_LINK (includes, function-body, [action-if-found [, action-if-not-found]]))

    Depending on the current language (see section Language Choice), create a test program to see whether a function whose body consists of function-body can be compiled and linked.

参考《 http://yatsugatake.org/kodaira/2019/12/16/rubyインストールでconfigure-error-something-wrong-with-ldflags-l/ 》一文,在 ~/.rbenv/sources/2.7.1/ruby-2.7.1/configureac_fn_c_try_link 方法中把出错信息拷贝出来,以免清场的时候销毁了:

  (eval "$ac_link") 2>conftest.err
  ac_status=$?
  if test -s conftest.err; then
    grep -v '^ *+' conftest.err >conftest.er1
    cat conftest.er1 >&5
    mv -f conftest.er1 conftest.err
  fi
+ mkdir -p ~/Downloads/conftest-err
+ cp -R conftest.err ~/Downloads/conftest-err/conftest.err.$1

然后手工编译就得到了不同之前的错误信息:

$ cd ~/.rbenv/sources/2.7.1/ruby-2.7.1
$ ./configure --prefix=$HOME/.rbenv/versions/2.7.1/
$ cat ~/Downloads/conftest-err/conftest.err.8281
while processing /var/folders/7x/4lfxaf2pdu9_4cqs8m6crri30000gn/T/conftest-65c613.o:
warning: line table paramters mismatch. Cannot emit.

8281 对应的 configure 文件的行,正是打印 error: something wrong with LDFLAGS= 的地方,应该就是原始错误信息了。

从谷歌搜索结果看,这个错误跟 Xcode 相关。有趣的是出错信息里还有个笔误:把 parameters 写成了 paramters。

在谷歌搜索 line table paramters mismatch 得不到什么有用的信息,基本上都是 Flutter 编译错误 log。

回到 configure 本身,把 $ac_link 打印出来看看:

$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5
# =>
# clang \
#     -o conftest \
#     -g \
#     -O2 \
#     -I/usr/local/opt/readline/include \
#     -I/usr/local/opt/[email protected]/include \
#     -L/usr/local/opt/readline \
#     -L/usr/local/opt/[email protected]/lib \
#     conftest.c  >&5

正是 clang 的 -g 参数导致了 line table 错误。我系统中至少有 3 份 clang (built-in,homebrew, swiftenv),当前使用的是:

⋊>  which clang
/Users/*/.swiftenv/shims/clang

⋊>  clang --version
Apple clang version 7.0.0 ([email protected]:apple/swift-clang.git 537fb1fd3a8fab5c200a7b9000ab362e95449472) ([email protected]:apple/swift-llvm.git c5340df2d1aed4cd16fb43e1ce11139a407f2055) (based on LLVM 7.0.0)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2019-09-11-a.xctoolchain/usr/bin

换成以下位置的 clang 就不会出错了:

⋊>  /usr/bin/clang --version
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

⋊>  /usr/local/opt/llvm/bin/clang --version
clang version 8.0.0 (tags/RELEASE_800/final)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin

所以,网上那些号称 sudo xcode-select -s /Library/Developer/CommandLineToolssudo xcode-select -s /Applications/Xcode.app/ 可以解决问题的人,是碰巧解决了问题,并没有真正发现问题。这两句在我电脑上就没有效果,因为执行完了之后,仍然选择的是 swift-DEVELOPMENT-SNAPSHOT 的 toolchain——原本一直用的就是这份 toolchain。

所以在我这里最简单的解决问题方法是:

⋊>  swiftenv global <stable-version>
1 个赞