Build.include 4.1 KB

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