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 # 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 # MATH set to 'y' to use the math library
47 # POSIX_COMPAT set to 'y' to use POSIX compatibility layer
49 # Optionally, for a binary:
50 # STATIC_NEEDED set to 'y' for init binaries, will build statically
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)
80 EXTRA_OUTPUT += $(BINARY).disasm
81 EXTRA_CLEAN += $(BINARY).map
89 ifeq ($(CONFIG_BUILD_SHARED_LIBS),y)
91 LARCHIVE = $(LIBRARY).la
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)
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
113 LIBMATH_PREFIX = $(LIB_PREFIX)/math
114 LIBMATH_INCLUDES_FLAGS = \
115 -I$(LIBMATH_PREFIX)/include \
116 -I$(LIBMATH_PREFIX)/arch/$(UARCH)/include
118 LIBPOSIX_PREFIX = $(LIB_PREFIX)/posix
120 LIBBLOCK_PREFIX = $(LIB_PREFIX)/block
121 LIBFS_PREFIX = $(LIB_PREFIX)/fs
122 LIBCLUI_PREFIX = $(LIB_PREFIX)/clui
123 LIBFMTUTIL_PREFIX = $(LIB_PREFIX)/fmtutil
125 LIBGRAPH_PREFIX = $(LIB_PREFIX)/graph
126 LIBSOFTREND_PREFIX = $(LIB_PREFIX)/softrend
127 LIBDRAW_PREFIX = $(LIB_PREFIX)/draw
128 LIBGUI_PREFIX = $(LIB_PREFIX)/gui
130 LIBEXT4_PREFIX = $(LIB_PREFIX)/ext4
132 LIBUSB_PREFIX = $(LIB_PREFIX)/usb
133 LIBUSBHOST_PREFIX = $(LIB_PREFIX)/usbhost
134 LIBUSBDEV_PREFIX = $(LIB_PREFIX)/usbdev
135 LIBUSBHID_PREFIX = $(LIB_PREFIX)/usbhid
136 LIBUSBVIRT_PREFIX = $(LIB_PREFIX)/usbvirt
138 LIBDRV_PREFIX = $(LIB_PREFIX)/drv
139 LIBHOUND_PREFIX = $(LIB_PREFIX)/hound
140 LIBPCM_PREFIX = $(LIB_PREFIX)/pcm
141 LIBNET_PREFIX = $(LIB_PREFIX)/net
142 LIBNIC_PREFIX = $(LIB_PREFIX)/nic
143 LIBMINIX_PREFIX = $(LIB_PREFIX)/minix
145 LIBSCSI_PREFIX = $(LIB_PREFIX)/scsi
147 LIBBITHENGE_PREFIX = $(LIB_PREFIX)/bithenge
149 LIBHTTP_PREFIX = $(LIB_PREFIX)/http
150 LIBURI_PREFIX = $(LIB_PREFIX)/uri
152 LIBMBR_PREFIX = $(LIB_PREFIX)/mbr
153 LIBGPT_PREFIX = $(LIB_PREFIX)/gpt
155 ifeq ($(STATIC_NEEDED),y)
158 ifeq ($(STATIC_ONLY),y)
161 ifeq ($(CONFIG_USE_SHARED_LIBS),y)
169 # Build static whenever we use libusb because that library uses
170 # thread local variables
171 ifneq ($(findstring usb, $(LIBS)),)
175 ifeq ($(STATIC_BUILD),y)
176 BASE_LIBS = $(LIBC_PREFIX)/libc.a $(LIBSOFTINT_PREFIX)/libsoftint.a
177 LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link.ld
179 BASE_LIBS += $(LIBMATH_PREFIX)/libmath.a
182 BASE_LIBS = $(LIBC_PREFIX)/libc.so0 $(LIBSOFTINT_PREFIX)/libsofti.so0
184 LINKER_SCRIPT ?= $(LIBC_PREFIX)/arch/$(UARCH)/_link-dlexe.ld
186 BASE_LIBS += $(LIBMATH_PREFIX)/libmath.so0
191 INCLUDES_FLAGS = $(LIBC_INCLUDES_FLAGS) $(LIBMATH_INCLUDES_FLAGS)
193 INCLUDES_FLAGS = $(LIBC_INCLUDES_FLAGS)
196 ifeq ($(CONFIG_OPTIMIZE_FOR_SIZE),y)
204 all: $(VERSION_DEF) $(COMMON_MAKEFILE) $(CONFIG_MAKEFILE) $(CONFIG_HEADER) $(LIBS) $(OUTPUT) $(LOUTPUT) $(EXTRA_OUTPUT)
205 -[ -f $(DEPEND) ] && cp -a $(DEPEND) $(DEPEND_PREV)
208 rm -f $(DEPEND) $(DEPEND_PREV) $(JOB) $(OUTPUT) $(LARCHIVE) $(LOUTPUT) $(EXTRA_OUTPUT) $(EXTRA_CLEAN)
209 find . -name '*.o' -follow -exec rm \{\} \;
210 find . -name '*.lo' -follow -exec rm \{\} \;
212 GCC_CFLAGS = $(INCLUDES_FLAGS) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \
213 -fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) \
214 -finput-charset=UTF-8 -ffreestanding -fno-builtin -nostdlib -nostdinc \
215 -Wall -Wextra -Wno-clobbered -Wno-unused-parameter -Wmissing-prototypes \
216 -std=gnu99 -Werror-implicit-function-declaration -Wwrite-strings \
217 -pipe -ggdb -D__$(ENDIANESS)__
219 ICC_CFLAGS = $(INCLUDES_FLAGS) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \
220 -fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) \
221 -finput-charset=UTF-8 -ffreestanding -fno-builtin -nostdlib -nostdinc \
222 -Wall -Wextra -Wno-clobbered -Wno-unused-parameter -Wmissing-prototypes \
223 -Werror-implicit-function-declaration -Wwrite-strings \
224 -pipe -g -D__$(ENDIANESS)__
226 # clang does not support following options but I am not sure whether
227 # something won't break because of that:
228 # -fexec-charset=UTF-8 -fwide-exec-charset=UTF-32$(ENDIANESS) -finput-charset=UTF-8
229 CLANG_CFLAGS = $(LIBC_INCLUDES_FLAGS) -O$(OPTIMIZATION) -imacros $(CONFIG_HEADER) \
230 -ffreestanding -fno-builtin -nostdlib -nostdinc \
231 -Wall -Wextra -Wno-unused-parameter -Wmissing-prototypes \
232 -Werror-implicit-function-declaration -Wwrite-strings \
234 -pipe -g -target $(CLANG_TARGET) -D__$(ENDIANESS)__
236 LIB_CFLAGS = $(CFLAGS) -fPIC -D__IN_SHARED_LIBC__
237 LIB_LFLAGS = $(LFLAGS) -shared -soname $(LSONAME) --whole-archive
239 ifeq ($(CONFIG_DEBUG),y)
240 GCC_CFLAGS += -Werror
241 ICC_CFLAGS += -Werror
244 ifeq ($(CONFIG_LINE_DEBUG),y)
250 # Prepare for POSIX before including platform specific stuff
251 ifeq ($(POSIX_COMPAT),y)
252 CFLAGS = -I$(LIBPOSIX_PREFIX)/include/posix -I$(LIBPOSIX_PREFIX)/include/
253 BASE_LIBS = $(LIBPOSIX_PREFIX)/libposixaslibc.a $(LIBPOSIX_PREFIX)/libc4posix.a $(LIBSOFTINT_PREFIX)/libsoftint.a
256 ## Setup platform configuration
259 -include $(LIBC_PREFIX)/arch/$(UARCH)/Makefile.common
261 ## Compilation options
264 JOBFILE = $(LIBC_PREFIX)/../../../tools/jobfile.py
266 ifeq ($(COMPILER),gcc_cross)
267 CFLAGS += $(GCC_CFLAGS) $(EXTRA_CFLAGS)
268 DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
271 ifeq ($(COMPILER),gcc_helenos)
272 CFLAGS += $(GCC_CFLAGS) $(EXTRA_CFLAGS)
273 DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
276 ifeq ($(COMPILER),gcc_native)
277 CFLAGS += $(GCC_CFLAGS) $(EXTRA_CFLAGS)
278 DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
281 ifeq ($(COMPILER),icc)
282 CFLAGS += $(ICC_CFLAGS) $(EXTRA_CFLAGS)
283 DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
286 ifeq ($(COMPILER),clang)
287 CFLAGS += $(CLANG_CFLAGS) $(EXTRA_CFLAGS)
288 GCC_CFLAGS += $(EXTRA_CFLAGS)
289 DEPEND_DEFS = $(DEFS) $(CONFIG_DEFS)
294 OBJECTS := $(addsuffix .o,$(basename $(SOURCES)))
295 LOBJECTS := $(addsuffix .lo,$(basename $(SOURCES)))
299 ifeq ($(CONFIG_LINE_DEBUG),y)
300 $(OBJDUMP) -d -S $< > $@
302 $(OBJDUMP) -d $< > $@
305 $(BINARY): $(LINKER_SCRIPT) $(OBJECTS) $(LIBS) $(BASE_LIBS)
306 $(LD) -n $(LFLAGS) -T $(LINKER_SCRIPT) -M -Map $(BINARY).map -o $(BINARY) $(OBJECTS) $(LIBS) $(BASE_LIBS)
307 ifeq ($(CONFIG_STRIP_BINARIES),y)
314 ifeq ($(CONFIG_LINE_DEBUG),y)
315 $(OBJDUMP) -d -S $< > $@
317 $(OBJDUMP) -d $< > $@
320 $(LOUTPUT): $(LARCHIVE) $(LIBC_PREFIX)/arch/$(UARCH)/_link-shlib.ld
321 $(LD) -T $(LIBC_PREFIX)/arch/$(UARCH)/_link-shlib.ld $(LIB_LFLAGS) $(LARCHIVE) -o $@ -Map $(LOUTPUT).map
332 $(AR) rc $@ $(OBJECTS)
337 $(AR) rc $@ $(LOBJECTS)
341 $(GCC) $(DEFS) $(GCC_CFLAGS) -D__ASM__ -c $< -o $@
343 $(JOBFILE) $(JOB) $< $@ as asm/preproc $(DEFS) $(CFLAGS) -D__ASM__
347 $(AS) $(AFLAGS) -o $@ $<
349 $(JOBFILE) $(JOB) $< $@ as asm
353 $(CC) $(DEFS) $(CFLAGS) -c $< -o $@
355 $(JOBFILE) $(JOB) $< $@ cc core $(DEFS) $(CFLAGS)
359 $(CC) $(DEFS) $(LIB_CFLAGS) -D__ASM__ -c $< -o $@
361 $(JOBFILE) $(JOB) $< $@ as asm/preproc $(DEFS) $(CFLAGS) -D__ASM__
365 $(AS) $(AFLAGS) -o $@ $<
367 $(JOBFILE) $(JOB) $< $@ as asm
371 $(CC) $(DEFS) $(LIB_CFLAGS) -c $< -o $@
373 $(JOBFILE) $(JOB) $< $@ cc core $(DEFS) $(CFLAGS)
376 $(DEPEND): $(PRE_DEPEND)
377 makedepend -f - -- $(DEPEND_DEFS) $(CFLAGS) -- $(SOURCES) > $@ 2> /dev/null
378 -[ -f $(DEPEND_PREV) ] && diff -q $(DEPEND_PREV) $@ && mv -f $(DEPEND_PREV) $@
381 # This explicit dependecy of the output binary on the object files seems to be
382 # necessary to prevent parallel build failures (GNU make bug #26893 ???).
383 $(OUTPUT): $(OBJECTS)
385 $(LARCHIVE): $(LOBJECTS)