1cff422c28d400138e52255eb91873dca1e6fb06
[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 ifdef OCAMLOPT
70 Archive.EXE := $(strip $(OCAMLOPT) -cc $(CXX) $(OCAMLCFLAGS) $(UsedOcamLibs:%=%.cmxa) $(OCAMLDEBUGFLAG) -o)
71 else
72 Archive.EXE := $(strip $(OCAMLC) -cc $(CXX) $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG:%=%.cma) -o)
73 endif
74
75 # Source files
76 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
77 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
78
79 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
80 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
81
82 OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
83 OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
84
85 # Intermediate files
86 ObjectsCMI   := $(OcamlSources:%.ml=%.cmi)
87 ObjectsCMO   := $(OcamlSources:%.ml=%.cmo)
88 ObjectsCMX   := $(OcamlSources:%.ml=%.cmx)
89
90 ifdef LIBRARYNAME
91 LibraryCMA   := $(ObjDir)/$(LIBRARYNAME).cma
92 LibraryCMXA  := $(ObjDir)/$(LIBRARYNAME).cmxa
93 endif
94
95 ifdef TOOLNAME
96 ToolEXE      := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
97 endif
98
99 # Output files
100 #   The .cmo files are the only intermediates; all others are to be installed.
101 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
102 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
103 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
104
105 ifdef LIBRARYNAME
106 LibraryA   := $(OcamlDir)/lib$(LIBRARYNAME).a
107 OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
108 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
109 endif
110
111 ifdef TOOLNAME
112 ifdef EXAMPLE_TOOL
113 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
114 else
115 OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
116 endif
117 endif
118
119 # Installation targets
120 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
121
122 ifdef LIBRARYNAME
123 DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
124 DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
125 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
126 endif
127
128 ##===- Dependencies -------------------------------------------------------===##
129 # Copy the sources into the intermediate directory because older ocamlc doesn't
130 # support -o except when linking (outputs are placed next to inputs).
131
132 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
133         $(Verb) $(CP) -f $< $@
134
135 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
136         $(Verb) $(CP) -f $< $@
137
138 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
139
140 ifdef LIBRARYNAME
141 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
142                                    $(OcamlDir)/.dir $(ObjDir)/.dir
143         $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
144
145 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
146 endif
147
148 ifdef TOOLNAME
149 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
150                                 $(OcamlDir)/.dir $(ObjDir)/.dir
151         $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
152
153 -include $(ObjDir)/$(TOOLNAME).ocamldep
154 endif
155
156 ##===- Build static library from C sources --------------------------------===##
157
158 ifdef LibraryA
159 all-local:: $(LibraryA)
160 clean-local:: clean-a
161 install-local:: install-a
162 uninstall-local:: uninstall-a
163
164 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
165         $(Echo) "Building $(BuildMode) $(notdir $@)"
166         -$(Verb) $(RM) -f $@
167         $(Verb) $(Archive) $@ $(ObjectsO)
168         $(Verb) $(Ranlib) $@
169
170 clean-a::
171         -$(Verb) $(RM) -f $(LibraryA)
172
173 install-a:: $(LibraryA)
174         $(Echo) "Installing $(BuildMode) $(DestA)"
175         $(Verb) $(MKDIR) $(PROJ_libocamldir)
176         $(Verb) $(INSTALL) $(LibraryA) $(DestA)
177         $(Verb) 
178
179 uninstall-a::
180         $(Echo) "Uninstalling $(DestA)"
181         -$(Verb) $(RM) -f $(DestA)
182 endif
183
184
185 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
186
187 all-local:: build-deplibs
188 clean-local:: clean-deplibs
189 install-local:: install-deplibs
190 uninstall-local:: uninstall-deplibs
191
192 build-deplibs: $(OutputLibs)
193
194 $(OcamlDir)/%.a: $(LibDir)/%.a
195         $(Verb) ln -sf $< $@
196
197 $(OcamlDir)/%.o: $(LibDir)/%.o
198         $(Verb) ln -sf $< $@
199
200 clean-deplibs:
201         $(Verb) $(RM) -f $(OutputLibs)
202
203 install-deplibs:
204         $(Verb) $(MKDIR) $(PROJ_libocamldir)
205         $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
206           ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
207         done
208
209 uninstall-deplibs:
210         $(Verb) $(RM) -f $(DestLibs)
211
212
213 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
214
215 ifneq ($(OcamlHeaders),)
216 all-local:: build-cmis
217 clean-local:: clean-cmis
218 install-local:: install-cmis
219 uninstall-local:: uninstall-cmis
220
221 build-cmis: $(OutputsCMI)
222
223 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
224         $(Verb) $(CP) -f $< $@
225
226 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
227         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
228         $(Verb) $(Compile.CMI) $@ $<
229
230 clean-cmis::
231         -$(Verb) $(RM) -f $(OutputsCMI)
232
233 # Also install the .mli's (headers) as documentation.
234 install-cmis: $(OutputsCMI) $(OcamlHeaders)
235         $(Verb) $(MKDIR) $(PROJ_libocamldir)
236         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
237           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
238           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
239         done
240         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
241           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
242           $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
243         done
244
245 uninstall-cmis::
246         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
247           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
248           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
249         done
250         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
251           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
252           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
253         done
254 endif
255
256
257 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
258
259 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
260         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
261         $(Verb) $(Compile.CMO) $@ $<
262
263 ifdef LIBRARYNAME
264 all-local:: $(OutputCMA)
265 clean-local:: clean-cma
266 install-local:: install-cma
267 uninstall-local:: uninstall-cma
268
269 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
270         $(Verb) $(CP) -f $< $@
271
272 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
273         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
274         $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
275
276 clean-cma::
277         $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
278
279 install-cma:: $(OutputCMA)
280         $(Echo) "Installing $(BuildMode) $(DestCMA)"
281         $(Verb) $(MKDIR) $(PROJ_libocamldir)
282         $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
283
284 uninstall-cma::
285         $(Echo) "Uninstalling $(DestCMA)"
286         -$(Verb) $(RM) -f $(DestCMA)
287 endif
288
289 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
290
291 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
292 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
293 ifdef OCAMLOPT
294
295 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
296         $(Verb) $(CP) -f $< $@
297
298 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
299         $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
300         $(Verb) $(Compile.CMX) $@ $<
301
302 ifdef LIBRARYNAME
303 all-local:: $(OutputCMXA) $(OutputsCMX)
304 clean-local:: clean-cmxa
305 install-local:: install-cmxa
306 uninstall-local:: uninstall-cmxa
307
308 $(OutputCMXA): $(LibraryCMXA)
309         $(Verb) $(CP) -f $< $@
310         $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
311
312 $(LibraryCMXA): $(ObjectsCMX)
313         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
314         $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
315         $(Verb) $(RM) -f $(@:.cmxa=.o)
316
317 clean-cmxa::
318         $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
319
320 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
321         $(Verb) $(MKDIR) $(PROJ_libocamldir)
322         $(Echo) "Installing $(BuildMode) $(DestCMXA)"
323         $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
324         $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
325         $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
326         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
327           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
328           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
329         done
330
331 uninstall-cmxa::
332         $(Echo) "Uninstalling $(DestCMXA)"
333         $(Verb) $(RM) -f $(DestCMXA)
334         $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
335         $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
336         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
337           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
338           $(RM) -f $(PROJ_libocamldir)/$$i; \
339         done
340 endif
341 endif
342
343 ##===- Build executables --------------------------------------------------===##
344
345 ifdef TOOLNAME
346 all-local:: $(OutputEXE)
347 clean-local:: clean-exe
348
349 $(OutputEXE): $(ToolEXE) $(OcamlDir)/.dir
350         $(Verb) $(CP) -f $< $@
351
352 ifndef OCAMLOPT
353 $(ToolEXE): $(ObjectsCMO) $(OcamlDir)/.dir
354         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
355         $(Verb) $(Archive.EXE) $@ $<
356 else
357 $(ToolEXE): $(ObjectsCMX) $(OcamlDir)/.dir
358         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
359         $(Verb) $(Archive.EXE) $@ $<
360 endif
361 endif
362
363 ##===- Generate documentation ---------------------------------------------===##
364
365 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
366         $(Echo) "Documenting $(notdir $@)"
367         $(Verb) $(OCAMLDOC) -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
368
369 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
370
371 ##===- Debugging gunk -----------------------------------------------------===##
372 printvars:: printcamlvars
373
374 printcamlvars::
375         $(Echo) "LLVM_CONFIG  : " '$(LLVM_CONFIG)'
376         $(Echo) "OCAMLCFLAGS  : " '$(OCAMLCFLAGS)'
377         $(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
378         $(Echo) "OCAMLC       : " '$(OCAMLC)'
379         $(Echo) "OCAMLOPT     : " '$(OCAMLOPT)'
380         $(Echo) "OCAMLDEP     : " '$(OCAMLDEP)'
381         $(Echo) "Compile.CMI  : " '$(Compile.CMI)'
382         $(Echo) "Compile.CMO  : " '$(Compile.CMO)'
383         $(Echo) "Archive.CMA  : " '$(Archive.CMA)'
384         $(Echo) "Compile.CMX  : " '$(Compile.CMX)'
385         $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
386         $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
387         $(Echo) "LibraryCMA   : " '$(LibraryCMA)'
388         $(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
389         $(Echo) "OcamlSources1: " '$(OcamlSources1)'
390         $(Echo) "OcamlSources2: " '$(OcamlSources2)'
391         $(Echo) "OcamlSources : " '$(OcamlSources)'
392         $(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
393         $(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
394         $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
395         $(Echo) "ObjectsCMI   : " '$(ObjectsCMI)'
396         $(Echo) "ObjectsCMO   : " '$(ObjectsCMO)'
397         $(Echo) "ObjectsCMX   : " '$(ObjectsCMX)'
398         $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
399         $(Echo) "DestA        : " '$(DestA)'
400         $(Echo) "DestCMA      : " '$(DestCMA)'
401         $(Echo) "DestCMXA     : " '$(DestCMXA)'
402         $(Echo) "UsedLibs     : " '$(UsedLibs)'
403         $(Echo) "UsedLibNames : " '$(UsedLibNames)'
404
405 .PHONY: printcamlvars   build-cmis \
406             clean-a     clean-cmis     clean-cma     clean-cmxa \
407           install-a   install-cmis   install-cma   install-cmxa \
408           install-exe \
409                 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \
410                 uninstall-exe