xref: /linux/scripts/Kconfig.include (revision bb29b957eaaa15be33243550d9ee576c2d601669)
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-probe,<flag>)
27# The command $(cc-option) and $(cc-option-str) test <flag> with. Not meant
28# to be called directly; use one of those two instead.
29cc-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
30
31# $(cc-option,<flag>)
32# Return y if the compiler supports <flag>, n otherwise
33cc-option = $(success,$(cc-probe,$(1)))
34
35# $(cc-option-str,<flag>[,<fallback>])
36# Return <flag> if the compiler supports it, <fallback> otherwise.
37# <fallback> defaults to an empty string and is returned untested.
38cc-option-str = $(if-success,$(cc-probe,$(1)),$(1),$(2))
39
40# $(ld-probe,<flag>)
41# The command $(ld-option) and $(ld-option-str) test <flag> with. Not meant
42# to be called directly; use one of those two instead.
43ld-probe = $(LD) -v $(1)
44
45# $(ld-option,<flag>)
46# Return y if the linker supports <flag>, n otherwise
47ld-option = $(success,$(ld-probe,$(1)))
48
49# $(ld-option-str,<flag>[,<fallback>])
50# Return <flag> if the linker supports it, <fallback> otherwise.
51# <fallback> defaults to an empty string and is returned untested.
52ld-option-str = $(if-success,$(ld-probe,$(1)),$(1),$(2))
53
54# $(as-instr,<instr>)
55# Return y if the assembler supports <instr>, n otherwise
56as-instr = $(success,printf "%b\n" "$(1)" | $(CC) $(CLANG_FLAGS) $(2) -Wa$(comma)--fatal-warnings -c -x assembler-with-cpp -o /dev/null -)
57as-instr64 = $(as-instr,$(1),$(m64-flag))
58
59# check if $(CC) and $(LD) exist
60$(error-if,$(failure,command -v $(CC)),C compiler '$(CC)' not found)
61$(error-if,$(failure,command -v $(LD)),linker '$(LD)' not found)
62
63# Get the C compiler name, version, and error out if it is not supported.
64cc-info := $(shell,$(srctree)/scripts/cc-version.sh $(CC))
65$(error-if,$(success,test -z "$(cc-info)"),Sorry$(comma) this C compiler is not supported.)
66cc-name := $(shell,set -- $(cc-info) && echo $1)
67cc-version := $(shell,set -- $(cc-info) && echo $2)
68
69# Get the assembler name, version, and error out if it is not supported.
70as-info := $(shell,$(srctree)/scripts/as-version.sh $(CC) $(CLANG_FLAGS))
71$(error-if,$(success,test -z "$(as-info)"),Sorry$(comma) this assembler is not supported.)
72as-name := $(shell,set -- $(as-info) && echo $1)
73as-version := $(shell,set -- $(as-info) && echo $2)
74
75# Get the linker name, version, and error out if it is not supported.
76ld-info := $(shell,$(srctree)/scripts/ld-version.sh $(LD))
77$(error-if,$(success,test -z "$(ld-info)"),Sorry$(comma) this linker is not supported.)
78ld-name := $(shell,set -- $(ld-info) && echo $1)
79ld-version := $(shell,set -- $(ld-info) && echo $2)
80
81# machine bit flags
82#  $(m32-flag): -m32 if the compiler supports it, or an empty string otherwise.
83#  $(m64-flag): -m64 if the compiler supports it, or an empty string otherwise.
84m32-flag := $(cc-option-str,-m32)
85m64-flag := $(cc-option-str,-m64)
86
87# Test whether the compiler can link userspace applications
88cc_can_link_user = $(success,$(srctree)/scripts/cc-can-link.sh $(CC) $(CLANG_FLAGS) $(USERCFLAGS) $(USERLDFLAGS) $(1))
89
90rustc-version := $(shell,$(srctree)/scripts/rustc-version.sh $(RUSTC))
91rustc-llvm-version := $(shell,$(srctree)/scripts/rustc-llvm-version.sh $(RUSTC))
92
93# $(rustc-option,<flag>)
94# Return y if the Rust compiler supports <flag>, n otherwise
95# If you are testing for unstable features, consider testing RUSTC_VERSION
96# instead, as features may have different completeness while available.
97rustc-option = $(success,trap "rm -rf .tmp_$$" EXIT; mkdir .tmp_$$; $(RUSTC) $(1) --crate-type=rlib /dev/null --out-dir=.tmp_$$ -o .tmp_$$/tmp.rlib)
98