Adding ocamldoc generation.
[oota-llvm.git] / bindings / ocaml / Makefile.ocaml
index 3141cbd068ec7b849cb1baf14c074ad50c8e16b3..55a052af7e0123313b647646e8b3843abdd7d863 100644 (file)
@@ -2,32 +2,31 @@
 # 
 #                     The LLVM Compiler Infrastructure
 #
-# This file was developed by Gordon Henriksen and is distributed under the
-# University of Illinois Open Source License. See LICENSE.TXT for details.
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
 # 
 ##===----------------------------------------------------------------------===##
 # 
 # An ocaml library is a unique project type in the context of LLVM, so rules are
 # here rather than in Makefile.rules.
 # 
+# Reference materials on installing ocaml libraries:
+# 
+#   https://fedoraproject.org/wiki/Packaging/OCaml
+#   http://pkg-ocaml-maint.alioth.debian.org/ocaml_packaging_policy.txt
+# 
 ##===----------------------------------------------------------------------===##
 
 include $(LEVEL)/Makefile.config
 
-# Find the ocaml stdlib root. /usr/local/lib/ocaml is the default when built
-# from source; distros use something like /usr/lib/ocaml/3.10.0.
-ifndef OCAML_LIBDIR
-OCAML_LIBDIR := $(shell $(OCAMLC) -where)
-endif
-
-# CFLAGS needs to be set before Makefile.rules is included. Yes, ocaml puts its
-# includes under its libdir.
-CFLAGS += -I$(OCAML_LIBDIR)
+# CFLAGS needs to be set before Makefile.rules is included.
+CXX.Flags += -I"$(shell $(OCAMLC) -where)"
+C.Flags += -I"$(shell $(OCAMLC) -where)"
 
 include $(LEVEL)/Makefile.common
 
 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
-# user can override this with OCAML_LIBDIR.
+# user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
 PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
 OcamlDir := $(LibDir)/ocaml
 
@@ -38,44 +37,72 @@ UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
 endif
 
 # Tools
-OCAMLCFLAGS += -I $(OcamlDir)
+OCAMLCFLAGS += -I $(OcamlDir) -I $(ObjDir)
 OCAMLAFLAGS += $(patsubst %,-cclib %, \
-                 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
-                                $(UsedLibs) -l$(LIBRARYNAME))
-
-Compile.CMI  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
-Compile.CMO  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
-Archive.CMA  := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) -o)
-
-Compile.CMX  := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) -o)
-Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) -o)
+                 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
+                                          $(shell $(LLVM_CONFIG) --ldflags)) \
+                                          $(UsedLibs))
+# -g was introduced in 3.10.0.
+#ifneq ($(ENABLE_OPTIMIZED),1)
+#  OCAMLDEBUGFLAG := -g
+#endif
+
+Compile.CMI  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
+Compile.CMO  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
+Archive.CMA  := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
+                                  -o)
+
+Compile.CMX  := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
+Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
 
 # Source files
-OcamlSources := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
-OcamlHeaders := $(OcamlSources:.ml=.mli)
-
-# Output and intermediate files
-#   The .cmo files are the only intermediates; all others get installed.
-BareLibraryA := lib$(LIBRARYNAME).a
-LibraryA     := $(OcamlDir)/$(BareLibraryA)
-LibraryCMA   := $(OcamlDir)/$(LIBRARYNAME).cma
-LibraryCMXA  := $(OcamlDir)/$(LIBRARYNAME).cmxa
-ObjectsCMI   := $(OcamlSources:$(PROJ_SRC_DIR)/%.ml=$(OcamlDir)/%.cmi)
-ObjectsCMO   := $(OcamlSources:$(PROJ_SRC_DIR)/%.ml=$(ObjDir)/%.cmo)
-ObjectsCMX   := $(OcamlSources:$(PROJ_SRC_DIR)/%.ml=$(OcamlDir)/%.cmx)
-
-# Dependencies
-#   Punting on ocamldep, since its output is only suitable for builds where
-#   objects are placed directly adjacent to sources, which is not us.
-#   Unfortunately, this is subtly incorrect and leads to occasional problems.
-#   ocamlc/ocamlopt really need an option akin to gcc -M or gcc -MD.
-$(ObjectsCMO): $(ObjectsCMI) $(UsedOcamLibs:%=$(OcamlDir)/%.cmi)
-$(ObjectsCMX): $(ObjectsCMI) $(UsedOcamLibs:%=$(OcamlDir)/%.cmi)
+OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
+OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
+
+OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
+OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
+
+# Intermediate files
+LibraryCMA   := $(ObjDir)/$(LIBRARYNAME).cma
+LibraryCMXA  := $(ObjDir)/$(LIBRARYNAME).cmxa
+ObjectsCMI   := $(OcamlSources:%.ml=%.cmi)
+ObjectsCMO   := $(OcamlSources:%.ml=%.cmo)
+ObjectsCMX   := $(OcamlSources:%.ml=%.cmx)
+
+# Output files
+#   The .cmo files are the only intermediates; all others are to be installed.
+LibraryA   := $(OcamlDir)/lib$(LIBRARYNAME).a
+OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
+OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
+OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
+OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
+OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
 
 # Installation targets
 DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
 DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
+DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
+
+
+##===- Dependencies -------------------------------------------------------===##
+# Copy the sources into the intermediate directory because older ocamlc doesn't
+# support -o except when linking (outputs are placed next to inputs).
+
+$(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
+       $(Verb) $(CP) -f $< $@
+
+$(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
+       $(Verb) $(CP) -f $< $@
+
+$(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
+                                   $(OcamlDir)/.dir $(ObjDir)/.dir
+       $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
+
+$(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
+
+-include $(ObjDir)/$(LIBRARYNAME).ocamldep
 
 
 ##===- Build static library from C sources --------------------------------===##
@@ -105,6 +132,33 @@ uninstall-a::
        -$(Verb) $(RM) -f $(DestA)
 
 
+##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
+
+all-local:: build-deplibs
+clean-local:: clean-deplibs
+install-local:: install-deplibs
+uninstall-local:: uninstall-deplibs
+
+build-deplibs: $(OutputLibs)
+
+$(OcamlDir)/%.a: $(LibDir)/%.a
+       $(Verb) ln -sf $< $@
+
+$(OcamlDir)/%.o: $(LibDir)/%.o
+       $(Verb) ln -sf $< $@
+
+clean-deplibs:
+       $(Verb) rm -f $(OutputLibs)
+
+install-deplibs:
+       $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
+         ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
+       done
+
+uninstall-deplibs:
+       $(Verb) rm -f $(DestLibs)
+
+
 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
 
 all-local:: build-cmis
@@ -112,33 +166,36 @@ clean-local:: clean-cmis
 install-local:: install-cmis
 uninstall-local:: uninstall-cmis
 
-build-cmis: $(ObjectsCMI)
+build-cmis: $(OutputsCMI)
 
-$(OcamlDir)/%.cmi: $(PROJ_SRC_DIR)/%.mli $(OcamlDir)/.dir
+$(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
+       $(Verb) $(CP) -f $< $@
+
+$(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
        $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
        $(Verb) $(Compile.CMI) $@ $<
 
 clean-cmis::
-       -$(Verb) $(RM) -f $(ObjectsCMI)
+       -$(Verb) $(RM) -f $(OutputsCMI)
 
 # Also install the .mli's (headers) as documentation.
-install-cmis: $(ObjectsCMI)
+install-cmis: $(OutputsCMI) $(OcamlHeaders)
        $(Verb) $(MKDIR) $(PROJ_libocamldir)
-       $(Verb) for i in $(patsubst $(OcamlDir)/%,%,$(ObjectsCMI)); do \
+       $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
          $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
          $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
        done
-       $(Verb) for i in $(patsubst $(PROJ_SRC_DIR)/%,%,$(OcamlHeaders)); do \
+       $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
          $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
-         $(DataInstall) $(PROJ_SRC_DIR)/$$i "$(PROJ_libocamldir)/$$i"; \
+         $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
        done
 
 uninstall-cmis::
-       $(Verb) for i in $(patsubst $(OcamlDir)/%,%,$(ObjectsCMI)); do \
+       $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
          $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
          $(RM) -f "$(PROJ_libocamldir)/$$i"; \
        done
-       $(Verb) for i in $(patsubst $(PROJ_SRC_DIR)/%,%,$(OcamlHeaders)); do \
+       $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
          $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
          $(RM) -f "$(PROJ_libocamldir)/$$i"; \
        done
@@ -146,41 +203,33 @@ uninstall-cmis::
 
 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
 
-all-local:: $(LibraryCMA)
+all-local:: $(OutputCMA)
 clean-local:: clean-cma
 install-local:: install-cma
 uninstall-local:: uninstall-cma
 
+$(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
+       $(Verb) $(CP) -f $< $@
+
 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
        $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
        $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
-       $(Verb) for i in $(UsedLibNames); do \
-         ln -sf "$(LibDir)/$$i" "$(OcamlDir)/$$i"; \
-       done
 
-$(ObjDir)/%.cmo: $(PROJ_SRC_DIR)/%.ml $(OcamlDir)/.dir
+$(ObjDir)/%.cmo: $(ObjDir)/%.ml
        $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
        $(Verb) $(Compile.CMO) $@ $<
 
 clean-cma::
-       $(Verb) $(RM) -f $(LibraryCMA)
+       $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
 
-install-cma:: $(LibraryCMA)
+install-cma:: $(OutputCMA)
        $(Echo) "Installing $(BuildMode) $(DestCMA)"
        $(Verb) $(MKDIR) $(PROJ_libocamldir)
-       $(Verb) $(DataInstall) $(LibraryCMA) "$(DestCMA)"
-       $(Verb) for i in $(UsedLibNames); do \
-         $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
-         ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
-       done
+       $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
 
 uninstall-cma::
        $(Echo) "Uninstalling $(DestCMA)"
        -$(Verb) $(RM) -f $(DestCMA)
-       $(Verb) for i in $(UsedLibNames); do \
-         $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
-         $(RM) -f "$(PROJ_libocamldir)/$$i"; \
-       done
 
 
 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
@@ -189,47 +238,60 @@ uninstall-cma::
 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
 ifdef OCAMLOPT
 
-all-local:: $(LibraryCMXA)
+all-local:: $(OutputCMXA) $(OutputsCMX)
 clean-local:: clean-cmxa
 install-local:: install-cmxa
 uninstall-local:: uninstall-cmxa
 
+$(OutputCMXA): $(LibraryCMXA)
+       $(Verb) $(CP) -f $< $@
+       $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
+
+$(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
+       $(Verb) $(CP) -f $< $@
+
 $(LibraryCMXA): $(ObjectsCMX)
        $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
        $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
        $(Verb) $(RM) -f $(@:.cmxa=.o)
 
-$(OcamlDir)/%.cmx: $(PROJ_SRC_DIR)/%.ml
+$(ObjDir)/%.cmx: $(ObjDir)/%.ml
        $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
        $(Verb) $(Compile.CMX) $@ $<
 
 clean-cmxa::
-       $(Verb) $(RM) -f $(LibraryCMXA) $(LibraryCMXA:.cmxa=.o) \
-         $(LibraryCMXA:.cmxa=.a) $(ObjectsCMX)
+       $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
 
-install-cmxa:: $(LibraryCMXA)
+install-cmxa:: $(OutputCMXA) $(OutputsCMX)
        $(Verb) $(MKDIR) $(PROJ_libocamldir)
        $(Echo) "Installing $(BuildMode) $(DestCMXA)"
-       $(Verb) $(DataInstall) $(LibraryCMXA) $(DestCMXA)
+       $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
        $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
-       $(Verb) $(DataInstall) $(LibraryCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
-       $(Verb) for i in $(ObjectsCMX:$(OcamlDir)/%=%); do \
+       $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
+       $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
          $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
          $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
        done
 
-uninstall-cmxa:: $(LibraryCMXA)
+uninstall-cmxa::
        $(Echo) "Uninstalling $(DestCMXA)"
        $(Verb) $(RM) -f $(DestCMXA)
        $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
        $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
-       $(Verb) for i in $(ObjectsCMX:$(OcamlDir)/%=%); do \
+       $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
          $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
          $(RM) -f $(PROJ_libocamldir)/$$i; \
        done
 
 endif
 
+##===- Generate documentation ---------------------------------------------===##
+
+$(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
+       $(Echo) "Documenting $(notdir $@)"
+       $(Verb) $(OCAMLDOC) -I $(OcamlDir) -I $(ObjDir) -dump $@ $(OcamlHeaders)
+
+ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
 
 ##===- Debugging gunk -----------------------------------------------------===##
 printvars:: printcamlvars
@@ -240,6 +302,7 @@ printcamlvars::
        $(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
        $(Echo) "OCAMLC       : " '$(OCAMLC)'
        $(Echo) "OCAMLOPT     : " '$(OCAMLOPT)'
+       $(Echo) "OCAMLDEP     : " '$(OCAMLDEP)'
        $(Echo) "Compile.CMI  : " '$(Compile.CMI)'
        $(Echo) "Compile.CMO  : " '$(Compile.CMO)'
        $(Echo) "Archive.CMA  : " '$(Archive.CMA)'
@@ -248,7 +311,9 @@ printcamlvars::
        $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
        $(Echo) "LibraryCMA   : " '$(LibraryCMA)'
        $(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
+       $(Echo) "OcamlSources1: " '$(OcamlSources1)'
        $(Echo) "OcamlSources : " '$(OcamlSources)'
+       $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
        $(Echo) "ObjectsCMI   : " '$(ObjectsCMI)'
        $(Echo) "ObjectsCMO   : " '$(ObjectsCMO)'
        $(Echo) "ObjectsCMX   : " '$(ObjectsCMX)'