Makefile.ocaml revision ae77a201488fc439bfdfa6314b947a1f5457b701
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
69# Source files
70OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
71OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
72
73OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
74OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
75
76# Intermediate files
77LibraryCMA   := $(ObjDir)/$(LIBRARYNAME).cma
78LibraryCMXA  := $(ObjDir)/$(LIBRARYNAME).cmxa
79ObjectsCMI   := $(OcamlSources:%.ml=%.cmi)
80ObjectsCMO   := $(OcamlSources:%.ml=%.cmo)
81ObjectsCMX   := $(OcamlSources:%.ml=%.cmx)
82
83# Output files
84#   The .cmo files are the only intermediates; all others are to be installed.
85LibraryA   := $(OcamlDir)/lib$(LIBRARYNAME).a
86OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
87OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
88OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
89OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
90OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
91
92# Installation targets
93DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
94DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
95DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
96DestLibs := $(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
120ifneq ($(ObjectsO),)
121all-local:: $(LibraryA)
122clean-local:: clean-a
123install-local:: install-a
124uninstall-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
132clean-a::
133	-$(Verb) $(RM) -f $(LibraryA)
134
135install-a:: $(LibraryA)
136	$(Echo) "Installing $(BuildMode) $(DestA)"
137	$(Verb) $(MKDIR) $(PROJ_libocamldir)
138	$(Verb) $(INSTALL) $(LibraryA) $(DestA)
139	$(Verb) 
140
141uninstall-a::
142	$(Echo) "Uninstalling $(DestA)"
143	-$(Verb) $(RM) -f $(DestA)
144endif
145
146
147##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
148
149all-local:: build-deplibs
150clean-local:: clean-deplibs
151install-local:: install-deplibs
152uninstall-local:: uninstall-deplibs
153
154build-deplibs: $(OutputLibs)
155
156$(OcamlDir)/%.a: $(LibDir)/%.a
157	$(Verb) ln -sf $< $@
158
159$(OcamlDir)/%.o: $(LibDir)/%.o
160	$(Verb) ln -sf $< $@
161
162clean-deplibs:
163	$(Verb) rm -f $(OutputLibs)
164
165install-deplibs:
166	$(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
167	  ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
168	done
169
170uninstall-deplibs:
171	$(Verb) rm -f $(DestLibs)
172
173
174##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
175
176all-local:: build-cmis
177clean-local:: clean-cmis
178install-local:: install-cmis
179uninstall-local:: uninstall-cmis
180
181build-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
190clean-cmis::
191	-$(Verb) $(RM) -f $(OutputsCMI)
192
193# Also install the .mli's (headers) as documentation.
194install-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
205uninstall-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
218all-local:: $(OutputCMA)
219clean-local:: clean-cma
220install-local:: install-cma
221uninstall-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
234clean-cma::
235	$(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
236
237install-cma:: $(OutputCMA)
238	$(Echo) "Installing $(BuildMode) $(DestCMA)"
239	$(Verb) $(MKDIR) $(PROJ_libocamldir)
240	$(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
241
242uninstall-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.
251ifdef OCAMLOPT
252
253all-local:: $(OutputCMXA) $(OutputsCMX)
254clean-local:: clean-cmxa
255install-local:: install-cmxa
256uninstall-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
274clean-cmxa::
275	$(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
276
277install-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
288uninstall-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
298endif
299
300##===- Generate documentation ---------------------------------------------===##
301
302$(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
303	$(Echo) "Documenting $(notdir $@)"
304	$(Verb) $(OCAMLDOC) -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
305
306ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
307
308##===- Debugging gunk -----------------------------------------------------===##
309printvars:: printcamlvars
310
311printcamlvars::
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
343