Build.include 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. # Convenient variables
  12. comma := ,
  13. squote := '
  14. ###
  15. # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
  16. dot-target = $(dir $@).$(notdir $@)
  17. ###
  18. # filename of target with directory and extension stripped
  19. basetarget = $(basename $(notdir $@))
  20. ###
  21. # The temporary file to save gcc -MD generated dependencies must not
  22. # contain a comma
  23. depfile = $(subst $(comma),_,$(dot-target).d)
  24. ###
  25. # Check if both arguments has same arguments. Result is empty string if equal.
  26. arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
  27. $(filter-out $(cmd_$@), $(cmd_$(1))) )
  28. ###
  29. # Escape single quote for use in echo statements
  30. escsq = $(subst $(squote),'\$(squote)',$1)
  31. # Echo command
  32. # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
  33. echo-cmd = $(if $($(quiet)cmd_$(1)),\
  34. echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
  35. ###
  36. # Replace >$< with >$$< to preserve $ when reloading the .cmd file
  37. # (needed for make)
  38. # Replace >#< with >\#< to avoid starting a comment in the .cmd file
  39. # (needed for make)
  40. # Replace >'< with >'\''< to be able to enclose the whole string in '...'
  41. # (needed for the shell)
  42. make-cmd = $(call escsq,$(subst \#,\\\#,$(subst $$,$$$$,$(cmd_$(1)))))
  43. ###
  44. # Find any prerequisites that is newer than target or that does not exist.
  45. # PHONY targets skipped in both cases.
  46. any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
  47. ###
  48. # if_changed_dep - execute command if any prerequisite is newer than
  49. # target, or command line has changed and update
  50. # dependencies in the cmd file
  51. if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \
  52. @set -e; \
  53. $(echo-cmd) $(cmd_$(1)); \
  54. cat $(depfile) > $(dot-target).cmd; \
  55. printf '%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd)
  56. # if_changed - execute command if any prerequisite is newer than
  57. # target, or command line has changed
  58. if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
  59. @set -e; \
  60. $(echo-cmd) $(cmd_$(1)); \
  61. printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
  62. ###
  63. # C flags to be used in rule definitions, includes:
  64. # - depfile generation
  65. # - global $(CFLAGS)
  66. # - per target C flags
  67. # - per object C flags
  68. # - BUILD_STR macro to allow '-D"$(variable)"' constructs
  69. c_flags = -Wp,-MD,$(depfile),-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj))