Makefile.rules 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #
  2. # Asterisk -- An open source telephony toolkit.
  3. #
  4. # Makefile rules
  5. #
  6. # Copyright (C) 2006-2010, Digium, Inc.
  7. #
  8. # Kevin P. Fleming <kpfleming@digium.com>
  9. #
  10. # This program is free software, distributed under the terms of
  11. # the GNU General Public License
  12. #
  13. # Each command is preceded by a short comment on what to do.
  14. # Prefixing one or the other with @\# or @ or nothing makes the desired
  15. # behaviour. ECHO_PREFIX prefixes the comment, CMD_PREFIX prefixes the command.
  16. -include $(ASTTOPDIR)/makeopts
  17. # Helpful functions
  18. # call with $(call function,...)
  19. tolower = $(shell echo $(1) | tr '[:upper:]' '[:lower:]')
  20. # Takes a list of MENUSELECT_CFLAG Id and returns CFLAGS to declare
  21. # the ones which are enabled.
  22. get_menuselect_cflags=$(patsubst %,-D%,$(filter $1,$(MENUSELECT_CFLAGS)))
  23. .PHONY: dist-clean
  24. # If 'make' decides to create intermediate files to satisfy a build requirement
  25. # (like producing a .i from a .c), we want to keep them, so tell make to keep
  26. # all intermediate files
  27. .SECONDARY:
  28. # extra cflags to build dependencies. Recursively expanded.
  29. MAKE_DEPS=-MD -MT $@ -MF .$(subst /,_,$@).d -MP
  30. ifeq ($(findstring ADDRESS_SANITIZER,$(MENUSELECT_CFLAGS)),ADDRESS_SANITIZER)
  31. _ASTLDFLAGS+=-fsanitize=address
  32. _ASTCFLAGS+=-fno-omit-frame-pointer -fsanitize=address
  33. endif
  34. ifeq ($(findstring THREAD_SANITIZER,$(MENUSELECT_CFLAGS)),THREAD_SANITIZER)
  35. _ASTLDFLAGS+=-fsanitize=thread -pie -fPIE
  36. _ASTCFLAGS+=-fno-omit-frame-pointer -pie -fPIE -fsanitize=thread
  37. endif
  38. ifeq ($(findstring LEAK_SANITIZER,$(MENUSELECT_CFLAGS)),LEAK_SANITIZER)
  39. _ASTLDFLAGS+=-fsanitize=leak
  40. _ASTCFLAGS+=-fno-omit-frame-pointer -fsanitize=leak
  41. endif
  42. ifeq ($(findstring UNDEFINED_SANITIZER,$(MENUSELECT_CFLAGS)),UNDEFINED_SANITIZER)
  43. _ASTLDFLAGS+=-fsanitize=undefined
  44. _ASTCFLAGS+=-fno-omit-frame-pointer -fsanitize=undefined
  45. endif
  46. ifeq ($(NOISY_BUILD),)
  47. ECHO_PREFIX=@
  48. CMD_PREFIX=@
  49. else
  50. ECHO_PREFIX=@\#
  51. CMD_PREFIX=
  52. endif
  53. OPTIMIZE?=-O3
  54. ifneq ($(findstring darwin,$(OSARCH)),)
  55. ifeq ($(shell if test `/usr/bin/sw_vers -productVersion | cut -c4` -gt 5; then echo 6; else echo 0; fi),6)
  56. # Snow Leopard/Lion has an issue with this optimization flag on large files
  57. OPTIMIZE+=-fno-inline-functions
  58. endif
  59. endif
  60. ifeq ($(CC),gcc)
  61. # gcc version 8.2.1 and above must have partial-inlining disabled in order
  62. # to avoid a documented bug. Sort to make the lowest version number come
  63. # first. If it's the specified version then the current gcc version is equal
  64. # to or greater, so add the custom optimization rule.
  65. gcc_versions=$(shell printf "%s\n" $$(gcc -dumpversion) 8.2.1 | sort -n)
  66. ifeq ($(firstword $(gcc_versions)),8.2.1)
  67. OPTIMIZE+=-fno-partial-inlining
  68. endif
  69. endif
  70. ifeq ($(findstring DONT_OPTIMIZE,$(MENUSELECT_CFLAGS))$(AST_CODE_COVERAGE),no)
  71. _ASTCFLAGS+=$(OPTIMIZE)
  72. else
  73. _ASTCFLAGS+=-O0
  74. endif
  75. ifeq ($(AST_CODE_COVERAGE),yes)
  76. _ASTCFLAGS_COVERAGE=-ftest-coverage -fprofile-arcs
  77. _ASTLDFLAGS+=-ftest-coverage -fprofile-arcs
  78. else
  79. _ASTCFLAGS_COVERAGE=
  80. endif
  81. ifeq ($(findstring $(CONFIG_CFLAGS),$(_ASTCFLAGS)),)
  82. _ASTCFLAGS+=$(CONFIG_CFLAGS)
  83. endif
  84. # shortcuts for common combinations of flags; these must be recursively expanded so that
  85. # per-target settings will be applied
  86. CC_CFLAGS=$(PTHREAD_CFLAGS) $(_ASTCFLAGS) $(ASTCFLAGS)
  87. CXX_CFLAGS=$(PTHREAD_CFLAGS) $(filter-out -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations $(AST_DECLARATION_AFTER_STATEMENT),$(_ASTCFLAGS) $(ASTCFLAGS))
  88. # Clang -Werror warning suppressions
  89. ifeq ($(C_COMPILER_FAMILY),clang)
  90. CC_CFLAGS+=-Wno-unused-value -Wno-parentheses-equality
  91. endif
  92. ifeq ($(GNU_LD),1)
  93. SO_SUPPRESS_SYMBOLS=-Wl,--version-script,$(subst .so,.exports,$@),--warn-common
  94. ifneq ($(wildcard $(subst .so,.dynamics,$@)),)
  95. SO_SUPPRESS_SYMBOLS+=-Wl,--dynamic-list,$(subst .so,.dynamics,$@)
  96. endif
  97. endif
  98. CC_LDFLAGS_SO=$(PTHREAD_CFLAGS) $(_ASTLDFLAGS) $(SOLINK) $(SO_SUPPRESS_SYMBOLS) $(ASTLDFLAGS)
  99. CXX_LDFLAGS_SO=$(PTHREAD_CFLAGS) $(_ASTLDFLAGS) $(SOLINK) $(SO_SUPPRESS_SYMBOLS) $(ASTLDFLAGS)
  100. CC_LIBS=$(PTHREAD_LIBS) $(LIBS)
  101. CXX_LIBS=$(PTHREAD_LIBS) $(LIBS)
  102. # determine whether to double-compile so that the optimizer can report code path problems
  103. # In this case, we run the preprocessor to produce a .i or .ii file from the source
  104. # code, then compile once with optimizer enabled (and the output to /dev/null),
  105. # and if that doesn't fail then compile again with optimizer disabled
  106. ifeq ($(findstring COMPILE_DOUBLE,$(MENUSELECT_CFLAGS)),COMPILE_DOUBLE)
  107. COMPILE_DOUBLE=yes
  108. endif
  109. ifeq ($(findstring DONT_OPTIMIZE,$(MENUSELECT_CFLAGS))$(AST_DEVMODE),)
  110. _ASTCFLAGS+=$(AST_FORTIFY_SOURCE)
  111. endif
  112. ifeq ($(findstring BUILD_NATIVE,$(MENUSELECT_CFLAGS)),BUILD_NATIVE)
  113. _ASTCFLAGS+=-march=native
  114. endif
  115. %.o: %.s
  116. $(ECHO_PREFIX) echo " [AS] $< -> $@"
  117. ifeq ($(COMPILE_DOUBLE),yes)
  118. $(CMD_PREFIX) $(CC) -o /dev/null -c $< $(CC_CFLAGS) $(OPTIMIZE)
  119. endif
  120. $(CMD_PREFIX) $(CC) -o $@ -c $< $(CC_CFLAGS) $(_ASTCFLAGS_COVERAGE)
  121. %.o: %.i
  122. $(ECHO_PREFIX) echo " [CCi] $< -> $@"
  123. ifneq ($(AST_CLANG_BLOCKS),)
  124. ifeq ($(COMPILE_DOUBLE),yes)
  125. $(CMD_PREFIX) $(CC) -o /dev/null -c $< $(CC_CFLAGS) $(OPTIMIZE) -Wno-unused-command-line-argument
  126. endif
  127. $(CMD_PREFIX) $(CC) -o $@ -c $< $(CC_CFLAGS) $(_ASTCFLAGS_COVERAGE) -Wno-unused-command-line-argument
  128. else
  129. ifeq ($(COMPILE_DOUBLE),yes)
  130. $(CMD_PREFIX) $(CC) -o /dev/null -c $< $(CC_CFLAGS) $(OPTIMIZE)
  131. endif
  132. $(CMD_PREFIX) $(CC) -o $@ -c $< $(CC_CFLAGS) $(_ASTCFLAGS_COVERAGE)
  133. endif
  134. ifneq ($(COMPILE_DOUBLE),yes)
  135. %.o: %.c
  136. $(ECHO_PREFIX) echo " [CC] $< -> $@"
  137. $(CMD_PREFIX) $(CC) -o $@ -c $< $(MAKE_DEPS) $(CC_CFLAGS) $(_ASTCFLAGS_COVERAGE)
  138. endif
  139. %.i: %.c
  140. $(ECHO_PREFIX) echo " [CPP] $< -> $@"
  141. $(CMD_PREFIX) $(CC) -o $@ -E $< $(MAKE_DEPS) $(CC_CFLAGS) $(_ASTCFLAGS_COVERAGE)
  142. %.oo: %.ii
  143. $(ECHO_PREFIX) echo " [CXXi] $< -> $@"
  144. ifeq ($(COMPILE_DOUBLE),yes)
  145. $(CMD_PREFIX) $(CXX) -o /dev/null -c $< $(CXX_CFLAGS) $(OPTIMIZE)
  146. endif
  147. $(CMD_PREFIX) $(CXX) -o $@ -c $< $(CXX_CFLAGS) $(_ASTCFLAGS_COVERAGE)
  148. ifneq ($(COMPILE_DOUBLE),yes)
  149. %.oo: %.cc
  150. $(ECHO_PREFIX) echo " [CXX] $< -> $@"
  151. $(CMD_PREFIX) $(CXX) -o $@ -c $< $(MAKE_DEPS) $(CXX_CFLAGS) $(_ASTCFLAGS_COVERAGE)
  152. endif
  153. %.ii: %.cc
  154. $(ECHO_PREFIX) echo " [CPP] $< -> $@"
  155. $(CMD_PREFIX) $(CXX) -o $@ -E $< $(MAKE_DEPS) $(CXX_CFLAGS) $(_ASTCFLAGS_COVERAGE)
  156. %.so: %.o
  157. ifeq ($(GNU_LD),1)
  158. $(CMD_PREFIX) $(ASTTOPDIR)/build_tools/make_linker_version_script $* "$(LINKER_SYMBOL_PREFIX)" "$(ASTTOPDIR)"
  159. endif
  160. $(ECHO_PREFIX) echo " [LD] $^ -> $@"
  161. $(CMD_PREFIX) $(CC) -o $@ $(CC_LDFLAGS_SO) $^ $(CC_LIBS)
  162. %.so: %.oo
  163. ifeq ($(GNU_LD),1)
  164. $(CMD_PREFIX) $(ASTTOPDIR)/build_tools/make_linker_version_script $* "$(LINKER_SYMBOL_PREFIX)" "$(ASTTOPDIR)"
  165. endif
  166. $(ECHO_PREFIX) echo " [LDXX] $^ -> $@"
  167. $(CMD_PREFIX) $(CXX) -o $@ $(CXX_LDFLAGS_SO) $^ $(CXX_LIBS)
  168. %: %.o
  169. $(ECHO_PREFIX) echo " [LD] $^ -> $@"
  170. $(CMD_PREFIX) $(CXX) -o $@ $(PTHREAD_CFLAGS) $(_ASTLDFLAGS) $^ $(CXX_LIBS) $(ASTLDFLAGS)
  171. # These CC commands just create an object file with the input file embedded in it.
  172. # It can be access from code as follows:
  173. # If your input file is named abc_def.xml...
  174. #
  175. # extern const uint8_t _binary_abc_def_xml_start[];
  176. # extern const uint8_t _binary_abc_def_xml_end[];
  177. # extern const size_t _binary_abc_def_xml_size;
  178. %.o: %.xml
  179. $(ECHO_PREFIX) echo " [LD] $^ -> $@"
  180. $(CMD_PREFIX) $(CC) -g -Wl,-znoexecstack -nostartfiles -nodefaultlibs -nostdlib -r -Wl,-b,binary -o $@ $^
  181. %.o: %.xslt
  182. $(ECHO_PREFIX) echo " [LD] $^ -> $@"
  183. $(CMD_PREFIX) $(CC) -g -Wl,-znoexecstack -nostartfiles -nodefaultlibs -nostdlib -r -Wl,-b,binary -o $@ $^
  184. dist-clean:: clean