fa03c63f326c43607de5e2368127db383a243fb7
[oota-llvm.git] / bindings / ocaml / Makefile.ocaml
1 ##===- tools/ml/Makefile -----------------------------------*- Makefile -*-===##
2
3 #                     The LLVM Compiler Infrastructure
4 #
5 # This file was developed by Gordon Henriksen and is distributed under the
6 # University of Illinois Open Source 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 CFLAGS += -I$(shell $(OCAMLC) -where)
24
25 include $(LEVEL)/Makefile.common
26
27 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
28 # user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
29 PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
30 OcamlDir := $(LibDir)/ocaml
31
32 # Info from llvm-config and similar
33 ifdef UsedComponents
34 UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
35 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
36 endif
37
38 # Tools
39 OCAMLCFLAGS += -I $(OcamlDir) -I $(ObjDir)
40 OCAMLAFLAGS += $(patsubst %,-cclib %, \
41                  $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
42                                           $(shell $(LLVM_CONFIG) --ldflags)) \
43                                           $(UsedLibs))
44
45 Compile.CMI  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
46 Compile.CMO  := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
47 Archive.CMA  := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) -o)
48
49 Compile.CMX  := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) -o)
50 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) -o)
51
52 # Source files
53 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
54 OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
55
56 OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
57 OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
58
59 # Intermediate files
60 LibraryCMA   := $(ObjDir)/$(LIBRARYNAME).cma
61 LibraryCMXA  := $(ObjDir)/$(LIBRARYNAME).cmxa
62 ObjectsCMI   := $(OcamlSources:%.ml=%.cmi)
63 ObjectsCMO   := $(OcamlSources:%.ml=%.cmo)
64 ObjectsCMX   := $(OcamlSources:%.ml=%.cmx)
65
66 # Output files
67 #   The .cmo files are the only intermediates; all others are to be installed.
68 LibraryA   := $(OcamlDir)/lib$(LIBRARYNAME).a
69 OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
70 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
71 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
72 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
73
74 # Installation targets
75 DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
76 DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
77 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
78
79
80 ##===- Dependencies -------------------------------------------------------===##
81 # Copy the sources into the intermediate directory because older ocamlc doesn't
82 # support -o except when linking (outputs are placed next to inputs).
83
84 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
85         $(Verb) $(CP) -f $< $@
86
87 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
88         $(Verb) $(CP) -f $< $@
89
90 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
91                                    $(OcamlDir)/.dir $(ObjDir)/.dir
92         $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
93
94 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
95
96 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
97
98
99 ##===- Build static library from C sources --------------------------------===##
100
101 all-local:: $(LibraryA)
102 clean-local:: clean-a
103 install-local:: install-a
104 uninstall-local:: uninstall-a
105
106 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
107         $(Echo) "Building $(BuildMode) $(notdir $@)"
108         -$(Verb) $(RM) -f $@
109         $(Verb) $(Archive) $@ $(ObjectsO)
110         $(Verb) $(Ranlib) $@
111
112 clean-a::
113         -$(Verb) $(RM) -f $(LibraryA)
114
115 install-a:: $(LibraryA)
116         $(Echo) "Installing $(BuildMode) $(DestA)"
117         $(Verb) $(MKDIR) $(PROJ_libocamldir)
118         $(Verb) $(LTInstall) $(LibraryA) $(DestA)
119         $(Verb) 
120
121 uninstall-a::
122         $(Echo) "Uninstalling $(DestA)"
123         -$(Verb) $(RM) -f $(DestA)
124
125
126 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
127
128 all-local:: build-cmis
129 clean-local:: clean-cmis
130 install-local:: install-cmis
131 uninstall-local:: uninstall-cmis
132
133 build-cmis: $(OutputsCMI)
134
135 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
136         $(Verb) $(CP) -f $< $@
137
138 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
139         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
140         $(Verb) $(Compile.CMI) $@ $<
141
142 clean-cmis::
143         -$(Verb) $(RM) -f $(OutputsCMI)
144
145 # Also install the .mli's (headers) as documentation.
146 install-cmis: $(OutputsCMI) $(OcamlHeaders)
147         $(Verb) $(MKDIR) $(PROJ_libocamldir)
148         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
149           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
150           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
151         done
152         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
153           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
154           $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
155         done
156
157 uninstall-cmis::
158         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
159           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
160           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
161         done
162         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
163           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
164           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
165         done
166
167
168 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
169
170 all-local:: $(OutputCMA)
171 clean-local:: clean-cma
172 install-local:: install-cma
173 uninstall-local:: uninstall-cma
174
175 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
176         $(Verb) $(CP) -f $< $@
177
178 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
179         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
180         $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
181         $(Verb) for i in $(UsedLibNames); do \
182           ln -sf "$(LibDir)/$$i" "$(OcamlDir)/$$i"; \
183         done
184
185 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
186         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
187         $(Verb) $(Compile.CMO) $@ $<
188
189 clean-cma::
190         $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
191
192 install-cma:: $(OutputCMA)
193         $(Echo) "Installing $(BuildMode) $(DestCMA)"
194         $(Verb) $(MKDIR) $(PROJ_libocamldir)
195         $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
196         $(Verb) for i in $(UsedLibNames); do \
197           ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
198         done
199
200 uninstall-cma::
201         $(Echo) "Uninstalling $(DestCMA)"
202         -$(Verb) $(RM) -f $(DestCMA)
203         $(Verb) for i in $(UsedLibNames); do \
204           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
205         done
206
207
208 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
209
210 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
211 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
212 ifdef OCAMLOPT
213
214 all-local:: $(OutputCMXA) $(OutputsCMX)
215 clean-local:: clean-cmxa
216 install-local:: install-cmxa
217 uninstall-local:: uninstall-cmxa
218
219 $(OutputCMXA): $(LibraryCMXA)
220         $(Verb) $(CP) -f $< $@
221         $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
222
223 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
224         $(Verb) $(CP) -f $< $@
225
226 $(LibraryCMXA): $(ObjectsCMX)
227         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
228         $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
229         $(Verb) $(RM) -f $(@:.cmxa=.o)
230
231 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
232         $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
233         $(Verb) $(Compile.CMX) $@ $<
234
235 clean-cmxa::
236         $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
237
238 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
239         $(Verb) $(MKDIR) $(PROJ_libocamldir)
240         $(Echo) "Installing $(BuildMode) $(DestCMXA)"
241         $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
242         $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
243         $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
244         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
245           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
246           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
247         done
248
249 uninstall-cmxa::
250         $(Echo) "Uninstalling $(DestCMXA)"
251         $(Verb) $(RM) -f $(DestCMXA)
252         $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
253         $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
254         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
255           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
256           $(RM) -f $(PROJ_libocamldir)/$$i; \
257         done
258
259 endif
260
261
262 ##===- Debugging gunk -----------------------------------------------------===##
263 printvars:: printcamlvars
264
265 printcamlvars::
266         $(Echo) "LLVM_CONFIG  : " '$(LLVM_CONFIG)'
267         $(Echo) "OCAMLCFLAGS  : " '$(OCAMLCFLAGS)'
268         $(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
269         $(Echo) "OCAMLC       : " '$(OCAMLC)'
270         $(Echo) "OCAMLOPT     : " '$(OCAMLOPT)'
271         $(Echo) "OCAMLDEP     : " '$(OCAMLDEP)'
272         $(Echo) "Compile.CMI  : " '$(Compile.CMI)'
273         $(Echo) "Compile.CMO  : " '$(Compile.CMO)'
274         $(Echo) "Archive.CMA  : " '$(Archive.CMA)'
275         $(Echo) "Compile.CMX  : " '$(Compile.CMX)'
276         $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
277         $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
278         $(Echo) "LibraryCMA   : " '$(LibraryCMA)'
279         $(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
280         $(Echo) "OcamlSources1: " '$(OcamlSources1)'
281         $(Echo) "OcamlSources : " '$(OcamlSources)'
282         $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
283         $(Echo) "ObjectsCMI   : " '$(ObjectsCMI)'
284         $(Echo) "ObjectsCMO   : " '$(ObjectsCMO)'
285         $(Echo) "ObjectsCMX   : " '$(ObjectsCMX)'
286         $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
287         $(Echo) "DestA        : " '$(DestA)'
288         $(Echo) "DestCMA      : " '$(DestCMA)'
289         $(Echo) "DestCMXA     : " '$(DestCMXA)'
290         $(Echo) "UsedLibs     : " '$(UsedLibs)'
291         $(Echo) "UsedLibNames : " '$(UsedLibNames)'
292
293 .PHONY: printcamlvars   build-cmis \
294             clean-a     clean-cmis     clean-cma     clean-cmxa \
295           install-a   install-cmis   install-cma   install-cmxa \
296                 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa