2 # Copyright (c) 2005 Martin Decky
3 # Copyright (c) 2007 Jakub Jermar
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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
49 # Optionally, for a library:
50 # SOVERSION shared library version (major.minor),
51 # if missing, no shared library is built
53 # (x) required variables
54 # (/) exactly one of the variables must be defined
57 ROOT_PATH = $(USPACE_PREFIX)/..
59 VERSION_DEF = $(ROOT_PATH)/version
61 COMMON_MAKEFILE = $(ROOT_PATH)/Makefile.common
62 COMMON_HEADER = $(ROOT_PATH)/common.h
64 CONFIG_MAKEFILE = $(ROOT_PATH)/Makefile.config
65 CONFIG_HEADER = $(ROOT_PATH)/config.h
67 -include $(VERSION_DEF)
68 -include $(COMMON_MAKEFILE)
69 -include $(CONFIG_MAKEFILE)
71 OUTPUTS = $(EXTRA_OUTPUT)
75 TEST_BINARY = test-$(BINARY)
76 OUTPUTS += $(BINARY) $(BINARY).disasm
77 EXTRA_CLEAN += $(BINARY).map
82 TEST_BINARY = test-$(LIBRARY)
83 OUTPUTS += $(LIBRARY).a
86 ifeq ($(CONFIG_BUILD_SHARED_LIBS),y)
88 SLIBRARY = $(LIBRARY).so.$(SOVERSION)
89 LSONAME = $(LIBRARY).so.$(basename $(SOVERSION))
90 OUTPUTS += $(SLIBRARY) $(SLIBRARY).disasm $(LSONAME)
91 EXTRA_CLEAN += $(LIBRARY).la $(SLIBRARY).map
95 LIB_PREFIX = $(USPACE_PREFIX)/lib
97 LIBC_PREFIX = $(LIB_PREFIX)/c
98 LIBC_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
104 LIBCPP_PREFIX = $(LIB_PREFIX)/cpp
105 LIBCPP_INCLUDES_FLAGS = -isystem $(LIBCPP_PREFIX)/include
107 LIBDLTEST_PREFIX = $(LIB_PREFIX)/dltest
109 START_FILES = $(LIBC_PREFIX)/crt0.o $(LIBC_PREFIX)/crt1.o
111 AFLAGS = --fatal-warnings
112 LDFLAGS = -Wl,--fatal-warnings,--warn-common
116 ifeq ($(STATIC_NEEDED),y)
120 ifneq ($(CONFIG_BUILD_SHARED_LIBS),y)
124 ifneq ($(CONFIG_USE_SHARED_LIBS),y)
130 # LINK_DYNAMIC is only here to allow app/dltest to link dynamically in otherwise static build.
131 ifeq ($(LINK_DYNAMIC),y)
135 ifeq ($(STATIC_BUILD),y)
141 ifneq ($(LIBRARY),libc)
142 ifeq ($(STATIC_BUILD),y)
143 BASE_LIBS += $(LIBC_PREFIX)/libc.a
145 BASE_LIBS += $(LIBC_PREFIX)/libc.so.0
151 INCLUDES_FLAGS = $(LIBC_INCLUDES_FLAGS)
154 INCLUDES_FLAGS += -Iinclude -I.
157 INCLUDES_FLAGS += $(foreach lib,$(LIBS), -I$(LIB_PREFIX)/$(lib) -I$(LIB_PREFIX)/$(lib)/include)
161 ifneq ($(filter %.cpp %.cc %.cxx, $(SOURCES)),)
162 ifneq ($(LIBRARY),libcpp)
167 ifneq ($(LIBRARY),libc)
171 # PCUT-based unit tests
172 ifneq ($(TEST_SOURCES),)
173 TEST_OUTPUTS = $(TEST_BINARY) $(TEST_BINARY).disasm
174 TEST_CFLAGS = -I$(LIB_PREFIX)/pcut/include -D__helenos__ $(EXTRA_TEST_CFLAGS)
175 TEST_BINARY_LIBS = $(LIB_PREFIX)/pcut/libpcut.a
176 EXTRA_CLEAN += $(TEST_OUTPUTS) $(TEST_BINARY).map
178 TEST_BINARY_LIBS += $(LIBRARY).a
180 TEST_BINARY_LIBS += $(TEST_LIBS)
183 # Flags that are not necessary, and can be overriden, but are used by default.
186 -ffunction-sections \
187 -fno-builtin-strftime \
191 -Wno-unused-parameter \
192 -Wmissing-prototypes \
194 -Werror-implicit-function-declaration \
198 # XXX: -fno-builtin-strftime is for a seemingly spurious format warning.
200 ifeq ($(CONFIG_DEBUG),y)
201 DEFAULT_CFLAGS += -Werror
204 ifeq ($(CONFIG_UBSAN),y)
205 DEFAULT_CFLAGS += -fsanitize=undefined
208 ifeq ($(COMPILER),clang)
210 -Wno-missing-braces \
211 -Wno-missing-field-initializers \
212 -Wno-typedef-redefinition \
213 -Wno-unused-command-line-argument
216 -Wno-nonnull-compare \
220 ifeq ($(CONFIG_LINE_DEBUG),y)
221 DEFAULT_CFLAGS += -ggdb
224 # Flags that should always be used, even for third-party software.
231 # Flags that are always used for HelenOS code, but not for third-party.
235 -imacros $(CONFIG_HEADER) \
237 -fexec-charset=UTF-8 \
238 -finput-charset=UTF-8 \
240 -fdebug-prefix-map=$(realpath $(ROOT_PATH))=.
242 # TODO: Use a different name.
243 # CFLAGS variable is traditionally used for overridable flags.
244 CFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CFLAGS) $(HELENOS_CFLAGS) $(DEFAULT_CFLAGS)
246 # Flags for the compilation of C++ code.
247 CXX_BASE_LIBS = $(LIBCPP_PREFIX)/libcpp.a $(BASE_LIBS)
250 -ffunction-sections \
254 -Wno-unused-parameter \
256 -Werror-implicit-function-declaration
258 ifeq ($(CONFIG_DEBUG),y)
259 DEFAULT_CXXFLAGS += -Werror
262 COMMON_CXXFLAGS = $(COMMON_CFLAGS) -fno-exceptions
265 $(LIBCPP_INCLUDES_FLAGS) $(INCLUDES_FLAGS) \
266 -imacros $(CONFIG_HEADER) \
268 -fexec-charset=UTF-8 \
269 -finput-charset=UTF-8 \
271 -fdebug-prefix-map=$(realpath $(ROOT_PATH))=.
273 CXXFLAGS = $(COMMON_CPPFLAGS) $(COMMON_CXXFLAGS) $(HELENOS_CXXFLAGS) $(DEFAULT_CXXFLAGS)
275 ## Setup platform configuration
278 -include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.common
280 ## Compilation options
284 JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py
285 # NOTE: You must not change the order of arguments.
286 CC_JOB = $(JOBFILE) $(JOB) $(CC) $< -o $@
287 CXX_JOB = $(JOBFILE) $(JOB) $(CXX) $< -o $@
289 CC_JOB = $(CC) $< -o $@
290 CXX_JOB = $(CXX) $< -o $@
293 ifeq ($(CONFIG_STRIP_BINARIES),y)
297 LIB_CFLAGS = $(CFLAGS) -fPIC
298 LIB_CXXFLAGS = $(CXXFLAGS) -fPIC
299 LIB_LDFLAGS = $(LDFLAGS) -shared -Wl,-soname,$(LSONAME) -Wl,--no-undefined,--no-allow-shlib-undefined
301 AS_CFLAGS := $(addprefix -Xassembler ,$(AFLAGS))
303 OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
304 LOBJECTS := $(addsuffix .lo,$(basename $(SOURCES)))
305 TEST_OBJECTS := $(addsuffix .test.o,$(basename $(TEST_SOURCES)))
306 DEPENDS := $(addsuffix .d,$(basename $(SOURCES))) $(addsuffix .test.d,$(basename $(TEST_SOURCES)))
308 LIBTAGS := $(foreach lib,$(DEPLIBS), $(USPACE_PREFIX)/lib/$(lib)/tag)
309 LIBARGS := $(addprefix -L$(USPACE_PREFIX)/lib/, $(LIBS)) $(addprefix -l, $(LIBS))
311 .PHONY: all all-test clean fasterclean depend
315 all-test: $(TEST_OUTPUTS)
318 find . -name '*.o' -follow -exec rm \{\} \;
319 find . -name '*.lo' -follow -exec rm \{\} \;
320 find . -name '*.d' -follow -exec rm \{\} \;
323 rm -rf $(JOB) $(OUTPUTS) $(EXTRA_CLEAN) tag deps.mk
325 depend: $(PRE_DEPEND)
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.
334 # Generate inter-module make dependencies.
335 # This is needed to ensure correct build order of libraries and code depending on them.
338 for lib in $(DEPLIBS); do \
339 echo "$(SELF_TARGET): lib/$$lib.build" >> $@.new; \
344 ifeq ($(CONFIG_LINE_DEBUG),y)
345 $(OBJDUMP) -d -S $< > $@
347 $(OBJDUMP) -d $< > $@
352 ifneq ($(filter %.cpp %.cc %.cxx, $(SOURCES)),)
353 $(BINARY): $(OBJECTS) $(LIBTAGS)
354 $(CXX) $(CXXFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(CXX_BASE_LIBS)
356 $(BINARY): $(OBJECTS) $(LIBTAGS)
357 $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ $(START_FILES) $(OBJECTS) $(LIBARGS) $(BASE_LIBS)
362 ifneq ($(TEST_BINARY),)
363 $(TEST_BINARY): $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBTAGS)
364 $(CC) $(CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ $(START_FILES) $(TEST_OBJECTS) $(TEST_BINARY_LIBS) $(LIBARGS) $(BASE_LIBS)
370 $(LIBRARY).a: $(OBJECTS)
371 $(AR) rc $@ $(OBJECTS)
377 $(LIBRARY).la: $(LOBJECTS)
378 $(AR) rc $@ $(LOBJECTS)
380 $(SLIBRARY): $(LIBRARY).la $(LIBTAGS)
381 $(CC) $(CFLAGS) $(LIB_LDFLAGS) $(EXTRA_LDFLAGS) -Wl,-Map,$@.map -o $@ -Wl,--whole-archive $(LIBRARY).la -Wl,--no-whole-archive $(LIBARGS) $(BASE_LIBS)
388 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
391 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
394 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS)
397 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
400 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
403 $(CXX_JOB) -c -MD -MP $(DEFS) $(CXXFLAGS) $(EXTRA_CXXFLAGS)
405 %.test.o: %.c | depend
406 $(CC_JOB) -c -MD -MP $(DEFS) $(CFLAGS) $(EXTRA_CFLAGS) $(TEST_CFLAGS)
409 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
412 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS) $(AS_CFLAGS)
415 $(CC_JOB) -c -MD -MP $(DEFS) $(LIB_CFLAGS) $(EXTRA_CFLAGS)
418 $(CXX_JOB) -c -MD -MP $(DEFS) $(LIB_CXXFLAGS) $(EXTRA_CXXFLAGS)
421 $(CXX_JOB) -c -MD -MP $(DEFS) $(LIB_CXXFLAGS) $(EXTRA_CXXFLAGS)
424 $(CXX_JOB) -c -MD -MP $(DEFS) $(LIB_CXXFLAGS) $(EXTRA_CXXFLAGS)