blob: 278b31ea3223f7d17eee562b99c15948cc9ad330 [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#
13##===----------------------------------------------------------------------===##
14
15include $(LEVEL)/Makefile.config
16
Gordon Henriksen3b802472007-10-04 00:07:50 +000017# CFLAGS needs to be set before Makefile.rules is included.
18CFLAGS += -I$(shell $(OCAMLC) -where)
Gordon Henriksen0908d492007-09-18 12:26:17 +000019
20include $(LEVEL)/Makefile.common
21
22# Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
Gordon Henriksen1d4e0782007-10-02 16:42:10 +000023# user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
Gordon Henriksen0908d492007-09-18 12:26:17 +000024PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
25OcamlDir := $(LibDir)/ocaml
26
27# Info from llvm-config and similar
28ifdef UsedComponents
29UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
30UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
31endif
32
33# Tools
Gordon Henriksena8c36602007-09-20 16:47:41 +000034OCAMLCFLAGS += -I $(OcamlDir) -I $(ObjDir)
Gordon Henriksen0908d492007-09-18 12:26:17 +000035OCAMLAFLAGS += $(patsubst %,-cclib %, \
36 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
37 $(UsedLibs) -l$(LIBRARYNAME))
38
39Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
40Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
41Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) -o)
42
43Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) -o)
44Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) -o)
45
46# Source files
Gordon Henriksena8c36602007-09-20 16:47:41 +000047OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
48OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
Gordon Henriksen0908d492007-09-18 12:26:17 +000049
Gordon Henriksena8c36602007-09-20 16:47:41 +000050OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
51OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
Gordon Henriksen0908d492007-09-18 12:26:17 +000052
Gordon Henriksena8c36602007-09-20 16:47:41 +000053# Intermediate files
54LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
55LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
56ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
57ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
58ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
59
60# Output files
61# The .cmo files are the only intermediates; all others are to be installed.
62LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
63OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
64OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
65OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
66OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
Gordon Henriksen0908d492007-09-18 12:26:17 +000067
68# Installation targets
69DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
70DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
71DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
72
73
Gordon Henriksena8c36602007-09-20 16:47:41 +000074##===- Dependencies -------------------------------------------------------===##
75# Copy the sources into the intermediate directory because older ocamlc doesn't
76# support -o except when linking (outputs are placed next to inputs).
77
78$(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
79 $(Verb) $(CP) -f $< $@
80
81$(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
82 $(Verb) $(CP) -f $< $@
83
Gordon Henriksenab4456f2007-09-23 13:37:44 +000084$(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
85 $(OcamlDir)/.dir $(ObjDir)/.dir
Gordon Henriksena8c36602007-09-20 16:47:41 +000086 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
87
Gordon Henriksen46abf912007-09-26 20:56:12 +000088$(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
89
Gordon Henriksena8c36602007-09-20 16:47:41 +000090-include $(ObjDir)/$(LIBRARYNAME).ocamldep
91
92
Gordon Henriksen0908d492007-09-18 12:26:17 +000093##===- Build static library from C sources --------------------------------===##
94
95all-local:: $(LibraryA)
96clean-local:: clean-a
97install-local:: install-a
98uninstall-local:: uninstall-a
99
100$(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
101 $(Echo) "Building $(BuildMode) $(notdir $@)"
102 -$(Verb) $(RM) -f $@
103 $(Verb) $(Archive) $@ $(ObjectsO)
104 $(Verb) $(Ranlib) $@
105
106clean-a::
107 -$(Verb) $(RM) -f $(LibraryA)
108
109install-a:: $(LibraryA)
110 $(Echo) "Installing $(BuildMode) $(DestA)"
111 $(Verb) $(MKDIR) $(PROJ_libocamldir)
112 $(Verb) $(LTInstall) $(LibraryA) $(DestA)
113 $(Verb)
114
115uninstall-a::
116 $(Echo) "Uninstalling $(DestA)"
117 -$(Verb) $(RM) -f $(DestA)
118
119
120##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
121
122all-local:: build-cmis
123clean-local:: clean-cmis
124install-local:: install-cmis
125uninstall-local:: uninstall-cmis
126
Gordon Henriksena8c36602007-09-20 16:47:41 +0000127build-cmis: $(OutputsCMI)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000128
Gordon Henriksena8c36602007-09-20 16:47:41 +0000129$(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
130 $(Verb) $(CP) -f $< $@
131
132$(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
Gordon Henriksen0908d492007-09-18 12:26:17 +0000133 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
134 $(Verb) $(Compile.CMI) $@ $<
135
136clean-cmis::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000137 -$(Verb) $(RM) -f $(OutputsCMI)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000138
139# Also install the .mli's (headers) as documentation.
Gordon Henriksena8c36602007-09-20 16:47:41 +0000140install-cmis: $(OutputsCMI) $(OcamlHeaders)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000141 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000142 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000143 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
144 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
145 done
Gordon Henriksena8c36602007-09-20 16:47:41 +0000146 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000147 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
Gordon Henriksena8c36602007-09-20 16:47:41 +0000148 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000149 done
150
151uninstall-cmis::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000152 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000153 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
154 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
155 done
Gordon Henriksena8c36602007-09-20 16:47:41 +0000156 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000157 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
158 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
159 done
160
161
162##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
163
Gordon Henriksena8c36602007-09-20 16:47:41 +0000164all-local:: $(OutputCMA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000165clean-local:: clean-cma
166install-local:: install-cma
167uninstall-local:: uninstall-cma
168
Gordon Henriksena8c36602007-09-20 16:47:41 +0000169$(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
170 $(Verb) $(CP) -f $< $@
171
Gordon Henriksen0908d492007-09-18 12:26:17 +0000172$(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
173 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
174 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
175 $(Verb) for i in $(UsedLibNames); do \
176 ln -sf "$(LibDir)/$$i" "$(OcamlDir)/$$i"; \
177 done
178
Gordon Henriksena8c36602007-09-20 16:47:41 +0000179$(ObjDir)/%.cmo: $(ObjDir)/%.ml
Gordon Henriksen0908d492007-09-18 12:26:17 +0000180 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
181 $(Verb) $(Compile.CMO) $@ $<
182
183clean-cma::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000184 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000185
Gordon Henriksena8c36602007-09-20 16:47:41 +0000186install-cma:: $(OutputCMA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000187 $(Echo) "Installing $(BuildMode) $(DestCMA)"
188 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000189 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
Gordon Henriksen0908d492007-09-18 12:26:17 +0000190 $(Verb) for i in $(UsedLibNames); do \
191 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
192 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
193 done
194
195uninstall-cma::
196 $(Echo) "Uninstalling $(DestCMA)"
197 -$(Verb) $(RM) -f $(DestCMA)
198 $(Verb) for i in $(UsedLibNames); do \
199 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
200 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
201 done
202
203
204##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
205
206# The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
207# If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
208ifdef OCAMLOPT
209
Gordon Henriksena8c36602007-09-20 16:47:41 +0000210all-local:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000211clean-local:: clean-cmxa
212install-local:: install-cmxa
213uninstall-local:: uninstall-cmxa
214
Gordon Henriksena8c36602007-09-20 16:47:41 +0000215$(OutputCMXA): $(LibraryCMXA)
216 $(Verb) $(CP) -f $< $@
217 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
218
219$(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
220 $(Verb) $(CP) -f $< $@
221
Gordon Henriksen0908d492007-09-18 12:26:17 +0000222$(LibraryCMXA): $(ObjectsCMX)
223 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
224 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
225 $(Verb) $(RM) -f $(@:.cmxa=.o)
226
Gordon Henriksena8c36602007-09-20 16:47:41 +0000227$(ObjDir)/%.cmx: $(ObjDir)/%.ml
Gordon Henriksen0908d492007-09-18 12:26:17 +0000228 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
229 $(Verb) $(Compile.CMX) $@ $<
230
231clean-cmxa::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000232 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000233
Gordon Henriksena8c36602007-09-20 16:47:41 +0000234install-cmxa:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000235 $(Verb) $(MKDIR) $(PROJ_libocamldir)
236 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
Gordon Henriksena8c36602007-09-20 16:47:41 +0000237 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000238 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
Gordon Henriksena8c36602007-09-20 16:47:41 +0000239 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
240 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000241 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
242 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
243 done
244
Gordon Henriksena8c36602007-09-20 16:47:41 +0000245uninstall-cmxa::
Gordon Henriksen0908d492007-09-18 12:26:17 +0000246 $(Echo) "Uninstalling $(DestCMXA)"
247 $(Verb) $(RM) -f $(DestCMXA)
248 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
249 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000250 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000251 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
252 $(RM) -f $(PROJ_libocamldir)/$$i; \
253 done
254
255endif
256
257
258##===- Debugging gunk -----------------------------------------------------===##
259printvars:: printcamlvars
260
261printcamlvars::
262 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
263 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
264 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
265 $(Echo) "OCAMLC : " '$(OCAMLC)'
266 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000267 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000268 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
269 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
270 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
271 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
272 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
273 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
274 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
275 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000276 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000277 $(Echo) "OcamlSources : " '$(OcamlSources)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000278 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000279 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
280 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
281 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
282 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
283 $(Echo) "DestA : " '$(DestA)'
284 $(Echo) "DestCMA : " '$(DestCMA)'
285 $(Echo) "DestCMXA : " '$(DestCMXA)'
286 $(Echo) "UsedLibs : " '$(UsedLibs)'
287 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
288
289.PHONY: printcamlvars build-cmis \
290 clean-a clean-cmis clean-cma clean-cmxa \
291 install-a install-cmis install-cma install-cmxa \
292 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa