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 $(OCAMLFIND) c -where)"
24 C.Flags += -I"$(shell $(OCAMLFIND) c -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 --system-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 $(OcamlDir)
59 ifndef IS_CLEANING_TARGET
61 OCAMLAFLAGS += $(patsubst %,-cclib %, \
62 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
63 $(shell $(LLVM_CONFIG) --ldflags)) \
64 $(UsedLibs) $(ExtraLibs))
66 OCAMLAFLAGS += $(patsubst %,-cclib %, \
67 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
68 $(UsedLibs) $(ExtraLibs))
72 ifneq ($(ENABLE_OPTIMIZED),1)
76 Compile.CMI := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
77 Compile.CMO := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
78 Compile.CMX := $(strip $(OCAMLFIND) opt -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
81 # Avoid the need for LD_LIBRARY_PATH
82 ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
83 ifneq ($(HOST_OS),Darwin)
84 OCAMLRPATH := $(RPATH) -Wl,'$(SharedLibDir)'
90 Archive.CMA := $(strip $(OCAMLFIND) c -a -dllib -l$(LIBRARYNAME) $(OCAMLDEBUGFLAG) \
93 Archive.CMA := $(strip $(OCAMLFIND) c -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
98 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(patsubst %,-cclib %, \
99 $(LLVMLibsOptions) -l$(LIBRARYNAME) \
100 -L$(SharedLibDir) $(OCAMLRPATH)) \
101 $(OCAMLDEBUGFLAG) -o)
103 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
108 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
112 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
115 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
116 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
118 OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
119 OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
122 ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
123 ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
124 ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
127 LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
128 LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
132 ToolEXE := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
136 # The .cmo files are the only intermediates; all others are to be installed.
137 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
138 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
139 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
142 LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
143 OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
144 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
148 SharedLib := $(OcamlDir)/dll$(LIBRARYNAME)$(SHLIBEXT)
153 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
155 OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
159 # Installation targets
160 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
163 DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
164 DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
165 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
169 DestSharedLib := $(PROJ_libocamldir)/dll$(LIBRARYNAME)$(SHLIBEXT)
172 ##===- Dependencies -------------------------------------------------------===##
173 # Copy the sources into the intermediate directory because older ocamlc doesn't
174 # support -o except when linking (outputs are placed next to inputs).
176 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
177 $(Verb) $(CP) -f $< $@
179 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
180 $(Verb) $(CP) -f $< $@
182 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
185 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
186 $(OcamlDir)/.dir $(ObjDir)/.dir
187 $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
189 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
193 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
194 $(OcamlDir)/.dir $(ObjDir)/.dir
195 $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
197 -include $(ObjDir)/$(TOOLNAME).ocamldep
200 ##===- Build static library from C sources --------------------------------===##
203 all-local:: $(LibraryA)
204 clean-local:: clean-a
205 install-local:: install-a
206 uninstall-local:: uninstall-a
208 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
209 $(Echo) "Building $(BuildMode) $(notdir $@)"
211 $(Verb) $(Archive) $@ $(ObjectsO)
215 -$(Verb) $(RM) -f $(LibraryA)
217 install-a:: $(LibraryA)
218 $(Echo) "Installing $(BuildMode) $(DestA)"
219 $(Verb) $(MKDIR) $(PROJ_libocamldir)
220 $(Verb) $(INSTALL) $(LibraryA) $(DestA)
224 $(Echo) "Uninstalling $(DestA)"
225 -$(Verb) $(RM) -f $(DestA)
229 ##===- Build stub library from C sources ----------------------------------===##
232 all-local:: $(SharedLib)
233 clean-local:: clean-shared
234 install-local:: install-shared
235 uninstall-local:: uninstall-shared
237 $(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
238 $(Echo) "Building $(BuildMode) $(notdir $@)"
239 $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) $(LLVMLibsOptions) \
243 -$(Verb) $(RM) -f $(SharedLib)
245 install-shared:: $(SharedLib)
246 $(Echo) "Installing $(BuildMode) $(DestSharedLib)"
247 $(Verb) $(MKDIR) $(PROJ_libocamldir)
248 $(Verb) $(INSTALL) $(SharedLib) $(DestSharedLib)
252 $(Echo) "Uninstalling $(DestSharedLib)"
253 -$(Verb) $(RM) -f $(DestSharedLib)
257 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
259 all-local:: build-deplibs
260 clean-local:: clean-deplibs
261 install-local:: install-deplibs
262 uninstall-local:: uninstall-deplibs
264 build-deplibs: $(OutputLibs)
266 $(OcamlDir)/%.a: $(LibDir)/%.a
269 $(OcamlDir)/%.o: $(LibDir)/%.o
273 $(Verb) $(RM) -f $(OutputLibs)
276 $(Verb) $(MKDIR) $(PROJ_libocamldir)
277 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
278 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
282 $(Verb) $(RM) -f $(DestLibs)
285 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
287 ifneq ($(OcamlHeaders),)
288 all-local:: build-cmis
289 clean-local:: clean-cmis
290 install-local:: install-cmis
291 uninstall-local:: uninstall-cmis
293 build-cmis: $(OutputsCMI)
295 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
296 $(Verb) $(CP) -f $< $@
298 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
299 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
300 $(Verb) $(Compile.CMI) $@ $<
303 -$(Verb) $(RM) -f $(OutputsCMI)
305 # Also install the .mli's (headers) as documentation.
306 install-cmis: $(OutputsCMI) $(OcamlHeaders)
307 $(Verb) $(MKDIR) $(PROJ_libocamldir)
308 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
309 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
310 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
312 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
313 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
314 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
318 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
319 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
320 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
322 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
323 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
324 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
329 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
331 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
332 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
333 $(Verb) $(Compile.CMO) $@ $<
336 all-local:: $(OutputCMA)
337 clean-local:: clean-cma
338 install-local:: install-cma
339 uninstall-local:: uninstall-cma
341 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
342 $(Verb) $(CP) -f $< $@
344 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
345 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
346 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
349 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
351 install-cma:: $(OutputCMA)
352 $(Echo) "Installing $(BuildMode) $(DestCMA)"
353 $(Verb) $(MKDIR) $(PROJ_libocamldir)
354 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
357 $(Echo) "Uninstalling $(DestCMA)"
358 -$(Verb) $(RM) -f $(DestCMA)
361 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
363 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
364 # If unavailable, 'configure' will set HAVE_OCAMLOPT to 0 in Makefile.config.
365 ifeq ($(HAVE_OCAMLOPT),1)
367 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
368 $(Verb) $(CP) -f $< $@
370 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
371 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
372 $(Verb) $(Compile.CMX) $@ $<
375 all-local:: $(OutputCMXA) $(OutputsCMX)
376 clean-local:: clean-cmxa
377 install-local:: install-cmxa
378 uninstall-local:: uninstall-cmxa
380 $(OutputCMXA): $(LibraryCMXA)
381 $(Verb) $(CP) -f $< $@
382 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
384 $(LibraryCMXA): $(ObjectsCMX)
385 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
386 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
387 $(Verb) $(RM) -f $(@:.cmxa=.o)
390 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
392 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
393 $(Verb) $(MKDIR) $(PROJ_libocamldir)
394 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
395 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
396 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
397 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
398 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
399 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
400 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
404 $(Echo) "Uninstalling $(DestCMXA)"
405 $(Verb) $(RM) -f $(DestCMXA)
406 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
407 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
408 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
409 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
410 $(RM) -f $(PROJ_libocamldir)/$$i; \
415 ##===- Generate documentation ---------------------------------------------===##
417 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
418 $(Echo) "Documenting $(notdir $@)"
419 $(Verb) $(OCAMLFIND) doc -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
421 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
423 ##===- Debugging gunk -----------------------------------------------------===##
424 printvars:: printcamlvars
427 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
428 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
429 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
430 $(Echo) "OCAMLFIND : " '$(OCAMLFIND)'
431 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
432 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
433 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
434 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
435 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
436 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
437 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
438 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
439 $(Echo) "SharedLib : " '$(SharedLib)'
440 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
441 $(Echo) "OcamlSources2: " '$(OcamlSources2)'
442 $(Echo) "OcamlSources : " '$(OcamlSources)'
443 $(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
444 $(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
445 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
446 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
447 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
448 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
449 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
450 $(Echo) "DestA : " '$(DestA)'
451 $(Echo) "DestCMA : " '$(DestCMA)'
452 $(Echo) "DestCMXA : " '$(DestCMXA)'
453 $(Echo) "DestSharedLib: " '$(DestSharedLib)'
454 $(Echo) "UsedLibs : " '$(UsedLibs)'
455 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
456 $(Echo) "ExtraLibs : " '$(ExtraLibs)'
458 .PHONY: printcamlvars build-cmis \
459 clean-a clean-cmis clean-cma clean-cmxa \
460 install-a install-cmis install-cma install-cmxa \
462 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \