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