source: mainline/uspace/Makefile.common@ e768aea

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e768aea was 2f7d77c6, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Add rudimentary support for undefined behavior sanitizer.

  • Property mode set to 100644
File size: 9.9 KB
Line 
1#
2# Copyright (c) 2005 Martin Decky
3# Copyright (c) 2007 Jakub Jermar
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9#
10# - Redistributions of source code must retain the above copyright
11# notice, this list of conditions and the following disclaimer.
12# - Redistributions in binary form must reproduce the above copyright
13# notice, this list of conditions and the following disclaimer in the
14# documentation and/or other materials provided with the distribution.
15# - The name of the author may not be used to endorse or promote products
16# derived from this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29
30# Individual makefiles set:
31#
32# USPACE_PREFIX (*) relative path to uspace/ directory
33# SOURCES (*) list of source files
34# LIBS libraries to link with
35# DEFS compiler defines
36# EXTRA_CFLAGS additional flags to pass to C compiler
37# LINKER_SCRIPT linker script
38# PRE_DEPEND targets required for dependency check
39#
40# BINARY (/) binary output name (like appname)
41# LIBRARY (/) library output name (like libname)
42#
43# EXTRA_OUTPUT additional output targets
44# EXTRA_CLEAN additional cleanup targets
45#
46# Optionally, for a binary:
47# STATIC_NEEDED set to 'y' for init binaries, will build statically
48# linked version
49# STATIC_ONLY set to 'y' if binary cannot be linked dynamically
50# (e.g. uses thread-local variables)
51#
52# Optionally, for a library:
53# SOVERSION shared library version (major.minor),
54# if missing, no shared library is built
55#
56# (x) required variables
57# (/) exactly one of the variables must be defined
58#
59
60ROOT_PATH = $(USPACE_PREFIX)/..
61
62VERSION_DEF = $(ROOT_PATH)/version
63
64COMMON_MAKEFILE = $(ROOT_PATH)/Makefile.common
65COMMON_HEADER = $(ROOT_PATH)/common.h
66
67CONFIG_MAKEFILE = $(ROOT_PATH)/Makefile.config
68CONFIG_HEADER = $(ROOT_PATH)/config.h
69
70-include $(VERSION_DEF)
71-include $(COMMON_MAKEFILE)
72-include $(CONFIG_MAKEFILE)
73
74OUTPUTS = $(EXTRA_OUTPUT)
75
76ifneq ($(BINARY),)
77 JOB = $(BINARY).job
78 TEST_BINARY = test-$(BINARY)
79 OUTPUTS += $(BINARY) $(BINARY).disasm
80 EXTRA_CLEAN += $(BINARY).map
81endif
82
83ifneq ($(LIBRARY),)
84 JOB = $(LIBRARY).job
85 TEST_BINARY = test-$(LIBRARY)
86 OUTPUTS += $(LIBRARY).a
87endif
88
89ifeq ($(CONFIG_BUILD_SHARED_LIBS),y)
90 ifneq ($(SOVERSION),)
91 SLIBRARY = $(LIBRARY).so.$(SOVERSION)
92 LSONAME = $(LIBRARY).so.$(basename $(SOVERSION))
93 OUTPUTS += $(SLIBRARY) $(SLIBRARY).disasm $(LSONAME)
94 EXTRA_CLEAN += $(LIBRARY).la $(SLIBRARY).map
95 endif
96endif
97
98LIB_PREFIX = $(USPACE_PREFIX)/lib
99
100LIBC_PREFIX = $(LIB_PREFIX)/c
101LIBC_INCLUDES_FLAGS = \
102 -I$(LIBC_PREFIX)/include \
103 -I$(LIBC_PREFIX)/arch/$(UARCH)/include \
104 -I$(ROOT_PATH)/abi/include
105LIBSOFTFLOAT_PREFIX = $(LIB_PREFIX)/softfloat
106LIBSOFTINT_PREFIX = $(LIB_PREFIX)/softint
107
108LIBMATH_PREFIX = $(LIB_PREFIX)/math
109LIBMATH_INCLUDES_FLAGS = \
110 -I$(LIBMATH_PREFIX)/include \
111 -I$(LIBMATH_PREFIX)/arch/$(UARCH)/include
112
113LIBPOSIX_PREFIX = $(LIB_PREFIX)/posix
114LIBDLTEST_PREFIX = $(LIB_PREFIX)/dltest
115
116AFLAGS = --fatal-warnings
117LDFLAGS = -Wl,--fatal-warnings,--warn-common
118
119ifeq ($(STATIC_NEEDED),y)
120 STATIC_BUILD = y
121else
122 ifeq ($(STATIC_ONLY),y)
123 STATIC_BUILD = y
124 else
125 ifeq ($(CONFIG_USE_SHARED_LIBS),y)
126 STATIC_BUILD = n
127 else
128 STATIC_BUILD = y
129 endif
130 endif
131endif
132
133ifeq ($(STATIC_BUILD),y)
134 BASE_LIBS = $(LIBC_PREFIX)/libc.a
135else
136 BASE_LIBS = $(LIBC_PREFIX)/libc.so.0
137 LINK_DYNAMIC = y
138endif
139
140BASE_LIBS += $(LIBSOFTFLOAT_PREFIX)/libsoftfloat.a $(LIBSOFTINT_PREFIX)/libsoftint.a
141
142ifeq ($(LINK_DYNAMIC),y)
143 LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link-dlexe.ld
144else
145 LDFLAGS += -static
146 LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld
147endif
148
149LIB_LINKER_SCRIPT = $(LIBC_PREFIX)/arch/$(UARCH)/_link-shlib.ld
150
151INCLUDES_FLAGS = $(LIBC_INCLUDES_FLAGS)
152
153ifneq ($(LIBRARY),)
154 INCLUDES_FLAGS += -Iinclude -I.
155endif
156
157INCLUDES_FLAGS += $(foreach lib,$(LIBS), -I$(LIB_PREFIX)/$(lib) -I$(LIB_PREFIX)/$(lib)/include)
158
159# TODO: get rid of this special case
160ifneq ($(filter math, $(LIBS)),)
161 INCLUDES_FLAGS += $(LIBMATH_INCLUDES_FLAGS)
162endif
163
164# PCUT-based unit tests
165ifneq ($(TEST_SOURCES),)
166 TEST_OUTPUTS = $(TEST_BINARY) $(TEST_BINARY).disasm
167 TEST_CFLAGS = -I$(LIB_PREFIX)/pcut/include -D__helenos__
168 TEST_BINARY_LIBS = $(LIB_PREFIX)/pcut/libpcut.a
169 EXTRA_CLEAN += $(TEST_OUTPUTS) $(TEST_BINARY).map
170ifneq ($(LIBRARY),)
171 TEST_BINARY_LIBS += $(LIBRARY).a
172endif
173 TEST_BINARY_LIBS += $(TEST_LIBS)
174endif
175
176# Flags that are not necessary, and can be overriden, but are used by default.
177DEFAULT_CFLAGS = \
178 -O$(OPTIMIZATION) \
179 -ffunction-sections \
180 -pipe \
181 -Wall \
182 -Wextra \
183 -Wno-unused-parameter \
184 -Wmissing-prototypes \
185 -Wwrite-strings \
186 -Werror-implicit-function-declaration
187
188ifeq ($(CONFIG_DEBUG),y)
189 DEFAULT_CFLAGS += -Werror
190endif
191
192ifeq ($(CONFIG_UBSAN),y)
193 DEFAULT_CFLAGS += -fsanitize=undefined
194endif
195
196ifeq ($(COMPILER),clang)
197 DEFAULT_CFLAGS += \
198 -Wno-missing-field-initializers \
199 -Wno-typedef-redefinition \
200 -Wno-unused-command-line-argument
201else
202 DEFAULT_CFLAGS += \
203 -Wno-clobbered
204endif
205
206ifeq ($(CONFIG_LINE_DEBUG),y)
207 DEFAULT_CFLAGS += -ggdb
208endif
209
210# Flags that should always be used, even for third-party software.
211COMMON_CPPFLAGS = \
212 -nostdinc \
213 -D__$(ENDIANESS)__
214
215COMMON_CFLAGS = \
216 -ffreestanding \
217 -nostdlib
218
219# Flags that are always used for HelenOS code, but not for third-party.
220HELENOS_CFLAGS = \
221 -std=gnu99 \
222 $(INCLUDES_FLAGS) \
223 -imacros $(CONFIG_HEADER) \
224 -D_HELENOS_SOURCE \
225 -fexec-charset=UTF-8 \
226 -finput-charset=UTF-8 \
227 -fno-common \
228 -fdebug-prefix-map=$(realpath $(ROOT_PATH))=.
229
230# TODO: Use a different name.
231# CFLAGS variable is traditionally used for overridable flags.
232CFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CFLAGS) $(HELENOS_CFLAGS) $(DEFAULT_CFLAGS)
233
234## Setup platform configuration
235#
236
237-include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.common
238
239## Compilation options
240#
241
242ifeq ($(PRECHECK),y)
243 JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py
244 # XXX: Do not change the order of arguments.
245 CC_JOB = $(JOBFILE) $(JOB) $(CC) $< -o $@
246else
247 CC_JOB = $(CC) $< -o $@
248endif
249
250ifeq ($(CONFIG_STRIP_BINARIES),y)
251 LDFLAGS += -s
252endif
253
254LIB_CFLAGS = $(CFLAGS) -fPIC
255LIB_LDFLAGS = $(LDFLAGS) -shared -Wl,-soname,$(LSONAME)
256
257AS_CFLAGS := $(addprefix -Xassembler ,$(AFLAGS))
258
259OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
260LOBJECTS := $(addsuffix .lo,$(basename $(SOURCES)))
261TEST_OBJECTS := $(addsuffix .test.o,$(basename $(TEST_SOURCES)))
262DEPENDS := $(addsuffix .d,$(basename $(SOURCES))) $(addsuffix .test.d,$(basename $(TEST_SOURCES)))
263
264LIBTAGS := $(foreach lib,$(LIBS), $(USPACE_PREFIX)/lib/$(lib)/tag)
265LIBARGS := $(addprefix -L$(USPACE_PREFIX)/lib/, $(LIBS)) $(addprefix -l, $(LIBS))
266
267.PHONY: all all-test clean fasterclean depend
268
269all: tag $(OUTPUTS)
270
271all-test: $(TEST_OUTPUTS)
272
273clean: fasterclean
274 find . -name '*.o' -follow -exec rm \{\} \;
275 find . -name '*.lo' -follow -exec rm \{\} \;
276 find . -name '*.d' -follow -exec rm \{\} \;
277
278fasterclean:
279 rm -rf $(JOB) $(OUTPUTS) $(EXTRA_CLEAN) tag deps.mk
280
281depend: $(PRE_DEPEND)
282
283# "Tag" files are used to force relink of binaries when dependencies get rebuilt,
284# regardless of whether the dependency was linked statically or dynamically,
285# or which version of a dynamic library was used. Prerequisites for this file
286# are defined further down.
287tag:
288 touch tag
289
290# Generate inter-module make dependencies.
291# This is needed to ensure correct build order of libraries and code depending on them.
292deps.mk: Makefile
293 echo > $@.new
294 for lib in $(LIBS); do \
295 echo "$(SELF_TARGET): lib/$$lib.build" >> $@.new; \
296 done
297 mv -f $@.new $@
298
299%.disasm: %
300ifeq ($(CONFIG_LINE_DEBUG),y)
301 $(OBJDUMP) -d -S $< > $@
302else
303 $(OBJDUMP) -d $< > $@
304endif
305
306ifneq ($(BINARY),)
307$(BINARY): $(LINKER_SCRIPT) $(OBJECTS) $(LIBTAGS) $(BASE_LIBS)
308 $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -T $(LINKER_SCRIPT) -Wl,-Map,$@.map -o $@ $(OBJECTS) $(LIBARGS) $(BASE_LIBS)
309endif
310
311ifneq ($(TEST_BINARY),)
312$(TEST_BINARY): $(LINKER_SCRIPT) $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBTAGS) $(BASE_LIBS)
313 $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -T $(LINKER_SCRIPT) -Wl,-Map,$@.map -o $@ $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBARGS) $(BASE_LIBS)
314endif
315
316ifneq ($(LIBRARY),)
317tag: $(LIBRARY).a
318
319$(LIBRARY).a: $(OBJECTS)
320 $(AR) rc $@ $(OBJECTS)
321endif
322
323ifneq ($(SLIBRARY),)
324tag: $(SLIBRARY)
325
326$(LIBRARY).la: $(LOBJECTS)
327 $(AR) rc $@ $(LOBJECTS)
328
329$(SLIBRARY): $(LIB_LINKER_SCRIPT) $(LIBRARY).la
330 $(CC) $(CFLAGS) $(LIB_LDFLAGS) $(EXTRA_LDFLAGS) -T $(LIB_LINKER_SCRIPT) -Wl,-Map,$@.map -o $@ -Wl,--whole-archive $(LIBRARY).la -Wl,--no-whole-archive
331
332$(LSONAME):
333 ln -s $(SLIBRARY) $@
334endif
335
336%.o: %.S | depend
337 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
338
339%.o: %.s | depend
340 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
341
342%.o: %.c | depend
343 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS)
344
345%.test.o: %.c | depend
346 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(TEST_CFLAGS)
347
348%.lo: %.S | depend
349 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
350
351%.lo: %.s | depend
352 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
353
354%.lo: %.c | depend
355 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS)
356
357-include $(DEPENDS)
358
Note: See TracBrowser for help on using the repository browser.