1 ##===- tools/ml/Makefile -----------------------------------*- Makefile -*-===##
3 # The LLVM Compiler Infrastructure
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.
8 ##===----------------------------------------------------------------------===##
10 # An ocaml library is a unique project type in the context of LLVM, so rules are
11 # here rather than in Makefile.rules.
13 # Reference materials on installing ocaml libraries:
15 # https://fedoraproject.org/wiki/Packaging/OCaml
16 # http://pkg-ocaml-maint.alioth.debian.org/ocaml_packaging_policy.txt
18 ##===----------------------------------------------------------------------===##
20 include $(LEVEL)/Makefile.config
22 # CFLAGS needs to be set before Makefile.rules is included.
23 CFLAGS += -I$(shell $(OCAMLC) -where)
25 include $(LEVEL)/Makefile.common
27 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
28 # user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
29 PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
30 OcamlDir := $(LibDir)/ocaml
32 # Info from llvm-config and similar
34 UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
35 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
39 OCAMLCFLAGS += -I $(OcamlDir) -I $(ObjDir)
40 OCAMLAFLAGS += $(patsubst %,-cclib %, \
41 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
42 $(shell $(LLVM_CONFIG) --ldflags)) \
45 Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
46 Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) -o)
47 Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) -o)
49 Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) -o)
50 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) -o)
53 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
54 OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
56 OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
57 OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
60 LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
61 LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
62 ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
63 ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
64 ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
67 # The .cmo files are the only intermediates; all others are to be installed.
68 LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
69 OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
70 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
71 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
72 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
73 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
75 # Installation targets
76 DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
77 DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
78 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
79 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
82 ##===- 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).
86 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
87 $(Verb) $(CP) -f $< $@
89 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
90 $(Verb) $(CP) -f $< $@
92 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
93 $(OcamlDir)/.dir $(ObjDir)/.dir
94 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
96 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
98 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
101 ##===- Build static library from C sources --------------------------------===##
103 all-local:: $(LibraryA)
104 clean-local:: clean-a
105 install-local:: install-a
106 uninstall-local:: uninstall-a
108 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
109 $(Echo) "Building $(BuildMode) $(notdir $@)"
111 $(Verb) $(Archive) $@ $(ObjectsO)
115 -$(Verb) $(RM) -f $(LibraryA)
117 install-a:: $(LibraryA)
118 $(Echo) "Installing $(BuildMode) $(DestA)"
119 $(Verb) $(MKDIR) $(PROJ_libocamldir)
120 $(Verb) $(LTInstall) $(LibraryA) $(DestA)
124 $(Echo) "Uninstalling $(DestA)"
125 -$(Verb) $(RM) -f $(DestA)
128 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
130 all-local:: build-deplibs
131 clean-local:: clean-deplibs
132 install-local:: install-deplibs
133 uninstall-local:: uninstall-deplibs
135 build-deplibs: $(OutputLibs)
137 $(OcamlDir)/%.a: $(LibDir)/%.a
141 $(Verb) rm -f $(OutputLibs)
144 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
145 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
149 $(Verb) rm -f $(DestLibs)
152 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
154 all-local:: build-cmis
155 clean-local:: clean-cmis
156 install-local:: install-cmis
157 uninstall-local:: uninstall-cmis
159 build-cmis: $(OutputsCMI)
161 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
162 $(Verb) $(CP) -f $< $@
164 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
165 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
166 $(Verb) $(Compile.CMI) $@ $<
169 -$(Verb) $(RM) -f $(OutputsCMI)
171 # Also install the .mli's (headers) as documentation.
172 install-cmis: $(OutputsCMI) $(OcamlHeaders)
173 $(Verb) $(MKDIR) $(PROJ_libocamldir)
174 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
175 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
176 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
178 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
179 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
180 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
184 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
185 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
186 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
188 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
189 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
190 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
194 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
196 all-local:: $(OutputCMA)
197 clean-local:: clean-cma
198 install-local:: install-cma
199 uninstall-local:: uninstall-cma
201 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
202 $(Verb) $(CP) -f $< $@
204 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
205 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
206 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
208 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
209 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
210 $(Verb) $(Compile.CMO) $@ $<
213 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
215 install-cma:: $(OutputCMA)
216 $(Echo) "Installing $(BuildMode) $(DestCMA)"
217 $(Verb) $(MKDIR) $(PROJ_libocamldir)
218 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
221 $(Echo) "Uninstalling $(DestCMA)"
222 -$(Verb) $(RM) -f $(DestCMA)
225 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
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.
231 all-local:: $(OutputCMXA) $(OutputsCMX)
232 clean-local:: clean-cmxa
233 install-local:: install-cmxa
234 uninstall-local:: uninstall-cmxa
236 $(OutputCMXA): $(LibraryCMXA)
237 $(Verb) $(CP) -f $< $@
238 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
240 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
241 $(Verb) $(CP) -f $< $@
243 $(LibraryCMXA): $(ObjectsCMX)
244 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
245 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
246 $(Verb) $(RM) -f $(@:.cmxa=.o)
248 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
249 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
250 $(Verb) $(Compile.CMX) $@ $<
253 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
255 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
256 $(Verb) $(MKDIR) $(PROJ_libocamldir)
257 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
258 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
259 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
260 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
261 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
262 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
263 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
267 $(Echo) "Uninstalling $(DestCMXA)"
268 $(Verb) $(RM) -f $(DestCMXA)
269 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
270 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
271 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
272 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
273 $(RM) -f $(PROJ_libocamldir)/$$i; \
279 ##===- Debugging gunk -----------------------------------------------------===##
280 printvars:: printcamlvars
283 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
284 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
285 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
286 $(Echo) "OCAMLC : " '$(OCAMLC)'
287 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
288 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
289 $(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)'
297 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
298 $(Echo) "OcamlSources : " '$(OcamlSources)'
299 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
300 $(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)'
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