1 #**************************************************************************
2 #* *
3 #* OCaml *
4 #* *
5 #* Xavier Leroy, projet Cristal, INRIA Rocquencourt *
6 #* *
7 #* Copyright 1999 Institut National de Recherche en Informatique et *
8 #* en Automatique. *
9 #* *
10 #* All rights reserved. This file is distributed under the terms of *
11 #* the GNU Lesser General Public License version 2.1, with the *
12 #* special exception on linking described in the file LICENSE. *
13 #* *
14 #**************************************************************************
15
16 ROOTDIR = ..
17
18 include $(ROOTDIR)/Makefile.config
19 include $(ROOTDIR)/Makefile.common
20
21 # Lists of source files
22
23 BYTECODE_C_SOURCES := $(addsuffix .c, \
24 interp misc stacks fix_code startup_aux startup_byt freelist major_gc \
25 minor_gc memory alloc roots_byt globroots fail_byt signals \
26 signals_byt printexc backtrace_byt backtrace compare ints \
27 floats str array io extern intern hash sys meta parsing gc_ctrl md5 obj \
28 lexing callback debugger weak compact finalise custom dynlink \
29 spacetime_byt afl $(UNIX_OR_WIN32) bigarray main memprof domain)
30
31 NATIVE_C_SOURCES := $(addsuffix .c, \
32 startup_aux startup_nat main fail_nat roots_nat signals \
33 signals_nat misc freelist major_gc minor_gc memory alloc compare ints \
34 floats str array io extern intern hash sys parsing gc_ctrl md5 obj \
35 lexing $(UNIX_OR_WIN32) printexc callback weak compact finalise custom \
36 globroots backtrace_nat backtrace dynlink_nat debugger meta \
37 dynlink clambda_checks spacetime_nat spacetime_snapshot afl bigarray \
38 memprof domain)
39
40 # The other_files variable stores the list of files whose dependencies
41 # should be computed by `make depend` although they do not need to be
42 # compiled on the current build system
43 ifeq "$(UNIX_OR_WIN32)" "win32"
44 other_files := unix.c
45 else
46 other_files := win32.c
47 endif
48
49 ifeq "$(TOOLCHAIN)" "msvc"
50 ASM_EXT := asm
51 ASM_SOURCES := $(ARCH)nt.$(ASM_EXT)
52 else
53 ASM_EXT := S
54 ASM_SOURCES := $(ARCH).$(ASM_EXT)
55 endif
56
57 # Targets to build and install
58
59 PROGRAMS := ocamlrun$(EXE)
60 BYTECODE_STATIC_LIBRARIES := ld.conf libcamlrun.$(A)
61 BYTECODE_SHARED_LIBRARIES :=
62 NATIVE_STATIC_LIBRARIES := libasmrun.$(A)
63 NATIVE_SHARED_LIBRARIES :=
64
65 ifeq "$(RUNTIMED)" "true"
66 PROGRAMS += ocamlrund$(EXE)
67 BYTECODE_STATIC_LIBRARIES += libcamlrund.$(A)
68 NATIVE_STATIC_LIBRARIES += libasmrund.$(A)
69 endif
70
71 ifeq "$(RUNTIMEI)" "true"
72 PROGRAMS += ocamlruni$(EXE)
73 BYTECODE_STATIC_LIBRARIES += libcamlruni.$(A)
74 NATIVE_STATIC_LIBRARIES += libasmruni.$(A)
75 endif
76
77 ifeq "$(UNIX_OR_WIN32)" "unix"
78 ifeq "$(SUPPORTS_SHARED_LIBRARIES)" "true"
79 BYTECODE_STATIC_LIBRARIES += libcamlrun_pic.$(A)
80 BYTECODE_SHARED_LIBRARIES += libcamlrun_shared.$(SO)
81 NATIVE_STATIC_LIBRARIES += libasmrun_pic.$(A)
82 NATIVE_SHARED_LIBRARIES += libasmrun_shared.$(SO)
83 endif
84 endif
85
86 # List of object files for each target
87
88 ASM_OBJECTS := $(ASM_SOURCES:.$(ASM_EXT)=.$(O))
89
90 libcamlrun_OBJECTS := $(BYTECODE_C_SOURCES:.c=_b.$(O))
91
92 libcamlrund_OBJECTS := $(BYTECODE_C_SOURCES:.c=_bd.$(O)) \
93 instrtrace_bd.$(O)
94
95 libcamlruni_OBJECTS := $(BYTECODE_C_SOURCES:.c=_bi.$(O))
96
97 libcamlrunpic_OBJECTS := $(BYTECODE_C_SOURCES:.c=_bpic.$(O))
98
99 libasmrun_OBJECTS := $(NATIVE_C_SOURCES:.c=_n.$(O)) $(ASM_OBJECTS)
100
101 libasmrund_OBJECTS := $(NATIVE_C_SOURCES:.c=_nd.$(O)) $(ASM_OBJECTS)
102
103 libasmruni_OBJECTS := $(NATIVE_C_SOURCES:.c=_ni.$(O)) $(ASM_OBJECTS)
104
105 libasmrunpic_OBJECTS := $(NATIVE_C_SOURCES:.c=_npic.$(O)) \
106 $(ASM_OBJECTS:.$(O)=_libasmrunpic.$(O))
107
108 # General (non target-specific) assembler and compiler flags
109
110 ifdef BOOTSTRAPPING_FLEXLINK
111 OC_CPPFLAGS += -DBOOTSTRAPPING_FLEXLINK
112 endif
113
114 # On Windows, OCAML_STDLIB_DIR needs to be defined dynamically
115
116 ifeq "$(UNIX_OR_WIN32)" "win32"
117 # OCAML_STDLIB_DIR needs to arrive in dynlink.c as a string which both gcc and
118 # msvc are willing parse without warning. This means we can't pass UTF-8
119 # directly since, as far as I can tell, cl can cope, but the pre-processor
120 # can't. So the string needs to be directly translated to L"" form. To do this,
121 # we take advantage of the fact that Cygwin uses GNU libiconv which includes a
122 # Java pseudo-encoding which translates any UTF-8 sequences to \uXXXX (and,
123 # unlike the C99 pseudo-encoding, emits two surrogate values when needed, rather
124 # than \UXXXXXXXX). The \u is then translated to \x in order to accommodate
125 # pre-Visual Studio 2013 compilers where \x is a non-standard alias for \u.
126 OCAML_STDLIB_DIR = $(shell echo $(LIBDIR)| iconv -t JAVA | sed -e 's/\\u/\\x/g')
127 STDLIB_CPP_FLAG = -DOCAML_STDLIB_DIR='L"$(OCAML_STDLIB_DIR)"'
128 else # Unix
129 OCAML_STDLIB_DIR = $(LIBDIR)
130 STDLIB_CPP_FLAG = -DOCAML_STDLIB_DIR='"$(OCAML_STDLIB_DIR)"'
131 endif
132
133 OC_CPPFLAGS += $(IFLEXDIR)
134
135 ifneq "$(CCOMPTYPE)" "msvc"
136 OC_CFLAGS += -g
137 endif
138
139 OC_NATIVE_CPPFLAGS = -DNATIVE_CODE -DTARGET_$(ARCH)
140
141 ifeq "$(UNIX_OR_WIN32)" "unix"
142 OC_NATIVE_CPPFLAGS += -DMODEL_$(MODEL)
143 endif
144
145 OC_NATIVE_CPPFLAGS += -DSYS_$(SYSTEM) $(IFLEXDIR) $(LIBUNWIND_INCLUDE_FLAGS)
146
147 OC_DEBUG_CPPFLAGS=-DDEBUG
148 OC_INSTR_CPPFLAGS=-DCAML_INSTR
149
150 ifeq "$(TOOLCHAIN)" "msvc"
151 ASMFLAGS=
152 ifeq ($(WITH_SPACETIME),true)
153 ASMFLAGS=/DWITH_SPACETIME
154 endif
155 endif
156
157 ASPPFLAGS = -DSYS_$(SYSTEM) -I$(ROOTDIR)/runtime
158 ifeq "$(UNIX_OR_WIN32)" "unix"
159 ASPPFLAGS += -DMODEL_$(MODEL)
160 endif
161
162 # Commands used to build native libraries
163
164 ifeq "$(UNIX_OR_WIN32)" "win32"
165 LIBS = $(BYTECCLIBS) $(EXTRALIBS)
166 ifdef BOOTSTRAPPING_FLEXLINK
167 MAKE_OCAMLRUN=$(MKEXE_BOOT)
168 else
169 MAKE_OCAMLRUN = $(MKEXE) -o $(1) $(2)
170 endif
171 else
172 LIBS = $(BYTECCLIBS)
173 MAKE_OCAMLRUN = $(MKEXE) -o $(1) $(2)
174 endif
175
176 # Build, install and clean targets
177
178 .PHONY: all
179 all: $(BYTECODE_STATIC_LIBRARIES) $(BYTECODE_SHARED_LIBRARIES) $(PROGRAMS)
180
181 .PHONY: allopt
182 allopt: $(NATIVE_STATIC_LIBRARIES) $(NATIVE_SHARED_LIBRARIES)
183
184 INSTALL_INCDIR=$(INSTALL_LIBDIR)/caml
185 .PHONY: install
186 install:
187 $(INSTALL_PROG) $(PROGRAMS) "$(INSTALL_BINDIR)"
188 $(INSTALL_DATA) $(BYTECODE_STATIC_LIBRARIES) "$(INSTALL_LIBDIR)"
189 ifneq "$(BYTECODE_SHARED_LIBRARIES)" ""
190 $(INSTALL_PROG) $(BYTECODE_SHARED_LIBRARIES) "$(INSTALL_LIBDIR)"
191 endif
192 mkdir -p "$(INSTALL_INCDIR)"
193 $(INSTALL_DATA) caml/domain_state.tbl caml/*.h "$(INSTALL_INCDIR)"
194
195 .PHONY: installopt
196 installopt:
197 $(INSTALL_DATA) $(NATIVE_STATIC_LIBRARIES) "$(INSTALL_LIBDIR)"
198 ifneq "$(NATIVE_SHARED_LIBRARIES)" ""
199 $(INSTALL_PROG) $(NATIVE_SHARED_LIBRARIES) "$(INSTALL_LIBDIR)"
200 endif
201
202 .PHONY: clean
203 clean:
204 rm -f $(PROGRAMS) *.$(O) *.$(A) *.$(SO) ld.conf
205 rm -f primitives prims.c caml/opnames.h caml/jumptbl.h
206 rm -f caml/version.h domain_state*.inc
207
208 .PHONY: distclean
209 distclean: clean
210
211 # Generated non-object files
212
213 ld.conf: $(ROOTDIR)/Makefile.config
214 echo "$(STUBLIBDIR)" > $@
215 echo "$(LIBDIR)" >> $@
216
217 # If primitives contain duplicated lines (e.g. because the code is defined
218 # like
219 # #ifdef X
220 # CAMLprim value caml_foo() ...
221 # #else
222 # CAMLprim value caml_foo() ...
223 # end), horrible things will happen (duplicated entries in Runtimedef ->
224 # double registration in Symtable -> empty entry in the PRIM table ->
225 # the bytecode interpreter is confused).
226 # We sort the primitive file and remove duplicates to avoid this problem.
227
228 # Warning: we use "sort | uniq" instead of "sort -u" because in the MSVC
229 # port, the "sort" program in the path is Microsoft's and not cygwin's
230
231 # Warning: POSIX sort is locale dependent, that's why we set LC_ALL explicitly.
232 # Sort is unstable for "is_directory" and "isatty"
233 # see http://pubs.opengroup.org/onlinepubs/9699919799/utilities/sort.html:
234 # "using sort to process pathnames, it is recommended that LC_ALL .. set to C"
235
236 # To speed up builds, we avoid changing "primitives" when files
237 # containing primitives change but the primitives table does not
238 primitives: $(shell ./gen_primitives.sh > primitives.new; \
239 cmp -s primitives primitives.new || echo primitives.new)
240 cp $^ $@
241
242 prims.c : primitives
243 (echo '#define CAML_INTERNALS'; \
244 echo '#include "caml/mlvalues.h"'; \
245 echo '#include "caml/prims.h"'; \
246 sed -e 's/.*/extern value &();/' primitives; \
247 echo 'c_primitive caml_builtin_cprim[] = {'; \
248 sed -e 's/.*/ &,/' primitives; \
249 echo ' 0 };'; \
250 echo 'char * caml_names_of_builtin_cprim[] = {'; \
251 sed -e 's/.*/ "&",/' primitives; \
252 echo ' 0 };') > prims.c
253
254 caml/opnames.h : caml/instruct.h
255 cat $^ | tr -d '\r' | \
256 sed -e '/\/\*/d' \
257 -e '/^#/d' \
258 -e 's/enum /static char * names_of_/' \
259 -e 's/{$$/[] = {/' \
260 -e 's/\([[:upper:]][[:upper:]_0-9]*\)/"\1"/g' > $@
261
262 # caml/jumptbl.h is required only if you have GCC 2.0 or later
263 caml/jumptbl.h : caml/instruct.h
264 cat $^ | tr -d '\r' | \
265 sed -n -e '/^ /s/ \([A-Z]\)/ \&\&lbl_\1/gp' \
266 -e '/^}/q' > $@
267
268 caml/version.h : $(ROOTDIR)/tools/make-version-header.sh $(ROOTDIR)/VERSION
269 $^ > $@
270
271 # Libraries and programs
272
273 ocamlrun$(EXE): prims.$(O) libcamlrun.$(A)
274 $(call MAKE_OCAMLRUN,$@,$^ $(LIBS))
275
276 libcamlrun.$(A): $(libcamlrun_OBJECTS)
277 $(call MKLIB,$@, $^)
278
279 ocamlrund$(EXE): prims.$(O) libcamlrund.$(A)
280 $(MKEXE) $(MKEXEDEBUGFLAG) -o $@ $^ $(LIBS)
281
282 libcamlrund.$(A): $(libcamlrund_OBJECTS)
283 $(call MKLIB,$@, $^)
284
285 ocamlruni$(EXE): prims.$(O) libcamlruni.$(A)
286 $(MKEXE) -o $@ $^ $(LIBS)
287
288 libcamlruni.$(A): $(libcamlruni_OBJECTS)
289 $(call MKLIB,$@, $^)
290
291 libcamlrun_pic.$(A): $(libcamlrunpic_OBJECTS)
292 $(call MKLIB,$@, $^)
293
294 libcamlrun_shared.$(SO): $(libcamlrunpic_OBJECTS)
295 $(MKDLL) -o $@ $^ $(BYTECCLIBS)
296
297 libasmrun.$(A): $(libasmrun_OBJECTS)
298 $(call MKLIB,$@, $^)
299
300 libasmrund.$(A): $(libasmrund_OBJECTS)
301 $(call MKLIB,$@, $^)
302
303 libasmruni.$(A): $(libasmruni_OBJECTS)
304 $(call MKLIB,$@, $^)
305
306 libasmrun_pic.$(A): $(libasmrunpic_OBJECTS)
307 $(call MKLIB,$@, $^)
308
309 libasmrun_shared.$(SO): $(libasmrunpic_OBJECTS)
310 $(MKDLL) -o $@ $^ $(NATIVECCLIBS)
311
312 # Target-specific preprocessor and compiler flags
313
314 %_bd.$(O): OC_CPPFLAGS += $(OC_DEBUG_CPPFLAGS)
315
316 %_bi.$(O): OC_CPPFLAGS += $(OC_INSTR_CPPFLAGS)
317
318 %_bpic.$(O): OC_CFLAGS += $(SHAREDLIB_CFLAGS)
319
320 %_n.$(O): OC_CPPFLAGS += $(OC_NATIVE_CPPFLAGS)
321
322 %_nd.$(O): OC_CPPFLAGS += $(OC_NATIVE_CPPFLAGS) $(OC_DEBUG_CPPFLAGS)
323
324 %_ni.$(O): OC_CPPFLAGS += $(OC_NATIVE_CPPFLAGS) $(OC_INSTR_CPPFLAGS)
325
326 %_npic.$(O): OC_CFLAGS += $(SHAREDLIB_CFLAGS)
327 %_npic.$(O): OC_CPPFLAGS += $(OC_NATIVE_CPPFLAGS)
328
329 # Compilation of C files
330
331 # The COMPILE_C_FILE macro below receives as argument the pattern
332 # that corresponds to the name of the generated object file
333 # (without the extension, which is added by the macro)
334 define COMPILE_C_FILE
335 $(1).$(O): %.c
336 $$(CC) -c $$(OC_CFLAGS) $$(OC_CPPFLAGS) $$(OUTPUTOBJ)$$@ $$<
337 endef
338
339 object_types := % %_b %_bd %_bi %_bpic %_n %_nd %_ni %_np %_npic
340
341 $(foreach object_type, $(object_types), \
342 $(eval $(call COMPILE_C_FILE,$(object_type))))
343
344 dynlink_%.$(O): OC_CPPFLAGS += $(STDLIB_CPP_FLAG)
345
346 $(foreach object_type,$(subst %,,$(object_types)), \
347 $(eval dynlink$(object_type).$(O): $(ROOTDIR)/Makefile.config))
348
349 # Compilation of assembly files
350
351 %.o: %.S
352 $(ASPP) $(ASPPFLAGS) -o $@ $< || \
353 { echo "If your assembler produced syntax errors, it is probably";\
354 echo "unhappy with the preprocessor. Check your assembler, or";\
355 echo "try producing $*.o by hand.";\
356 exit 2; }
357
358 %_libasmrunpic.o: %.S
359 $(ASPP) $(ASPPFLAGS) $(SHAREDLIB_CFLAGS) -o $@ $<
360
361 domain_state64.inc: caml/domain_state.tbl gen_domain_state64_inc.awk
362 awk -f gen_domain_state64_inc.awk $< > $@
363
364 domain_state32.inc: caml/domain_state.tbl gen_domain_state32_inc.awk
365 awk -f gen_domain_state32_inc.awk $< > $@
366
367 amd64nt.obj: amd64nt.asm domain_state64.inc
368 $(ASM)$@ $(ASMFLAGS) $<
369
370 i386nt.obj: i386nt.asm domain_state32.inc
371 $(ASM)$@ $(ASMFLAGS) $<
372
373 %_libasmrunpic.obj: %.asm
374 $(ASM)$@ $(ASMFLAGS) $<
375
376 # Dependencies
377
378 .PHONY: depend
379 ifeq "$(TOOLCHAIN)" "msvc"
380 depend:
381 $(error Dependencies cannot be regenerated using the MSVC ports)
382 else
383
384 NATIVE_DEP_CPPFLAGS := $(OC_CPPFLAGS) $(OC_NATIVE_CPPFLAGS)
385 NATIVE_DEP_FILES := $(NATIVE_C_SOURCES) $(other_files)
386
387 depend: *.c caml/opnames.h caml/jumptbl.h caml/version.h
388 $(CC) -MM $(OC_CPPFLAGS) *.c | \
389 sed -e 's/\([^.]*\)\.o/\1_b.$$(O)/' > .depend
390 $(CC) -MM $(OC_CPPFLAGS) $(OC_DEBUG_CPPFLAGS) *.c | \
391 sed -e 's/\([^.]*\)\.o/\1_bd.$$(O)/' >> .depend
392 $(CC) -MM $(OC_CPPFLAGS) $(OC_INSTR_CPPFLAGS) *.c | \
393 sed -e 's/\([^.]*\)\.o/\1_bi.$$(O)/' >> .depend
394 $(CC) -MM $(OC_CPPFLAGS) *.c | \
395 sed -e 's/\([^.]*\)\.o/\1_bpic.$$(O)/' >> .depend
396 $(CC) -MM $(NATIVE_DEP_CPPFLAGS) *.c | \
397 sed -e 's/\([^.]*\)\.o/\1_n.$$(O)/' >> .depend
398 $(CC) -MM $(NATIVE_DEP_CPPFLAGS) $(OC_DEBUG_CPPFLAGS) *.c | \
399 sed -e 's/\([^.]*\)\.o/\1_nd.$$(O)/' >> .depend
400 $(CC) -MM $(NATIVE_DEP_CPPFLAGS) $(OC_INSTR_CPPFLAGS) *.c | \
401 sed -e 's/\([^.]*\)\.o/\1_ni.$$(O)/' >> .depend
402 $(CC) -MM $(NATIVE_DEP_CPPFLAGS) *.c | \
403 sed -e 's/\([^.]*\)\.o/\1_npic.$$(O)/' >> .depend
404 endif
405
406 include .depend
407