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 := ' |
| 15 | |
| 16 | ### |
| 17 | # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o |
| 18 | dot-target = $(dir $@).$(notdir $@) |
| 19 | |
| 20 | ### |
| 21 | # filename of target with directory and extension stripped |
| 22 | basetarget = $(basename $(notdir $@)) |
| 23 | |
| 24 | ### |
| 25 | # The temporary file to save gcc -MD generated dependencies must not |
| 26 | # contain a comma |
| 27 | depfile = $(subst $(comma),_,$(dot-target).d) |
| 28 | |
| 29 | ### |
| 30 | # Check if both arguments has same arguments. Result is empty string if equal. |
| 31 | arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \ |
| 32 | $(filter-out $(cmd_$@), $(cmd_$(1))) ) |
| 33 | |
| 34 | ### |
| 35 | # Escape single quote for use in echo statements |
| 36 | escsq = $(subst $(squote),'\$(squote)',$1) |
| 37 | |
| 38 | # Echo command |
| 39 | # Short version is used, if $(quiet) equals `quiet_', otherwise full one. |
| 40 | echo-cmd = $(if $($(quiet)cmd_$(1)),\ |
| 41 | echo ' $(call escsq,$($(quiet)cmd_$(1)))';) |
| 42 | |
| 43 | ### |
| 44 | # Replace >$< with >$$< to preserve $ when reloading the .cmd file |
| 45 | # (needed for make) |
| 46 | # Replace >#< with >\#< to avoid starting a comment in the .cmd file |
| 47 | # (needed for make) |
| 48 | # Replace >'< with >'\''< to be able to enclose the whole string in '...' |
| 49 | # (needed for the shell) |
| 50 | make-cmd = $(call escsq,$(subst \#,\\\#,$(subst $$,$$$$,$(cmd_$(1))))) |
| 51 | |
| 52 | ### |
| 53 | # Find any prerequisites that is newer than target or that does not exist. |
| 54 | # PHONY targets skipped in both cases. |
| 55 | any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^) |
| 56 | |
| 57 | ### |
Jiri Olsa | 275e2d9 | 2015-09-23 12:33:59 +0200 | [diff] [blame] | 58 | # Copy dependency data into .cmd file |
| 59 | # - gcc -M dependency info |
| 60 | # - command line to create object 'cmd_object :=' |
Jiri Olsa | 9fb81323 | 2015-09-23 12:34:00 +0200 | [diff] [blame] | 61 | dep-cmd = $(if $(wildcard $(fixdep)), \ |
| 62 | $(fixdep) $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp; \ |
| 63 | rm -f $(depfile); \ |
| 64 | mv -f $(dot-target).tmp $(dot-target).cmd, \ |
| 65 | printf '\# cannot find fixdep (%s)\n' $(fixdep) > $(dot-target).cmd; \ |
| 66 | printf '\# using basic dep data\n\n' >> $(dot-target).cmd; \ |
| 67 | cat $(depfile) >> $(dot-target).cmd; \ |
| 68 | printf '%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd) |
Jiri Olsa | 275e2d9 | 2015-09-23 12:33:59 +0200 | [diff] [blame] | 69 | |
| 70 | ### |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 71 | # if_changed_dep - execute command if any prerequisite is newer than |
| 72 | # target, or command line has changed and update |
| 73 | # dependencies in the cmd file |
| 74 | if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \ |
| 75 | @set -e; \ |
Jiri Olsa | 275e2d9 | 2015-09-23 12:33:59 +0200 | [diff] [blame] | 76 | $(echo-cmd) $(cmd_$(1)) && $(dep-cmd)) |
Jiri Olsa | c819e2c | 2014-12-29 13:51:45 +0100 | [diff] [blame] | 77 | |
| 78 | # if_changed - execute command if any prerequisite is newer than |
| 79 | # target, or command line has changed |
| 80 | if_changed = $(if $(strip $(any-prereq) $(arg-check)), \ |
| 81 | @set -e; \ |
| 82 | $(echo-cmd) $(cmd_$(1)); \ |
| 83 | printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd) |
| 84 | |
| 85 | ### |
| 86 | # C flags to be used in rule definitions, includes: |
| 87 | # - depfile generation |
| 88 | # - global $(CFLAGS) |
| 89 | # - per target C flags |
| 90 | # - per object C flags |
| 91 | # - BUILD_STR macro to allow '-D"$(variable)"' constructs |
| 92 | c_flags = -Wp,-MD,$(depfile),-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj)) |
Wang Nan | f61bdc3 | 2016-09-26 07:26:55 +0000 | [diff] [blame] | 93 | cxx_flags = -Wp,-MD,$(depfile),-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXXFLAGS_$(basetarget).o) $(CXXFLAGS_$(obj)) |
Jiri Olsa | 0c3b7e4 | 2016-09-27 16:18:46 +0200 | [diff] [blame] | 94 | |
| 95 | ### |
| 96 | ## HOSTCC C flags |
| 97 | |
| 98 | host_c_flags = -Wp,-MD,$(depfile),-MT,$@ $(CHOSTFLAGS) -D"BUILD_STR(s)=\#s" $(CHOSTFLAGS_$(basetarget).o) $(CHOSTFLAGS_$(obj)) |