+COBJS := libvirt.cmo libvirt_version.cmo
+OPTOBJS := libvirt.cmx libvirt_version.cmx
+
+ifneq ($(OCAMLMKLIB),)
+# Good, we can just use ocamlmklib
+mllibvirt.cma: libvirt_c.o $(COBJS)
+ $(OCAMLMKLIB) -o mllibvirt $^ $(LDFLAGS) -lvirt
+
+mllibvirt.cmxa: libvirt_c.o $(OPTOBJS)
+ $(OCAMLMKLIB) -o mllibvirt $^ $(LDFLAGS) -lvirt
+
+else
+ifeq ($(WIN32),yes)
+# Ugh, MinGW doesn't have ocamlmklib. This technique is copied from the
+# example in OCaml distribution, otherlibs/win32unix/Makefile.nt
+
+mllibvirt.cma: dllmllibvirt.dll libmllibvirt.a $(COBJS)
+ $(OCAMLC) -a -linkall -o $@ $(COBJS) \
+ -dllib -lmllibvirt -cclib -lmllibvirt -cclib "$(LDFLAGS) -lvirt"
+
+mllibvirt.cmxa: libmllibvirt.a $(OPTOBJS)
+ $(OCAMLOPT) -a -linkall -o $@ $(OPTOBJS) \
+ -cclib -lmllibvirt -cclib "$(LDFLAGS) -lvirt"
+
+dllmllibvirt.dll: libvirt_c.o
+ $(CC) -shared -o $@ $^ \
+ $(LDFLAGS) "$(shell ocamlc -where)"/ocamlrun.a -lvirt
+
+libmllibvirt.a: libvirt_c.o
+ ar rc $@ $^
+ ranlib $@
+
+else
+# Don't know how to build a library on this platform.
+$(BYTE_TARGETS) $(OPT_TARGETS):
+ echo "Error: ocamlmklib missing, and no known way to build libraries on this platform"
+ exit 1
+endif
+endif
+
+# Automatically generate the C code from a Perl script 'generator.pl'.
+libvirt_c.c: generator.pl
+ perl -w $<