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