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
RevLine 
[1b1164e8]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)
[364778b2]41#
[1b1164e8]42# EXTRA_OUTPUT additional output targets
43# EXTRA_CLEAN additional cleanup targets
44#
[364778b2]45# Optionally, for a binary:
[5b72635]46# STATIC_NEEDED set to 'y' for init binaries, will build statically
47# linked version
48#
[c631734]49# Optionally, for a library:
50# SOVERSION shared library version (major.minor),
51# if missing, no shared library is built
[364778b2]52#
[1b1164e8]53# (x) required variables
54# (/) exactly one of the variables must be defined
55#
56
57ROOT_PATH = $(USPACE_PREFIX)/..
58
[e6ae77a]59VERSION_DEF = $(ROOT_PATH)/version
60
[1b1164e8]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
[e6ae77a]67-include $(VERSION_DEF)
[1b1164e8]68-include $(COMMON_MAKEFILE)
69-include $(CONFIG_MAKEFILE)
70
[c631734]71OUTPUTS = $(EXTRA_OUTPUT)
72
[1b1164e8]73ifneq ($(BINARY),)
74 JOB = $(BINARY).job
[01579ad]75 TEST_BINARY = test-$(BINARY)
[c631734]76 OUTPUTS += $(BINARY) $(BINARY).disasm
[1b1164e8]77 EXTRA_CLEAN += $(BINARY).map
78endif
79
80ifneq ($(LIBRARY),)
81 JOB = $(LIBRARY).job
[01579ad]82 TEST_BINARY = test-$(LIBRARY)
[c631734]83 OUTPUTS += $(LIBRARY).a
[1b1164e8]84endif
85
[d9be488]86ifeq ($(CONFIG_BUILD_SHARED_LIBS),y)
[c631734]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
[7fb3f1c]92 endif
[143932e3]93endif
94
[36a75a2]95LIB_PREFIX = $(USPACE_PREFIX)/lib
[849ed54]96
97LIBC_PREFIX = $(LIB_PREFIX)/c
[6c5fc8e]98LIBC_INCLUDES_FLAGS = \
[7570a95f]99 -isystem $(LIBC_PREFIX)/include \
100 -isystem $(LIBC_PREFIX)/arch/$(UARCH)/include \
[e344422]101 -isystem $(ROOT_PATH)/abi/arch/$(KARCH)/include \
[7570a95f]102 -isystem $(ROOT_PATH)/abi/include
[058c240]103
104LIBCPP_PREFIX = $(LIB_PREFIX)/cpp
[7570a95f]105LIBCPP_INCLUDES_FLAGS = -isystem $(LIBCPP_PREFIX)/include
[058c240]106
[9182e86f]107LIBDLTEST_PREFIX = $(LIB_PREFIX)/dltest
[7c014d1]108
[2eadda9]109START_FILES = $(LIBC_PREFIX)/crt0.o $(LIBC_PREFIX)/crt1.o
[c2c4127]110
[6480827]111LDFLAGS = -Wl,--fatal-warnings,--warn-common
[ae7bbfd0]112
[83b64a59]113STATIC_BUILD = n
114
[5b72635]115ifeq ($(STATIC_NEEDED),y)
116 STATIC_BUILD = y
[83b64a59]117endif
118
119ifneq ($(CONFIG_BUILD_SHARED_LIBS),y)
120 STATIC_BUILD = y
121endif
122
123ifneq ($(CONFIG_USE_SHARED_LIBS),y)
124 ifeq ($(LIBRARY),)
[5b72635]125 STATIC_BUILD = y
126 endif
127endif
128
[0716a1f]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
[83b64a59]134ifeq ($(STATIC_BUILD),y)
135 LDFLAGS += -static
[d9be488]136endif
137
[2eadda9]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
[954c024]148BASE_LIBS += -lgcc
[d2e7a51]149
[9d8b12da]150INCLUDES_FLAGS = $(LIBC_INCLUDES_FLAGS)
[1b1164e8]151
[cecba66e]152ifneq ($(LIBRARY),)
153 INCLUDES_FLAGS += -Iinclude -I.
154endif
155
156INCLUDES_FLAGS += $(foreach lib,$(LIBS), -I$(LIB_PREFIX)/$(lib) -I$(LIB_PREFIX)/$(lib)/include)
157
[83b64a59]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
[9d8b12da]168endif
169
[01579ad]170# PCUT-based unit tests
171ifneq ($(TEST_SOURCES),)
[c631734]172 TEST_OUTPUTS = $(TEST_BINARY) $(TEST_BINARY).disasm
[4e6a610]173 TEST_CFLAGS = -I$(LIB_PREFIX)/pcut/include -D__helenos__ $(EXTRA_TEST_CFLAGS)
[c631734]174 TEST_BINARY_LIBS = $(LIB_PREFIX)/pcut/libpcut.a
175 EXTRA_CLEAN += $(TEST_OUTPUTS) $(TEST_BINARY).map
[01579ad]176ifneq ($(LIBRARY),)
[c631734]177 TEST_BINARY_LIBS += $(LIBRARY).a
[01579ad]178endif
[c631734]179 TEST_BINARY_LIBS += $(TEST_LIBS)
[01579ad]180endif
181
[2660ee3]182# Flags that are not necessary, and can be overriden, but are used by default.
183DEFAULT_CFLAGS = \
184 -O$(OPTIMIZATION) \
185 -ffunction-sections \
[58e7b26]186 -fno-builtin-strftime \
[2660ee3]187 -pipe \
188 -Wall \
189 -Wextra \
190 -Wno-unused-parameter \
191 -Wwrite-strings \
[7570a95f]192 -Werror-implicit-function-declaration \
[874ed210]193 -Wunknown-pragmas \
194 -Wa,--fatal-warnings
[143932e3]195
[9fb280c]196ifneq ($(COMPILER),clang)
197 DEFAULT_CFLAGS += -Wmissing-prototypes -Wsystem-headers
198endif
199
[58e7b26]200# XXX: -fno-builtin-strftime is for a seemingly spurious format warning.
201
[e805e2f]202ifeq ($(CONFIG_DEBUG),y)
[2660ee3]203 DEFAULT_CFLAGS += -Werror
204endif
205
[2f7d77c6]206ifeq ($(CONFIG_UBSAN),y)
207 DEFAULT_CFLAGS += -fsanitize=undefined
208endif
209
[2660ee3]210ifeq ($(COMPILER),clang)
211 DEFAULT_CFLAGS += \
[8591b31]212 -Wno-missing-braces \
[2660ee3]213 -Wno-missing-field-initializers \
214 -Wno-typedef-redefinition \
215 -Wno-unused-command-line-argument
216else
217 DEFAULT_CFLAGS += \
[8591b31]218 -Wno-nonnull-compare \
[2660ee3]219 -Wno-clobbered
[e805e2f]220endif
221
[1e00216]222ifeq ($(CONFIG_LINE_DEBUG),y)
[2660ee3]223 DEFAULT_CFLAGS += -ggdb
[1e00216]224endif
225
[2660ee3]226# Flags that should always be used, even for third-party software.
[ea42e44]227COMMON_CPPFLAGS = \
[2660ee3]228 -D__$(ENDIANESS)__
229
[ea42e44]230COMMON_CFLAGS = \
231 -nostdlib
232
[2660ee3]233# Flags that are always used for HelenOS code, but not for third-party.
234HELENOS_CFLAGS = \
[dc68f72]235 -std=gnu11 \
[2660ee3]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
[9fb280c]244ifeq ($(COMPILER),clang)
245 COMMON_CFLAGS += -ffreestanding -nostdlibinc -Wno-unused-command-line-argument
246endif
247
[2660ee3]248# TODO: Use a different name.
249# CFLAGS variable is traditionally used for overridable flags.
[ea42e44]250CFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CFLAGS) $(HELENOS_CFLAGS) $(DEFAULT_CFLAGS)
[2660ee3]251
[058c240]252# Flags for the compilation of C++ code.
[8e2154e7]253CXX_BASE_LIBS = $(LIBCPP_PREFIX)/libcpp.a $(BASE_LIBS)
[73401643]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
[5a50430]264ifeq ($(CONFIG_DEBUG),y)
265 DEFAULT_CXXFLAGS += -Werror
266endif
267
[82d9087]268COMMON_CXXFLAGS = $(COMMON_CFLAGS) -fno-exceptions
[73401643]269HELENOS_CXXFLAGS = \
270 -std=c++17 -frtti \
271 $(LIBCPP_INCLUDES_FLAGS) $(INCLUDES_FLAGS) \
[4fb8163]272 -imacros $(CONFIG_HEADER) \
[73401643]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)
[058c240]280
[1b1164e8]281## Setup platform configuration
282#
283
284-include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.common
285
286## Compilation options
287#
288
[7f881cd8]289ifeq ($(PRECHECK),y)
290 JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py
[95d45482]291 # NOTE: You must not change the order of arguments.
[7f881cd8]292 CC_JOB = $(JOBFILE) $(JOB) $(CC) $< -o $@
[1bf924a]293 CXX_JOB = $(JOBFILE) $(JOB) $(CXX) $< -o $@
[7f881cd8]294else
295 CC_JOB = $(CC) $< -o $@
[1bf924a]296 CXX_JOB = $(CXX) $< -o $@
[7f881cd8]297endif
[1b1164e8]298
[315130c]299ifeq ($(CONFIG_STRIP_BINARIES),y)
[009c485]300 LDFLAGS += -s
[315130c]301endif
302
[a0a273e]303LIB_CFLAGS = $(CFLAGS) -fPIC
[fd244cd]304LIB_CXXFLAGS = $(CXXFLAGS) -fPIC
[2eadda9]305LIB_LDFLAGS = $(LDFLAGS) -shared -Wl,-soname,$(LSONAME) -Wl,--no-undefined,--no-allow-shlib-undefined
[a0a273e]306
[1b1164e8]307OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
[143932e3]308LOBJECTS := $(addsuffix .lo,$(basename $(SOURCES)))
[01579ad]309TEST_OBJECTS := $(addsuffix .test.o,$(basename $(TEST_SOURCES)))
[4761f54]310DEPENDS := $(addsuffix .d,$(basename $(SOURCES))) $(addsuffix .test.d,$(basename $(TEST_SOURCES)))
311
[83b64a59]312LIBTAGS := $(foreach lib,$(DEPLIBS), $(USPACE_PREFIX)/lib/$(lib)/tag)
[cecba66e]313LIBARGS := $(addprefix -L$(USPACE_PREFIX)/lib/, $(LIBS)) $(addprefix -l, $(LIBS))
314
[7654f3e]315.PHONY: all all-test clean fasterclean depend
[c631734]316
[cecba66e]317all: tag $(OUTPUTS)
[c631734]318
319all-test: $(TEST_OUTPUTS)
320
[2660ee3]321clean: fasterclean
[7654f3e]322 find . -name '*.o' -follow -exec rm \{\} \;
323 find . -name '*.lo' -follow -exec rm \{\} \;
324 find . -name '*.d' -follow -exec rm \{\} \;
325
326fasterclean:
[2660ee3]327 rm -rf $(JOB) $(OUTPUTS) $(EXTRA_CLEAN) tag deps.mk
[c631734]328
329depend: $(PRE_DEPEND)
330
[cecba66e]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
[4f674d7]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
[83b64a59]342 for lib in $(DEPLIBS); do \
[4f674d7]343 echo "$(SELF_TARGET): lib/$$lib.build" >> $@.new; \
344 done
345 mv -f $@.new $@
346
[52a7f238]347%.disasm: %
[9ded977]348ifeq ($(CONFIG_LINE_DEBUG),y)
349 $(OBJDUMP) -d -S $< > $@
350else
[1b1164e8]351 $(OBJDUMP) -d $< > $@
[9ded977]352endif
[1b1164e8]353
[52a7f238]354ifneq ($(BINARY),)
[73401643]355
[65ee021]356ifneq ($(filter %.cpp %.cc %.cxx, $(SOURCES)),)
[82d9087]357$(BINARY): $(OBJECTS) $(LIBTAGS)
358 $(CXX) $(CXXFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(CXX_BASE_LIBS)
[73401643]359else
[82d9087]360$(BINARY): $(OBJECTS) $(LIBTAGS)
361 $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(BASE_LIBS)
[1b1164e8]362endif
363
[8e2154e7]364endif
365
[c631734]366ifneq ($(TEST_BINARY),)
[82d9087]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)
[1d465bf]369endif
[143932e3]370
[1b1164e8]371ifneq ($(LIBRARY),)
[cecba66e]372tag: $(LIBRARY).a
373
[eff9f8c]374$(LIBRARY).a: $(OBJECTS)
[1b1164e8]375 $(AR) rc $@ $(OBJECTS)
376endif
377
[c631734]378ifneq ($(SLIBRARY),)
[cecba66e]379tag: $(SLIBRARY)
380
[c631734]381$(LIBRARY).la: $(LOBJECTS)
[143932e3]382 $(AR) rc $@ $(LOBJECTS)
383
[3d7594d]384$(SLIBRARY): $(LIBRARY).la $(LIBTAGS)
[82d9087]385 $(CC) $(CFLAGS) $(LIB_LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ -Wl,--whole-archive $(LIBRARY).la -Wl,--no-whole-archive $(LIBARGS) $(BASE_LIBS)
[c631734]386
387$(LSONAME):
388 ln -s $(SLIBRARY) $@
[01579ad]389endif
390
[4761f54]391%.o: %.S | depend
[874ed210]392 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS)
[1b1164e8]393
[4761f54]394%.o: %.s | depend
[874ed210]395 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS)
[1b1164e8]396
[4761f54]397%.o: %.c | depend
[c631734]398 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS)
[1b1164e8]399
[058c240]400%.o: %.cpp | depend
[1bf924a]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)
[058c240]408
[4761f54]409%.test.o: %.c | depend
[c631734]410 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(TEST_CFLAGS)
[01579ad]411
[4761f54]412%.lo: %.S | depend
[874ed210]413 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS)
[ec3e2ed]414
[4761f54]415%.lo: %.s | depend
[874ed210]416 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS)
[ec3e2ed]417
[4761f54]418%.lo: %.c | depend
[c631734]419 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS)
[664af708]420
[0d2bc9d]421%.lo: %.cpp | depend
[fd244cd]422 $(CXX_JOB) -c -MD -MP $(DEFS) $(LIB_CXXFLAGS) $(EXTRA_CXXFLAGS)
[1bf924a]423
424%.lo: %.cxx | depend
[fd244cd]425 $(CXX_JOB) -c -MD -MP $(DEFS) $(LIB_CXXFLAGS) $(EXTRA_CXXFLAGS)
[1bf924a]426
427%.lo: %.cc | depend
[fd244cd]428 $(CXX_JOB) -c -MD -MP $(DEFS) $(LIB_CXXFLAGS) $(EXTRA_CXXFLAGS)
[0d2bc9d]429
[731d8f9]430-include $(DEPENDS)
Note: See TracBrowser for help on using the repository browser.