[OCaml] Rework Llvm_executionengine using ctypes.
[oota-llvm.git] / bindings / ocaml / Makefile.ocaml
1 ##===- bindings/ocaml/Makefile.ocaml -----------------------*- 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 $(OCAMLFIND) c -where)"
24 C.Flags += -I"$(shell $(OCAMLFIND) c -where)"
25
26 ifeq ($(ENABLE_SHARED),1)
27 LINK_COMPONENTS := all
28 endif
29
30 include $(LEVEL)/Makefile.common
31
32 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
33 # user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
34 PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
35 OcamlDir := $(LibDir)/ocaml
36
37 # Info from llvm-config and similar
38 ifndef IS_CLEANING_TARGET
39 ifdef UsedComponents
40 UsedLibs = $(shell $(LLVM_CONFIG) --libs --system-libs $(UsedComponents))
41 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
42 endif
43 endif
44
45 # How do we link OCaml executables with LLVM?
46 # 1) If this is a --enable-shared build, build stub libraries. This also allows
47 #    to use LLVM from toplevels.
48 # 2) If this is a --disable-shared build, embed ocamlc options for building
49 #    a custom runtime and a static executable. It is not possible to use LLVM
50 #    from toplevels.
51 ifneq ($(ObjectsO),)
52 ifeq ($(ENABLE_SHARED),1)
53 OCAMLSTUBS := 1
54 endif
55 endif
56
57 # Tools
58 OCAMLCFLAGS += -I $(OcamlDir) $(addprefix -package ,$(FindlibPackages))
59
60 ifndef IS_CLEANING_TARGET
61 ifneq ($(ObjectsO),)
62 OCAMLAFLAGS += $(patsubst %,-cclib %, \
63                  $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
64                                           $(shell $(LLVM_CONFIG) --ldflags)) \
65                                           $(UsedLibs) $(ExtraLibs))
66 else
67 OCAMLAFLAGS += $(patsubst %,-cclib %, \
68                  $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
69                                           $(UsedLibs) $(ExtraLibs))
70 endif
71 endif
72
73 ifneq ($(ENABLE_OPTIMIZED),1)
74   OCAMLDEBUGFLAG := -g
75 endif
76
77 Compile.CMI  := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
78 Compile.CMO  := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
79 Compile.CMX  := $(strip $(OCAMLFIND) opt -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
80
81 ifdef OCAMLSTUBS
82 # Avoid the need for LD_LIBRARY_PATH
83 ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
84 ifneq ($(HOST_OS),Darwin)
85 OCAMLRPATH   := $(RPATH) -Wl,'$(SharedLibDir)'
86 endif
87 endif
88 endif
89
90 ifdef OCAMLSTUBS
91 Archive.CMA  := $(strip $(OCAMLFIND) c -a -dllib -l$(LIBRARYNAME) $(OCAMLDEBUGFLAG) \
92                                        -o)
93 else
94 Archive.CMA  := $(strip $(OCAMLFIND) c -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
95                                        -o)
96 endif
97
98 ifdef OCAMLSTUBS
99 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(patsubst %,-cclib %, \
100                                     $(LLVMLibsOptions) -l$(LIBRARYNAME) \
101                                     -L$(SharedLibDir) $(OCAMLRPATH)) \
102                                     $(OCAMLDEBUGFLAG) -o)
103 else
104 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
105 endif
106
107 # Source files
108 ifndef OcamlSources1
109 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
110 endif
111
112 ifndef OcamlHeaders1
113 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
114 endif
115
116 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
117 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
118
119 OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
120 OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
121
122 # Intermediate files
123 ObjectsCMI   := $(OcamlSources:%.ml=%.cmi)
124 ObjectsCMO   := $(OcamlSources:%.ml=%.cmo)
125 ObjectsCMX   := $(OcamlSources:%.ml=%.cmx)
126
127 ifdef LIBRARYNAME
128 LibraryCMA   := $(ObjDir)/$(LIBRARYNAME).cma
129 LibraryCMXA  := $(ObjDir)/$(LIBRARYNAME).cmxa
130 endif
131
132 ifdef TOOLNAME
133 ToolEXE      := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
134 endif
135
136 # Output files
137 #   The .cmo files are the only intermediates; all others are to be installed.
138 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
139 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
140 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
141
142 ifdef LIBRARYNAME
143 LibraryA   := $(OcamlDir)/lib$(LIBRARYNAME).a
144 OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
145 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
146 endif
147
148 ifdef OCAMLSTUBS
149 SharedLib := $(OcamlDir)/dll$(LIBRARYNAME)$(SHLIBEXT)
150 endif
151
152 ifdef TOOLNAME
153 ifdef EXAMPLE_TOOL
154 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
155 else
156 OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
157 endif
158 endif
159
160 # Installation targets
161 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
162
163 ifdef LIBRARYNAME
164 DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
165 DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
166 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
167 endif
168
169 ifdef OCAMLSTUBS
170 DestSharedLib := $(PROJ_libocamldir)/dll$(LIBRARYNAME)$(SHLIBEXT)
171 endif
172
173 ##===- Dependencies -------------------------------------------------------===##
174 # Copy the sources into the intermediate directory because older ocamlc doesn't
175 # support -o except when linking (outputs are placed next to inputs).
176
177 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
178         $(Verb) $(CP) -f $< $@
179
180 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
181         $(Verb) $(CP) -f $< $@
182
183 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
184
185 ifdef LIBRARYNAME
186 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
187                                    $(OcamlDir)/.dir $(ObjDir)/.dir
188         $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
189
190 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
191 endif
192
193 ifdef TOOLNAME
194 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
195                                 $(OcamlDir)/.dir $(ObjDir)/.dir
196         $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
197
198 -include $(ObjDir)/$(TOOLNAME).ocamldep
199 endif
200
201 ##===- Build static library from C sources --------------------------------===##
202
203 ifdef LibraryA
204 all-local:: $(LibraryA)
205 clean-local:: clean-a
206 install-local:: install-a
207 uninstall-local:: uninstall-a
208
209 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
210         $(Echo) "Building $(BuildMode) $(notdir $@)"
211         -$(Verb) $(RM) -f $@
212         $(Verb) $(Archive) $@ $(ObjectsO)
213         $(Verb) $(Ranlib) $@
214
215 clean-a::
216         -$(Verb) $(RM) -f $(LibraryA)
217
218 install-a:: $(LibraryA)
219         $(Echo) "Installing $(BuildMode) $(DestA)"
220         $(Verb) $(MKDIR) $(PROJ_libocamldir)
221         $(Verb) $(INSTALL) $(LibraryA) $(DestA)
222         $(Verb)
223
224 uninstall-a::
225         $(Echo) "Uninstalling $(DestA)"
226         -$(Verb) $(RM) -f $(DestA)
227 endif
228
229
230 ##===- Build stub library from C sources ----------------------------------===##
231
232 ifdef SharedLib
233 all-local:: $(SharedLib)
234 clean-local:: clean-shared
235 install-local:: install-shared
236 uninstall-local:: uninstall-shared
237
238 $(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
239         $(Echo) "Building $(BuildMode) $(notdir $@)"
240         $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) $(LLVMLibsOptions) \
241                         -o $@ $(ObjectsO)
242
243 clean-shared::
244         -$(Verb) $(RM) -f $(SharedLib)
245
246 install-shared:: $(SharedLib)
247         $(Echo) "Installing $(BuildMode) $(DestSharedLib)"
248         $(Verb) $(MKDIR) $(PROJ_libocamldir)
249         $(Verb) $(INSTALL) $(SharedLib) $(DestSharedLib)
250         $(Verb)
251
252 uninstall-shared::
253         $(Echo) "Uninstalling $(DestSharedLib)"
254         -$(Verb) $(RM) -f $(DestSharedLib)
255 endif
256
257
258 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
259
260 all-local:: build-deplibs
261 clean-local:: clean-deplibs
262 install-local:: install-deplibs
263 uninstall-local:: uninstall-deplibs
264
265 build-deplibs: $(OutputLibs)
266
267 $(OcamlDir)/%.a: $(LibDir)/%.a
268         $(Verb) ln -sf $< $@
269
270 $(OcamlDir)/%.o: $(LibDir)/%.o
271         $(Verb) ln -sf $< $@
272
273 clean-deplibs:
274         $(Verb) $(RM) -f $(OutputLibs)
275
276 install-deplibs:
277         $(Verb) $(MKDIR) $(PROJ_libocamldir)
278         $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
279           ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
280         done
281
282 uninstall-deplibs:
283         $(Verb) $(RM) -f $(DestLibs)
284
285
286 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
287
288 ifneq ($(OcamlHeaders),)
289 all-local:: build-cmis
290 clean-local:: clean-cmis
291 install-local:: install-cmis
292 uninstall-local:: uninstall-cmis
293
294 build-cmis: $(OutputsCMI)
295
296 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
297         $(Verb) $(CP) -f $< $@
298
299 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
300         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
301         $(Verb) $(Compile.CMI) $@ $<
302
303 clean-cmis::
304         -$(Verb) $(RM) -f $(OutputsCMI)
305
306 # Also install the .mli's (headers) as documentation.
307 install-cmis: $(OutputsCMI) $(OcamlHeaders)
308         $(Verb) $(MKDIR) $(PROJ_libocamldir)
309         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
310           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
311           $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
312         done
313         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
314           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
315           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
316         done
317
318 uninstall-cmis::
319         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
320           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
321           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
322         done
323         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
324           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
325           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
326         done
327 endif
328
329
330 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
331
332 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
333         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
334         $(Verb) $(Compile.CMO) $@ $<
335
336 ifdef LIBRARYNAME
337 all-local:: $(OutputCMA)
338 clean-local:: clean-cma
339 install-local:: install-cma
340 uninstall-local:: uninstall-cma
341
342 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
343         $(Verb) $(CP) -f $< $@
344
345 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
346         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
347         $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
348
349 clean-cma::
350         $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
351
352 install-cma:: $(OutputCMA)
353         $(Echo) "Installing $(BuildMode) $(DestCMA)"
354         $(Verb) $(MKDIR) $(PROJ_libocamldir)
355         $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
356
357 uninstall-cma::
358         $(Echo) "Uninstalling $(DestCMA)"
359         -$(Verb) $(RM) -f $(DestCMA)
360 endif
361
362 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
363
364 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
365 # If unavailable, 'configure' will set HAVE_OCAMLOPT to 0 in Makefile.config.
366 ifeq ($(HAVE_OCAMLOPT),1)
367
368 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
369         $(Verb) $(CP) -f $< $@
370
371 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
372         $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
373         $(Verb) $(Compile.CMX) $@ $<
374
375 ifdef LIBRARYNAME
376 all-local:: $(OutputCMXA) $(OutputsCMX)
377 clean-local:: clean-cmxa
378 install-local:: install-cmxa
379 uninstall-local:: uninstall-cmxa
380
381 $(OutputCMXA): $(LibraryCMXA)
382         $(Verb) $(CP) -f $< $@
383         $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
384
385 $(LibraryCMXA): $(ObjectsCMX)
386         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
387         $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
388         $(Verb) $(RM) -f $(@:.cmxa=.o)
389
390 clean-cmxa::
391         $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
392
393 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
394         $(Verb) $(MKDIR) $(PROJ_libocamldir)
395         $(Echo) "Installing $(BuildMode) $(DestCMXA)"
396         $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
397         $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
398         $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
399         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
400           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
401           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
402         done
403
404 uninstall-cmxa::
405         $(Echo) "Uninstalling $(DestCMXA)"
406         $(Verb) $(RM) -f $(DestCMXA)
407         $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
408         $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
409         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
410           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
411           $(RM) -f $(PROJ_libocamldir)/$$i; \
412         done
413 endif
414 endif
415
416 ##===- Generate documentation ---------------------------------------------===##
417
418 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
419         $(Echo) "Documenting $(notdir $@)"
420         $(Verb) $(OCAMLFIND) doc -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
421
422 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
423
424 ##===- Debugging gunk -----------------------------------------------------===##
425 printvars:: printcamlvars
426
427 printcamlvars::
428         $(Echo) "LLVM_CONFIG  : " '$(LLVM_CONFIG)'
429         $(Echo) "OCAMLCFLAGS  : " '$(OCAMLCFLAGS)'
430         $(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
431         $(Echo) "OCAMLFIND    : " '$(OCAMLFIND)'
432         $(Echo) "Compile.CMI  : " '$(Compile.CMI)'
433         $(Echo) "Compile.CMO  : " '$(Compile.CMO)'
434         $(Echo) "Archive.CMA  : " '$(Archive.CMA)'
435         $(Echo) "Compile.CMX  : " '$(Compile.CMX)'
436         $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
437         $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
438         $(Echo) "LibraryCMA   : " '$(LibraryCMA)'
439         $(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
440         $(Echo) "SharedLib    : " '$(SharedLib)'
441         $(Echo) "OcamlSources1: " '$(OcamlSources1)'
442         $(Echo) "OcamlSources2: " '$(OcamlSources2)'
443         $(Echo) "OcamlSources : " '$(OcamlSources)'
444         $(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
445         $(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
446         $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
447         $(Echo) "ObjectsCMI   : " '$(ObjectsCMI)'
448         $(Echo) "ObjectsCMO   : " '$(ObjectsCMO)'
449         $(Echo) "ObjectsCMX   : " '$(ObjectsCMX)'
450         $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
451         $(Echo) "DestA        : " '$(DestA)'
452         $(Echo) "DestCMA      : " '$(DestCMA)'
453         $(Echo) "DestCMXA     : " '$(DestCMXA)'
454         $(Echo) "DestSharedLib: " '$(DestSharedLib)'
455         $(Echo) "UsedLibs     : " '$(UsedLibs)'
456         $(Echo) "UsedLibNames : " '$(UsedLibNames)'
457         $(Echo) "ExtraLibs    : " '$(ExtraLibs)'
458
459 .PHONY: printcamlvars   build-cmis \
460             clean-a     clean-cmis     clean-cma     clean-cmxa \
461           install-a   install-cmis   install-cma   install-cmxa \
462           install-exe \
463                 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \
464                 uninstall-exe