Added some documentation on how to add static-pie support to the porting guide
[uclibc-ng.git] / Rules.mak
blob8b54e2854e0e6a33bebf3416e4a9647aba1eb31c
1 # Rules.mak for uClibc-ng
3 # Copyright (C) 2000-2008 Erik Andersen <andersen@uclibc.org>
4 # Copyright (C) 2015-2022 Waldemar Brodkorb <wbx@uclibc-ng.org>
6 # Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
9 # make nano-doc
10 # FOO = bar -- recursively expanded variable. Value is remebered verbatim.
11 # If it contains references to other variables, these references
12 # are expanded whenever this variable is _substituted_.
13 # FOO := bar -- simply expanded variable. Right hand is expanded when
14 # the variable is _defined_. Therefore faster than =.
15 # FOO ?= bar -- set a value only if it is not already set
16 # (behaves as =, not :=).
17 # FOO += bar -- append; if FOO is not defined, acts like = (not :=).
20 # check for proper make version
21 ifneq ($(findstring x3.7,x$(MAKE_VERSION)),)
22 $(error Your make is too old $(MAKE_VERSION). Go get at least 3.80)
23 endif
25 #-----------------------------------------------------------
26 # This file contains rules which are shared between multiple
27 # Makefiles. All normal configuration options live in the
28 # file named ".config". Don't mess with this file unless
29 # you know what you are doing.
31 clean_targets := clean realclean distclean \
32 objclean-y headers_clean-y CLEAN_utils
33 noconfig_targets := menuconfig config nconfig \
34 oldaskconfig silentoldconfig oldconfig allnoconfig allyesconfig \
35 alldefconfig randconfig defconfig savedefconfig listnewconfig \
36 olddefconfig \
37 xconfig gconfig update-po-config mconf qconf gconf nconf conf \
38 release dist tags help
41 #-----------------------------------------------------------
42 # If you are running a cross compiler, you will want to set
43 # 'CROSS_COMPILE' to something more interesting ... Target
44 # architecture is determined by asking the CC compiler what
45 # arch it compiles things for, so unless your compiler is
46 # broken, you should not need to specify TARGET_ARCH.
48 # Most people will set this stuff on the command line, i.e.
49 # make CROSS_COMPILE=arm-linux-
50 # will build uClibc-ng for 'arm'.
51 # CROSS is still supported for backward compatibily only
53 CROSS_COMPILE ?= $(CROSS)
55 CC = $(CROSS_COMPILE)gcc
56 AR = $(CROSS_COMPILE)ar
57 LD = $(CROSS_COMPILE)ld
58 NM = $(CROSS_COMPILE)nm
59 OBJDUMP = $(CROSS_COMPILE)objdump
60 STRIPTOOL = $(CROSS_COMPILE)strip
62 INSTALL = install
63 LN = ln
64 RM = rm -f
65 TAR = tar
66 SED = sed
67 AWK = awk
69 STRIP_FLAGS ?= -x -R .note -R .comment
71 # Select the compiler needed to build binaries for your development system
72 HOSTCC = gcc
73 BUILD_CFLAGS = -Os
75 #---------------------------------------------------------
76 # Nothing beyond this point should ever be touched by mere
77 # mortals. Unless you hang out with the gods, you should
78 # probably leave all this stuff alone.
80 # strip quotes
81 qstrip = $(strip $(subst ",,$(1)))
82 #"))
84 # kconfig stuff
85 KCONFIG_CONFIG ?= $(top_builddir).config
86 KCONFIG_CONFIG := $(abspath $(KCONFIG_CONFIG))
87 export KCONFIG_CONFIG
88 KCONFIG_AUTOCONFIG := $(dir $(KCONFIG_CONFIG))include/config/auto.conf
89 export KCONFIG_AUTOCONFIG
90 KCONFIG_TRISTATE := $(dir $(KCONFIG_CONFIG))include/config/tristate.conf
91 export KCONFIG_TRISTATE
92 srctree := $(abspath $(top_srcdir))
93 export srctree
94 KCONFIG_AUTOHEADER := $(dir $(KCONFIG_CONFIG))include/generated/autoconf.h
95 export KCONFIG_AUTOHEADER
96 Kconfig := $(abspath $(top_srcdir)extra/Configs/Config.in)
98 # Pull in the user's uClibc-ng configuration
99 ifeq ($(filter $(noconfig_targets) clean CLEAN_%,$(MAKECMDGOALS)),)
100 -include $(KCONFIG_CONFIG)
101 endif
103 TARGET_ARCH:=$(call qstrip,$(TARGET_ARCH))
104 ifeq ($(TARGET_ARCH),)
105 ARCH ?= $(shell uname -m | $(SED) -e s/i.86/i386/ \
106 -e s/sun.*/sparc/ -e s/sparc.*/sparc/ \
107 -e s/arm.*/arm/ -e s/sa110/arm/ \
108 -e s/nds32.*/nds32/ \
109 -e s/sh.*/sh/ \
110 -e s/s390x/s390/ -e s/parisc.*/hppa/ \
111 -e s/ppc.*/powerpc/ -e s/mips.*/mips/ \
112 -e s/xtensa.*/xtensa/ )
113 else
114 ARCH = $(TARGET_ARCH)
115 endif
116 export ARCH
118 # Make certain these contain a final "/", but no "//"s.
119 scrub_path = $(strip $(subst //,/, $(subst ,/, $(call qstrip,$(1)))))
120 TARGET_SUBARCH := $(call qstrip,$(TARGET_SUBARCH))
121 RUNTIME_PREFIX := $(call scrub_path,$(RUNTIME_PREFIX))
122 DEVEL_PREFIX := $(call scrub_path,$(DEVEL_PREFIX))
123 MULTILIB_DIR := $(call scrub_path,$(MULTILIB_DIR))
124 KERNEL_HEADERS := $(call scrub_path,$(KERNEL_HEADERS))
125 export RUNTIME_PREFIX DEVEL_PREFIX KERNEL_HEADERS MULTILIB_DIR
128 # Now config hard core
129 MAJOR_VERSION := 1
130 MINOR_VERSION := 0
131 SUBLEVEL := 42
132 EXTRAVERSION :=
133 VERSION := $(MAJOR_VERSION).$(MINOR_VERSION).$(SUBLEVEL)
134 ABI_VERSION := $(MAJOR_VERSION)
135 ifneq ($(EXTRAVERSION),)
136 VERSION := $(VERSION)$(EXTRAVERSION)
137 endif
138 # Ensure consistent sort order, 'gcc -print-search-dirs' behavior, etc.
139 LC_ALL := C
140 export MAJOR_VERSION MINOR_VERSION SUBLEVEL VERSION ABI_VERSION LC_ALL
142 LIBC := libc
143 SHARED_LIBNAME := $(LIBC).so.$(ABI_VERSION)
145 UCLIBC_LDSO_NAME := $(call qstrip,$(TARGET_LDSO_NAME))
146 ARCH_NATIVE_BIT := $(call qstrip,$(TARGET_ARCH_BITS))
148 UCLIBC_LDSO := $(UCLIBC_LDSO_NAME).so.$(ABI_VERSION)
149 NONSHARED_LIBNAME := uclibc_nonshared.a
150 NONSHARED_LIBPTHREAD := libpthread_nonshared.a
152 libc := $(top_builddir)lib/$(SHARED_LIBNAME)
153 libc.depend := $(top_builddir)lib/$(SHARED_LIBNAME:.$(ABI_VERSION)=)
154 interp := $(top_builddir)lib/interp.os
155 ldso := $(top_builddir)lib/$(UCLIBC_LDSO)
156 headers_dep := $(top_builddir)include/bits/sysnum.h \
157 $(top_builddir)include/bits/uClibc_config.h
158 sub_headers := $(headers_dep)
160 LIBS := $(interp) -L$(top_builddir)lib $(libc:.$(ABI_VERSION)=)
162 # Make sure DESTDIR and PREFIX can be used to install
163 # PREFIX is a uClibcism while DESTDIR is a common GNUism
164 ifndef PREFIX
165 PREFIX = $(DESTDIR)
166 endif
168 ifneq ($(HAVE_SHARED),y)
169 libc :=
170 interp :=
171 ldso :=
172 endif
174 comma:=,
175 space:= #
177 ifeq ($(CROSS_COMPILE),)
178 CROSS_COMPILE=$(call qstrip,$(CROSS_COMPILER_PREFIX))
179 endif
181 # A nifty macro to make testing gcc features easier
182 check_gcc=$(shell \
183 tf="/tmp/cgccucl$$$$.o"; \
184 if $(CC) $(1) -S -o $$tf -xc /dev/null > /dev/null 2>&1; \
185 then echo "$(1)"; else echo "$(2)"; fi; rm -f $$tf )
186 check_as=$(shell \
187 tf="/tmp/casucl$$$$.o"; \
188 if $(CC) -Wa,$(1) -Wa,-Z -c -o $$tf -xassembler /dev/null > /dev/null 2>&1; \
189 then echo "-Wa,$(1)"; fi; rm -f $$tf )
190 check_ld=$(shell \
191 tf="/tmp/clducl$$$$.c"; echo "int _start(){return 0;}int main(){return 0;}" >$$tf; \
192 if $(CC) $(LDFLAG-fuse-ld) $(CFLAG_-Wl--no-warn-mismatch) -Wl,$(1) $(CFLAG_-nostdlib) -o /dev/null $$tf > /dev/null 2>&1; \
193 then echo "$(1)"; fi; rm -f $$tf )
195 # Use variable indirection here so that we can have variable
196 # names with fun chars in them like equal signs
197 define check-tool-var
198 ifeq ($(filter $(clean_targets) CLEAN_%,$(MAKECMDGOALS)),)
199 _v = $(2)_$(3)
200 ifndef $$(_v)
201 $$(_v) := $$(call $(1),$(subst %, ,$(3)))
202 export $$(_v)
203 endif
204 endif
205 endef
207 # Usage: check-gcc-var,<flag>
208 # Check the C compiler to see if it supports <flag>.
209 # Export the variable CFLAG_<flag> if it does.
210 define check-gcc-var
211 $(call check-tool-var,check_gcc,CFLAG,$(1))
212 endef
213 # Usage: check-as-var,<flag>
214 # Check the assembler to see if it supports <flag>. Export the
215 # variable ASFLAG_<flag> if it does (for invoking the assembler),
216 # as well CFLAG_-Wa<flag> (for invoking the compiler driver).
217 define check-as-var
218 $(call check-tool-var,check_as,ASFLAG,$(1))
219 _v = CFLAG_-Wa$(1)
220 export $$(_v) = $$(if $$(ASFLAG_$(1)),-Wa$$(comma)$$(ASFLAG_$(1)))
221 endef
222 # Usage: check-ld-var,<flag>
223 # Check the linker to see if it supports <flag>. Export the
224 # variable LDFLAG_<flag> if it does (for invoking the linker),
225 # as well CFLAG_-Wl<flag> (for invoking the compiler driver).
226 define check-ld-var
227 $(call check-tool-var,check_ld,LDFLAG,$(1))
228 _v = CFLAG_-Wl$(1)
229 export $$(_v) = $$(if $$(LDFLAG_$(1)),-Wl$$(comma)$$(LDFLAG_$(1)))
230 endef
231 # Usage: cache-output-var,<variable>,<shell command>
232 # Execute <shell command> and cache the output in <variable>.
233 define cache-output-var
234 ifndef $(1)
235 $(1) := $$(shell $(2))
236 export $(1)
237 endif
238 endef
241 ARFLAGS:=cr
243 # Note: The check for -nostdlib has to be before all calls to check_ld
244 $(eval $(call check-gcc-var,-nostdlib))
245 $(eval $(call check-gcc-var,-nostartfiles))
246 # deliberately not named CFLAG-fuse-ld since unchecked and from user
247 LDFLAG-fuse-ld := $(filter -fuse-ld=%,$(call qstrip,$(UCLIBC_EXTRA_CFLAGS)))
248 # failed to merge target specific data of file /dev/null
249 # Could use -Wl,--script,$(top_srcdir)extra/scripts/none.lds as well.
250 $(eval $(call check-ld-var,--no-warn-mismatch))
252 $(eval $(call cache-output-var,GCC_VER,$(CC) -dumpversion))
253 GCC_VER := $(subst ., ,$(GCC_VER))
254 GCC_MAJOR_VER ?= $(word 1,$(GCC_VER))
255 GCC_MINOR_VER ?= $(word 2,$(GCC_VER))
257 # Flags in OPTIMIZATION are used only for non-debug builds
259 OPTIMIZATION:=
260 OPTIMIZATION-$(GCC_MAJOR_VER):=
261 OPTIMIZATION-$(GCC_MAJOR_VER).$(GCC_MINOR_VER):=
263 # Use '-Os' optimization if available, else use -O2, allow Config to override
264 $(eval $(call check-gcc-var,-Os))
265 ifneq ($(CFLAG_-Os),)
266 OPTIMIZATION += $(CFLAG_-Os)
267 else
268 $(eval $(call check-gcc-var,-O2))
269 OPTIMIZATION += $(CFLAG_-O2)
270 endif
271 # Use the gcc 3.4 -funit-at-a-time optimization when available
272 $(eval $(call check-gcc-var,-funit-at-a-time))
273 OPTIMIZATION-3.4 += $(CFLAG_-funit-at-a-time)
274 $(eval $(call check-gcc-var,-fstrict-aliasing))
275 OPTIMIZATION += $(CFLAG_-fstrict-aliasing)
277 # CPU_CFLAGS-y contain options which are not warnings,
278 # not include or library paths, and not optimizations.
280 # Why -funsigned-char: I hunted a bug related to incorrect
281 # sign extension of 'char' type for 10 hours straight. Not fun.
282 CPU_CFLAGS-y := -funsigned-char -fno-builtin -fcommon
284 $(eval $(call check-gcc-var,-fno-asm))
285 CPU_CFLAGS-y += $(CFLAG_-fno-asm)
286 $(eval $(call check-gcc-var,-fmerge-all-constants))
287 CPU_CFLAGS-y += $(CFLAG_-fmerge-all-constants)
289 ifeq ($(UCLIBC_HAS_SOFT_FLOAT),y)
290 ifneq ($(TARGET_ARCH),bfin)
291 ifneq ($(TARGET_ARCH),lm32)
292 ifneq ($(TARGET_ARCH),nios2)
293 ifneq ($(TARGET_ARCH),nds32)
294 ifneq ($(TARGET_ARCH),sh)
295 ifneq ($(TARGET_ARCH),c6x)
296 ifneq ($(TARGET_ARCH),h8300)
297 ifneq ($(TARGET_ARCH),arc)
298 ifneq ($(TARGET_ARCH),aarch64)
299 CPU_CFLAGS-y += -msoft-float
300 endif
301 endif
302 endif
303 endif
304 endif
305 endif
306 endif
307 endif
308 endif
309 endif
311 ifeq ($(TARGET_ARCH),aarch64)
312 CPU_CFLAGS-y += -ftls-model=initial-exec
313 endif
315 $(eval $(call check-gcc-var,-std=gnu99))
316 CPU_CFLAGS-y += $(CFLAG_-std=gnu99)
318 CPU_CFLAGS-$(UCLIBC_FORMAT_SHARED_FLAT) += -mid-shared-library
319 CPU_CFLAGS-$(UCLIBC_FORMAT_FLAT_SEP_DATA) += -msep-data
321 CPU_LDFLAGS-$(ARCH_LITTLE_ENDIAN) += -Wl,-EL
322 CPU_LDFLAGS-$(ARCH_BIG_ENDIAN) += -Wl,-EB
324 PICFLAG-y := -fPIC
325 PICFLAG-$(UCLIBC_FORMAT_FDPIC_ELF) := -mfdpic
326 PICFLAG-$(UCLIBC_FORMAT_DSBT_ELF) := -mdsbt -fpic
327 PICFLAG := $(PICFLAG-y)
328 PIEFLAG_NAME:=-fPIE
330 $(eval $(call check-gcc-var,-fdata-sections))
331 $(eval $(call check-gcc-var,-ffunction-sections))
333 # Some nice CPU specific optimizations
334 ifeq ($(TARGET_ARCH),i386)
335 $(eval $(call check-gcc-var,-fomit-frame-pointer))
336 OPTIMIZATION += $(CFLAG_-fomit-frame-pointer)
338 ifeq ($(CONFIG_386)$(CONFIG_486)$(CONFIG_586),y)
339 # TODO: Change this to a gcc version check. This bug
340 # should be fixed with at least gcc-4.3.
341 # Non-SSE capable processor.
342 # NB: this may make SSE insns segfault!
343 # -O1 -march=pentium3, -Os -msse etc are known to be affected.
344 # See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13685
345 # -m32 is needed if host is 64-bit
346 OPTIMIZATION+=$(call check_gcc,-m32 -mpreferred-stack-boundary=2,)
347 else
348 $(eval $(call check-gcc-var,-mpreferred-stack-boundary=4))
349 OPTIMIZATION += $(CFLAG_-mpreferred-stack-boundary=4)
350 endif
352 # Choice of alignment (please document why!)
353 # -falign-labels: in-line labels
354 # (reachable by normal code flow, aligning will insert nops
355 # which will be executed - may even make things slower)
356 # -falign-jumps: reachable only by a jump
357 # Generic: no alignment at all (smallest code)
358 GCC_FALIGN=$(call check_gcc,-falign-functions=1 -falign-jumps=1 -falign-labels=1 -falign-loops=1,-malign-jumps=1 -malign-loops=1)
359 OPTIMIZATION+=$(GCC_FALIGN)
361 # Putting each function and data object into its own section
362 # allows for kbytes of less text if users link against static uclibc
363 # using ld --gc-sections.
364 # ld 2.18 can't do that (yet?) for shared libraries, so we itself
365 # do not use --gc-sections at shared lib link time.
366 # However, in combination with sections being sorted by alignment
367 # it does result in much reduced padding:
368 # text data bss dec hex
369 # 235319 1472 5992 242783 3b45f old.so
370 # 234104 1472 5980 241556 3af94 new.so
371 # Without -ffunction-sections, all functions will get aligned
372 # to 4 byte boundary by as/ld. This is arguably a bug in as.
373 # It specifies 4 byte align for .text even if not told to do so:
374 # Idx Name Size VMA LMA File off Algn
375 # 0 .text xxxxxxxx 00000000 00000000 xxxxxxxx 2**2 <===!
376 CPU_CFLAGS-y += $(CFLAG_-ffunction-sections) $(CFLAG_-fdata-sections)
377 CPU_LDFLAGS-y += $(CFLAG_-Wl--sort-common)
378 $(eval $(call check-ld-var,--sort-section=alignment))
379 CPU_LDFLAGS-y += $(CFLAG_-Wl--sort-section=alignment)
381 CPU_LDFLAGS-y+=-m32
382 CPU_CFLAGS-y+=-m32
383 endif
385 ifeq ($(TARGET_ARCH),sparc)
386 CPU_CFLAGS-$(CONFIG_SPARC_V7)+=-mcpu=v7
387 CPU_CFLAGS-$(CONFIG_SPARC_V8)+=-mcpu=v8
388 CPU_CFLAGS-$(CONFIG_SPARC_V9)+=-mcpu=v9
389 CPU_CFLAGS-$(CONFIG_SPARC_V9B)+=$(call check_gcc,-mcpu=v9b,-mcpu=ultrasparc)
390 endif
392 ifeq ($(TARGET_ARCH),arm)
393 CPU_CFLAGS-$(ARCH_LITTLE_ENDIAN)+=-mlittle-endian
394 CPU_CFLAGS-$(ARCH_BIG_ENDIAN)+=-mbig-endian
395 endif
397 ifeq ($(TARGET_ARCH),metag)
398 SYMBOL_PREFIX=_
399 CPU_CFLAGS-$(CONFIG_META_1_2)+=
400 CPU_CFLAGS-$(CONFIG_META_2_1)+=-Wa,-mcpu=metac21
401 endif
403 ifeq ($(TARGET_ARCH),mips)
404 OPTIMIZATION+=-mno-split-addresses
405 CPU_CFLAGS-$(CONFIG_MIPS_N64_ABI)+=-mabi=64
406 CPU_CFLAGS-$(CONFIG_MIPS_O32_ABI)+=-mabi=32
407 CPU_CFLAGS-$(CONFIG_MIPS_N32_ABI)+=-mabi=n32
408 CPU_CFLAGS-$(CONFIG_MIPS_NAN_LEGACY)+=$(call check_gcc,-mnan=legacy)
409 CPU_CFLAGS-$(CONFIG_MIPS_NAN_2008)+=$(call check_gcc,-mnan=2008)
410 CPU_LDFLAGS-y += $(CPU_CFLAGS)
411 endif
413 ifeq ($(TARGET_ARCH),nds32)
414 CPU_CFLAGS-$(CONFIG_NDS32_ISA)+=-march=nds32
415 CFLAGS-.os+= -DPIC
416 CFLAGS-.oS+= -DPIC
417 endif
419 ifeq ($(TARGET_ARCH),sh)
420 $(eval $(call check-gcc-var,-mprefergot))
421 OPTIMIZATION += $(CFLAG_-mprefergot)
422 CPU_CFLAGS-$(ARCH_LITTLE_ENDIAN)+=-ml
423 CPU_CFLAGS-$(ARCH_BIG_ENDIAN)+=-mb
424 CPU_CFLAGS-$(CONFIG_SH2)+=-m2
425 CPU_CFLAGS-$(CONFIG_SH3)+=-m3
426 ifeq ($(UCLIBC_HAS_FPU),y)
427 CPU_CFLAGS-$(CONFIG_SH2A)+=-m2a
428 CPU_CFLAGS-$(CONFIG_SH4)+=-m4
429 CPU_CFLAGS-$(CONFIG_SH4A)+=-m4a
430 else
431 CPU_CFLAGS-$(CONFIG_SH2A)+=-m2a-nofpu
432 CPU_CFLAGS-$(CONFIG_SH4)+=-m4-nofpu
433 CPU_CFLAGS-$(CONFIG_SH4A)+=-m4a-nofpu
434 endif
435 endif
437 ifeq ($(TARGET_ARCH),h8300)
438 CPU_CFLAGS-$(CONFIG_H8300H) += -mh -mint32
439 CPU_CFLAGS-$(CONFIG_H8S) += -ms -mint32
440 endif
442 ifeq ($(TARGET_ARCH),cris)
443 CPU_LDFLAGS-$(CONFIG_CRIS)+=-Wl,-mcrislinux
444 CPU_LDFLAGS-$(CONFIG_CRISV32)+=-Wl,-mcrislinux
445 CPU_CFLAGS-$(CONFIG_CRIS)+=-mlinux
446 PICFLAG:=-fpic
447 PIEFLAG_NAME:=-fpie
448 endif
450 ifeq ($(TARGET_ARCH),csky)
451 # In csky gas implement, we use $t and $d to detect .text or literal pool.
452 # So we couldn't strip them for objdump.
453 STRIP_FLAGS += -K "$$"t -K "$$"d
455 CPU_CFLAGS-$(ARCH_LITTLE_ENDIAN) += -mlittle-endian
456 CPU_CFLAGS-$(ARCH_BIG_ENDIAN) += -mbig-endian
457 endif
459 ifeq ($(TARGET_ARCH),kvx)
460 CPU_CFLAGS-$(CONFIG_KVX) += -march=kvx
461 endif
463 ifeq ($(TARGET_ARCH),m68k)
464 # -fPIC is only supported for 68020 and above. It is not supported
465 # for 68000, 68010, or Coldfire.
466 PICFLAG:=-fpic
467 PIEFLAG_NAME:=-fpie
468 endif
470 ifeq ($(TARGET_ARCH),powerpc)
471 # PowerPC can hold 8192 entries in its GOT with -fpic which is more than
472 # enough. Therefore use -fpic which will reduce code size and generates
473 # faster code.
474 PICFLAG:=-fpic
475 PIEFLAG_NAME:=-fpie
476 PPC_HAS_REL16:=$(shell printf "\t.text\n\taddis 11,30,_GLOBAL_OFFSET_TABLE_-.@ha\n" | $(CC) -c -x assembler -o /dev/null - 2> /dev/null && echo -n y || echo -n n)
477 PPC_HAS_SECUREPLT:=$(shell $(CC) --verbose 2>&1 | grep -- --enable-secureplt > /dev/null && echo -n y || echo -n n)
478 CPU_CFLAGS-$(PPC_HAS_SECUREPLT) += -DPPC_HAS_SECUREPLT
479 CPU_CFLAGS-$(PPC_HAS_REL16)+= -DHAVE_ASM_PPC_REL16
480 CPU_CFLAGS-$(CONFIG_E500) += "-D__NO_MATH_INLINES"
481 endif
483 ifeq ($(TARGET_ARCH),bfin)
484 SYMBOL_PREFIX=_
485 ifeq ($(UCLIBC_FORMAT_FDPIC_ELF),y)
486 CPU_CFLAGS-y:=-mfdpic
487 CPU_LDFLAGS-y += -Wl,-melf32bfinfd
488 PICFLAG:=-fpic
489 PIEFLAG_NAME:=-fpie
490 endif
491 ifeq ($(UCLIBC_FORMAT_SHARED_FLAT),y)
492 PICFLAG := -mleaf-id-shared-library
493 endif
494 endif
496 ifeq ($(TARGET_ARCH),frv)
497 # Using -pie causes the program to have an interpreter, which is
498 # forbidden, so we must make do with -shared. Unfortunately,
499 # -shared by itself would get us global function descriptors
500 # and calls through PLTs, dynamic resolution of symbols, etc,
501 # which would break as well, but -Bsymbolic comes to the rescue.
502 CPU_LDFLAGS-y += -Wl,-melf32frvfd -Wl,-Bsymbolic
503 endif
505 ifeq ($(strip $(TARGET_ARCH)),avr32)
506 CPU_CFLAGS-$(CONFIG_AVR32_AP7) += -march=ap
507 CPU_CFLAGS-$(CONFIG_LINKRELAX) += -mrelax
508 CPU_LDFLAGS-$(CONFIG_LINKRELAX) += --relax
509 endif
511 ifeq ($(TARGET_ARCH),c6x)
512 PIEFLAG:=
513 CPU_CFLAGS-$(CONFIG_TMS320C64X) += -march=c64x
514 CPU_CFLAGS-$(CONFIG_TMS320C64XPLUS) += -march=c64x+
515 CPU_CFLAGS-$(ARCH_LITTLE_ENDIAN)+=-mlittle-endian
516 CPU_CFLAGS-$(ARCH_BIG_ENDIAN)+=-mbig-endian
517 CPU_LDFLAGS-y += $(CPU_CFLAGS)
518 endif
520 $(eval $(call check-gcc-var,$(PIEFLAG_NAME)))
521 PIEFLAG := $(CFLAG_$(PIEFLAG_NAME))
522 ifeq ($(PIEFLAG),)
523 PIEFLAG := $(PICFLAG)
524 endif
525 # We need to keep track of both the CC PIE flag (above) as
526 # well as the LD PIE flag (below) because we can't rely on
527 # gcc passing -pie if we used -fPIE. We need to directly use -pie
528 # instead of -Wl,-pie as gcc picks up the wrong startfile/endfile
529 $(eval $(call cache-output-var,LDPIEFLAG,$(CC) -Wl$(comma)--help 2>/dev/null | grep -q -- -pie && echo "-pie"))
531 # Check for --as-needed support in linker
532 ifndef LD_FLAG_ASNEEDED
533 _LD_FLAG_ASNEEDED:=$(shell $(CC) -Wl,--help 2>/dev/null | grep -- --as-needed)
534 ifneq ($(_LD_FLAG_ASNEEDED),)
535 export LD_FLAG_ASNEEDED:=--as-needed
536 endif
537 endif
538 ifndef LD_FLAG_NO_ASNEEDED
539 ifdef LD_FLAG_ASNEEDED
540 export LD_FLAG_NO_ASNEEDED:=--no-as-needed
541 endif
542 endif
543 ifndef CC_FLAG_ASNEEDED
544 ifdef LD_FLAG_ASNEEDED
545 export CC_FLAG_ASNEEDED:=-Wl,$(LD_FLAG_ASNEEDED)
546 endif
547 endif
548 ifndef CC_FLAG_NO_ASNEEDED
549 ifdef LD_FLAG_NO_ASNEEDED
550 export CC_FLAG_NO_ASNEEDED:=-Wl,$(LD_FLAG_NO_ASNEEDED)
551 endif
552 endif
553 link.asneeded = $(if $(CC_FLAG_ASNEEDED),$(if $(CC_FLAG_NO_ASNEEDED),$(CC_FLAG_ASNEEDED) $(1) $(CC_FLAG_NO_ASNEEDED)))
555 # Check for AS_NEEDED support in linker script (binutils>=2.16.1 has it)
556 ifndef ASNEEDED
557 export ASNEEDED:=$(shell $(CC) -Wl,--help 2>/dev/null | grep -q -- --as-needed && echo "AS_NEEDED ( $(UCLIBC_LDSO) )" || echo "$(UCLIBC_LDSO)")
558 endif
560 # Add a bunch of extra pedantic annoyingly strict checks
561 WARNING_FLAGS = -Wstrict-prototypes -Wstrict-aliasing -Wno-nonnull-compare
562 ifeq ($(EXTRA_WARNINGS),y)
563 WARNING_FLAGS += \
564 -Wnonnull-compare \
565 -Wnodeclaration-after-statement \
566 -Wformat=2 \
567 -Wmissing-noreturn \
568 -Wmissing-format-attribute \
569 -Wmissing-prototypes \
570 -Wmissing-declarations \
571 -Wnested-externs \
572 -Wnonnull \
573 -Wold-style-declaration \
574 -Wold-style-definition \
575 -Wshadow \
576 -Wundef
577 endif
578 WARNING_FLAGS += $(WARNING_FLAGS-gcc-$(GCC_MAJOR_VER))
579 $(foreach w,$(WARNING_FLAGS),$(eval $(call check-gcc-var,$(w))))
580 XWARNINGS = $(call qstrip,$(WARNINGS)) $(foreach w,$(WARNING_FLAGS),$(CFLAG_$(w)))
582 CPU_CFLAGS=$(call qstrip,$(CPU_CFLAGS-y))
584 # Save the tested flag in a single variable and force it to be
585 # evaluated just once. Then use that computed value.
586 $(eval $(call check-gcc-var,-fno-stack-protector))
587 SSP_DISABLE_FLAGS ?= $(CFLAG_-fno-stack-protector)
588 ifeq ($(UCLIBC_BUILD_SSP),y)
589 $(eval $(call check-gcc-var,-fno-stack-protector-all))
590 $(eval $(call check-gcc-var,-fstack-protector))
591 $(eval $(call check-gcc-var,-fstack-protector-all))
592 SSP_CFLAGS := $(CFLAG_-fno-stack-protector-all)
593 SSP_CFLAGS += $(CFLAG_-fstack-protector)
594 SSP_ALL_CFLAGS ?= $(CFLAG_-fstack-protector-all)
595 else
596 SSP_CFLAGS := $(SSP_DISABLE_FLAGS)
597 endif
599 # Collect all CFLAGS components
600 CFLAGS := $(XWARNINGS) $(CPU_CFLAGS) $(SSP_CFLAGS) \
601 -nostdinc -I$(top_builddir)include \
602 -I$(top_srcdir)include -include libc-symbols.h \
603 -I$(top_srcdir)libc/sysdeps/linux/$(TARGET_ARCH) \
604 -I$(top_srcdir)libc/sysdeps/linux \
605 -I$(top_srcdir)ldso/ldso/$(TARGET_ARCH) \
606 -I$(top_srcdir)ldso/include -I.
608 # We need this to be checked within libc-symbols.h
609 ifneq ($(HAVE_SHARED),y)
610 CFLAGS += -DSTATIC
611 endif
613 $(eval $(call check-ld-var,--warn-once))
614 $(eval $(call check-ld-var,--sort-common))
615 $(eval $(call check-ld-var,--discard-all))
616 LDFLAGS_NOSTRIP:=$(LDFLAG-fuse-ld) $(CPU_LDFLAGS-y) -shared \
617 -Wl,--warn-common $(CFLAG_-Wl--warn-once) -Wl,-z,combreloc
619 ifeq ($(UCLIBC_BUILD_RELRO),y)
620 LDFLAGS_NOSTRIP+=-Wl,-z,relro
621 endif
623 ifeq ($(UCLIBC_BUILD_NOW),y)
624 LDFLAGS_NOSTRIP+=-Wl,-z,now
625 endif
627 ifeq ($(LDSO_GNU_HASH_SUPPORT),y)
628 # Be sure that binutils support it
629 $(eval $(call check-ld-var,--hash-style=gnu))
630 ifeq ($(LDFLAG_--hash-style=gnu),)
631 ifneq ($(filter-out $(clean_targets) CLEAN_% install_headers headers-y,$(MAKECMDGOALS)),)
632 $(error Your binutils do not support --hash-style option, while you want to use it)
633 endif
634 else
635 LDFLAGS_NOSTRIP += $(CFLAG_-Wl--hash-style=gnu)
636 endif
637 endif
639 ifeq ($(DODEBUG),y)
640 CFLAGS += -O0 -g3 -DDEBUG
641 else
642 CFLAGS += $(OPTIMIZATION)
643 CFLAGS += $(OPTIMIZATION-$(GCC_MAJOR_VER))
644 CFLAGS += $(OPTIMIZATION-$(GCC_MAJOR_VER).$(GCC_MINOR_VER))
645 $(eval $(call check-ld-var,-O2))
646 LDFLAGS_NOSTRIP += $(CFLAG_-Wl-O2)
647 endif
649 LDFLAGS:=$(LDFLAGS_NOSTRIP) -Wl,-z,defs
651 ifeq ($(DOSTRIP),y)
652 LDFLAGS += -Wl,-s
653 else
654 STRIPTOOL := true -Stripping_disabled
655 endif
657 ifneq ($(strip $(UCLIBC_EXTRA_CFLAGS)),"")
658 CFLAGS += $(call qstrip,$(UCLIBC_EXTRA_CFLAGS))
659 endif
660 ifneq ($(strip $(UCLIBC_EXTRA_LDFLAGS)),"")
661 LDFLAGS += $(call qstrip,$(UCLIBC_EXTRA_LDFLAGS))
662 endif
664 ifeq ($(UCLIBC_HAS_STDIO_FUTEXES),y)
665 CFLAGS += -D__USE_STDIO_FUTEXES__
666 endif
668 ifeq ($(UCLIBC_HAS_THREADS),y)
669 ifeq ($(UCLIBC_HAS_THREADS_NATIVE),y)
670 PTNAME := nptl
671 CFLAGS += -DHAVE_FORCED_UNWIND -D_LIBC_REENTRANT
672 else
673 ifeq ($(UCLIBC_HAS_LINUXTHREADS),y)
674 PTNAME := linuxthreads
675 endif
676 endif
677 PTDIR := libpthread/$(PTNAME)
678 # set up system dependencies include dirs (NOTE: order matters!)
679 ifeq ($(UCLIBC_HAS_THREADS_NATIVE),y)
680 PTINC:= -I$(top_builddir)$(PTDIR) \
681 -I$(top_srcdir)$(PTDIR) \
682 $(if $(TARGET_SUBARCH),-I$(top_srcdir)$(PTDIR)/sysdeps/unix/sysv/linux/$(TARGET_ARCH)/$(TARGET_SUBARCH)) \
683 -I$(top_srcdir)$(PTDIR)/sysdeps/unix/sysv/linux/$(TARGET_ARCH) \
684 -I$(top_builddir)$(PTDIR)/sysdeps/$(TARGET_ARCH) \
685 -I$(top_srcdir)$(PTDIR)/sysdeps/$(TARGET_ARCH) \
686 -I$(top_builddir)$(PTDIR)/sysdeps/unix/sysv/linux \
687 -I$(top_srcdir)$(PTDIR)/sysdeps/unix/sysv/linux \
688 -I$(top_srcdir)$(PTDIR)/sysdeps/pthread \
689 -I$(top_srcdir)$(PTDIR)/sysdeps/pthread/bits \
690 -I$(top_srcdir)$(PTDIR)/sysdeps/generic
692 # Test for TLS if NPTL support was selected.
694 GCC_HAS_TLS=$(shell \
695 echo "extern __thread int foo;" | $(CC) -o /dev/null -S -xc - 2>&1)
696 ifneq ($(GCC_HAS_TLS),)
697 gcc_tls_test_fail:
698 @echo "####";
699 @echo "#### Your compiler does not support TLS and you are trying to build uClibc-ng";
700 @echo "#### with NPTL support. Upgrade your binutils and gcc to versions which";
701 @echo "#### support TLS for your architecture. Do not contact uClibc-ng maintainers";
702 @echo "#### about this problem.";
703 @echo "####";
704 @echo "#### Exiting...";
705 @echo "####";
706 @exit 1;
707 endif
708 else
709 PTINC := \
710 -I$(top_srcdir)$(PTDIR)/sysdeps/unix/sysv/linux/$(TARGET_ARCH) \
711 -I$(top_srcdir)$(PTDIR)/sysdeps/$(TARGET_ARCH) \
712 -I$(top_srcdir)$(PTDIR)/sysdeps/unix/sysv/linux \
713 -I$(top_srcdir)$(PTDIR)/sysdeps/pthread \
714 -I$(top_srcdir)$(PTDIR) \
715 -I$(top_srcdir)libpthread
716 endif
717 CFLAGS+=$(PTINC)
718 else
719 PTNAME :=
720 PTINC :=
721 endif
722 CFLAGS += -I$(top_srcdir)libc/sysdeps/linux/common
724 #CFLAGS += -iwithprefix include-fixed -iwithprefix include
725 $(eval $(call cache-output-var,CC_IPREFIX,$(CC) -print-file-name=include))
726 CC_INC := -isystem $(dir $(CC_IPREFIX))include-fixed -isystem $(CC_IPREFIX)
727 CFLAGS += $(CC_INC)
729 CFLAGS += -I$(KERNEL_HEADERS)
731 ifneq ($(DOASSERTS),y)
732 CFLAGS+=-DNDEBUG
733 endif
735 ifeq ($(SYMBOL_PREFIX),_)
736 CFLAGS+=-D__UCLIBC_UNDERSCORES__
737 endif
739 # Keep the check_as from being needlessly executed
740 ifeq ($(UCLIBC_BUILD_NOEXECSTACK),y)
741 $(eval $(call check-as-var,--noexecstack))
742 endif
743 ASFLAGS += $(ASFLAG_--noexecstack)
745 LIBGCC_CFLAGS ?= $(CFLAGS) $(CPU_CFLAGS-y)
746 $(eval $(call cache-output-var,LIBGCC_A,$(CC) $(LIBGCC_CFLAGS) -print-libgcc-file-name))
747 $(eval $(call cache-output-var,LIBGCC_EH,$(CC) $(LIBGCC_CFLAGS) -print-file-name=libgcc_eh.a))
748 # with -O0 we (e.g. lockf) might end up with references to
749 # _Unwind_Resume, so pull in gcc_eh in this case..
750 # with a --disable-shared toolchain, libgcc_eh.a and libgcc.a are combined
751 # in libgcc.a, so check if the printed file exist, before adding to the commandline
752 LIBGCC_EH_FILE := $(shell if [ -f $(LIBGCC_EH) ]; then echo $(LIBGCC_EH); fi)
753 LIBGCC_DIR := $(dir $(LIBGCC_A))
754 LIBGCC := $(LIBGCC_A) $(if $(DODEBUG),$(LIBGCC_EH_FILE))
756 # moved from libpthread/linuxthreads
757 ifeq ($(UCLIBC_CTOR_DTOR),y)
758 SHARED_START_FILES:=$(top_builddir)lib/crti.o $(LIBGCC_DIR)crtbeginS.o
759 SHARED_END_FILES:=$(LIBGCC_DIR)crtendS.o $(top_builddir)lib/crtn.o
760 endif
762 LOCAL_INSTALL_PATH := $(if $(O),$(O)/)install_dir
764 PTHREAD_GENERATE_MANGLE ?= -n "s/^.*@@@name@@@\([^@]*\)@@@value@@@[^0-9Xxa-fA-F-]*\([0-9Xxa-fA-F-][0-9Xxa-fA-F-]*\).*@@@end@@@.*\$$/\#define \1 \2/p"