Sketch out an implementation of Briggs' copy placement algorithm.
[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 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
74
75 # Installation targets
76 DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
77 DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
78 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
79 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
80
81
82 ##===- Dependencies -------------------------------------------------------===##
83 # Copy the sources into the intermediate directory because older ocamlc doesn't
84 # support -o except when linking (outputs are placed next to inputs).
85
86 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
87         $(Verb) $(CP) -f $< $@
88
89 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
90         $(Verb) $(CP) -f $< $@
91
92 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
93                                    $(OcamlDir)/.dir $(ObjDir)/.dir
94         $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
95
96 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
97
98 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
99
100
101 ##===- Build static library from C sources --------------------------------===##
102
103 all-local:: $(LibraryA)
104 clean-local:: clean-a
105 install-local:: install-a
106 uninstall-local:: uninstall-a
107
108 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
109         $(Echo) "Building $(BuildMode) $(notdir $@)"
110         -$(Verb) $(RM) -f $@
111         $(Verb) $(Archive) $@ $(ObjectsO)
112         $(Verb) $(Ranlib) $@
113
114 clean-a::
115         -$(Verb) $(RM) -f $(LibraryA)
116
117 install-a:: $(LibraryA)
118         $(Echo) "Installing $(BuildMode) $(DestA)"
119         $(Verb) $(MKDIR) $(PROJ_libocamldir)
120         $(Verb) $(LTInstall) $(LibraryA) $(DestA)
121         $(Verb) 
122
123 uninstall-a::
124         $(Echo) "Uninstalling $(DestA)"
125         -$(Verb) $(RM) -f $(DestA)
126
127
128 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
129
130 all-local:: build-deplibs
131 clean-local:: clean-deplibs
132 install-local:: install-deplibs
133 uninstall-local:: uninstall-deplibs
134
135 build-deplibs: $(OutputLibs)
136
137 $(OcamlDir)/%.a: $(LibDir)/%.a
138         $(Verb) ln -sf $< $@
139
140 clean-deplibs:
141         $(Verb) rm -f $(OutputLibs)
142
143 install-deplibs:
144         $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
145           ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
146         done
147
148 uninstall-deplibs:
149         $(Verb) rm -f $(DestLibs)
150
151
152 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
153
154 all-local:: build-cmis
155 clean-local:: clean-cmis
156 install-local:: install-cmis
157 uninstall-local:: uninstall-cmis
158
159 build-cmis: $(OutputsCMI)
160
161 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
162         $(Verb) $(CP) -f $< $@
163
164 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
165         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
166         $(Verb) $(Compile.CMI) $@ $<
167
168 clean-cmis::
169         -$(Verb) $(RM) -f $(OutputsCMI)
170
171 # Also install the .mli's (headers) as documentation.
172 install-cmis: $(OutputsCMI) $(OcamlHeaders)
173         $(Verb) $(MKDIR) $(PROJ_libocamldir)
174         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
175           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
176           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
177         done
178         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
179           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
180           $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
181         done
182
183 uninstall-cmis::
184         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
185           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
186           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
187         done
188         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
189           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
190           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
191         done
192
193
194 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
195
196 all-local:: $(OutputCMA)
197 clean-local:: clean-cma
198 install-local:: install-cma
199 uninstall-local:: uninstall-cma
200
201 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
202         $(Verb) $(CP) -f $< $@
203
204 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
205         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
206         $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
207
208 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
209         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
210         $(Verb) $(Compile.CMO) $@ $<
211
212 clean-cma::
213         $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
214
215 install-cma:: $(OutputCMA)
216         $(Echo) "Installing $(BuildMode) $(DestCMA)"
217         $(Verb) $(MKDIR) $(PROJ_libocamldir)
218         $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
219
220 uninstall-cma::
221         $(Echo) "Uninstalling $(DestCMA)"
222         -$(Verb) $(RM) -f $(DestCMA)
223
224
225 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
226
227 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
228 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
229 ifdef OCAMLOPT
230
231 all-local:: $(OutputCMXA) $(OutputsCMX)
232 clean-local:: clean-cmxa
233 install-local:: install-cmxa
234 uninstall-local:: uninstall-cmxa
235
236 $(OutputCMXA): $(LibraryCMXA)
237         $(Verb) $(CP) -f $< $@
238         $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
239
240 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
241         $(Verb) $(CP) -f $< $@
242
243 $(LibraryCMXA): $(ObjectsCMX)
244         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
245         $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
246         $(Verb) $(RM) -f $(@:.cmxa=.o)
247
248 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
249         $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
250         $(Verb) $(Compile.CMX) $@ $<
251
252 clean-cmxa::
253         $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
254
255 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
256         $(Verb) $(MKDIR) $(PROJ_libocamldir)
257         $(Echo) "Installing $(BuildMode) $(DestCMXA)"
258         $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
259         $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
260         $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
261         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
262           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
263           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
264         done
265
266 uninstall-cmxa::
267         $(Echo) "Uninstalling $(DestCMXA)"
268         $(Verb) $(RM) -f $(DestCMXA)
269         $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
270         $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
271         $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
272           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
273           $(RM) -f $(PROJ_libocamldir)/$$i; \
274         done
275
276 endif
277
278
279 ##===- Debugging gunk -----------------------------------------------------===##
280 printvars:: printcamlvars
281
282 printcamlvars::
283         $(Echo) "LLVM_CONFIG  : " '$(LLVM_CONFIG)'
284         $(Echo) "OCAMLCFLAGS  : " '$(OCAMLCFLAGS)'
285         $(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
286         $(Echo) "OCAMLC       : " '$(OCAMLC)'
287         $(Echo) "OCAMLOPT     : " '$(OCAMLOPT)'
288         $(Echo) "OCAMLDEP     : " '$(OCAMLDEP)'
289         $(Echo) "Compile.CMI  : " '$(Compile.CMI)'
290         $(Echo) "Compile.CMO  : " '$(Compile.CMO)'
291         $(Echo) "Archive.CMA  : " '$(Archive.CMA)'
292         $(Echo) "Compile.CMX  : " '$(Compile.CMX)'
293         $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
294         $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
295         $(Echo) "LibraryCMA   : " '$(LibraryCMA)'
296         $(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
297         $(Echo) "OcamlSources1: " '$(OcamlSources1)'
298         $(Echo) "OcamlSources : " '$(OcamlSources)'
299         $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
300         $(Echo) "ObjectsCMI   : " '$(ObjectsCMI)'
301         $(Echo) "ObjectsCMO   : " '$(ObjectsCMO)'
302         $(Echo) "ObjectsCMX   : " '$(ObjectsCMX)'
303         $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
304         $(Echo) "DestA        : " '$(DestA)'
305         $(Echo) "DestCMA      : " '$(DestCMA)'
306         $(Echo) "DestCMXA     : " '$(DestCMXA)'
307         $(Echo) "UsedLibs     : " '$(UsedLibs)'
308         $(Echo) "UsedLibNames : " '$(UsedLibNames)'
309
310 .PHONY: printcamlvars   build-cmis \
311             clean-a     clean-cmis     clean-cma     clean-cmxa \
312           install-a   install-cmis   install-cma   install-cmxa \
313                 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa