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