blob: 1f772a4301bb7dc389e189d4b32e4837dbfceabf [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
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 Henriksena8c36602007-09-20 16:47:41 +000041OCAMLCFLAGS += -I $(OcamlDir) -I $(ObjDir)
Gordon Henriksen0908d492007-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 Henriksena8c36602007-09-20 16:47:41 +000054OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
55OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
Gordon Henriksen0908d492007-09-18 12:26:17 +000056
Gordon Henriksena8c36602007-09-20 16:47:41 +000057OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
58OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
Gordon Henriksen0908d492007-09-18 12:26:17 +000059
Gordon Henriksena8c36602007-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 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
79
80
Gordon Henriksena8c36602007-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
91$(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders)
92 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
93
94-include $(ObjDir)/$(LIBRARYNAME).ocamldep
95
96
Gordon Henriksen0908d492007-09-18 12:26:17 +000097##===- Build static library from C sources --------------------------------===##
98
99all-local:: $(LibraryA)
100clean-local:: clean-a
101install-local:: install-a
102uninstall-local:: uninstall-a
103
104$(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
105 $(Echo) "Building $(BuildMode) $(notdir $@)"
106 -$(Verb) $(RM) -f $@
107 $(Verb) $(Archive) $@ $(ObjectsO)
108 $(Verb) $(Ranlib) $@
109
110clean-a::
111 -$(Verb) $(RM) -f $(LibraryA)
112
113install-a:: $(LibraryA)
114 $(Echo) "Installing $(BuildMode) $(DestA)"
115 $(Verb) $(MKDIR) $(PROJ_libocamldir)
116 $(Verb) $(LTInstall) $(LibraryA) $(DestA)
117 $(Verb)
118
119uninstall-a::
120 $(Echo) "Uninstalling $(DestA)"
121 -$(Verb) $(RM) -f $(DestA)
122
123
124##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
125
126all-local:: build-cmis
127clean-local:: clean-cmis
128install-local:: install-cmis
129uninstall-local:: uninstall-cmis
130
Gordon Henriksena8c36602007-09-20 16:47:41 +0000131build-cmis: $(OutputsCMI)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000132
Gordon Henriksena8c36602007-09-20 16:47:41 +0000133$(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
134 $(Verb) $(CP) -f $< $@
135
136$(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
Gordon Henriksen0908d492007-09-18 12:26:17 +0000137 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
138 $(Verb) $(Compile.CMI) $@ $<
139
140clean-cmis::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000141 -$(Verb) $(RM) -f $(OutputsCMI)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000142
143# Also install the .mli's (headers) as documentation.
Gordon Henriksena8c36602007-09-20 16:47:41 +0000144install-cmis: $(OutputsCMI) $(OcamlHeaders)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000145 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000146 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000147 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
148 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
149 done
Gordon Henriksena8c36602007-09-20 16:47:41 +0000150 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000151 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
Gordon Henriksena8c36602007-09-20 16:47:41 +0000152 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000153 done
154
155uninstall-cmis::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000156 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000157 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
158 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
159 done
Gordon Henriksena8c36602007-09-20 16:47:41 +0000160 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000161 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
162 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
163 done
164
165
166##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
167
Gordon Henriksena8c36602007-09-20 16:47:41 +0000168all-local:: $(OutputCMA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000169clean-local:: clean-cma
170install-local:: install-cma
171uninstall-local:: uninstall-cma
172
Gordon Henriksena8c36602007-09-20 16:47:41 +0000173$(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
174 $(Verb) $(CP) -f $< $@
175
Gordon Henriksen0908d492007-09-18 12:26:17 +0000176$(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
177 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
178 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
179 $(Verb) for i in $(UsedLibNames); do \
180 ln -sf "$(LibDir)/$$i" "$(OcamlDir)/$$i"; \
181 done
182
Gordon Henriksena8c36602007-09-20 16:47:41 +0000183$(ObjDir)/%.cmo: $(ObjDir)/%.ml
Gordon Henriksen0908d492007-09-18 12:26:17 +0000184 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
185 $(Verb) $(Compile.CMO) $@ $<
186
187clean-cma::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000188 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000189
Gordon Henriksena8c36602007-09-20 16:47:41 +0000190install-cma:: $(OutputCMA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000191 $(Echo) "Installing $(BuildMode) $(DestCMA)"
192 $(Verb) $(MKDIR) $(PROJ_libocamldir)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000193 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
Gordon Henriksen0908d492007-09-18 12:26:17 +0000194 $(Verb) for i in $(UsedLibNames); do \
195 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
196 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 \
203 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
204 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
205 done
206
207
208##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
209
210# The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
211# If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
212ifdef OCAMLOPT
213
Gordon Henriksena8c36602007-09-20 16:47:41 +0000214all-local:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000215clean-local:: clean-cmxa
216install-local:: install-cmxa
217uninstall-local:: uninstall-cmxa
218
Gordon Henriksena8c36602007-09-20 16:47:41 +0000219$(OutputCMXA): $(LibraryCMXA)
220 $(Verb) $(CP) -f $< $@
221 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
222
223$(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
224 $(Verb) $(CP) -f $< $@
225
Gordon Henriksen0908d492007-09-18 12:26:17 +0000226$(LibraryCMXA): $(ObjectsCMX)
227 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
228 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
229 $(Verb) $(RM) -f $(@:.cmxa=.o)
230
Gordon Henriksena8c36602007-09-20 16:47:41 +0000231$(ObjDir)/%.cmx: $(ObjDir)/%.ml
Gordon Henriksen0908d492007-09-18 12:26:17 +0000232 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
233 $(Verb) $(Compile.CMX) $@ $<
234
235clean-cmxa::
Gordon Henriksena8c36602007-09-20 16:47:41 +0000236 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000237
Gordon Henriksena8c36602007-09-20 16:47:41 +0000238install-cmxa:: $(OutputCMXA) $(OutputsCMX)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000239 $(Verb) $(MKDIR) $(PROJ_libocamldir)
240 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
Gordon Henriksena8c36602007-09-20 16:47:41 +0000241 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
Gordon Henriksen0908d492007-09-18 12:26:17 +0000242 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
Gordon Henriksena8c36602007-09-20 16:47:41 +0000243 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
244 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000245 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
246 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
247 done
248
Gordon Henriksena8c36602007-09-20 16:47:41 +0000249uninstall-cmxa::
Gordon Henriksen0908d492007-09-18 12:26:17 +0000250 $(Echo) "Uninstalling $(DestCMXA)"
251 $(Verb) $(RM) -f $(DestCMXA)
252 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
253 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
Gordon Henriksena8c36602007-09-20 16:47:41 +0000254 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
Gordon Henriksen0908d492007-09-18 12:26:17 +0000255 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
256 $(RM) -f $(PROJ_libocamldir)/$$i; \
257 done
258
259endif
260
261
262##===- Debugging gunk -----------------------------------------------------===##
263printvars:: printcamlvars
264
265printcamlvars::
266 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
267 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
268 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
269 $(Echo) "OCAMLC : " '$(OCAMLC)'
270 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000271 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000272 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
273 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
274 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
275 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
276 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
277 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
278 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
279 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000280 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000281 $(Echo) "OcamlSources : " '$(OcamlSources)'
Gordon Henriksena8c36602007-09-20 16:47:41 +0000282 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
Gordon Henriksen0908d492007-09-18 12:26:17 +0000283 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
284 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
285 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
286 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
287 $(Echo) "DestA : " '$(DestA)'
288 $(Echo) "DestCMA : " '$(DestCMA)'
289 $(Echo) "DestCMXA : " '$(DestCMXA)'
290 $(Echo) "UsedLibs : " '$(UsedLibs)'
291 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
292
293.PHONY: printcamlvars build-cmis \
294 clean-a clean-cmis clean-cma clean-cmxa \
295 install-a install-cmis install-cma install-cmxa \
296 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa