blob: 8ad085ecad3e1cf920099e7200bcdcb6dd3c4271 [file] [log] [blame]
Gordon Henriksen7e269ec2007-09-18 12:26:17 +00001##===- 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
15include $(LEVEL)/Makefile.config
16
17# Find the ocaml stdlib root. /usr/local/lib/ocaml is the default when built
18# from source; distros use something like /usr/lib/ocaml/3.10.0.
19ifndef OCAML_LIBDIR
20OCAML_LIBDIR := $(shell $(OCAMLC) -where)
21endif
22
23# CFLAGS needs to be set before Makefile.rules is included. Yes, ocaml puts its
24# includes under its libdir.
25CFLAGS += -I$(OCAML_LIBDIR)
26
27include $(LEVEL)/Makefile.common
28
29# Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
30# user can override this with OCAML_LIBDIR.
31PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
32OcamlDir := $(LibDir)/ocaml
33
34# Info from llvm-config and similar
35ifdef UsedComponents
36UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
37UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
38endif
39
40# Tools
Gordon Henriksen55e3d502007-09-20 16:47:41 +000041OCAMLCFLAGS += -I $(OcamlDir) -I $(ObjDir)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000042OCAMLAFLAGS += $(patsubst %,-cclib %, \
43 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
44 $(UsedLibs) -l$(LIBRARYNAME))
45
46Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
47Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
48Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) -o)
49
50Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) -o)
51Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) -o)
52
53# Source files
Gordon Henriksen55e3d502007-09-20 16:47:41 +000054OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
55OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000056
Gordon Henriksen55e3d502007-09-20 16:47:41 +000057OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
58OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000059
Gordon Henriksen55e3d502007-09-20 16:47:41 +000060# Intermediate files
61LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
62LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
63ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
64ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
65ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
66
67# Output files
68# The .cmo files are the only intermediates; all others are to be installed.
69LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
70OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
71OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
72OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
73OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000074
75# Installation targets
76DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
77DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
78DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
79
80
Gordon Henriksen55e3d502007-09-20 16:47:41 +000081##===- Dependencies -------------------------------------------------------===##
82# Copy the sources into the intermediate directory because older ocamlc doesn't
83# support -o except when linking (outputs are placed next to inputs).
84
85$(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
86 $(Verb) $(CP) -f $< $@
87
88$(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
89 $(Verb) $(CP) -f $< $@
90
Gordon Henriksenc2ff95d2007-09-23 13:37:44 +000091$(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
92 $(OcamlDir)/.dir $(ObjDir)/.dir
Gordon Henriksen55e3d502007-09-20 16:47:41 +000093 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
94
Gordon Henriksen4780e3f2007-09-26 20:56:12 +000095$(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
96
Gordon Henriksen55e3d502007-09-20 16:47:41 +000097-include $(ObjDir)/$(LIBRARYNAME).ocamldep
98
99
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000100##===- Build static library from C sources --------------------------------===##
101
102all-local:: $(LibraryA)
103clean-local:: clean-a
104install-local:: install-a
105uninstall-local:: uninstall-a
106
107$(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
108 $(Echo) "Building $(BuildMode) $(notdir $@)"
109 -$(Verb) $(RM) -f $@
110 $(Verb) $(Archive) $@ $(ObjectsO)
111 $(Verb) $(Ranlib) $@
112
113clean-a::
114 -$(Verb) $(RM) -f $(LibraryA)
115
116install-a:: $(LibraryA)
117 $(Echo) "Installing $(BuildMode) $(DestA)"
118 $(Verb) $(MKDIR) $(PROJ_libocamldir)
119 $(Verb) $(LTInstall) $(LibraryA) $(DestA)
120 $(Verb)
121
122uninstall-a::
123 $(Echo) "Uninstalling $(DestA)"
124 -$(Verb) $(RM) -f $(DestA)
125
126
127##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
128
129all-local:: build-cmis
130clean-local:: clean-cmis
131install-local:: install-cmis
132uninstall-local:: uninstall-cmis
133
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000134build-cmis: $(OutputsCMI)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000135
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000136$(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
137 $(Verb) $(CP) -f $< $@
138
139$(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000140 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
141 $(Verb) $(Compile.CMI) $@ $<
142
143clean-cmis::
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000144 -$(Verb) $(RM) -f $(OutputsCMI)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000145
146# Also install the .mli's (headers) as documentation.
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000147install-cmis: $(OutputsCMI) $(OcamlHeaders)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000148 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000149 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000150 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
151 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
152 done
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000153 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000154 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000155 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000156 done
157
158uninstall-cmis::
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000159 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000160 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
161 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
162 done
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000163 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000164 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
165 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
166 done
167
168
169##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
170
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000171all-local:: $(OutputCMA)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000172clean-local:: clean-cma
173install-local:: install-cma
174uninstall-local:: uninstall-cma
175
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000176$(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
177 $(Verb) $(CP) -f $< $@
178
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000179$(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
180 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
181 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
182 $(Verb) for i in $(UsedLibNames); do \
183 ln -sf "$(LibDir)/$$i" "$(OcamlDir)/$$i"; \
184 done
185
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000186$(ObjDir)/%.cmo: $(ObjDir)/%.ml
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000187 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
188 $(Verb) $(Compile.CMO) $@ $<
189
190clean-cma::
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000191 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000192
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000193install-cma:: $(OutputCMA)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000194 $(Echo) "Installing $(BuildMode) $(DestCMA)"
195 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000196 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000197 $(Verb) for i in $(UsedLibNames); do \
198 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
199 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
200 done
201
202uninstall-cma::
203 $(Echo) "Uninstalling $(DestCMA)"
204 -$(Verb) $(RM) -f $(DestCMA)
205 $(Verb) for i in $(UsedLibNames); do \
206 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
207 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
208 done
209
210
211##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
212
213# The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
214# If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
215ifdef OCAMLOPT
216
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000217all-local:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000218clean-local:: clean-cmxa
219install-local:: install-cmxa
220uninstall-local:: uninstall-cmxa
221
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000222$(OutputCMXA): $(LibraryCMXA)
223 $(Verb) $(CP) -f $< $@
224 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
225
226$(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
227 $(Verb) $(CP) -f $< $@
228
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000229$(LibraryCMXA): $(ObjectsCMX)
230 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
231 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
232 $(Verb) $(RM) -f $(@:.cmxa=.o)
233
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000234$(ObjDir)/%.cmx: $(ObjDir)/%.ml
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000235 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
236 $(Verb) $(Compile.CMX) $@ $<
237
238clean-cmxa::
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000239 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000240
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000241install-cmxa:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000242 $(Verb) $(MKDIR) $(PROJ_libocamldir)
243 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000244 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000245 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000246 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
247 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000248 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
249 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
250 done
251
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000252uninstall-cmxa::
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000253 $(Echo) "Uninstalling $(DestCMXA)"
254 $(Verb) $(RM) -f $(DestCMXA)
255 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
256 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000257 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000258 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
259 $(RM) -f $(PROJ_libocamldir)/$$i; \
260 done
261
262endif
263
264
265##===- Debugging gunk -----------------------------------------------------===##
266printvars:: printcamlvars
267
268printcamlvars::
269 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
270 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
271 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
272 $(Echo) "OCAMLC : " '$(OCAMLC)'
273 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000274 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000275 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
276 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
277 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
278 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
279 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
280 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
281 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
282 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000283 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000284 $(Echo) "OcamlSources : " '$(OcamlSources)'
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000285 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000286 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
287 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
288 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
289 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
290 $(Echo) "DestA : " '$(DestA)'
291 $(Echo) "DestCMA : " '$(DestCMA)'
292 $(Echo) "DestCMXA : " '$(DestCMXA)'
293 $(Echo) "UsedLibs : " '$(UsedLibs)'
294 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
295
296.PHONY: printcamlvars build-cmis \
297 clean-a clean-cmis clean-cma clean-cmxa \
298 install-a install-cmis install-cma install-cmxa \
299 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa