blob: fb03b1a3e24745cb90abc117bd615c09bdc6ba5d [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 %, \
41 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
42 $(UsedLibs) -l$(LIBRARYNAME))
43
44Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
45Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
46Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) -o)
47
48Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) -o)
49Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) -o)
50
51# Source files
Gordon Henriksena8c36602007-09-20 16:47:41 +000052OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
53OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
Gordon Henriksen0908d492007-09-18 12:26:17 +000054
Gordon Henriksena8c36602007-09-20 16:47:41 +000055OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
56OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
Gordon Henriksen0908d492007-09-18 12:26:17 +000057
Gordon Henriksena8c36602007-09-20 16:47:41 +000058# Intermediate files
59LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
60LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
61ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
62ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
63ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
64
65# Output files
66# The .cmo files are the only intermediates; all others are to be installed.
67LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
68OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
69OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
70OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
71OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
Gordon Henriksen0908d492007-09-18 12:26:17 +000072
73# Installation targets
74DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
75DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
76DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
77
78
Gordon Henriksena8c36602007-09-20 16:47:41 +000079##===- Dependencies -------------------------------------------------------===##
80# Copy the sources into the intermediate directory because older ocamlc doesn't
81# support -o except when linking (outputs are placed next to inputs).
82
83$(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
84 $(Verb) $(CP) -f $< $@
85
86$(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
87 $(Verb) $(CP) -f $< $@
88
Gordon Henriksenab4456f2007-09-23 13:37:44 +000089$(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
90 $(OcamlDir)/.dir $(ObjDir)/.dir
Gordon Henriksena8c36602007-09-20 16:47:41 +000091 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
92
Gordon Henriksen46abf912007-09-26 20:56:12 +000093$(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
94
Gordon Henriksena8c36602007-09-20 16:47:41 +000095-include $(ObjDir)/$(LIBRARYNAME).ocamldep
96
97
Gordon Henriksen0908d492007-09-18 12:26:17 +000098##===- Build static library from C sources --------------------------------===##
99
100all-local:: $(LibraryA)
101clean-local:: clean-a
102install-local:: install-a
103uninstall-local:: uninstall-a
104
105$(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
106 $(Echo) "Building $(BuildMode) $(notdir $@)"
107 -$(Verb) $(RM) -f $@
108 $(Verb) $(Archive) $@ $(ObjectsO)
109 $(Verb) $(Ranlib) $@
110
111clean-a::
112 -$(Verb) $(RM) -f $(LibraryA)
113
114install-a:: $(LibraryA)
115 $(Echo) "Installing $(BuildMode) $(DestA)"
116 $(Verb) $(MKDIR) $(PROJ_libocamldir)
117 $(Verb) $(LTInstall) $(LibraryA) $(DestA)
118 $(Verb)
119
120uninstall-a::
121 $(Echo) "Uninstalling $(DestA)"
122 -$(Verb) $(RM) -f $(DestA)
123
124
125##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
126
127all-local:: build-cmis
128clean-local:: clean-cmis
129install-local:: install-cmis
130uninstall-local:: uninstall-cmis
131
Gordon Henriksena8c36602007-09-20 16:47:41 +0000132build-cmis: $(OutputsCMI)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000133
Gordon Henriksena8c36602007-09-20 16:47:41 +0000134$(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
135 $(Verb) $(CP) -f $< $@
136
137$(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
Gordon Henriksen0908d492007-09-18 12:26:17 +0000138 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
139 $(Verb) $(Compile.CMI) $@ $<
140
141clean-cmis::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000142 -$(Verb) $(RM) -f $(OutputsCMI)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000143
144# Also install the .mli's (headers) as documentation.
Gordon Henriksena8c36602007-09-20 16:47:41 +0000145install-cmis: $(OutputsCMI) $(OcamlHeaders)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000146 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000147 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000148 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
149 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
150 done
Gordon Henriksena8c36602007-09-20 16:47:41 +0000151 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000152 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
Gordon Henriksena8c36602007-09-20 16:47:41 +0000153 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000154 done
155
156uninstall-cmis::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000157 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000158 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
159 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
160 done
Gordon Henriksena8c36602007-09-20 16:47:41 +0000161 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000162 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
163 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
164 done
165
166
167##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
168
Gordon Henriksena8c36602007-09-20 16:47:41 +0000169all-local:: $(OutputCMA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000170clean-local:: clean-cma
171install-local:: install-cma
172uninstall-local:: uninstall-cma
173
Gordon Henriksena8c36602007-09-20 16:47:41 +0000174$(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
175 $(Verb) $(CP) -f $< $@
176
Gordon Henriksen0908d492007-09-18 12:26:17 +0000177$(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
178 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
179 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
180 $(Verb) for i in $(UsedLibNames); do \
181 ln -sf "$(LibDir)/$$i" "$(OcamlDir)/$$i"; \
182 done
183
Gordon Henriksena8c36602007-09-20 16:47:41 +0000184$(ObjDir)/%.cmo: $(ObjDir)/%.ml
Gordon Henriksen0908d492007-09-18 12:26:17 +0000185 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
186 $(Verb) $(Compile.CMO) $@ $<
187
188clean-cma::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000189 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000190
Gordon Henriksena8c36602007-09-20 16:47:41 +0000191install-cma:: $(OutputCMA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000192 $(Echo) "Installing $(BuildMode) $(DestCMA)"
193 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000194 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
Gordon Henriksen0908d492007-09-18 12:26:17 +0000195 $(Verb) for i in $(UsedLibNames); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000196 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
197 done
198
199uninstall-cma::
200 $(Echo) "Uninstalling $(DestCMA)"
201 -$(Verb) $(RM) -f $(DestCMA)
202 $(Verb) for i in $(UsedLibNames); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000203 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
204 done
205
206
207##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
208
209# The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
210# If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
211ifdef OCAMLOPT
212
Gordon Henriksena8c36602007-09-20 16:47:41 +0000213all-local:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000214clean-local:: clean-cmxa
215install-local:: install-cmxa
216uninstall-local:: uninstall-cmxa
217
Gordon Henriksena8c36602007-09-20 16:47:41 +0000218$(OutputCMXA): $(LibraryCMXA)
219 $(Verb) $(CP) -f $< $@
220 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
221
222$(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
223 $(Verb) $(CP) -f $< $@
224
Gordon Henriksen0908d492007-09-18 12:26:17 +0000225$(LibraryCMXA): $(ObjectsCMX)
226 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
227 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
228 $(Verb) $(RM) -f $(@:.cmxa=.o)
229
Gordon Henriksena8c36602007-09-20 16:47:41 +0000230$(ObjDir)/%.cmx: $(ObjDir)/%.ml
Gordon Henriksen0908d492007-09-18 12:26:17 +0000231 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
232 $(Verb) $(Compile.CMX) $@ $<
233
234clean-cmxa::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000235 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000236
Gordon Henriksena8c36602007-09-20 16:47:41 +0000237install-cmxa:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000238 $(Verb) $(MKDIR) $(PROJ_libocamldir)
239 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
Gordon Henriksena8c36602007-09-20 16:47:41 +0000240 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000241 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
Gordon Henriksena8c36602007-09-20 16:47:41 +0000242 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
243 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000244 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
245 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
246 done
247
Gordon Henriksena8c36602007-09-20 16:47:41 +0000248uninstall-cmxa::
Gordon Henriksen0908d492007-09-18 12:26:17 +0000249 $(Echo) "Uninstalling $(DestCMXA)"
250 $(Verb) $(RM) -f $(DestCMXA)
251 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
252 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000253 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000254 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
255 $(RM) -f $(PROJ_libocamldir)/$$i; \
256 done
257
258endif
259
260
261##===- Debugging gunk -----------------------------------------------------===##
262printvars:: printcamlvars
263
264printcamlvars::
265 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
266 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
267 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
268 $(Echo) "OCAMLC : " '$(OCAMLC)'
269 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000270 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000271 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
272 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
273 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
274 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
275 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
276 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
277 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
278 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000279 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000280 $(Echo) "OcamlSources : " '$(OcamlSources)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000281 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000282 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
283 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
284 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
285 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
286 $(Echo) "DestA : " '$(DestA)'
287 $(Echo) "DestCMA : " '$(DestCMA)'
288 $(Echo) "DestCMXA : " '$(DestCMXA)'
289 $(Echo) "UsedLibs : " '$(UsedLibs)'
290 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
291
292.PHONY: printcamlvars build-cmis \
293 clean-a clean-cmis clean-cma clean-cmxa \
294 install-a install-cmis install-cma install-cmxa \
295 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa