hbench: add Doxygen groups
[helenos.git] / uspace / Makefile.common
blob37737bff0d9b71fd0fd4f3b505782d1dfaec88bd
2 # Copyright (c) 2005 Martin Decky
3 # Copyright (c) 2007 Jakub Jermar
4 # All rights reserved.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
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.
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.
30 # Individual makefiles set:
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
39 #   BINARY         (/) binary output name (like appname)
40 #   LIBRARY        (/) library output name (like libname)
42 #   EXTRA_OUTPUT       additional output targets
43 #   EXTRA_CLEAN        additional cleanup targets
45 # Optionally, for a binary:
46 #   STATIC_NEEDED      set to 'y' for init binaries, will build statically
47 #                      linked version
48 #   STATIC_ONLY        set to 'y' if binary cannot be linked dynamically
49 #                      (e.g. uses thread-local variables)
51 # Optionally, for a library:
52 #   SOVERSION            shared library version (major.minor),
53 #                        if missing, no shared library is built
55 # (x) required variables
56 # (/) exactly one of the variables must be defined
59 ROOT_PATH = $(USPACE_PREFIX)/..
61 VERSION_DEF = $(ROOT_PATH)/version
63 COMMON_MAKEFILE = $(ROOT_PATH)/Makefile.common
64 COMMON_HEADER = $(ROOT_PATH)/common.h
66 CONFIG_MAKEFILE = $(ROOT_PATH)/Makefile.config
67 CONFIG_HEADER = $(ROOT_PATH)/config.h
69 -include $(VERSION_DEF)
70 -include $(COMMON_MAKEFILE)
71 -include $(CONFIG_MAKEFILE)
73 OUTPUTS = $(EXTRA_OUTPUT)
75 ifneq ($(BINARY),)
76         JOB = $(BINARY).job
77         TEST_BINARY = test-$(BINARY)
78         OUTPUTS += $(BINARY) $(BINARY).disasm
79         EXTRA_CLEAN += $(BINARY).map
80 endif
82 ifneq ($(LIBRARY),)
83         JOB = $(LIBRARY).job
84         TEST_BINARY = test-$(LIBRARY)
85         OUTPUTS += $(LIBRARY).a
86 endif
88 ifeq ($(CONFIG_BUILD_SHARED_LIBS),y)
89         ifneq ($(SOVERSION),)
90                 SLIBRARY = $(LIBRARY).so.$(SOVERSION)
91                 LSONAME = $(LIBRARY).so.$(basename $(SOVERSION))
92                 OUTPUTS += $(SLIBRARY) $(SLIBRARY).disasm $(LSONAME)
93                 EXTRA_CLEAN += $(LIBRARY).la $(SLIBRARY).map
94         endif
95 endif
97 LIB_PREFIX = $(USPACE_PREFIX)/lib
99 LIBC_PREFIX = $(LIB_PREFIX)/c
100 LIBC_INCLUDES_FLAGS = \
101         -isystem $(LIBC_PREFIX)/include \
102         -isystem $(LIBC_PREFIX)/arch/$(UARCH)/include \
103         -isystem $(ROOT_PATH)/abi/arch/$(KARCH)/include \
104         -isystem $(ROOT_PATH)/abi/include
106 LIBCPP_PREFIX = $(LIB_PREFIX)/cpp
107 LIBCPP_INCLUDES_FLAGS = -isystem $(LIBCPP_PREFIX)/include
109 LIBDLTEST_PREFIX = $(LIB_PREFIX)/dltest
111 START_FILES = $(LIBC_PREFIX)/crt0.o $(LIBC_PREFIX)/crt1.o
113 AFLAGS = --fatal-warnings
114 LDFLAGS = -Wl,--fatal-warnings,--warn-common
116 ifeq ($(STATIC_NEEDED),y)
117         STATIC_BUILD = y
118 else
119         ifeq ($(STATIC_ONLY),y)
120                 STATIC_BUILD = y
121         else
122                 ifeq ($(CONFIG_USE_SHARED_LIBS),y)
123                         STATIC_BUILD = n
124                 else
125                         STATIC_BUILD = y
126                 endif
127         endif
128 endif
130 ifneq ($(STATIC_BUILD),y)
131         LINK_DYNAMIC = y
132 endif
134 BASE_LIBS =
136 ifneq ($(LIBRARY),libc)
137         ifeq ($(STATIC_BUILD),y)
138                 BASE_LIBS += $(LIBC_PREFIX)/libc.a
139         else
140                 BASE_LIBS += $(LIBC_PREFIX)/libc.so.0
141         endif
142 endif
144 BASE_LIBS += -lgcc
146 ifneq ($(LINK_DYNAMIC),y)
147         LDFLAGS += -static
148 endif
150 INCLUDES_FLAGS = $(LIBC_INCLUDES_FLAGS)
152 ifneq ($(LIBRARY),)
153         INCLUDES_FLAGS += -Iinclude -I.
154 endif
156 INCLUDES_FLAGS += $(foreach lib,$(LIBS), -I$(LIB_PREFIX)/$(lib) -I$(LIB_PREFIX)/$(lib)/include)
158 # TODO: get rid of this special case
159 ifneq ($(filter math, $(LIBS)),)
160         INCLUDES_FLAGS += $(LIBMATH_INCLUDES_FLAGS)
161 endif
163 # PCUT-based unit tests
164 ifneq ($(TEST_SOURCES),)
165         TEST_OUTPUTS = $(TEST_BINARY) $(TEST_BINARY).disasm
166         TEST_CFLAGS = -I$(LIB_PREFIX)/pcut/include -D__helenos__ $(EXTRA_TEST_CFLAGS)
167         TEST_BINARY_LIBS = $(LIB_PREFIX)/pcut/libpcut.a
168         EXTRA_CLEAN += $(TEST_OUTPUTS) $(TEST_BINARY).map
169 ifneq ($(LIBRARY),)
170         TEST_BINARY_LIBS += $(LIBRARY).a
171 endif
172         TEST_BINARY_LIBS += $(TEST_LIBS)
173 endif
175 # Flags that are not necessary, and can be overriden, but are used by default.
176 DEFAULT_CFLAGS = \
177         -O$(OPTIMIZATION) \
178         -ffunction-sections \
179         -fno-builtin-strftime \
180         -pipe \
181         -Wall \
182         -Wextra \
183         -Wno-unused-parameter \
184         -Wmissing-prototypes \
185         -Wwrite-strings \
186         -Werror-implicit-function-declaration \
187         -Wsystem-headers \
188         -Wunknown-pragmas
190 # XXX: -fno-builtin-strftime is for a seemingly spurious format warning.
192 ifeq ($(CONFIG_DEBUG),y)
193         DEFAULT_CFLAGS += -Werror
194 endif
196 ifeq ($(CONFIG_UBSAN),y)
197         DEFAULT_CFLAGS += -fsanitize=undefined
198 endif
200 ifeq ($(COMPILER),clang)
201         DEFAULT_CFLAGS += \
202                 -Wno-missing-braces \
203                 -Wno-missing-field-initializers \
204                 -Wno-typedef-redefinition \
205                 -Wno-unused-command-line-argument
206 else
207         DEFAULT_CFLAGS += \
208                 -Wno-nonnull-compare \
209                 -Wno-clobbered
210 endif
212 ifeq ($(CONFIG_LINE_DEBUG),y)
213         DEFAULT_CFLAGS += -ggdb
214 endif
216 # Flags that should always be used, even for third-party software.
217 COMMON_CPPFLAGS = \
218         -D__$(ENDIANESS)__
220 COMMON_CFLAGS = \
221         -nostdlib
223 # Flags that are always used for HelenOS code, but not for third-party.
224 HELENOS_CFLAGS = \
225         -std=gnu11 \
226         $(INCLUDES_FLAGS) \
227         -imacros $(CONFIG_HEADER) \
228         -D_HELENOS_SOURCE \
229         -fexec-charset=UTF-8 \
230         -finput-charset=UTF-8 \
231         -fno-common \
232         -fdebug-prefix-map=$(realpath $(ROOT_PATH))=.
234 # TODO: Use a different name.
235 # CFLAGS variable is traditionally used for overridable flags.
236 CFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CFLAGS) $(HELENOS_CFLAGS) $(DEFAULT_CFLAGS)
238 # Flags for the compilation of C++ code.
239 CXX_BASE_LIBS = $(LIBCPP_PREFIX)/libcpp.a $(BASE_LIBS)
240 DEFAULT_CXXFLAGS = \
241         -O$(OPTIMIZATION) \
242         -ffunction-sections \
243         -pipe \
244         -Wall \
245         -Wextra \
246         -Wno-unused-parameter \
247         -Wwrite-strings \
248         -Werror-implicit-function-declaration
250 ifeq ($(CONFIG_DEBUG),y)
251         DEFAULT_CXXFLAGS += -Werror
252 endif
254 COMMON_CXXFLAGS = $(COMMON_CFLAGS) -fno-exceptions
255 HELENOS_CXXFLAGS = \
256         -std=c++17 -frtti \
257         $(LIBCPP_INCLUDES_FLAGS) $(INCLUDES_FLAGS) \
258         -imacros $(CONFIG_HEADER) \
259         -D_HELENOS_SOURCE \
260         -fexec-charset=UTF-8 \
261         -finput-charset=UTF-8 \
262         -fno-common \
263         -fdebug-prefix-map=$(realpath $(ROOT_PATH))=.
265 CXXFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CXXFLAGS) $(HELENOS_CXXFLAGS) $(DEFAULT_CXXFLAGS)
267 ## Setup platform configuration
270 -include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.common
272 ## Compilation options
275 ifeq ($(PRECHECK),y)
276         JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py
277         # NOTE: You must not change the order of arguments.
278         CC_JOB = $(JOBFILE) $(JOB) $(CC) $< -o $@
279         CXX_JOB = $(JOBFILE) $(JOB) $(CXX) $< -o $@
280 else
281         CC_JOB = $(CC) $< -o $@
282         CXX_JOB = $(CXX) $< -o $@
283 endif
285 ifeq ($(CONFIG_STRIP_BINARIES),y)
286         LDFLAGS += -s
287 endif
289 LIB_CFLAGS = $(CFLAGS) -fPIC
290 LIB_LDFLAGS = $(LDFLAGS) -shared -Wl,-soname,$(LSONAME) -Wl,--no-undefined,--no-allow-shlib-undefined
292 AS_CFLAGS := $(addprefix -Xassembler ,$(AFLAGS))
294 OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
295 LOBJECTS := $(addsuffix .lo,$(basename $(SOURCES)))
296 TEST_OBJECTS := $(addsuffix .test.o,$(basename $(TEST_SOURCES)))
297 DEPENDS := $(addsuffix .d,$(basename $(SOURCES))) $(addsuffix .test.d,$(basename $(TEST_SOURCES)))
299 LIBTAGS := $(foreach lib,$(LIBS), $(USPACE_PREFIX)/lib/$(lib)/tag)
300 LIBARGS := $(addprefix -L$(USPACE_PREFIX)/lib/, $(LIBS)) $(addprefix -l, $(LIBS))
302 .PHONY: all all-test clean fasterclean depend
304 all: tag $(OUTPUTS)
306 all-test: $(TEST_OUTPUTS)
308 clean: fasterclean
309         find . -name '*.o' -follow -exec rm \{\} \;
310         find . -name '*.lo' -follow -exec rm \{\} \;
311         find . -name '*.d' -follow -exec rm \{\} \;
313 fasterclean:
314         rm -rf $(JOB) $(OUTPUTS) $(EXTRA_CLEAN) tag deps.mk
316 depend: $(PRE_DEPEND)
318 # "Tag" files are used to force relink of binaries when dependencies get rebuilt,
319 # regardless of whether the dependency was linked statically or dynamically,
320 # or which version of a dynamic library was used. Prerequisites for this file
321 # are defined further down.
322 tag:
323         touch tag
325 # Generate inter-module make dependencies.
326 # This is needed to ensure correct build order of libraries and code depending on them.
327 deps.mk: Makefile
328         echo > $@.new
329         for lib in $(LIBS); do \
330                 echo "$(SELF_TARGET): lib/$$lib.build" >> $@.new; \
331         done
332         mv -f $@.new $@
334 %.disasm: %
335 ifeq ($(CONFIG_LINE_DEBUG),y)
336         $(OBJDUMP) -d -S $< > $@
337 else
338         $(OBJDUMP) -d $< > $@
339 endif
341 ifneq ($(BINARY),)
343 ifneq ($(filter %.cpp %.cc %.cxx, $(SOURCES)),)
344 $(BINARY): $(OBJECTS) $(LIBTAGS)
345         $(CXX) $(CXXFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(CXX_BASE_LIBS)
346 else
347 $(BINARY): $(OBJECTS) $(LIBTAGS)
348         $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(BASE_LIBS)
349 endif
351 endif
353 ifneq ($(TEST_BINARY),)
354 $(TEST_BINARY): $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBTAGS)
355         $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ $(START_FILES) $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBARGS) $(BASE_LIBS)
356 endif
358 ifneq ($(LIBRARY),)
359 tag: $(LIBRARY).a
361 $(LIBRARY).a: $(OBJECTS)
362         $(AR) rc $@ $(OBJECTS)
363 endif
365 ifneq ($(SLIBRARY),)
366 tag: $(SLIBRARY)
368 $(LIBRARY).la: $(LOBJECTS)
369         $(AR) rc $@ $(LOBJECTS)
371 $(SLIBRARY): $(LIBRARY).la
372         $(CC) $(CFLAGS) $(LIB_LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ -Wl,--whole-archive $(LIBRARY).la -Wl,--no-whole-archive $(LIBARGS) $(BASE_LIBS)
374 $(LSONAME):
375         ln -s $(SLIBRARY) $@
376 endif
378 %.o: %.S | depend
379         $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
381 %.o: %.s | depend
382         $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
384 %.o: %.c | depend
385         $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS)
387 %.o: %.cpp | depend
388         $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
390 %.o: %.cxx | depend
391         $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
393 %.o: %.cc | depend
394         $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
396 %.test.o: %.c | depend
397         $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(TEST_CFLAGS)
399 %.lo: %.S | depend
400         $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
402 %.lo: %.s | depend
403         $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
405 %.lo: %.c | depend
406         $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS)
408 %.lo: %.cpp | depend
409         $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
411 %.lo: %.cxx | depend
412         $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
414 %.lo: %.cc | depend
415         $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
417 -include $(DEPENDS)