Makefile.compiler 2.7 KB

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