xref: /linux/tools/build/Build.include (revision 7685b334d1e4927cc73b62c65293ba65748d9c52)
1c819e2cfSJiri Olsa###
2c819e2cfSJiri Olsa# build: Generic definitions
3c819e2cfSJiri Olsa#
4c819e2cfSJiri Olsa#  Lots of this code have been borrowed or heavily inspired from parts
5c819e2cfSJiri Olsa#  of kbuild code, which is not credited, but mostly developed by:
6c819e2cfSJiri Olsa#
7c819e2cfSJiri Olsa#  Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015
8c819e2cfSJiri Olsa#  Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015
9c819e2cfSJiri Olsa#
10c819e2cfSJiri Olsa
11c819e2cfSJiri Olsa###
12c819e2cfSJiri Olsa# Convenient variables
13c819e2cfSJiri Olsacomma   := ,
14c819e2cfSJiri Olsasquote  := '
159564a8cfSRasmus Villemoespound   := \#
16*4a73aff8SCharlie Jenkinsempty   :=
17*4a73aff8SCharlie Jenkinsspace   := $(empty) $(empty)
18c819e2cfSJiri Olsa
19c819e2cfSJiri Olsa###
20c819e2cfSJiri Olsa# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
21c819e2cfSJiri Olsadot-target = $(dir $@).$(notdir $@)
22c819e2cfSJiri Olsa
23c819e2cfSJiri Olsa###
24c819e2cfSJiri Olsa# filename of target with directory and extension stripped
25c819e2cfSJiri Olsabasetarget = $(basename $(notdir $@))
26c819e2cfSJiri Olsa
27c819e2cfSJiri Olsa###
28c819e2cfSJiri Olsa# The temporary file to save gcc -MD generated dependencies must not
29c819e2cfSJiri Olsa# contain a comma
30c819e2cfSJiri Olsadepfile = $(subst $(comma),_,$(dot-target).d)
31c819e2cfSJiri Olsa
32c819e2cfSJiri Olsa###
33c819e2cfSJiri Olsa# Check if both arguments has same arguments. Result is empty string if equal.
34c819e2cfSJiri Olsaarg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
35c819e2cfSJiri Olsa                    $(filter-out $(cmd_$@),   $(cmd_$(1))) )
36c819e2cfSJiri Olsa
37c819e2cfSJiri Olsa###
38c819e2cfSJiri Olsa# Escape single quote for use in echo statements
39c819e2cfSJiri Olsaescsq = $(subst $(squote),'\$(squote)',$1)
40c819e2cfSJiri Olsa
41c819e2cfSJiri Olsa# Echo command
42c819e2cfSJiri Olsa# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
43c819e2cfSJiri Olsaecho-cmd = $(if $($(quiet)cmd_$(1)),\
44c819e2cfSJiri Olsa           echo '  $(call escsq,$($(quiet)cmd_$(1)))';)
45c819e2cfSJiri Olsa
46c819e2cfSJiri Olsa###
47c819e2cfSJiri Olsa# Replace >$< with >$$< to preserve $ when reloading the .cmd file
48c819e2cfSJiri Olsa# (needed for make)
499564a8cfSRasmus Villemoes# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
50c819e2cfSJiri Olsa# (needed for make)
51c819e2cfSJiri Olsa# Replace >'< with >'\''< to be able to enclose the whole string in '...'
52c819e2cfSJiri Olsa# (needed for the shell)
539564a8cfSRasmus Villemoesmake-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
54c819e2cfSJiri Olsa
55c819e2cfSJiri Olsa###
56c819e2cfSJiri Olsa# Find any prerequisites that is newer than target or that does not exist.
57c819e2cfSJiri Olsa# PHONY targets skipped in both cases.
58c819e2cfSJiri Olsaany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
59c819e2cfSJiri Olsa
60c819e2cfSJiri Olsa###
61275e2d95SJiri Olsa# Copy dependency data into .cmd file
62275e2d95SJiri Olsa#  - gcc -M dependency info
63275e2d95SJiri Olsa#  - command line to create object 'cmd_object :='
649fb81323SJiri Olsadep-cmd = $(if $(wildcard $(fixdep)),                                           \
659fb81323SJiri Olsa           $(fixdep) $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;           \
669fb81323SJiri Olsa           rm -f $(depfile);                                                    \
679fb81323SJiri Olsa           mv -f $(dot-target).tmp $(dot-target).cmd,                           \
689feeb638SPaul Menzel           printf '$(pound) cannot find fixdep (%s)\n' $(fixdep) > $(dot-target).cmd; \
699feeb638SPaul Menzel           printf '$(pound) using basic dep data\n\n' >> $(dot-target).cmd;           \
709fb81323SJiri Olsa           cat $(depfile) >> $(dot-target).cmd;                                 \
71a5ba0a1aSJiri Olsa           printf '\n%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd)
72275e2d95SJiri Olsa
73275e2d95SJiri Olsa###
74c819e2cfSJiri Olsa# if_changed_dep  - execute command if any prerequisite is newer than
75c819e2cfSJiri Olsa#                   target, or command line has changed and update
76c819e2cfSJiri Olsa#                   dependencies in the cmd file
77c819e2cfSJiri Olsaif_changed_dep = $(if $(strip $(any-prereq) $(arg-check)),         \
78c819e2cfSJiri Olsa                  @set -e;                                         \
79a278f3d8SAndrii Nakryiko                  $(echo-cmd) $(cmd_$(1));                         \
80a278f3d8SAndrii Nakryiko                  $(dep-cmd))
81c819e2cfSJiri Olsa
82c819e2cfSJiri Olsa# if_changed      - execute command if any prerequisite is newer than
83c819e2cfSJiri Olsa#                   target, or command line has changed
84c819e2cfSJiri Olsaif_changed = $(if $(strip $(any-prereq) $(arg-check)),                   \
85c819e2cfSJiri Olsa              @set -e;                                                   \
86c819e2cfSJiri Olsa              $(echo-cmd) $(cmd_$(1));                                   \
87c819e2cfSJiri Olsa              printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
88c819e2cfSJiri Olsa
89c819e2cfSJiri Olsa###
90c819e2cfSJiri Olsa# C flags to be used in rule definitions, includes:
91c819e2cfSJiri Olsa# - depfile generation
92c819e2cfSJiri Olsa# - global $(CFLAGS)
93c819e2cfSJiri Olsa# - per target C flags
94c819e2cfSJiri Olsa# - per object C flags
95c819e2cfSJiri Olsa# - BUILD_STR macro to allow '-D"$(variable)"' constructs
96baa1973eSPeter Foleyc_flags_1 = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj))
972ec8107dSJiri Olsac_flags_2 = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(c_flags_1))
982ec8107dSJiri Olsac_flags   = $(filter-out $(CFLAGS_REMOVE_$(obj)), $(c_flags_2))
99baa1973eSPeter Foleycxx_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXXFLAGS_$(basetarget).o) $(CXXFLAGS_$(obj))
1000c3b7e42SJiri Olsa
1010c3b7e42SJiri Olsa###
1020c3b7e42SJiri Olsa## HOSTCC C flags
1030c3b7e42SJiri Olsa
104c77a78c2SJohn Garryhost_c_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(HOSTCFLAGS) -D"BUILD_STR(s)=\#s" $(HOSTCFLAGS_$(basetarget).o) $(HOSTCFLAGS_$(obj))
105b61442dfSMasahiro Yamada
106b61442dfSMasahiro Yamada# output directory for tests below
107b61442dfSMasahiro YamadaTMPOUT = .tmp_$$$$
108b61442dfSMasahiro Yamada
109b61442dfSMasahiro Yamada# try-run
110b61442dfSMasahiro Yamada# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
111b61442dfSMasahiro Yamada# Exit code chooses option. "$$TMP" serves as a temporary file and is
112b61442dfSMasahiro Yamada# automatically cleaned up.
113b61442dfSMasahiro Yamadatry-run = $(shell set -e;		\
114b61442dfSMasahiro Yamada	TMP=$(TMPOUT)/tmp;		\
115b61442dfSMasahiro Yamada	mkdir -p $(TMPOUT);		\
116b61442dfSMasahiro Yamada	trap "rm -rf $(TMPOUT)" EXIT;	\
117b61442dfSMasahiro Yamada	if ($(1)) >/dev/null 2>&1;	\
118b61442dfSMasahiro Yamada	then echo "$(2)";		\
119b61442dfSMasahiro Yamada	else echo "$(3)";		\
120b61442dfSMasahiro Yamada	fi)
121b61442dfSMasahiro Yamada
122b61442dfSMasahiro Yamada# cc-option
123b61442dfSMasahiro Yamada# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
124b61442dfSMasahiro Yamadacc-option = $(call try-run, \
125b61442dfSMasahiro Yamada	$(CC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
126b61442dfSMasahiro Yamada
127b61442dfSMasahiro Yamada# delete partially updated (i.e. corrupted) files on error
128b61442dfSMasahiro Yamada.DELETE_ON_ERROR:
129