summaryrefslogtreecommitdiff
path: root/makefiles/gmake/compiler.mk
blob: c2183d8540399c7f77d2266062aa2a703a732b8c (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
# sets compiler settings
#
# requires:
# - INCLUDE_DIRS
#
# provides:
# - BIN_OBJS: the object files we need for the binaries which we build always
# - TEST_BIN_OBJS: same as BIN_OBJS but for test binaries compiled only when
#   testing
#

# start of gcc section

ifeq "$(COMPILER)" "gcc"

GCC_MAJOR_VERSION ?=	$(shell $(TOPDIR)/makefiles/gmake/guess_env --gcc-major-version $(CC) "$(CURDIR)" $(TOPDIR))
GCC_MINOR_VERSION ?=	$(shell $(TOPDIR)/makefiles/gmake/guess_env --gcc-minor-version $(CC) "$(CURDIR)" $(TOPDIR))

# -Wswitch-default: not good for switches with enums
# -Wsystem-headers: bad idea, as header files are usually happily broken :-)
# -Wtraditional: we don't want to program tradition K&R C anymore!
# -Wunsafe-loop-optimizations: ??
# -Wno-attributes, -Wmissing-format-attribute: ?? later
# -Wpacked -Wpadded: ?? very questionable
# -Wunreachable-code: doesn't work
# -Wno-div-by-zero: we get NaN and friend over macros, so need for funny tricks :-)
# -Wstrict-overflow=5 is relatively new, later maybe
# -fstack-protector or -fstack-protector-all: should be used, but I
#  have currently big problems to get it around compiler gcc and -lssl
#  probing! FIXME later
# -fstack-protector-all: does something funny to the shared objects..
# -Wstack-protector makes no sense without SSP
# everything implied by -Wall is not explicitly specified (gcc 4.2.3)
# TODO: reenable -O2
# -Waggregate-return: is for K&R code and mostly useless nowadays
# TODO: snprintf Solaris 8 and cygwin croaks on -Wfloat-equal, compilers are
# too old for diagnostic pragmas, drop -Wfloat-equal for now

# compilation flags and compilers
COMMON_COMPILE_FLAGS = \
	-g \
	-fstrict-aliasing \
	-pedantic -Wall -Werror \
	-Wunused -Wno-import \
	-Wformat -Wformat-y2k -Wformat-nonliteral -Wformat-security \
	-Wswitch-enum -Wunknown-pragmas \
	-Wundef -Wshadow -Wpointer-arith \
	-Wcast-qual -Wcast-align \
	-Wwrite-strings \
	-Wmissing-noreturn \
	-Wno-multichar -Wparentheses -Wredundant-decls \
	-Winline \
	-Wdisabled-optimization
ifeq "$(GCC_MAJOR_VERSION)" "4"
COMMON_COMPILE_FLAGS += \
	-Wfatal-errors -Wmissing-include-dirs -Wvariadic-macros \
	-Wvolatile-register-var \
	-Wstrict-aliasing=2 -Wextra -Winit-self
# -Wconversion had to meanings before gcc 4.3 (warn about ABI changes when porting
# old K&R code without function prototypes) and warn about conversions loosing
# precision. So we enable only -Wconversion (not -Wtraditional-conversion) for gcc
# >= 4.3 and no -Wconversion for older gcc versions!
# (see also http://gcc.gnu.org/wiki/NewWconversion)
ifeq "$(GCC_MINOR_VERSION)" "3"
COMMON_COMPILE_FLAGS += -Wconversion
endif

endif

ifeq "$(GCC_MAJOR_VERSION)" "3"

# gcc 3.3, testend on OpenBSD 4.2
ifeq "$(GCC_MINOR_VERSION)" "3"
COMMON_COMPILE_FLAGS += \
	-W
endif

# gcc 3.4, not tested yet
ifeq "$(GCC_MINOR_VERSION)" "4"
COMMON_COMPILE_FLAGS += \
	-Wstrict-aliasing=2 -Wextra -Winit-self
endif

endif

STD99_COMPILE_FLAGS = \
	-std=c99

COMPILE_FLAGS = \
	$(COMMON_COMPILE_FLAGS) \
	$(STD99_COMPILE_FLAGS) \
	-Wnonnull \
	-Wbad-function-cast -Wstrict-prototypes \
	-Wmissing-prototypes -Wmissing-declarations \
	-Wnested-externs

# gcc 4.x
ifeq "$(GCC_MAJOR_VERSION)" "4"
COMPILE_FLAGS += \
	-Wc++-compat -Wdeclaration-after-statement -Wold-style-definition \
	-funit-at-a-time
endif

ifeq "$(GCC_MAJOR_VERSION)" "3"

# gcc 3.4, not tested yet
ifeq "$(GCC_MINOR_VERSION)" "4"
COMPILE_FLAGS += \
	-Wdeclaration-after-statement -Wold-style-definition \
	-funit-at-a-time
endif

# gcc 3.3, testend on OpenBSD 4.2
ifeq "$(GCC_MINOR_VERSION)" "3"
#COMPILE_FLAGS += \
#	-Wdeclaration-after-statement
endif

endif

# gcc 4.x
ifeq "$(GCC_MAJOR_VERSION)" "4"
endif

ifeq "$(GCC_MAJOR_VERSION)" "3"
endif

#CC = gcc

endif

# end of gcc section

# start of tcc section

# currently we don't need this, the tcc flags are fairly consistent
#TCC_MAJOR_VERSION ?=	$(shell $(TOPDIR)/makefiles/gmake/guess_env --tcc-major-version $(CC) "$(CURDIR)" $(TOPDIR))
#TCC_MINOR_VERSION ?=	$(shell $(TOPDIR)/makefiles/gmake/guess_env --tcc-minor-version $(CC) "$(CURDIR)" $(TOPDIR))

ifeq "$(COMPILER)" "tcc"
COMPILE_FLAGS = \
	-Wall -Werror -Wunsupported -Wwrite-strings -g
endif

# end of tcc section

# start of icc section

# currently we don't need this, the icc flags are fairly consistent
#ICC_MAJOR_VERSION ?=	$(shell $(TOPDIR)/makefiles/gmake/guess_env --icc-major-version $(CC) "$(CURDIR)" $(TOPDIR))
#ICC_MINOR_VERSION ?=	$(shell $(TOPDIR)/makefiles/gmake/guess_env --icc-minor-version $(CC) "$(CURDIR)" $(TOPDIR))

# -vec-report0: turn of SSE2 vector usage messages (they are common since P-4 anyway!)

ifeq "$(COMPILER)" "icc"
COMPILE_FLAGS = \
	-Wall -Werror -w1 -vec-report0
endif

# end of icc section

# start of spro section

# -xc99=all: full C99 compliance for the code (syntax and library functions)
# -Xc: full ISO compliance, no K&R stuf
# -mt: enable mutlithreading (-D_REENTRANT for header files, -lthread for ld)
# -errwarn=%all: convert all warnings to errors
# -v: do more restrictive syntax checking
# TODO: enable -O2

ifeq "$(COMPILER)" "spro"
STD99_COMPILE_FLAGS = \
	-xc99=all
COMPILE_FLAGS = \
	$(STD99_COMPILE_FLAGS) -Xc -errwarn=%all -mt -v
endif

# end of spro section

# start of pcc section

# currently we don't need this, the pcc flags are fairly consistent
#PCC_MAJOR_VERSION ?=	$(shell $(TOPDIR)/makefiles/gmake/guess_env --pcc-major-version $(CC) "$(CURDIR)" $(TOPDIR))
#PCC_MINOR_VERSION ?=	$(shell $(TOPDIR)/makefiles/gmake/guess_env --pcc-minor-version $(CC) "$(CURDIR)" $(TOPDIR))

ifeq "$(COMPILER)" "pcc"
COMPILE_FLAGS = \
	--fatal-warnings -g
endif

# end of pcc section

ifeq "$(ENABLE_THREADS)" "1"

# set flags for threading support using POSIX threads. This is completly different
# between compiler/platforms
ifeq "$(COMPILER)" "gcc"
ifeq "$(PLATFORM)" "LINUX"
PTHREADS_CFLAGS = -D_REENTRANT -pthread
PTHREADS_LDFLAGS = -pthread
PTHREADS_LIBS =
endif
ifeq "$(PLATFORM)" "SUNOS"
PTHREADS_CFLAGS = -D_REENTRANT -pthreads
PTHREADS_LDFLAGS = -pthreads
PTHREADS_LIBS = 
endif
ifeq "$(PLATFORM)" "FREEBSD"
PTHREADS_CFLAGS = -D_REENTRANT -pthread
PTHREADS_LDFLAGS = -pthread
PTHREADS_LIBS =
endif
ifeq "$(PLATFORM)" "NETBSD"
PTHREADS_CFLAGS = -D_REENTRANT -pthread
PTHREADS_LDFLAGS = -pthread
PTHREADS_LIBS =
endif
ifeq "$(PLATFORM)" "OPENBSD"
PTHREADS_CFLAGS = -D_REENTRANT -pthread
PTHREADS_LDFLAGS = -pthread
PTHREADS_LIBS =
endif
ifeq "$(PLATFORM)" "CYGWIN"
PTHREADS_CFLAGS =
PTHREADS_LDFLAGS =
PTHREADS_LIBS =
endif
endif

ifeq "$(COMPILER)" "tcc"
ifeq "$(PLATFORM)" "LINUX"
PTHREADS_CFLAGS = -D_REENTRANT
PTHREADS_LDFLAGS =
PTHREADS_LIBS = -lpthread
endif
endif

ifeq "$(COMPILER)" "pcc"
ifeq "$(PLATFORM)" "LINUX"
PTHREADS_CFLAGS = -D_REENTRANT -pthread
PTHREADS_LDFLAGS = -pthread
PTHREADS_LIBS =
endif
endif

ifeq "$(COMPILER)" "icc"
ifeq "$(PLATFORM)" "LINUX"
PTHREADS_CFLAGS = -D_REENTRANT -pthread
PTHREADS_LDFLAGS = -pthread
PTHREADS_LIBS =
endif
endif

ifeq "$(COMPILER)" "clang"
PTHREADS_CFLAGS = -D_REENTRANT -pthread
PTHREADS_LDFLAGS = -pthread
PTHREADS_LIBS =
endif

endif

# set flags for position independend code (as required for shared libraries
# on some platforms)
ifeq "$(COMPILER)" "gcc"
ifeq "$(PLATFORM)" "LINUX"
SO_COMPILE_FLAGS = -fPIC
endif
ifeq "$(PLATFORM)" "SUNOS"
SO_COMPILE_FLAGS = -fPIC
endif
ifeq "$(PLATFORM)" "FREEBSD"
SO_COMPILE_FLAGS = -fPIC
endif
ifeq "$(PLATFORM)" "NETBSD"
SO_COMPILE_FLAGS = -fPIC
endif
ifeq "$(PLATFORM)" "OPENBSD"
SO_COMPILE_FLAGS = -fPIC
endif
ifeq "$(PLATFORM)" "CYGWIN"
# code on Cygwin is always position independend
SO_COMPILE_FLAGS =
endif
endif

ifeq "$(COMPILER)" "pcc"
ifeq "$(PLATFORM)" "LINUX"
SO_COMPILE_FLAGS = -fPIC
endif
endif

ifeq "$(COMPILER)" "icc"
ifeq "$(PLATFORM)" "LINUX"
SO_COMPILE_FLAGS = -fPIC
endif
endif

# TODO: test this
#ifeq "$(COMPILER)" "spro"
#ifeq "$(PLATFORM)" "SUNOS"
#ifeq "$(ARCH)" "sun4u"
#SO_COMPILE_FLAGS = -xcode=pic32
#endif
#endif
#endif

ifeq "$(COMPILER)" "clang"
SO_COMPILE_FLAGS =
endif

ALL_CFLAGS = $(CFLAGS) $(COMPILE_FLAGS) $(PLATFORM_COMPILE_FLAGS) $(INCLUDE_DIRS) $(INCLUDE_CFLAGS) $(PTHREADS_CFLAGS)
ALL_LDFLAGS = $(LDFLAGS) $(INCLUDE_LDFLAGS) $(PTHREADS_LDFLAGS) $(LDFLAGS_NET) $(LDFLAGS_LT) $(LDFLAGS_DL)

LIBS = $(INCLUDE_LIBS) $(PTHREADS_LIBS) $(LIBS_NET) $(LIBS_LT) $(LIBS_DL)
LINK = $(CC)

%.o : %.c
	$(CC) -c -o $@ $(ALL_CFLAGS) $<

%$(EXE): %.o $(OBJS) $(TEST_OBJS)
	$(LINK) -o $@ $(ALL_LDFLAGS) $(OBJS) $(TEST_OBJS) $< $(LIBS)

%.sho : %.c
	$(CC) -c -o $@ $(SO_COMPILE_FLAGS) -DSHARED $(ALL_CFLAGS) $<

%$(SO) : %.sho $(OBJS)
	$(LINK) -shared -o $@ $(ALL_LDFLAGS) $(LIBS) $(OBJS) $<

BIN_OBJS = $(BINS:$(EXE)=.o) 
TEST_BIN_OBJS = $(TEST_BINS:$(EXE)=.o)
SH_OBJS = $(OBJS:.o=.sho)