aboutsummaryrefslogtreecommitdiffstats
path: root/bindings/ocaml/Makefile.ocaml
blob: 5e00cf59c4c28145543ac22ba0e2f33856f8d517 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
##===- bindings/ocaml/Makefile.ocaml -----------------------*- Makefile -*-===##
#
#                     The LLVM Compiler Infrastructure
#
# This file is distributed under the University of Illinois Open Source
# License. See LICENSE.TXT for details.
#
##===----------------------------------------------------------------------===##
#
# An OCaml library is a unique project type in the context of LLVM, so rules are
# here rather than in Makefile.rules.
#
# Reference materials on installing OCaml libraries:
#
#   https://fedoraproject.org/wiki/Packaging/OCaml
#   http://pkg-ocaml-maint.alioth.debian.org/ocaml_packaging_policy.txt
#
##===----------------------------------------------------------------------===##

include $(LEVEL)/Makefile.config

# We have our own rules for building static libraries.
NO_BUILD_ARCHIVE = 1

# CFLAGS needs to be set before Makefile.rules is included.
CXX.Flags += -I"$(shell $(OCAMLFIND) c -where)"
C.Flags += -I"$(shell $(OCAMLFIND) c -where)"

ifeq ($(ENABLE_SHARED),1)
LINK_COMPONENTS := all
endif

include $(LEVEL)/Makefile.common

# Intentionally ignore PROJ_prefix here. We want the ocaml stdlib. However, the
# user can override this with OCAML_LIBDIR or configure --with-ocaml-libdir=.
PROJ_libocamldir := $(DESTDIR)$(OCAML_LIBDIR)
OcamlDir := $(LibDir)/ocaml

# Info from llvm-config and similar
ifndef IS_CLEANING_TARGET
ifdef UsedComponents
UsedLibs = $(shell $(LLVM_CONFIG) --libs --system-libs $(UsedComponents))
UsedLibNames = $(shell $(LLVM_CONFIG) --libnames $(UsedComponents))
endif
endif

# How do we link OCaml executables with LLVM?
# 1) If this is a --enable-shared build, build stub libraries. This also allows
#    to use LLVM from toplevels.
# 2) If this is a --disable-shared build, embed ocamlc options for building
#    a custom runtime and a static executable. It is not possible to use LLVM
#    from toplevels.
ifneq ($(ObjectsO),)
ifeq ($(ENABLE_SHARED),1)
OCAMLSTUBS     := 1
OCAMLSTUBFLAGS := $(patsubst %,-cclib %, $(LLVMLibsOptions) -l$(LIBRARYNAME))
endif
endif

# Avoid the need for LD_LIBRARY_PATH
ifneq ($(HOST_OS), $(filter $(HOST_OS), Cygwin MingW))
ifneq ($(HOST_OS),Darwin)
OCAMLRPATH     := $(RPATH) -Wl,'$$ORIGIN/../../lib'
endif
endif

# Tools
OCAMLCFLAGS += -I $(OcamlDir) $(addprefix -package ,$(FindlibPackages))

ifndef IS_CLEANING_TARGET
ifneq ($(ObjectsO),)
OCAMLAFLAGS += $(patsubst %,-cclib %, \
                 $(filter-out -L$(LibDir),-l$(LIBRARYNAME) \
                                          $(shell $(LLVM_CONFIG) --ldflags)) \
                                          $(UsedLibs) $(ExtraLibs))
else
OCAMLAFLAGS += $(patsubst %,-cclib %, \
                 $(filter-out -L$(LibDir),$(shell $(LLVM_CONFIG) --ldflags)) \
                                          $(UsedLibs) $(ExtraLibs))
endif
endif

ifneq ($(DEBUG_SYMBOLS),1)
  OCAMLDEBUGFLAG := -g
endif

Compile.CMI  := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
Compile.CMO  := $(strip $(OCAMLFIND) c -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)
Compile.CMX  := $(strip $(OCAMLFIND) opt -c $(OCAMLCFLAGS) $(OCAMLDEBUGFLAG) -o)

ifdef OCAMLSTUBS
# -dllib is engaged with ocamlc builds, $(OCAMLSTUBFLAGS) in ocamlc -custom builds.
Archive.CMA  := $(strip $(OCAMLFIND) c -a -dllib -l$(LIBRARYNAME) $(OCAMLSTUBFLAGS) \
																			 $(OCAMLDEBUGFLAG) -o)
else
Archive.CMA  := $(strip $(OCAMLFIND) c -a -custom $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) \
                                       -o)
endif

ifdef OCAMLSTUBS
Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLSTUBFLAGS) $(OCAMLDEBUGFLAG) -o)
else
Archive.CMXA := $(strip $(OCAMLFIND) opt -a $(OCAMLAFLAGS) $(OCAMLDEBUGFLAG) -o)
endif

# Source files
ifndef OcamlSources1
OcamlSources1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.ml))
endif

ifndef OcamlHeaders1
OcamlHeaders1 := $(sort $(wildcard $(PROJ_SRC_DIR)/*.mli))
endif

OcamlSources2 := $(filter-out $(ExcludeSources),$(OcamlSources1))
OcamlHeaders2 := $(filter-out $(ExcludeHeaders),$(OcamlHeaders1))

OcamlSources := $(OcamlSources2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)
OcamlHeaders := $(OcamlHeaders2:$(PROJ_SRC_DIR)/%=$(ObjDir)/%)

# Intermediate files
ObjectsCMI   := $(OcamlSources:%.ml=%.cmi)
ObjectsCMO   := $(OcamlSources:%.ml=%.cmo)
ObjectsCMX   := $(OcamlSources:%.ml=%.cmx)

ifdef LIBRARYNAME
LibraryCMA   := $(ObjDir)/$(LIBRARYNAME).cma
LibraryCMXA  := $(ObjDir)/$(LIBRARYNAME).cmxa
endif

ifdef TOOLNAME
ToolEXE      := $(ObjDir)/$(TOOLNAME)$(EXEEXT)
endif

# Output files
#   The .cmo files are the only intermediates; all others are to be installed.
OutputsCMI := $(ObjectsCMI:$(ObjDir)/%.cmi=$(OcamlDir)/%.cmi)
OutputsCMX := $(ObjectsCMX:$(ObjDir)/%.cmx=$(OcamlDir)/%.cmx)
OutputLibs := $(UsedLibNames:%=$(OcamlDir)/%)

ifdef LIBRARYNAME
LibraryA   := $(OcamlDir)/lib$(LIBRARYNAME).a
OutputCMA  := $(LibraryCMA:$(ObjDir)/%.cma=$(OcamlDir)/%.cma)
OutputCMXA := $(LibraryCMXA:$(ObjDir)/%.cmxa=$(OcamlDir)/%.cmxa)
endif

ifdef OCAMLSTUBS
SharedLib := $(OcamlDir)/dll$(LIBRARYNAME)$(SHLIBEXT)
endif

ifdef TOOLNAME
ifdef EXAMPLE_TOOL
OutputEXE := $(ExmplDir)/$(strip $(TOOLNAME))$(EXEEXT)
else
OutputEXE := $(ToolDir)/$(strip $(TOOLNAME))$(EXEEXT)
endif
endif

# Installation targets
DestLibs := $(UsedLibNames:%=$(PROJ_libocamldir)/%)

ifdef LIBRARYNAME
DestA    := $(PROJ_libocamldir)/lib$(LIBRARYNAME).a
DestCMA  := $(PROJ_libocamldir)/$(LIBRARYNAME).cma
DestCMXA := $(PROJ_libocamldir)/$(LIBRARYNAME).cmxa
endif

ifdef OCAMLSTUBS
DestSharedLib := $(PROJ_libocamldir)/dll$(LIBRARYNAME)$(SHLIBEXT)
endif

##===- Dependencies -------------------------------------------------------===##
# Copy the sources into the intermediate directory because older ocamlc doesn't
# support -o except when linking (outputs are placed next to inputs).

$(ObjDir)/%.mli: $(PROJ_SRC_DIR)/%.mli $(ObjDir)/.dir
	$(Verb) $(CP) -f $< $@

$(ObjDir)/%.ml: $(PROJ_SRC_DIR)/%.ml $(ObjDir)/.dir
	$(Verb) $(CP) -f $< $@

$(ObjectsCMI): $(UsedOcamlInterfaces:%=$(OcamlDir)/%.cmi)

ifdef LIBRARYNAME
$(ObjDir)/$(LIBRARYNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
                                   $(OcamlDir)/.dir $(ObjDir)/.dir
	$(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@

-include $(ObjDir)/$(LIBRARYNAME).ocamldep
endif

ifdef TOOLNAME
$(ObjDir)/$(TOOLNAME).ocamldep: $(OcamlSources) $(OcamlHeaders) \
                                $(OcamlDir)/.dir $(ObjDir)/.dir
	$(Verb) $(OCAMLFIND) dep $(OCAMLCFLAGS) $(OcamlSources) $(OcamlHeaders) > $@

-include $(ObjDir)/$(TOOLNAME).ocamldep
endif

##===- Build static library from C sources --------------------------------===##

ifdef LibraryA
all-local:: $(LibraryA)
clean-local:: clean-a
install-local:: install-a
uninstall-local:: uninstall-a

$(LibraryA): $(ObjectsO) $(OcamlDir)/.dir
	$(Echo) "Building $(BuildMode) $(notdir $@)"
	-$(Verb) $(RM) -f $@
	$(Verb) $(Archive) $@ $(ObjectsO)
	$(Verb) $(Ranlib) $@

clean-a::
	-$(Verb) $(RM) -f $(LibraryA)

install-a:: $(LibraryA)
	$(Echo) "Installing $(BuildMode) $(DestA)"
	$(Verb) $(MKDIR) $(PROJ_libocamldir)
	$(Verb) $(INSTALL) $(LibraryA) $(DestA)
	$(Verb)

uninstall-a::
	$(Echo) "Uninstalling $(DestA)"
	-$(Verb) $(RM) -f $(DestA)
endif


##===- Build stub library from C sources ----------------------------------===##

ifdef SharedLib
all-local:: $(SharedLib)
clean-local:: clean-shared
install-local:: install-shared
uninstall-local:: uninstall-shared

$(SharedLib): $(ObjectsO) $(OcamlDir)/.dir
	$(Echo) "Building $(BuildMode) $(notdir $@)"
	$(Verb) $(Link) $(SharedLinkOptions) $(OCAMLRPATH) $(LLVMLibsOptions) \
			-o $@ $(ObjectsO)

clean-shared::
	-$(Verb) $(RM) -f $(SharedLib)

install-shared:: $(SharedLib)
	$(Echo) "Installing $(BuildMode) $(DestSharedLib)"
	$(Verb) $(MKDIR) $(PROJ_libocamldir)
	$(Verb) $(INSTALL) $(SharedLib) $(DestSharedLib)
	$(Verb)

uninstall-shared::
	$(Echo) "Uninstalling $(DestSharedLib)"
	-$(Verb) $(RM) -f $(DestSharedLib)
endif


##===- Deposit dependent libraries adjacent to Ocaml libs -----------------===##

all-local:: build-deplibs
clean-local:: clean-deplibs
install-local:: install-deplibs
uninstall-local:: uninstall-deplibs

build-deplibs: $(OutputLibs)

$(OcamlDir)/%.a: $(LibDir)/%.a
	$(Verb) ln -sf $< $@

$(OcamlDir)/%.o: $(LibDir)/%.o
	$(Verb) ln -sf $< $@

clean-deplibs:
	$(Verb) $(RM) -f $(OutputLibs)

install-deplibs:
	$(Verb) $(MKDIR) $(PROJ_libocamldir)
	$(Verb) for i in $(DestLibs:$(PROJ_libocamldir)/%=%); do \
	  ln -sf "$(PROJ_libdir)/$$i" "$(PROJ_libocamldir)/$$i"; \
	done

uninstall-deplibs:
	$(Verb) $(RM) -f $(DestLibs)


##===- Build ocaml interfaces (.mli's -> .cmi's) --------------------------===##

ifneq ($(OcamlHeaders),)
all-local:: build-cmis
clean-local:: clean-cmis
install-local:: install-cmis
uninstall-local:: uninstall-cmis

build-cmis: $(OutputsCMI)

$(OcamlDir)/%.cmi: $(ObjDir)/%.cmi $(OcamlDir)/.dir
	$(Verb) $(CP) -f $< $@

$(ObjDir)/%.cmi: $(ObjDir)/%.mli $(ObjDir)/.dir
	$(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
	$(Verb) $(Compile.CMI) $@ $<

clean-cmis::
	-$(Verb) $(RM) -f $(OutputsCMI)

# Also install the .mli's (headers) as documentation.
install-cmis: $(OutputsCMI) $(OcamlHeaders)
	$(Verb) $(MKDIR) $(PROJ_libocamldir)
	$(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
	  $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
	  $(DataInstall) $(ObjDir)/$$i "$(PROJ_libocamldir)/$$i"; \
	done
	$(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
	  $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
	  $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
	done

uninstall-cmis::
	$(Verb) for i in $(OutputsCMI:$(OcamlDir)/%=%); do \
	  $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
	  $(RM) -f "$(PROJ_libocamldir)/$$i"; \
	done
	$(Verb) for i in $(OcamlHeaders:$(ObjDir)/%=%); do \
	  $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
	  $(RM) -f "$(PROJ_libocamldir)/$$i"; \
	done
endif


##===- Build ocaml bytecode archive (.ml's -> .cmo's -> .cma) -------------===##

$(ObjDir)/%.cmo: $(ObjDir)/%.ml
	$(Echo) "Compiling $(notdir $<) for $(BuildMode) build"
	$(Verb) $(Compile.CMO) $@ $<

ifdef LIBRARYNAME
all-local:: $(OutputCMA)
clean-local:: clean-cma
install-local:: install-cma
uninstall-local:: uninstall-cma

$(OutputCMA): $(LibraryCMA) $(OcamlDir)/.dir
	$(Verb) $(CP) -f $< $@

$(LibraryCMA): $(ObjectsCMO) $(OcamlDir)/.dir
	$(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
	$(Verb) $(Archive.CMA) $@ $(ObjectsCMO)

clean-cma::
	$(Verb) $(RM) -f $(OutputCMA) $(UsedLibNames:%=$(OcamlDir)/%)

install-cma:: $(OutputCMA)
	$(Echo) "Installing $(BuildMode) $(DestCMA)"
	$(Verb) $(MKDIR) $(PROJ_libocamldir)
	$(Verb) $(DataInstall) $(OutputCMA) "$(DestCMA)"

uninstall-cma::
	$(Echo) "Uninstalling $(DestCMA)"
	-$(Verb) $(RM) -f $(DestCMA)
endif

##===- Build optimized ocaml archive (.ml's -> .cmx's -> .cmxa, .a) -------===##

# The ocamlopt compiler is supported on a set of targets disjoint from LLVM's.
# If unavailable, 'configure' will set HAVE_OCAMLOPT to 0 in Makefile.config.
ifeq ($(HAVE_OCAMLOPT),1)

$(OcamlDir)/%.cmx: $(ObjDir)/%.cmx
	$(Verb) $(CP) -f $< $@

$(ObjDir)/%.cmx: $(ObjDir)/%.ml
	$(Echo) "Compiling optimized $(notdir $<) for $(BuildMode) build"
	$(Verb) $(Compile.CMX) $@ $<

ifdef LIBRARYNAME
all-local:: $(OutputCMXA) $(OutputsCMX)
clean-local:: clean-cmxa
install-local:: install-cmxa
uninstall-local:: uninstall-cmxa

$(OutputCMXA): $(LibraryCMXA)
	$(Verb) $(CP) -f $< $@
	$(Verb) $(CP) -f $(<:.cmxa=.a) $(@:.cmxa=.a)

$(LibraryCMXA): $(ObjectsCMX)
	$(Echo) "Archiving $(notdir $@) for $(BuildMode) build"
	$(Verb) $(Archive.CMXA) $@ $(ObjectsCMX)
	$(Verb) $(RM) -f $(@:.cmxa=.o)

clean-cmxa::
	$(Verb) $(RM) -f $(OutputCMXA) $(OutputCMXA:.cmxa=.a) $(OutputsCMX)

install-cmxa:: $(OutputCMXA) $(OutputsCMX)
	$(Verb) $(MKDIR) $(PROJ_libocamldir)
	$(Echo) "Installing $(BuildMode) $(DestCMXA)"
	$(Verb) $(DataInstall) $(OutputCMXA) $(DestCMXA)
	$(Echo) "Installing $(BuildMode) $(DestCMXA:.cmxa=.a)"
	$(Verb) $(DataInstall) $(OutputCMXA:.cmxa=.a) $(DestCMXA:.cmxa=.a)
	$(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
	  $(EchoCmd) "Installing $(BuildMode) $(PROJ_libocamldir)/$$i"; \
	  $(DataInstall) $(OcamlDir)/$$i "$(PROJ_libocamldir)/$$i"; \
	done

uninstall-cmxa::
	$(Echo) "Uninstalling $(DestCMXA)"
	$(Verb) $(RM) -f $(DestCMXA)
	$(Echo) "Uninstalling $(DestCMXA:.cmxa=.a)"
	$(Verb) $(RM) -f $(DestCMXA:.cmxa=.a)
	$(Verb) for i in $(OutputsCMX:$(OcamlDir)/%=%); do \
	  $(EchoCmd) "Uninstalling $(PROJ_libocamldir)/$$i"; \
	  $(RM) -f $(PROJ_libocamldir)/$$i; \
	done
endif
endif

##===- Generate documentation ---------------------------------------------===##

$(ObjDir)/$(LIBRARYNAME).odoc: $(ObjectsCMI)
	$(Echo) "Documenting $(notdir $@)"
	$(Verb) $(OCAMLFIND) doc -I $(ObjDir) -I $(OcamlDir) -dump $@ $(OcamlHeaders)

ocamldoc: $(ObjDir)/$(LIBRARYNAME).odoc

##===- Debugging gunk -----------------------------------------------------===##
printvars:: printcamlvars

printcamlvars::
	$(Echo) "LLVM_CONFIG  : " '$(LLVM_CONFIG)'
	$(Echo) "OCAMLCFLAGS  : " '$(OCAMLCFLAGS)'
	$(Echo) "OCAMLAFLAGS  : " '$(OCAMLAFLAGS)'
	$(Echo) "OCAMLRPATH   : " '$(OCAMLRPATH)'
	$(Echo) "OCAMLSTUBS   : " '$(OCAMLSTUBS)'
	$(Echo) "OCAMLSTUBFLAGS : " '$(OCAMLSTUBFLAGS)'
	$(Echo) "OCAMLFIND    : " '$(OCAMLFIND)'
	$(Echo) "Compile.CMI  : " '$(Compile.CMI)'
	$(Echo) "Compile.CMO  : " '$(Compile.CMO)'
	$(Echo) "Archive.CMA  : " '$(Archive.CMA)'
	$(Echo) "Compile.CMX  : " '$(Compile.CMX)'
	$(Echo) "Archive.CMXA : " '$(Archive.CMXA)'
	$(Echo) "CAML_LIBDIR  : " '$(CAML_LIBDIR)'
	$(Echo) "LibraryA     : " '$(LibraryA)'
	$(Echo) "LibraryCMA   : " '$(LibraryCMA)'
	$(Echo) "LibraryCMXA  : " '$(LibraryCMXA)'
	$(Echo) "SharedLib    : " '$(SharedLib)'
	$(Echo) "OcamlSources1: " '$(OcamlSources1)'
	$(Echo) "OcamlSources2: " '$(OcamlSources2)'
	$(Echo) "OcamlSources : " '$(OcamlSources)'
	$(Echo) "OcamlHeaders1: " '$(OcamlHeaders1)'
	$(Echo) "OcamlHeaders2: " '$(OcamlHeaders2)'
	$(Echo) "OcamlHeaders : " '$(OcamlHeaders)'
	$(Echo) "ObjectsCMI   : " '$(ObjectsCMI)'
	$(Echo) "ObjectsCMO   : " '$(ObjectsCMO)'
	$(Echo) "ObjectsCMX   : " '$(ObjectsCMX)'
	$(Echo) "OCAML_LIBDIR : " '$(OCAML_LIBDIR)'
	$(Echo) "DestA        : " '$(DestA)'
	$(Echo) "DestCMA      : " '$(DestCMA)'
	$(Echo) "DestCMXA     : " '$(DestCMXA)'
	$(Echo) "DestSharedLib: " '$(DestSharedLib)'
	$(Echo) "UsedLibs     : " '$(UsedLibs)'
	$(Echo) "UsedLibNames : " '$(UsedLibNames)'
	$(Echo) "ExtraLibs    : " '$(ExtraLibs)'

.PHONY: printcamlvars   build-cmis \
            clean-a     clean-cmis     clean-cma     clean-cmxa \
          install-a   install-cmis   install-cma   install-cmxa \
          install-exe \
		uninstall-a uninstall-cmis uninstall-cma uninstall-cmxa \
		uninstall-exe