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