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