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'
69 OCAMLCFLAGS += -I $(OcamlDir) $(addprefix -package ,$(FindlibPackages))
71 ifndef IS_CLEANING_TARGET
73 OCAMLAFLAGS += $(patsubst %,-cclib %, \
74 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
75 $(shell $(LLVM_CONFIG) --ldflags)) \
76 $(UsedLibs) $(ExtraLibs))
78 OCAMLAFLAGS += $(patsubst %,-cclib %, \
79 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
80 $(UsedLibs) $(ExtraLibs))
84 ifneq ($(DEBUG_SYMBOLS),1)
88 Compile.CMI := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
89 Compile.CMO := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
90 Compile.CMX := $(strip $(OCAMLFIND) opt -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
93 # -dllib is engaged with ocamlc builds, $(OCAMLSTUBFLAGS) in ocamlc -custom builds.
94 Archive.CMA := $(strip $(OCAMLFIND) c -a -dllib -l$(LIBRARYNAME) $(OCAMLSTUBFLAGS) \
97 Archive.CMA := $(strip $(OCAMLFIND) c -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
102 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLSTUBFLAGS) $(OCAMLDEBUGFLAG) -o)
104 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
109 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
113 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
116 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
117 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
119 OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
120 OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
123 ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
124 ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
125 ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
128 LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
129 LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
133 ToolEXE := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
137 # The .cmo files are the only intermediates; all others are to be installed.
138 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
139 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
140 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
143 LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
144 OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
145 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
149 SharedLib := $(OcamlDir)/dll$(LIBRARYNAME)$(SHLIBEXT)
154 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
156 OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
160 # Installation targets
161 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
164 DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
165 DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
166 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
170 DestSharedLib := $(PROJ_libocamldir)/dll$(LIBRARYNAME)$(SHLIBEXT)
173 ##===- Dependencies -------------------------------------------------------===##
174 # Copy the sources into the intermediate directory because older ocamlc doesn't
175 # support -o except when linking (outputs are placed next to inputs).
177 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
178 $(Verb) $(CP) -f $< $@
180 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
181 $(Verb) $(CP) -f $< $@
183 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
186 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
187 $(OcamlDir)/.dir $(ObjDir)/.dir
188 $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
190 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
194 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
195 $(OcamlDir)/.dir $(ObjDir)/.dir
196 $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
198 -include $(ObjDir)/$(TOOLNAME).ocamldep
201 ##===- Build static library from C sources --------------------------------===##
204 all-local:: $(LibraryA)
205 clean-local:: clean-a
206 install-local:: install-a
207 uninstall-local:: uninstall-a
209 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
210 $(Echo) "Building $(BuildMode) $(notdir $@)"
212 $(Verb) $(Archive) $@ $(ObjectsO)
216 -$(Verb) $(RM) -f $(LibraryA)
218 install-a:: $(LibraryA)
219 $(Echo) "Installing $(BuildMode) $(DestA)"
220 $(Verb) $(MKDIR) $(PROJ_libocamldir)
221 $(Verb) $(INSTALL) $(LibraryA) $(DestA)
225 $(Echo) "Uninstalling $(DestA)"
226 -$(Verb) $(RM) -f $(DestA)
230 ##===- Build stub library from C sources ----------------------------------===##
233 all-local:: $(SharedLib)
234 clean-local:: clean-shared
235 install-local:: install-shared
236 uninstall-local:: uninstall-shared
238 $(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
239 $(Echo) "Building $(BuildMode) $(notdir $@)"
240 $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) -o $@ $(ObjectsO) \
244 -$(Verb) $(RM) -f $(SharedLib)
246 install-shared:: $(SharedLib)
247 $(Echo) "Installing $(BuildMode) $(DestSharedLib)"
248 $(Verb) $(MKDIR) $(PROJ_libocamldir)
249 $(Verb) $(INSTALL) $(SharedLib) $(DestSharedLib)
253 $(Echo) "Uninstalling $(DestSharedLib)"
254 -$(Verb) $(RM) -f $(DestSharedLib)
258 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
260 all-local:: build-deplibs
261 clean-local:: clean-deplibs
262 install-local:: install-deplibs
263 uninstall-local:: uninstall-deplibs
265 build-deplibs: $(OutputLibs)
267 $(OcamlDir)/%.a: $(LibDir)/%.a
270 $(OcamlDir)/%.o: $(LibDir)/%.o
274 $(Verb) $(RM) -f $(OutputLibs)
277 $(Verb) $(MKDIR) $(PROJ_libocamldir)
278 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
279 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
283 $(Verb) $(RM) -f $(DestLibs)
286 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
288 ifneq ($(OcamlHeaders),)
289 all-local:: build-cmis
290 clean-local:: clean-cmis
291 install-local:: install-cmis
292 uninstall-local:: uninstall-cmis
294 build-cmis: $(OutputsCMI)
296 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
297 $(Verb) $(CP) -f $< $@
299 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
300 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
301 $(Verb) $(Compile.CMI) $@ $<
304 -$(Verb) $(RM) -f $(OutputsCMI)
306 # Also install the .mli's (headers) as documentation.
307 install-cmis: $(OutputsCMI) $(OcamlHeaders)
308 $(Verb) $(MKDIR) $(PROJ_libocamldir)
309 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
310 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
311 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
313 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
314 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
315 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
319 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
320 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
321 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
323 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
324 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
325 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
330 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
332 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
333 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
334 $(Verb) $(Compile.CMO) $@ $<
337 all-local:: $(OutputCMA)
338 clean-local:: clean-cma
339 install-local:: install-cma
340 uninstall-local:: uninstall-cma
342 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
343 $(Verb) $(CP) -f $< $@
345 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
346 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
347 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
350 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
352 install-cma:: $(OutputCMA)
353 $(Echo) "Installing $(BuildMode) $(DestCMA)"
354 $(Verb) $(MKDIR) $(PROJ_libocamldir)
355 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
358 $(Echo) "Uninstalling $(DestCMA)"
359 -$(Verb) $(RM) -f $(DestCMA)
362 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
364 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
365 # If unavailable, 'configure' will set HAVE_OCAMLOPT to 0 in Makefile.config.
366 ifeq ($(HAVE_OCAMLOPT),1)
368 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
369 $(Verb) $(CP) -f $< $@
371 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
372 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
373 $(Verb) $(Compile.CMX) $@ $<
376 all-local:: $(OutputCMXA) $(OutputsCMX)
377 clean-local:: clean-cmxa
378 install-local:: install-cmxa
379 uninstall-local:: uninstall-cmxa
381 $(OutputCMXA): $(LibraryCMXA)
382 $(Verb) $(CP) -f $< $@
383 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
385 $(LibraryCMXA): $(ObjectsCMX)
386 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
387 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
388 $(Verb) $(RM) -f $(@:.cmxa=.o)
391 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
393 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
394 $(Verb) $(MKDIR) $(PROJ_libocamldir)
395 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
396 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
397 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
398 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
399 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
400 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
401 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
405 $(Echo) "Uninstalling $(DestCMXA)"
406 $(Verb) $(RM) -f $(DestCMXA)
407 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
408 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
409 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
410 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
411 $(RM) -f $(PROJ_libocamldir)/$$i; \
416 ##===- Generate documentation ---------------------------------------------===##
418 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
419 $(Echo) "Documenting $(notdir $@)"
420 $(Verb) $(OCAMLFIND) doc -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
422 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
424 ##===- Debugging gunk -----------------------------------------------------===##
425 printvars:: printcamlvars
428 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
429 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
430 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
431 $(Echo) "OCAMLRPATH : " '$(OCAMLRPATH)'
432 $(Echo) "OCAMLSTUBS : " '$(OCAMLSTUBS)'
433 $(Echo) "OCAMLSTUBFLAGS : " '$(OCAMLSTUBFLAGS)'
434 $(Echo) "OCAMLFIND : " '$(OCAMLFIND)'
435 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
436 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
437 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
438 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
439 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
440 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
441 $(Echo) "LibraryA : " '$(LibraryA)'
442 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
443 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
444 $(Echo) "SharedLib : " '$(SharedLib)'
445 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
446 $(Echo) "OcamlSources2: " '$(OcamlSources2)'
447 $(Echo) "OcamlSources : " '$(OcamlSources)'
448 $(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
449 $(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
450 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
451 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
452 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
453 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
454 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
455 $(Echo) "DestA : " '$(DestA)'
456 $(Echo) "DestCMA : " '$(DestCMA)'
457 $(Echo) "DestCMXA : " '$(DestCMXA)'
458 $(Echo) "DestSharedLib: " '$(DestSharedLib)'
459 $(Echo) "UsedLibs : " '$(UsedLibs)'
460 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
461 $(Echo) "ExtraLibs : " '$(ExtraLibs)'
463 .PHONY: printcamlvars build-cmis \
464 clean-a clean-cmis clean-cma clean-cmxa \
465 install-a install-cmis install-cma install-cmxa \
467 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \