xref: /openbmc/linux/tools/build/Build.include (revision c77a78c2)
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   := \#
16c819e2cfSJiri Olsa
17c819e2cfSJiri Olsa###
18c819e2cfSJiri Olsa# Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
19c819e2cfSJiri Olsadot-target = $(dir $@).$(notdir $@)
20c819e2cfSJiri Olsa
21c819e2cfSJiri Olsa###
22c819e2cfSJiri Olsa# filename of target with directory and extension stripped
23c819e2cfSJiri Olsabasetarget = $(basename $(notdir $@))
24c819e2cfSJiri Olsa
25c819e2cfSJiri Olsa###
26c819e2cfSJiri Olsa# The temporary file to save gcc -MD generated dependencies must not
27c819e2cfSJiri Olsa# contain a comma
28c819e2cfSJiri Olsadepfile = $(subst $(comma),_,$(dot-target).d)
29c819e2cfSJiri Olsa
30c819e2cfSJiri Olsa###
31c819e2cfSJiri Olsa# Check if both arguments has same arguments. Result is empty string if equal.
32c819e2cfSJiri Olsaarg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
33c819e2cfSJiri Olsa                    $(filter-out $(cmd_$@),   $(cmd_$(1))) )
34c819e2cfSJiri Olsa
35c819e2cfSJiri Olsa###
36c819e2cfSJiri Olsa# Escape single quote for use in echo statements
37c819e2cfSJiri Olsaescsq = $(subst $(squote),'\$(squote)',$1)
38c819e2cfSJiri Olsa
39c819e2cfSJiri Olsa# Echo command
40c819e2cfSJiri Olsa# Short version is used, if $(quiet) equals `quiet_', otherwise full one.
41c819e2cfSJiri Olsaecho-cmd = $(if $($(quiet)cmd_$(1)),\
42c819e2cfSJiri Olsa           echo '  $(call escsq,$($(quiet)cmd_$(1)))';)
43c819e2cfSJiri Olsa
44c819e2cfSJiri Olsa###
45c819e2cfSJiri Olsa# Replace >$< with >$$< to preserve $ when reloading the .cmd file
46c819e2cfSJiri Olsa# (needed for make)
479564a8cfSRasmus Villemoes# Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
48c819e2cfSJiri Olsa# (needed for make)
49c819e2cfSJiri Olsa# Replace >'< with >'\''< to be able to enclose the whole string in '...'
50c819e2cfSJiri Olsa# (needed for the shell)
519564a8cfSRasmus Villemoesmake-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
52c819e2cfSJiri Olsa
53c819e2cfSJiri Olsa###
54c819e2cfSJiri Olsa# Find any prerequisites that is newer than target or that does not exist.
55c819e2cfSJiri Olsa# PHONY targets skipped in both cases.
56c819e2cfSJiri Olsaany-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
57c819e2cfSJiri Olsa
58c819e2cfSJiri Olsa###
59275e2d95SJiri Olsa# Copy dependency data into .cmd file
60275e2d95SJiri Olsa#  - gcc -M dependency info
61275e2d95SJiri Olsa#  - command line to create object 'cmd_object :='
629fb81323SJiri Olsadep-cmd = $(if $(wildcard $(fixdep)),                                           \
639fb81323SJiri Olsa           $(fixdep) $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp;           \
649fb81323SJiri Olsa           rm -f $(depfile);                                                    \
659fb81323SJiri Olsa           mv -f $(dot-target).tmp $(dot-target).cmd,                           \
669feeb638SPaul Menzel           printf '$(pound) cannot find fixdep (%s)\n' $(fixdep) > $(dot-target).cmd; \
679feeb638SPaul Menzel           printf '$(pound) using basic dep data\n\n' >> $(dot-target).cmd;           \
689fb81323SJiri Olsa           cat $(depfile) >> $(dot-target).cmd;                                 \
69a5ba0a1aSJiri Olsa           printf '\n%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd)
70275e2d95SJiri Olsa
71275e2d95SJiri Olsa###
72c819e2cfSJiri Olsa# if_changed_dep  - execute command if any prerequisite is newer than
73c819e2cfSJiri Olsa#                   target, or command line has changed and update
74c819e2cfSJiri Olsa#                   dependencies in the cmd file
75c819e2cfSJiri Olsaif_changed_dep = $(if $(strip $(any-prereq) $(arg-check)),         \
76c819e2cfSJiri Olsa                  @set -e;                                         \
77a278f3d8SAndrii Nakryiko                  $(echo-cmd) $(cmd_$(1));                         \
78a278f3d8SAndrii Nakryiko                  $(dep-cmd))
79c819e2cfSJiri Olsa
80c819e2cfSJiri Olsa# if_changed      - execute command if any prerequisite is newer than
81c819e2cfSJiri Olsa#                   target, or command line has changed
82c819e2cfSJiri Olsaif_changed = $(if $(strip $(any-prereq) $(arg-check)),                   \
83c819e2cfSJiri Olsa              @set -e;                                                   \
84c819e2cfSJiri Olsa              $(echo-cmd) $(cmd_$(1));                                   \
85c819e2cfSJiri Olsa              printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
86c819e2cfSJiri Olsa
87c819e2cfSJiri Olsa###
88c819e2cfSJiri Olsa# C flags to be used in rule definitions, includes:
89c819e2cfSJiri Olsa# - depfile generation
90c819e2cfSJiri Olsa# - global $(CFLAGS)
91c819e2cfSJiri Olsa# - per target C flags
92c819e2cfSJiri Olsa# - per object C flags
93c819e2cfSJiri Olsa# - BUILD_STR macro to allow '-D"$(variable)"' constructs
94baa1973eSPeter Foleyc_flags_1 = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj))
952ec8107dSJiri Olsac_flags_2 = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(c_flags_1))
962ec8107dSJiri Olsac_flags   = $(filter-out $(CFLAGS_REMOVE_$(obj)), $(c_flags_2))
97baa1973eSPeter Foleycxx_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXXFLAGS_$(basetarget).o) $(CXXFLAGS_$(obj))
980c3b7e42SJiri Olsa
990c3b7e42SJiri Olsa###
1000c3b7e42SJiri Olsa## HOSTCC C flags
1010c3b7e42SJiri Olsa
102*c77a78c2SJohn Garryhost_c_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(HOSTCFLAGS) -D"BUILD_STR(s)=\#s" $(HOSTCFLAGS_$(basetarget).o) $(HOSTCFLAGS_$(obj))
103b61442dfSMasahiro Yamada
104b61442dfSMasahiro Yamada# output directory for tests below
105b61442dfSMasahiro YamadaTMPOUT = .tmp_$$$$
106b61442dfSMasahiro Yamada
107b61442dfSMasahiro Yamada# try-run
108b61442dfSMasahiro Yamada# Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
109b61442dfSMasahiro Yamada# Exit code chooses option. "$$TMP" serves as a temporary file and is
110b61442dfSMasahiro Yamada# automatically cleaned up.
111b61442dfSMasahiro Yamadatry-run = $(shell set -e;		\
112b61442dfSMasahiro Yamada	TMP=$(TMPOUT)/tmp;		\
113b61442dfSMasahiro Yamada	mkdir -p $(TMPOUT);		\
114b61442dfSMasahiro Yamada	trap "rm -rf $(TMPOUT)" EXIT;	\
115b61442dfSMasahiro Yamada	if ($(1)) >/dev/null 2>&1;	\
116b61442dfSMasahiro Yamada	then echo "$(2)";		\
117b61442dfSMasahiro Yamada	else echo "$(3)";		\
118b61442dfSMasahiro Yamada	fi)
119b61442dfSMasahiro Yamada
120b61442dfSMasahiro Yamada# cc-option
121b61442dfSMasahiro Yamada# Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
122b61442dfSMasahiro Yamadacc-option = $(call try-run, \
123b61442dfSMasahiro Yamada	$(CC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
124b61442dfSMasahiro Yamada
125b61442dfSMasahiro Yamada# delete partially updated (i.e. corrupted) files on error
126b61442dfSMasahiro Yamada.DELETE_ON_ERROR:
127