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 ifneq ($(ENABLE_OPTIMIZED),1)
76 Compile.CMI := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
77 Compile.CMO := $(strip $(OCAMLC) -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
78 Compile.CMX := $(strip $(OCAMLOPT) -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 $(OCAMLC) -a -dllib -l$(LIBRARYNAME) $(OCAMLDEBUGFLAG) \
93 Archive.CMA := $(strip $(OCAMLC) -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
98 Archive.CMXA := $(strip $(OCAMLOPT) -a $(patsubst %,-cclib %, \
99 $(LLVMLibsOptions) -l$(LIBRARYNAME) \
100 -L$(SharedLibDir) $(OCAMLRPATH)) \
101 $(OCAMLDEBUGFLAG) -o)
103 Archive.CMXA := $(strip $(OCAMLOPT) -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
107 Archive.EXE := $(strip $(OCAMLOPT) -cc $(CXX) $(OCAMLCFLAGS) $(UsedOcamlLibs:%=%.cmxa) $(OCAMLDEBUGFLAG) -o)
109 Archive.EXE := $(strip $(OCAMLC) -cc $(CXX) $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG:%=%.cma) -o)
114 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
118 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
121 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
122 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
124 OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
125 OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
128 ObjectsCMI := $(OcamlSources:%.ml=%.cmi)
129 ObjectsCMO := $(OcamlSources:%.ml=%.cmo)
130 ObjectsCMX := $(OcamlSources:%.ml=%.cmx)
133 LibraryCMA := $(ObjDir)/$(LIBRARYNAME).cma
134 LibraryCMXA := $(ObjDir)/$(LIBRARYNAME).cmxa
138 ToolEXE := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
142 # The .cmo files are the only intermediates; all others are to be installed.
143 OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
144 OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
145 OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)
148 LibraryA := $(OcamlDir)/lib$(LIBRARYNAME).a
149 OutputCMA := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
150 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
154 SharedLib := $(OcamlDir)/dll$(LIBRARYNAME)$(SHLIBEXT)
159 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
161 OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
165 # Installation targets
166 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
169 DestA := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
170 DestCMA := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
171 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
175 DestSharedLib := $(PROJ_libocamldir)/dll$(LIBRARYNAME)$(SHLIBEXT)
178 ##===- Dependencies -------------------------------------------------------===##
179 # Copy the sources into the intermediate directory because older ocamlc doesn't
180 # support -o except when linking (outputs are placed next to inputs).
182 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
183 $(Verb) $(CP) -f $< $@
185 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
186 $(Verb) $(CP) -f $< $@
188 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
191 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
192 $(OcamlDir)/.dir $(ObjDir)/.dir
193 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
195 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
199 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
200 $(OcamlDir)/.dir $(ObjDir)/.dir
201 $(Verb) $(OCAMLDEP) $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
203 -include $(ObjDir)/$(TOOLNAME).ocamldep
206 ##===- Build static library from C sources --------------------------------===##
209 all-local:: $(LibraryA)
210 clean-local:: clean-a
211 install-local:: install-a
212 uninstall-local:: uninstall-a
214 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
215 $(Echo) "Building $(BuildMode) $(notdir $@)"
217 $(Verb) $(Archive) $@ $(ObjectsO)
221 -$(Verb) $(RM) -f $(LibraryA)
223 install-a:: $(LibraryA)
224 $(Echo) "Installing $(BuildMode) $(DestA)"
225 $(Verb) $(MKDIR) $(PROJ_libocamldir)
226 $(Verb) $(INSTALL) $(LibraryA) $(DestA)
230 $(Echo) "Uninstalling $(DestA)"
231 -$(Verb) $(RM) -f $(DestA)
235 ##===- Build stub library from C sources ----------------------------------===##
238 all-local:: $(SharedLib)
239 clean-local:: clean-shared
240 install-local:: install-shared
241 uninstall-local:: uninstall-shared
243 $(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
244 $(Echo) "Building $(BuildMode) $(notdir $@)"
245 $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) $(LLVMLibsOptions) \
249 -$(Verb) $(RM) -f $(SharedLib)
251 install-shared:: $(SharedLib)
252 $(Echo) "Installing $(BuildMode) $(DestSharedLib)"
253 $(Verb) $(MKDIR) $(PROJ_libocamldir)
254 $(Verb) $(INSTALL) $(SharedLib) $(DestSharedLib)
258 $(Echo) "Uninstalling $(DestSharedLib)"
259 -$(Verb) $(RM) -f $(DestSharedLib)
263 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
265 all-local:: build-deplibs
266 clean-local:: clean-deplibs
267 install-local:: install-deplibs
268 uninstall-local:: uninstall-deplibs
270 build-deplibs: $(OutputLibs)
272 $(OcamlDir)/%.a: $(LibDir)/%.a
275 $(OcamlDir)/%.o: $(LibDir)/%.o
279 $(Verb) $(RM) -f $(OutputLibs)
282 $(Verb) $(MKDIR) $(PROJ_libocamldir)
283 $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
284 ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
288 $(Verb) $(RM) -f $(DestLibs)
291 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
293 ifneq ($(OcamlHeaders),)
294 all-local:: build-cmis
295 clean-local:: clean-cmis
296 install-local:: install-cmis
297 uninstall-local:: uninstall-cmis
299 build-cmis: $(OutputsCMI)
301 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
302 $(Verb) $(CP) -f $< $@
304 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
305 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
306 $(Verb) $(Compile.CMI) $@ $<
309 -$(Verb) $(RM) -f $(OutputsCMI)
311 # Also install the .mli's (headers) as documentation.
312 install-cmis: $(OutputsCMI) $(OcamlHeaders)
313 $(Verb) $(MKDIR) $(PROJ_libocamldir)
314 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
315 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
316 $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
318 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
319 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
320 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
324 $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
325 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
326 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
328 $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
329 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
330 $(RM) -f "$(PROJ_libocamldir)/$$i"; \
335 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
337 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
338 $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
339 $(Verb) $(Compile.CMO) $@ $<
342 all-local:: $(OutputCMA)
343 clean-local:: clean-cma
344 install-local:: install-cma
345 uninstall-local:: uninstall-cma
347 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
348 $(Verb) $(CP) -f $< $@
350 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
351 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
352 $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
355 $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
357 install-cma:: $(OutputCMA)
358 $(Echo) "Installing $(BuildMode) $(DestCMA)"
359 $(Verb) $(MKDIR) $(PROJ_libocamldir)
360 $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
363 $(Echo) "Uninstalling $(DestCMA)"
364 -$(Verb) $(RM) -f $(DestCMA)
367 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
369 # The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
370 # If unavailable, 'configure' will not define OCAMLOPT in Makefile.config.
373 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
374 $(Verb) $(CP) -f $< $@
376 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
377 $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
378 $(Verb) $(Compile.CMX) $@ $<
381 all-local:: $(OutputCMXA) $(OutputsCMX)
382 clean-local:: clean-cmxa
383 install-local:: install-cmxa
384 uninstall-local:: uninstall-cmxa
386 $(OutputCMXA): $(LibraryCMXA)
387 $(Verb) $(CP) -f $< $@
388 $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
390 $(LibraryCMXA): $(ObjectsCMX)
391 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
392 $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
393 $(Verb) $(RM) -f $(@:.cmxa=.o)
396 $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
398 install-cmxa:: $(OutputCMXA) $(OutputsCMX)
399 $(Verb) $(MKDIR) $(PROJ_libocamldir)
400 $(Echo) "Installing $(BuildMode) $(DestCMXA)"
401 $(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
402 $(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
403 $(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
404 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
405 $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
406 $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
410 $(Echo) "Uninstalling $(DestCMXA)"
411 $(Verb) $(RM) -f $(DestCMXA)
412 $(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
413 $(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
414 $(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
415 $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
416 $(RM) -f $(PROJ_libocamldir)/$$i; \
421 ##===- Build executables --------------------------------------------------===##
424 all-local:: $(OutputEXE)
425 clean-local:: clean-exe
427 $(OutputEXE): $(ToolEXE) $(OcamlDir)/.dir
428 $(Verb) $(CP) -f $< $@
431 $(ToolEXE): $(ObjectsCMO) $(OcamlDir)/.dir
432 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
433 $(Verb) $(Archive.EXE) $@ $(ObjectsCMO)
435 $(ToolEXE): $(ObjectsCMX) $(OcamlDir)/.dir
436 $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
437 $(Verb) $(Archive.EXE) $@ $(ObjectsCMX)
441 ##===- Generate documentation ---------------------------------------------===##
443 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
444 $(Echo) "Documenting $(notdir $@)"
445 $(Verb) $(OCAMLDOC) -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
447 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
449 ##===- Debugging gunk -----------------------------------------------------===##
450 printvars:: printcamlvars
453 $(Echo) "LLVM_CONFIG : " '$(LLVM_CONFIG)'
454 $(Echo) "OCAMLCFLAGS : " '$(OCAMLCFLAGS)'
455 $(Echo) "OCAMLAFLAGS : " '$(OCAMLAFLAGS)'
456 $(Echo) "OCAMLC : " '$(OCAMLC)'
457 $(Echo) "OCAMLOPT : " '$(OCAMLOPT)'
458 $(Echo) "OCAMLDEP : " '$(OCAMLDEP)'
459 $(Echo) "Compile.CMI : " '$(Compile.CMI)'
460 $(Echo) "Compile.CMO : " '$(Compile.CMO)'
461 $(Echo) "Archive.CMA : " '$(Archive.CMA)'
462 $(Echo) "Compile.CMX : " '$(Compile.CMX)'
463 $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
464 $(Echo) "CAML_LIBDIR : " '$(CAML_LIBDIR)'
465 $(Echo) "LibraryCMA : " '$(LibraryCMA)'
466 $(Echo) "LibraryCMXA : " '$(LibraryCMXA)'
467 $(Echo) "SharedLib : " '$(SharedLib)'
468 $(Echo) "OcamlSources1: " '$(OcamlSources1)'
469 $(Echo) "OcamlSources2: " '$(OcamlSources2)'
470 $(Echo) "OcamlSources : " '$(OcamlSources)'
471 $(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
472 $(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
473 $(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
474 $(Echo) "ObjectsCMI : " '$(ObjectsCMI)'
475 $(Echo) "ObjectsCMO : " '$(ObjectsCMO)'
476 $(Echo) "ObjectsCMX : " '$(ObjectsCMX)'
477 $(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
478 $(Echo) "DestA : " '$(DestA)'
479 $(Echo) "DestCMA : " '$(DestCMA)'
480 $(Echo) "DestCMXA : " '$(DestCMXA)'
481 $(Echo) "DestSharedLib: " '$(DestSharedLib)'
482 $(Echo) "UsedLibs : " '$(UsedLibs)'
483 $(Echo) "UsedLibNames : " '$(UsedLibNames)'
485 .PHONY: printcamlvars build-cmis \
486 clean-a clean-cmis clean-cma clean-cmxa \
487 install-a install-cmis install-cma install-cmxa \
489 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \