1 ##===- bindings/ocaml/Makefile.ocaml -----------------------*- 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 ifeq ($(ENABLE_SHARED),1)
27 LINK_COMPONENTS := all
30 include $(LEVEL)/Makefile.common
32 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
33 # user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
34 PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
35 OcamlDir := $(LibDir)/ocaml
37 # Info from llvm-config and similar
38 ifndef IS_CLEANING_TARGET
40 UsedLibs = $(shell $(LLVM_CONFIG) --libs $(UsedComponents))
41 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
45 # How do we link OCaml executables with LLVM?
46 # 1) If this is a --enable-shared build, build stub libraries. This also allows
47 # to use LLVM from toplevels.
48 # 2) If this is a --disable-shared build, embed ocamlc options for building
49 # a custom runtime and a static executable. It is not possible to use LLVM
52 ifeq ($(ENABLE_SHARED),1)
58 OCAMLCFLAGS += -I $(ObjDir) -I $(OcamlDir)
59 ifndef IS_CLEANING_TARGET
61 OCAMLAFLAGS += $(patsubst %,-cclib %, \
62 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
63 $(shell $(LLVM_CONFIG) --ldflags)) \
66 OCAMLAFLAGS += $(patsubst %,-cclib %, \
67 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
72 # -g was introduced in 3.10.0.
73 #ifneq ($(ENABLE_OPTIMIZED),1)
74 # OCAMLDEBUGFLAG := -g
77 Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
78 Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
79 Compile.CMX := $(strip $(OCAMLOPT) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
82 Archive.CMA := $(strip $(OCAMLC) -a -dllib -l$(LIBRARYNAME) $(OCAMLDEBUGFLAG) \
85 Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
90 Archive.CMXA := $(strip $(OCAMLOPT) -a $(patsubst %,-cclib %, \
91 $(LLVMLibsOptions) -l$(LIBRARYNAME)) \
92 -cclib -L$(SharedLibDir) \
95 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
99 Archive.EXE := $(strip $(OCAMLOPT) -cc $(CXX) $(OCAMLCFLAGS) $(UsedOcamlLibs:%=%.cmxa) $(OCAMLDEBUGFLAG) -o)
101 Archive.EXE := $(strip $(OCAMLC) -cc $(CXX) $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG:%=%.cma) -o)
106 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
110 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
113 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
114 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
116 OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
117 OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
120 ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
121 ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
122 ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
125 LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
126 LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
130 ToolEXE := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
134 # The .cmo files are the only intermediates; all others are to be installed.
135 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
136 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
137 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
140 LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
141 OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
142 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
146 SharedLib := $(OcamlDir)/dll$(LIBRARYNAME)$(SHLIBEXT)
151 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
153 OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
157 # Installation targets
158 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
161 DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
162 DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
163 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
167 DestSharedLib := $(PROJ_libocamldir)/dll$(LIBRARYNAME)$(SHLIBEXT)
170 ##===- Dependencies -------------------------------------------------------===##
171 # Copy the sources into the intermediate directory because older ocamlc doesn't
172 # support -o except when linking (outputs are placed next to inputs).
174 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
175 $(Verb) $(CP) -f $< $@
177 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
178 $(Verb) $(CP) -f $< $@
180 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
183 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
184 $(OcamlDir)/.dir $(ObjDir)/.dir
185 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
187 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
191 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
192 $(OcamlDir)/.dir $(ObjDir)/.dir
193 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
195 -include $(ObjDir)/$(TOOLNAME).ocamldep
198 ##===- Build static library from C sources --------------------------------===##
201 all-local:: $(LibraryA)
202 clean-local:: clean-a
203 install-local:: install-a
204 uninstall-local:: uninstall-a
206 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
207 $(Echo) "Building $(BuildMode) $(notdir $@)"
209 $(Verb) $(Archive) $@ $(ObjectsO)
213 -$(Verb) $(RM) -f $(LibraryA)
215 install-a:: $(LibraryA)
216 $(Echo) "Installing $(BuildMode) $(DestA)"
217 $(Verb) $(MKDIR) $(PROJ_libocamldir)
218 $(Verb) $(INSTALL) $(LibraryA) $(DestA)
222 $(Echo) "Uninstalling $(DestA)"
223 -$(Verb) $(RM) -f $(DestA)
227 ##===- Build stub library from C sources ----------------------------------===##
230 all-local:: $(SharedLib)
231 clean-local:: clean-shared
232 install-local:: install-shared
233 uninstall-local:: uninstall-shared
235 $(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
236 $(Echo) "Building $(BuildMode) $(notdir $@)"
237 $(Verb) $(Link) $(SharedLinkOptions) $(LLVMLibsOptions) \
241 -$(Verb) $(RM) -f $(SharedLib)
243 install-shared:: $(SharedLib)
244 $(Echo) "Installing $(BuildMode) $(DestSharedLib)"
245 $(Verb) $(MKDIR) $(PROJ_libocamldir)
246 $(Verb) $(INSTALL) $(SharedLib) $(DestSharedLib)
250 $(Echo) "Uninstalling $(DestSharedLib)"
251 -$(Verb) $(RM) -f $(DestSharedLib)
255 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
257 all-local:: build-deplibs
258 clean-local:: clean-deplibs
259 install-local:: install-deplibs
260 uninstall-local:: uninstall-deplibs
262 build-deplibs: $(OutputLibs)
264 $(OcamlDir)/%.a: $(LibDir)/%.a
267 $(OcamlDir)/%.o: $(LibDir)/%.o
271 $(Verb) $(RM) -f $(OutputLibs)
274 $(Verb) $(MKDIR) $(PROJ_libocamldir)
275 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
276 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
280 $(Verb) $(RM) -f $(DestLibs)
283 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
285 ifneq ($(OcamlHeaders),)
286 all-local:: build-cmis
287 clean-local:: clean-cmis
288 install-local:: install-cmis
289 uninstall-local:: uninstall-cmis
291 build-cmis: $(OutputsCMI)
293 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
294 $(Verb) $(CP) -f $< $@
296 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
297 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
298 $(Verb) $(Compile.CMI) $@ $<
301 -$(Verb) $(RM) -f $(OutputsCMI)
303 # Also install the .mli's (headers) as documentation.
304 install-cmis: $(OutputsCMI) $(OcamlHeaders)
305 $(Verb) $(MKDIR) $(PROJ_libocamldir)
306 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
307 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
308 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
310 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
311 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
312 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
316 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
317 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
318 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
320 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
321 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
322 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
327 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
329 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
330 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
331 $(Verb) $(Compile.CMO) $@ $<
334 all-local:: $(OutputCMA)
335 clean-local:: clean-cma
336 install-local:: install-cma
337 uninstall-local:: uninstall-cma
339 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
340 $(Verb) $(CP) -f $< $@
342 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
343 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
344 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
347 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
349 install-cma:: $(OutputCMA)
350 $(Echo) "Installing $(BuildMode) $(DestCMA)"
351 $(Verb) $(MKDIR) $(PROJ_libocamldir)
352 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
355 $(Echo) "Uninstalling $(DestCMA)"
356 -$(Verb) $(RM) -f $(DestCMA)
359 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
361 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
362 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
365 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
366 $(Verb) $(CP) -f $< $@
368 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
369 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
370 $(Verb) $(Compile.CMX) $@ $<
373 all-local:: $(OutputCMXA) $(OutputsCMX)
374 clean-local:: clean-cmxa
375 install-local:: install-cmxa
376 uninstall-local:: uninstall-cmxa
378 $(OutputCMXA): $(LibraryCMXA)
379 $(Verb) $(CP) -f $< $@
380 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
382 $(LibraryCMXA): $(ObjectsCMX)
383 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
384 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
385 $(Verb) $(RM) -f $(@:.cmxa=.o)
388 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
390 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
391 $(Verb) $(MKDIR) $(PROJ_libocamldir)
392 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
393 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
394 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
395 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
396 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
397 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
398 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
402 $(Echo) "Uninstalling $(DestCMXA)"
403 $(Verb) $(RM) -f $(DestCMXA)
404 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
405 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
406 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
407 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
408 $(RM) -f $(PROJ_libocamldir)/$$i; \
413 ##===- Build executables --------------------------------------------------===##
416 all-local:: $(OutputEXE)
417 clean-local:: clean-exe
419 $(OutputEXE): $(ToolEXE) $(OcamlDir)/.dir
420 $(Verb) $(CP) -f $< $@
423 $(ToolEXE): $(ObjectsCMO) $(OcamlDir)/.dir
424 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
425 $(Verb) $(Archive.EXE) $@ $(ObjectsCMO)
427 $(ToolEXE): $(ObjectsCMX) $(OcamlDir)/.dir
428 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
429 $(Verb) $(Archive.EXE) $@ $(ObjectsCMX)
433 ##===- Generate documentation ---------------------------------------------===##
435 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
436 $(Echo) "Documenting $(notdir $@)"
437 $(Verb) $(OCAMLDOC) -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
439 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
441 ##===- Debugging gunk -----------------------------------------------------===##
442 printvars:: printcamlvars
445 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
446 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
447 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
448 $(Echo) "OCAMLC : " '$(OCAMLC)'
449 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
450 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
451 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
452 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
453 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
454 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
455 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
456 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
457 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
458 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
459 $(Echo) "SharedLib : " '$(SharedLib)'
460 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
461 $(Echo) "OcamlSources2: " '$(OcamlSources2)'
462 $(Echo) "OcamlSources : " '$(OcamlSources)'
463 $(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
464 $(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
465 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
466 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
467 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
468 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
469 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
470 $(Echo) "DestA : " '$(DestA)'
471 $(Echo) "DestCMA : " '$(DestCMA)'
472 $(Echo) "DestCMXA : " '$(DestCMXA)'
473 $(Echo) "DestSharedLib: " '$(DestSharedLib)'
474 $(Echo) "UsedLibs : " '$(UsedLibs)'
475 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
477 .PHONY: printcamlvars build-cmis \
478 clean-a clean-cmis clean-cma clean-cmxa \
479 install-a install-cmis install-cma install-cmxa \
481 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \