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