merge mainline changes
[helenos.git] / uspace / Makefile.common
blob38ad73ab5681f3fd8840a88736ce5eb48a4509d8
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 #   LINKER_SCRIPT      linker script
38 #   PRE_DEPEND         targets required for dependency check
40 #   BINARY         (/) binary output name (like appname)
41 #   LIBRARY        (/) library output name (like libname)
43 #   EXTRA_OUTPUT       additional output targets
44 #   EXTRA_CLEAN        additional cleanup targets
46 #   POSIX_COMPAT       set to 'y' to use POSIX compatibility layer
47 #   NEEDS_MATH         set to 'y' to add implementation of mathematical functions
49 # Optionally, for a binary:
50 #   STATIC_NEEDED      set to 'y' for init binaries, will build statically
51 #                      linked version
52 #   STATIC_ONLY        set to 'y' if binary cannot be linked dynamically
53 #                      (e.g. uses thread-local variables)
55 # Optionally, for a libary:
56 #   SLIBRARY           Name with full version, e.g. libfoo.so.0.0 
57 #   LSONAME            Soname / name with short version, e.g. libfoo.so.0
59 # (x) required variables
60 # (/) exactly one of the variables must be defined
63 ROOT_PATH = $(USPACE_PREFIX)/..
65 VERSION_DEF = $(ROOT_PATH)/version
67 COMMON_MAKEFILE = $(ROOT_PATH)/Makefile.common
68 COMMON_HEADER = $(ROOT_PATH)/common.h
70 CONFIG_MAKEFILE = $(ROOT_PATH)/Makefile.config
71 CONFIG_HEADER = $(ROOT_PATH)/config.h
73 -include $(VERSION_DEF)
74 -include $(COMMON_MAKEFILE)
75 -include $(CONFIG_MAKEFILE)
77 ifneq ($(BINARY),)
78         JOB = $(BINARY).job
79         OUTPUT = $(BINARY)
80         EXTRA_OUTPUT += $(BINARY).disasm
81         EXTRA_CLEAN += $(BINARY).map
82 endif
84 ifneq ($(LIBRARY),)
85         JOB = $(LIBRARY).job
86         OUTPUT = $(LIBRARY).a
87 endif
89 ifeq ($(CONFIG_BUILD_SHARED_LIBS), y)
90         ifneq ($(SLIBRARY),)
91                 LARCHIVE = $(LIBRARY).la
92                 LOUTPUT = $(SLIBRARY)
93                 EXTRA_OUTPUT += $(LOUTPUT).disasm $(LIBRARY).so $(LSONAME)
94                 EXTRA_CLEAN += $(LOUTPUT).map $(LOUTPUT).ldisasm \
95                     $(LIBC_PREFIX)/shared/arch/$(UARCH)/_lib.ld \
96                     $(LIBRARY).so $(LSONAME)
97         endif
98 endif
100 DEPEND = Makefile.depend
101 DEPEND_PREV = $(DEPEND).prev
103 LIB_PREFIX = $(USPACE_PREFIX)/lib
105 LIBC_PREFIX = $(LIB_PREFIX)/c
106 LIBC_INCLUDES_FLAGS = \
107         -I$(LIBC_PREFIX)/include \
108         -I$(LIBC_PREFIX)/arch/$(UARCH)/include \
109         -I$(ROOT_PATH)/abi/include
110 LIBSOFTFLOAT_PREFIX = $(LIB_PREFIX)/softfloat
111 LIBSOFTINT_PREFIX = $(LIB_PREFIX)/softint
112 LIBMATH_PREFIX = $(LIB_PREFIX)/math
114 LIBPOSIX_PREFIX = $(LIB_PREFIX)/posix
116 LIBBLOCK_PREFIX = $(LIB_PREFIX)/block
117 LIBFS_PREFIX = $(LIB_PREFIX)/fs
118 LIBCLUI_PREFIX = $(LIB_PREFIX)/clui
119 LIBFMTUTIL_PREFIX = $(LIB_PREFIX)/fmtutil
121 LIBGRAPH_PREFIX = $(LIB_PREFIX)/graph
122 LIBSOFTREND_PREFIX = $(LIB_PREFIX)/softrend
123 LIBDRAW_PREFIX = $(LIB_PREFIX)/draw
124 LIBGUI_PREFIX = $(LIB_PREFIX)/gui
126 LIBEXT4_PREFIX = $(LIB_PREFIX)/ext4
128 LIBUSB_PREFIX = $(LIB_PREFIX)/usb
129 LIBUSBHOST_PREFIX = $(LIB_PREFIX)/usbhost
130 LIBUSBDEV_PREFIX = $(LIB_PREFIX)/usbdev
131 LIBUSBHID_PREFIX = $(LIB_PREFIX)/usbhid
132 LIBUSBVIRT_PREFIX = $(LIB_PREFIX)/usbvirt
134 LIBDRV_PREFIX = $(LIB_PREFIX)/drv
135 LIBHOUND_PREFIX = $(LIB_PREFIX)/hound
136 LIBPCM_PREFIX = $(LIB_PREFIX)/pcm
137 LIBNET_PREFIX = $(LIB_PREFIX)/net
138 LIBNIC_PREFIX = $(LIB_PREFIX)/nic
139 LIBMINIX_PREFIX = $(LIB_PREFIX)/minix
141 LIBSCSI_PREFIX = $(LIB_PREFIX)/scsi
143 LIBBITHENGE_PREFIX = $(LIB_PREFIX)/bithenge
145 LIBHTTP_PREFIX = $(LIB_PREFIX)/http
146 LIBURI_PREFIX = $(LIB_PREFIX)/uri
148 LIBMBR_PREFIX = $(LIB_PREFIX)/mbr
149 LIBGPT_PREFIX = $(LIB_PREFIX)/gpt
151 ifeq ($(STATIC_NEEDED),y)
152         STATIC_BUILD = y
153 else
154         ifeq ($(STATIC_ONLY),y)
155                 STATIC_BUILD = y
156         else
157                 ifeq ($(CONFIG_USE_SHARED_LIBS), y)
158                         STATIC_BUILD = n
159                 else
160                         STATIC_BUILD = y
161                 endif
162         endif
163 endif
165 # Build static whenever we use libusb because that library uses 
166 # thread local variables
167 ifneq ($(findstring usb, $(LIBS)),)
168         STATIC_BUILD = y
169 endif
171 ifeq ($(STATIC_BUILD),y)
172         BASE_LIBS = $(LIBC_PREFIX)/libc.a $(LIBSOFTINT_PREFIX)/libsoftint.a
173         LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld
174 else
175         BASE_LIBS = $(LIBC_PREFIX)/libc.so0 $(LIBSOFTINT_PREFIX)/libsofti.so0
176         LFLAGS = -Bdynamic
177         LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link-dlexe.ld
178 endif
180 ifeq ($(CONFIG_OPTIMIZE_FOR_SIZE),y)
181         OPTIMIZATION = s
182 else
183         OPTIMIZATION = 3
184 endif
186 .PHONY: all clean
188 all: $(VERSION_DEF) $(COMMON_MAKEFILE) $(CONFIG_MAKEFILE) $(CONFIG_HEADER) $(LIBS) $(OUTPUT) $(LOUTPUT) $(EXTRA_OUTPUT)
189         -[ -f $(DEPEND) ] && cp -a $(DEPEND) $(DEPEND_PREV)
191 clean:
192         rm -f $(DEPEND) $(DEPEND_PREV) $(JOB) $(OUTPUT) $(LARCHIVE) $(LOUTPUT) $(EXTRA_OUTPUT) $(EXTRA_CLEAN)
193         find . -name '*.o' -follow -exec rm \{\} \;
194         find . -name '*.lo' -follow -exec rm \{\} \;
196 GCC_CFLAGS = $(LIBC_INCLUDES_FLAGS) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \
197         -fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) \
198         -finput-charset=UTF-8 -ffreestanding -fno-builtin -nostdlib -nostdinc \
199         -Wall -Wextra -Wno-clobbered -Wno-unused-parameter -Wmissing-prototypes \
200         -std=gnu99 -Werror-implicit-function-declaration -Wwrite-strings \
201         -pipe -ggdb -D__$(ENDIANESS)__
203 ICC_CFLAGS = $(LIBC_INCLUDES_FLAGS) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \
204         -fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) \
205         -finput-charset=UTF-8 -ffreestanding -fno-builtin -nostdlib -nostdinc \
206         -Wall -Wextra -Wno-clobbered -Wno-unused-parameter -Wmissing-prototypes \
207         -Werror-implicit-function-declaration -Wwrite-strings \
208         -pipe -g -D__$(ENDIANESS)__
210 # clang does not support following options but I am not sure whether
211 # something won't break because of that:
212 # -fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) -finput-charset=UTF-8
213 CLANG_CFLAGS = $(LIBC_INCLUDES_FLAGS) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \
214         -ffreestanding -fno-builtin -nostdlib -nostdinc \
215         -Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes \
216         -Werror-implicit-function-declaration -Wwrite-strings \
217         -integrated-as \
218         -pipe -g -target $(CLANG_TARGET) -D__$(ENDIANESS)__
220 LIB_CFLAGS = $(CFLAGS) -fPIC -D__IN_SHARED_LIBC__
221 LIB_LFLAGS = $(LFLAGS) -shared -soname $(LSONAME) --whole-archive
223 ifeq ($(CONFIG_DEBUG),y)
224         GCC_CFLAGS += -Werror
225         ICC_CFLAGS += -Werror
226 endif
228 ifeq ($(CONFIG_LINE_DEBUG),y)
229         GCC_CFLAGS += -g
230         ICC_CFLAGS += -g
231         CLANG_CFLAGS += -g
232 endif
234 # Prepare for POSIX before including platform specific stuff
235 ifeq ($(POSIX_COMPAT),y)
236         CFLAGS = -I$(LIBPOSIX_PREFIX)/include/posix  -I$(LIBPOSIX_PREFIX)/include/
237         BASE_LIBS = $(LIBPOSIX_PREFIX)/libposixaslibc.a $(LIBPOSIX_PREFIX)/libc4posix.a $(LIBSOFTINT_PREFIX)/libsoftint.a
238 endif
240 # Do we need math?
241 ifeq ($(NEEDS_MATH),y)
242         BASE_LIBS += $(LIBMATH_PREFIX)/libmath.a
243 endif
245 ## Setup platform configuration
248 -include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.common
250 ## Compilation options
253 JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py
255 ifeq ($(COMPILER),gcc_cross)
256         CFLAGS += $(GCC_CFLAGS) $(EXTRA_CFLAGS)
257         DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
258 endif
260 ifeq ($(COMPILER),gcc_helenos)
261         CFLAGS += $(GCC_CFLAGS) $(EXTRA_CFLAGS)
262         DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
263 endif
265 ifeq ($(COMPILER),gcc_native)
266         CFLAGS += $(GCC_CFLAGS) $(EXTRA_CFLAGS)
267         DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
268 endif
270 ifeq ($(COMPILER),icc)
271         CFLAGS += $(ICC_CFLAGS) $(EXTRA_CFLAGS)
272         DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
273 endif
275 ifeq ($(COMPILER),clang)
276         CFLAGS += $(CLANG_CFLAGS) $(EXTRA_CFLAGS)
277         GCC_CFLAGS += $(EXTRA_CFLAGS)
278         DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
279 endif
281 -include $(DEPEND)
283 OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
284 LOBJECTS := $(addsuffix .lo,$(basename $(SOURCES)))
286 ifneq ($(BINARY),)
287 %.disasm: $(BINARY)
288 ifeq ($(CONFIG_LINE_DEBUG),y)
289         $(OBJDUMP) -d -S $< > $@
290 else
291         $(OBJDUMP) -d $< > $@
292 endif
294 $(BINARY): $(LINKER_SCRIPT) $(OBJECTS) $(LIBS) $(BASE_LIBS)
295         $(LD) -n $(LFLAGS) -T $(LINKER_SCRIPT) -M -Map $(BINARY).map -o $(BINARY) $(OBJECTS) $(LIBS) $(BASE_LIBS)
296 ifeq ($(CONFIG_STRIP_BINARIES),y)
297         $(STRIP) $(BINARY)
298 endif
299 endif
301 ifneq ($(SLIBRARY),)
302 %.disasm: $(LOUTPUT)
303 ifeq ($(CONFIG_LINE_DEBUG),y)
304         $(OBJDUMP) -d -S $< > $@
305 else
306         $(OBJDUMP) -d $< > $@
307 endif
309 $(LOUTPUT): $(LARCHIVE) $(LIBC_PREFIX)/arch/$(UARCH)/_link-shlib.ld
310         $(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link-shlib.ld $(LIB_LFLAGS) $(LARCHIVE) -o $@ -Map $(LOUTPUT).map
312 $(LIBRARY).so:
313         ln -s $(SLIBRARY) $@
315 $(LSONAME):
316         ln -s $(SLIBRARY) $@
317 endif
319 ifneq ($(LIBRARY),)
320 %.a: $(OBJECTS)
321         $(AR) rc $@ $(OBJECTS)
322 endif
324 ifneq ($(SLIBRARY),)
325 %.la: $(LOBJECTS)
326         $(AR) rc $@ $(LOBJECTS)
327 endif
329 %.o: %.S $(DEPEND)
330         $(GCC) $(DEFS) $(GCC_CFLAGS) -D__ASM__ -c $< -o $@
331 ifeq ($(PRECHECK),y)
332         $(JOBFILE) $(JOB) $< $@ as asm/preproc $(DEFS) $(CFLAGS) -D__ASM__
333 endif
335 %.o: %.s $(DEPEND)
336         $(AS) $(AFLAGS) -o $@ $<
337 ifeq ($(PRECHECK),y)
338         $(JOBFILE) $(JOB) $< $@ as asm
339 endif
341 %.o: %.c $(DEPEND)
342         $(CC) $(DEFS) $(CFLAGS) -c $< -o $@
343 ifeq ($(PRECHECK),y)
344         $(JOBFILE) $(JOB) $< $@ cc core $(DEFS) $(CFLAGS)
345 endif
347 %.lo: %.S $(DEPEND)
348         $(CC) $(DEFS) $(LIB_CFLAGS) -D__ASM__ -c $< -o $@
349 ifeq ($(PRECHECK),y)
350         $(JOBFILE) $(JOB) $< $@ as asm/preproc $(DEFS) $(CFLAGS) -D__ASM__
351 endif
353 %.lo: %.s $(DEPEND)
354         $(AS) $(AFLAGS) -o $@ $<
355 ifeq ($(PRECHECK),y)
356         $(JOBFILE) $(JOB) $< $@ as asm
357 endif
359 %.lo: %.c $(DEPEND)
360         $(CC) $(DEFS) $(LIB_CFLAGS) -c $< -o $@
361 ifeq ($(PRECHECK),y)
362         $(JOBFILE) $(JOB) $< $@ cc core $(DEFS) $(CFLAGS)
363 endif
365 $(DEPEND): $(PRE_DEPEND)
366         makedepend -f - -- $(DEPEND_DEFS) $(CFLAGS) -- $(SOURCES) > $@ 2> /dev/null
367         -[ -f $(DEPEND_PREV) ] && diff -q $(DEPEND_PREV) $@ && mv -f $(DEPEND_PREV) $@
370 # This explicit dependecy of the output binary on the object files seems to be
371 # necessary to prevent parallel build failures (GNU make bug #26893 ???).
372 $(OUTPUT): $(OBJECTS)
374 $(LARCHIVE): $(LOBJECTS)