HomebrewやMacPortsを利用せずにtreeコマンドをインストールする

今回はMacにtreeコマンドをインストールしてみます。
HomebrewやMacPortsを使えば簡単にインストールできるようなのですが、今回はあえてソースからインストールを行いたいと思います。

事前準備

makeコマンドが利用できることを確認してください。
まれにmakeが行方不明になっている人もいるらしいです。

$ make --version
GNU Make 3.81
Copyright (C) 2006  Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

This program built for i386-apple-darwin11.3.0

次にgccコマンドが利用できることを確認します。

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.4.0
Thread model: posix

この辺りのコンパイル環境が初めから整っていることも、Macが優良な点ですね。

ソースのダウンロード

以下のコマンドでtreeのソースをダウンロードします。

$ curl -O http://pkgs.fedoraproject.org/repo/pkgs/tree/tree-1.7.0.tgz/abe3e03e469c542d8e157cdd93f4d8a6/tree-1.7.0.tgz

同時に展開もしておきましょう。

$ tar xvfz tree-1.7.0.tgz 
x tree-1.7.0/CHANGES
x tree-1.7.0/INSTALL
x tree-1.7.0/LICENSE
x tree-1.7.0/Makefile
x tree-1.7.0/README
x tree-1.7.0/TODO
x tree-1.7.0/color.c
x tree-1.7.0/hash.c
x tree-1.7.0/html.c
x tree-1.7.0/json.c
x tree-1.7.0/strverscmp.c
x tree-1.7.0/tree.c
x tree-1.7.0/tree.h
x tree-1.7.0/unix.c
x tree-1.7.0/xml.c
x tree-1.7.0/doc/tree.1
x tree-1.7.0/doc/tree.1.fr
x tree-1.7.0/doc/xml.dtd

コンパイル、そしてインストール

まず初めにコンパイルオプションを設定するためMakefileを一部編集します。
55行目あたりにOS Xのオプションが書かれていますので、アンコメントしていきましょう。

$ cd tree-1.7.0
$ vim Makefile
・・・
# Uncomment for OS X:
CC=cc
CFLAGS=-O2 -Wall -fomit-frame-pointer -no-cpp-precomp
LDFLAGS=
MANDIR=/usr/share/man/man1
OBJS+=strverscmp.o
・・・

それではコンパイルを行います。

$ make 
cc -O2 -Wall -fomit-frame-pointer -no-cpp-precomp -c -o strverscmp.o strverscmp.c
cc  -o tree tree.o unix.o html.o xml.o json.o hash.o color.o strverscmp.o

無事に成功しましたね。
ではビルドに成功したtreeコマンドをインストールします。

$ sudo make install
Password:
install -d /usr/bin
install -d /usr/share/man/man1
if [ -e tree ]; then \
        install tree /usr/bin/tree; \
    fi
install doc/tree.1 /usr/share/man/man1/tree.1

※デフォルトで/usr/binにtreeコマンドがインストールされますので、管理者権限が必要です。もしそれが嫌ならインストールパスも書き換え、pathさえ通せば問題ないでしょう。

動作確認

以下のコマンドを実行します。

$ tree --version
tree v1.7.0 (c) 1996 - 2014 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro 

無事にユーザーが参照できる場所にtreeコマンドがインストールされていそうです。
例えばこんな順番でコマンドを実行すると・・・

$ mkdir -p hoge/hogehoge/hogehogehoge
$ touch hoge/test.txt
$ tree hoge
hoge
├── hogehoge
│   └── hogehogehoge
└── test.txt

2 directories, 1 file

大変見やすくなりましたね!

まとめ

HomebrewやMacPortsをインストールしたくない、でもtreeコマンドを使いたい!
というかたは是非試してみてください!!! (あんまり、そんな人もいないかな・・・)