Rename some ocaml functions.
[oota-llvm.git] / bindings / ocaml / Makefile.ocaml
1 ##===- tools/ml/Makefile -----------------------------------*- Makefile -*-===##
2
3 #                     The LLVM Compiler Infrastructure
4 #
5 # This file is distributed under the University of Illinois Open Source
6 # 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 # Reference materials on installing ocaml libraries:
14
15 #   https://fedoraproject.org/wiki/Packaging/OCaml
16 #   http://pkg-ocaml-maint.alioth.debian.org/ocaml_packaging_policy.txt
17
18 ##===----------------------------------------------------------------------===##
19
20 include $(LEVEL)/Makefile.config
21
22 # CFLAGS needs to be set before Makefile.rules is included.
23 CXX.Flags += -I"$(shell $(OCAMLC) -where)"
24 C.Flags += -I"$(shell $(OCAMLC) -where)"
25
26 include $(LEVEL)/Makefile.common
27
28 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
29 # user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
30 PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
31 OcamlDir := $(LibDir)/ocaml
32
33 # Info from llvm-config and similar
34 ifndef IS_CLEANING_TARGET
35 ifdef UsedComponents
36 UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
37 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
38 endif
39 endif
40
41 # Tools
42 OCAMLCFLAGS += -I $(ObjDir) -I $(OcamlDir)
43 ifndef IS_CLEANING_TARGET
44 ifneq ($(ObjectsO),)
45 OCAMLAFLAGS += $(patsubst %,-cclib %, \
46                  $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
47                                           $(shell $(LLVM_CONFIG) --ldflags)) \
48                                           $(UsedLibs))
49 else
50 OCAMLAFLAGS += $(patsubst %,-cclib %, \
51                  $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
52                                           $(UsedLibs))
53 endif
54 endif
55  
56 # -g was introduced in 3.10.0.
57 #ifneq ($(ENABLE_OPTIMIZED),1)
58 #  OCAMLDEBUGFLAG := -g
59 #endif
60
61 Compile.CMI  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
62 Compile.CMO  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
63 Archive.CMA  := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
64                                   -o)
65
66 Compile.CMX  := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
67 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
68
69 # Source files
70 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
71 OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
72
73 OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
74 OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
75
76 # Intermediate files
77 LibraryCMA   := $(ObjDir)/$(LIBRARYNAME).cma
78 LibraryCMXA  := $(ObjDir)/$(LIBRARYNAME).cmxa
79 ObjectsCMI   := $(OcamlSources:%.ml=%.cmi)
80 ObjectsCMO   := $(OcamlSources:%.ml=%.cmo)
81 ObjectsCMX   := $(OcamlSources:%.ml=%.cmx)
82
83 # Output files
84 #   The .cmo files are the only intermediates; all others are to be installed.
85 LibraryA   := $(OcamlDir)/lib$(LIBRARYNAME).a
86 OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
87 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
88 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
89 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
90 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
91
92 # Installation targets
93 DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
94 DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
95 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
96 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
97
98
99 ##===- Dependencies -------------------------------------------------------===##
100 # Copy the sources into the intermediate directory because older ocamlc doesn't
101 # support -o except when linking (outputs are placed next to inputs).
102
103 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
104         $(Verb) $(CP) -f $< $@
105
106 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
107         $(Verb) $(CP) -f $< $@
108
109 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
110                                    $(OcamlDir)/.dir $(ObjDir)/.dir
111         $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
112
113 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
114
115 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
116
117
118 ##===- Build static library from C sources --------------------------------===##
119
120 ifneq ($(ObjectsO),)
121 all-local:: $(LibraryA)
122 clean-local:: clean-a
123 install-local:: install-a
124 uninstall-local:: uninstall-a
125
126 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
127         $(Echo) "Building $(BuildMode) $(notdir $@)"
128         -$(Verb) $(RM) -f $@
129         $(Verb) $(Archive) $@ $(ObjectsO)
130         $(Verb) $(Ranlib) $@
131
132 clean-a::
133         -$(Verb) $(RM) -f $(LibraryA)
134
135 install-a:: $(LibraryA)
136         $(Echo) "Installing $(BuildMode) $(DestA)"
137         $(Verb) $(MKDIR) $(PROJ_libocamldir)
138         $(Verb) $(INSTALL) $(LibraryA) $(DestA)
139         $(Verb) 
140
141 uninstall-a::
142         $(Echo) "Uninstalling $(DestA)"
143         -$(Verb) $(RM) -f $(DestA)
144 endif
145
146
147 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
148
149 all-local:: build-deplibs
150 clean-local:: clean-deplibs
151 install-local:: install-deplibs
152 uninstall-local:: uninstall-deplibs
153
154 build-deplibs: $(OutputLibs)
155
156 $(OcamlDir)/%.a: $(LibDir)/%.a
157         $(Verb) ln -sf $< $@
158
159 $(OcamlDir)/%.o: $(LibDir)/%.o
160         $(Verb) ln -sf $< $@
161
162 clean-deplibs:
163         $(Verb) rm -f $(OutputLibs)
164
165 install-deplibs:
166         $(Verb) $(MKDIR) $(PROJ_libocamldir)
167         $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
168           ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
169         done
170
171 uninstall-deplibs:
172         $(Verb) rm -f $(DestLibs)
173
174
175 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
176
177 all-local:: build-cmis
178 clean-local:: clean-cmis
179 install-local:: install-cmis
180 uninstall-local:: uninstall-cmis
181
182 build-cmis: $(OutputsCMI)
183
184 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
185         $(Verb) $(CP) -f $< $@
186
187 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
188         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
189         $(Verb) $(Compile.CMI) $@ $<
190
191 clean-cmis::
192         -$(Verb) $(RM) -f $(OutputsCMI)
193
194 # Also install the .mli's (headers) as documentation.
195 install-cmis: $(OutputsCMI) $(OcamlHeaders)
196         $(Verb) $(MKDIR) $(PROJ_libocamldir)
197         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
198           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
199           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
200         done
201         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
202           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
203           $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
204         done
205
206 uninstall-cmis::
207         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
208           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
209           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
210         done
211         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
212           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
213           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
214         done
215
216
217 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
218
219 all-local:: $(OutputCMA)
220 clean-local:: clean-cma
221 install-local:: install-cma
222 uninstall-local:: uninstall-cma
223
224 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
225         $(Verb) $(CP) -f $< $@
226
227 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
228         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
229         $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
230
231 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
232         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
233         $(Verb) $(Compile.CMO) $@ $<
234
235 clean-cma::
236         $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
237
238 install-cma:: $(OutputCMA)
239         $(Echo) "Installing $(BuildMode) $(DestCMA)"
240         $(Verb) $(MKDIR) $(PROJ_libocamldir)
241         $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
242
243 uninstall-cma::
244         $(Echo) "Uninstalling $(DestCMA)"
245         -$(Verb) $(RM) -f $(DestCMA)
246
247
248 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
249
250 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
251 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
252 ifdef OCAMLOPT
253
254 all-local:: $(OutputCMXA) $(OutputsCMX)
255 clean-local:: clean-cmxa
256 install-local:: install-cmxa
257 uninstall-local:: uninstall-cmxa
258
259 $(OutputCMXA): $(LibraryCMXA)
260         $(Verb) $(CP) -f $< $@
261         $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
262
263 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
264         $(Verb) $(CP) -f $< $@
265
266 $(LibraryCMXA): $(ObjectsCMX)
267         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
268         $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
269         $(Verb) $(RM) -f $(@:.cmxa=.o)
270
271 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
272         $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
273         $(Verb) $(Compile.CMX) $@ $<
274
275 clean-cmxa::
276         $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
277
278 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
279         $(Verb) $(MKDIR) $(PROJ_libocamldir)
280         $(Echo) "Installing $(BuildMode) $(DestCMXA)"
281         $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
282         $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
283         $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
284         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
285           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
286           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
287         done
288
289 uninstall-cmxa::
290         $(Echo) "Uninstalling $(DestCMXA)"
291         $(Verb) $(RM) -f $(DestCMXA)
292         $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
293         $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
294         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
295           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
296           $(RM) -f $(PROJ_libocamldir)/$$i; \
297         done
298
299 endif
300
301 ##===- Generate documentation ---------------------------------------------===##
302
303 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
304         $(Echo) "Documenting $(notdir $@)"
305         $(Verb) $(OCAMLDOC) -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
306
307 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
308
309 ##===- Debugging gunk -----------------------------------------------------===##
310 printvars:: printcamlvars
311
312 printcamlvars::
313         $(Echo) "LLVM_CONFIG  : " '$(LLVM_CONFIG)'
314         $(Echo) "OCAMLCFLAGS  : " '$(OCAMLCFLAGS)'
315         $(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
316         $(Echo) "OCAMLC       : " '$(OCAMLC)'
317         $(Echo) "OCAMLOPT     : " '$(OCAMLOPT)'
318         $(Echo) "OCAMLDEP     : " '$(OCAMLDEP)'
319         $(Echo) "Compile.CMI  : " '$(Compile.CMI)'
320         $(Echo) "Compile.CMO  : " '$(Compile.CMO)'
321         $(Echo) "Archive.CMA  : " '$(Archive.CMA)'
322         $(Echo) "Compile.CMX  : " '$(Compile.CMX)'
323         $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
324         $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
325         $(Echo) "LibraryCMA   : " '$(LibraryCMA)'
326         $(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
327         $(Echo) "OcamlSources1: " '$(OcamlSources1)'
328         $(Echo) "OcamlSources : " '$(OcamlSources)'
329         $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
330         $(Echo) "ObjectsCMI   : " '$(ObjectsCMI)'
331         $(Echo) "ObjectsCMO   : " '$(ObjectsCMO)'
332         $(Echo) "ObjectsCMX   : " '$(ObjectsCMX)'
333         $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
334         $(Echo) "DestA        : " '$(DestA)'
335         $(Echo) "DestCMA      : " '$(DestCMA)'
336         $(Echo) "DestCMXA     : " '$(DestCMXA)'
337         $(Echo) "UsedLibs     : " '$(UsedLibs)'
338         $(Echo) "UsedLibNames : " '$(UsedLibNames)'
339
340 .PHONY: printcamlvars   build-cmis \
341             clean-a     clean-cmis     clean-cma     clean-cmxa \
342           install-a   install-cmis   install-cma   install-cmxa \
343                 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa