[OCaml] Avoid embedding absolute paths into executables.
[oota-llvm.git] / bindings / ocaml / Makefile.ocaml
1 ##===- bindings/ocaml/Makefile.ocaml -----------------------*- Makefile -*-===##
2 #
3 #                     The LLVM Compiler Infrastructure
4 #
5 # This file is distributed under the University of Illinois Open Source
6 # License. See LICENSE.TXT for details.
7 #
8 ##===----------------------------------------------------------------------===##
9 #
10 # An OCaml library is a unique project type in the context of LLVM, so rules are
11 # here rather than in Makefile.rules.
12 #
13 # Reference materials on installing OCaml libraries:
14 #
15 #   https://fedoraproject.org/wiki/Packaging/OCaml
16 #   http://pkg-ocaml-maint.alioth.debian.org/ocaml_packaging_policy.txt
17 #
18 ##===----------------------------------------------------------------------===##
19
20 include $(LEVEL)/Makefile.config
21
22 # We have our own rules for building static libraries.
23 NO_BUILD_ARCHIVE = 1
24
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)"
28
29 ifeq ($(ENABLE_SHARED),1)
30 LINK_COMPONENTS := all
31 endif
32
33 include $(LEVEL)/Makefile.common
34
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
39
40 # Info from llvm-config and similar
41 ifndef IS_CLEANING_TARGET
42 ifdef UsedComponents
43 UsedLibs = $(shell $(LLVM_CONFIG) --libs --system-libs $(UsedComponents))
44 UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
45 endif
46 endif
47
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
53 #    from toplevels.
54 ifneq ($(ObjectsO),)
55 ifeq ($(ENABLE_SHARED),1)
56 OCAMLSTUBS     := 1
57 OCAMLSTUBFLAGS := $(patsubst %,-cclib %, $(LLVMLibsOptions) -l$(LIBRARYNAME))
58 endif
59 endif
60
61 # Avoid the need for LD_LIBRARY_PATH
62 ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
63 ifneq ($(HOST_OS),Darwin)
64 OCAMLRPATH     := $(RPATH) -Wl,'$$ORIGIN/../../lib'
65 endif
66 endif
67
68 # Tools
69 OCAMLCFLAGS += -I $(OcamlDir) $(addprefix -package ,$(FindlibPackages))
70
71 ifndef IS_CLEANING_TARGET
72 ifneq ($(ObjectsO),)
73 OCAMLAFLAGS += $(patsubst %,-cclib %, \
74                  $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
75                                           $(shell $(LLVM_CONFIG) --ldflags)) \
76                                           $(UsedLibs) $(ExtraLibs))
77 else
78 OCAMLAFLAGS += $(patsubst %,-cclib %, \
79                  $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
80                                           $(UsedLibs) $(ExtraLibs))
81 endif
82 endif
83
84 ifneq ($(ENABLE_OPTIMIZED),1)
85   OCAMLDEBUGFLAG := -g
86 endif
87
88 Compile.CMI  := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
89 Compile.CMO  := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
90 Compile.CMX  := $(strip $(OCAMLFIND) opt -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
91
92 ifdef OCAMLSTUBS
93 Archive.CMA  := $(strip $(OCAMLFIND) c -a -dllib -l$(LIBRARYNAME) $(OCAMLDEBUGFLAG) \
94                                        -o)
95 else
96 Archive.CMA  := $(strip $(OCAMLFIND) c -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
97                                        -o)
98 endif
99
100 ifdef OCAMLSTUBS
101 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLSTUBFLAGS) $(OCAMLDEBUGFLAG) -o)
102 else
103 Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
104 endif
105
106 # Source files
107 ifndef OcamlSources1
108 OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
109 endif
110
111 ifndef OcamlHeaders1
112 OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
113 endif
114
115 OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
116 OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))
117
118 OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
119 OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
120
121 # Intermediate files
122 ObjectsCMI   := $(OcamlSources:%.ml=%.cmi)
123 ObjectsCMO   := $(OcamlSources:%.ml=%.cmo)
124 ObjectsCMX   := $(OcamlSources:%.ml=%.cmx)
125
126 ifdef LIBRARYNAME
127 LibraryCMA   := $(ObjDir)/$(LIBRARYNAME).cma
128 LibraryCMXA  := $(ObjDir)/$(LIBRARYNAME).cmxa
129 endif
130
131 ifdef TOOLNAME
132 ToolEXE      := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
133 endif
134
135 # Output files
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)/%)
140
141 ifdef LIBRARYNAME
142 LibraryA   := $(OcamlDir)/lib$(LIBRARYNAME).a
143 OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
144 OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
145 endif
146
147 ifdef OCAMLSTUBS
148 SharedLib := $(OcamlDir)/dll$(LIBRARYNAME)$(SHLIBEXT)
149 endif
150
151 ifdef TOOLNAME
152 ifdef EXAMPLE_TOOL
153 OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
154 else
155 OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
156 endif
157 endif
158
159 # Installation targets
160 DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)
161
162 ifdef LIBRARYNAME
163 DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
164 DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
165 DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
166 endif
167
168 ifdef OCAMLSTUBS
169 DestSharedLib := $(PROJ_libocamldir)/dll$(LIBRARYNAME)$(SHLIBEXT)
170 endif
171
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).
175
176 $(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
177         $(Verb) $(CP) -f $< $@
178
179 $(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
180         $(Verb) $(CP) -f $< $@
181
182 $(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)
183
184 ifdef LIBRARYNAME
185 $(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
186                                    $(OcamlDir)/.dir $(ObjDir)/.dir
187         $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
188
189 -include $(ObjDir)/$(LIBRARYNAME).ocamldep
190 endif
191
192 ifdef TOOLNAME
193 $(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
194                                 $(OcamlDir)/.dir $(ObjDir)/.dir
195         $(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@
196
197 -include $(ObjDir)/$(TOOLNAME).ocamldep
198 endif
199
200 ##===- Build static library from C sources --------------------------------===##
201
202 ifdef LibraryA
203 all-local:: $(LibraryA)
204 clean-local:: clean-a
205 install-local:: install-a
206 uninstall-local:: uninstall-a
207
208 $(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
209         $(Echo) "Building $(BuildMode) $(notdir $@)"
210         -$(Verb) $(RM) -f $@
211         $(Verb) $(Archive) $@ $(ObjectsO)
212         $(Verb) $(Ranlib) $@
213
214 clean-a::
215         -$(Verb) $(RM) -f $(LibraryA)
216
217 install-a:: $(LibraryA)
218         $(Echo) "Installing $(BuildMode) $(DestA)"
219         $(Verb) $(MKDIR) $(PROJ_libocamldir)
220         $(Verb) $(INSTALL) $(LibraryA) $(DestA)
221         $(Verb)
222
223 uninstall-a::
224         $(Echo) "Uninstalling $(DestA)"
225         -$(Verb) $(RM) -f $(DestA)
226 endif
227
228
229 ##===- Build stub library from C sources ----------------------------------===##
230
231 ifdef SharedLib
232 all-local:: $(SharedLib)
233 clean-local:: clean-shared
234 install-local:: install-shared
235 uninstall-local:: uninstall-shared
236
237 $(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
238         $(Echo) "Building $(BuildMode) $(notdir $@)"
239         $(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) $(LLVMLibsOptions) \
240                         -o $@ $(ObjectsO)
241
242 clean-shared::
243         -$(Verb) $(RM) -f $(SharedLib)
244
245 install-shared:: $(SharedLib)
246         $(Echo) "Installing $(BuildMode) $(DestSharedLib)"
247         $(Verb) $(MKDIR) $(PROJ_libocamldir)
248         $(Verb) $(INSTALL) $(SharedLib) $(DestSharedLib)
249         $(Verb)
250
251 uninstall-shared::
252         $(Echo) "Uninstalling $(DestSharedLib)"
253         -$(Verb) $(RM) -f $(DestSharedLib)
254 endif
255
256
257 ##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##
258
259 all-local:: build-deplibs
260 clean-local:: clean-deplibs
261 install-local:: install-deplibs
262 uninstall-local:: uninstall-deplibs
263
264 build-deplibs: $(OutputLibs)
265
266 $(OcamlDir)/%.a: $(LibDir)/%.a
267         $(Verb) ln -sf $< $@
268
269 $(OcamlDir)/%.o: $(LibDir)/%.o
270         $(Verb) ln -sf $< $@
271
272 clean-deplibs:
273         $(Verb) $(RM) -f $(OutputLibs)
274
275 install-deplibs:
276         $(Verb) $(MKDIR) $(PROJ_libocamldir)
277         $(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
278           ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
279         done
280
281 uninstall-deplibs:
282         $(Verb) $(RM) -f $(DestLibs)
283
284
285 ##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##
286
287 ifneq ($(OcamlHeaders),)
288 all-local:: build-cmis
289 clean-local:: clean-cmis
290 install-local:: install-cmis
291 uninstall-local:: uninstall-cmis
292
293 build-cmis: $(OutputsCMI)
294
295 $(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
296         $(Verb) $(CP) -f $< $@
297
298 $(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
299         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
300         $(Verb) $(Compile.CMI) $@ $<
301
302 clean-cmis::
303         -$(Verb) $(RM) -f $(OutputsCMI)
304
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"; \
311         done
312         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
313           $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
314           $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
315         done
316
317 uninstall-cmis::
318         $(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
319           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
320           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
321         done
322         $(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
323           $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
324           $(RM) -f "$(PROJ_libocamldir)/$$i"; \
325         done
326 endif
327
328
329 ##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##
330
331 $(ObjDir)/%.cmo: $(ObjDir)/%.ml
332         $(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
333         $(Verb) $(Compile.CMO) $@ $<
334
335 ifdef LIBRARYNAME
336 all-local:: $(OutputCMA)
337 clean-local:: clean-cma
338 install-local:: install-cma
339 uninstall-local:: uninstall-cma
340
341 $(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
342         $(Verb) $(CP) -f $< $@
343
344 $(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
345         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
346         $(Verb) $(Archive.CMA) $@ $(ObjectsCMO)
347
348 clean-cma::
349         $(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)
350
351 install-cma:: $(OutputCMA)
352         $(Echo) "Installing $(BuildMode) $(DestCMA)"
353         $(Verb) $(MKDIR) $(PROJ_libocamldir)
354         $(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"
355
356 uninstall-cma::
357         $(Echo) "Uninstalling $(DestCMA)"
358         -$(Verb) $(RM) -f $(DestCMA)
359 endif
360
361 ##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##
362
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)
366
367 $(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
368         $(Verb) $(CP) -f $< $@
369
370 $(ObjDir)/%.cmx: $(ObjDir)/%.ml
371         $(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
372         $(Verb) $(Compile.CMX) $@ $<
373
374 ifdef LIBRARYNAME
375 all-local:: $(OutputCMXA) $(OutputsCMX)
376 clean-local:: clean-cmxa
377 install-local:: install-cmxa
378 uninstall-local:: uninstall-cmxa
379
380 $(OutputCMXA): $(LibraryCMXA)
381         $(Verb) $(CP) -f $< $@
382         $(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)
383
384 $(LibraryCMXA): $(ObjectsCMX)
385         $(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
386         $(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
387         $(Verb) $(RM) -f $(@:.cmxa=.o)
388
389 clean-cmxa::
390         $(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)
391
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"; \
401         done
402
403 uninstall-cmxa::
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; \
411         done
412 endif
413 endif
414
415 ##===- Generate documentation ---------------------------------------------===##
416
417 $(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
418         $(Echo) "Documenting $(notdir $@)"
419         $(Verb) $(OCAMLFIND) doc -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)
420
421 ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc
422
423 ##===- Debugging gunk -----------------------------------------------------===##
424 printvars:: printcamlvars
425
426 printcamlvars::
427         $(Echo) "LLVM_CONFIG  : " '$(LLVM_CONFIG)'
428         $(Echo) "OCAMLCFLAGS  : " '$(OCAMLCFLAGS)'
429         $(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
430         $(Echo) "OCAMLRPATH   : " '$(OCAMLRPATH)'
431         $(Echo) "OCAMLSTUBS   : " '$(OCAMLSTUBS)'
432         $(Echo) "OCAMLSTUBFLAGS : " '$(OCAMLSTUBFLAGS)'
433         $(Echo) "OCAMLFIND    : " '$(OCAMLFIND)'
434         $(Echo) "Compile.CMI  : " '$(Compile.CMI)'
435         $(Echo) "Compile.CMO  : " '$(Compile.CMO)'
436         $(Echo) "Archive.CMA  : " '$(Archive.CMA)'
437         $(Echo) "Compile.CMX  : " '$(Compile.CMX)'
438         $(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
439         $(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
440         $(Echo) "LibraryA     : " '$(LibraryA)'
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)'
461
462 .PHONY: printcamlvars   build-cmis \
463             clean-a     clean-cmis     clean-cma     clean-cmxa \
464           install-a   install-cmis   install-cma   install-cmxa \
465           install-exe \
466                 uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \
467                 uninstall-exe