1# SPDX-License-Identifier: GPL-2.0-only
2# Kconfig helper macros
3
4# Convenient variables
5comma       := ,
6quote       := "
7squote      := '
8empty       :=
9space       := $(empty) $(empty)
10dollar      := $
11right_paren := )
12left_paren  := (
13
14# $(if-success,<command>,<then>,<else>)
15# Return <then> if <command> exits with 0, <else> otherwise.
16if-success = $(shell,{ $(1); } >/dev/null 2>&1 && echo "$(2)" || echo "$(3)")
17
18# $(success,<command>)
19# Return y if <command> exits with 0, n otherwise
20success = $(if-success,$(1),y,n)
21
22# $(failure,<command>)
23# Return n if <command> exits with 0, y otherwise
24failure = $(if-success,$(1),n,y)
25
26# $(cc-option,<flag>)
27# Return y if the compiler supports <flag>, n otherwise
28cc-option = $(success,trap "rm -rf .tmp_$$" EXIT; mkdir .tmp_$$; $(CC) -Werror $(CLANG_FLAGS) $(1) -c -x c /dev/null -o .tmp_$$/tmp.o)
29
30# $(ld-option,<flag>)
31# Return y if the linker supports <flag>, n otherwise
32ld-option = $(success,$(LD) -v $(1))
33
34# $(as-instr,<instr>)
35# Return y if the assembler supports <instr>, n otherwise
36as-instr = $(success,printf "%b\n" "$(1)" | $(CC) $(CLANG_FLAGS) $(2) -Wa$(comma)--fatal-warnings -c -x assembler-with-cpp -o /dev/null -)
37as-instr64 = $(as-instr,$(1),$(m64-flag))
38
39# check if $(CC) and $(LD) exist
40$(error-if,$(failure,command -v $(CC)),C compiler '$(CC)' not found)
41$(error-if,$(failure,command -v $(LD)),linker '$(LD)' not found)
42
43# Get the C compiler name, version, and error out if it is not supported.
44cc-info := $(shell,$(srctree)/scripts/cc-version.sh $(CC))
45$(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this C compiler is not supported.)
46cc-name := $(shell,set -- $(cc-info) && echo $1)
47cc-version := $(shell,set -- $(cc-info) && echo $2)
48
49# Get the assembler name, version, and error out if it is not supported.
50as-info := $(shell,$(srctree)/scripts/as-version.sh $(CC) $(CLANG_FLAGS))
51$(error-if,$(success,test -z "$(as-info)"),Sorry$(comma) this assembler is not supported.)
52as-name := $(shell,set -- $(as-info) && echo $1)
53as-version := $(shell,set -- $(as-info) && echo $2)
54
55# Get the linker name, version, and error out if it is not supported.
56ld-info := $(shell,$(srctree)/scripts/ld-version.sh $(LD))
57$(error-if,$(success,test -z "$(ld-info)"),Sorry$(comma) this linker is not supported.)
58ld-name := $(shell,set -- $(ld-info) && echo $1)
59ld-version := $(shell,set -- $(ld-info) && echo $2)
60
61# machine bit flags
62#  $(m32-flag): -m32 if the compiler supports it, or an empty string otherwise.
63#  $(m64-flag): -m64 if the compiler supports it, or an empty string otherwise.
64cc-option-bit = $(if-success,$(CC) -Werror $(1) -E -x c /dev/null -o /dev/null,$(1))
65m32-flag := $(cc-option-bit,-m32)
66m64-flag := $(cc-option-bit,-m64)
67
68rustc-version := $(shell,$(srctree)/scripts/rustc-version.sh $(RUSTC))
69rustc-llvm-version := $(shell,$(srctree)/scripts/rustc-llvm-version.sh $(RUSTC))
70
71# $(rustc-option,<flag>)
72# Return y if the Rust compiler supports <flag>, n otherwise
73# Calls to this should be guarded so that they are not evaluated if
74# CONFIG_RUST_IS_AVAILABLE is not set.
75# If you are testing for unstable features, consider testing RUSTC_VERSION
76# instead, as features may have different completeness while available.
77rustc-option = $(success,trap "rm -rf .tmp_$$" EXIT; mkdir .tmp_$$; $(RUSTC) $(1) --crate-type=rlib /dev/null --out-dir=.tmp_$$ -o .tmp_$$/tmp.rlib)
78