blob: b7f4fdec29fcdfdbfb2e08ebfbea5911583f5bf8 [file] [log] [blame]
Gordon Henriksen0908d492007-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 Henriksenc0491ac2007-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 Henriksen0908d492007-09-18 12:26:17 +000018##===----------------------------------------------------------------------===##
19
20include $(LEVEL)/Makefile.config
21
Gordon Henriksen3b802472007-10-04 00:07:50 +000022# CFLAGS needs to be set before Makefile.rules is included.
23CFLAGS += -I$(shell $(OCAMLC) -where)
Gordon Henriksen0908d492007-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 Henriksen1d4e0782007-10-02 16:42:10 +000028# user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
Gordon Henriksen0908d492007-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 Henriksena8c36602007-09-20 16:47:41 +000039OCAMLCFLAGS += -I $(OcamlDir) -I $(ObjDir)
Gordon Henriksen0908d492007-09-18 12:26:17 +000040OCAMLAFLAGS += $(patsubst %,-cclib %, \
Gordon Henriksened29a282007-10-12 19:48:13 +000041 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
42 $(shell $(LLVM_CONFIG) --ldflags)) \
43 $(UsedLibs))
Gordon Henriksen0908d492007-09-18 12:26:17 +000044
45Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
46Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
47Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) -o)
48
49Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) -o)
50Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) -o)
51
52# Source files
Gordon Henriksena8c36602007-09-20 16:47:41 +000053OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
54OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
Gordon Henriksen0908d492007-09-18 12:26:17 +000055
Gordon Henriksena8c36602007-09-20 16:47:41 +000056OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
57OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
Gordon Henriksen0908d492007-09-18 12:26:17 +000058
Gordon Henriksena8c36602007-09-20 16:47:41 +000059# Intermediate files
60LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
61LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
62ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
63ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
64ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
65
66# Output files
67# The .cmo files are the only intermediates; all others are to be installed.
68LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
69OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
70OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
71OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
72OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
Gordon Henriksen516276e2007-12-03 21:15:53 +000073OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
Gordon Henriksen0908d492007-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
Gordon Henriksen516276e2007-12-03 21:15:53 +000079DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
Gordon Henriksen0908d492007-09-18 12:26:17 +000080
81
Gordon Henriksena8c36602007-09-20 16:47:41 +000082##===- 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
Gordon Henriksenab4456f2007-09-23 13:37:44 +000092$(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
93 $(OcamlDir)/.dir $(ObjDir)/.dir
Gordon Henriksena8c36602007-09-20 16:47:41 +000094 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
95
Gordon Henriksen46abf912007-09-26 20:56:12 +000096$(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
97
Gordon Henriksena8c36602007-09-20 16:47:41 +000098-include $(ObjDir)/$(LIBRARYNAME).ocamldep
99
100
Gordon Henriksen0908d492007-09-18 12:26:17 +0000101##===- Build static library from C sources --------------------------------===##
102
103all-local:: $(LibraryA)
104clean-local:: clean-a
105install-local:: install-a
106uninstall-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
114clean-a::
115 -$(Verb) $(RM) -f $(LibraryA)
116
117install-a:: $(LibraryA)
118 $(Echo) "Installing $(BuildMode) $(DestA)"
119 $(Verb) $(MKDIR) $(PROJ_libocamldir)
120 $(Verb) $(LTInstall) $(LibraryA) $(DestA)
121 $(Verb)
122
123uninstall-a::
124 $(Echo) "Uninstalling $(DestA)"
125 -$(Verb) $(RM) -f $(DestA)
126
127
Gordon Henriksen516276e2007-12-03 21:15:53 +0000128##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
129
130all-local:: build-deplibs
131clean-local:: clean-deplibs
132install-local:: install-deplibs
133uninstall-local:: uninstall-deplibs
134
135build-deplibs: $(OutputLibs)
136
137$(OcamlDir)/%.a: $(LibDir)/%.a
138 $(Verb) ln -sf $< $@
139
140clean-deplibs:
141 $(Verb) rm -f $(OutputLibs)
142
143install-deplibs:
144 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
145 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
146 done
147
148uninstall-deplibs:
149 $(Verb) rm -f $(DestLibs)
150
151
Gordon Henriksen0908d492007-09-18 12:26:17 +0000152##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
153
154all-local:: build-cmis
155clean-local:: clean-cmis
156install-local:: install-cmis
157uninstall-local:: uninstall-cmis
158
Gordon Henriksena8c36602007-09-20 16:47:41 +0000159build-cmis: $(OutputsCMI)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000160
Gordon Henriksena8c36602007-09-20 16:47:41 +0000161$(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
162 $(Verb) $(CP) -f $< $@
163
164$(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
Gordon Henriksen0908d492007-09-18 12:26:17 +0000165 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
166 $(Verb) $(Compile.CMI) $@ $<
167
168clean-cmis::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000169 -$(Verb) $(RM) -f $(OutputsCMI)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000170
171# Also install the .mli's (headers) as documentation.
Gordon Henriksena8c36602007-09-20 16:47:41 +0000172install-cmis: $(OutputsCMI) $(OcamlHeaders)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000173 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000174 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000175 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
176 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
177 done
Gordon Henriksena8c36602007-09-20 16:47:41 +0000178 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000179 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
Gordon Henriksena8c36602007-09-20 16:47:41 +0000180 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000181 done
182
183uninstall-cmis::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000184 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000185 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
186 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
187 done
Gordon Henriksena8c36602007-09-20 16:47:41 +0000188 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000189 $(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
Gordon Henriksena8c36602007-09-20 16:47:41 +0000196all-local:: $(OutputCMA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000197clean-local:: clean-cma
198install-local:: install-cma
199uninstall-local:: uninstall-cma
200
Gordon Henriksena8c36602007-09-20 16:47:41 +0000201$(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
202 $(Verb) $(CP) -f $< $@
203
Gordon Henriksen0908d492007-09-18 12:26:17 +0000204$(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
205 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
206 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000207
Gordon Henriksena8c36602007-09-20 16:47:41 +0000208$(ObjDir)/%.cmo: $(ObjDir)/%.ml
Gordon Henriksen0908d492007-09-18 12:26:17 +0000209 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
210 $(Verb) $(Compile.CMO) $@ $<
211
212clean-cma::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000213 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000214
Gordon Henriksena8c36602007-09-20 16:47:41 +0000215install-cma:: $(OutputCMA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000216 $(Echo) "Installing $(BuildMode) $(DestCMA)"
217 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000218 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
Gordon Henriksen0908d492007-09-18 12:26:17 +0000219
220uninstall-cma::
221 $(Echo) "Uninstalling $(DestCMA)"
222 -$(Verb) $(RM) -f $(DestCMA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000223
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.
229ifdef OCAMLOPT
230
Gordon Henriksena8c36602007-09-20 16:47:41 +0000231all-local:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000232clean-local:: clean-cmxa
233install-local:: install-cmxa
234uninstall-local:: uninstall-cmxa
235
Gordon Henriksena8c36602007-09-20 16:47:41 +0000236$(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
Gordon Henriksen0908d492007-09-18 12:26:17 +0000243$(LibraryCMXA): $(ObjectsCMX)
244 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
245 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
246 $(Verb) $(RM) -f $(@:.cmxa=.o)
247
Gordon Henriksena8c36602007-09-20 16:47:41 +0000248$(ObjDir)/%.cmx: $(ObjDir)/%.ml
Gordon Henriksen0908d492007-09-18 12:26:17 +0000249 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
250 $(Verb) $(Compile.CMX) $@ $<
251
252clean-cmxa::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000253 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000254
Gordon Henriksena8c36602007-09-20 16:47:41 +0000255install-cmxa:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000256 $(Verb) $(MKDIR) $(PROJ_libocamldir)
257 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
Gordon Henriksena8c36602007-09-20 16:47:41 +0000258 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000259 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
Gordon Henriksena8c36602007-09-20 16:47:41 +0000260 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
261 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000262 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
263 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
264 done
265
Gordon Henriksena8c36602007-09-20 16:47:41 +0000266uninstall-cmxa::
Gordon Henriksen0908d492007-09-18 12:26:17 +0000267 $(Echo) "Uninstalling $(DestCMXA)"
268 $(Verb) $(RM) -f $(DestCMXA)
269 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
270 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000271 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000272 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
273 $(RM) -f $(PROJ_libocamldir)/$$i; \
274 done
275
276endif
277
278
279##===- Debugging gunk -----------------------------------------------------===##
280printvars:: printcamlvars
281
282printcamlvars::
283 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
284 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
285 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
286 $(Echo) "OCAMLC : " '$(OCAMLC)'
287 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000288 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000289 $(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)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000297 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000298 $(Echo) "OcamlSources : " '$(OcamlSources)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000299 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000300 $(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