source: mainline/uspace/Makefile.common@ 6006f35

Last change on this file since 6006f35 was 9fb280c, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Make clang slightly less broken

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