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