YAMAGUCHI::weblog

海水パンツとゴーグルで、巨万の富を築きました。カリブの怪物、フリーアルバイター瞳です。

Ocamlnetをインストールする

動機

ヘタれなのでOSのコアをいじったりechoサーバ書いたりってのを始めたら大変だと思い、Web系で手っ取り早く遊びたいと思ったので。

メモ

今回はversion 3.0beta2のインストールをしました。(執筆時最新)

$ wget http://download.camlcity.org/download/ocamlnet-3.0test2.tar.gz
$ tar xzf ocamlnet-3.0test2.tar.gz
$ cd ocamlnet-3.0test2
$ ./configure
Welcome to Ocamlnet version 3.0test2
Checking operating system... Generic
Checking for findlib... found
Checking multi-threading support... posix (ok)
Checking word size... 64 bit
Checking for PCRE... found
Checking whether Ocaml has Printexc.register_printer... ./configure: line 27: code_printexc_register_printer: command not found
found
Checking whether Ocaml has fancy page tables... found
Checking for POSIX shared memory... found
Checking for POSIX semaphores... not found
Checking for POSIX fadvise... not found
Checking for POSIX fallocate... not found
Checking for POSIX memalign... found
Checking for POSIX pthread... found
Checking for syslog... found
Checking for GPROF... found
Checking for win32... no

Effective options:
    -disable-gtk
    -disable-gtk2
    -disable-tcl
    -disable-ssl
    -disable-zip
    -disable-apache
    -without-nethttpd
    -without-rpc-auth-dh
    -bindir /opt/local/bin
    -datadir /opt/local/lib/ocaml/site-lib/netstring

Writing Makefile.conf

Please check Makefile.conf.

You can now compile Ocamlnet by invoking
   make all
for the bytecode compiler, and optionally by invoking
   make opt
for the native-code compiler (if supported on your architecture).
Finally, a
   make install
will install the package(s).

無事configure完了。最低限だけあればよいのでこれでよし。

$ make all
$ sudo make install

無事インストールも完了。使えるかテストします。まずはインタプリタ。「トップレベルでロードする」という意味がわからず5分ぐらいGoogle先生にご指導いただいた。Mac OSで実行した場合はすんなりできたけど、Ubuntuのほうでは、.ocamlinitに設定が必要でした。

$ vim .ocamlinit
+ #use "topfind"

これで確認します。

$ ocaml
        Objective Caml version 3.11.2

Findlib has been successfully loaded. Additional directives:
  #require "package";;      to load a package
  #list;;                   to list the available packages
  #camlp4o;;                to load camlp4 (standard syntax)
  #camlp4r;;                to load camlp4 (revised syntax)
  #predicates "p,q,...";;   to set these predicates
  Topfind.reset();;         to force that packages will be reloaded
  #thread;;                 to enable threads

# #use "http_get.ml";;
/opt/local/lib/ocaml/unix.cma: loaded
/opt/local/lib/ocaml/bigarray.cma: loaded
/opt/local/lib/ocaml/site-lib/netsys: added to search path
/opt/local/lib/ocaml/site-lib/netsys/netsys_oothr.cmo: loaded
/opt/local/lib/ocaml/site-lib/netsys/netsys.cma: loaded
/opt/local/lib/ocaml/site-lib/equeue: added to search path
/opt/local/lib/ocaml/site-lib/equeue/equeue.cma: loaded
/opt/local/lib/ocaml/site-lib/pcre: added to search path
/opt/local/lib/ocaml/site-lib/pcre/pcre.cma: loaded
/opt/local/lib/ocaml/site-lib/netstring: added to search path
/opt/local/lib/ocaml/site-lib/netstring/netstring.cma: loaded
/opt/local/lib/ocaml/site-lib/netstring/netstring_top.cmo: loaded
/opt/local/lib/ocaml/site-lib/netstring/netaccel.cma: loaded
/opt/local/lib/ocaml/site-lib/netstring/netaccel_link.cmo: loaded
/opt/local/lib/ocaml/site-lib/netclient: added to search path
/opt/local/lib/ocaml/site-lib/netclient/netclient.cma: loaded
val get_and_print : string -> unit = <fun>
# 

さらにコンパイル。examplesにあるhttp_get.mlをちょっと修正。

(* #require "netclient";; *)

(* This example shows how to get a file from a HTTP server using
 * the Convenience module.
 *
 * Load this into the toplevel, then:
 * get_and_print "http://www.caml.org/";;
 *)

open Http_client.Convenience;;

let get_and_print url =
  let s = http_get url in
  print_string s;
  flush stdout
;;

get_and_print "http://www.caml.org/";

下記でコンパイルできました。

$ ocamlfind ocamlc -o http_get -linkpkg -package netclient http_get.ml

ライブラリをリンクしてコンパイルする方法がよくわからなかったのと、ocamlcとocamloptの違い*1が分からなかったので最初戸惑いました。これで合ってるのかも微妙ですが。下記でocalmoptにした場合ではうまくコンパイルできませんでした。

$ ./http_get
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
...
  </body>
</html>

*1:前者がバイトコンパイル、後者がバイナリ