GC poses hazards to the inliner. Consider:
[oota-llvm.git] / bindings / ocaml / Makefile.ocaml
1 ##===- tools/ml/Makefile -----------------------------------*- Makefile -*-===##
2
3 #                     The LLVM Compiler Infrastructure
4 #
5 # This file was developed by Gordon Henriksen and is distributed under the
6 # University of Illinois Open Source 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 CFLAGS += -I$(shell $(OCAMLC) -where)
24
25 include $(LEVEL)/Makefile.common
26
27 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
28 # user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
29 PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
30 OcamlDir := $(LibDir)/ocaml
31
32 # Info from llvm-config and similar
33 ifdef UsedComponents
34 UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
35 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
36 endif
37
38 # Tools
39 OCAMLCFLAGS += -I $(OcamlDir) -I $(ObjDir)
40 OCAMLAFLAGS += $(patsubst %,-cclib %, \
41                  $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
42                                           $(shell $(LLVM_CONFIG) --ldflags)) \
43                                           $(UsedLibs))
44  
45 ifneq ($(ENABLE_OPTIMIZED),1)
46   OCAMLDEBUGFLAG := -g
47 endif
48
49 Compile.CMI  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
50 Compile.CMO  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
51 Archive.CMA  := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
52                                   -o)
53
54 Compile.CMX  := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
55 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
56
57 # Source files
58 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
59 OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
60
61 OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
62 OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
63
64 # Intermediate files
65 LibraryCMA   := $(ObjDir)/$(LIBRARYNAME).cma
66 LibraryCMXA  := $(ObjDir)/$(LIBRARYNAME).cmxa
67 ObjectsCMI   := $(OcamlSources:%.ml=%.cmi)
68 ObjectsCMO   := $(OcamlSources:%.ml=%.cmo)
69 ObjectsCMX   := $(OcamlSources:%.ml=%.cmx)
70
71 # Output files
72 #   The .cmo files are the only intermediates; all others are to be installed.
73 LibraryA   := $(OcamlDir)/lib$(LIBRARYNAME).a
74 OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
75 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
76 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
77 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
78 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
79
80 # Installation targets
81 DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
82 DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
83 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
84 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
85
86
87 ##===- Dependencies -------------------------------------------------------===##
88 # Copy the sources into the intermediate directory because older ocamlc doesn't
89 # support -o except when linking (outputs are placed next to inputs).
90
91 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
92         $(Verb) $(CP) -f $< $@
93
94 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
95         $(Verb) $(CP) -f $< $@
96
97 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
98                                    $(OcamlDir)/.dir $(ObjDir)/.dir
99         $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
100
101 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
102
103 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
104
105
106 ##===- Build static library from C sources --------------------------------===##
107
108 all-local:: $(LibraryA)
109 clean-local:: clean-a
110 install-local:: install-a
111 uninstall-local:: uninstall-a
112
113 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
114         $(Echo) "Building $(BuildMode) $(notdir $@)"
115         -$(Verb) $(RM) -f $@
116         $(Verb) $(Archive) $@ $(ObjectsO)
117         $(Verb) $(Ranlib) $@
118
119 clean-a::
120         -$(Verb) $(RM) -f $(LibraryA)
121
122 install-a:: $(LibraryA)
123         $(Echo) "Installing $(BuildMode) $(DestA)"
124         $(Verb) $(MKDIR) $(PROJ_libocamldir)
125         $(Verb) $(LTInstall) $(LibraryA) $(DestA)
126         $(Verb) 
127
128 uninstall-a::
129         $(Echo) "Uninstalling $(DestA)"
130         -$(Verb) $(RM) -f $(DestA)
131
132
133 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
134
135 all-local:: build-deplibs
136 clean-local:: clean-deplibs
137 install-local:: install-deplibs
138 uninstall-local:: uninstall-deplibs
139
140 build-deplibs: $(OutputLibs)
141
142 $(OcamlDir)/%.a: $(LibDir)/%.a
143         $(Verb) ln -sf $< $@
144
145 $(OcamlDir)/%.o: $(LibDir)/%.o
146         $(Verb) ln -sf $< $@
147
148 clean-deplibs:
149         $(Verb) rm -f $(OutputLibs)
150
151 install-deplibs:
152         $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
153           ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
154         done
155
156 uninstall-deplibs:
157         $(Verb) rm -f $(DestLibs)
158
159
160 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
161
162 all-local:: build-cmis
163 clean-local:: clean-cmis
164 install-local:: install-cmis
165 uninstall-local:: uninstall-cmis
166
167 build-cmis: $(OutputsCMI)
168
169 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
170         $(Verb) $(CP) -f $< $@
171
172 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
173         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
174         $(Verb) $(Compile.CMI) $@ $<
175
176 clean-cmis::
177         -$(Verb) $(RM) -f $(OutputsCMI)
178
179 # Also install the .mli's (headers) as documentation.
180 install-cmis: $(OutputsCMI) $(OcamlHeaders)
181         $(Verb) $(MKDIR) $(PROJ_libocamldir)
182         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
183           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
184           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
185         done
186         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
187           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
188           $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
189         done
190
191 uninstall-cmis::
192         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
193           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
194           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
195         done
196         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
197           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
198           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
199         done
200
201
202 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
203
204 all-local:: $(OutputCMA)
205 clean-local:: clean-cma
206 install-local:: install-cma
207 uninstall-local:: uninstall-cma
208
209 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
210         $(Verb) $(CP) -f $< $@
211
212 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
213         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
214         $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
215
216 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
217         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
218         $(Verb) $(Compile.CMO) $@ $<
219
220 clean-cma::
221         $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
222
223 install-cma:: $(OutputCMA)
224         $(Echo) "Installing $(BuildMode) $(DestCMA)"
225         $(Verb) $(MKDIR) $(PROJ_libocamldir)
226         $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
227
228 uninstall-cma::
229         $(Echo) "Uninstalling $(DestCMA)"
230         -$(Verb) $(RM) -f $(DestCMA)
231
232
233 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
234
235 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
236 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
237 ifdef OCAMLOPT
238
239 all-local:: $(OutputCMXA) $(OutputsCMX)
240 clean-local:: clean-cmxa
241 install-local:: install-cmxa
242 uninstall-local:: uninstall-cmxa
243
244 $(OutputCMXA): $(LibraryCMXA)
245         $(Verb) $(CP) -f $< $@
246         $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
247
248 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
249         $(Verb) $(CP) -f $< $@
250
251 $(LibraryCMXA): $(ObjectsCMX)
252         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
253         $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
254         $(Verb) $(RM) -f $(@:.cmxa=.o)
255
256 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
257         $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
258         $(Verb) $(Compile.CMX) $@ $<
259
260 clean-cmxa::
261         $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
262
263 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
264         $(Verb) $(MKDIR) $(PROJ_libocamldir)
265         $(Echo) "Installing $(BuildMode) $(DestCMXA)"
266         $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
267         $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
268         $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
269         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
270           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
271           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
272         done
273
274 uninstall-cmxa::
275         $(Echo) "Uninstalling $(DestCMXA)"
276         $(Verb) $(RM) -f $(DestCMXA)
277         $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
278         $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
279         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
280           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
281           $(RM) -f $(PROJ_libocamldir)/$$i; \
282         done
283
284 endif
285
286
287 ##===- Debugging gunk -----------------------------------------------------===##
288 printvars:: printcamlvars
289
290 printcamlvars::
291         $(Echo) "LLVM_CONFIG  : " '$(LLVM_CONFIG)'
292         $(Echo) "OCAMLCFLAGS  : " '$(OCAMLCFLAGS)'
293         $(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
294         $(Echo) "OCAMLC       : " '$(OCAMLC)'
295         $(Echo) "OCAMLOPT     : " '$(OCAMLOPT)'
296         $(Echo) "OCAMLDEP     : " '$(OCAMLDEP)'
297         $(Echo) "Compile.CMI  : " '$(Compile.CMI)'
298         $(Echo) "Compile.CMO  : " '$(Compile.CMO)'
299         $(Echo) "Archive.CMA  : " '$(Archive.CMA)'
300         $(Echo) "Compile.CMX  : " '$(Compile.CMX)'
301         $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
302         $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
303         $(Echo) "LibraryCMA   : " '$(LibraryCMA)'
304         $(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
305         $(Echo) "OcamlSources1: " '$(OcamlSources1)'
306         $(Echo) "OcamlSources : " '$(OcamlSources)'
307         $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
308         $(Echo) "ObjectsCMI   : " '$(ObjectsCMI)'
309         $(Echo) "ObjectsCMO   : " '$(ObjectsCMO)'
310         $(Echo) "ObjectsCMX   : " '$(ObjectsCMX)'
311         $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
312         $(Echo) "DestA        : " '$(DestA)'
313         $(Echo) "DestCMA      : " '$(DestCMA)'
314         $(Echo) "DestCMXA     : " '$(DestCMXA)'
315         $(Echo) "UsedLibs     : " '$(UsedLibs)'
316         $(Echo) "UsedLibNames : " '$(UsedLibNames)'
317
318 .PHONY: printcamlvars   build-cmis \
319             clean-a     clean-cmis     clean-cma     clean-cmxa \
320           install-a   install-cmis   install-cma   install-cmxa \
321                 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa