2
0

Makefile 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. COREPATH=../../../src
  2. #include $(COREPATH)/Makefile.defs
  3. CFG2TXT=../../scripts/cdefs2doc/dump_counters.pl
  4. CFG2DOCBOOK=../../scripts/cdefs2doc/dump_counters.pl
  5. # output directory for generated txt files
  6. txt_output_dir=.
  7. # output directory for generated docbook xml files
  8. docbook_output_dir=docbook
  9. # list of files containing counter defs in the following format:
  10. # <filename>:<cfg_grp_name>
  11. # can be easily updated by adding the output of:
  12. # make diff-list (which obeys grp_exclude and file_exclude)
  13. # or completely regenerated by replacing files_list with the output of:
  14. # make gen-files-list
  15. files_list= \
  16. $(COREPATH)/core/resolve.c:resolve \
  17. $(COREPATH)/core/tcp_stats.c:tcp \
  18. $(COREPATH)/modules/cdp/cdp_stats.c:cdp \
  19. $(COREPATH)/modules/db_mysql/db_mysql.c:db_mysql \
  20. $(COREPATH)/modules/ims_charging/ims_charging_stats.c:ims_charging \
  21. $(COREPATH)/modules/ims_dialog/dlg_ng_stats.c:ims_dialog \
  22. $(COREPATH)/modules/ims_qos/ims_qos_stats.c:ims_qos \
  23. $(COREPATH)/modules/ims_usrloc_scscf/ul_scscf_stats.c:ims_usrloc_scscf \
  24. $(COREPATH)/modules/sctp/sctp_stats.c:sctp
  25. # list of excluded groups
  26. grp_exclude=
  27. # list of file prefixes to exclude (full path needed)
  28. file_exclude=$(COREPATH)/obsolete
  29. # special per file group overrides
  30. # format= grp_filename=... ,where filename does not contain the extension
  31. # e.g.:
  32. # grp_f_tcp_stats=tcp
  33. # grp_f_sctp_stats=sctp
  34. # special per group group name overrides
  35. # e.g.:
  36. # grp_g_maxfwd=mf
  37. grp_g_resolve=dns
  38. grp_g_db_mysql=mysql
  39. # override auto-detected group if set to 1 (else the group is used inside the
  40. # file only if it cannot be autodetected)
  41. ifeq ($(group_override),1)
  42. override force_grp=force-
  43. else
  44. override force_grp=
  45. endif
  46. # command used for gcc (contains extra includes)
  47. gcc=gcc
  48. #-I$(COREPATH)/lib -I$(COREPATH) -I/usr/include/libxml2
  49. # defines used by gcc
  50. c_defs=-DNAME='\"kamailio\"' -DVERSION='\"5.1.0-dev3\"' -DARCH='\"x86_64\"' \
  51. -DOS='linux_' -DOS_QUOTED='\"linux\"' -DCOMPILER='\"gcc 4.9.2\"' \
  52. -D__CPU_x86_64 -D__OS_linux -DSER_VER=5001000 \
  53. -DCFG_DIR='\"/usr/local/etc/kamailio/\"' \
  54. -DRUN_DIR='\"/var/run/kamailio/\"' -DPKG_MALLOC -DSHM_MEM -DSHM_MMAP \
  55. -DDNS_IP_HACK -DUSE_MCAST -DUSE_TCP -DDISABLE_NAGLE -DHAVE_RESOLV_RES \
  56. -DUSE_DNS_CACHE -DUSE_DNS_FAILOVER -DUSE_DST_BLACKLIST -DUSE_NAPTR \
  57. -DWITH_XAVP -DMEM_JOIN_FREE -DF_MALLOC -DQ_MALLOC -DTLSF_MALLOC \
  58. -DDBG_SR_MEMORY -DUSE_TLS -DTLS_HOOKS -DUSE_CORE_STATS -DSTATISTICS \
  59. -DMALLOC_STATS -DWITH_AS_SUPPORT -DUSE_SCTP -DFAST_LOCK -DADAPTIVE_WAIT \
  60. -DADAPTIVE_WAIT_LOOPS=1024 -DCC_GCC_LIKE_ASM -DHAVE_GETHOSTBYNAME2 \
  61. -DHAVE_UNION_SEMUN -DHAVE_SCHED_YIELD -DHAVE_MSG_NOSIGNAL \
  62. -DHAVE_MSGHDR_MSG_CONTROL -DHAVE_ALLOCA_H -DHAVE_TIMEGM \
  63. -DHAVE_SCHED_SETSCHEDULER -DHAVE_IP_MREQN -DHAVE_EPOLL -DHAVE_SIGIO_RT \
  64. -DSIGINFO64_WORKARROUND -DUSE_FUTEX -DHAVE_SELECT
  65. # common makefile vars used in defs
  66. LOCALBASE=/usr/local
  67. SYSBASE=/usr
  68. filter_files=$(filter-out $(addsuffix %,$(file_exclude)),\
  69. $(filter-out $(addprefix %:,$(grp_exclude)),$(1)))
  70. #filtered files list
  71. flist=$(call filter_files,$(files_list))
  72. # throws an error if input is not in the format filename:grp
  73. check_fname_grp=$(if $(filter-out 2,$(words $(subst :, ,$(1)))),\
  74. $(error bad format "$(1)", it should be filename:grp))
  75. # get prereq from file:grp (get_prereq(file:grp) => file)
  76. get_prereq=$(firstword $(subst :, ,$(1)))
  77. # get grp from file:grp (get_grp(file:grp) => grp)
  78. get_listed_grp=$(word 2, $(subst :, ,$(1)))
  79. # get base file name from file:grp: get_bname(file:grp)
  80. # => basename(file) without extension (e.g. get_bname(foo/bar.c:x) => bar)
  81. #
  82. get_bname=$(basename $(notdir $(call get_prereq,$(1))))
  83. #get grp from file:grp, using the overrides
  84. get_grp=$(strip $(if $(grp_f_$(call get_bname,$(1))), \
  85. $(grp_f_$(call get_bname,$(1))),\
  86. $(if $(grp_g_$(call get_listed_grp,$(1))),\
  87. $(grp_g_$(call get_listed_grp,$(1))),\
  88. $(call get_listed_grp,$(1))) ) )
  89. # get target from file:grp (get_target(file:grp) => counter_grp.txt)
  90. get_target=counters_$(call get_grp,$(1))
  91. # $(LF) definition (do not remove)
  92. define LF
  93. endef
  94. # get all the lines containing DEFS or INCLUDES definitions from the Makefile.
  95. # WARNING: does not work with all sed implementation (tested with GNU sed).
  96. # It uses a hack to restore the LFs (LFs are removed by $(shell)): LFs are
  97. # replaced with '^LF^' and then ^LF^ is subst'ed back to a real LF.
  98. get_make_idefs=$(subst ^LF^,$(LF),$(shell sed \
  99. -ne '/^[\t ]*\(DEFS\|INCLUDES\)[\t ]*[+:]\?=.*[^\]$$/H'\
  100. -ne '/^[\t ]*\(DEFS\|INCLUDES\)[\t ]*[+:]\?=.*\\$$/,/\(^$$\)\|\([^\]$$\)/H'\
  101. -ne '$${g;s/\n/^LF^/g;p}'\
  102. < $(1)/Makefile ))
  103. # get all the lines from the makefile containing variable definitions.
  104. # It will also return conditionals and try to filter out possible rules.
  105. # WARNING: does not work with all sed implementation (tested with GNU sed).
  106. # It uses a hack to restore the LFs (LFs are removed by $(shell)): LFs are
  107. # replaced with '^LF^' and then ^LF^ is subst'ed back to a real LF.
  108. get_make_vars=$(subst ^LF^,$(LF),$(shell sed -n \
  109. -e ': start' \
  110. -e '/^\(ifeq\|ifneq\|else\|endif\)[\t ]*\($$\|.*[^\]$$\)/{H;b end}' \
  111. -e '/^\(ifeq\|ifneq\|else\|endif\)[\t ]\+.*[\]$$/,/[^\]$$/{H;b end}' \
  112. -e '/^[a-zA-Z._/$$][a-zA-Z0-9._()/$$ \t-]*:\([^=]\|$$\)/b eat_rule' \
  113. -e '/^[\t ]*[A-Za-z._][A-Za-z0-9._-]*[\t ]*[+:]\?=.*[^\]$$/{H;b end}' \
  114. -e '/^[\t ]*[A-Za-z._][A-Za-z0-9._-]*[\t ]*[+:]\?=.*\\$$/,/\(^$$\)\|\([^\]$$\)/{H;b end}' \
  115. -e ': end' \
  116. -e '$${g;s/\n/^LF^/g;p}'\
  117. -e 'b' \
  118. -e ': eat_rule' \
  119. -e '$$b end' \
  120. -e 'n' \
  121. -e '/^[a-zA-Z._/$$][a-zA-Z0-9._()/$$ \t-]*:\([^=]\|$$\)/b eat_rule' \
  122. -e '/^[\t]/b eat_rule' \
  123. -e 'b start' \
  124. < $(1)/Makefile ))
  125. define mk_rules
  126. $(call check_fname_grp, $(1))
  127. #$$(info generating counter_$$(call get_grp,$(1)).txt: $$(call get_prereq,$(1)))
  128. DEFS:=
  129. INCLUDES:=
  130. # extract all the includes and defs from the module makefile and
  131. # evaluate them
  132. $$(eval $$(call get_make_vars,$$(dir $$(call get_prereq,$(1)))))
  133. # override COREPATH (we know better)
  134. COREPATH=../../../src
  135. # save the result in a per group e_idefs_<grp_name> var
  136. $$(eval e_idefs_$$(call get_grp,$(1)):=$$(DEFS) $$(INCLUDES))
  137. # debugging:
  138. #$$(info eval: $$(call get_make_vars,$$(dir $$(call get_prereq,$(1)))))
  139. #$$(info e_idefs_$$(call get_grp,$(1))=$$(e_idefs_$$(call get_grp,$(1))))
  140. $(txt_output_dir)/$$(call get_target,$(1)).txt: \
  141. $$(call get_prereq,$(1)) Makefile $(CFG2TXT)
  142. $(CFG2TXT) --file $$< --$(force_grp)grp=$$(call get_grp,$(1)) \
  143. --gcc="$(gcc)" --txt \
  144. --defs="$(c_defs) $$(e_idefs_$$(call get_grp,$(1)))" \
  145. > "$$@" || (rm -f "$$@"; exit 1)
  146. $(docbook_output_dir)/$$(call get_target,$(1)).xml: \
  147. $$(call get_prereq,$(1)) Makefile $(CFG2TXT)
  148. $(CFG2DOCBOOK) --file $$< --$(force_grp)grp=$$(call get_grp,$(1)) \
  149. --gcc="$(gcc)" --docbook \
  150. --defs="$(c_defs) $$(e_idefs_$$(call get_grp,$(1)))" \
  151. > "$$@" || (rm -f "$$@"; exit 1)
  152. clean_$$(call get_target,$(1)).txt:
  153. rm -f "$(txt_output_dir)/$$(call get_target,$(1)).txt"
  154. clean_$$(call get_target,$(1)).xml:
  155. rm -f "$(docbook_output_dir)/$$(call get_target,$(1)).xml"
  156. txt: $(txt_output_dir)/$$(call get_target,$(1)).txt
  157. docbook: $(docbook_output_dir)/$$(call get_target,$(1)).xml
  158. clean_txt: clean_$$(call get_target,$(1)).txt
  159. clean_docbook: clean_$$(call get_target,$(1)).xml
  160. endef
  161. find_counter_files_cmd= find $(COREPATH) -type f -name "*.c" \
  162. -exec grep "counter_def_t[ ][ ]*[a-zA-Z0-9_][a-zA-Z0-9_]*\[\][ ]*=" /dev/null {} \; \
  163. | cut -d: -f1
  164. # shell command to generate a file:grp list from a list of files
  165. # grp will be the modulename if the file is in a module directory or
  166. # the file name with the extension and _cnt, cnt_, _stats, stats_,
  167. # _cnts, or cnts_ stripped out of
  168. # it.
  169. # output: list of " "filename":"grpname
  170. gen_file_grp_cmd=\
  171. sed -e "s!\(.*/modules[^/]*/\([^/][^/]*\)/.*\)! \1:\2!" \
  172. -e "s!^\([^ ].*/\([^/.]*\)[^/]*$$\)!\1:\2!" \
  173. -e "s!^\([^ :]*\):\(.*\)_cnts\?[_]*!\1:\2!" \
  174. -e "s!^\([^ :]*\):\(.*\)cnts\?[_]*!\1:\2!" \
  175. -e "s!^\([^ :]*\):\(.*\)_stats[_]*!\1:\2!" \
  176. -e "s!^\([^ :]*\):\(.*\)stats[_]*!\1:\2!"
  177. # special vars for generating the list of files or updates
  178. found_lst=$(shell $(find_counter_files_cmd) | $(gen_file_grp_cmd))
  179. # filtered found lst
  180. f_found_lst=$(call filter_files,$(found_lst))
  181. diff_lst=$(filter-out $(flist),$(f_found_lst))
  182. get_core_files=$(filter-out $(COREPATH)/modules% $(COREPATH)/lib%,$(1))
  183. sort_files=$(sort $(call get_core_files,$(1)))\
  184. $(sort $(filter-out $(call get_core_files,$(1)),$(1)))
  185. # replace $(COREPATH) with the text "$(COREPATH)"
  186. subst_corepath=$(patsubst $(patsubst %/,%,$(COREPATH))/%,\$$(COREPATH)/%,$(1))
  187. # help will be the default rule (on-purpose since without having a patched
  188. # GCC:TranslationUnit module, make all won't work)
  189. .PHONY: help
  190. help:
  191. @echo "To regenerate $(foreach f,$(flist),$(call get_target,$f).{txt,xml})"
  192. @echo "type: $(MAKE) all ."
  193. @echo "or to regenerate all the counter lists by searching all"
  194. @echo " the source files for definitions, type: $(MAKE) autogen ."
  195. @echo "NOTE: you need the GCC:TranslationUnit perl module with an "
  196. @echo "extra patch applied (see $(CFG2TXT) --patch)."
  197. .PHONY: txt
  198. txt:
  199. .PHONY: docbook
  200. docbook:
  201. .PHONY: clean_txt
  202. clean_txt:
  203. .PHONY: clean_docbook
  204. clean_docbook:
  205. .PHONY: all
  206. all: txt $(docbook_output_dir)/counter_list.xml
  207. .PHONY: clean
  208. clean: clean_txt clean_docbook
  209. @rm -f $(docbook_output_dir)/counter_list.xml
  210. .PHONY: proper
  211. proper:
  212. @rm -f $(txt_output_dir)/counters_*.txt
  213. @rm -f $(docbook_output_dir)/counters_*.xml
  214. repo_ver="sip-router"\
  215. "git-$(shell git rev-parse --verify --short=6 HEAD 2>/dev/null)"
  216. ifeq ($(repo_ver),git-)
  217. repo_ver="sip-router unknown"
  218. endif
  219. $(docbook_output_dir)/counter_list.xml: Makefile \
  220. $(foreach f,$(flist),$(docbook_output_dir)/$(call get_target,$f).xml)
  221. @echo '<?xml version="1.0" encoding="UTF-8"?>' >$@
  222. @echo '<!-- this file is autogenerated, do not edit! -->' >>$@
  223. @echo '<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"' >>$@
  224. @echo ' "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd"' >>$@
  225. @echo ' [ <!ENTITY % local.common.attrib' >>$@
  226. @echo " \"xmlns:xi CDATA #FIXED 'http://www.w3.org/2001/XInclude'\">]">>$@
  227. @echo '>' >>$@
  228. @echo '<book id="counter_list"'\
  229. 'xmlns:xi="http://www.w3.org/2001/XInclude">' >>$@
  230. @echo ' <title>Counter List</title>' >>$@
  231. @echo ' <bookinfo>' >>$@
  232. @echo ' <productname class="trade">kamailio.org</productname>' >>$@
  233. @echo ' <authorgroup>' >>$@
  234. @echo ' <author>' >>$@
  235. @echo ' <firstname>Kamailio</firstname>' >>$@
  236. @echo ' <surname>Development Team</surname>' >>$@
  237. @echo ' <affiliation><orgname>https://www.kamailio.org</orgname></affiliation>' >>$@
  238. @echo ' <address>[email protected]</address>' >>$@
  239. @echo ' </author>' >>$@
  240. @echo ' </authorgroup>' >>$@
  241. @echo ' <copyright>' >>$@
  242. @echo ' <year>2008-2017</year>' >>$@
  243. @echo ' <holder>Kamailio Project</holder>' >>$@
  244. @echo ' </copyright>' >>$@
  245. @echo ' </bookinfo>' >>$@
  246. @$(foreach f,$(flist),\
  247. echo ' <xi:include'\
  248. 'href="'$(call get_target,$f).xml'"/>' \
  249. >>$@ ; )
  250. @echo '</book>' >>$@
  251. # finds all the files containing cfg defs
  252. .PHONY: find
  253. find:
  254. @$(find_counter_files_cmd)
  255. # print the list of the autogenerated files
  256. .PHONY: print-lst
  257. print-lst:
  258. @$(find_counter_files_cmd) | $(gen_file_grp_cmd)
  259. #
  260. .PHONY: gen-file-list
  261. .PHONY: gen-files_list
  262. .PHONY: gen_files_list
  263. gen-file-list gen-files-list gen_files_list:
  264. @$(foreach f,$(call subst_corepath,$(call sort_files,$(f_found_lst))),\
  265. echo "$f \\";)
  266. .PHONY: check-list
  267. .PHONY: update-list
  268. .PHONY: diff-list
  269. check-list update-list diff-list:
  270. @$(foreach f,$(call subst_corepath,$(call sort_files,$(diff_lst))),\
  271. echo "$f \\";)
  272. # try to generate the docs from all the sources
  273. .PHONY: autogen
  274. autogen:
  275. @$(MAKE) all files_list="$(call sort_files,$(f_found_lst))"
  276. $(foreach f,$(flist),$(eval $(call mk_rules,$(f))))