LoginSignup
7
2

More than 5 years have passed since last update.

OCamlMakefileの使い方

Last updated at Posted at 2017-05-02

OCamlMakefileとは?

OCamlMakefileはOCamlのプロジェクトのビルドを助けてくれるツールの一種。
その名の通りではあるが、GNU makeで書かれており、GNU makeが導入されている環境なら使うことができる1

使い方

上述の通り、GNU makeで書かれているので、自分のプロジェクトのMakefileにOCamlMakefileをincludeし、変数やルールを適切に設定することで使用することができる。
(GNU make自体に詳しくなくても使えますが、もしmakeを勉強したいならオライリーのpdfが無料公開されているので、読んでみるといいといいと思います。2)

OCamlMakefile自体は公式サイトからダウンロードできる。

使用例1: hello world

まずは簡単のため

hello.ml
let () =
    Printf.printf "Hello, world!\n"

というファイルをビルドすることを考える。
同じディレクトリにダウンロードしてきたOCamlMakefileを配置し、以下のようにMakefileを書くと良い。

Makefile
SOURCES = hello.ml
RESULT  = hello

-include OCamlMakefile

実行例は以下の通り。

console
$ ls
Makefile       OCamlMakefile  hello.ml
$ make #デフォルトではバイトコードの実行ファイルを生成する。
$ ./hello
Hello, world!
$ make clean #生成された実行ファイルや.cmi,.cmoなどの中間ファイルを削除

使用例2:ネイティブコードを生成する。

デフォルトではバイトコードの実行ファイルが生成されるが、これはbyte-codeターゲットがOCamlMakefile中で一番上に定義されていることによる。よって、OCamlMakefileをincludeする前に自分でルールを定義すればネイティブコード等他のターゲットを実行することができる。

例えば以下のようにMakefileを定義すればよい。

Makefile
SOURCES = hello.ml
RESULT  = hello
# OCamlMakefileをインクルードする前に定義すること。
default: native-code 
-include OCamlMakefile

makefileに慣れ親しんだ人には当然かもしれないが、defaultというターゲット名に意味はないので、

Makefile
SOURCES = hello.ml
RESULT  = hello
all: native-code 
-include OCamlMakefile

と定義しても意味は変わらない。
byte-codenative-code等はそれぞれbcncにエイリアスされているため、Makefileを変更せずとも、

console
$ make nc

等でもネイティブの実行ファイルを生成できる。

この他OCamlMakefileには以下のようなターゲットが定義されている。

byte-code                      (bc)
byte-code-nolink               (bcnl) - no linking stage
byte-code-library              (bcl)
native-code                    (nc)
native-code-nolink             (ncnl) - no linking stage
native-code-library            (ncl)
debug-code                     (dc)
debug-code-nolink              (dcnl) - no linking stage
debug-code-library             (dcl)
profiling-byte-code            (pbc)
profiling-byte-code-library    (pbcl)
profiling-native-code          (pnc)
profiling-native-code-library  (pncl)
byte-code-dll                  (bcd)
native-code-dll                (ncd)
pack-byte-code                 (pabc)
pack-native-code               (panc)
toplevel                       (top)
subprojs

使用例3:異なるコンパイラを使う

たとえば、ネイティブコード生成にはデフォルトではocamloptが使われる。もしocamlopt.optを使いたい場合は以下のように定義すれば良い。

Makefile
SOURCES = hello.ml
RESULT  = hello
OCAMLOPT= ocamlopt.opt
default: native-code
-include OCamlMakefile

なお、OCamlMakefileでデフォルトで使うコマンド等は
https://github.com/mmottl/ocaml-makefile/blob/master/OCamlMakefile#L225-L330
の辺りで定義されている。
GNU makeに詳しくない人も、この部分は比較的容易に読めるので、何が変更可能かは、OCamlMakefileのコードを直接読んで確認してみてください。

この他、公式サイトに書いてある通り、デバッグやプロファイル、ドキュメント生成向けの機能等もあるらしいので気になる人は直接読んでみてほしい。

参考サイト


  1. 検証はしていないが、BSD系のmakeでも使用できる可能性はある。 

  2. 公式サイトだと章毎に別れているので、https://github.com/jr4qpv/GNU_Make_3rd_jp 等を使うと良いと思います。 

7
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2