きりかノート 3冊め

おあそびプログラミング

port:clang-3.1がMountain Lionで正しく動作しないように見える件

boehmgcの件調べてて気付いたんだけど、port:clang-3.1がMountain Lion上でなんかおかしい。

   % sudo port -v configure boehmgc configure.compiler=macports-clang-3.1
     :
   checking for gcc... /opt/local/bin/clang-mp-3.1
   checking whether the C compiler works... no
   configure: error: in `/opt/local/var/macports/build/_Volumes_CHome_kimuraw_proj_
   macports_ports_devel_boehmgc/boehmgc/work/gc-7.2':
   configure: error: C compiler cannot create executables
   See `config.log' for more details

コンパイルエラーならともかく、configureが通らないってどういうことよ。まずはメッセージのとおりconfig.logを見てみよう。

   configure:3522: checking whether the C compiler works
   configure:3544: /opt/local/bin/clang-mp-3.1 -pipe -O2 -arch x86_64 -I/opt/local/
   include -D_XOPEN_SOURCE=600 -D_DARWIN_C_SOURCE -L/opt/local/lib -arch x86_64 con
   ftest.c  >&5
   Undefined symbols for architecture x86_64:
     "start", referenced from:
        -u command line option
   ld: symbol(s) not found for architecture x86_64
   clang: error: linker command failed with exit code 1 (use -v to see invocation)
   configure:3548: $? = 1
   configure:3586: result: no
   configure: failed program was:
   | /* confdefs.h */
   | #define PACKAGE_NAME "gc"
   | #define PACKAGE_TARNAME "gc"
   | #define PACKAGE_VERSION "7.2"
   | #define PACKAGE_STRING "gc 7.2"
   | #define PACKAGE_BUGREPORT "gc@linux.hpl.hp.com"
   | #define PACKAGE_URL ""
   | #define GC_VERSION_MAJOR 7
   | #define GC_VERSION_MINOR 2
   | #define PACKAGE "gc"
   | #define VERSION "7.2"
   | /* end confdefs.h.  */
   |
   | int
   | main ()
   | {
   |
   |   ;
   |   return 0;
   | }
   configure:3591: error: in `/opt/local/var/macports/build/_Volumes_CHome_kimuraw_
   proj_macports_ports_devel_boehmgc/boehmgc/work/gc-7.2':
   configure:3593: error: C compiler cannot create executables
   See `config.log' for more details

conftest.cがコンパイルエラーになるから、動作するコンパイラがないって判定なわけね。

簡単なプログラムをコンパイルしてみる

   % cat a.c
   int main() {
       return 0;
   }

こんなのを用意。

   % clang-mp-3.1 a.c    # port:clang-3.1
   Undefined symbols for architecture x86_64:
     "start", referenced from:
    -u command line option
   ld: symbol(s) not found for architecture x86_64
   clang: error: linker command failed with exit code 1 (use -v to see invocation)
   %

ええー。

   % clang-mp-3.0 a.c    # port:clang-3.0
   %

そりゃそうだ。

   % /usr/bin/clang a.c  # Xcode-4.4.1
   %

ですよねー。ということで、port:clang-3.1だけダメみたい。

バージョン3.1の問題?

感じとしては、clang-3.1だけcrt1.oにリンクできてないっぽいよね。

   % clang-mp-3.1 -lcrt1.o a.c
   % # 成功!

うん。

clangのコードのほうをあれこれ調べてみた結果、どうもlib/Driver/Tools.cppのあたりがあやしいのではないかと思うのですよ。

   4162 :       } else {
   4163 :         if (getDarwinToolChain().isMacosxVersionLT(10, 5))
   4164 :           CmdArgs.push_back("-lcrt1.o");
   4165 :         else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
   4166 :           CmdArgs.push_back("-lcrt1.10.5.o");
   4167 :         else if (getDarwinToolChain().isMacosxVersionLT(10, 8))
   4168 :           CmdArgs.push_back("-lcrt1.10.6.o");
   4169 :
   4170 :         // darwin_crt2 spec is empty.
   4171 :       }

ここね。ようするにターゲットの環境がMac OS Xでバージョンが10.8>=のとき"-lcrt1"しないってことなんだけど、これはマチガイなんじゃないかなーと。少なくともなんかにリンクする必要はあるでしょ。

試しにここをrevertしてみると、ちゃんと動いたりするわけで。

   Index: lib/Driver/Tools.cpp
   ===================================================================
   --- lib/Driver/Tools.cpp        (revision 161487)
   +++ lib/Driver/Tools.cpp        (working copy)
   @@ -4425,7 +4425,7 @@
                    CmdArgs.push_back("-lcrt1.o");
                  else if (getDarwinToolChain().isMacosxVersionLT(10, 6))
                    CmdArgs.push_back("-lcrt1.10.5.o");
   -              else if (getDarwinToolChain().isMacosxVersionLT(10, 8))
   +              else
                    CmdArgs.push_back("-lcrt1.10.6.o");

                  // darwin_crt2 spec is empty.

-### で処理の中身を確認してみる

clangやgccに"-###"を渡すと詳しい動作を出力してくれる(って今回知ったんだけど)。これを利用してみよう。

   % clang-mp-3.1 -### a.c
   clang version 3.1 (branches/release_31)
   Target: x86_64-apple-darwin12.0.0
   Thread model: posix
    "/Volumes/CUnix/opt/local/libexec/llvm-3.1/bin/clang" "-cc1" "-triple" "x86_64-
   apple-macosx10.8.0" "-emit-obj" "-mrelax-all" "-disable-free" "-main-file-name"
   "a.c" "-pic-level" "2" "-mdisable-fp-elim" "-masm-verbose" "-munwind-tables" "-t
   arget-cpu" "core2" "-target-linker-version" "128.2" "-resource-dir" "/Volumes/CU
   nix/opt/local/libexec/llvm-3.1/bin/../lib/clang/3.1" "-fmodule-cache-path" "/var
   /folders/7w/v9hj8n9s5dd19w11q_7tlns80000gp/T/clang-module-cache" "-fdebug-compil
   ation-dir" "/Users/kimuraw" "-ferror-limit" "19" "-fmessage-length" "0" "-stack-
   protector" "1" "-mstackrealign" "-fblocks" "-fobjc-runtime-has-arc" "-fobjc-runt
   ime-has-weak" "-fobjc-dispatch-method=mixed" "-fobjc-default-synthesize-properti
   es" "-fdiagnostics-show-option" "-o" "/var/folders/7w/v9hj8n9s5dd19w11q_7tlns800
   00gp/T/a-LXqY8N.o" "-x" "c" "a.c"
    "/opt/local/libexec/llvm-3.1/bin/ld" "-demangle" "-dynamic" "-arch" "x86_64" "-
   macosx_version_min" "10.8.0" "-o" "a.out" "/var/folders/7w/v9hj8n9s5dd19w11q_7tl
   ns80000gp/T/a-LXqY8N.o" "-lSystem" "/Volumes/CUnix/opt/local/libexec/llvm-3.1/bi
   n/../lib/clang/3.1/lib/darwin/libclang_rt.osx.a"

うん、-lcrt1がないもんね。シンボル"start"がないというのはわかる。

   % clang-mp-3.0 -### a.c
   clang version 3.0 (tags/RELEASE_30/final)
   Target: x86_64-apple-darwin12.0.0
   Thread model: posix
    "/Volumes/CUnix/opt/local/libexec/llvm-3.0/bin/clang" "-cc1" "-triple" "x86_64-
   apple-macosx10.8.0" "-emit-obj" "-mrelax-all" "-disable-free" "-disable-llvm-ver
   ifier" "-main-file-name" "a.c" "-pic-level" "1" "-mdisable-fp-elim" "-masm-verbo
   se" "-munwind-tables" "-target-cpu" "core2" "-target-linker-version" "128.2" "-r
   esource-dir" "/Volumes/CUnix/opt/local/libexec/llvm-3.0/bin/../lib/clang/3.0" "-
   fmodule-cache-path" "/var/folders/7w/v9hj8n9s5dd19w11q_7tlns80000gp/T/clang-modu
   le-cache" "-ferror-limit" "19" "-fmessage-length" "0" "-stack-protector" "1" "-f
   blocks" "-fobjc-runtime-has-arc" "-fobjc-runtime-has-weak" "-fobjc-dispatch-meth
   od=mixed" "-fdiagnostics-show-option" "-o" "/var/folders/7w/v9hj8n9s5dd19w11q_7t
   lns80000gp/T/a-p09ZxO.o" "-x" "c" "a.c"
    "/opt/local/libexec/llvm-3.0/bin/ld" "-demangle" "-dynamic" "-arch" "x86_64" "-
   macosx_version_min" "10.8.0" "-o" "a.out" "-lcrt1.10.6.o" "/var/folders/7w/v9hj8
   n9s5dd19w11q_7tlns80000gp/T/a-p09ZxO.o" "-lSystem" "/Volumes/CUnix/opt/local/lib
   exec/llvm-3.0/bin/../lib/clang/3.0/lib/darwin/libclang_rt.osx.a"

こっちは"-lcrt1.10.6.o"があるね。

   % /usr/bin/clang -### a.c
   Apple clang version 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn)
   Target: x86_64-apple-darwin12.0.0
   Thread model: posix
    "/usr/bin/clang" "-cc1" "-triple" "x86_64-apple-macosx10.8.0" "-emit-obj" "-mre
   lax-all" "-disable-free" "-disable-llvm-verifier" "-main-file-name" "a.c" "-pic-
   level" "1" "-mdisable-fp-elim" "-relaxed-aliasing" "-masm-verbose" "-munwind-tab
   les" "-target-cpu" "core2" "-target-linker-version" "133.3" "-resource-dir" "/us
   r/bin/../lib/clang/4.0" "-fmodule-cache-path" "/var/folders/7w/v9hj8n9s5dd19w11q
   _7tlns80000gp/T/clang-module-cache" "-fdebug-compilation-dir" "/Users/kimuraw" "
   -ferror-limit" "19" "-fmessage-length" "0" "-stack-protector" "1" "-mstackrealig
   n" "-fblocks" "-fobjc-runtime-has-arc" "-fobjc-runtime-has-weak" "-fobjc-dispatc
   h-method=mixed" "-fobjc-default-synthesize-properties" "-fdiagnostics-show-optio
   n" "-o" "/var/folders/7w/v9hj8n9s5dd19w11q_7tlns80000gp/T/a-B6ZlJc.o" "-x" "c" "
   a.c"
    "/usr/bin/ld" "-demangle" "-dynamic" "-arch" "x86_64" "-macosx_version_min" "10
   .8.0" "-o" "a.out" "/var/folders/7w/v9hj8n9s5dd19w11q_7tlns80000gp/T/a-B6ZlJc.o"
    "-lSystem" "/usr/bin/../lib/clang/4.0/lib/darwin/libclang_rt.osx.a"

あれれ、、、「これに-lcrt1があるね。ってことはclang-3.1のバグだよね」という予定だったのに。ていうかなんでXcodeのclangこれで機能するんだ?

ということで、もーわかんない!!

opensource.apple.comでのXcode-4.4の公開待ちです。

(2012-08-12 追記)ちょうど似たようなチケットがmacports.orgのほうに#35629として登録されてて、それを読んで理解した。port:ld64とport:clang-3.1でミスマッチが起きてて、clangは10.8対応でld64は未対応な状態になってる。なのでリンカをport:ld64じゃなくって、Xcode-4.4のやつを使ってやれば解消する。