Build.include 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. # Copy dependency data into .cmd file
  49. # - gcc -M dependency info
  50. # - command line to create object 'cmd_object :='
  51. dep-cmd = $(if $(wildcard $(fixdep)), \
  52. $(fixdep) $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp; \
  53. rm -f $(depfile); \
  54. mv -f $(dot-target).tmp $(dot-target).cmd, \
  55. printf '\# cannot find fixdep (%s)\n' $(fixdep) > $(dot-target).cmd; \
  56. printf '\# using basic dep data\n\n' >> $(dot-target).cmd; \
  57. cat $(depfile) >> $(dot-target).cmd; \
  58. printf '%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd)
  59. ###
  60. # if_changed_dep - execute command if any prerequisite is newer than
  61. # target, or command line has changed and update
  62. # dependencies in the cmd file
  63. if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \
  64. @set -e; \
  65. $(echo-cmd) $(cmd_$(1)) && $(dep-cmd))
  66. # if_changed - execute command if any prerequisite is newer than
  67. # target, or command line has changed
  68. if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
  69. @set -e; \
  70. $(echo-cmd) $(cmd_$(1)); \
  71. printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
  72. ###
  73. # C flags to be used in rule definitions, includes:
  74. # - depfile generation
  75. # - global $(CFLAGS)
  76. # - per target C flags
  77. # - per object C flags
  78. # - BUILD_STR macro to allow '-D"$(variable)"' constructs
  79. c_flags = -Wp,-MD,$(depfile),-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj))
  80. cxx_flags = -Wp,-MD,$(depfile),-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXXFLAGS_$(basetarget).o) $(CXXFLAGS_$(obj))
  81. ###
  82. ## HOSTCC C flags
  83. host_c_flags = -Wp,-MD,$(depfile),-MT,$@ $(CHOSTFLAGS) -D"BUILD_STR(s)=\#s" $(CHOSTFLAGS_$(basetarget).o) $(CHOSTFLAGS_$(obj))