Makefile 11 KB

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