Makefile 11 KB

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