Fix srcdir <> objdir builds with ocaml 2.10. Downrev versions don't care whether
[oota-llvm.git] / bindings / ocaml / Makefile.ocaml
1 ##===- tools/ml/Makefile -----------------------------------*- Makefile -*-===##
2
3 #                     The LLVM Compiler Infrastructure
4 #
5 # This file was developed by Gordon Henriksen and is distributed under the
6 # University of Illinois Open Source License. See LICENSE.TXT for details.
7
8 ##===----------------------------------------------------------------------===##
9
10 # An ocaml library is a unique project type in the context of LLVM, so rules are
11 # here rather than in Makefile.rules.
12
13 ##===----------------------------------------------------------------------===##
14
15 include $(LEVEL)/Makefile.config
16
17 # Find the ocaml stdlib root. /usr/local/lib/ocaml is the default when built
18 # from source; distros use something like /usr/lib/ocaml/3.10.0.
19 ifndef OCAML_LIBDIR
20 OCAML_LIBDIR := $(shell $(OCAMLC) -where)
21 endif
22
23 # CFLAGS needs to be set before Makefile.rules is included. Yes, ocaml puts its
24 # includes under its libdir.
25 CFLAGS += -I$(OCAML_LIBDIR)
26
27 include $(LEVEL)/Makefile.common
28
29 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
30 # user can override this with OCAML_LIBDIR.
31 PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
32 OcamlDir := $(LibDir)/ocaml
33
34 # Info from llvm-config and similar
35 ifdef UsedComponents
36 UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
37 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
38 endif
39
40 # Tools
41 OCAMLCFLAGS += -I $(OcamlDir) -I $(ObjDir)
42 OCAMLAFLAGS += $(patsubst %,-cclib %, \
43                  $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
44                                  $(UsedLibs) -l$(LIBRARYNAME))
45
46 Compile.CMI  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
47 Compile.CMO  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
48 Archive.CMA  := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) -o)
49
50 Compile.CMX  := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) -o)
51 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) -o)
52
53 # Source files
54 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
55 OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
56
57 OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
58 OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
59
60 # Intermediate files
61 LibraryCMA   := $(ObjDir)/$(LIBRARYNAME).cma
62 LibraryCMXA  := $(ObjDir)/$(LIBRARYNAME).cmxa
63 ObjectsCMI   := $(OcamlSources:%.ml=%.cmi)
64 ObjectsCMO   := $(OcamlSources:%.ml=%.cmo)
65 ObjectsCMX   := $(OcamlSources:%.ml=%.cmx)
66
67 # Output files
68 #   The .cmo files are the only intermediates; all others are to be installed.
69 LibraryA   := $(OcamlDir)/lib$(LIBRARYNAME).a
70 OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
71 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
72 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
73 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
74
75 # Installation targets
76 DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
77 DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
78 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
79
80
81 ##===- Dependencies -------------------------------------------------------===##
82 # Copy the sources into the intermediate directory because older ocamlc doesn't
83 # support -o except when linking (outputs are placed next to inputs).
84
85 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
86         $(Verb) $(CP) -f $< $@
87
88 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
89         $(Verb) $(CP) -f $< $@
90
91 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
92                                    $(OcamlDir)/.dir $(ObjDir)/.dir
93         $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
94
95 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
96
97
98 ##===- Build static library from C sources --------------------------------===##
99
100 all-local:: $(LibraryA)
101 clean-local:: clean-a
102 install-local:: install-a
103 uninstall-local:: uninstall-a
104
105 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
106         $(Echo) "Building $(BuildMode) $(notdir $@)"
107         -$(Verb) $(RM) -f $@
108         $(Verb) $(Archive) $@ $(ObjectsO)
109         $(Verb) $(Ranlib) $@
110
111 clean-a::
112         -$(Verb) $(RM) -f $(LibraryA)
113
114 install-a:: $(LibraryA)
115         $(Echo) "Installing $(BuildMode) $(DestA)"
116         $(Verb) $(MKDIR) $(PROJ_libocamldir)
117         $(Verb) $(LTInstall) $(LibraryA) $(DestA)
118         $(Verb) 
119
120 uninstall-a::
121         $(Echo) "Uninstalling $(DestA)"
122         -$(Verb) $(RM) -f $(DestA)
123
124
125 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
126
127 all-local:: build-cmis
128 clean-local:: clean-cmis
129 install-local:: install-cmis
130 uninstall-local:: uninstall-cmis
131
132 build-cmis: $(OutputsCMI)
133
134 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
135         $(Verb) $(CP) -f $< $@
136
137 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
138         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
139         $(Verb) $(Compile.CMI) $@ $<
140
141 clean-cmis::
142         -$(Verb) $(RM) -f $(OutputsCMI)
143
144 # Also install the .mli's (headers) as documentation.
145 install-cmis: $(OutputsCMI) $(OcamlHeaders)
146         $(Verb) $(MKDIR) $(PROJ_libocamldir)
147         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
148           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
149           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
150         done
151         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
152           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
153           $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
154         done
155
156 uninstall-cmis::
157         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
158           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
159           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
160         done
161         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
162           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
163           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
164         done
165
166
167 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
168
169 all-local:: $(OutputCMA)
170 clean-local:: clean-cma
171 install-local:: install-cma
172 uninstall-local:: uninstall-cma
173
174 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
175         $(Verb) $(CP) -f $< $@
176
177 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
178         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
179         $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
180         $(Verb) for i in $(UsedLibNames); do \
181           ln -sf "$(LibDir)/$$i" "$(OcamlDir)/$$i"; \
182         done
183
184 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
185         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
186         $(Verb) $(Compile.CMO) $@ $<
187
188 clean-cma::
189         $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
190
191 install-cma:: $(OutputCMA)
192         $(Echo) "Installing $(BuildMode) $(DestCMA)"
193         $(Verb) $(MKDIR) $(PROJ_libocamldir)
194         $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
195         $(Verb) for i in $(UsedLibNames); do \
196           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
197           ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
198         done
199
200 uninstall-cma::
201         $(Echo) "Uninstalling $(DestCMA)"
202         -$(Verb) $(RM) -f $(DestCMA)
203         $(Verb) for i in $(UsedLibNames); do \
204           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
205           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
206         done
207
208
209 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
210
211 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
212 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
213 ifdef OCAMLOPT
214
215 all-local:: $(OutputCMXA) $(OutputsCMX)
216 clean-local:: clean-cmxa
217 install-local:: install-cmxa
218 uninstall-local:: uninstall-cmxa
219
220 $(OutputCMXA): $(LibraryCMXA)
221         $(Verb) $(CP) -f $< $@
222         $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
223
224 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
225         $(Verb) $(CP) -f $< $@
226
227 $(LibraryCMXA): $(ObjectsCMX)
228         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
229         $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
230         $(Verb) $(RM) -f $(@:.cmxa=.o)
231
232 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
233         $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
234         $(Verb) $(Compile.CMX) $@ $<
235
236 clean-cmxa::
237         $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
238
239 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
240         $(Verb) $(MKDIR) $(PROJ_libocamldir)
241         $(Echo) "Installing $(BuildMode) $(DestCMXA)"
242         $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
243         $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
244         $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
245         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
246           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
247           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
248         done
249
250 uninstall-cmxa::
251         $(Echo) "Uninstalling $(DestCMXA)"
252         $(Verb) $(RM) -f $(DestCMXA)
253         $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
254         $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
255         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
256           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
257           $(RM) -f $(PROJ_libocamldir)/$$i; \
258         done
259
260 endif
261
262
263 ##===- Debugging gunk -----------------------------------------------------===##
264 printvars:: printcamlvars
265
266 printcamlvars::
267         $(Echo) "LLVM_CONFIG  : " '$(LLVM_CONFIG)'
268         $(Echo) "OCAMLCFLAGS  : " '$(OCAMLCFLAGS)'
269         $(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
270         $(Echo) "OCAMLC       : " '$(OCAMLC)'
271         $(Echo) "OCAMLOPT     : " '$(OCAMLOPT)'
272         $(Echo) "OCAMLDEP     : " '$(OCAMLDEP)'
273         $(Echo) "Compile.CMI  : " '$(Compile.CMI)'
274         $(Echo) "Compile.CMO  : " '$(Compile.CMO)'
275         $(Echo) "Archive.CMA  : " '$(Archive.CMA)'
276         $(Echo) "Compile.CMX  : " '$(Compile.CMX)'
277         $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
278         $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
279         $(Echo) "LibraryCMA   : " '$(LibraryCMA)'
280         $(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
281         $(Echo) "OcamlSources1: " '$(OcamlSources1)'
282         $(Echo) "OcamlSources : " '$(OcamlSources)'
283         $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
284         $(Echo) "ObjectsCMI   : " '$(ObjectsCMI)'
285         $(Echo) "ObjectsCMO   : " '$(ObjectsCMO)'
286         $(Echo) "ObjectsCMX   : " '$(ObjectsCMX)'
287         $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
288         $(Echo) "DestA        : " '$(DestA)'
289         $(Echo) "DestCMA      : " '$(DestCMA)'
290         $(Echo) "DestCMXA     : " '$(DestCMXA)'
291         $(Echo) "UsedLibs     : " '$(UsedLibs)'
292         $(Echo) "UsedLibNames : " '$(UsedLibNames)'
293
294 .PHONY: printcamlvars   build-cmis \
295             clean-a     clean-cmis     clean-cma     clean-cmxa \
296           install-a   install-cmis   install-cma   install-cmxa \
297                 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa