1ec8f24b7SThomas Gleixner# SPDX-License-Identifier: GPL-2.0-only 2e1cfdc0eSMasahiro Yamada# Kconfig helper macros 3e1cfdc0eSMasahiro Yamada 4e1cfdc0eSMasahiro Yamada# Convenient variables 5e1cfdc0eSMasahiro Yamadacomma := , 6e1cfdc0eSMasahiro Yamadaquote := " 7e1cfdc0eSMasahiro Yamadasquote := ' 8e1cfdc0eSMasahiro Yamadaempty := 9e1cfdc0eSMasahiro Yamadaspace := $(empty) $(empty) 10e1cfdc0eSMasahiro Yamadadollar := $ 11e1cfdc0eSMasahiro Yamadaright_paren := ) 12e1cfdc0eSMasahiro Yamadaleft_paren := ( 13e1cfdc0eSMasahiro Yamada 14e1cfdc0eSMasahiro Yamada# $(if-success,<command>,<then>,<else>) 15e1cfdc0eSMasahiro Yamada# Return <then> if <command> exits with 0, <else> otherwise. 16e1cfdc0eSMasahiro Yamadaif-success = $(shell,{ $(1); } >/dev/null 2>&1 && echo "$(2)" || echo "$(3)") 17e1cfdc0eSMasahiro Yamada 18e1cfdc0eSMasahiro Yamada# $(success,<command>) 19e1cfdc0eSMasahiro Yamada# Return y if <command> exits with 0, n otherwise 20e1cfdc0eSMasahiro Yamadasuccess = $(if-success,$(1),y,n) 21e1cfdc0eSMasahiro Yamada 22902a6898SMasahiro Yamada# $(failure,<command>) 23902a6898SMasahiro Yamada# Return n if <command> exits with 0, y otherwise 24902a6898SMasahiro Yamadafailure = $(if-success,$(1),n,y) 25902a6898SMasahiro Yamada 26889a6212SKees Cook# $(cc-probe,<flag>) 27889a6212SKees Cook# The command $(cc-option) and $(cc-option-str) test <flag> with. Not meant 28889a6212SKees Cook# to be called directly; use one of those two instead. 29*7202c087SNathan Chancellorcc-probe = trap "rm -rf .tmp_$$" EXIT; mkdir .tmp_$$; $(CC) -Werror $(CLANG_FLAGS) $(shell,echo $(1) | sed "s;^-Wno-;-W;") -c -x c /dev/null -o .tmp_$$/tmp.o 30889a6212SKees Cook 31e1cfdc0eSMasahiro Yamada# $(cc-option,<flag>) 32e1cfdc0eSMasahiro Yamada# Return y if the compiler supports <flag>, n otherwise 33889a6212SKees Cookcc-option = $(success,$(cc-probe,$(1))) 34889a6212SKees Cook 35889a6212SKees Cook# $(cc-option-str,<flag>[,<fallback>]) 36889a6212SKees Cook# Return <flag> if the compiler supports it, <fallback> otherwise. 37889a6212SKees Cook# <fallback> defaults to an empty string and is returned untested. 38889a6212SKees Cookcc-option-str = $(if-success,$(cc-probe,$(1)),$(1),$(2)) 39e1cfdc0eSMasahiro Yamada 40cfcc4650SKees Cook# $(ld-probe,<flag>) 41cfcc4650SKees Cook# The command $(ld-option) and $(ld-option-str) test <flag> with. Not meant 42cfcc4650SKees Cook# to be called directly; use one of those two instead. 43cfcc4650SKees Cookld-probe = $(LD) -v $(1) 44cfcc4650SKees Cook 45e1cfdc0eSMasahiro Yamada# $(ld-option,<flag>) 46e1cfdc0eSMasahiro Yamada# Return y if the linker supports <flag>, n otherwise 47cfcc4650SKees Cookld-option = $(success,$(ld-probe,$(1))) 48cfcc4650SKees Cook 49cfcc4650SKees Cook# $(ld-option-str,<flag>[,<fallback>]) 50cfcc4650SKees Cook# Return <flag> if the linker supports it, <fallback> otherwise. 51cfcc4650SKees Cook# <fallback> defaults to an empty string and is returned untested. 52cfcc4650SKees Cookld-option-str = $(if-success,$(ld-probe,$(1)),$(1),$(2)) 5359f53855SMasahiro Yamada 5442d519e3SCatalin Marinas# $(as-instr,<instr>) 5542d519e3SCatalin Marinas# Return y if the assembler supports <instr>, n otherwise 5646916980SMasahiro Yamadaas-instr = $(success,printf "%b\n" "$(1)" | $(CC) $(CLANG_FLAGS) $(2) -Wa$(comma)--fatal-warnings -c -x assembler-with-cpp -o /dev/null -) 5746916980SMasahiro Yamadaas-instr64 = $(as-instr,$(1),$(m64-flag)) 5842d519e3SCatalin Marinas 59902a6898SMasahiro Yamada# check if $(CC) and $(LD) exist 602f7ab126SMiguel Ojeda$(error-if,$(failure,command -v $(CC)),C compiler '$(CC)' not found) 61902a6898SMasahiro Yamada$(error-if,$(failure,command -v $(LD)),linker '$(LD)' not found) 62902a6898SMasahiro Yamada 632f7ab126SMiguel Ojeda# Get the C compiler name, version, and error out if it is not supported. 64aec6c60aSMasahiro Yamadacc-info := $(shell,$(srctree)/scripts/cc-version.sh $(CC)) 652f7ab126SMiguel Ojeda$(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this C compiler is not supported.) 66aec6c60aSMasahiro Yamadacc-name := $(shell,set -- $(cc-info) && echo $1) 67aec6c60aSMasahiro Yamadacc-version := $(shell,set -- $(cc-info) && echo $2) 68aec6c60aSMasahiro Yamada 69ba64beb1SMasahiro Yamada# Get the assembler name, version, and error out if it is not supported. 70ba64beb1SMasahiro Yamadaas-info := $(shell,$(srctree)/scripts/as-version.sh $(CC) $(CLANG_FLAGS)) 71ba64beb1SMasahiro Yamada$(error-if,$(success,test -z "$(as-info)"),Sorry$(comma) this assembler is not supported.) 72ba64beb1SMasahiro Yamadaas-name := $(shell,set -- $(as-info) && echo $1) 73ba64beb1SMasahiro Yamadaas-version := $(shell,set -- $(as-info) && echo $2) 74ba64beb1SMasahiro Yamada 7502aff859SMasahiro Yamada# Get the linker name, version, and error out if it is not supported. 7602aff859SMasahiro Yamadald-info := $(shell,$(srctree)/scripts/ld-version.sh $(LD)) 7702aff859SMasahiro Yamada$(error-if,$(success,test -z "$(ld-info)"),Sorry$(comma) this linker is not supported.) 7802aff859SMasahiro Yamadald-name := $(shell,set -- $(ld-info) && echo $1) 7902aff859SMasahiro Yamadald-version := $(shell,set -- $(ld-info) && echo $2) 8075959d44SThomas Gleixner 818cc4fd73SMasahiro Yamada# machine bit flags 828cc4fd73SMasahiro Yamada# $(m32-flag): -m32 if the compiler supports it, or an empty string otherwise. 838cc4fd73SMasahiro Yamada# $(m64-flag): -m64 if the compiler supports it, or an empty string otherwise. 84889a6212SKees Cookm32-flag := $(cc-option-str,-m32) 85889a6212SKees Cookm64-flag := $(cc-option-str,-m64) 86c4229743SMatthew Maurer 8780623f2cSThomas Weißschuh# Test whether the compiler can link userspace applications 8880623f2cSThomas Weißschuhcc_can_link_user = $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(1)) 8980623f2cSThomas Weißschuh 90af0121c2SGary Guorustc-version := $(shell,$(srctree)/scripts/rustc-version.sh $(RUSTC)) 91af0121c2SGary Guorustc-llvm-version := $(shell,$(srctree)/scripts/rustc-llvm-version.sh $(RUSTC)) 92af0121c2SGary Guo 93c4229743SMatthew Maurer# $(rustc-option,<flag>) 94c4229743SMatthew Maurer# Return y if the Rust compiler supports <flag>, n otherwise 95c4229743SMatthew Maurer# If you are testing for unstable features, consider testing RUSTC_VERSION 96c4229743SMatthew Maurer# instead, as features may have different completeness while available. 97c4229743SMatthew Maurerrustc-option = $(success,trap "rm -rf .tmp_$$" EXIT; mkdir .tmp_$$; $(RUSTC) $(1) --crate-type=rlib /dev/null --out-dir=.tmp_$$ -o .tmp_$$/tmp.rlib) 98