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