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 $(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 Archive.CMA := $(strip $(OCAMLC) -a -dllib -l$(LIBRARYNAME) $(OCAMLDEBUGFLAG) \
85 Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
90 Archive.CMXA := $(strip $(OCAMLOPT) -a $(patsubst %,-cclib %, \
91 $(LLVMLibsOptions) -l$(LIBRARYNAME)) \
94 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
98 Archive.EXE := $(strip $(OCAMLOPT) -cc $(CXX) $(OCAMLCFLAGS) $(UsedOcamlLibs:%=%.cmxa) $(OCAMLDEBUGFLAG) -o)
100 Archive.EXE := $(strip $(OCAMLC) -cc $(CXX) $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG:%=%.cma) -o)
105 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
109 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
112 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
113 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
115 OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
116 OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
119 ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
120 ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
121 ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
124 LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
125 LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
129 ToolEXE := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
133 # The .cmo files are the only intermediates; all others are to be installed.
134 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
135 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
136 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
139 LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
140 OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
141 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
145 SharedLib := $(OcamlDir)/dll$(LIBRARYNAME).$(SHLIBEXT)
150 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
152 OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
156 # Installation targets
157 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
160 DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
161 DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
162 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
166 DestSharedLib := $(PROJ_libocamldir)/dll$(LIBRARYNAME).$(SHLIBEXT)
169 ##===- Dependencies -------------------------------------------------------===##
170 # Copy the sources into the intermediate directory because older ocamlc doesn't
171 # support -o except when linking (outputs are placed next to inputs).
173 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
174 $(Verb) $(CP) -f $< $@
176 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
177 $(Verb) $(CP) -f $< $@
179 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
182 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
183 $(OcamlDir)/.dir $(ObjDir)/.dir
184 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
186 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
190 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
191 $(OcamlDir)/.dir $(ObjDir)/.dir
192 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
194 -include $(ObjDir)/$(TOOLNAME).ocamldep
197 ##===- Build static library from C sources --------------------------------===##
200 all-local:: $(LibraryA)
201 clean-local:: clean-a
202 install-local:: install-a
203 uninstall-local:: uninstall-a
205 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
206 $(Echo) "Building $(BuildMode) $(notdir $@)"
208 $(Verb) $(Archive) $@ $(ObjectsO)
212 -$(Verb) $(RM) -f $(LibraryA)
214 install-a:: $(LibraryA)
215 $(Echo) "Installing $(BuildMode) $(DestA)"
216 $(Verb) $(MKDIR) $(PROJ_libocamldir)
217 $(Verb) $(INSTALL) $(LibraryA) $(DestA)
221 $(Echo) "Uninstalling $(DestA)"
222 -$(Verb) $(RM) -f $(DestA)
226 ##===- Build stub library from C sources ----------------------------------===##
229 all-local:: $(SharedLib)
230 clean-local:: clean-shared
231 install-local:: install-shared
232 uninstall-local:: uninstall-shared
234 $(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
235 $(Echo) "Building $(BuildMode) $(notdir $@)"
236 $(Verb) $(Link) $(SharedLinkOptions) $(LLVMLibsOptions) \
240 -$(Verb) $(RM) -f $(SharedLib)
242 install-shared:: $(SharedLib)
243 $(Echo) "Installing $(BuildMode) $(DestSharedLib)"
244 $(Verb) $(MKDIR) $(PROJ_libocamldir)
245 $(Verb) $(INSTALL) $(SharedLib) $(DestSharedLib)
249 $(Echo) "Uninstalling $(DestSharedLib)"
250 -$(Verb) $(RM) -f $(DestSharedLib)
254 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
256 all-local:: build-deplibs
257 clean-local:: clean-deplibs
258 install-local:: install-deplibs
259 uninstall-local:: uninstall-deplibs
261 build-deplibs: $(OutputLibs)
263 $(OcamlDir)/%.a: $(LibDir)/%.a
266 $(OcamlDir)/%.o: $(LibDir)/%.o
270 $(Verb) $(RM) -f $(OutputLibs)
273 $(Verb) $(MKDIR) $(PROJ_libocamldir)
274 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
275 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
279 $(Verb) $(RM) -f $(DestLibs)
282 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
284 ifneq ($(OcamlHeaders),)
285 all-local:: build-cmis
286 clean-local:: clean-cmis
287 install-local:: install-cmis
288 uninstall-local:: uninstall-cmis
290 build-cmis: $(OutputsCMI)
292 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
293 $(Verb) $(CP) -f $< $@
295 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
296 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
297 $(Verb) $(Compile.CMI) $@ $<
300 -$(Verb) $(RM) -f $(OutputsCMI)
302 # Also install the .mli's (headers) as documentation.
303 install-cmis: $(OutputsCMI) $(OcamlHeaders)
304 $(Verb) $(MKDIR) $(PROJ_libocamldir)
305 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
306 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
307 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
309 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
310 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
311 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
315 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
316 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
317 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
319 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
320 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
321 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
326 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
328 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
329 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
330 $(Verb) $(Compile.CMO) $@ $<
333 all-local:: $(OutputCMA)
334 clean-local:: clean-cma
335 install-local:: install-cma
336 uninstall-local:: uninstall-cma
338 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
339 $(Verb) $(CP) -f $< $@
341 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
342 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
343 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
346 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
348 install-cma:: $(OutputCMA)
349 $(Echo) "Installing $(BuildMode) $(DestCMA)"
350 $(Verb) $(MKDIR) $(PROJ_libocamldir)
351 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
354 $(Echo) "Uninstalling $(DestCMA)"
355 -$(Verb) $(RM) -f $(DestCMA)
358 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
360 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
361 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
364 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
365 $(Verb) $(CP) -f $< $@
367 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
368 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
369 $(Verb) $(Compile.CMX) $@ $<
372 all-local:: $(OutputCMXA) $(OutputsCMX)
373 clean-local:: clean-cmxa
374 install-local:: install-cmxa
375 uninstall-local:: uninstall-cmxa
377 $(OutputCMXA): $(LibraryCMXA)
378 $(Verb) $(CP) -f $< $@
379 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
381 $(LibraryCMXA): $(ObjectsCMX)
382 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
383 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
384 $(Verb) $(RM) -f $(@:.cmxa=.o)
387 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
389 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
390 $(Verb) $(MKDIR) $(PROJ_libocamldir)
391 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
392 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
393 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
394 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
395 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
396 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
397 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
401 $(Echo) "Uninstalling $(DestCMXA)"
402 $(Verb) $(RM) -f $(DestCMXA)
403 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
404 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
405 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
406 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
407 $(RM) -f $(PROJ_libocamldir)/$$i; \
412 ##===- Build executables --------------------------------------------------===##
415 all-local:: $(OutputEXE)
416 clean-local:: clean-exe
418 $(OutputEXE): $(ToolEXE) $(OcamlDir)/.dir
419 $(Verb) $(CP) -f $< $@
422 $(ToolEXE): $(ObjectsCMO) $(OcamlDir)/.dir
423 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
424 $(Verb) $(Archive.EXE) $@ $(ObjectsCMO)
426 $(ToolEXE): $(ObjectsCMX) $(OcamlDir)/.dir
427 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
428 $(Verb) $(Archive.EXE) $@ $(ObjectsCMX)
432 ##===- Generate documentation ---------------------------------------------===##
434 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
435 $(Echo) "Documenting $(notdir $@)"
436 $(Verb) $(OCAMLDOC) -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
438 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
440 ##===- Debugging gunk -----------------------------------------------------===##
441 printvars:: printcamlvars
444 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
445 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
446 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
447 $(Echo) "OCAMLC : " '$(OCAMLC)'
448 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
449 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
450 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
451 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
452 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
453 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
454 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
455 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
456 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
457 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
458 $(Echo) "SharedLib : " '$(SharedLib)'
459 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
460 $(Echo) "OcamlSources2: " '$(OcamlSources2)'
461 $(Echo) "OcamlSources : " '$(OcamlSources)'
462 $(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
463 $(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
464 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
465 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
466 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
467 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
468 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
469 $(Echo) "DestA : " '$(DestA)'
470 $(Echo) "DestCMA : " '$(DestCMA)'
471 $(Echo) "DestCMXA : " '$(DestCMXA)'
472 $(Echo) "DestSharedLib: " '$(DestSharedLib)'
473 $(Echo) "UsedLibs : " '$(UsedLibs)'
474 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
476 .PHONY: printcamlvars build-cmis \
477 clean-a clean-cmis clean-cma clean-cmxa \
478 install-a install-cmis install-cma install-cmxa \
480 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \