Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 1 | ### |
| 2 | # build: Generic definitions |
| 3 | # |
| 4 | # Lots of this code have been borrowed or heavily inspired from parts |
| 5 | # of kbuild code, which is not credited, but mostly developed by: |
| 6 | # |
| 7 | # Copyright (C) Sam Ravnborg <sam@mars.ravnborg.org>, 2015 |
| 8 | # Copyright (C) Linus Torvalds <torvalds@linux-foundation.org>, 2015 |
| 9 | # |
| 10 | |
| 11 | ### |
| 12 | # Convenient variables |
| 13 | comma := , |
| 14 | squote := ' |
Rasmus Villemoes | 9564a8c | 2018-04-08 23:35:28 +0200 | [diff] [blame] | 15 | pound := \# |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 16 | |
| 17 | ### |
| 18 | # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o |
| 19 | dot-target = $(dir $@).$(notdir $@) |
| 20 | |
| 21 | ### |
| 22 | # filename of target with directory and extension stripped |
| 23 | basetarget = $(basename $(notdir $@)) |
| 24 | |
| 25 | ### |
| 26 | # The temporary file to save gcc -MD generated dependencies must not |
| 27 | # contain a comma |
| 28 | depfile = $(subst $(comma),_,$(dot-target).d) |
| 29 | |
| 30 | ### |
| 31 | # Check if both arguments has same arguments. Result is empty string if equal. |
| 32 | arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ |
| 33 | $(filter-out $(cmd_$@), $(cmd_$(1))) ) |
| 34 | |
| 35 | ### |
| 36 | # Escape single quote for use in echo statements |
| 37 | escsq = $(subst $(squote),'\$(squote)',$1) |
| 38 | |
| 39 | # Echo command |
| 40 | # Short version is used, if $(quiet) equals `quiet_', otherwise full one. |
| 41 | echo-cmd = $(if $($(quiet)cmd_$(1)),\ |
| 42 | echo ' $(call escsq,$($(quiet)cmd_$(1)))';) |
| 43 | |
| 44 | ### |
| 45 | # Replace >$< with >$$< to preserve $ when reloading the .cmd file |
| 46 | # (needed for make) |
Rasmus Villemoes | 9564a8c | 2018-04-08 23:35:28 +0200 | [diff] [blame] | 47 | # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 48 | # (needed for make) |
| 49 | # Replace >'< with >'\''< to be able to enclose the whole string in '...' |
| 50 | # (needed for the shell) |
Rasmus Villemoes | 9564a8c | 2018-04-08 23:35:28 +0200 | [diff] [blame] | 51 | make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1))))) |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 52 | |
| 53 | ### |
| 54 | # Find any prerequisites that is newer than target or that does not exist. |
| 55 | # PHONY targets skipped in both cases. |
| 56 | any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^) |
| 57 | |
| 58 | ### |
Jiri Olsa | 275e2d9 | 2015-09-23 12:33:59 +0200 | [diff] [blame] | 59 | # Copy dependency data into .cmd file |
| 60 | # - gcc -M dependency info |
| 61 | # - command line to create object 'cmd_object :=' |
Jiri Olsa | 9fb81323 | 2015-09-23 12:34:00 +0200 | [diff] [blame] | 62 | dep-cmd = $(if $(wildcard $(fixdep)), \ |
| 63 | $(fixdep) $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp; \ |
| 64 | rm -f $(depfile); \ |
| 65 | mv -f $(dot-target).tmp $(dot-target).cmd, \ |
Paul Menzel | 9feeb638 | 2018-06-05 19:00:22 +0200 | [diff] [blame] | 66 | printf '$(pound) cannot find fixdep (%s)\n' $(fixdep) > $(dot-target).cmd; \ |
| 67 | printf '$(pound) using basic dep data\n\n' >> $(dot-target).cmd; \ |
Jiri Olsa | 9fb81323 | 2015-09-23 12:34:00 +0200 | [diff] [blame] | 68 | cat $(depfile) >> $(dot-target).cmd; \ |
Jiri Olsa | a5ba0a1 | 2016-12-04 21:42:52 +0100 | [diff] [blame] | 69 | printf '\n%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd) |
Jiri Olsa | 275e2d9 | 2015-09-23 12:33:59 +0200 | [diff] [blame] | 70 | |
| 71 | ### |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 72 | # if_changed_dep - execute command if any prerequisite is newer than |
| 73 | # target, or command line has changed and update |
| 74 | # dependencies in the cmd file |
| 75 | if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \ |
Jiri Olsa | 2fedf79 | 2016-12-04 21:42:53 +0100 | [diff] [blame] | 76 | @set -e; \ |
Andrii Nakryiko | a278f3d | 2020-07-30 19:42:44 -0700 | [diff] [blame] | 77 | $(echo-cmd) $(cmd_$(1)); \ |
| 78 | $(dep-cmd)) |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 79 | |
| 80 | # if_changed - execute command if any prerequisite is newer than |
| 81 | # target, or command line has changed |
Jiri Olsa | 2fedf79 | 2016-12-04 21:42:53 +0100 | [diff] [blame] | 82 | if_changed = $(if $(strip $(any-prereq) $(arg-check)), \ |
| 83 | @set -e; \ |
| 84 | $(echo-cmd) $(cmd_$(1)); \ |
| 85 | printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 86 | |
| 87 | ### |
| 88 | # C flags to be used in rule definitions, includes: |
| 89 | # - depfile generation |
| 90 | # - global $(CFLAGS) |
| 91 | # - per target C flags |
| 92 | # - per object C flags |
| 93 | # - BUILD_STR macro to allow '-D"$(variable)"' constructs |
Peter Foley | baa1973 | 2016-11-27 21:43:46 -0500 | [diff] [blame] | 94 | c_flags_1 = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj)) |
Jiri Olsa | 2ec8107 | 2016-11-02 14:35:47 +0100 | [diff] [blame] | 95 | c_flags_2 = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(c_flags_1)) |
| 96 | c_flags = $(filter-out $(CFLAGS_REMOVE_$(obj)), $(c_flags_2)) |
Peter Foley | baa1973 | 2016-11-27 21:43:46 -0500 | [diff] [blame] | 97 | cxx_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXXFLAGS_$(basetarget).o) $(CXXFLAGS_$(obj)) |
Jiri Olsa | 0c3b7e4 | 2016-09-27 16:18:46 +0200 | [diff] [blame] | 98 | |
| 99 | ### |
| 100 | ## HOSTCC C flags |
| 101 | |
John Garry | c77a78c | 2021-10-30 00:30:41 +0800 | [diff] [blame] | 102 | host_c_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(HOSTCFLAGS) -D"BUILD_STR(s)=\#s" $(HOSTCFLAGS_$(basetarget).o) $(HOSTCFLAGS_$(obj)) |
Masahiro Yamada | b61442d | 2021-04-16 22:00:51 +0900 | [diff] [blame] | 103 | |
| 104 | # output directory for tests below |
| 105 | TMPOUT = .tmp_$$$$ |
| 106 | |
| 107 | # try-run |
| 108 | # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise) |
| 109 | # Exit code chooses option. "$$TMP" serves as a temporary file and is |
| 110 | # automatically cleaned up. |
| 111 | try-run = $(shell set -e; \ |
| 112 | TMP=$(TMPOUT)/tmp; \ |
| 113 | mkdir -p $(TMPOUT); \ |
| 114 | trap "rm -rf $(TMPOUT)" EXIT; \ |
| 115 | if ($(1)) >/dev/null 2>&1; \ |
| 116 | then echo "$(2)"; \ |
| 117 | else echo "$(3)"; \ |
| 118 | fi) |
| 119 | |
| 120 | # cc-option |
| 121 | # Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586) |
| 122 | cc-option = $(call try-run, \ |
| 123 | $(CC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2)) |
| 124 | |
| 125 | # delete partially updated (i.e. corrupted) files on error |
| 126 | .DELETE_ON_ERROR: |