1 #**************************************************************************
2 #* *
3 #* OCaml *
4 #* *
5 #* Xavier Leroy, projet Cristal, INRIA Rocquencourt *
6 #* Mark Shinwell, Jane Street Europe *
7 #* *
8 #* Copyright 1999 Institut National de Recherche en Informatique et *
9 #* en Automatique. *
10 #* Copyright 2018--2019 Jane Street Group LLC *
11 #* *
12 #* All rights reserved. This file is distributed under the terms of *
13 #* the GNU Lesser General Public License version 2.1, with the *
14 #* special exception on linking described in the file LICENSE. *
15 #* *
16 #**************************************************************************
17
18 # Makefile for the dynamic link library
19
20 # FIXME reduce redundancy by including ../Makefile
21
22 ROOTDIR = ../..
23
24 include $(ROOTDIR)/Makefile.config
25 include $(ROOTDIR)/Makefile.common
26 include $(ROOTDIR)/Makefile.best_binaries
27
28 CAMLRUN ?= $(ROOTDIR)/boot/ocamlrun
29
30 OCAMLC=$(BEST_OCAMLC) -nostdlib -I $(ROOTDIR)/stdlib
31 OCAMLOPT=$(BEST_OCAMLOPT) -nostdlib -I $(ROOTDIR)/stdlib
32
33 # COMPFLAGS should be in sync with the toplevel Makefile's COMPFLAGS.
34 COMPFLAGS=-strict-sequence -principal -absname -w +a-4-9-40-41-42-44-45-48-66 \
35 -warn-error A \
36 -bin-annot -safe-string -strict-formats
37 ifeq "$(FLAMBDA)" "true"
38 OPTCOMPFLAGS=-O3
39 else
40 OPTCOMPFLAGS=
41 endif
42
43 COMPFLAGS += -I byte
44 OPTCOMPFLAGS += -I native
45
46 LOCAL_SRC=dynlink_compilerlibs
47
48 OBJS=byte/dynlink_compilerlibs.cmo dynlink_types.cmo \
49 dynlink_platform_intf.cmo dynlink_common.cmo byte/dynlink.cmo
50
51 NATOBJS=native/dynlink_compilerlibs.cmx dynlink_types.cmx \
52 dynlink_platform_intf.cmx dynlink_common.cmx native/dynlink.cmx
53
54 # We need/desire access to compilerlibs for various reasons:
55 # - The bytecode dynamic linker is in compilerlibs and has many dependencies
56 # from there.
57 # - It stops duplication of code (e.g. magic numbers from [Config]).
58 # - It allows future improvement by re-using various types.
59 # We have to pack our own version of compilerlibs (even if compilerlibs
60 # becomes packed in the future by default) otherwise problems will be caused
61 # if a user tries to link dynlink.cm{x,}a with code either having modules
62 # of the same names or code that is already linked against compilerlibs.
63 #
64 # The modules needed from compilerlibs have to be recompiled so that the
65 # -for-pack option can be specified. Packing without such option having been
66 # specified, as used to be performed in this Makefile, is currently permitted
67 # for bytecode (but may be disallowed in the future) but not native.
68
69 # .mli files from compilerlibs that don't have a corresponding .ml file.
70 COMPILERLIBS_INTFS=\
71 parsing/asttypes.mli \
72 parsing/parsetree.mli \
73 typing/outcometree.mli \
74 file_formats/cmo_format.mli \
75 file_formats/cmxs_format.mli
76
77 # .ml files from compilerlibs that have corresponding .mli files.
78 COMPILERLIBS_SOURCES=\
79 utils/config.ml \
80 utils/build_path_prefix_map.ml \
81 utils/misc.ml \
82 utils/identifiable.ml \
83 utils/numbers.ml \
84 utils/arg_helper.ml \
85 utils/clflags.ml \
86 utils/profile.ml \
87 utils/consistbl.ml \
88 utils/terminfo.ml \
89 utils/warnings.ml \
90 utils/load_path.ml \
91 parsing/location.ml \
92 parsing/longident.ml \
93 parsing/docstrings.ml \
94 parsing/syntaxerr.ml \
95 parsing/ast_helper.ml \
96 parsing/ast_mapper.ml \
97 parsing/attr_helper.ml \
98 parsing/builtin_attributes.ml \
99 typing/ident.ml \
100 typing/path.ml \
101 typing/primitive.ml \
102 typing/type_immediacy.ml \
103 typing/types.ml \
104 typing/btype.ml \
105 typing/subst.ml \
106 typing/predef.ml \
107 typing/datarepr.ml \
108 file_formats/cmi_format.ml \
109 typing/persistent_env.ml \
110 typing/env.ml \
111 lambda/lambda.ml \
112 lambda/runtimedef.ml \
113 bytecomp/instruct.ml \
114 bytecomp/opcodes.ml \
115 bytecomp/bytesections.ml \
116 bytecomp/dll.ml \
117 bytecomp/meta.ml \
118 bytecomp/symtable.ml
119
120 # Rules to make a local copy of the .ml and .mli files required. We also
121 # provide .ml files for .mli-only modules---without this, such modules do
122 # not seem to be located by the type checker inside bytecode packs.
123
124 $(LOCAL_SRC)/Makefile: $(LOCAL_SRC)/Makefile.copy-sources Makefile
125 cp -f $< $@
126 for ml in $(COMPILERLIBS_SOURCES); do \
127 echo "$(LOCAL_SRC)/$$(basename $$ml): $(ROOTDIR)/$$ml" \
128 >> $@; \
129 echo "$(LOCAL_SRC)/$$(basename $$ml)i: $(ROOTDIR)/$${ml}i" \
130 >> $@; \
131 done;
132 for mli in $(COMPILERLIBS_INTFS); do \
133 echo "$(LOCAL_SRC)/$$(basename $$mli): $(ROOTDIR)/$$mli" \
134 >> $@; \
135 echo \
136 "$(LOCAL_SRC)/$$(basename $$mli .mli).ml: $(ROOTDIR)/$$mli"\
137 >> $@; \
138 done
139
140 # Rules to automatically generate dependencies for the local copy of the
141 # compilerlibs sources.
142
143 COMPILERLIBS_SOURCES_NO_DIRS=$(notdir $(COMPILERLIBS_SOURCES))
144
145 COMPILERLIBS_INTFS_NO_DIRS=$(notdir $(COMPILERLIBS_INTFS))
146
147 COMPILERLIBS_INTFS_BASE_NAMES=$(basename $(COMPILERLIBS_INTFS_NO_DIRS))
148
149 COMPILERLIBS_INTFS_ML_NO_DIRS=$(addsuffix .ml, $(COMPILERLIBS_INTFS_BASE_NAMES))
150
151 COMPILERLIBS_COPIED_INTFS=\
152 $(addprefix $(LOCAL_SRC)/, $(COMPILERLIBS_INTFS_ML_NO_DIRS))
153
154 COMPILERLIBS_COPIED_SOURCES=\
155 $(addprefix $(LOCAL_SRC)/, $(COMPILERLIBS_SOURCES_NO_DIRS)) \
156 $(COMPILERLIBS_COPIED_INTFS)
157
158 COMPILERLIBS_SOURCES_INTFS=\
159 $(addsuffix i, $(COMPILERLIBS_SOURCES))
160
161 COMPILERLIBS_COPIED_SOURCES_INTFS=\
162 $(addsuffix i, $(COMPILERLIBS_COPIED_SOURCES))
163
164 # $(LOCAL_SRC)/Makefile uses the variables above in dependencies, so must be
165 # include'd after they've been defined.
166 -include $(LOCAL_SRC)/Makefile
167
168 # Rules to build the local copy of the compilerlibs sources in such a way
169 # that the resulting .cm{o,x} files can be packed.
170
171 COMPILERLIBS_CMO=$(COMPILERLIBS_COPIED_SOURCES:.ml=.cmo)
172 COMPILERLIBS_CMX=$(COMPILERLIBS_COPIED_SOURCES:.ml=.cmx)
173
174 $(LOCAL_SRC)/%.cmi:
175 $(OCAMLC) -c -for-pack Dynlink_compilerlibs $(COMPFLAGS) \
176 -I $(LOCAL_SRC) -o $@ $(LOCAL_SRC)/$*.mli
177
178 $(LOCAL_SRC)/%.cmo:
179 $(OCAMLC) -c -for-pack Dynlink_compilerlibs $(COMPFLAGS) \
180 -I $(LOCAL_SRC) -o $@ $(LOCAL_SRC)/$*.ml
181
182 $(LOCAL_SRC)/%.cmx:
183 $(OCAMLOPT) -c -for-pack Dynlink_compilerlibs $(COMPFLAGS) \
184 $(OPTCOMPFLAGS) -I $(LOCAL_SRC) -o $@ $(LOCAL_SRC)/$*.ml
185
186 # Rules for building the [Dynlink_compilerlibs] bytecode and native packs
187 # from their components.
188
189 byte/dynlink_compilerlibs.cmo: $(COMPILERLIBS_CMO)
190 $(OCAMLC) $(COMPFLAGS) -pack -o $@ $(COMPILERLIBS_CMO)
191
192 byte/dynlink_compilerlibs.cmi: byte/dynlink_compilerlibs.cmo
193
194 native/dynlink_compilerlibs.cmx: $(COMPILERLIBS_CMX)
195 $(OCAMLOPT) $(COMPFLAGS) $(OPTCOMPFLAGS) -pack -o $@ $(COMPILERLIBS_CMX)
196
197 %/dynlink.cmi: dynlink.cmi dynlink.mli
198 cp $^ $*/
199
200 # Rules for building the interface of the [Dynlink_compilerlibs] packs.
201 # To avoid falling foul of the problem described below, the .cmo and .cmx
202 # files for the dynlink-specific compilerlibs packs generated here---and in
203 # particular the corresponding .cmi files -- are kept in separate directories.
204
205 # The main dynlink rules start here.
206
207 all: dynlink.cma extract_crc
208
209 allopt: dynlink.cmxa
210
211 dynlink.cma: $(OBJS)
212 $(OCAMLC) $(COMPFLAGS) -ccopt "$(NATDYNLINKOPTS)" -a -I byte -o $@ $^
213
214 dynlink.cmxa: $(NATOBJS)
215 $(OCAMLOPT) $(COMPFLAGS) -ccopt "$(NATDYNLINKOPTS)" -a -I native \
216 -o $@ $^
217 # As for all other .cmxa files, ensure that the .cmx files are in the same
218 # directory. If this were omitted, ocamldoc in particular will fail to build
219 # with a -opaque warning. Note that installopt refers to $(NATOBJS) so doesn't
220 # require this file to exist, hence its inclusion in the recipe for dynlink.cmxa
221 # rather than as a dependency elsewhere.
222 cp native/dynlink.cmx dynlink.cmx
223
224 # Since there is no .mli for [Dynlink_platform_intf], we need to be
225 # careful that compilation of the .cmx file does not write the .cmi file again,
226 # which would cause rebuilding of ocamlopt. The easiest way to do this seems
227 # to be to copy the .ml file, which is a valid .mli, to the .mli.
228 dynlink_platform_intf.mli: dynlink_platform_intf.ml
229 cp $< $@
230
231 extract_crc: dynlink.cma byte/dynlink_compilerlibs.cmo extract_crc.cmo
232 $(OCAMLC) -o $@ $^
233
234 install:
235 $(INSTALL_DATA) \
236 dynlink.cmi dynlink.cma \
237 "$(INSTALL_LIBDIR)"
238 ifeq "$(INSTALL_SOURCE_ARTIFACTS)" "true"
239 $(INSTALL_DATA) \
240 dynlink.cmti dynlink.mli \
241 "$(INSTALL_LIBDIR)"
242 endif
243 $(INSTALL_PROG) \
244 extract_crc "$(INSTALL_LIBDIR)/extract_crc$(EXE)"
245
246 installopt:
247 if $(NATDYNLINK); then \
248 $(INSTALL_DATA) \
249 $(NATOBJS) dynlink.cmxa dynlink.$(A) \
250 "$(INSTALL_LIBDIR)" && \
251 cd "$(INSTALL_LIBDIR)" && $(RANLIB) dynlink.$(A); \
252 fi
253
254 partialclean:
255 rm -f extract_crc *.cm[ioaxt] *.cmti *.cmxa \
256 byte/*.cm[iot] byte/*.cmti \
257 native/*.cm[ixt] native/*.cmti native/*.$(O) \
258 $(LOCAL_SRC)/*.cm[ioaxt] $(LOCAL_SRC)/*.cmti $(LOCAL_SRC)/*.$(O)
259
260 clean: partialclean
261 rm -f *.$(A) *.$(O) *.so *.dll dynlink_platform_intf.mli \
262 $(LOCAL_SRC)/*.ml $(LOCAL_SRC)/*.mli $(LOCAL_SRC)/Makefile \
263 $(LOCAL_SRC)/.depend byte/dynlink.mli native/dynlink.mli
264
265 .PHONY: beforedepend
266 beforedepend: dynlink_platform_intf.mli
267
268 .PHONY: depend
269 ifeq "$(TOOLCHAIN)" "msvc"
270 depend:
271 $(error Dependencies cannot be regenerated using the MSVC ports)
272 else
273 DEPEND_DUMMY_FILES=\
274 native/dynlink_compilerlibs.ml \
275 byte/dynlink_compilerlibs.mli \
276 byte/dynlink.mli \
277 native/dynlink.mli
278
279 depend: beforedepend
280 touch $(DEPEND_DUMMY_FILES)
281 $(CAMLRUN) $(ROOTDIR)/boot/ocamlc -depend -slash \
282 -I byte -bytecode *.mli *.ml byte/dynlink.ml > .depend
283 $(CAMLRUN) $(ROOTDIR)/boot/ocamlc -depend -slash \
284 -I native -native *.ml native/dynlink.ml >> .depend
285 rm -f $(DEPEND_DUMMY_FILES)
286 endif
287
288 include .depend
289
290 .SUFFIXES: .ml .mli .cmi .cmo .cmx .$(O)
291
292 .mli.cmi:
293 $(OCAMLC) -c $(COMPFLAGS) $<
294
295 .ml.cmo:
296 $(OCAMLC) -c $(COMPFLAGS) $<
297
298 .ml.cmx:
299 $(OCAMLOPT) -c $(COMPFLAGS) $(OPTCOMPFLAGS) $<
300