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 # We have our own rules for building static libraries.
25 # CFLAGS needs to be set before Makefile.rules is included.
26 CXX.Flags += -I"$(shell $(OCAMLFIND) c -where)"
27 C.Flags += -I"$(shell $(OCAMLFIND) c -where)"
29 ifeq ($(ENABLE_SHARED),1)
30 LINK_COMPONENTS := all
33 include $(LEVEL)/Makefile.common
35 # Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
36 # user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
37 PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
38 OcamlDir := $(LibDir)/ocaml
40 # Info from llvm-config and similar
41 ifndef IS_CLEANING_TARGET
43 UsedLibs = $(shell $(LLVM_CONFIG) --libs --system-libs $(UsedComponents))
44 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
48 # How do we link OCaml executables with LLVM?
49 # 1) If this is a --enable-shared build, build stub libraries. This also allows
50 # to use LLVM from toplevels.
51 # 2) If this is a --disable-shared build, embed ocamlc options for building
52 # a custom runtime and a static executable. It is not possible to use LLVM
55 ifeq ($(ENABLE_SHARED),1)
57 OCAMLSTUBFLAGS := $(patsubst %,-cclib %, $(LLVMLibsOptions) -l$(LIBRARYNAME))
61 # Avoid the need for LD_LIBRARY_PATH
62 ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
63 ifneq ($(HOST_OS),Darwin)
64 OCAMLRPATH := $(RPATH) -Wl,'$$ORIGIN/../../lib'
68 # See http://caml.inria.fr/mantis/view.php?id=6642
69 OCAMLORIGIN := -ccopt -L'$$CAMLORIGIN/..' \
70 -ccopt $(RPATH) -ccopt -Wl,'$$CAMLORIGIN/..'
73 OCAMLCFLAGS += -I $(OcamlDir) $(addprefix -package ,$(FindlibPackages))
75 ifndef IS_CLEANING_TARGET
77 OCAMLAFLAGS += $(patsubst %,-cclib %, \
78 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
79 $(shell $(LLVM_CONFIG) --ldflags)) \
80 $(UsedLibs) $(ExtraLibs))
82 OCAMLAFLAGS += $(patsubst %,-cclib %, \
83 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
84 $(UsedLibs) $(ExtraLibs))
88 ifneq ($(DEBUG_SYMBOLS),1)
92 Compile.CMI := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
93 Compile.CMO := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
94 Compile.CMX := $(strip $(OCAMLFIND) opt -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
97 # -dllib is engaged with ocamlc builds, $(OCAMLSTUBFLAGS) in ocamlc -custom builds.
98 Archive.CMA := $(strip $(OCAMLFIND) c -a -dllib -l$(LIBRARYNAME) $(OCAMLSTUBFLAGS) \
99 $(OCAMLDEBUGFLAG) $(OCAMLORIGIN) -o)
101 Archive.CMA := $(strip $(OCAMLFIND) c -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
106 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLSTUBFLAGS) $(OCAMLDEBUGFLAG) \
109 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
115 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
119 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
122 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
123 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
125 OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
126 OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
129 ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
130 ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
131 ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
134 LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
135 LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
139 ToolEXE := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
143 # The .cmo files are the only intermediates; all others are to be installed.
144 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
145 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
146 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
149 LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
150 OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
151 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
155 SharedLib := $(OcamlDir)/dll$(LIBRARYNAME)$(SHLIBEXT)
160 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
162 OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
166 # Installation targets
167 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
170 DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
171 DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
172 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
176 DestSharedLib := $(PROJ_libocamldir)/dll$(LIBRARYNAME)$(SHLIBEXT)
179 ##===- Dependencies -------------------------------------------------------===##
180 # Copy the sources into the intermediate directory because older ocamlc doesn't
181 # support -o except when linking (outputs are placed next to inputs).
183 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
184 $(Verb) $(CP) -f $< $@
186 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
187 $(Verb) $(CP) -f $< $@
189 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
192 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
193 $(OcamlDir)/.dir $(ObjDir)/.dir
194 $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
196 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
200 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
201 $(OcamlDir)/.dir $(ObjDir)/.dir
202 $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
204 -include $(ObjDir)/$(TOOLNAME).ocamldep
207 ##===- Build static library from C sources --------------------------------===##
210 all-local:: $(LibraryA)
211 clean-local:: clean-a
212 install-local:: install-a
213 uninstall-local:: uninstall-a
215 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
216 $(Echo) "Building $(BuildMode) $(notdir $@)"
218 $(Verb) $(Archive) $@ $(ObjectsO)
222 -$(Verb) $(RM) -f $(LibraryA)
224 install-a:: $(LibraryA)
225 $(Echo) "Installing $(BuildMode) $(DestA)"
226 $(Verb) $(MKDIR) $(PROJ_libocamldir)
227 $(Verb) $(INSTALL) $(LibraryA) $(DestA)
231 $(Echo) "Uninstalling $(DestA)"
232 -$(Verb) $(RM) -f $(DestA)
236 ##===- Build stub library from C sources ----------------------------------===##
239 all-local:: $(SharedLib)
240 clean-local:: clean-shared
241 install-local:: install-shared
242 uninstall-local:: uninstall-shared
244 $(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
245 $(Echo) "Building $(BuildMode) $(notdir $@)"
246 $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) -o $@ $(ObjectsO) \
250 -$(Verb) $(RM) -f $(SharedLib)
252 install-shared:: $(SharedLib)
253 $(Echo) "Installing $(BuildMode) $(DestSharedLib)"
254 $(Verb) $(MKDIR) $(PROJ_libocamldir)
255 $(Verb) $(INSTALL) $(SharedLib) $(DestSharedLib)
259 $(Echo) "Uninstalling $(DestSharedLib)"
260 -$(Verb) $(RM) -f $(DestSharedLib)
264 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
266 all-local:: build-deplibs
267 clean-local:: clean-deplibs
268 install-local:: install-deplibs
269 uninstall-local:: uninstall-deplibs
271 build-deplibs: $(OutputLibs)
273 $(OcamlDir)/%.a: $(LibDir)/%.a
276 $(OcamlDir)/%.o: $(LibDir)/%.o
280 $(Verb) $(RM) -f $(OutputLibs)
283 $(Verb) $(MKDIR) $(PROJ_libocamldir)
284 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
285 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
289 $(Verb) $(RM) -f $(DestLibs)
292 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
294 ifneq ($(OcamlHeaders),)
295 all-local:: build-cmis
296 clean-local:: clean-cmis
297 install-local:: install-cmis
298 uninstall-local:: uninstall-cmis
300 build-cmis: $(OutputsCMI)
302 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
303 $(Verb) $(CP) -f $< $@
305 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
306 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
307 $(Verb) $(Compile.CMI) $@ $<
310 -$(Verb) $(RM) -f $(OutputsCMI)
312 # Also install the .mli's (headers) as documentation.
313 install-cmis: $(OutputsCMI) $(OcamlHeaders)
314 $(Verb) $(MKDIR) $(PROJ_libocamldir)
315 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
316 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
317 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
319 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
320 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
321 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
325 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
326 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
327 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
329 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
330 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
331 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
336 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
338 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
339 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
340 $(Verb) $(Compile.CMO) $@ $<
343 all-local:: $(OutputCMA)
344 clean-local:: clean-cma
345 install-local:: install-cma
346 uninstall-local:: uninstall-cma
348 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
349 $(Verb) $(CP) -f $< $@
351 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
352 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
353 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
356 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
358 install-cma:: $(OutputCMA)
359 $(Echo) "Installing $(BuildMode) $(DestCMA)"
360 $(Verb) $(MKDIR) $(PROJ_libocamldir)
361 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
364 $(Echo) "Uninstalling $(DestCMA)"
365 -$(Verb) $(RM) -f $(DestCMA)
368 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
370 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
371 # If unavailable, 'configure' will set HAVE_OCAMLOPT to 0 in Makefile.config.
372 ifeq ($(HAVE_OCAMLOPT),1)
374 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
375 $(Verb) $(CP) -f $< $@
377 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
378 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
379 $(Verb) $(Compile.CMX) $@ $<
382 all-local:: $(OutputCMXA) $(OutputsCMX)
383 clean-local:: clean-cmxa
384 install-local:: install-cmxa
385 uninstall-local:: uninstall-cmxa
387 $(OutputCMXA): $(LibraryCMXA)
388 $(Verb) $(CP) -f $< $@
389 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
391 $(LibraryCMXA): $(ObjectsCMX)
392 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
393 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
394 $(Verb) $(RM) -f $(@:.cmxa=.o)
397 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
399 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
400 $(Verb) $(MKDIR) $(PROJ_libocamldir)
401 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
402 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
403 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
404 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
405 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
406 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
407 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
411 $(Echo) "Uninstalling $(DestCMXA)"
412 $(Verb) $(RM) -f $(DestCMXA)
413 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
414 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
415 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
416 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
417 $(RM) -f $(PROJ_libocamldir)/$$i; \
422 ##===- Generate documentation ---------------------------------------------===##
424 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
425 $(Echo) "Documenting $(notdir $@)"
426 $(Verb) $(OCAMLFIND) doc -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
428 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
430 ##===- Debugging gunk -----------------------------------------------------===##
431 printvars:: printcamlvars
434 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
435 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
436 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
437 $(Echo) "OCAMLRPATH : " '$(OCAMLRPATH)'
438 $(Echo) "OCAMLSTUBS : " '$(OCAMLSTUBS)'
439 $(Echo) "OCAMLSTUBFLAGS : " '$(OCAMLSTUBFLAGS)'
440 $(Echo) "OCAMLFIND : " '$(OCAMLFIND)'
441 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
442 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
443 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
444 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
445 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
446 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
447 $(Echo) "LibraryA : " '$(LibraryA)'
448 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
449 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
450 $(Echo) "SharedLib : " '$(SharedLib)'
451 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
452 $(Echo) "OcamlSources2: " '$(OcamlSources2)'
453 $(Echo) "OcamlSources : " '$(OcamlSources)'
454 $(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
455 $(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
456 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
457 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
458 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
459 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
460 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
461 $(Echo) "DestA : " '$(DestA)'
462 $(Echo) "DestCMA : " '$(DestCMA)'
463 $(Echo) "DestCMXA : " '$(DestCMXA)'
464 $(Echo) "DestSharedLib: " '$(DestSharedLib)'
465 $(Echo) "UsedLibs : " '$(UsedLibs)'
466 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
467 $(Echo) "ExtraLibs : " '$(ExtraLibs)'
469 .PHONY: printcamlvars build-cmis \
470 clean-a clean-cmis clean-cma clean-cmxa \
471 install-a install-cmis install-cma install-cmxa \
473 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \