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