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