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 ifneq ($(ENABLE_OPTIMIZED),1)
49 Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
50 Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
51 Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
54 Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
55 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
58 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
59 OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
61 OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
62 OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
65 LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
66 LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
67 ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
68 ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
69 ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
72 # The .cmo files are the only intermediates; all others are to be installed.
73 LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
74 OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
75 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
76 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
77 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
78 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
80 # Installation targets
81 DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
82 DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
83 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
84 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
87 ##===- Dependencies -------------------------------------------------------===##
88 # Copy the sources into the intermediate directory because older ocamlc doesn't
89 # support -o except when linking (outputs are placed next to inputs).
91 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
92 $(Verb) $(CP) -f $< $@
94 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
95 $(Verb) $(CP) -f $< $@
97 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
98 $(OcamlDir)/.dir $(ObjDir)/.dir
99 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
101 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
103 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
106 ##===- Build static library from C sources --------------------------------===##
108 all-local:: $(LibraryA)
109 clean-local:: clean-a
110 install-local:: install-a
111 uninstall-local:: uninstall-a
113 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
114 $(Echo) "Building $(BuildMode) $(notdir $@)"
116 $(Verb) $(Archive) $@ $(ObjectsO)
120 -$(Verb) $(RM) -f $(LibraryA)
122 install-a:: $(LibraryA)
123 $(Echo) "Installing $(BuildMode) $(DestA)"
124 $(Verb) $(MKDIR) $(PROJ_libocamldir)
125 $(Verb) $(LTInstall) $(LibraryA) $(DestA)
129 $(Echo) "Uninstalling $(DestA)"
130 -$(Verb) $(RM) -f $(DestA)
133 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
135 all-local:: build-deplibs
136 clean-local:: clean-deplibs
137 install-local:: install-deplibs
138 uninstall-local:: uninstall-deplibs
140 build-deplibs: $(OutputLibs)
142 $(OcamlDir)/%.a: $(LibDir)/%.a
145 $(OcamlDir)/%.o: $(LibDir)/%.o
149 $(Verb) rm -f $(OutputLibs)
152 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
153 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
157 $(Verb) rm -f $(DestLibs)
160 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
162 all-local:: build-cmis
163 clean-local:: clean-cmis
164 install-local:: install-cmis
165 uninstall-local:: uninstall-cmis
167 build-cmis: $(OutputsCMI)
169 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
170 $(Verb) $(CP) -f $< $@
172 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
173 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
174 $(Verb) $(Compile.CMI) $@ $<
177 -$(Verb) $(RM) -f $(OutputsCMI)
179 # Also install the .mli's (headers) as documentation.
180 install-cmis: $(OutputsCMI) $(OcamlHeaders)
181 $(Verb) $(MKDIR) $(PROJ_libocamldir)
182 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
183 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
184 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
186 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
187 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
188 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
192 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
193 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
194 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
196 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
197 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
198 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
202 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
204 all-local:: $(OutputCMA)
205 clean-local:: clean-cma
206 install-local:: install-cma
207 uninstall-local:: uninstall-cma
209 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
210 $(Verb) $(CP) -f $< $@
212 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
213 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
214 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
216 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
217 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
218 $(Verb) $(Compile.CMO) $@ $<
221 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
223 install-cma:: $(OutputCMA)
224 $(Echo) "Installing $(BuildMode) $(DestCMA)"
225 $(Verb) $(MKDIR) $(PROJ_libocamldir)
226 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
229 $(Echo) "Uninstalling $(DestCMA)"
230 -$(Verb) $(RM) -f $(DestCMA)
233 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
235 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
236 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
239 all-local:: $(OutputCMXA) $(OutputsCMX)
240 clean-local:: clean-cmxa
241 install-local:: install-cmxa
242 uninstall-local:: uninstall-cmxa
244 $(OutputCMXA): $(LibraryCMXA)
245 $(Verb) $(CP) -f $< $@
246 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
248 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
249 $(Verb) $(CP) -f $< $@
251 $(LibraryCMXA): $(ObjectsCMX)
252 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
253 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
254 $(Verb) $(RM) -f $(@:.cmxa=.o)
256 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
257 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
258 $(Verb) $(Compile.CMX) $@ $<
261 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
263 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
264 $(Verb) $(MKDIR) $(PROJ_libocamldir)
265 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
266 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
267 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
268 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
269 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
270 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
271 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
275 $(Echo) "Uninstalling $(DestCMXA)"
276 $(Verb) $(RM) -f $(DestCMXA)
277 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
278 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
279 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
280 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
281 $(RM) -f $(PROJ_libocamldir)/$$i; \
287 ##===- Debugging gunk -----------------------------------------------------===##
288 printvars:: printcamlvars
291 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
292 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
293 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
294 $(Echo) "OCAMLC : " '$(OCAMLC)'
295 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
296 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
297 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
298 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
299 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
300 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
301 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
302 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
303 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
304 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
305 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
306 $(Echo) "OcamlSources : " '$(OcamlSources)'
307 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
308 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
309 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
310 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
311 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
312 $(Echo) "DestA : " '$(DestA)'
313 $(Echo) "DestCMA : " '$(DestCMA)'
314 $(Echo) "DestCMXA : " '$(DestCMXA)'
315 $(Echo) "UsedLibs : " '$(UsedLibs)'
316 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
318 .PHONY: printcamlvars build-cmis \
319 clean-a clean-cmis clean-cma clean-cmxa \
320 install-a install-cmis install-cma install-cmxa \
321 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa