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