57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 1) # SPDX-License-Identifier: GPL-2.0-only
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 2)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 3) # cc-cross-prefix
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 4) # Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 5) # Return first <prefix> where a <prefix>gcc is found in PATH.
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 6) # If no gcc found in PATH with listed prefixes return nothing
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 7) #
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 8) # Note: '2>/dev/null' is here to force Make to invoke a shell. Otherwise, it
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 9) # would try to directly execute the shell builtin 'command'. This workaround
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 10) # should be kept for a long time since this issue was fixed only after the
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 11) # GNU Make 4.2.1 release.
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 12) cc-cross-prefix = $(firstword $(foreach c, $(1), \
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 13) $(if $(shell command -v -- $(c)gcc 2>/dev/null), $(c))))
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 14)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 15) # output directory for tests below
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 16) TMPOUT = $(if $(KBUILD_EXTMOD),$(firstword $(KBUILD_EXTMOD))/).tmp_$$$$
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 17)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 18) # try-run
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 19) # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 20) # Exit code chooses option. "$$TMP" serves as a temporary file and is
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 21) # automatically cleaned up.
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 22) try-run = $(shell set -e; \
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 23) TMP=$(TMPOUT)/tmp; \
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 24) mkdir -p $(TMPOUT); \
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 25) trap "rm -rf $(TMPOUT)" EXIT; \
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 26) if ($(1)) >/dev/null 2>&1; \
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 27) then echo "$(2)"; \
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 28) else echo "$(3)"; \
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 29) fi)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 30)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 31) # as-option
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 32) # Usage: cflags-y += $(call as-option,-Wa$(comma)-isa=foo,)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 33)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 34) as-option = $(call try-run,\
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 35) $(CC) $(KBUILD_CFLAGS) $(1) -c -x assembler /dev/null -o "$$TMP",$(1),$(2))
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 36)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 37) # as-instr
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 38) # Usage: cflags-y += $(call as-instr,instr,option1,option2)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 39)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 40) as-instr = $(call try-run,\
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 41) printf "%b\n" "$(1)" | $(CC) $(KBUILD_AFLAGS) -c -x assembler -o "$$TMP" -,$(2),$(3))
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 42)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 43) # __cc-option
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 44) # Usage: MY_CFLAGS += $(call __cc-option,$(CC),$(MY_CFLAGS),-march=winchip-c6,-march=i586)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 45) __cc-option = $(call try-run,\
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 46) $(1) -Werror $(2) $(3) -c -x c /dev/null -o "$$TMP",$(3),$(4))
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 47)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 48) # cc-option
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 49) # Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 50)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 51) cc-option = $(call __cc-option, $(CC),\
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 52) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS),$(1),$(2))
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 53)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 54) # cc-option-yn
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 55) # Usage: flag := $(call cc-option-yn,-march=winchip-c6)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 56) cc-option-yn = $(call try-run,\
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 57) $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) $(1) -c -x c /dev/null -o "$$TMP",y,n)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 58)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 59) # cc-disable-warning
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 60) # Usage: cflags-y += $(call cc-disable-warning,unused-but-set-variable)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 61) cc-disable-warning = $(call try-run,\
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 62) $(CC) -Werror $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1)))
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 63)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 64) # cc-ifversion
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 65) # Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 66) cc-ifversion = $(shell [ $(CONFIG_GCC_VERSION)0 $(1) $(2)000 ] && echo $(3) || echo $(4))
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 67)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 68) # ld-option
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 69) # Usage: KBUILD_LDFLAGS += $(call ld-option, -X, -Y)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 70) ld-option = $(call try-run, $(LD) $(KBUILD_LDFLAGS) $(1) -v,$(1),$(2),$(3))
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 71)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 72) # ld-ifversion
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 73) # Usage: $(call ld-ifversion, -ge, 22252, y)
57fd251c78964 (Masahiro Yamada 2021-02-28 15:10:27 +0900 74) ld-ifversion = $(shell [ $(CONFIG_LD_VERSION)0 $(1) $(2)0 ] && echo $(3) || echo $(4))