Add demos/tetris build script for GCC
[helenos.git] / uspace / Makefile.common
blobbcb3ced6b84edc1fb670fc357d16826b69d62dae
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
48 # Optionally, for a binary:
49 #   STATIC_NEEDED      set to 'y' for init binaries, will build statically
50 #                      linked version
51 #   STATIC_ONLY        set to 'y' if binary cannot be linked dynamically
52 #                      (e.g. uses thread-local variables)
54 # Optionally, for a libary:
55 #   SLIBRARY           Name with full version, e.g. libfoo.so.0.0 
56 #   LSONAME            Soname / name with short version, e.g. libfoo.so.0
58 # (x) required variables
59 # (/) exactly one of the variables must be defined
62 ROOT_PATH = $(USPACE_PREFIX)/..
64 VERSION_DEF = $(ROOT_PATH)/version
66 COMMON_MAKEFILE = $(ROOT_PATH)/Makefile.common
67 COMMON_HEADER = $(ROOT_PATH)/common.h
69 CONFIG_MAKEFILE = $(ROOT_PATH)/Makefile.config
70 CONFIG_HEADER = $(ROOT_PATH)/config.h
72 -include $(VERSION_DEF)
73 -include $(COMMON_MAKEFILE)
74 -include $(CONFIG_MAKEFILE)
76 ifneq ($(BINARY),)
77         JOB = $(BINARY).job
78         OUTPUT = $(BINARY)
79         EXTRA_OUTPUT += $(BINARY).disasm
80         EXTRA_CLEAN += $(BINARY).map
81 endif
83 ifneq ($(LIBRARY),)
84         JOB = $(LIBRARY).job
85         OUTPUT = $(LIBRARY).a
86 endif
88 ifeq ($(CONFIG_BUILD_SHARED_LIBS), y)
89         ifneq ($(SLIBRARY),)
90                 LARCHIVE = $(LIBRARY).la
91                 LOUTPUT = $(SLIBRARY)
92                 EXTRA_OUTPUT += $(LOUTPUT).disasm $(LIBRARY).so $(LSONAME)
93                 EXTRA_CLEAN += $(LOUTPUT).map $(LOUTPUT).ldisasm \
94                     $(LIBC_PREFIX)/shared/arch/$(UARCH)/_lib.ld \
95                     $(LIBRARY).so $(LSONAME)
96         endif
97 endif
99 DEPEND = Makefile.depend
100 DEPEND_PREV = $(DEPEND).prev
102 LIB_PREFIX = $(USPACE_PREFIX)/lib
104 LIBC_PREFIX = $(LIB_PREFIX)/c
105 LIBC_INCLUDES_FLAGS = \
106         -I$(LIBC_PREFIX)/include \
107         -I$(LIBC_PREFIX)/arch/$(UARCH)/include \
108         -I$(ROOT_PATH)/abi/include
109 LIBSOFTFLOAT_PREFIX = $(LIB_PREFIX)/softfloat
110 LIBSOFTINT_PREFIX = $(LIB_PREFIX)/softint
112 LIBPOSIX_PREFIX = $(LIB_PREFIX)/posix
114 LIBBLOCK_PREFIX = $(LIB_PREFIX)/block
115 LIBFS_PREFIX = $(LIB_PREFIX)/fs
116 LIBCLUI_PREFIX = $(LIB_PREFIX)/clui
117 LIBFMTUTIL_PREFIX = $(LIB_PREFIX)/fmtutil
119 LIBGRAPH_PREFIX = $(LIB_PREFIX)/graph
120 LIBSOFTREND_PREFIX = $(LIB_PREFIX)/softrend
121 LIBDRAW_PREFIX = $(LIB_PREFIX)/draw
122 LIBGUI_PREFIX = $(LIB_PREFIX)/gui
124 LIBEXT4_PREFIX = $(LIB_PREFIX)/ext4
126 LIBUSB_PREFIX = $(LIB_PREFIX)/usb
127 LIBUSBHOST_PREFIX = $(LIB_PREFIX)/usbhost
128 LIBUSBDEV_PREFIX = $(LIB_PREFIX)/usbdev
129 LIBUSBHID_PREFIX = $(LIB_PREFIX)/usbhid
130 LIBUSBVIRT_PREFIX = $(LIB_PREFIX)/usbvirt
132 LIBDRV_PREFIX = $(LIB_PREFIX)/drv
133 LIBHOUND_PREFIX = $(LIB_PREFIX)/hound
134 LIBPCM_PREFIX = $(LIB_PREFIX)/pcm
135 LIBNET_PREFIX = $(LIB_PREFIX)/net
136 LIBNIC_PREFIX = $(LIB_PREFIX)/nic
137 LIBMINIX_PREFIX = $(LIB_PREFIX)/minix
139 LIBSCSI_PREFIX = $(LIB_PREFIX)/scsi
141 LIBBITHENGE_PREFIX = $(LIB_PREFIX)/bithenge
143 LIBHTTP_PREFIX = $(LIB_PREFIX)/http
144 LIBURI_PREFIX = $(LIB_PREFIX)/uri
146 ifeq ($(STATIC_NEEDED),y)
147         STATIC_BUILD = y
148 else
149         ifeq ($(STATIC_ONLY),y)
150                 STATIC_BUILD = y
151         else
152                 ifeq ($(CONFIG_USE_SHARED_LIBS), y)
153                         STATIC_BUILD = n
154                 else
155                         STATIC_BUILD = y
156                 endif
157         endif
158 endif
160 # Build static whenever we use libusb because that library uses 
161 # thread local variables
162 ifneq ($(findstring usb, $(LIBS)),)
163         STATIC_BUILD = y
164 endif
166 ifeq ($(STATIC_BUILD),y)
167         BASE_LIBS = $(LIBC_PREFIX)/libc.a $(LIBSOFTINT_PREFIX)/libsoftint.a
168         LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld
169 else
170         BASE_LIBS = $(LIBC_PREFIX)/libc.so0 $(LIBSOFTINT_PREFIX)/libsofti.so0
171         LFLAGS = -Bdynamic
172         LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link-dlexe.ld
173 endif
175 ifeq ($(CONFIG_OPTIMIZE_FOR_SIZE),y)
176         OPTIMIZATION = s
177 else
178         OPTIMIZATION = 3
179 endif
181 .PHONY: all clean
183 all: $(VERSION_DEF) $(COMMON_MAKEFILE) $(CONFIG_MAKEFILE) $(CONFIG_HEADER) $(LIBS) $(OUTPUT) $(LOUTPUT) $(EXTRA_OUTPUT)
184         -[ -f $(DEPEND) ] && cp -a $(DEPEND) $(DEPEND_PREV)
186 clean:
187         rm -f $(DEPEND) $(DEPEND_PREV) $(JOB) $(OUTPUT) $(LARCHIVE) $(LOUTPUT) $(EXTRA_OUTPUT) $(EXTRA_CLEAN)
188         find . -name '*.o' -follow -exec rm \{\} \;
189         find . -name '*.lo' -follow -exec rm \{\} \;
191 GCC_CFLAGS = $(LIBC_INCLUDES_FLAGS) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \
192         -fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) \
193         -finput-charset=UTF-8 -ffreestanding -fno-builtin -nostdlib -nostdinc \
194         -Wall -Wextra -Wno-clobbered -Wno-unused-parameter -Wmissing-prototypes \
195         -std=gnu99 -Werror-implicit-function-declaration -Wwrite-strings \
196         -pipe -ggdb -D__$(ENDIANESS)__
198 ICC_CFLAGS = $(LIBC_INCLUDES_FLAGS) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \
199         -fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) \
200         -finput-charset=UTF-8 -ffreestanding -fno-builtin -nostdlib -nostdinc \
201         -Wall -Wextra -Wno-clobbered -Wno-unused-parameter -Wmissing-prototypes \
202         -Werror-implicit-function-declaration -Wwrite-strings \
203         -pipe -g -D__$(ENDIANESS)__
205 # clang does not support following options but I am not sure whether
206 # something won't break because of that:
207 # -fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) -finput-charset=UTF-8
208 CLANG_CFLAGS = $(LIBC_INCLUDES_FLAGS) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \
209         -ffreestanding -fno-builtin -nostdlib -nostdinc \
210         -Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes \
211         -Werror-implicit-function-declaration -Wwrite-strings \
212         -integrated-as \
213         -pipe -g -target $(CLANG_TARGET) -D__$(ENDIANESS)__
215 LIB_CFLAGS = $(CFLAGS) -fPIC -D__IN_SHARED_LIBC__
216 LIB_LFLAGS = $(LFLAGS) -shared -soname $(LSONAME) --whole-archive
218 ifeq ($(CONFIG_DEBUG),y)
219         GCC_CFLAGS += -Werror
220         ICC_CFLAGS += -Werror
221 endif
223 ifeq ($(CONFIG_LINE_DEBUG),y)
224         GCC_CFLAGS += -g
225         ICC_CFLAGS += -g
226         CLANG_CFLAGS += -g
227 endif
229 # Prepare for POSIX before including platform specific stuff
230 ifeq ($(POSIX_COMPAT),y)
231         CFLAGS = -I$(LIBPOSIX_PREFIX)/include/posix  -I$(LIBPOSIX_PREFIX)/include/
232         BASE_LIBS = $(LIBPOSIX_PREFIX)/libposixaslibc.a $(LIBPOSIX_PREFIX)/libc4posix.a $(LIBSOFTINT_PREFIX)/libsoftint.a
233 endif
235 ## Setup platform configuration
238 -include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.common
240 ## Compilation options
243 JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py
245 ifeq ($(COMPILER),gcc_cross)
246         CFLAGS += $(GCC_CFLAGS) $(EXTRA_CFLAGS)
247         DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
248 endif
250 ifeq ($(COMPILER),gcc_helenos)
251         CFLAGS += $(GCC_CFLAGS) $(EXTRA_CFLAGS)
252         DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
253 endif
255 ifeq ($(COMPILER),gcc_native)
256         CFLAGS += $(GCC_CFLAGS) $(EXTRA_CFLAGS)
257         DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
258 endif
260 ifeq ($(COMPILER),icc)
261         CFLAGS += $(ICC_CFLAGS) $(EXTRA_CFLAGS)
262         DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
263 endif
265 ifeq ($(COMPILER),clang)
266         CFLAGS += $(CLANG_CFLAGS) $(EXTRA_CFLAGS)
267         GCC_CFLAGS += $(EXTRA_CFLAGS)
268         DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
269 endif
271 -include $(DEPEND)
273 OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
274 LOBJECTS := $(addsuffix .lo,$(basename $(SOURCES)))
276 ifneq ($(BINARY),)
277 %.disasm: $(BINARY)
278 ifeq ($(CONFIG_LINE_DEBUG),y)
279         $(OBJDUMP) -d -S $< > $@
280 else
281         $(OBJDUMP) -d $< > $@
282 endif
284 $(BINARY): $(LINKER_SCRIPT) $(OBJECTS) $(LIBS) $(BASE_LIBS)
285         $(LD) -n $(LFLAGS) -T $(LINKER_SCRIPT) -M -Map $(BINARY).map -o $(BINARY) $(OBJECTS) $(LIBS) $(BASE_LIBS)
286 ifeq ($(CONFIG_STRIP_BINARIES),y)
287         $(STRIP) $(BINARY)
288 endif
289 endif
291 ifneq ($(SLIBRARY),)
292 %.disasm: $(LOUTPUT)
293 ifeq ($(CONFIG_LINE_DEBUG),y)
294         $(OBJDUMP) -d -S $< > $@
295 else
296         $(OBJDUMP) -d $< > $@
297 endif
299 $(LOUTPUT): $(LARCHIVE) $(LIBC_PREFIX)/arch/$(UARCH)/_link-shlib.ld
300         $(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link-shlib.ld $(LIB_LFLAGS) $(LARCHIVE) -o $@ -Map $(LOUTPUT).map
302 $(LIBRARY).so:
303         ln -s $(SLIBRARY) $@
305 $(LSONAME):
306         ln -s $(SLIBRARY) $@
307 endif
309 ifneq ($(LIBRARY),)
310 %.a: $(OBJECTS)
311         $(AR) rc $@ $(OBJECTS)
312 endif
314 ifneq ($(SLIBRARY),)
315 %.la: $(LOBJECTS)
316         $(AR) rc $@ $(LOBJECTS)
317 endif
319 %.o: %.S $(DEPEND)
320         $(GCC) $(DEFS) $(GCC_CFLAGS) -D__ASM__ -c $< -o $@
321 ifeq ($(PRECHECK),y)
322         $(JOBFILE) $(JOB) $< $@ as asm/preproc $(DEFS) $(CFLAGS) -D__ASM__
323 endif
325 %.o: %.s $(DEPEND)
326         $(AS) $(AFLAGS) -o $@ $<
327 ifeq ($(PRECHECK),y)
328         $(JOBFILE) $(JOB) $< $@ as asm
329 endif
331 %.o: %.c $(DEPEND)
332         $(CC) $(DEFS) $(CFLAGS) -c $< -o $@
333 ifeq ($(PRECHECK),y)
334         $(JOBFILE) $(JOB) $< $@ cc core $(DEFS) $(CFLAGS)
335 endif
337 %.lo: %.S $(DEPEND)
338         $(CC) $(DEFS) $(LIB_CFLAGS) -D__ASM__ -c $< -o $@
339 ifeq ($(PRECHECK),y)
340         $(JOBFILE) $(JOB) $< $@ as asm/preproc $(DEFS) $(CFLAGS) -D__ASM__
341 endif
343 %.lo: %.s $(DEPEND)
344         $(AS) $(AFLAGS) -o $@ $<
345 ifeq ($(PRECHECK),y)
346         $(JOBFILE) $(JOB) $< $@ as asm
347 endif
349 %.lo: %.c $(DEPEND)
350         $(CC) $(DEFS) $(LIB_CFLAGS) -c $< -o $@
351 ifeq ($(PRECHECK),y)
352         $(JOBFILE) $(JOB) $< $@ cc core $(DEFS) $(CFLAGS)
353 endif
355 $(DEPEND): $(PRE_DEPEND)
356         makedepend -f - -- $(DEPEND_DEFS) $(CFLAGS) -- $(SOURCES) > $@ 2> /dev/null
357         -[ -f $(DEPEND_PREV) ] && diff -q $(DEPEND_PREV) $@ && mv -f $(DEPEND_PREV) $@
360 # This explicit dependecy of the output binary on the object files seems to be
361 # necessary to prevent parallel build failures (GNU make bug #26893 ???).
362 $(OUTPUT): $(OBJECTS)
364 $(LARCHIVE): $(LOBJECTS)