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