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 --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 $(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 # Avoid the need for LD_LIBRARY_PATH
83 ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
84 ifneq ($(HOST_OS),Darwin)
85 OCAMLRPATH := $(RPATH) -Wl,'$(SharedLibDir)'
91 Archive.CMA := $(strip $(OCAMLC) -a -dllib -l$(LIBRARYNAME) $(OCAMLDEBUGFLAG) \
94 Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
99 Archive.CMXA := $(strip $(OCAMLOPT) -a $(patsubst %,-cclib %, \
100 $(LLVMLibsOptions) -l$(LIBRARYNAME) \
101 -L$(SharedLibDir) $(OCAMLRPATH)) \
102 $(OCAMLDEBUGFLAG) -o)
104 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
108 Archive.EXE := $(strip $(OCAMLOPT) -cc $(CXX) $(OCAMLCFLAGS) $(UsedOcamlLibs:%=%.cmxa) $(OCAMLDEBUGFLAG) -o)
110 Archive.EXE := $(strip $(OCAMLC) -cc $(CXX) $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG:%=%.cma) -o)
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) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
196 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
200 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
201 $(OcamlDir)/.dir $(ObjDir)/.dir
202 $(Verb) $(OCAMLDEP) $(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) $(LLVMLibsOptions) \
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 not define OCAMLOPT in Makefile.config.
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 ##===- Build executables --------------------------------------------------===##
425 all-local:: $(OutputEXE)
426 clean-local:: clean-exe
428 $(OutputEXE): $(ToolEXE) $(OcamlDir)/.dir
429 $(Verb) $(CP) -f $< $@
432 $(ToolEXE): $(ObjectsCMO) $(OcamlDir)/.dir
433 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
434 $(Verb) $(Archive.EXE) $@ $(ObjectsCMO)
436 $(ToolEXE): $(ObjectsCMX) $(OcamlDir)/.dir
437 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
438 $(Verb) $(Archive.EXE) $@ $(ObjectsCMX)
442 ##===- Generate documentation ---------------------------------------------===##
444 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
445 $(Echo) "Documenting $(notdir $@)"
446 $(Verb) $(OCAMLDOC) -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
448 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
450 ##===- Debugging gunk -----------------------------------------------------===##
451 printvars:: printcamlvars
454 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
455 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
456 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
457 $(Echo) "OCAMLC : " '$(OCAMLC)'
458 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
459 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
460 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
461 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
462 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
463 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
464 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
465 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
466 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
467 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
468 $(Echo) "SharedLib : " '$(SharedLib)'
469 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
470 $(Echo) "OcamlSources2: " '$(OcamlSources2)'
471 $(Echo) "OcamlSources : " '$(OcamlSources)'
472 $(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
473 $(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
474 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
475 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
476 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
477 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
478 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
479 $(Echo) "DestA : " '$(DestA)'
480 $(Echo) "DestCMA : " '$(DestCMA)'
481 $(Echo) "DestCMXA : " '$(DestCMXA)'
482 $(Echo) "DestSharedLib: " '$(DestSharedLib)'
483 $(Echo) "UsedLibs : " '$(UsedLibs)'
484 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
486 .PHONY: printcamlvars build-cmis \
487 clean-a clean-cmis clean-cma clean-cmxa \
488 install-a install-cmis install-cma install-cmxa \
490 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \