Sunday, January 30, 2011

[MacOSX] Gitのビルド

Mac OS XでGitをビルドするのは簡単です。Xcodeさえインストールしてあれば、Gitのサイトからsourceを落としてきて、伸張して、ターミナルでディレクトリに移動(cd)して、以下のようなコマンドを入力していけば済みます。
$ ./configure --prefix=/usr/local && make -j3 && sudo make install

さて。このビルドによって作成されたバイナリがどのライブラリにlinkしているかを調べるには、otoolというコマンドを使います。
$ otool -L git
git:
/opt/local/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
/opt/local/lib/libiconv.2.dylib (compatibility version 8.0.0, current version 8.0.0)
/opt/local/lib/libcrypto.1.0.0.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.1)

はてさて。/opt/localほにゃららにlinkしています。Gitは、/opt/local/libや/sw/libというディレクトリが存在するかをチェックして、もし存在するならそこにあるライブラリにlinkするようにしているのでこういうことになります。以下がMakefileの該当箇所です。
ifeq ($(uname_S),Darwin)
ifndef NO_FINK
ifeq ($(shell test -d /sw/lib && echo y),y)
BASIC_CFLAGS += -I/sw/include
BASIC_LDFLAGS += -L/sw/lib
endif
endif
ifndef NO_DARWIN_PORTS
ifeq ($(shell test -d /opt/local/lib && echo y),y)
BASIC_CFLAGS += -I/opt/local/include
BASIC_LDFLAGS += -L/opt/local/lib
endif
endif
PTHREAD_LIBS =
endif

この/swと/opt/localは、それぞれFinkMacPorts(旧DarwinPorts)によって作成されるものです。これらによってインストールされるライブラリは大抵/usr/lib以下にあるものより新しいものなので、普段はこれでも問題ありません。

ただ、例えばビルドしたバイナリを他の環境で動かしたい時などは問題が出る場合があります。相手の環境にFinkやMacPortsがインストールされているとは限りませんし、またたとえそれらをインストールしていても、自分の環境と同じライブラリがインストールされているとも限りません。そのため、バイナリ配布をする際には必ずotoolコマンドでチェックして、自分の環境にしかインストールされていないライブラリにlinkしていないかを確認する必要があります。

では本題。Gitでビルドする際に、/opt/local/libや/sw/libのライブラリにlinkしないようにするにはどうすればいいのか。

  1. FinkやMacPortsをアンインストール———そしてビルダーデビュー(笑)

  2. /opt/localや/swを一時的に別の場所に退避———デスクトップとか。ゴミ箱オススメ(ぉぃ

  3. Makefileの該当箇所を弄る———削除しちゃえ(爆

  4. 'NO_FINK'や'NO_DARWIN_PORTS'を定義———これが一番マシです


4の'NO_FINK'や'NO_DARWIN_PORTS'は、さっきのMakefileの抜粋の2行目と8行目にも出てきました。configureにコメントで解説が書いてあります。
# Define NO_FINK if you are building on Darwin/Mac OS X, have Fink
# installed in /sw, but don't want GIT to link against any libraries
# installed there. If defined you may specify your own (or Fink's)
# include directories and library directories by defining CFLAGS
# and LDFLAGS appropriately.
#
# Define NO_DARWIN_PORTS if you are building on Darwin/Mac OS X,
# have DarwinPorts installed in /opt/local, but don't want GIT to
# link against any libraries installed there. If defined you may
# specify your own (or DarwinPort's) include directories and
# library directories by defining CFLAGS and LDFLAGS appropriately.

手順としては、

  1. ./configureを走らせる

  2. config.mak.autogenというファイルを開く

  3. 適当な行に'NO_FINK=YesPlease'や'NO_DARWIN_PORTS=YesPlease'などと追加する

  4. makeする


とやっていきます。するとこんな感じになります。
$ otool -L git
git:
/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.3)
/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)
/usr/lib/libcrypto.0.9.8.dylib (compatibility version 0.9.8, current version 0.9.8)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.1)

めでたしめでたしです。これで安心して/opt/localと/swをゴミ箱に捨てられます(ぉ

No comments:

Post a Comment