[python bindings] Added code to get the length of a memory buffer. Tests are included.
[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 ifdef OCAMLOPT
70 Archive.EXE := $(strip $(OCAMLOPT) -cc $(CXX) $(OCAMLCFLAGS) $(UsedOcamLibs:%=%.cmxa) $(OCAMLDEBUGFLAG) -o)
71 else
72 Archive.EXE := $(strip $(OCAMLC) -cc $(CXX) $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG:%=%.cma) -o)
73 endif
74
75 # Source files
76 ifndef OcamlSources1
77 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
78 endif
79
80 ifndef OcamlHeaders1
81 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
82 endif
83
84 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
85 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
86
87 OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
88 OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
89
90 # Intermediate files
91 ObjectsCMI   := $(OcamlSources:%.ml=%.cmi)
92 ObjectsCMO   := $(OcamlSources:%.ml=%.cmo)
93 ObjectsCMX   := $(OcamlSources:%.ml=%.cmx)
94
95 ifdef LIBRARYNAME
96 LibraryCMA   := $(ObjDir)/$(LIBRARYNAME).cma
97 LibraryCMXA  := $(ObjDir)/$(LIBRARYNAME).cmxa
98 endif
99
100 ifdef TOOLNAME
101 ToolEXE      := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
102 endif
103
104 # Output files
105 #   The .cmo files are the only intermediates; all others are to be installed.
106 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
107 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
108 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
109
110 ifdef LIBRARYNAME
111 LibraryA   := $(OcamlDir)/lib$(LIBRARYNAME).a
112 OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
113 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
114 endif
115
116 ifdef TOOLNAME
117 ifdef EXAMPLE_TOOL
118 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
119 else
120 OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
121 endif
122 endif
123
124 # Installation targets
125 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
126
127 ifdef LIBRARYNAME
128 DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
129 DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
130 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
131 endif
132
133 ##===- Dependencies -------------------------------------------------------===##
134 # Copy the sources into the intermediate directory because older ocamlc doesn't
135 # support -o except when linking (outputs are placed next to inputs).
136
137 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
138         $(Verb) $(CP) -f $< $@
139
140 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
141         $(Verb) $(CP) -f $< $@
142
143 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
144
145 ifdef LIBRARYNAME
146 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
147                                    $(OcamlDir)/.dir $(ObjDir)/.dir
148         $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
149
150 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
151 endif
152
153 ifdef TOOLNAME
154 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
155                                 $(OcamlDir)/.dir $(ObjDir)/.dir
156         $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
157
158 -include $(ObjDir)/$(TOOLNAME).ocamldep
159 endif
160
161 ##===- Build static library from C sources --------------------------------===##
162
163 ifdef LibraryA
164 all-local:: $(LibraryA)
165 clean-local:: clean-a
166 install-local:: install-a
167 uninstall-local:: uninstall-a
168
169 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
170         $(Echo) "Building $(BuildMode) $(notdir $@)"
171         -$(Verb) $(RM) -f $@
172         $(Verb) $(Archive) $@ $(ObjectsO)
173         $(Verb) $(Ranlib) $@
174
175 clean-a::
176         -$(Verb) $(RM) -f $(LibraryA)
177
178 install-a:: $(LibraryA)
179         $(Echo) "Installing $(BuildMode) $(DestA)"
180         $(Verb) $(MKDIR) $(PROJ_libocamldir)
181         $(Verb) $(INSTALL) $(LibraryA) $(DestA)
182         $(Verb) 
183
184 uninstall-a::
185         $(Echo) "Uninstalling $(DestA)"
186         -$(Verb) $(RM) -f $(DestA)
187 endif
188
189
190 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
191
192 all-local:: build-deplibs
193 clean-local:: clean-deplibs
194 install-local:: install-deplibs
195 uninstall-local:: uninstall-deplibs
196
197 build-deplibs: $(OutputLibs)
198
199 $(OcamlDir)/%.a: $(LibDir)/%.a
200         $(Verb) ln -sf $< $@
201
202 $(OcamlDir)/%.o: $(LibDir)/%.o
203         $(Verb) ln -sf $< $@
204
205 clean-deplibs:
206         $(Verb) $(RM) -f $(OutputLibs)
207
208 install-deplibs:
209         $(Verb) $(MKDIR) $(PROJ_libocamldir)
210         $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
211           ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
212         done
213
214 uninstall-deplibs:
215         $(Verb) $(RM) -f $(DestLibs)
216
217
218 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
219
220 ifneq ($(OcamlHeaders),)
221 all-local:: build-cmis
222 clean-local:: clean-cmis
223 install-local:: install-cmis
224 uninstall-local:: uninstall-cmis
225
226 build-cmis: $(OutputsCMI)
227
228 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
229         $(Verb) $(CP) -f $< $@
230
231 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
232         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
233         $(Verb) $(Compile.CMI) $@ $<
234
235 clean-cmis::
236         -$(Verb) $(RM) -f $(OutputsCMI)
237
238 # Also install the .mli's (headers) as documentation.
239 install-cmis: $(OutputsCMI) $(OcamlHeaders)
240         $(Verb) $(MKDIR) $(PROJ_libocamldir)
241         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
242           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
243           $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
244         done
245         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
246           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
247           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
248         done
249
250 uninstall-cmis::
251         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
252           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
253           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
254         done
255         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
256           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
257           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
258         done
259 endif
260
261
262 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
263
264 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
265         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
266         $(Verb) $(Compile.CMO) $@ $<
267
268 ifdef LIBRARYNAME
269 all-local:: $(OutputCMA)
270 clean-local:: clean-cma
271 install-local:: install-cma
272 uninstall-local:: uninstall-cma
273
274 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
275         $(Verb) $(CP) -f $< $@
276
277 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
278         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
279         $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
280
281 clean-cma::
282         $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
283
284 install-cma:: $(OutputCMA)
285         $(Echo) "Installing $(BuildMode) $(DestCMA)"
286         $(Verb) $(MKDIR) $(PROJ_libocamldir)
287         $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
288
289 uninstall-cma::
290         $(Echo) "Uninstalling $(DestCMA)"
291         -$(Verb) $(RM) -f $(DestCMA)
292 endif
293
294 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
295
296 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
297 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
298 ifdef OCAMLOPT
299
300 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
301         $(Verb) $(CP) -f $< $@
302
303 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
304         $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
305         $(Verb) $(Compile.CMX) $@ $<
306
307 ifdef LIBRARYNAME
308 all-local:: $(OutputCMXA) $(OutputsCMX)
309 clean-local:: clean-cmxa
310 install-local:: install-cmxa
311 uninstall-local:: uninstall-cmxa
312
313 $(OutputCMXA): $(LibraryCMXA)
314         $(Verb) $(CP) -f $< $@
315         $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
316
317 $(LibraryCMXA): $(ObjectsCMX)
318         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
319         $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
320         $(Verb) $(RM) -f $(@:.cmxa=.o)
321
322 clean-cmxa::
323         $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
324
325 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
326         $(Verb) $(MKDIR) $(PROJ_libocamldir)
327         $(Echo) "Installing $(BuildMode) $(DestCMXA)"
328         $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
329         $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
330         $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
331         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
332           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
333           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
334         done
335
336 uninstall-cmxa::
337         $(Echo) "Uninstalling $(DestCMXA)"
338         $(Verb) $(RM) -f $(DestCMXA)
339         $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
340         $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
341         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
342           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
343           $(RM) -f $(PROJ_libocamldir)/$$i; \
344         done
345 endif
346 endif
347
348 ##===- Build executables --------------------------------------------------===##
349
350 ifdef TOOLNAME
351 all-local:: $(OutputEXE)
352 clean-local:: clean-exe
353
354 $(OutputEXE): $(ToolEXE) $(OcamlDir)/.dir
355         $(Verb) $(CP) -f $< $@
356
357 ifndef OCAMLOPT
358 $(ToolEXE): $(ObjectsCMO) $(OcamlDir)/.dir
359         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
360         $(Verb) $(Archive.EXE) $@ $(ObjectsCMO)
361 else
362 $(ToolEXE): $(ObjectsCMX) $(OcamlDir)/.dir
363         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
364         $(Verb) $(Archive.EXE) $@ $(ObjectsCMX)
365 endif
366 endif
367
368 ##===- Generate documentation ---------------------------------------------===##
369
370 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
371         $(Echo) "Documenting $(notdir $@)"
372         $(Verb) $(OCAMLDOC) -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
373
374 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
375
376 ##===- Debugging gunk -----------------------------------------------------===##
377 printvars:: printcamlvars
378
379 printcamlvars::
380         $(Echo) "LLVM_CONFIG  : " '$(LLVM_CONFIG)'
381         $(Echo) "OCAMLCFLAGS  : " '$(OCAMLCFLAGS)'
382         $(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
383         $(Echo) "OCAMLC       : " '$(OCAMLC)'
384         $(Echo) "OCAMLOPT     : " '$(OCAMLOPT)'
385         $(Echo) "OCAMLDEP     : " '$(OCAMLDEP)'
386         $(Echo) "Compile.CMI  : " '$(Compile.CMI)'
387         $(Echo) "Compile.CMO  : " '$(Compile.CMO)'
388         $(Echo) "Archive.CMA  : " '$(Archive.CMA)'
389         $(Echo) "Compile.CMX  : " '$(Compile.CMX)'
390         $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
391         $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
392         $(Echo) "LibraryCMA   : " '$(LibraryCMA)'
393         $(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
394         $(Echo) "OcamlSources1: " '$(OcamlSources1)'
395         $(Echo) "OcamlSources2: " '$(OcamlSources2)'
396         $(Echo) "OcamlSources : " '$(OcamlSources)'
397         $(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
398         $(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
399         $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
400         $(Echo) "ObjectsCMI   : " '$(ObjectsCMI)'
401         $(Echo) "ObjectsCMO   : " '$(ObjectsCMO)'
402         $(Echo) "ObjectsCMX   : " '$(ObjectsCMX)'
403         $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
404         $(Echo) "DestA        : " '$(DestA)'
405         $(Echo) "DestCMA      : " '$(DestCMA)'
406         $(Echo) "DestCMXA     : " '$(DestCMXA)'
407         $(Echo) "UsedLibs     : " '$(UsedLibs)'
408         $(Echo) "UsedLibNames : " '$(UsedLibNames)'
409
410 .PHONY: printcamlvars   build-cmis \
411             clean-a     clean-cmis     clean-cma     clean-cmxa \
412           install-a   install-cmis   install-cma   install-cmxa \
413           install-exe \
414                 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \
415                 uninstall-exe