1 ##===- tools/ml/Makefile -----------------------------------*- Makefile -*-===##
3 # The LLVM Compiler Infrastructure
5 # This file is distributed under the University of Illinois Open Source
6 # 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 CXX.Flags += -I"$(shell $(OCAMLC) -where)"
24 C.Flags += -I"$(shell $(OCAMLC) -where)"
26 include $(LEVEL)/Makefile.common
28 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
29 # user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
30 PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
31 OcamlDir := $(LibDir)/ocaml
33 # Info from llvm-config and similar
34 ifndef IS_CLEANING_TARGET
36 UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
37 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
42 OCAMLCFLAGS += -I $(ObjDir) -I $(OcamlDir)
43 ifndef IS_CLEANING_TARGET
45 OCAMLAFLAGS += $(patsubst %,-cclib %, \
46 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
47 $(shell $(LLVM_CONFIG) --ldflags)) \
50 OCAMLAFLAGS += $(patsubst %,-cclib %, \
51 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
56 # -g was introduced in 3.10.0.
57 #ifneq ($(ENABLE_OPTIMIZED),1)
58 # OCAMLDEBUGFLAG := -g
61 Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
62 Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
63 Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
66 Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
67 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
70 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
71 OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
73 OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
74 OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
77 LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
78 LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
79 ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
80 ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
81 ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
84 # The .cmo files are the only intermediates; all others are to be installed.
85 LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
86 OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
87 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
88 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
89 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
90 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
92 # Installation targets
93 DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
94 DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
95 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
96 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
99 ##===- Dependencies -------------------------------------------------------===##
100 # Copy the sources into the intermediate directory because older ocamlc doesn't
101 # support -o except when linking (outputs are placed next to inputs).
103 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
104 $(Verb) $(CP) -f $< $@
106 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
107 $(Verb) $(CP) -f $< $@
109 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
110 $(OcamlDir)/.dir $(ObjDir)/.dir
111 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
113 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
115 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
118 ##===- Build static library from C sources --------------------------------===##
121 all-local:: $(LibraryA)
122 clean-local:: clean-a
123 install-local:: install-a
124 uninstall-local:: uninstall-a
126 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
127 $(Echo) "Building $(BuildMode) $(notdir $@)"
129 $(Verb) $(Archive) $@ $(ObjectsO)
133 -$(Verb) $(RM) -f $(LibraryA)
135 install-a:: $(LibraryA)
136 $(Echo) "Installing $(BuildMode) $(DestA)"
137 $(Verb) $(MKDIR) $(PROJ_libocamldir)
138 $(Verb) $(INSTALL) $(LibraryA) $(DestA)
142 $(Echo) "Uninstalling $(DestA)"
143 -$(Verb) $(RM) -f $(DestA)
147 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
149 all-local:: build-deplibs
150 clean-local:: clean-deplibs
151 install-local:: install-deplibs
152 uninstall-local:: uninstall-deplibs
154 build-deplibs: $(OutputLibs)
156 $(OcamlDir)/%.a: $(LibDir)/%.a
159 $(OcamlDir)/%.o: $(LibDir)/%.o
163 $(Verb) rm -f $(OutputLibs)
166 $(Verb) $(MKDIR) $(PROJ_libocamldir)
167 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
168 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
172 $(Verb) rm -f $(DestLibs)
175 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
177 all-local:: build-cmis
178 clean-local:: clean-cmis
179 install-local:: install-cmis
180 uninstall-local:: uninstall-cmis
182 build-cmis: $(OutputsCMI)
184 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
185 $(Verb) $(CP) -f $< $@
187 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
188 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
189 $(Verb) $(Compile.CMI) $@ $<
192 -$(Verb) $(RM) -f $(OutputsCMI)
194 # Also install the .mli's (headers) as documentation.
195 install-cmis: $(OutputsCMI) $(OcamlHeaders)
196 $(Verb) $(MKDIR) $(PROJ_libocamldir)
197 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
198 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
199 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
201 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
202 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
203 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
207 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
208 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
209 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
211 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
212 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
213 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
217 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
219 all-local:: $(OutputCMA)
220 clean-local:: clean-cma
221 install-local:: install-cma
222 uninstall-local:: uninstall-cma
224 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
225 $(Verb) $(CP) -f $< $@
227 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
228 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
229 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
231 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
232 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
233 $(Verb) $(Compile.CMO) $@ $<
236 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
238 install-cma:: $(OutputCMA)
239 $(Echo) "Installing $(BuildMode) $(DestCMA)"
240 $(Verb) $(MKDIR) $(PROJ_libocamldir)
241 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
244 $(Echo) "Uninstalling $(DestCMA)"
245 -$(Verb) $(RM) -f $(DestCMA)
248 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
250 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
251 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
254 all-local:: $(OutputCMXA) $(OutputsCMX)
255 clean-local:: clean-cmxa
256 install-local:: install-cmxa
257 uninstall-local:: uninstall-cmxa
259 $(OutputCMXA): $(LibraryCMXA)
260 $(Verb) $(CP) -f $< $@
261 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
263 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
264 $(Verb) $(CP) -f $< $@
266 $(LibraryCMXA): $(ObjectsCMX)
267 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
268 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
269 $(Verb) $(RM) -f $(@:.cmxa=.o)
271 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
272 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
273 $(Verb) $(Compile.CMX) $@ $<
276 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
278 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
279 $(Verb) $(MKDIR) $(PROJ_libocamldir)
280 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
281 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
282 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
283 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
284 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
285 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
286 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
290 $(Echo) "Uninstalling $(DestCMXA)"
291 $(Verb) $(RM) -f $(DestCMXA)
292 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
293 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
294 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
295 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
296 $(RM) -f $(PROJ_libocamldir)/$$i; \
301 ##===- Generate documentation ---------------------------------------------===##
303 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
304 $(Echo) "Documenting $(notdir $@)"
305 $(Verb) $(OCAMLDOC) -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
307 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
309 ##===- Debugging gunk -----------------------------------------------------===##
310 printvars:: printcamlvars
313 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
314 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
315 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
316 $(Echo) "OCAMLC : " '$(OCAMLC)'
317 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
318 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
319 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
320 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
321 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
322 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
323 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
324 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
325 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
326 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
327 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
328 $(Echo) "OcamlSources : " '$(OcamlSources)'
329 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
330 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
331 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
332 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
333 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
334 $(Echo) "DestA : " '$(DestA)'
335 $(Echo) "DestCMA : " '$(DestCMA)'
336 $(Echo) "DestCMXA : " '$(DestCMXA)'
337 $(Echo) "UsedLibs : " '$(UsedLibs)'
338 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
340 .PHONY: printcamlvars build-cmis \
341 clean-a clean-cmis clean-cma clean-cmxa \
342 install-a install-cmis install-cma install-cmxa \
343 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa