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