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)
61 OCAMLCFLAGS += -I $(OcamlDir) $(addprefix -package ,$(FindlibPackages))
63 ifndef IS_CLEANING_TARGET
65 OCAMLAFLAGS += $(patsubst %,-cclib %, \
66 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
67 $(shell $(LLVM_CONFIG) --ldflags)) \
68 $(UsedLibs) $(ExtraLibs))
70 OCAMLAFLAGS += $(patsubst %,-cclib %, \
71 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
72 $(UsedLibs) $(ExtraLibs))
76 ifneq ($(ENABLE_OPTIMIZED),1)
80 Compile.CMI := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
81 Compile.CMO := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
82 Compile.CMX := $(strip $(OCAMLFIND) opt -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
85 # Avoid the need for LD_LIBRARY_PATH
86 ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
87 ifneq ($(HOST_OS),Darwin)
88 OCAMLRPATH := $(RPATH) -Wl,'$(SharedLibDir)'
94 Archive.CMA := $(strip $(OCAMLFIND) c -a -dllib -l$(LIBRARYNAME) $(OCAMLDEBUGFLAG) \
97 Archive.CMA := $(strip $(OCAMLFIND) c -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
102 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(patsubst %,-cclib %, \
103 $(LLVMLibsOptions) -l$(LIBRARYNAME) \
104 -L$(SharedLibDir) $(OCAMLRPATH)) \
105 $(OCAMLDEBUGFLAG) -o)
107 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
112 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
116 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
119 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
120 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
122 OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
123 OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
126 ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
127 ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
128 ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
131 LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
132 LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
136 ToolEXE := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
140 # The .cmo files are the only intermediates; all others are to be installed.
141 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
142 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
143 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
146 LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
147 OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
148 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
152 SharedLib := $(OcamlDir)/dll$(LIBRARYNAME)$(SHLIBEXT)
157 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
159 OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
163 # Installation targets
164 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
167 DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
168 DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
169 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
173 DestSharedLib := $(PROJ_libocamldir)/dll$(LIBRARYNAME)$(SHLIBEXT)
176 ##===- Dependencies -------------------------------------------------------===##
177 # Copy the sources into the intermediate directory because older ocamlc doesn't
178 # support -o except when linking (outputs are placed next to inputs).
180 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
181 $(Verb) $(CP) -f $< $@
183 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
184 $(Verb) $(CP) -f $< $@
186 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
189 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
190 $(OcamlDir)/.dir $(ObjDir)/.dir
191 $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
193 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
197 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
198 $(OcamlDir)/.dir $(ObjDir)/.dir
199 $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
201 -include $(ObjDir)/$(TOOLNAME).ocamldep
204 ##===- Build static library from C sources --------------------------------===##
207 all-local:: $(LibraryA)
208 clean-local:: clean-a
209 install-local:: install-a
210 uninstall-local:: uninstall-a
212 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
213 $(Echo) "Building $(BuildMode) $(notdir $@)"
215 $(Verb) $(Archive) $@ $(ObjectsO)
219 -$(Verb) $(RM) -f $(LibraryA)
221 install-a:: $(LibraryA)
222 $(Echo) "Installing $(BuildMode) $(DestA)"
223 $(Verb) $(MKDIR) $(PROJ_libocamldir)
224 $(Verb) $(INSTALL) $(LibraryA) $(DestA)
228 $(Echo) "Uninstalling $(DestA)"
229 -$(Verb) $(RM) -f $(DestA)
233 ##===- Build stub library from C sources ----------------------------------===##
236 all-local:: $(SharedLib)
237 clean-local:: clean-shared
238 install-local:: install-shared
239 uninstall-local:: uninstall-shared
241 $(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
242 $(Echo) "Building $(BuildMode) $(notdir $@)"
243 $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) $(LLVMLibsOptions) \
247 -$(Verb) $(RM) -f $(SharedLib)
249 install-shared:: $(SharedLib)
250 $(Echo) "Installing $(BuildMode) $(DestSharedLib)"
251 $(Verb) $(MKDIR) $(PROJ_libocamldir)
252 $(Verb) $(INSTALL) $(SharedLib) $(DestSharedLib)
256 $(Echo) "Uninstalling $(DestSharedLib)"
257 -$(Verb) $(RM) -f $(DestSharedLib)
261 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
263 all-local:: build-deplibs
264 clean-local:: clean-deplibs
265 install-local:: install-deplibs
266 uninstall-local:: uninstall-deplibs
268 build-deplibs: $(OutputLibs)
270 $(OcamlDir)/%.a: $(LibDir)/%.a
273 $(OcamlDir)/%.o: $(LibDir)/%.o
277 $(Verb) $(RM) -f $(OutputLibs)
280 $(Verb) $(MKDIR) $(PROJ_libocamldir)
281 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
282 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
286 $(Verb) $(RM) -f $(DestLibs)
289 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
291 ifneq ($(OcamlHeaders),)
292 all-local:: build-cmis
293 clean-local:: clean-cmis
294 install-local:: install-cmis
295 uninstall-local:: uninstall-cmis
297 build-cmis: $(OutputsCMI)
299 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
300 $(Verb) $(CP) -f $< $@
302 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
303 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
304 $(Verb) $(Compile.CMI) $@ $<
307 -$(Verb) $(RM) -f $(OutputsCMI)
309 # Also install the .mli's (headers) as documentation.
310 install-cmis: $(OutputsCMI) $(OcamlHeaders)
311 $(Verb) $(MKDIR) $(PROJ_libocamldir)
312 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
313 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
314 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
316 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
317 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
318 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
322 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
323 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
324 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
326 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
327 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
328 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
333 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
335 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
336 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
337 $(Verb) $(Compile.CMO) $@ $<
340 all-local:: $(OutputCMA)
341 clean-local:: clean-cma
342 install-local:: install-cma
343 uninstall-local:: uninstall-cma
345 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
346 $(Verb) $(CP) -f $< $@
348 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
349 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
350 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
353 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
355 install-cma:: $(OutputCMA)
356 $(Echo) "Installing $(BuildMode) $(DestCMA)"
357 $(Verb) $(MKDIR) $(PROJ_libocamldir)
358 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
361 $(Echo) "Uninstalling $(DestCMA)"
362 -$(Verb) $(RM) -f $(DestCMA)
365 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
367 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
368 # If unavailable, 'configure' will set HAVE_OCAMLOPT to 0 in Makefile.config.
369 ifeq ($(HAVE_OCAMLOPT),1)
371 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
372 $(Verb) $(CP) -f $< $@
374 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
375 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
376 $(Verb) $(Compile.CMX) $@ $<
379 all-local:: $(OutputCMXA) $(OutputsCMX)
380 clean-local:: clean-cmxa
381 install-local:: install-cmxa
382 uninstall-local:: uninstall-cmxa
384 $(OutputCMXA): $(LibraryCMXA)
385 $(Verb) $(CP) -f $< $@
386 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
388 $(LibraryCMXA): $(ObjectsCMX)
389 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
390 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
391 $(Verb) $(RM) -f $(@:.cmxa=.o)
394 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
396 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
397 $(Verb) $(MKDIR) $(PROJ_libocamldir)
398 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
399 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
400 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
401 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
402 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
403 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
404 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
408 $(Echo) "Uninstalling $(DestCMXA)"
409 $(Verb) $(RM) -f $(DestCMXA)
410 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
411 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
412 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
413 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
414 $(RM) -f $(PROJ_libocamldir)/$$i; \
419 ##===- Generate documentation ---------------------------------------------===##
421 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
422 $(Echo) "Documenting $(notdir $@)"
423 $(Verb) $(OCAMLFIND) doc -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
425 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
427 ##===- Debugging gunk -----------------------------------------------------===##
428 printvars:: printcamlvars
431 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
432 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
433 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
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) "LibraryCMA : " '$(LibraryCMA)'
442 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
443 $(Echo) "SharedLib : " '$(SharedLib)'
444 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
445 $(Echo) "OcamlSources2: " '$(OcamlSources2)'
446 $(Echo) "OcamlSources : " '$(OcamlSources)'
447 $(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
448 $(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
449 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
450 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
451 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
452 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
453 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
454 $(Echo) "DestA : " '$(DestA)'
455 $(Echo) "DestCMA : " '$(DestCMA)'
456 $(Echo) "DestCMXA : " '$(DestCMXA)'
457 $(Echo) "DestSharedLib: " '$(DestSharedLib)'
458 $(Echo) "UsedLibs : " '$(UsedLibs)'
459 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
460 $(Echo) "ExtraLibs : " '$(ExtraLibs)'
462 .PHONY: printcamlvars build-cmis \
463 clean-a clean-cmis clean-cma clean-cmxa \
464 install-a install-cmis install-cma install-cmxa \
466 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \