Build.include 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. empty :=
  16. space := $(empty) $(empty)
  17. ###
  18. # Name of target with a '.' as filename prefix. foo/bar.o => foo/.bar.o
  19. dot-target = $(dir $@).$(notdir $@)
  20. ###
  21. # filename of target with directory and extension stripped
  22. basetarget = $(basename $(notdir $@))
  23. ###
  24. # The temporary file to save gcc -MD generated dependencies must not
  25. # contain a comma
  26. depfile = $(subst $(comma),_,$(dot-target).d)
  27. ###
  28. # Check if both arguments has same arguments. Result is empty string if equal.
  29. arg-check = $(strip $(filter-out $(cmd_$(1)), $(cmd_$@)) \
  30. $(filter-out $(cmd_$@), $(cmd_$(1))) )
  31. ###
  32. # Escape single quote for use in echo statements
  33. escsq = $(subst $(squote),'\$(squote)',$1)
  34. # Echo command
  35. # Short version is used, if $(quiet) equals `quiet_', otherwise full one.
  36. echo-cmd = $(if $($(quiet)cmd_$(1)),\
  37. echo ' $(call escsq,$($(quiet)cmd_$(1)))';)
  38. ###
  39. # Replace >$< with >$$< to preserve $ when reloading the .cmd file
  40. # (needed for make)
  41. # Replace >#< with >$(pound)< to avoid starting a comment in the .cmd file
  42. # (needed for make)
  43. # Replace >'< with >'\''< to be able to enclose the whole string in '...'
  44. # (needed for the shell)
  45. make-cmd = $(call escsq,$(subst $(pound),$$(pound),$(subst $$,$$$$,$(cmd_$(1)))))
  46. ###
  47. # Find any prerequisites that is newer than target or that does not exist.
  48. # PHONY targets skipped in both cases.
  49. any-prereq = $(filter-out $(PHONY),$?) $(filter-out $(PHONY) $(wildcard $^),$^)
  50. ###
  51. # Copy dependency data into .cmd file
  52. # - gcc -M dependency info
  53. # - command line to create object 'cmd_object :='
  54. dep-cmd = $(if $(wildcard $(fixdep)), \
  55. $(fixdep) $(depfile) $@ '$(make-cmd)' > $(dot-target).tmp; \
  56. rm -f $(depfile); \
  57. mv -f $(dot-target).tmp $(dot-target).cmd, \
  58. printf '$(pound) cannot find fixdep (%s)\n' $(fixdep) > $(dot-target).cmd; \
  59. printf '$(pound) using basic dep data\n\n' >> $(dot-target).cmd; \
  60. cat $(depfile) >> $(dot-target).cmd; \
  61. printf '\n%s\n' 'cmd_$@ := $(make-cmd)' >> $(dot-target).cmd)
  62. ###
  63. # if_changed_dep - execute command if any prerequisite is newer than
  64. # target, or command line has changed and update
  65. # dependencies in the cmd file
  66. if_changed_dep = $(if $(strip $(any-prereq) $(arg-check)), \
  67. @set -e; \
  68. $(echo-cmd) $(cmd_$(1)); \
  69. $(dep-cmd))
  70. # if_changed - execute command if any prerequisite is newer than
  71. # target, or command line has changed
  72. if_changed = $(if $(strip $(any-prereq) $(arg-check)), \
  73. @set -e; \
  74. $(echo-cmd) $(cmd_$(1)); \
  75. printf '%s\n' 'cmd_$@ := $(make-cmd)' > $(dot-target).cmd)
  76. ###
  77. # C flags to be used in rule definitions, includes:
  78. # - depfile generation
  79. # - global $(CFLAGS)
  80. # - per target C flags
  81. # - per object C flags
  82. # - BUILD_STR macro to allow '-D"$(variable)"' constructs
  83. c_flags_1 = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CFLAGS) -D"BUILD_STR(s)=\#s" $(CFLAGS_$(basetarget).o) $(CFLAGS_$(obj))
  84. c_flags_2 = $(filter-out $(CFLAGS_REMOVE_$(basetarget).o), $(c_flags_1))
  85. c_flags = $(filter-out $(CFLAGS_REMOVE_$(obj)), $(c_flags_2))
  86. cxx_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(CXXFLAGS) -D"BUILD_STR(s)=\#s" $(CXXFLAGS_$(basetarget).o) $(CXXFLAGS_$(obj))
  87. ###
  88. # Rust flags to be used on rule definition, includes:
  89. # - global $(RUST_FLAGS)
  90. # - per target Rust flags
  91. # - per object Rust flags
  92. rust_flags_1 = $(RUST_FLAGS) $(RUST_FLAGS_$(basetarget).o) $(RUST_FLAGS_$(obj))
  93. rust_flags_2 = $(filter-out $(RUST_FLAGS_REMOVE_$(basetarget).o), $(rust_flags_1))
  94. rust_flags = $(filter-out $(RUST_FLAGS_REMOVE_$(obj)), $(rust_flags_2))
  95. ###
  96. ## HOSTCC C flags
  97. host_c_flags = -Wp,-MD,$(depfile) -Wp,-MT,$@ $(HOSTCFLAGS) -D"BUILD_STR(s)=\#s" $(HOSTCFLAGS_$(basetarget).o) $(HOSTCFLAGS_$(obj))
  98. # output directory for tests below
  99. TMPOUT = .tmp_$$$$
  100. # try-run
  101. # Usage: option = $(call try-run, $(CC)...-o "$$TMP",option-ok,otherwise)
  102. # Exit code chooses option. "$$TMP" serves as a temporary file and is
  103. # automatically cleaned up.
  104. try-run = $(shell set -e; \
  105. TMP=$(TMPOUT)/tmp; \
  106. mkdir -p $(TMPOUT); \
  107. trap "rm -rf $(TMPOUT)" EXIT; \
  108. if ($(1)) >/dev/null 2>&1; \
  109. then echo "$(2)"; \
  110. else echo "$(3)"; \
  111. fi)
  112. # cc-option
  113. # Usage: cflags-y += $(call cc-option,-march=winchip-c6,-march=i586)
  114. cc-option = $(call try-run, \
  115. $(CC) -Werror $(1) -c -x c /dev/null -o "$$TMP",$(1),$(2))
  116. # delete partially updated (i.e. corrupted) files on error
  117. .DELETE_ON_ERROR: