blob: 6008c3a083121a1b232aad13c865bc4ebd26f4e9 [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#
Gordon Henriksen3ed72362007-10-06 21:00:36 +000013# 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#
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000018##===----------------------------------------------------------------------===##
19
20include $(LEVEL)/Makefile.config
21
Gordon Henriksen12c1de42007-10-04 00:07:50 +000022# CFLAGS needs to be set before Makefile.rules is included.
23CFLAGS += -I$(shell $(OCAMLC) -where)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000024
25include $(LEVEL)/Makefile.common
26
27# Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
Gordon Henriksenfda571d2007-10-02 16:42:10 +000028# user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000029PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
30OcamlDir := $(LibDir)/ocaml
31
32# Info from llvm-config and similar
33ifdef UsedComponents
34UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
35UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
36endif
37
38# Tools
Gordon Henriksen55e3d502007-09-20 16:47:41 +000039OCAMLCFLAGS += -I $(OcamlDir) -I $(ObjDir)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000040OCAMLAFLAGS += $(patsubst %,-cclib %, \
Gordon Henriksen2a50f362007-10-12 19:48:13 +000041 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
42 $(shell $(LLVM_CONFIG) --ldflags)) \
43 $(UsedLibs))
Gordon Henriksen8db172d2007-12-23 16:59:28 +000044
45ifneq ($(ENABLE_OPTIMIZED),1)
46 OCAMLDEBUGFLAG := -g
47endif
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000048
Gordon Henriksen8db172d2007-12-23 16:59:28 +000049Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
50Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
51Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
52 -o)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000053
Gordon Henriksen8db172d2007-12-23 16:59:28 +000054Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
55Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000056
57# Source files
Gordon Henriksen55e3d502007-09-20 16:47:41 +000058OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
59OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000060
Gordon Henriksen55e3d502007-09-20 16:47:41 +000061OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
62OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000063
Gordon Henriksen55e3d502007-09-20 16:47:41 +000064# Intermediate files
65LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
66LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
67ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
68ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
69ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
70
71# Output files
72# The .cmo files are the only intermediates; all others are to be installed.
73LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
74OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
75OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
76OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
77OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
Gordon Henriksenf3608a02007-12-03 21:15:53 +000078OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000079
80# Installation targets
81DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
82DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
83DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
Gordon Henriksenf3608a02007-12-03 21:15:53 +000084DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +000085
86
Gordon Henriksen55e3d502007-09-20 16:47:41 +000087##===- Dependencies -------------------------------------------------------===##
88# Copy the sources into the intermediate directory because older ocamlc doesn't
89# support -o except when linking (outputs are placed next to inputs).
90
91$(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
92 $(Verb) $(CP) -f $< $@
93
94$(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
95 $(Verb) $(CP) -f $< $@
96
Gordon Henriksenc2ff95d2007-09-23 13:37:44 +000097$(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
98 $(OcamlDir)/.dir $(ObjDir)/.dir
Gordon Henriksen55e3d502007-09-20 16:47:41 +000099 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
100
Gordon Henriksen4780e3f2007-09-26 20:56:12 +0000101$(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
102
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000103-include $(ObjDir)/$(LIBRARYNAME).ocamldep
104
105
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000106##===- Build static library from C sources --------------------------------===##
107
108all-local:: $(LibraryA)
109clean-local:: clean-a
110install-local:: install-a
111uninstall-local:: uninstall-a
112
113$(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
114 $(Echo) "Building $(BuildMode) $(notdir $@)"
115 -$(Verb) $(RM) -f $@
116 $(Verb) $(Archive) $@ $(ObjectsO)
117 $(Verb) $(Ranlib) $@
118
119clean-a::
120 -$(Verb) $(RM) -f $(LibraryA)
121
122install-a:: $(LibraryA)
123 $(Echo) "Installing $(BuildMode) $(DestA)"
124 $(Verb) $(MKDIR) $(PROJ_libocamldir)
125 $(Verb) $(LTInstall) $(LibraryA) $(DestA)
126 $(Verb)
127
128uninstall-a::
129 $(Echo) "Uninstalling $(DestA)"
130 -$(Verb) $(RM) -f $(DestA)
131
132
Gordon Henriksenf3608a02007-12-03 21:15:53 +0000133##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
134
135all-local:: build-deplibs
136clean-local:: clean-deplibs
137install-local:: install-deplibs
138uninstall-local:: uninstall-deplibs
139
140build-deplibs: $(OutputLibs)
141
142$(OcamlDir)/%.a: $(LibDir)/%.a
143 $(Verb) ln -sf $< $@
144
Gordon Henriksen8db172d2007-12-23 16:59:28 +0000145$(OcamlDir)/%.o: $(LibDir)/%.o
146 $(Verb) ln -sf $< $@
147
Gordon Henriksenf3608a02007-12-03 21:15:53 +0000148clean-deplibs:
149 $(Verb) rm -f $(OutputLibs)
150
151install-deplibs:
152 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
153 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
154 done
155
156uninstall-deplibs:
157 $(Verb) rm -f $(DestLibs)
158
159
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000160##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
161
162all-local:: build-cmis
163clean-local:: clean-cmis
164install-local:: install-cmis
165uninstall-local:: uninstall-cmis
166
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000167build-cmis: $(OutputsCMI)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000168
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000169$(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
170 $(Verb) $(CP) -f $< $@
171
172$(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000173 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
174 $(Verb) $(Compile.CMI) $@ $<
175
176clean-cmis::
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000177 -$(Verb) $(RM) -f $(OutputsCMI)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000178
179# Also install the .mli's (headers) as documentation.
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000180install-cmis: $(OutputsCMI) $(OcamlHeaders)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000181 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000182 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000183 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
184 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
185 done
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000186 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000187 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000188 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000189 done
190
191uninstall-cmis::
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000192 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000193 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
194 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
195 done
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000196 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000197 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
198 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
199 done
200
201
202##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
203
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000204all-local:: $(OutputCMA)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000205clean-local:: clean-cma
206install-local:: install-cma
207uninstall-local:: uninstall-cma
208
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000209$(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
210 $(Verb) $(CP) -f $< $@
211
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000212$(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
213 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
214 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000215
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000216$(ObjDir)/%.cmo: $(ObjDir)/%.ml
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000217 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
218 $(Verb) $(Compile.CMO) $@ $<
219
220clean-cma::
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000221 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000222
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000223install-cma:: $(OutputCMA)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000224 $(Echo) "Installing $(BuildMode) $(DestCMA)"
225 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000226 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000227
228uninstall-cma::
229 $(Echo) "Uninstalling $(DestCMA)"
230 -$(Verb) $(RM) -f $(DestCMA)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000231
232
233##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
234
235# The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
236# If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
237ifdef OCAMLOPT
238
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000239all-local:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000240clean-local:: clean-cmxa
241install-local:: install-cmxa
242uninstall-local:: uninstall-cmxa
243
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000244$(OutputCMXA): $(LibraryCMXA)
245 $(Verb) $(CP) -f $< $@
246 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
247
248$(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
249 $(Verb) $(CP) -f $< $@
250
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000251$(LibraryCMXA): $(ObjectsCMX)
252 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
253 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
254 $(Verb) $(RM) -f $(@:.cmxa=.o)
255
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000256$(ObjDir)/%.cmx: $(ObjDir)/%.ml
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000257 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
258 $(Verb) $(Compile.CMX) $@ $<
259
260clean-cmxa::
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000261 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000262
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000263install-cmxa:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000264 $(Verb) $(MKDIR) $(PROJ_libocamldir)
265 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000266 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000267 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000268 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
269 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000270 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
271 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
272 done
273
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000274uninstall-cmxa::
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000275 $(Echo) "Uninstalling $(DestCMXA)"
276 $(Verb) $(RM) -f $(DestCMXA)
277 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
278 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000279 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000280 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
281 $(RM) -f $(PROJ_libocamldir)/$$i; \
282 done
283
284endif
285
286
287##===- Debugging gunk -----------------------------------------------------===##
288printvars:: printcamlvars
289
290printcamlvars::
291 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
292 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
293 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
294 $(Echo) "OCAMLC : " '$(OCAMLC)'
295 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000296 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000297 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
298 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
299 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
300 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
301 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
302 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
303 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
304 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000305 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000306 $(Echo) "OcamlSources : " '$(OcamlSources)'
Gordon Henriksen55e3d502007-09-20 16:47:41 +0000307 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
Gordon Henriksen7e269ec2007-09-18 12:26:17 +0000308 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
309 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
310 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
311 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
312 $(Echo) "DestA : " '$(DestA)'
313 $(Echo) "DestCMA : " '$(DestCMA)'
314 $(Echo) "DestCMXA : " '$(DestCMXA)'
315 $(Echo) "UsedLibs : " '$(UsedLibs)'
316 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
317
318.PHONY: printcamlvars build-cmis \
319 clean-a clean-cmis clean-cma clean-cmxa \
320 install-a install-cmis install-cma install-cmxa \
321 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa