Import Upstream version 20180207
[hcoop/debian/mlton.git] / runtime / Makefile
1 ## Copyright (C) 2010-2013,2018 Matthew Fluet.
2 # Copyright (C) 1999-2009 Henry Cejtin, Matthew Fluet, Suresh
3 # Jagannathan, and Stephen Weeks.
4 # Copyright (C) 1997-2000 NEC Research Institute.
5 #
6 # MLton is released under a BSD-style license.
7 # See the file MLton-LICENSE for details.
8 ##
9
10 CC := gcc
11 AR := ar
12 RANLIB := ranlib
13
14 WITH_GMP_DIR :=
15 ifneq ($(WITH_GMP_DIR),)
16 WITH_GMP_INC_DIR := $(WITH_GMP_DIR)/include
17 WITH_GMP_LIB_DIR := $(WITH_GMP_DIR)/lib
18 endif
19
20 FIND := find
21 GREP := grep
22 GZIP := gzip
23 PATCH := patch
24 SED := sed
25 TAR := tar
26
27 ######################################################################
28 ######################################################################
29
30 SRC := $(shell cd .. && pwd)
31 BUILD := $(SRC)/build
32 BIN := $(BUILD)/bin
33
34 PATH := $(BIN):$(shell echo $$PATH)
35
36 HOST_ARCH := $(shell ../bin/host-arch)
37 HOST_OS := $(shell ../bin/host-os)
38 TARGET := self
39 TARGET_ARCH := $(HOST_ARCH)
40 TARGET_OS := $(HOST_OS)
41
42 ######################################################################
43
44 ifeq ($(TARGET), self)
45 CROSS_PREFIX :=
46 else
47 CROSS_PREFIX := $(TARGET)-
48 endif
49
50 GCC_MAJOR_VERSION := \
51 $(shell $(CC) -v 2>&1 | $(GREP) 'gcc version' | \
52 sed 's/.*gcc version \([0-9][0-9]*\)\.\([0-9][0-9]*\).*/\1/')
53 GCC_MINOR_VERSION := \
54 $(shell $(CC) -v 2>&1 | $(GREP) 'gcc version' | \
55 sed 's/.*gcc version \([0-9][0-9]*\)\.\([0-9][0-9]*\).*/\2/')
56 GCC_VERSION := $(GCC_MAJOR_VERSION).$(GCC_MINOR_VERSION)
57
58 EXE :=
59
60 # These flags can be overridden by the user
61 CPPFLAGS :=
62 CFLAGS :=
63
64 WARNXCFLAGS :=
65 WARNXCFLAGS += -pedantic -Wall
66 ifeq ($(findstring $(GCC_MAJOR_VERSION), 4),$(GCC_MAJOR_VERSION))
67 WARNXCFLAGS += -Wextra
68 endif
69 WARNXCFLAGS += -Wformat=2
70 ifeq ($(findstring $(GCC_MAJOR_VERSION), 4),$(GCC_MAJOR_VERSION))
71 WARNXCFLAGS += -Wswitch-default -Wswitch-enum
72 endif
73 WARNXCFLAGS += -Wuninitialized
74 ifeq ($(findstring $(GCC_MAJOR_VERSION), 4),$(GCC_MAJOR_VERSION))
75 WARNXCFLAGS += -Winit-self
76 endif
77 ifeq ($(findstring $(GCC_MAJOR_VERSION), 4),$(GCC_MAJOR_VERSION))
78 WARNXCFLAGS += -Wstrict-aliasing=2
79 endif
80 WARNXCFLAGS += -Wfloat-equal
81 WARNXCFLAGS += -Wundef
82 WARNXCFLAGS += -Wshadow
83 WARNXCFLAGS += -Wpointer-arith
84 WARNXCFLAGS += -Wbad-function-cast -Wcast-qual
85 WARNXCFLAGS += -Wwrite-strings
86 # WARNXCFLAGS += -Wconversion
87 WARNXCFLAGS += -Waggregate-return
88 WARNXCFLAGS += -Wstrict-prototypes
89 ifeq ($(findstring $(GCC_MAJOR_VERSION), 4),$(GCC_MAJOR_VERSION))
90 WARNXCFLAGS += -Wold-style-definition
91 endif
92 WARNXCFLAGS += -Wmissing-prototypes -Wmissing-declarations
93 ifeq ($(findstring $(GCC_MAJOR_VERSION), 4),$(GCC_MAJOR_VERSION))
94 WARNXCFLAGS += -Wmissing-field-initializers
95 endif
96 WARNXCFLAGS += -Wmissing-noreturn
97 WARNXCFLAGS += -Wmissing-format-attribute
98 # WARNXCFLAGS += -Wpacked
99 # WARNXCFLAGS += -Wpadded
100 WARNXCFLAGS += -Wredundant-decls
101 WARNXCFLAGS += -Wnested-externs
102 # WARNXCFLAGS += -Wunreachable-code
103
104 XCFLAGS := -std=gnu11 -fno-common $(WARNXCFLAGS)
105 OPTXCFLAGS := -Wdisabled-optimization -O2 -fomit-frame-pointer
106 DEBUGXCFLAGS := -DASSERT=1 -Wno-uninitialized -O0 -g
107 PICXCFLAGS :=
108
109 # Win32&64 don't use PIC code, all other platforms do
110 ifeq ($(findstring $(TARGET_OS), mingw cygwin),)
111 PICXCFLAGS += -fPIC
112 endif
113
114 # Make mlton library symbols private (win32&64 use another technique)
115 ifeq ($(findstring $(GCC_MAJOR_VERSION), 4),$(GCC_MAJOR_VERSION))
116 ifeq ($(findstring $(TARGET_OS), mingw cygwin),)
117 XCFLAGS += -fvisibility=hidden
118 endif
119 endif
120
121 ifeq ($(TARGET_ARCH), alpha)
122 XCFLAGS += -mieee -mbwx -mtune=ev6 -mfp-rounding-mode=d
123 endif
124
125 ifeq ($(TARGET_ARCH), amd64)
126 XCFLAGS += -m64
127 endif
128
129 ifeq ($(findstring $(TARGET_ARCH), hppa ia64 powerpc sparc),$(TARGET_ARCH))
130 ifeq (4.2, $(firstword $(sort $(GCC_VERSION) 4.2)))
131 # GMP headers contain C99 inline functions which generate warnings
132 # with a suggestion to use this flag to disable the warnings.
133 XCFLAGS += -fgnu89-inline
134 endif
135 endif
136
137 ifeq ($(TARGET_ARCH), ia64)
138 XCFLAGS += -mtune=itanium2
139 ifeq ($(TARGET_OS), hpux)
140 XCFLAGS += -mlp64
141 endif
142 endif
143
144 ifeq ($(TARGET_OS)-$(TARGET_ARCH), aix-powerpc64)
145 XCFLAGS += -maix64
146 AR := ar -X 64 rc
147 endif
148
149 ifeq ($(TARGET_ARCH), sparc)
150 XCFLAGS += -m32 -mcpu=v8 -Wa,-xarch=v8plusa
151 endif
152
153 ifeq ($(TARGET_ARCH), x86)
154 XCFLAGS += -m32
155 OPTXCFLAGS += -malign-loops=2 -malign-jumps=2 -malign-functions=5
156 endif
157
158 ifeq ($(TARGET_OS), cygwin)
159 EXE := .exe
160 endif
161
162 ifeq ($(TARGET_OS), mingw)
163 EXE := .exe
164 # GCC doesn't recognize the %I64 format specifier which means %ll on windows
165 XCFLAGS += -Wno-format -Wno-missing-format-attribute
166 endif
167
168 ifeq ($(TARGET_OS), solaris)
169 XCFLAGS += -funroll-all-loops
170 endif
171
172 ifneq ($(WITH_GMP_INC_DIR),)
173 XCFLAGS += -I$(WITH_GMP_INC_DIR)
174 endif
175
176 ifneq ($(WITH_GMP_LIB_DIR),)
177 XCFLAGS += -L$(WITH_GMP_LIB_DIR)
178 endif
179
180 XCFLAGS += -I. -Iplatform
181 OPTCFLAGS := $(CFLAGS) $(CPPFLAGS) $(XCFLAGS) $(OPTXCFLAGS)
182 DEBUGCFLAGS := $(CFLAGS) $(CPPFLAGS) $(XCFLAGS) $(DEBUGXCFLAGS)
183 PICCFLAGS := $(OPTCFLAGS) $(PICXCFLAGS)
184
185 ALL := libgdtoa.a libgdtoa-gdb.a libgdtoa-pic.a \
186 libmlton.a libmlton-gdb.a libmlton-pic.a
187 ALL += gen/c-types.sml gen/basis-ffi.sml gen/sizes
188
189 all: $(ALL)
190
191
192 ### util ###
193
194 UTILHFILES := \
195 util.h \
196 $(shell $(FIND) util -type f -name '*.h')
197 UTILCFILES := \
198 $(shell $(FIND) util -type f -name '*.c')
199
200 util-pic.o: util.c $(UTILCFILES) cenv.h $(UTILHFILES)
201 $(CROSS_PREFIX)$(CC) $(PICCFLAGS) -c -o $@ $<
202 util-gdb.o: util.c $(UTILCFILES) cenv.h $(UTILHFILES)
203 $(CROSS_PREFIX)$(CC) $(DEBUGCFLAGS) -c -o $@ $<
204 util.o: util.c $(UTILCFILES) cenv.h $(UTILHFILES)
205 $(CROSS_PREFIX)$(CC) $(OPTCFLAGS) -c -o $@ $<
206
207 ### c-types.h ml-types.h gen/c-types.sml ###
208
209 c-types.h: gen/c-types.h
210 cp $< $@
211 ml-types.h: gen/ml-types.h
212 cp $< $@
213 gen/c-types.h gen/c-types.sml gen/ml-types.h: gen/gen-types.stamp
214 @touch $@
215 gen/gen-types.stamp: gen/gen-types.c util.h util.o
216 $(CROSS_PREFIX)$(CC) $(OPTCFLAGS) -o gen/gen-types gen/gen-types.c util.o
217 rm -f gen/c-types.h gen/c-types.sml gen/ml-types.h gen/gen-types.stamp
218 cd gen && ./gen-types
219 rm -f gen/gen-types$(EXE) gen/gen-types
220 touch $@
221
222 ### basis-ffi.h gen/basis-ffi.sml ###
223
224 basis-ffi.h: gen/basis-ffi.h
225 cp $< $@
226 gen/basis-ffi.h gen/basis-ffi.sml: gen/gen-basis-ffi.stamp
227 @touch $@
228 gen/gen-basis-ffi.stamp: gen/gen-basis-ffi.sml gen/basis-ffi.def
229 mlton -output gen/gen-basis-ffi gen/gen-basis-ffi.sml
230 rm -f gen/basis-ffi.h gen/basis-ffi.sml gen/gen-basis-ffi.stamp
231 cd gen && ./gen-basis-ffi
232 rm -f gen/gen-basis-ffi
233 touch $@
234
235 ### libgdtoa ###
236
237 GDTOAHFILES := arith.h gdtoa.h
238 GDTOAHFILES := $(patsubst %,gdtoa/%,$(GDTOAHFILES))
239 GDTOACFILES := \
240 dmisc.c g_ddfmt.c g_ffmt_p.c gdtoa.c misc.c strtoIf.c strtodI.c strtopdd.c strtord.c sum.c \
241 dtoa.c g_ddfmt_p.c g_xLfmt.c gethex.c smisc.c strtoIg.c strtodg.c strtopf.c strtordd.c ulp.c \
242 g_Qfmt.c g_dfmt.c g_xLfmt_p.c gmisc.c strtoIQ.c strtoIx.c strtof.c strtopx.c strtorf.c \
243 g_Qfmt_p.c g_dfmt_p.c g_xfmt.c hd_init.c strtoId.c strtoIxL.c strtopQ.c strtopxL.c strtorx.c \
244 g__fmt.c g_ffmt.c g_xfmt_p.c hexnan.c strtoIdd.c strtod.c strtopd.c strtorQ.c strtorxL.c
245 GDTOACFILES := $(patsubst %,gdtoa/%,$(GDTOACFILES))
246
247 GDTOA_OBJS := $(patsubst %.c,%.o,$(GDTOACFILES))
248 GDTOA_DEBUG_OBJS := $(patsubst %.c,%-gdb.o,$(GDTOACFILES))
249 GDTOA_PIC_OBJS := $(patsubst %.c,%-pic.o,$(GDTOACFILES))
250
251 gdtoa/README: gdtoa.tgz gdtoa.may_alias-unions.patch gdtoa.rename-public-fns.patch gdtoa.hide-private-fns.patch gdtoa.hide-public-fns.patch
252 $(GZIP) -dc gdtoa.tgz | $(TAR) xf -
253 $(PATCH) -s -d gdtoa -p1 <gdtoa.may_alias-unions.patch
254 $(PATCH) -s -d gdtoa -p1 <gdtoa.rename-public-fns.patch
255 $(PATCH) -s -d gdtoa -p1 <gdtoa.hide-private-fns.patch
256 $(PATCH) -s -d gdtoa -p1 <gdtoa.hide-public-fns.patch
257 @touch $@
258
259 gdtoa/gdtoa.h $(GDTOACFILES): gdtoa/README
260 @touch $@
261
262 gdtoa/arithchk.c: gdtoa/README
263 @touch $@
264
265 gdtoa/arithchk.out: gdtoa/arithchk.c
266 cd gdtoa && $(CROSS_PREFIX)$(CC) $(OPTCFLAGS) -w -O1 -o arithchk.out arithchk.c
267
268 gdtoa/arith.h: gdtoa/arithchk.out
269 cd gdtoa && ./arithchk.out >arith.h
270
271 gdtoa/qnan.c: gdtoa/README
272 @touch $@
273
274 gdtoa/qnan.out: gdtoa/arith.h gdtoa/qnan.c
275 cd gdtoa && $(CROSS_PREFIX)$(CC) $(OPTCFLAGS) -w -O1 -o qnan.out qnan.c
276
277 gdtoa/gd_qnan.h: gdtoa/qnan.out
278 cd gdtoa && ./qnan.out >gd_qnan.h
279
280 gdtoa/%-pic.o: gdtoa/%.c gdtoa/arith.h gdtoa/gd_qnan.h
281 $(CROSS_PREFIX)$(CC) $(PICCFLAGS) -w -DINFNAN_CHECK -c -o $@ $<
282 gdtoa/%-gdb.o: gdtoa/%.c gdtoa/arith.h gdtoa/gd_qnan.h
283 $(CROSS_PREFIX)$(CC) $(DEBUGCFLAGS) -w -DINFNAN_CHECK -c -o $@ $<
284 gdtoa/%.o: gdtoa/%.c gdtoa/arith.h gdtoa/gd_qnan.h
285 $(CROSS_PREFIX)$(CC) $(OPTCFLAGS) -w -DINFNAN_CHECK -c -o $@ $<
286
287 libgdtoa.a: $(GDTOA_OBJS)
288 libgdtoa-gdb.a: $(GDTOA_DEBUG_OBJS)
289 libgdtoa-pic.a: $(GDTOA_PIC_OBJS)
290
291 ### libmlton ###
292
293 PLATFORMHFILES := \
294 platform.h \
295 $(shell $(FIND) platform -type f -name '*.h')
296 PLATFORMCFILES := \
297 $(shell $(FIND) platform -type f -name '*.c')
298
299 GCHFILES := \
300 gc.h \
301 $(shell $(FIND) gc -type f -name '*.h')
302 GCCFILES := \
303 $(shell $(FIND) gc -type f -name '*.c')
304
305 BASISCFILES := \
306 $(shell $(FIND) basis -type f -name '*.c')
307
308 HFILES := \
309 cenv.h \
310 $(UTILHFILES) \
311 ml-types.h \
312 c-types.h \
313 basis-ffi.h \
314 $(PLATFORMHFILES) \
315 $(GCHFILES)
316
317 MLTON_OBJS := \
318 util.o \
319 platform.o \
320 platform/$(TARGET_OS).o \
321 gc.o
322 MLTON_OBJS += $(foreach f, $(basename $(BASISCFILES)), $(f).o)
323 MLTON_DEBUG_OBJS := $(patsubst %.o,%-gdb.o,$(MLTON_OBJS))
324 MLTON_PIC_OBJS := $(patsubst %.o,%-pic.o,$(MLTON_OBJS))
325
326 platform/$(TARGET_OS)-pic.o: $(PLATFORMCFILES)
327 platform/$(TARGET_OS)-gdb.o: $(PLATFORMCFILES)
328 platform/$(TARGET_OS).o: $(PLATFORMCFILES)
329
330 gc-pic.o: $(GCCFILES)
331 gc-gdb.o: $(GCCFILES)
332 gc.o: $(GCCFILES)
333
334 gc.c_XCFLAGS := -Wno-unreachable-code
335
336 basis/Real/Math-pic.o: basis/Real/Math-fns.h
337 basis/Real/Math-gdb.o: basis/Real/Math-fns.h
338 basis/Real/Math.o: basis/Real/Math-fns.h
339 basis/Real/Real-pic.o: basis/Real/Real-ops.h
340 basis/Real/Real-gdb.o: basis/Real/Real-ops.h
341 basis/Real/Real.o: basis/Real/Real-ops.h
342 basis/Real/Real.c_XCFLAGS := -Wno-float-equal
343 basis/Real/gdtoa-pic.o: $(GDTOAHFILES)
344 basis/Real/gdtoa-gdb.o: $(GDTOAHFILES)
345 basis/Real/gdtoa.o: $(GDTOAHFILES)
346 basis/Real/strto-pic.o: $(GDTOAHFILES)
347 basis/Real/strto-gdb.o: $(GDTOAHFILES)
348 basis/Real/strto.o: $(GDTOAHFILES)
349 basis/System/Date.c_XCFLAGS := -Wno-format-nonliteral
350 basis/Word/Word-pic.o: basis/Word/Word-consts.h basis/Word/Word-ops.h basis/Word/Word-check.h
351 basis/Word/Word-gdb.o: basis/Word/Word-consts.h basis/Word/Word-ops.h basis/Word/Word-check.h
352 basis/Word/Word.o: basis/Word/Word-consts.h basis/Word/Word-ops.h basis/Word/Word-check.h
353 basis/coerce-pic.o: basis/coerce.h
354 basis/coerce-gdb.o: basis/coerce.h
355 basis/coerce.o: basis/coerce.h
356 basis/cpointer-pic.o: basis/cpointer.h
357 basis/cpointer-gdb.o: basis/cpointer.h
358 basis/cpointer.o: basis/cpointer.h
359
360 %-pic.o: %.c $(HFILES)
361 $(CROSS_PREFIX)$(CC) $(PICCFLAGS) $($(<)_XCFLAGS) -c -o $@ $<
362 %-gdb.o: %.c $(HFILES)
363 $(CROSS_PREFIX)$(CC) $(DEBUGCFLAGS) $($(<)_XCFLAGS) -c -o $@ $<
364 %.o: %.c $(HFILES)
365 $(CROSS_PREFIX)$(CC) $(OPTCFLAGS) $($(<)_XCFLAGS) -c -o $@ $<
366
367 libmlton.a: $(MLTON_OBJS)
368 libmlton-gdb.a: $(MLTON_DEBUG_OBJS)
369 libmlton-pic.a: $(MLTON_PIC_OBJS)
370
371 ### gen/sizes ###
372
373 gen/sizes: gen/gen-sizes.stamp
374 @touch $@
375 gen/gen-sizes.stamp: gen/gen-sizes.c libmlton.a $(HFILES)
376 $(CROSS_PREFIX)$(CC) $(OPTCFLAGS) -o gen/gen-sizes gen/gen-sizes.c -L. -lmlton
377 rm -f gen/sizes
378 cd gen && ./gen-sizes
379 rm -f gen/gen-sizes$(EXE) gen/gen-sizes
380 touch $@
381
382
383 ######
384
385
386 %.a:
387 rm -f $@
388 $(CROSS_PREFIX)$(AR) rc $@ $^
389 $(CROSS_PREFIX)$(RANLIB) $@
390
391
392
393 .PHONY: flags
394 flags:
395 @echo TARGET = $(TARGET)
396 @echo TARGET_ARCH = $(TARGET_ARCH)
397 @echo TARGET_OS = $(TARGET_OS)
398 @echo GCC_MAJOR_VERSION = $(GCC_MAJOR_VERSION)
399 @echo GCC_MINOR_VERSION = $(GCC_MINOR_VERSION)
400 @echo GCC_VERSION = $(GCC_VERSION)
401 @echo EXE = $(EXE)
402 @echo XCFLAGS = $(XCFLAGS)
403 @echo OPTXCFLAGS = $(OPTXCFLAGS)
404 @echo DEBUGXCFLAGS = $(DEBUGXCFLAGS)
405 @echo PICXCFLAGS = $(PICXCFLAGS)
406
407
408 .PHONY: clean
409 clean:
410 ../bin/clean