source: mainline/uspace/Makefile.common@ 9c5e3a5

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

Enable -Werror for C++.

  • Property mode set to 100644
File size: 11.7 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
105
106LIBCPP_PREFIX = $(LIB_PREFIX)/cpp
107LIBCPP_INCLUDES_FLAGS = -I$(LIBCPP_PREFIX)/include
108
109LIBSOFTFLOAT_PREFIX = $(LIB_PREFIX)/softfloat
110LIBSOFTINT_PREFIX = $(LIB_PREFIX)/softint
111
112LIBMATH_PREFIX = $(LIB_PREFIX)/math
113LIBMATH_INCLUDES_FLAGS = \
114 -I$(LIBMATH_PREFIX)/include \
115 -I$(LIBMATH_PREFIX)/arch/$(UARCH)/include
116
117LIBPOSIX_PREFIX = $(LIB_PREFIX)/posix
118LIBDLTEST_PREFIX = $(LIB_PREFIX)/dltest
119
120START_FILES = $(LIBC_PREFIX)/crt0.o $(LIBC_PREFIX)/crt1.o
121
122AFLAGS = --fatal-warnings
123LDFLAGS = -Wl,--fatal-warnings,--warn-common
124
125ifeq ($(STATIC_NEEDED),y)
126 STATIC_BUILD = y
127else
128 ifeq ($(STATIC_ONLY),y)
129 STATIC_BUILD = y
130 else
131 ifeq ($(CONFIG_USE_SHARED_LIBS),y)
132 STATIC_BUILD = n
133 else
134 STATIC_BUILD = y
135 endif
136 endif
137endif
138
139ifneq ($(STATIC_BUILD),y)
140 LINK_DYNAMIC = y
141endif
142
143BASE_LIBS =
144
145ifneq ($(LIBRARY),libc)
146 ifeq ($(STATIC_BUILD),y)
147 BASE_LIBS += $(LIBC_PREFIX)/libc.a
148 else
149 BASE_LIBS += $(LIBC_PREFIX)/libc.so.0
150 endif
151endif
152
153BASE_LIBS += $(LIBSOFTFLOAT_PREFIX)/libsoftfloat.a $(LIBSOFTINT_PREFIX)/libsoftint.a
154
155ifeq ($(LINK_DYNAMIC),y)
156 LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link-dlexe.ld
157else
158 LDFLAGS += -static
159 LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld
160endif
161
162LIB_LINKER_SCRIPT = $(LIBC_PREFIX)/arch/$(UARCH)/_link-shlib.ld
163
164INCLUDES_FLAGS = $(LIBC_INCLUDES_FLAGS)
165
166ifneq ($(LIBRARY),)
167 INCLUDES_FLAGS += -Iinclude -I.
168endif
169
170INCLUDES_FLAGS += $(foreach lib,$(LIBS), -I$(LIB_PREFIX)/$(lib) -I$(LIB_PREFIX)/$(lib)/include)
171
172# TODO: get rid of this special case
173ifneq ($(filter math, $(LIBS)),)
174 INCLUDES_FLAGS += $(LIBMATH_INCLUDES_FLAGS)
175endif
176
177# PCUT-based unit tests
178ifneq ($(TEST_SOURCES),)
179 TEST_OUTPUTS = $(TEST_BINARY) $(TEST_BINARY).disasm
180 TEST_CFLAGS = -I$(LIB_PREFIX)/pcut/include -D__helenos__ $(EXTRA_TEST_CFLAGS)
181 TEST_BINARY_LIBS = $(LIB_PREFIX)/pcut/libpcut.a
182 EXTRA_CLEAN += $(TEST_OUTPUTS) $(TEST_BINARY).map
183ifneq ($(LIBRARY),)
184 TEST_BINARY_LIBS += $(LIBRARY).a
185endif
186 TEST_BINARY_LIBS += $(TEST_LIBS)
187endif
188
189# Flags that are not necessary, and can be overriden, but are used by default.
190DEFAULT_CFLAGS = \
191 -O$(OPTIMIZATION) \
192 -ffunction-sections \
193 -pipe \
194 -Wall \
195 -Wextra \
196 -Wno-unused-parameter \
197 -Wmissing-prototypes \
198 -Wwrite-strings \
199 -Werror-implicit-function-declaration
200
201ifeq ($(CONFIG_DEBUG),y)
202 DEFAULT_CFLAGS += -Werror
203endif
204
205ifeq ($(CONFIG_UBSAN),y)
206 DEFAULT_CFLAGS += -fsanitize=undefined
207endif
208
209ifeq ($(COMPILER),clang)
210 DEFAULT_CFLAGS += \
211 -Wno-missing-field-initializers \
212 -Wno-typedef-redefinition \
213 -Wno-unused-command-line-argument
214else
215 DEFAULT_CFLAGS += \
216 -Wno-clobbered
217endif
218
219ifeq ($(CONFIG_LINE_DEBUG),y)
220 DEFAULT_CFLAGS += -ggdb
221endif
222
223# Flags that should always be used, even for third-party software.
224COMMON_CPPFLAGS = \
225 -nostdinc \
226 -D__$(ENDIANESS)__
227
228COMMON_CFLAGS = \
229 -ffreestanding \
230 -nostdlib
231
232# Flags that are always used for HelenOS code, but not for third-party.
233HELENOS_CFLAGS = \
234 -std=gnu99 \
235 $(INCLUDES_FLAGS) \
236 -imacros $(CONFIG_HEADER) \
237 -D_HELENOS_SOURCE \
238 -fexec-charset=UTF-8 \
239 -finput-charset=UTF-8 \
240 -fno-common \
241 -fdebug-prefix-map=$(realpath $(ROOT_PATH))=.
242
243# TODO: Use a different name.
244# CFLAGS variable is traditionally used for overridable flags.
245CFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CFLAGS) $(HELENOS_CFLAGS) $(DEFAULT_CFLAGS)
246
247# Flags for the compilation of C++ code.
248CXX_BASE_LIBS = $(LIBCPP_PREFIX)/libcpp.a $(BASE_LIBS)
249DEFAULT_CXXFLAGS = \
250 -O$(OPTIMIZATION) \
251 -ffunction-sections \
252 -pipe \
253 -Wall \
254 -Wextra \
255 -Wno-unused-parameter \
256 -Wwrite-strings \
257 -Werror-implicit-function-declaration
258
259ifeq ($(CONFIG_DEBUG),y)
260 DEFAULT_CXXFLAGS += -Werror
261endif
262
263COMMON_CXXFLAGS = $(COMMON_CFLAGS)
264HELENOS_CXXFLAGS = \
265 -std=c++17 -frtti \
266 $(LIBCPP_INCLUDES_FLAGS) $(INCLUDES_FLAGS) \
267 -imacros $(CONFIG_HEADER) \
268 -D_HELENOS_SOURCE \
269 -fexec-charset=UTF-8 \
270 -finput-charset=UTF-8 \
271 -fno-common \
272 -fdebug-prefix-map=$(realpath $(ROOT_PATH))=.
273
274CXXFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CXXFLAGS) $(HELENOS_CXXFLAGS) $(DEFAULT_CXXFLAGS)
275
276## Setup platform configuration
277#
278
279-include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.common
280
281## Compilation options
282#
283
284ifeq ($(PRECHECK),y)
285 JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py
286 # XXX: Do not change the order of arguments.
287 CC_JOB = $(JOBFILE) $(JOB) $(CC) $< -o $@
288 CXX_JOB = $(JOBFILE) $(JOB) $(CXX) $< -o $@
289else
290 CC_JOB = $(CC) $< -o $@
291 CXX_JOB = $(CXX) $< -o $@
292endif
293
294ifeq ($(CONFIG_STRIP_BINARIES),y)
295 LDFLAGS += -s
296endif
297
298LIB_CFLAGS = $(CFLAGS) -fPIC
299LIB_LDFLAGS = $(LDFLAGS) -shared -Wl,-soname,$(LSONAME) -Wl,--no-undefined,--no-allow-shlib-undefined
300
301AS_CFLAGS := $(addprefix -Xassembler ,$(AFLAGS))
302
303OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
304LOBJECTS := $(addsuffix .lo,$(basename $(SOURCES)))
305TEST_OBJECTS := $(addsuffix .test.o,$(basename $(TEST_SOURCES)))
306DEPENDS := $(addsuffix .d,$(basename $(SOURCES))) $(addsuffix .test.d,$(basename $(TEST_SOURCES)))
307
308LIBTAGS := $(foreach lib,$(LIBS), $(USPACE_PREFIX)/lib/$(lib)/tag)
309LIBARGS := $(addprefix -L$(USPACE_PREFIX)/lib/, $(LIBS)) $(addprefix -l, $(LIBS))
310
311.PHONY: all all-test clean fasterclean depend
312
313all: tag $(OUTPUTS)
314
315all-test: $(TEST_OUTPUTS)
316
317clean: fasterclean
318 find . -name '*.o' -follow -exec rm \{\} \;
319 find . -name '*.lo' -follow -exec rm \{\} \;
320 find . -name '*.d' -follow -exec rm \{\} \;
321
322fasterclean:
323 rm -rf $(JOB) $(OUTPUTS) $(EXTRA_CLEAN) tag deps.mk
324
325depend: $(PRE_DEPEND)
326
327# "Tag" files are used to force relink of binaries when dependencies get rebuilt,
328# regardless of whether the dependency was linked statically or dynamically,
329# or which version of a dynamic library was used. Prerequisites for this file
330# are defined further down.
331tag:
332 touch tag
333
334# Generate inter-module make dependencies.
335# This is needed to ensure correct build order of libraries and code depending on them.
336deps.mk: Makefile
337 echo > $@.new
338 for lib in $(LIBS); do \
339 echo "$(SELF_TARGET): lib/$$lib.build" >> $@.new; \
340 done
341 mv -f $@.new $@
342
343%.disasm: %
344ifeq ($(CONFIG_LINE_DEBUG),y)
345 $(OBJDUMP) -d -S $< > $@
346else
347 $(OBJDUMP) -d $< > $@
348endif
349
350ifneq ($(BINARY),)
351
352ifneq ($(filter %.cpp %.cc %.cxx, $(SOURCES)),)
353$(BINARY): $(LINKER_SCRIPT) $(OBJECTS) $(LIBTAGS)
354 $(CXX) $(CXXFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -T $(LINKER_SCRIPT) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(CXX_BASE_LIBS)
355else
356$(BINARY): $(LINKER_SCRIPT) $(OBJECTS) $(LIBTAGS)
357 $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -T $(LINKER_SCRIPT) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(BASE_LIBS)
358endif
359
360endif
361
362ifneq ($(TEST_BINARY),)
363$(TEST_BINARY): $(LINKER_SCRIPT) $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBTAGS)
364 $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -T $(LINKER_SCRIPT) -Wl,-Map,$@.map -o $@ $(START_FILES) $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBARGS) $(BASE_LIBS)
365endif
366
367ifneq ($(LIBRARY),)
368tag: $(LIBRARY).a
369
370$(LIBRARY).a: $(OBJECTS)
371 $(AR) rc $@ $(OBJECTS)
372endif
373
374ifneq ($(SLIBRARY),)
375tag: $(SLIBRARY)
376
377$(LIBRARY).la: $(LOBJECTS)
378 $(AR) rc $@ $(LOBJECTS)
379
380$(SLIBRARY): $(LIB_LINKER_SCRIPT) $(LIBRARY).la
381 $(CC) $(CFLAGS) $(LIB_LDFLAGS) $(EXTRA_LDFLAGS) -T $(LIB_LINKER_SCRIPT) -Wl,-Map,$@.map -o $@ -Wl,--whole-archive $(LIBRARY).la -Wl,--no-whole-archive $(LIBARGS) $(BASE_LIBS)
382
383$(LSONAME):
384 ln -s $(SLIBRARY) $@
385endif
386
387%.o: %.S | depend
388 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
389
390%.o: %.s | depend
391 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
392
393%.o: %.c | depend
394 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS)
395
396%.o: %.cpp | depend
397 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
398
399%.o: %.cxx | depend
400 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
401
402%.o: %.cc | depend
403 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
404
405%.test.o: %.c | depend
406 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(TEST_CFLAGS)
407
408%.lo: %.S | depend
409 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
410
411%.lo: %.s | depend
412 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
413
414%.lo: %.c | depend
415 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS)
416
417%.lo: %.cpp | depend
418 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
419
420%.lo: %.cxx | depend
421 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
422
423%.lo: %.cc | depend
424 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
425
426-include $(DEPENDS)
427
Note: See TracBrowser for help on using the repository browser.