Ticket #588: ocaml.diff
File ocaml.diff, 8.4 KB (added by , 13 years ago) |
---|
-
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, 239 239 [], 240 240 [with_perl=]) 241 241 242 AC_ARG_WITH(ocaml, 243 AS_HELP_STRING([--with-ocaml], [enable Ocaml bindings]), 244 [], 245 [with_ocaml=]) 246 242 247 AC_ARG_WITH(lua, 243 248 AC_HELP_STRING([--with-lua], [enable Lua bindings]), 244 249 [], 245 250 [with_lua=]) 246 251 247 case $with_python$with_php$with_ruby$with_tcl$with_csharp$with_java$with_perl$with_ lua in252 case $with_python$with_php$with_ruby$with_tcl$with_csharp$with_java$with_perl$with_ocaml$with_lua in 248 253 *yes*) 249 254 dnl Default unspecified values to no. 250 255 test -z "$with_python" && with_python=no … … case $with_python$with_php$with_ruby$with_tcl$with_csharp$with_java$with_perl$wi 254 259 test -z "$with_java" && with_java=no 255 260 test -z "$with_ruby" && with_ruby=no 256 261 test -z "$with_perl" && with_perl=no 262 test -z "with_ocaml" && with_ocaml=no 257 263 test -z "$with_lua" && with_lua=no 258 264 ;; 259 265 esac … … if test no != "$with_perl" ; then 940 946 fi 941 947 fi 942 948 949 dnl ----------------------------------- 950 dnl INCLUDES and LIBS for OCAML 951 952 if 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 973 else 974 if test yes = "$with_ocaml" ; then 975 AC_MSG_ERROR([ocaml not found]) 976 fi 977 fi 978 AM_CONDITIONAL(WITH_OCAML, [test yes = "$with_ocaml"]) 979 AM_CONDITIONAL(WITH_OCAMLOPT, [test "x$OCAML_OPT" != "x"]) 980 943 981 if test no != "$with_lua" ; then 944 982 AC_PATH_PROGS(LUA, ["${LUA-lua}"], []) 945 983 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
- + 1 SWIG=../../swig/preinst-swig 2 INCLUDE=-I. -I../generic -I../ $(shell xapian-config-1.3 --swigflags --cxxflags) 3 LINK=$(shell xapian-config-1.3 --libs) 4 5 all: static 6 7 static: 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 17 swig: 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 27 libs: 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 36 toplevel: 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 44 clean: 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
- + 1 open Swig 2 open Xapian 3 open Unix 4 5 (* Quick test to ensure the module is loaded *) 6 let () = 7 assert (Xapian.module_name = "xapian"); 8 assert ((Xapian.version_string '() as string) = "1.3.0") 9 ;; 10 11 (* Basis document test *) 12 13 let () = 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 *) 26 let () = 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 48 get_eset *) 49 50 let _ = 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 58 print_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 6 val version_string : c_obj -> c_obj 7 val major_version : c_obj -> c_obj 8 val minor_version : c_obj -> c_obj 9 val revision : c_obj -> c_obj 10 11 val closure_from_lst : c_obj -> c_obj 12 val build_parsed_query : c_obj -> string -> c_obj 13 val writable_db : string -> int -> c_obj 14 val add_terms : c_obj -> string list -> c_obj list 15 16 %} 17 18 %insert (mltail) %{ 19 20 let version_string = _version_string 21 let major_version = _major_version 22 let minor_version = _minor_version 23 let revision = _revision 24 25 (* Helpers *) 26 27 let 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 33 let 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 38 let 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 44 let 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
- + 1 open Swig 2 open Xapian 3 ;;