Ticket #588: ocaml.diff

File ocaml.diff, 8.4 KB (added by Dan, 13 years ago)

add ocaml support

  • xapian-bindings/configure.ac

    diff --git a/xapian-bindings/configure.ac b/xapian-bindings/configure.ac
    index 6153b7b..a6d3d9c 100644
    a b AC_ARG_WITH(perl,  
    239239  [],
    240240  [with_perl=])
    241241
     242AC_ARG_WITH(ocaml,
     243  AS_HELP_STRING([--with-ocaml], [enable Ocaml bindings]),
     244  [],
     245  [with_ocaml=])
     246
    242247AC_ARG_WITH(lua,
    243248  AC_HELP_STRING([--with-lua], [enable Lua bindings]),
    244249  [],
    245250  [with_lua=])
    246251
    247 case $with_python$with_php$with_ruby$with_tcl$with_csharp$with_java$with_perl$with_lua in
     252case $with_python$with_php$with_ruby$with_tcl$with_csharp$with_java$with_perl$with_ocaml$with_lua in
    248253*yes*)
    249254  dnl Default unspecified values to no.
    250255  test -z "$with_python" && with_python=no
    case $with_python$with_php$with_ruby$with_tcl$with_csharp$with_java$with_perl$wi  
    254259  test -z "$with_java" && with_java=no
    255260  test -z "$with_ruby" && with_ruby=no
    256261  test -z "$with_perl" && with_perl=no
     262  test -z "with_ocaml" && with_ocaml=no
    257263  test -z "$with_lua" && with_lua=no
    258264  ;;
    259265esac
    if test no != "$with_perl" ; then  
    940946  fi
    941947fi
    942948
     949dnl -----------------------------------
     950dnl INCLUDES and LIBS for OCAML
     951
     952if test no != "$with_ocaml"; then
     953      AC_CHECK_PROG(OCAML,ocaml,ocaml)
     954      if test -n "$OCAML"; then
     955        with_ocaml="No (ocaml not available)"
     956      else
     957        AC_CHECK_PROG(OCAML_OPT,ocamlopt,ocamlopt)
     958        OCAML_INCLUDES=-I/usr/lib$LIBPOSTFIX/ocaml
     959        OCAML_LIBS=-L/usr/lib$LIBPOSTFIX/ocaml
     960        save_CPPFLAGS=$CPPFLAGS
     961        CPPFLAGS="$CPPFLAGS $OCAML_INCLUDES"
     962        AC_CHECK_HEADER(caml/mlvalues.h,[
     963          with_ocaml="Yes"
     964          AC_SUBST(OCAML_INCLUDES)
     965          AC_SUBST(OCAML_LIBS)
     966        ],[
     967          with_ocaml="No (missing header)"
     968          AC_MSG_WARN([Unable to find header caml/mlvalues.h. The OCAML packages will not be built])
     969        ])
     970        CPPFLAGS=$save_CPPFLAGS
     971         BINDINGS="$BINDINGS ocaml"
     972      fi
     973else
     974  if test yes = "$with_ocaml" ; then
     975    AC_MSG_ERROR([ocaml not found])
     976  fi
     977fi
     978AM_CONDITIONAL(WITH_OCAML, [test yes = "$with_ocaml"])
     979AM_CONDITIONAL(WITH_OCAMLOPT, [test "x$OCAML_OPT" != "x"])
     980
    943981if test no != "$with_lua" ; then
    944982  AC_PATH_PROGS(LUA, ["${LUA-lua}"], [])
    945983  AC_ARG_VAR(LUA, [lua interpreter])
  • new file xapian-bindings/ocaml/Makefile

    diff --git a/xapian-bindings/ocaml/Makefile b/xapian-bindings/ocaml/Makefile
    new file mode 100644
    index 0000000..bbb41de
    - +  
     1SWIG=../../swig/preinst-swig
     2INCLUDE=-I. -I../generic -I../ $(shell xapian-config-1.3 --swigflags --cxxflags)
     3LINK=$(shell xapian-config-1.3 --libs)
     4
     5all: static
     6
     7static: libs
     8        ocamlc -pp "camlp4o ./swigp4.cmo" -c smoketest.ml       
     9        ocamlfind ocamlc -g -ccopt -g -cclib                    \
     10                -g -custom  -o smoketest                        \
     11                -package unix  -linkpkg                         \
     12                swig.cmo  xapian.cmo  smoketest.cmo             \
     13                xapian_wrap.o                                   \
     14                -cc 'g++ -Wno-write-strings $(INCLUDE) $(LINK)'
     15
     16
     17swig:
     18        $(SWIG) -ocaml -co swigp4.ml
     19        $(SWIG) -ocaml -co swig.mli
     20        $(SWIG) -ocaml -co swig.ml
     21        ocamlfind ocamlc -package camlp4                        \
     22                -pp "camlp4o pa_extend.cmo q_MLast.cmo"         \
     23                -c swigp4.ml                                   
     24        ocamlc -c swig.mli swig.ml
     25
     26
     27libs: swig
     28        $(SWIG) -Wall $(INCLUDE) -c++ -ocaml -v \
     29                -outdir . -o xapian_wrap.c xapian-ocaml.i               
     30        ocamlc -cc 'g++ -Wno-write-strings' -g -c -ccopt        \
     31                -g -ccopt '-xc++' -ccopt '$(INCLUDE)'           \
     32                xapian_wrap.c                                   
     33        ocamlc -c xapian.mli xapian.ml
     34
     35
     36toplevel: libs
     37        ocamlfind ocamlmktop -custom swig.cmo                   \
     38                -package dynlink -package camlp4                \
     39                dynlink.cma camlp4o.cma swigp4.cmo              \
     40                xapian_wrap.o xapian.cmo -o xapian_top          \
     41                -cc 'g++ -Wno-write-strings $(INCLUDE) $(LINK)'
     42
     43
     44clean:
     45        rm -rf *.dSYM smoketest
     46        rm -f swigp4.ml swig.mli swig.ml
     47        rm -f *.cmo *.cmi
     48        rm -f xapian_wrap.*
     49        rm -f xapian.*
     50        rm -f xapian_top
  • new file xapian-bindings/ocaml/smoketest.ml

    diff --git a/xapian-bindings/ocaml/smoketest.ml b/xapian-bindings/ocaml/smoketest.ml
    new file mode 100644
    index 0000000..9e1a0ee
    - +  
     1open Swig
     2open Xapian
     3open Unix
     4
     5(* Quick test to ensure the module is loaded *)
     6let () =
     7  assert (Xapian.module_name =  "xapian");
     8  assert ((Xapian.version_string '() as string) = "1.3.0")
     9;;
     10
     11(* Basis document test *)
     12
     13let () =
     14  let doc = new_Document '() in
     15    ignore (add_terms doc ["hello"]);
     16    assert ((doc -> termlist_count() as int) = 1);
     17    ignore (doc -> set_data ("something"));
     18    assert ((doc -> get_data() as string) = "something");
     19    let tb = doc -> termlist_begin() in
     20    let te = doc -> termlist_end() in
     21      assert (tb -> equals(te) as bool == false);
     22      assert ((tb -> get_term() as string) = "hello")
     23;;
     24
     25(* simple database test *)
     26let () =
     27  let db = writable_db "test.db" 1 in
     28  let doc = new_Document '() in
     29    ignore (add_terms doc ["hello"]);
     30    ignore (doc -> set_data("goodbye"));
     31    ignore (db -> add_document(doc));
     32    ignore (db -> flush());
     33    let enq = new_Enquire '(db) in
     34    let qp = new_QueryParser '() in
     35      ignore (qp -> set_database(db));
     36      let pq = build_parsed_query qp "hello" in
     37        enq -> set_query(pq);
     38        let matches = enq -> get_mset(0, 10) in
     39          Printf.printf "Found %d\n" (matches -> size() as int);
     40          assert ((matches -> size() as int) = 1);
     41          let tb = pq -> get_terms_begin() in
     42            assert (pq -> empty() as bool = false);
     43            assert ((tb -> get_term() as string) = "hello")
     44;;
     45
     46
     47(* add a number of documents to a db then pull put one into an Rset and use
     48get_eset *)
     49
     50let _ =
     51  ignore (
     52    Array.map (fun el ->
     53                 ignore (Sys.remove (Filename.concat "test.db" el)))
     54      (Sys.readdir "test.db"));
     55  Unix.rmdir "test.db"
     56;;
     57
     58print_endline "All tests passed."
  • new file xapian-bindings/ocaml/util.i

    diff --git a/xapian-bindings/ocaml/util.i b/xapian-bindings/ocaml/util.i
    new file mode 100644
    index 0000000..c96dece
    - +  
     1%apply int { Xapian::doccount };
     2%apply int { Xapian::termcount };
     3
     4%insert (mlitail) %{
     5
     6val version_string : c_obj -> c_obj
     7val major_version : c_obj -> c_obj
     8val minor_version : c_obj -> c_obj
     9val revision : c_obj -> c_obj
     10
     11val closure_from_lst : c_obj -> c_obj
     12val build_parsed_query : c_obj -> string -> c_obj
     13val writable_db : string -> int -> c_obj
     14val add_terms : c_obj -> string list -> c_obj list
     15
     16%}
     17
     18%insert (mltail) %{
     19
     20let version_string = _version_string
     21let major_version = _major_version
     22let minor_version = _minor_version
     23let revision = _revision
     24
     25(* Helpers *)
     26
     27let closure_from_lst lst =
     28    match lst with
     29        Swig.C_list [Swig.C_string a; Swig.C_obj f] -> Swig.C_obj f
     30      | _ -> raise (Failure "ZOMG")
     31
     32
     33let build_parsed_query qp query =
     34  let c_query = Swig.C_string query in
     35    closure_from_lst ((invoke qp) "parse_query" (c_query))
     36
     37
     38let writable_db path flags =
     39  let c_path = Swig.C_string path in
     40    let c_flags = Swig.C_int flags in
     41    closure_from_lst (new_WritableDatabase (Swig.C_list[c_path; c_flags]))
     42
     43
     44let add_terms doc terms =
     45  List.map (fun el ->
     46              let c_el = Swig.C_string el in
     47                (invoke doc) "add_term" (c_el)) terms
     48
     49%}
  • new file xapian-bindings/ocaml/xapian-ocaml.i

    diff --git a/xapian-bindings/ocaml/xapian-ocaml.i b/xapian-bindings/ocaml/xapian-ocaml.i
    new file mode 100644
    index 0000000..3ddfbde
    - +  
     1%module xapian
     2%{
     3/* ocaml.i: SWIG interface file for the Ocaml bindings
     4 *
     5 * Copyright (C) 2011 Olly Betts
     6 *
     7 * This program is free software; you can redistribute it and/or
     8 * modify it under the terms of the GNU General Public License as
     9 * published by the Free Software Foundation; either version 2 of the
     10 * License, or (at your option) any later version.
     11 *
     12 * This program is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * You should have received a copy of the GNU General Public License
     18 * along with this program; if not, write to the Free Software
     19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
     20 * USA
     21 */
     22%}
     23
     24%include ../xapian-head.i
     25
     26%include util.i
     27%include ../xapian.i
  • new file xapian-bindings/ocaml/xapianinit

    diff --git a/xapian-bindings/ocaml/xapianinit b/xapian-bindings/ocaml/xapianinit
    new file mode 100644
    index 0000000..de2bc87
    - +  
     1open Swig
     2open Xapian
     3;;