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
35 UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
36 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
40 OCAMLCFLAGS += -I $(OcamlDir) -I $(ObjDir)
41 OCAMLAFLAGS += $(patsubst %,-cclib %, \
42 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
43 $(shell $(LLVM_CONFIG) --ldflags)) \
46 # -g was introduced in 3.10.0.
47 #ifneq ($(ENABLE_OPTIMIZED),1)
48 # OCAMLDEBUGFLAG := -g
51 Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
52 Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
53 Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
56 Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
57 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
60 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
61 OcamlHeaders1 := $(OcamlSources1:.ml=.mli)
63 OcamlSources := $(OcamlSources1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
64 OcamlHeaders := $(OcamlHeaders1:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
67 LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
68 LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
69 ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
70 ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
71 ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
74 # The .cmo files are the only intermediates; all others are to be installed.
75 LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
76 OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
77 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
78 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
79 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
80 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
82 # Installation targets
83 DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
84 DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
85 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
86 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
89 ##===- Dependencies -------------------------------------------------------===##
90 # Copy the sources into the intermediate directory because older ocamlc doesn't
91 # support -o except when linking (outputs are placed next to inputs).
93 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
94 $(Verb) $(CP) -f $< $@
96 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
97 $(Verb) $(CP) -f $< $@
99 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
100 $(OcamlDir)/.dir $(ObjDir)/.dir
101 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeader) > $@
103 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
105 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
108 ##===- Build static library from C sources --------------------------------===##
110 all-local:: $(LibraryA)
111 clean-local:: clean-a
112 install-local:: install-a
113 uninstall-local:: uninstall-a
115 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
116 $(Echo) "Building $(BuildMode) $(notdir $@)"
118 $(Verb) $(Archive) $@ $(ObjectsO)
122 -$(Verb) $(RM) -f $(LibraryA)
124 install-a:: $(LibraryA)
125 $(Echo) "Installing $(BuildMode) $(DestA)"
126 $(Verb) $(MKDIR) $(PROJ_libocamldir)
127 $(Verb) $(LTInstall) $(LibraryA) $(DestA)
131 $(Echo) "Uninstalling $(DestA)"
132 -$(Verb) $(RM) -f $(DestA)
135 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
137 all-local:: build-deplibs
138 clean-local:: clean-deplibs
139 install-local:: install-deplibs
140 uninstall-local:: uninstall-deplibs
142 build-deplibs: $(OutputLibs)
144 $(OcamlDir)/%.a: $(LibDir)/%.a
147 $(OcamlDir)/%.o: $(LibDir)/%.o
151 $(Verb) rm -f $(OutputLibs)
154 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
155 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
159 $(Verb) rm -f $(DestLibs)
162 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
164 all-local:: build-cmis
165 clean-local:: clean-cmis
166 install-local:: install-cmis
167 uninstall-local:: uninstall-cmis
169 build-cmis: $(OutputsCMI)
171 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
172 $(Verb) $(CP) -f $< $@
174 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
175 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
176 $(Verb) $(Compile.CMI) $@ $<
179 -$(Verb) $(RM) -f $(OutputsCMI)
181 # Also install the .mli's (headers) as documentation.
182 install-cmis: $(OutputsCMI) $(OcamlHeaders)
183 $(Verb) $(MKDIR) $(PROJ_libocamldir)
184 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
185 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
186 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
188 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
189 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
190 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
194 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
195 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
196 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
198 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
199 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
200 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
204 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
206 all-local:: $(OutputCMA)
207 clean-local:: clean-cma
208 install-local:: install-cma
209 uninstall-local:: uninstall-cma
211 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
212 $(Verb) $(CP) -f $< $@
214 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
215 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
216 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
218 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
219 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
220 $(Verb) $(Compile.CMO) $@ $<
223 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
225 install-cma:: $(OutputCMA)
226 $(Echo) "Installing $(BuildMode) $(DestCMA)"
227 $(Verb) $(MKDIR) $(PROJ_libocamldir)
228 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
231 $(Echo) "Uninstalling $(DestCMA)"
232 -$(Verb) $(RM) -f $(DestCMA)
235 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
237 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
238 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
241 all-local:: $(OutputCMXA) $(OutputsCMX)
242 clean-local:: clean-cmxa
243 install-local:: install-cmxa
244 uninstall-local:: uninstall-cmxa
246 $(OutputCMXA): $(LibraryCMXA)
247 $(Verb) $(CP) -f $< $@
248 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
250 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
251 $(Verb) $(CP) -f $< $@
253 $(LibraryCMXA): $(ObjectsCMX)
254 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
255 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
256 $(Verb) $(RM) -f $(@:.cmxa=.o)
258 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
259 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
260 $(Verb) $(Compile.CMX) $@ $<
263 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.o) $(OutputsCMX)
265 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
266 $(Verb) $(MKDIR) $(PROJ_libocamldir)
267 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
268 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
269 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
270 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
271 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
272 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
273 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
277 $(Echo) "Uninstalling $(DestCMXA)"
278 $(Verb) $(RM) -f $(DestCMXA)
279 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
280 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
281 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
282 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
283 $(RM) -f $(PROJ_libocamldir)/$$i; \
289 ##===- Debugging gunk -----------------------------------------------------===##
290 printvars:: printcamlvars
293 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
294 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
295 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
296 $(Echo) "OCAMLC : " '$(OCAMLC)'
297 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
298 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
299 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
300 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
301 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
302 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
303 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
304 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
305 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
306 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
307 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
308 $(Echo) "OcamlSources : " '$(OcamlSources)'
309 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
310 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
311 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
312 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
313 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
314 $(Echo) "DestA : " '$(DestA)'
315 $(Echo) "DestCMA : " '$(DestCMA)'
316 $(Echo) "DestCMXA : " '$(DestCMXA)'
317 $(Echo) "UsedLibs : " '$(UsedLibs)'
318 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
320 .PHONY: printcamlvars build-cmis \
321 clean-a clean-cmis clean-cma clean-cmxa \
322 install-a install-cmis install-cma install-cmxa \
323 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa