1# -*-Makefile-*-
2# This Makefile fragment tries to be general-purpose enough to be
3# used by many projects via the gnulib maintainer-makefile module.
4
5## Copyright (C) 2001-2016 Free Software Foundation, Inc.
6##
7## This program is free software: you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published by
9## the Free Software Foundation, either version 3 of the License, or
10## (at your option) any later version.
11##
12## This program is distributed in the hope that it will be useful,
13## but WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15## GNU General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20# This is reported not to work with make-3.79.1
21# ME := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
22ME := maint.mk
23
24# Helper variables.
25_empty =
26_sp = $(_empty) $(_empty)
27
28# _equal,S1,S2
29# ------------
30# If S1 == S2, return S1, otherwise the empty string.
31_equal = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))
32
33# member-check,VARIABLE,VALID-VALUES
34# ----------------------------------
35# Check that $(VARIABLE) is in the space-separated list of VALID-VALUES, and
36# return it.  Die otherwise.
37member-check =								\
38  $(strip								\
39    $(if $($(1)),							\
40      $(if $(findstring $(_sp),$($(1))),				\
41          $(error invalid $(1): '$($(1))', expected $(2)),		\
42          $(or $(findstring $(_sp)$($(1))$(_sp),$(_sp)$(2)$(_sp)),	\
43            $(error invalid $(1): '$($(1))', expected $(2)))),		\
44      $(error $(1) undefined)))
45
46# Do not save the original name or timestamp in the .tar.gz file.
47# Use --rsyncable if available.
48gzip_rsyncable := \
49  $(shell gzip --help 2>/dev/null|grep rsyncable >/dev/null \
50    && printf %s --rsyncable)
51GZIP_ENV = '--no-name --best $(gzip_rsyncable)'
52
53GIT = git
54VC = $(GIT)
55
56VC_LIST = $(srcdir)/$(_build-aux)/vc-list-files -C $(srcdir)
57
58# You can override this variable in cfg.mk to set your own regexp
59# matching files to ignore.
60VC_LIST_ALWAYS_EXCLUDE_REGEX ?= ^$$
61
62# This is to preprocess robustly the output of $(VC_LIST), so that even
63# when $(srcdir) is a pathological name like "....", the leading sed command
64# removes only the intended prefix.
65_dot_escaped_srcdir = $(subst .,\.,$(srcdir))
66
67# Post-process $(VC_LIST) output, prepending $(srcdir)/, but only
68# when $(srcdir) is not ".".
69ifeq ($(srcdir),.)
70  _prepend_srcdir_prefix =
71else
72  _prepend_srcdir_prefix = | $(SED) 's|^|$(srcdir)/|'
73endif
74
75# In order to be able to consistently filter "."-relative names,
76# (i.e., with no $(srcdir) prefix), this definition is careful to
77# remove any $(srcdir) prefix, and to restore what it removes.
78_sc_excl = \
79  $(or $(exclude_file_name_regexp--$@),^$$)
80VC_LIST_EXCEPT = \
81  $(VC_LIST) | $(SED) 's|^$(_dot_escaped_srcdir)/||' \
82	| if test -f $(srcdir)/.x-$@; then grep -vEf $(srcdir)/.x-$@; \
83	  else grep -Ev -e "$${VC_LIST_EXCEPT_DEFAULT-ChangeLog}"; fi \
84	| grep -Ev -e '($(VC_LIST_ALWAYS_EXCLUDE_REGEX)|$(_sc_excl))' \
85	$(_prepend_srcdir_prefix)
86
87ifeq ($(origin prev_version_file), undefined)
88  prev_version_file = $(srcdir)/.prev-version
89endif
90
91PREV_VERSION := $(shell cat $(prev_version_file) 2>/dev/null)
92VERSION_REGEXP = $(subst .,\.,$(VERSION))
93PREV_VERSION_REGEXP = $(subst .,\.,$(PREV_VERSION))
94
95ifeq ($(VC),$(GIT))
96  this-vc-tag = v$(VERSION)
97  this-vc-tag-regexp = v$(VERSION_REGEXP)
98else
99  tag-package = $(shell echo "$(PACKAGE)" | tr '[:lower:]' '[:upper:]')
100  tag-this-version = $(subst .,_,$(VERSION))
101  this-vc-tag = $(tag-package)-$(tag-this-version)
102  this-vc-tag-regexp = $(this-vc-tag)
103endif
104my_distdir = $(PACKAGE)-$(VERSION)
105
106# Old releases are stored here.
107release_archive_dir ?= ../release
108
109# If RELEASE_TYPE is undefined, but RELEASE is, use its second word.
110# But overwrite VERSION.
111ifdef RELEASE
112  VERSION := $(word 1, $(RELEASE))
113  RELEASE_TYPE ?= $(word 2, $(RELEASE))
114endif
115
116# Validate and return $(RELEASE_TYPE), or die.
117RELEASE_TYPES = alpha beta stable
118release-type = $(call member-check,RELEASE_TYPE,$(RELEASE_TYPES))
119
120# Override gnu_rel_host and url_dir_list in cfg.mk if these are not right.
121# Use alpha.gnu.org for alpha and beta releases.
122# Use ftp.gnu.org for stable releases.
123gnu_ftp_host-alpha = alpha.gnu.org
124gnu_ftp_host-beta = alpha.gnu.org
125gnu_ftp_host-stable = ftp.gnu.org
126gnu_rel_host ?= $(gnu_ftp_host-$(release-type))
127
128url_dir_list ?= $(if $(call _equal,$(gnu_rel_host),ftp.gnu.org),	\
129                     http://ftpmirror.gnu.org/$(PACKAGE),		\
130                     ftp://$(gnu_rel_host)/gnu/$(PACKAGE))
131
132# Override this in cfg.mk if you are using a different format in your
133# NEWS file.
134today = $(shell date +%Y-%m-%d)
135
136# Select which lines of NEWS are searched for $(news-check-regexp).
137# This is a sed line number spec.  The default says that we search
138# lines 1..10 of NEWS for $(news-check-regexp).
139# If you want to search only line 3 or only lines 20-22, use "3" or "20,22".
140news-check-lines-spec ?= 1,10
141news-check-regexp ?= '^\*.* $(VERSION_REGEXP) \($(today)\)'
142
143# Prevent programs like 'sort' from considering distinct strings to be equal.
144# Doing it here saves us from having to set LC_ALL elsewhere in this file.
145export LC_ALL = C
146
147## --------------- ##
148## Sanity checks.  ##
149## --------------- ##
150
151ifneq ($(_gl-Makefile),)
152_cfg_mk := $(wildcard $(srcdir)/cfg.mk)
153
154# Collect the names of rules starting with 'sc_'.
155syntax-check-rules := $(sort $(shell $(SED) -n \
156   's/^\(sc_[a-zA-Z0-9_-]*\):.*/\1/p' $(srcdir)/$(ME) $(_cfg_mk)))
157.PHONY: $(syntax-check-rules)
158
159ifeq ($(shell $(VC_LIST) >/dev/null 2>&1; echo $$?),0)
160  local-checks-available += $(syntax-check-rules)
161else
162  local-checks-available += no-vc-detected
163no-vc-detected:
164	@echo "No version control files detected; skipping syntax check"
165endif
166.PHONY: $(local-checks-available)
167
168# Arrange to print the name of each syntax-checking rule just before running it.
169$(syntax-check-rules): %: %.m
170sc_m_rules_ = $(patsubst %, %.m, $(syntax-check-rules))
171.PHONY: $(sc_m_rules_)
172$(sc_m_rules_):
173	@echo $(patsubst sc_%.m, %, $@)
174	@date +%s.%N > .sc-start-$(basename $@)
175
176# Compute and print the elapsed time for each syntax-check rule.
177sc_z_rules_ = $(patsubst %, %.z, $(syntax-check-rules))
178.PHONY: $(sc_z_rules_)
179$(sc_z_rules_): %.z: %
180	@end=$$(date +%s.%N);						\
181	start=$$(cat .sc-start-$*);					\
182	rm -f .sc-start-$*;						\
183	awk -v s=$$start -v e=$$end					\
184	  'END {printf "%.2f $(patsubst sc_%,%,$*)\n", e - s}' < /dev/null
185
186# The patsubst here is to replace each sc_% rule with its sc_%.z wrapper
187# that computes and prints elapsed time.
188local-check :=								\
189  $(patsubst sc_%, sc_%.z,						\
190    $(filter-out $(local-checks-to-skip), $(local-checks-available)))
191
192syntax-check: $(local-check)
193endif
194
195# _sc_search_regexp
196#
197# This macro searches for a given construct in the selected files and
198# then takes some action.
199#
200# Parameters (shell variables):
201#
202#  prohibit | require
203#
204#     Regular expression (ERE) denoting either a forbidden construct
205#     or a required construct.  Those arguments are exclusive.
206#
207#  exclude
208#
209#     Regular expression (ERE) denoting lines to ignore that matched
210#     a prohibit construct.  For example, this can be used to exclude
211#     comments that mention why the nearby code uses an alternative
212#     construct instead of the simpler prohibited construct.
213#
214#  in_vc_files | in_files
215#
216#     grep-E-style regexp selecting the files to check.  For in_vc_files,
217#     the regexp is used to select matching files from the list of all
218#     version-controlled files; for in_files, it's from the names printed
219#     by "find $(srcdir)".  When neither is specified, use all files that
220#     are under version control.
221#
222#  containing | non_containing
223#
224#     Select the files (non) containing strings matching this regexp.
225#     If both arguments are specified then CONTAINING takes
226#     precedence.
227#
228#  with_grep_options
229#
230#     Extra options for grep.
231#
232#  ignore_case
233#
234#     Ignore case.
235#
236#  halt
237#
238#     Message to display before to halting execution.
239#
240# Finally, you may exempt files based on an ERE matching file names.
241# For example, to exempt from the sc_space_tab check all files with the
242# .diff suffix, set this Make variable:
243#
244# exclude_file_name_regexp--sc_space_tab = \.diff$
245#
246# Note that while this functionality is mostly inherited via VC_LIST_EXCEPT,
247# when filtering by name via in_files, we explicitly filter out matching
248# names here as well.
249
250# Initialize each, so that envvar settings cannot interfere.
251export require =
252export prohibit =
253export exclude =
254export in_vc_files =
255export in_files =
256export containing =
257export non_containing =
258export halt =
259export with_grep_options =
260
261# By default, _sc_search_regexp does not ignore case.
262export ignore_case =
263_ignore_case = $$(test -n "$$ignore_case" && printf %s -i || :)
264
265define _sc_say_and_exit
266   dummy=; : so we do not need a semicolon before each use;		\
267   { printf '%s\n' "$(ME): $$msg" 1>&2; exit 1; };
268endef
269
270define _sc_search_regexp
271   dummy=; : so we do not need a semicolon before each use;		\
272									\
273   : Check arguments;							\
274   test -n "$$prohibit" && test -n "$$require"				\
275     && { msg='Cannot specify both prohibit and require'		\
276          $(_sc_say_and_exit) } || :;					\
277   test -z "$$prohibit" && test -z "$$require"				\
278     && { msg='Should specify either prohibit or require'		\
279          $(_sc_say_and_exit) } || :;					\
280   test -z "$$prohibit" && test -n "$$exclude"				\
281     && { msg='Use of exclude requires a prohibit pattern'		\
282          $(_sc_say_and_exit) } || :;					\
283   test -n "$$in_vc_files" && test -n "$$in_files"			\
284     && { msg='Cannot specify both in_vc_files and in_files'		\
285          $(_sc_say_and_exit) } || :;					\
286   test "x$$halt" != x							\
287     || { msg='halt not defined' $(_sc_say_and_exit) };			\
288									\
289   : Filter by file name;						\
290   if test -n "$$in_files"; then					\
291     files=$$(find $(srcdir) | grep -E "$$in_files"			\
292              | grep -Ev '$(_sc_excl)');				\
293   else									\
294     files=$$($(VC_LIST_EXCEPT));					\
295     if test -n "$$in_vc_files"; then					\
296       files=$$(echo "$$files" | grep -E "$$in_vc_files");		\
297     fi;								\
298   fi;									\
299									\
300   : Filter by content;							\
301   test -n "$$files" && test -n "$$containing"				\
302     && { files=$$(grep -l "$$containing" $$files); } || :;		\
303   test -n "$$files" && test -n "$$non_containing"			\
304     && { files=$$(grep -vl "$$non_containing" $$files); } || :;	\
305									\
306   : Check for the construct;						\
307   if test -n "$$files"; then						\
308     if test -n "$$prohibit"; then					\
309       grep $$with_grep_options $(_ignore_case) -nE "$$prohibit" $$files \
310         | grep -vE "$${exclude:-^$$}"					\
311         && { msg="$$halt" $(_sc_say_and_exit) } || :;			\
312     else								\
313       grep $$with_grep_options $(_ignore_case) -LE "$$require" $$files \
314           | grep .							\
315         && { msg="$$halt" $(_sc_say_and_exit) } || :;			\
316     fi									\
317   else :;								\
318   fi || :;
319endef
320
321sc_avoid_if_before_free:
322	@$(srcdir)/$(_build-aux)/useless-if-before-free			\
323		$(useless_free_options)					\
324	    $$($(VC_LIST_EXCEPT) | grep -v useless-if-before-free) &&	\
325	  { echo '$(ME): found useless "if" before "free" above' 1>&2;	\
326	    exit 1; } || :
327
328sc_cast_of_argument_to_free:
329	@prohibit='\<free *\( *\(' halt="don't cast free argument"	\
330	  $(_sc_search_regexp)
331
332sc_cast_of_x_alloc_return_value:
333	@prohibit='\*\) *x(m|c|re)alloc\>'				\
334	halt="don't cast x*alloc return value"				\
335	  $(_sc_search_regexp)
336
337sc_cast_of_alloca_return_value:
338	@prohibit='\*\) *alloca\>'					\
339	halt="don't cast alloca return value"				\
340	  $(_sc_search_regexp)
341
342sc_space_tab:
343	@prohibit='[ ]	'						\
344	halt='found SPACE-TAB sequence; remove the SPACE'		\
345	  $(_sc_search_regexp)
346
347# Don't use *scanf or the old ato* functions in "real" code.
348# They provide no error checking mechanism.
349# Instead, use strto* functions.
350sc_prohibit_atoi_atof:
351	@prohibit='\<([fs]?scanf|ato([filq]|ll)) *\('				\
352	halt='do not use *scan''f, ato''f, ato''i, ato''l, ato''ll or ato''q'	\
353	  $(_sc_search_regexp)
354
355# Use STREQ rather than comparing strcmp == 0, or != 0.
356sp_ = strcmp *\(.+\)
357sc_prohibit_strcmp:
358	@prohibit='! *strcmp *\(|\<$(sp_) *[!=]=|[!=]= *$(sp_)'		\
359	exclude='# *define STRN?EQ\('					\
360	halt='replace strcmp calls above with STREQ/STRNEQ'		\
361	  $(_sc_search_regexp)
362
363# Really.  You don't want to use this function.
364# It may fail to NUL-terminate the destination,
365# and always NUL-pads out to the specified length.
366sc_prohibit_strncpy:
367	@prohibit='\<strncpy *\('					\
368	halt='do not use strncpy, period'				\
369	  $(_sc_search_regexp)
370
371# Pass EXIT_*, not number, to usage, exit, and error (when exiting)
372# Convert all uses automatically, via these two commands:
373# git grep -l '\<exit *(1)' \
374#  | grep -vEf .x-sc_prohibit_magic_number_exit \
375#  | xargs --no-run-if-empty \
376#      perl -pi -e 's/(^|[^.])\b(exit ?)\(1\)/$1$2(EXIT_FAILURE)/'
377# git grep -l '\<exit *(0)' \
378#  | grep -vEf .x-sc_prohibit_magic_number_exit \
379#  | xargs --no-run-if-empty \
380#      perl -pi -e 's/(^|[^.])\b(exit ?)\(0\)/$1$2(EXIT_SUCCESS)/'
381sc_prohibit_magic_number_exit:
382	@prohibit='(^|[^.])\<(usage|exit|error) ?\(-?[0-9]+[,)]'	\
383	exclude='exit \(77\)|error ?\(((0|77),|[^,]*)'			\
384	halt='use EXIT_* values rather than magic number'		\
385	  $(_sc_search_regexp)
386
387# Using EXIT_SUCCESS as the first argument to error is misleading,
388# since when that parameter is 0, error does not exit.  Use '0' instead.
389sc_error_exit_success:
390	@prohibit='error *\(EXIT_SUCCESS,'				\
391	in_vc_files='\.[chly]$$'					\
392	halt='found error (EXIT_SUCCESS'				\
393	 $(_sc_search_regexp)
394
395# "FATAL:" should be fully upper-cased in error messages
396# "WARNING:" should be fully upper-cased, or fully lower-cased
397sc_error_message_warn_fatal:
398	@grep -nEA2 '[^rp]error *\(' $$($(VC_LIST_EXCEPT))		\
399	    | grep -E '"Warning|"Fatal|"fatal' &&			\
400	  { echo '$(ME): use FATAL, WARNING or warning'	1>&2;		\
401	    exit 1; } || :
402
403# Error messages should not start with a capital letter
404sc_error_message_uppercase:
405	@grep -nEA2 '[^rp]error *\(' $$($(VC_LIST_EXCEPT))		\
406	    | grep -E '"[A-Z]'						\
407	    | grep -vE '"FATAL|"WARNING|"Java|"C#|PRIuMAX' &&		\
408	  { echo '$(ME): found capitalized error message' 1>&2;		\
409	    exit 1; } || :
410
411# Error messages should not end with a period
412sc_error_message_period:
413	@grep -nEA2 '[^rp]error *\(' $$($(VC_LIST_EXCEPT))		\
414	    | grep -E '[^."]\."' &&					\
415	  { echo '$(ME): found error message ending in period' 1>&2;	\
416	    exit 1; } || :
417
418sc_file_system:
419	@prohibit=file''system						\
420	ignore_case=1							\
421	halt='found use of "file''system"; spell it "file system"'	\
422	  $(_sc_search_regexp)
423
424# Don't use cpp tests of this symbol.  All code assumes config.h is included.
425sc_prohibit_have_config_h:
426	@prohibit='^# *if.*HAVE''_CONFIG_H'				\
427	halt='found use of HAVE''_CONFIG_H; remove'			\
428	  $(_sc_search_regexp)
429
430# Nearly all .c files must include <config.h>.  However, we also permit this
431# via inclusion of a package-specific header, if cfg.mk specified one.
432# config_h_header must be suitable for grep -E.
433config_h_header ?= <config\.h>
434sc_require_config_h:
435	@require='^# *include $(config_h_header)'			\
436	in_vc_files='\.c$$'						\
437	halt='the above files do not include <config.h>'		\
438	  $(_sc_search_regexp)
439
440# Print each file name for which the first #include does not match
441# $(config_h_header).  Like grep -m 1, this only looks at the first match.
442perl_config_h_first_ =							\
443  -e 'BEGIN {$$ret = 0}'						\
444  -e 'if (/^\# *include\b/) {'						\
445  -e '  if (not m{^\# *include $(config_h_header)}) {'			\
446  -e '    print "$$ARGV\n";'						\
447  -e '    $$ret = 1;'							\
448  -e '  }'								\
449  -e '  \# Move on to next file after first include'			\
450  -e '  close ARGV;'							\
451  -e '}'								\
452  -e 'END {exit $$ret}'
453
454# You must include <config.h> before including any other header file.
455# This can possibly be via a package-specific header, if given by cfg.mk.
456sc_require_config_h_first:
457	@if $(VC_LIST_EXCEPT) | grep '\.c$$' > /dev/null; then		\
458	  files=$$($(VC_LIST_EXCEPT) | grep '\.c$$') &&			\
459	  perl -n $(perl_config_h_first_) $$files ||			\
460	    { echo '$(ME): the above files include some other header'	\
461		'before <config.h>' 1>&2; exit 1; } || :;		\
462	else :;								\
463	fi
464
465sc_prohibit_HAVE_MBRTOWC:
466	@prohibit='\bHAVE_MBRTOWC\b'					\
467	halt="do not use $$prohibit; it is always defined"		\
468	  $(_sc_search_regexp)
469
470# To use this "command" macro, you must first define two shell variables:
471# h: the header name, with no enclosing <> or ""
472# re: a regular expression that matches IFF something provided by $h is used.
473define _sc_header_without_use
474  dummy=; : so we do not need a semicolon before each use;		\
475  h_esc=`echo '[<"]'"$$h"'[">]'|$(SED) 's/\./\\\\./g'`;			\
476  if $(VC_LIST_EXCEPT) | grep '\.c$$' > /dev/null; then			\
477    files=$$(grep -l '^# *include '"$$h_esc"				\
478	     $$($(VC_LIST_EXCEPT) | grep '\.c$$')) &&			\
479    grep -LE "$$re" $$files | grep . &&					\
480      { echo "$(ME): the above files include $$h but don't use it"	\
481	1>&2; exit 1; } || :;						\
482  else :;								\
483  fi
484endef
485
486# Prohibit the inclusion of assert.h without an actual use of assert.
487sc_prohibit_assert_without_use:
488	@h='assert.h' re='\<assert *\(' $(_sc_header_without_use)
489
490# Prohibit the inclusion of close-stream.h without an actual use.
491sc_prohibit_close_stream_without_use:
492	@h='close-stream.h' re='\<close_stream *\(' $(_sc_header_without_use)
493
494# Prohibit the inclusion of getopt.h without an actual use.
495sc_prohibit_getopt_without_use:
496	@h='getopt.h' re='\<getopt(_long)? *\(' $(_sc_header_without_use)
497
498# Don't include quotearg.h unless you use one of its functions.
499sc_prohibit_quotearg_without_use:
500	@h='quotearg.h' re='\<quotearg(_[^ ]+)? *\(' $(_sc_header_without_use)
501
502# Don't include quote.h unless you use one of its functions.
503sc_prohibit_quote_without_use:
504	@h='quote.h' re='\<quote((_n)? *\(|_quoting_options\>)' \
505	  $(_sc_header_without_use)
506
507# Don't include this header unless you use one of its functions.
508sc_prohibit_long_options_without_use:
509	@h='long-options.h' re='\<parse_long_options *\(' \
510	  $(_sc_header_without_use)
511
512# Don't include this header unless you use one of its functions.
513sc_prohibit_inttostr_without_use:
514	@h='inttostr.h' re='\<(off|[iu]max|uint)tostr *\(' \
515	  $(_sc_header_without_use)
516
517# Don't include this header unless you use one of its functions.
518sc_prohibit_ignore_value_without_use:
519	@h='ignore-value.h' re='\<ignore_(value|ptr) *\(' \
520	  $(_sc_header_without_use)
521
522# Don't include this header unless you use one of its functions.
523sc_prohibit_error_without_use:
524	@h='error.h' \
525	re='\<error(_at_line|_print_progname|_one_per_line|_message_count)? *\('\
526	  $(_sc_header_without_use)
527
528# Don't include xalloc.h unless you use one of its functions.
529# Consider these symbols:
530# perl -lne '/^# *define (\w+)\(/ and print $1' lib/xalloc.h|grep -v '^__';
531# perl -lne '/^(?:extern )?(?:void|char) \*?(\w+) *\(/ and print $1' lib/xalloc.h
532# Divide into two sets on case, and filter each through this:
533# | sort | perl -MRegexp::Assemble -le \
534#  'print Regexp::Assemble->new(file => "/dev/stdin")->as_string'|sed 's/\?://g'
535# Note this was produced by the above:
536# _xa1 = \
537#x(((2n?)?re|c(har)?|n(re|m)|z)alloc|alloc_(oversized|die)|m(alloc|emdup)|strdup)
538# But we can do better, in at least two ways:
539# 1) take advantage of two "dup"-suffixed strings:
540# x(((2n?)?re|c(har)?|n(re|m)|[mz])alloc|alloc_(oversized|die)|(mem|str)dup)
541# 2) notice that "c(har)?|[mz]" is equivalent to the shorter and more readable
542# "char|[cmz]"
543# x(((2n?)?re|char|n(re|m)|[cmz])alloc|alloc_(oversized|die)|(mem|str)dup)
544_xa1 = x(((2n?)?re|char|n(re|m)|[cmz])alloc|alloc_(oversized|die)|(mem|str)dup)
545_xa2 = X([CZ]|N?M)ALLOC
546sc_prohibit_xalloc_without_use:
547	@h='xalloc.h' \
548	re='\<($(_xa1)|$(_xa2)) *\('\
549	  $(_sc_header_without_use)
550
551# Extract function names:
552# perl -lne '/^(?:extern )?(?:void|char) \*?(\w+) *\(/ and print $1' lib/hash.h
553_hash_re = \
554clear|delete|free|get_(first|next)|insert|lookup|print_statistics|reset_tuning
555_hash_fn = \<($(_hash_re)) *\(
556_hash_struct = (struct )?\<[Hh]ash_(table|tuning)\>
557sc_prohibit_hash_without_use:
558	@h='hash.h' \
559	re='$(_hash_fn)|$(_hash_struct)'\
560	  $(_sc_header_without_use)
561
562sc_prohibit_cloexec_without_use:
563	@h='cloexec.h' re='\<(set_cloexec_flag|dup_cloexec) *\(' \
564	  $(_sc_header_without_use)
565
566sc_prohibit_posixver_without_use:
567	@h='posixver.h' re='\<posix2_version *\(' $(_sc_header_without_use)
568
569sc_prohibit_same_without_use:
570	@h='same.h' re='\<same_name *\(' $(_sc_header_without_use)
571
572sc_prohibit_hash_pjw_without_use:
573	@h='hash-pjw.h' \
574	re='\<hash_pjw\>' \
575	  $(_sc_header_without_use)
576
577sc_prohibit_safe_read_without_use:
578	@h='safe-read.h' re='(\<SAFE_READ_ERROR\>|\<safe_read *\()' \
579	  $(_sc_header_without_use)
580
581sc_prohibit_argmatch_without_use:
582	@h='argmatch.h' \
583	re='(\<(ARRAY_CARDINALITY|X?ARGMATCH(|_TO_ARGUMENT|_VERIFY))\>|\<(invalid_arg|argmatch(_exit_fn|_(in)?valid)?) *\()' \
584	  $(_sc_header_without_use)
585
586sc_prohibit_canonicalize_without_use:
587	@h='canonicalize.h' \
588	re='CAN_(EXISTING|ALL_BUT_LAST|MISSING)|canonicalize_(mode_t|filename_mode|file_name)' \
589	  $(_sc_header_without_use)
590
591sc_prohibit_root_dev_ino_without_use:
592	@h='root-dev-ino.h' \
593	re='(\<ROOT_DEV_INO_(CHECK|WARN)\>|\<get_root_dev_ino *\()' \
594	  $(_sc_header_without_use)
595
596sc_prohibit_openat_without_use:
597	@h='openat.h' \
598	re='\<(openat_(permissive|needs_fchdir|(save|restore)_fail)|l?(stat|ch(own|mod))at|(euid)?accessat|(FCHMOD|FCHOWN|STAT)AT_INLINE)\>' \
599	  $(_sc_header_without_use)
600
601# Prohibit the inclusion of c-ctype.h without an actual use.
602ctype_re = isalnum|isalpha|isascii|isblank|iscntrl|isdigit|isgraph|islower\
603|isprint|ispunct|isspace|isupper|isxdigit|tolower|toupper
604sc_prohibit_c_ctype_without_use:
605	@h='c-ctype.h' re='\<c_($(ctype_re)) *\(' \
606	  $(_sc_header_without_use)
607
608# The following list was generated by running:
609# man signal.h|col -b|perl -ne '/bsd_signal.*;/.../sigwaitinfo.*;/ and print' \
610#   | perl -lne '/^\s+(?:int|void).*?(\w+).*/ and print $1' | fmt
611_sig_functions = \
612  bsd_signal kill killpg pthread_kill pthread_sigmask raise sigaction \
613  sigaddset sigaltstack sigdelset sigemptyset sigfillset sighold sigignore \
614  siginterrupt sigismember signal sigpause sigpending sigprocmask sigqueue \
615  sigrelse sigset sigsuspend sigtimedwait sigwait sigwaitinfo
616_sig_function_re = $(subst $(_sp),|,$(strip $(_sig_functions)))
617# The following were extracted from "man signal.h" manually.
618_sig_types_and_consts =							\
619  MINSIGSTKSZ SA_NOCLDSTOP SA_NOCLDWAIT SA_NODEFER SA_ONSTACK		\
620  SA_RESETHAND SA_RESTART SA_SIGINFO SIGEV_NONE SIGEV_SIGNAL		\
621  SIGEV_THREAD SIGSTKSZ SIG_BLOCK SIG_SETMASK SIG_UNBLOCK SS_DISABLE	\
622  SS_ONSTACK mcontext_t pid_t sig_atomic_t sigevent siginfo_t sigset_t	\
623  sigstack sigval stack_t ucontext_t
624# generated via this:
625# perl -lne '/^#ifdef (SIG\w+)/ and print $1' lib/sig2str.c|sort -u|fmt -70
626_sig_names =								\
627  SIGABRT SIGALRM SIGALRM1 SIGBUS SIGCANCEL SIGCHLD SIGCLD SIGCONT	\
628  SIGDANGER SIGDIL SIGEMT SIGFPE SIGFREEZE SIGGRANT SIGHUP SIGILL	\
629  SIGINFO SIGINT SIGIO SIGIOT SIGKAP SIGKILL SIGKILLTHR SIGLOST SIGLWP	\
630  SIGMIGRATE SIGMSG SIGPHONE SIGPIPE SIGPOLL SIGPRE SIGPROF SIGPWR	\
631  SIGQUIT SIGRETRACT SIGSAK SIGSEGV SIGSOUND SIGSTKFLT SIGSTOP SIGSYS	\
632  SIGTERM SIGTHAW SIGTRAP SIGTSTP SIGTTIN SIGTTOU SIGURG SIGUSR1	\
633  SIGUSR2 SIGVIRT SIGVTALRM SIGWAITING SIGWINCH SIGWIND SIGWINDOW	\
634  SIGXCPU SIGXFSZ
635_sig_syms_re = $(subst $(_sp),|,$(strip $(_sig_names) $(_sig_types_and_consts)))
636
637# Prohibit the inclusion of signal.h without an actual use.
638sc_prohibit_signal_without_use:
639	@h='signal.h'							\
640	re='\<($(_sig_function_re)) *\(|\<($(_sig_syms_re))\>'		\
641	  $(_sc_header_without_use)
642
643# Don't include stdio--.h unless you use one of its functions.
644sc_prohibit_stdio--_without_use:
645	@h='stdio--.h' re='\<((f(re)?|p)open|tmpfile) *\('		\
646	  $(_sc_header_without_use)
647
648# Don't include stdio-safer.h unless you use one of its functions.
649sc_prohibit_stdio-safer_without_use:
650	@h='stdio-safer.h' re='\<((f(re)?|p)open|tmpfile)_safer *\('	\
651	  $(_sc_header_without_use)
652
653# Prohibit the inclusion of strings.h without a sensible use.
654# Using the likes of bcmp, bcopy, bzero, index or rindex is not sensible.
655sc_prohibit_strings_without_use:
656	@h='strings.h'							\
657	re='\<(strn?casecmp|ffs(ll)?)\>'				\
658	  $(_sc_header_without_use)
659
660# Get the list of symbol names with this:
661# perl -lne '/^# *define ([A-Z]\w+)\(/ and print $1' lib/intprops.h|fmt
662_intprops_names =							\
663  TYPE_IS_INTEGER TYPE_SIGNED TYPE_MINIMUM TYPE_MAXIMUM			\
664  INT_BITS_STRLEN_BOUND INT_STRLEN_BOUND INT_BUFSIZE_BOUND		\
665  INT_ADD_RANGE_OVERFLOW INT_SUBTRACT_RANGE_OVERFLOW			\
666  INT_NEGATE_RANGE_OVERFLOW INT_MULTIPLY_RANGE_OVERFLOW			\
667  INT_DIVIDE_RANGE_OVERFLOW INT_REMAINDER_RANGE_OVERFLOW		\
668  INT_LEFT_SHIFT_RANGE_OVERFLOW INT_ADD_OVERFLOW INT_SUBTRACT_OVERFLOW	\
669  INT_NEGATE_OVERFLOW INT_MULTIPLY_OVERFLOW INT_DIVIDE_OVERFLOW		\
670  INT_REMAINDER_OVERFLOW INT_LEFT_SHIFT_OVERFLOW
671_intprops_syms_re = $(subst $(_sp),|,$(strip $(_intprops_names)))
672# Prohibit the inclusion of intprops.h without an actual use.
673sc_prohibit_intprops_without_use:
674	@h='intprops.h'							\
675	re='\<($(_intprops_syms_re)) *\('				\
676	  $(_sc_header_without_use)
677
678_stddef_syms_re = NULL|offsetof|ptrdiff_t|size_t|wchar_t
679# Prohibit the inclusion of stddef.h without an actual use.
680sc_prohibit_stddef_without_use:
681	@h='stddef.h'							\
682	re='\<($(_stddef_syms_re))\>'					\
683	  $(_sc_header_without_use)
684
685_de1 = dirfd|(close|(fd)?open|read|rewind|seek|tell)dir(64)?(_r)?
686_de2 = (versionsort|struct dirent|getdirentries|alphasort|scandir(at)?)(64)?
687_de3 = MAXNAMLEN|DIR|ino_t|d_ino|d_fileno|d_namlen
688_dirent_syms_re = $(_de1)|$(_de2)|$(_de3)
689# Prohibit the inclusion of dirent.h without an actual use.
690sc_prohibit_dirent_without_use:
691	@h='dirent.h'							\
692	re='\<($(_dirent_syms_re))\>'					\
693	  $(_sc_header_without_use)
694
695# Prohibit the inclusion of verify.h without an actual use.
696sc_prohibit_verify_without_use:
697	@h='verify.h'							\
698	re='\<(verify(true|expr)?|static_assert) *\('			\
699	  $(_sc_header_without_use)
700
701# Don't include xfreopen.h unless you use one of its functions.
702sc_prohibit_xfreopen_without_use:
703	@h='xfreopen.h' re='\<xfreopen *\(' $(_sc_header_without_use)
704
705sc_obsolete_symbols:
706	@prohibit='\<(HAVE''_FCNTL_H|O''_NDELAY)\>'			\
707	halt='do not use HAVE''_FCNTL_H or O'_NDELAY			\
708	  $(_sc_search_regexp)
709
710# FIXME: warn about definitions of EXIT_FAILURE, EXIT_SUCCESS, STREQ
711
712# Each nonempty ChangeLog line must start with a year number, or a TAB.
713sc_changelog:
714	@prohibit='^[^12	]'					\
715	in_vc_files='^ChangeLog$$'					\
716	halt='found unexpected prefix in a ChangeLog'			\
717	  $(_sc_search_regexp)
718
719# Ensure that each .c file containing a "main" function also
720# calls set_program_name.
721sc_program_name:
722	@require='set_program_name *\(.*\);'				\
723	in_vc_files='\.c$$'						\
724	containing='\<main *('						\
725	halt='the above files do not call set_program_name'		\
726	  $(_sc_search_regexp)
727
728# Ensure that each .c file containing a "main" function also
729# calls bindtextdomain.
730sc_bindtextdomain:
731	@require='bindtextdomain *\('					\
732	in_vc_files='\.c$$'						\
733	containing='\<main *('						\
734	halt='the above files do not call bindtextdomain'		\
735	  $(_sc_search_regexp)
736
737# Require that the final line of each test-lib.sh-using test be this one:
738# Exit $fail
739# Note: this test requires GNU grep's --label= option.
740Exit_witness_file ?= tests/test-lib.sh
741Exit_base := $(notdir $(Exit_witness_file))
742sc_require_test_exit_idiom:
743	@if test -f $(srcdir)/$(Exit_witness_file); then		\
744	  die=0;							\
745	  for i in $$(grep -l -F 'srcdir/$(Exit_base)'			\
746		$$($(VC_LIST) tests)); do				\
747	    tail -n1 $$i | grep '^Exit .' > /dev/null			\
748	      && : || { die=1; echo $$i; }				\
749	  done;								\
750	  test $$die = 1 &&						\
751	    { echo 1>&2 '$(ME): the final line in each of the above is not:'; \
752	      echo 1>&2 'Exit something';				\
753	      exit 1; } || :;						\
754	fi
755
756sc_trailing_blank:
757	@prohibit='[	 ]$$'						\
758	halt='found trailing blank(s)'					\
759	exclude='^Binary file .* matches$$'				\
760	  $(_sc_search_regexp)
761
762# Match lines like the following, but where there is only one space
763# between the options and the description:
764#   -D, --all-repeated[=delimit-method]  print all duplicate lines\n
765longopt_re = --[a-z][0-9A-Za-z-]*(\[?=[0-9A-Za-z-]*\]?)?
766sc_two_space_separator_in_usage:
767	@prohibit='^   *(-[A-Za-z],)? $(longopt_re) [^ ].*\\$$'		\
768	halt='help2man requires at least two spaces between an option and its description'\
769	  $(_sc_search_regexp)
770
771# A regexp matching function names like "error" that may be used
772# to emit translatable messages.
773_gl_translatable_diag_func_re ?= error
774
775# Look for diagnostics that aren't marked for translation.
776# This won't find any for which error's format string is on a separate line.
777sc_unmarked_diagnostics:
778	@prohibit='\<$(_gl_translatable_diag_func_re) *\([^"]*"[^"]*[a-z]{3}' \
779	exclude='(_|ngettext ?)\('					\
780	halt='found unmarked diagnostic(s)'				\
781	  $(_sc_search_regexp)
782
783# Avoid useless parentheses like those in this example:
784# #if defined (SYMBOL) || defined (SYM2)
785sc_useless_cpp_parens:
786	@prohibit='^# *if .*defined *\('				\
787	halt='found useless parentheses in cpp directive'		\
788	  $(_sc_search_regexp)
789
790# List headers for which HAVE_HEADER_H is always true, assuming you are
791# using the appropriate gnulib module.  CAUTION: for each "unnecessary"
792# #if HAVE_HEADER_H that you remove, be sure that your project explicitly
793# requires the gnulib module that guarantees the usability of that header.
794gl_assured_headers_ = \
795  cd $(gnulib_dir)/lib && echo *.in.h|$(SED) 's/\.in\.h//g'
796
797# Convert the list of names to upper case, and replace each space with "|".
798az_ = abcdefghijklmnopqrstuvwxyz
799AZ_ = ABCDEFGHIJKLMNOPQRSTUVWXYZ
800gl_header_upper_case_or_ =						\
801  $$($(gl_assured_headers_)						\
802    | tr $(az_)/.- $(AZ_)___						\
803    | tr -s ' ' '|'							\
804    )
805sc_prohibit_always_true_header_tests:
806	@or=$(gl_header_upper_case_or_);				\
807	re="HAVE_($$or)_H";						\
808	prohibit='\<'"$$re"'\>'						\
809	halt=$$(printf '%s\n'						\
810	'do not test the above HAVE_<header>_H symbol(s);'		\
811	'  with the corresponding gnulib module, they are always true')	\
812	  $(_sc_search_regexp)
813
814sc_prohibit_defined_have_decl_tests:
815	@prohibit='(#[	 ]*ifn?def|\<defined)\>[	 (]+HAVE_DECL_'	\
816	halt='HAVE_DECL macros are always defined'			\
817	  $(_sc_search_regexp)
818
819# ==================================================================
820gl_other_headers_ ?= \
821  intprops.h	\
822  openat.h	\
823  stat-macros.h
824
825# Perl -lne code to extract "significant" cpp-defined symbols from a
826# gnulib header file, eliminating a few common false-positives.
827# The exempted names below are defined only conditionally in gnulib,
828# and hence sometimes must/may be defined in application code.
829gl_extract_significant_defines_ = \
830  /^\# *define ([^_ (][^ (]*)(\s*\(|\s+\w+)/\
831    && $$2 !~ /(?:rpl_|_used_without_)/\
832    && $$1 !~ /^(?:NSIG|ENODATA)$$/\
833    && $$1 !~ /^(?:SA_RESETHAND|SA_RESTART)$$/\
834    and print $$1
835
836# Create a list of regular expressions matching the names
837# of macros that are guaranteed to be defined by parts of gnulib.
838define def_sym_regex
839	gen_h=$(gl_generated_headers_);					\
840	(cd $(gnulib_dir)/lib;						\
841	  for f in *.in.h $(gl_other_headers_); do			\
842	    test -f $$f							\
843	      && perl -lne '$(gl_extract_significant_defines_)' $$f;	\
844	  done;								\
845	) | sort -u							\
846	  | $(SED) 's/^/^ *# *(define|undef)  */;s/$$/\\>/'
847endef
848
849# Don't define macros that we already get from gnulib header files.
850sc_prohibit_always-defined_macros:
851	@if test -d $(gnulib_dir); then					\
852	  case $$(echo all: | grep -l -f - Makefile) in Makefile);; *)	\
853	    echo '$(ME): skipping $@: you lack GNU grep' 1>&2; exit 0;;	\
854	  esac;								\
855	  $(def_sym_regex) | grep -E -f - $$($(VC_LIST_EXCEPT))		\
856	    && { echo '$(ME): define the above via some gnulib .h file'	\
857		  1>&2;  exit 1; } || :;				\
858	fi
859# ==================================================================
860
861# Prohibit checked in backup files.
862sc_prohibit_backup_files:
863	@$(VC_LIST) | grep '~$$' &&				\
864	  { echo '$(ME): found version controlled backup file' 1>&2;	\
865	    exit 1; } || :
866
867# Require the latest GPL.
868sc_GPL_version:
869	@prohibit='either ''version [^3]'				\
870	halt='GPL vN, N!=3'						\
871	  $(_sc_search_regexp)
872
873# Require the latest GFDL.  Two regexp, since some .texi files end up
874# line wrapping between 'Free Documentation License,' and 'Version'.
875_GFDL_regexp = (Free ''Documentation.*Version 1\.[^3]|Version 1\.[^3] or any)
876sc_GFDL_version:
877	@prohibit='$(_GFDL_regexp)'					\
878	halt='GFDL vN, N!=3'						\
879	  $(_sc_search_regexp)
880
881# Don't use Texinfo's @acronym{}.
882# http://lists.gnu.org/archive/html/bug-gnulib/2010-03/msg00321.html
883texinfo_suffix_re_ ?= \.(txi|texi(nfo)?)$$
884sc_texinfo_acronym:
885	@prohibit='@acronym\{'						\
886	in_vc_files='$(texinfo_suffix_re_)'				\
887	halt='found use of Texinfo @acronym{}'				\
888	  $(_sc_search_regexp)
889
890cvs_keywords = \
891  Author|Date|Header|Id|Name|Locker|Log|RCSfile|Revision|Source|State
892
893sc_prohibit_cvs_keyword:
894	@prohibit='\$$($(cvs_keywords))\$$'				\
895	halt='do not use CVS keyword expansion'				\
896	  $(_sc_search_regexp)
897
898# This Perl code is slightly obfuscated.  Not only is each "$" doubled
899# because it's in a Makefile, but the $$c's are comments;  we cannot
900# use "#" due to the way the script ends up concatenated onto one line.
901# It would be much more concise, and would produce better output (including
902# counts) if written as:
903#   perl -ln -0777 -e '/\n(\n+)$/ and print "$ARGV: ".length $1' ...
904# but that would be far less efficient, reading the entire contents
905# of each file, rather than just the last two bytes of each.
906# In addition, while the code below detects both blank lines and a missing
907# newline at EOF, the above detects only the former.
908#
909# This is a perl script that is expected to be the single-quoted argument
910# to a command-line "-le".  The remaining arguments are file names.
911# Print the name of each file that does not end in exactly one newline byte.
912# I.e., warn if there are blank lines (2 or more newlines), or if the
913# last byte is not a newline.  However, currently we don't complain
914# about any file that contains exactly one byte.
915# Exit nonzero if at least one such file is found, otherwise, exit 0.
916# Warn about, but otherwise ignore open failure.  Ignore seek/read failure.
917#
918# Use this if you want to remove trailing empty lines from selected files:
919#   perl -pi -0777 -e 's/\n\n+$/\n/' files...
920#
921require_exactly_one_NL_at_EOF_ =					\
922  foreach my $$f (@ARGV)						\
923    {									\
924      open F, "<", $$f or (warn "failed to open $$f: $$!\n"), next;	\
925      my $$p = sysseek (F, -2, 2);					\
926      my $$c = "seek failure probably means file has < 2 bytes; ignore"; \
927      my $$last_two_bytes;						\
928      defined $$p and $$p = sysread F, $$last_two_bytes, 2;		\
929      close F;								\
930      $$c = "ignore read failure";					\
931      $$p && ($$last_two_bytes eq "\n\n"				\
932              || substr ($$last_two_bytes,1) ne "\n")			\
933          and (print $$f), $$fail=1;					\
934    }									\
935  END { exit defined $$fail }
936sc_prohibit_empty_lines_at_EOF:
937	@perl -le '$(require_exactly_one_NL_at_EOF_)' $$($(VC_LIST_EXCEPT)) \
938	  || { echo '$(ME): empty line(s) or no newline at EOF'		\
939		1>&2; exit 1; } || :
940
941# Make sure we don't use st_blocks.  Use ST_NBLOCKS instead.
942# This is a bit of a kludge, since it prevents use of the string
943# even in comments, but for now it does the job with no false positives.
944sc_prohibit_stat_st_blocks:
945	@prohibit='[.>]st_blocks'					\
946	halt='do not use st_blocks; use ST_NBLOCKS'			\
947	  $(_sc_search_regexp)
948
949# Make sure we don't define any S_IS* macros in src/*.c files.
950# They're already defined via gnulib's sys/stat.h replacement.
951sc_prohibit_S_IS_definition:
952	@prohibit='^ *# *define  *S_IS'					\
953	halt='do not define S_IS* macros; include <sys/stat.h>'		\
954	  $(_sc_search_regexp)
955
956# Perl block to convert a match to FILE_NAME:LINENO:TEST,
957# that is shared by two definitions below.
958perl_filename_lineno_text_ =						\
959    -e '  {'								\
960    -e '    $$n = ($$` =~ tr/\n/\n/ + 1);'				\
961    -e '    ($$v = $$&) =~ s/\n/\\n/g;'					\
962    -e '    print "$$ARGV:$$n:$$v\n";'					\
963    -e '  }'
964
965prohibit_doubled_words_ = \
966    the then in an on if is it but for or at and do to
967# expand the regex before running the check to avoid using expensive captures
968prohibit_doubled_word_expanded_ = \
969    $(join $(prohibit_doubled_words_),$(addprefix \s+,$(prohibit_doubled_words_)))
970prohibit_doubled_word_RE_ ?= \
971    /\b(?:$(subst $(_sp),|,$(prohibit_doubled_word_expanded_)))\b/gims
972prohibit_doubled_word_ =						\
973    -e 'while ($(prohibit_doubled_word_RE_))'				\
974    $(perl_filename_lineno_text_)
975
976# Define this to a regular expression that matches
977# any filename:dd:match lines you want to ignore.
978# The default is to ignore no matches.
979ignore_doubled_word_match_RE_ ?= ^$$
980
981sc_prohibit_doubled_word:
982	@perl -n -0777 $(prohibit_doubled_word_) $$($(VC_LIST_EXCEPT))	\
983	  | grep -vE '$(ignore_doubled_word_match_RE_)'			\
984	  | grep . && { echo '$(ME): doubled words' 1>&2; exit 1; } || :
985
986# A regular expression matching undesirable combinations of words like
987# "can not"; this matches them even when the two words appear on different
988# lines, but not when there is an intervening delimiter like "#" or "*".
989# Similarly undesirable, "See @xref{...}", since an @xref should start
990# a sentence.  Explicitly prohibit any prefix of "see" or "also".
991# Also prohibit a prefix matching "\w+ +".
992# @pxref gets the same see/also treatment and should be parenthesized;
993# presume it must *not* start a sentence.
994bad_xref_re_ ?= (?:[\w,:;] +|(?:see|also)\s+)\@xref\{
995bad_pxref_re_ ?= (?:[.!?]|(?:see|also))\s+\@pxref\{
996prohibit_undesirable_word_seq_RE_ ?=					\
997  /(?:\bcan\s+not\b|$(bad_xref_re_)|$(bad_pxref_re_))/gims
998prohibit_undesirable_word_seq_ =					\
999    -e 'while ($(prohibit_undesirable_word_seq_RE_))'			\
1000    $(perl_filename_lineno_text_)
1001# Define this to a regular expression that matches
1002# any filename:dd:match lines you want to ignore.
1003# The default is to ignore no matches.
1004ignore_undesirable_word_sequence_RE_ ?= ^$$
1005
1006sc_prohibit_undesirable_word_seq:
1007	@perl -n -0777 $(prohibit_undesirable_word_seq_)		\
1008	     $$($(VC_LIST_EXCEPT))					\
1009	  | grep -vE '$(ignore_undesirable_word_sequence_RE_)' | grep .	\
1010	  && { echo '$(ME): undesirable word sequence' >&2; exit 1; } || :
1011
1012# Except for shell files and for loops, double semicolon is probably a mistake
1013sc_prohibit_double_semicolon:
1014	@prohibit='; *;[	{} \]*(/[/*]|$$)'			\
1015	in_vc_files='\.[chly]$$'					\
1016	exclude='\bfor *\(.*\)'						\
1017	halt="Double semicolon detected"				\
1018	  $(_sc_search_regexp)
1019
1020_ptm1 = use "test C1 && test C2", not "test C1 -''a C2"
1021_ptm2 = use "test C1 || test C2", not "test C1 -''o C2"
1022# Using test's -a and -o operators is not portable.
1023# We prefer test over [, since the latter is spelled [[ in configure.ac.
1024sc_prohibit_test_minus_ao:
1025	@prohibit='(\<test| \[+) .+ -[ao] '				\
1026	halt='$(_ptm1); $(_ptm2)'					\
1027	  $(_sc_search_regexp)
1028
1029# Avoid a test bashism.
1030sc_prohibit_test_double_equal:
1031	@prohibit='(\<test| \[+) .+ == '				\
1032	containing='#! */bin/[a-z]*sh'					\
1033	halt='use "test x = x", not "test x =''= x"'			\
1034	  $(_sc_search_regexp)
1035
1036# Each program that uses proper_name_utf8 must link with one of the
1037# ICONV libraries.  Otherwise, some ICONV library must appear in LDADD.
1038# The perl -0777 invocation below extracts the possibly-multi-line
1039# definition of LDADD from the appropriate Makefile.am and exits 0
1040# when it contains "ICONV".
1041sc_proper_name_utf8_requires_ICONV:
1042	@progs=$$(grep -l 'proper_name_utf8 ''("' $$($(VC_LIST_EXCEPT)));\
1043	if test "x$$progs" != x; then					\
1044	  fail=0;							\
1045	  for p in $$progs; do						\
1046	    dir=$$(dirname "$$p");					\
1047	    perl -0777							\
1048	      -ne 'exit !(/^LDADD =(.+?[^\\]\n)/ms && $$1 =~ /ICONV/)'	\
1049	      $$dir/Makefile.am && continue;				\
1050	    base=$$(basename "$$p" .c);					\
1051	    grep "$${base}_LDADD.*ICONV)" $$dir/Makefile.am > /dev/null	\
1052	      || { fail=1; echo 1>&2 "$(ME): $$p uses proper_name_utf8"; }; \
1053	  done;								\
1054	  test $$fail = 1 &&						\
1055	    { echo 1>&2 '$(ME): the above do not link with any ICONV library'; \
1056	      exit 1; } || :;						\
1057	fi
1058
1059# Warn about "c0nst struct Foo const foo[]",
1060# but not about "char const *const foo" or "#define const const".
1061sc_redundant_const:
1062	@prohibit='\bconst\b[[:space:][:alnum:]]{2,}\bconst\b'		\
1063	halt='redundant "const" in declarations'			\
1064	  $(_sc_search_regexp)
1065
1066sc_const_long_option:
1067	@prohibit='^ *static.*struct option '				\
1068	exclude='const struct option|struct option const'		\
1069	halt='add "const" to the above declarations'			\
1070	  $(_sc_search_regexp)
1071
1072NEWS_hash =								\
1073  $$($(SED) -n '/^\*.* $(PREV_VERSION_REGEXP) ([0-9-]*)/,$$p'		\
1074       $(srcdir)/NEWS							\
1075     | perl -0777 -pe							\
1076	's/^Copyright.+?Free\sSoftware\sFoundation,\sInc\.\n//ms'	\
1077     | md5sum -								\
1078     | $(SED) 's/ .*//')
1079
1080# Ensure that we don't accidentally insert an entry into an old NEWS block.
1081sc_immutable_NEWS:
1082	@if test -f $(srcdir)/NEWS; then				\
1083	  test "$(NEWS_hash)" = '$(old_NEWS_hash)' && : ||		\
1084	    { echo '$(ME): you have modified old NEWS' 1>&2; exit 1; };	\
1085	fi
1086
1087# Update the hash stored above.  Do this after each release and
1088# for any corrections to old entries.
1089update-NEWS-hash: NEWS
1090	perl -pi -e 's/^(old_NEWS_hash[ \t]+:?=[ \t]+).*/$${1}'"$(NEWS_hash)/" \
1091	  $(srcdir)/cfg.mk
1092
1093# Ensure that we use only the standard $(VAR) notation,
1094# not @...@ in Makefile.am, now that we can rely on automake
1095# to emit a definition for each substituted variable.
1096# However, there is still one case in which @VAR@ use is not just
1097# legitimate, but actually required: when augmenting an automake-defined
1098# variable with a prefix.  For example, gettext uses this:
1099# MAKEINFO = env LANG= LC_MESSAGES= LC_ALL= LANGUAGE= @MAKEINFO@
1100# otherwise, makeinfo would put German or French (current locale)
1101# navigation hints in the otherwise-English documentation.
1102#
1103# Allow the package to add exceptions via a hook in cfg.mk;
1104# for example, @PRAGMA_SYSTEM_HEADER@ can be permitted by
1105# setting this to ' && !/PRAGMA_SYSTEM_HEADER/'.
1106_makefile_at_at_check_exceptions ?=
1107sc_makefile_at_at_check:
1108	@perl -ne '/\@\w+\@/'						\
1109          -e ' && !/(\w+)\s+=.*\@\1\@$$/'				\
1110          -e ''$(_makefile_at_at_check_exceptions)			\
1111	  -e 'and (print "$$ARGV:$$.: $$_"), $$m=1; END {exit !$$m}'	\
1112	    $$($(VC_LIST_EXCEPT) | grep -E '(^|/)(Makefile\.am|[^/]+\.mk)$$') \
1113	  && { echo '$(ME): use $$(...), not @...@' 1>&2; exit 1; } || :
1114
1115news-check: NEWS
1116	$(AM_V_GEN)if $(SED) -n $(news-check-lines-spec)p $<		\
1117	    | grep -E $(news-check-regexp) >/dev/null; then		\
1118	  :;								\
1119	else								\
1120	  echo 'NEWS: $$(news-check-regexp) failed to match' 1>&2;	\
1121	  exit 1;							\
1122	fi
1123
1124sc_makefile_TAB_only_indentation:
1125	@prohibit='^	[ ]{8}'						\
1126	in_vc_files='akefile|\.mk$$'					\
1127	halt='found TAB-8-space indentation'				\
1128	  $(_sc_search_regexp)
1129
1130sc_m4_quote_check:
1131	@prohibit='(AC_DEFINE(_UNQUOTED)?|AC_DEFUN)\([^[]'		\
1132	in_vc_files='(^configure\.ac|\.m4)$$'				\
1133	halt='quote the first arg to AC_DEF*'				\
1134	  $(_sc_search_regexp)
1135
1136fix_po_file_diag = \
1137'you have changed the set of files with translatable diagnostics;\n\
1138apply the above patch\n'
1139
1140# Generate a list of files in which to search for translatable strings.
1141perl_translatable_files_list_ =						\
1142  -e 'foreach $$file (@ARGV) {'						\
1143  -e '	\# Consider only file extensions with one or two letters'	\
1144  -e '	$$file =~ /\...?$$/ or next;'					\
1145  -e '	\# Ignore m4 and mk files'					\
1146  -e '	$$file =~ /\.m[4k]$$/ and next;'				\
1147  -e '	\# Ignore a .c or .h file with a corresponding .l or .y file'	\
1148  -e '	$$file =~ /(.+)\.[ch]$$/ && (-e "$${1}.l" || -e "$${1}.y")'	\
1149  -e '	  and next;'							\
1150  -e '	\# Skip unreadable files'					\
1151  -e '	-r $$file or next;'						\
1152  -e '	print "$$file ";'						\
1153  -e '}'
1154
1155# Verify that all source files using _() (more specifically, files that
1156# match $(_gl_translatable_string_re)) are listed in po/POTFILES.in.
1157po_file ?= $(srcdir)/po/POTFILES.in
1158generated_files ?= $(srcdir)/lib/*.[ch]
1159_gl_translatable_string_re ?= \b(N?_|gettext *)\([^)"]*("|$$)
1160sc_po_check:
1161	@if test -f $(po_file); then					\
1162	  grep -E -v '^(#|$$)' $(po_file)				\
1163	    | grep -v '^src/false\.c$$' | sort > $@-1;			\
1164	  files=$$(perl $(perl_translatable_files_list_)		\
1165	    $$($(VC_LIST_EXCEPT)) $(generated_files));			\
1166	  grep -E -l '$(_gl_translatable_string_re)' $$files		\
1167	    | $(SED) 's|^$(_dot_escaped_srcdir)/||' | sort -u > $@-2;	\
1168	  diff -u -L $(po_file) -L $(po_file) $@-1 $@-2			\
1169	    || { printf '$(ME): '$(fix_po_file_diag) 1>&2; exit 1; };	\
1170	  rm -f $@-1 $@-2;						\
1171	fi
1172
1173# Sometimes it is useful to change the PATH environment variable
1174# in Makefiles.  When doing so, it's better not to use the Unix-centric
1175# path separator of ':', but rather the automake-provided '$(PATH_SEPARATOR)'.
1176msg = 'Do not use ":" above; use $$(PATH_SEPARATOR) instead'
1177sc_makefile_path_separator_check:
1178	@prohibit='PATH[=].*:'						\
1179	in_vc_files='akefile|\.mk$$'					\
1180	halt=$(msg)							\
1181	  $(_sc_search_regexp)
1182
1183# Check that 'make alpha' will not fail at the end of the process,
1184# i.e., when pkg-M.N.tar.xz already exists (either in "." or in ../release)
1185# and is read-only.
1186writable-files:
1187	$(AM_V_GEN)if test -d $(release_archive_dir); then		\
1188	  for file in $(DIST_ARCHIVES); do				\
1189	    for p in ./ $(release_archive_dir)/; do			\
1190	      test -e $$p$$file || continue;				\
1191	      test -w $$p$$file						\
1192		|| { echo ERROR: $$p$$file is not writable; fail=1; };	\
1193	    done;							\
1194	  done;								\
1195	  test "$$fail" && exit 1 || : ;				\
1196	else :;								\
1197	fi
1198
1199v_etc_file = $(gnulib_dir)/lib/version-etc.c
1200sample-test = tests/sample-test
1201texi = doc/$(PACKAGE).texi
1202# Make sure that the copyright date in $(v_etc_file) is up to date.
1203# Do the same for the $(sample-test) and the main doc/.texi file.
1204sc_copyright_check:
1205	@require='enum { COPYRIGHT_YEAR = '$$(date +%Y)' };'		\
1206	in_files=$(v_etc_file)						\
1207	halt='out of date copyright in $(v_etc_file); update it'	\
1208	  $(_sc_search_regexp)
1209	@require='# Copyright \(C\) '$$(date +%Y)' Free'		\
1210	in_vc_files=$(sample-test)					\
1211	halt='out of date copyright in $(sample-test); update it'	\
1212	  $(_sc_search_regexp)
1213	@require='Copyright @copyright\{\} .*'$$(date +%Y)		\
1214	in_vc_files=$(texi)						\
1215	halt='out of date copyright in $(texi); update it'		\
1216	  $(_sc_search_regexp)
1217
1218# If tests/help-version exists and seems to be new enough, assume that its
1219# use of init.sh and path_prepend_ is correct, and ensure that every other
1220# use of init.sh is identical.
1221# This is useful because help-version cross-checks prog --version
1222# with $(VERSION), which verifies that its path_prepend_ invocation
1223# sets PATH correctly.  This is an inexpensive way to ensure that
1224# the other init.sh-using tests also get it right.
1225_hv_file ?= $(srcdir)/tests/help-version
1226_hv_regex_weak ?= ^ *\. .*/init\.sh"
1227# Fix syntax-highlighters "
1228_hv_regex_strong ?= ^ *\. "\$${srcdir=\.}/init\.sh"
1229sc_cross_check_PATH_usage_in_tests:
1230	@if test -f $(_hv_file); then					\
1231	  grep -l 'VERSION mismatch' $(_hv_file) >/dev/null		\
1232	    || { echo "$@: skipped: no such file: $(_hv_file)" 1>&2;	\
1233		 exit 0; };						\
1234	  grep -lE '$(_hv_regex_strong)' $(_hv_file) >/dev/null		\
1235	    || { echo "$@: $(_hv_file) lacks conforming use of init.sh" 1>&2; \
1236		 exit 1; };						\
1237	  good=$$(grep -E '$(_hv_regex_strong)' $(_hv_file));		\
1238	  grep -LFx "$$good"						\
1239		$$(grep -lE '$(_hv_regex_weak)' $$($(VC_LIST_EXCEPT)))	\
1240	      | grep . &&						\
1241	    { echo "$(ME): the above files use path_prepend_ inconsistently" \
1242		1>&2; exit 1; } || :;					\
1243	fi
1244
1245# BRE regex of file contents to identify a test script.
1246_test_script_regex ?= \<init\.sh\>
1247
1248# In tests, use "compare expected actual", not the reverse.
1249sc_prohibit_reversed_compare_failure:
1250	@prohibit='\<compare [^ ]+ ([^ ]*exp|/dev/null)'		\
1251	containing='$(_test_script_regex)'				\
1252	halt='reversed compare arguments'				\
1253	  $(_sc_search_regexp)
1254
1255# #if HAVE_... will evaluate to false for any non numeric string.
1256# That would be flagged by using -Wundef, however gnulib currently
1257# tests many undefined macros, and so we can't enable that option.
1258# So at least preclude common boolean strings as macro values.
1259sc_Wundef_boolean:
1260	@prohibit='^#define.*(yes|no|true|false)$$'			\
1261	in_files='$(CONFIG_INCLUDE)'					\
1262	halt='Use 0 or 1 for macro values'				\
1263	  $(_sc_search_regexp)
1264
1265# Even if you use pathmax.h to guarantee that PATH_MAX is defined, it might
1266# not be constant, or might overflow a stack.  In general, use PATH_MAX as
1267# a limit, not an array or alloca size.
1268sc_prohibit_path_max_allocation:
1269	@prohibit='(\balloca *\([^)]*|\[[^]]*)\bPATH_MAX'		\
1270	halt='Avoid stack allocations of size PATH_MAX'			\
1271	  $(_sc_search_regexp)
1272
1273sc_vulnerable_makefile_CVE-2009-4029:
1274	@prohibit='perm -777 -exec chmod a\+rwx|chmod 777 \$$\(distdir\)' \
1275	in_files='(^|/)Makefile\.in$$'					\
1276	halt=$$(printf '%s\n'						\
1277	  'the above files are vulnerable; beware of running'		\
1278	  '  "make dist*" rules, and upgrade to fixed automake'		\
1279	  '  see http://bugzilla.redhat.com/542609 for details')	\
1280	  $(_sc_search_regexp)
1281
1282sc_vulnerable_makefile_CVE-2012-3386:
1283	@prohibit='chmod a\+w \$$\(distdir\)'				\
1284	in_files='(^|/)Makefile\.in$$'					\
1285	halt=$$(printf '%s\n'						\
1286	  'the above files are vulnerable; beware of running'		\
1287	  '  "make distcheck", and upgrade to fixed automake'		\
1288	  '  see http://bugzilla.redhat.com/CVE-2012-3386 for details')	\
1289	  $(_sc_search_regexp)
1290
1291vc-diff-check:
1292	$(AM_V_GEN)(unset CDPATH; cd $(srcdir) && $(VC) diff) > vc-diffs || :
1293	$(AM_V_at)if test -s vc-diffs; then			\
1294	  cat vc-diffs;						\
1295	  echo "Some files are locally modified:" 1>&2;		\
1296	  exit 1;						\
1297	else							\
1298	  rm vc-diffs;						\
1299	fi
1300
1301rel-files = $(DIST_ARCHIVES)
1302
1303gnulib_dir ?= $(srcdir)/gnulib
1304gnulib-version = $$(cd $(gnulib_dir)				\
1305                    && { git describe || git rev-parse --short=10 HEAD; } )
1306bootstrap-tools ?= autoconf,automake,gnulib
1307
1308gpgv = $$(gpgv2 --version >/dev/null && echo gpgv2 || echo gpgv)
1309# If it's not already specified, derive the GPG key ID from
1310# the signed tag we've just applied to mark this release.
1311gpg_key_ID ?=								\
1312  $$(cd $(srcdir)							\
1313     && git cat-file tag v$(VERSION)					\
1314        | $(gpgv) --status-fd 1 --keyring /dev/null - - 2>/dev/null	\
1315        | awk '/^\[GNUPG:\] ERRSIG / {print $$3; exit}')
1316
1317translation_project_ ?= coordinator@translationproject.org
1318
1319# Make info-gnu the default only for a stable release.
1320announcement_Cc_stable = $(translation_project_), $(PACKAGE_BUGREPORT)
1321announcement_mail_headers_stable =		\
1322  To: info-gnu@gnu.org				\
1323  Cc: $(announcement_Cc_)			\
1324  Mail-Followup-To: $(PACKAGE_BUGREPORT)
1325
1326announcement_Cc_alpha = $(translation_project_)
1327announcement_mail_headers_alpha =		\
1328  To: $(PACKAGE_BUGREPORT)			\
1329  Cc: $(announcement_Cc_)
1330
1331announcement_mail_Cc_beta = $(announcement_mail_Cc_alpha)
1332announcement_mail_headers_beta = $(announcement_mail_headers_alpha)
1333
1334announcement_mail_Cc_ ?= $(announcement_mail_Cc_$(release-type))
1335announcement_mail_headers_ ?= $(announcement_mail_headers_$(release-type))
1336announcement: NEWS ChangeLog $(rel-files)
1337# Not $(AM_V_GEN) since the output of this command serves as
1338# announcement message: it would start with " GEN announcement".
1339	$(AM_V_at)$(srcdir)/$(_build-aux)/announce-gen			\
1340	    --mail-headers='$(announcement_mail_headers_)'		\
1341	    --release-type=$(release-type)				\
1342	    --package=$(PACKAGE)					\
1343	    --prev=$(PREV_VERSION)					\
1344	    --curr=$(VERSION)						\
1345	    --gpg-key-id=$(gpg_key_ID)					\
1346	    --srcdir=$(srcdir)						\
1347	    --news=$(srcdir)/NEWS					\
1348	    --bootstrap-tools=$(bootstrap-tools)			\
1349	    $$(case ,$(bootstrap-tools), in (*,gnulib,*)		\
1350	       echo --gnulib-version=$(gnulib-version);; esac)		\
1351	    --no-print-checksums					\
1352	    $(addprefix --url-dir=, $(url_dir_list))
1353
1354.PHONY: release-commit
1355release-commit:
1356	$(AM_V_GEN)cd $(srcdir)				\
1357	  && $(_build-aux)/do-release-commit-and-tag	\
1358	       -C $(abs_builddir) $(RELEASE)
1359
1360## ---------------- ##
1361## Updating files.  ##
1362## ---------------- ##
1363
1364ftp-gnu = ftp://ftp.gnu.org/gnu
1365www-gnu = http://www.gnu.org
1366
1367upload_dest_dir_ ?= $(PACKAGE)
1368upload_command =						\
1369  $(srcdir)/$(_build-aux)/gnupload $(GNUPLOADFLAGS)		\
1370  --to $(gnu_rel_host):$(upload_dest_dir_)			\
1371  $(rel-files)
1372emit_upload_commands:
1373	@echo =====================================
1374	@echo =====================================
1375	@echo '$(upload_command)'
1376	@echo '# send the ~/announce-$(my_distdir) e-mail'
1377	@echo =====================================
1378	@echo =====================================
1379
1380.PHONY: upload
1381upload:
1382	$(AM_V_GEN)$(upload_command)
1383
1384define emit-commit-log
1385  printf '%s\n' 'maint: post-release administrivia' ''			\
1386    '* NEWS: Add header line for next release.'				\
1387    '* .prev-version: Record previous version.'				\
1388    '* cfg.mk (old_NEWS_hash): Auto-update.'
1389endef
1390
1391.PHONY: no-submodule-changes
1392no-submodule-changes:
1393	$(AM_V_GEN)if test -d $(srcdir)/.git				\
1394		&& git --version >/dev/null 2>&1; then			\
1395	  diff=$$(cd $(srcdir) && git submodule -q foreach		\
1396		  git diff-index --name-only HEAD)			\
1397	    || exit 1;							\
1398	  case $$diff in '') ;;						\
1399	    *) echo '$(ME): submodule files are locally modified:';	\
1400		echo "$$diff"; exit 1;; esac;				\
1401	else								\
1402	  : ;								\
1403	fi
1404
1405submodule-checks ?= no-submodule-changes public-submodule-commit
1406
1407# Ensure that each sub-module commit we're using is public.
1408# Without this, it is too easy to tag and release code that
1409# cannot be built from a fresh clone.
1410.PHONY: public-submodule-commit
1411public-submodule-commit:
1412	$(AM_V_GEN)if test -d $(srcdir)/.git				\
1413		&& git --version >/dev/null 2>&1; then			\
1414	  cd $(srcdir) &&						\
1415	  git submodule --quiet foreach					\
1416	      'test "$$(git rev-parse "$$sha1")"			\
1417		  = "$$(git merge-base origin "$$sha1")"'		\
1418	    || { echo '$(ME): found non-public submodule commit' >&2;	\
1419		 exit 1; };						\
1420	else								\
1421	  : ;								\
1422	fi
1423# This rule has a high enough utility/cost ratio that it should be a
1424# dependent of "check" by default.  However, some of us do occasionally
1425# commit a temporary change that deliberately points to a non-public
1426# submodule commit, and want to be able to use rules like "make check".
1427# In that case, run e.g., "make check gl_public_submodule_commit="
1428# to disable this test.
1429gl_public_submodule_commit ?= public-submodule-commit
1430check: $(gl_public_submodule_commit)
1431
1432.PHONY: alpha beta stable release
1433ALL_RECURSIVE_TARGETS += alpha beta stable
1434alpha beta stable: $(local-check) writable-files $(submodule-checks)
1435	$(AM_V_GEN)test $@ = stable					\
1436	  && { echo $(VERSION) | grep -E '^[0-9]+(\.[0-9]+)+$$'		\
1437	       || { echo "invalid version string: $(VERSION)" 1>&2; exit 1;};}\
1438	  || :
1439	$(AM_V_at)$(MAKE) vc-diff-check
1440	$(AM_V_at)$(MAKE) news-check
1441	$(AM_V_at)$(MAKE) distcheck
1442	$(AM_V_at)$(MAKE) dist
1443	$(AM_V_at)$(MAKE) $(release-prep-hook) RELEASE_TYPE=$@
1444	$(AM_V_at)$(MAKE) -s emit_upload_commands RELEASE_TYPE=$@
1445
1446release:
1447	$(AM_V_GEN)$(MAKE) _version
1448	$(AM_V_GEN)$(MAKE) $(release-type)
1449
1450# Override this in cfg.mk if you follow different procedures.
1451release-prep-hook ?= release-prep
1452
1453gl_noteworthy_news_ = * Noteworthy changes in release ?.? (????-??-??) [?]
1454.PHONY: release-prep
1455release-prep:
1456	$(AM_V_GEN)$(MAKE) --no-print-directory -s announcement \
1457	  > ~/announce-$(my_distdir)
1458	$(AM_V_at)if test -d $(release_archive_dir); then	\
1459	  ln $(rel-files) $(release_archive_dir);		\
1460	  chmod a-w $(rel-files);				\
1461	fi
1462	$(AM_V_at)echo $(VERSION) > $(prev_version_file)
1463	$(AM_V_at)$(MAKE) update-NEWS-hash
1464	$(AM_V_at)perl -pi						\
1465	  -e '$$. == 3 and print "$(gl_noteworthy_news_)\n\n\n"'	\
1466	  $(srcdir)/NEWS
1467	$(AM_V_at)msg=$$($(emit-commit-log)) || exit 1;		\
1468	cd $(srcdir) && $(VC) commit -m "$$msg" -a
1469
1470# Override this with e.g., -s $(srcdir)/some_other_name.texi
1471# if the default $(PACKAGE)-derived name doesn't apply.
1472gendocs_options_ ?=
1473
1474.PHONY: web-manual
1475web-manual:
1476	$(AM_V_GEN)test -z "$(manual_title)" \
1477	  && { echo define manual_title in cfg.mk 1>&2; exit 1; } || :
1478	$(AM_V_at)cd '$(srcdir)/doc'; \
1479	  $(SHELL) ../$(_build-aux)/gendocs.sh $(gendocs_options_) \
1480	     -o '$(abs_builddir)/doc/manual' \
1481	     --email $(PACKAGE_BUGREPORT) $(PACKAGE) \
1482	    "$(PACKAGE_NAME) - $(manual_title)"
1483	$(AM_V_at)echo " *** Upload the doc/manual directory to web-cvs."
1484
1485.PHONY: web-manual-update
1486web-manual-update:
1487	$(AM_V_GEN)cd $(srcdir) \
1488	  && $(_build-aux)/gnu-web-doc-update -C $(abs_builddir)
1489
1490
1491# Code Coverage
1492
1493init-coverage:
1494	$(MAKE) $(AM_MAKEFLAGS) clean
1495	lcov --directory . --zerocounters
1496
1497COVERAGE_CCOPTS ?= "-g --coverage"
1498COVERAGE_OUT ?= doc/coverage
1499
1500build-coverage:
1501	$(MAKE) $(AM_MAKEFLAGS) CFLAGS=$(COVERAGE_CCOPTS) CXXFLAGS=$(COVERAGE_CCOPTS)
1502	$(MAKE) $(AM_MAKEFLAGS) CFLAGS=$(COVERAGE_CCOPTS) CXXFLAGS=$(COVERAGE_CCOPTS) check
1503	mkdir -p $(COVERAGE_OUT)
1504	lcov --directory . --output-file $(COVERAGE_OUT)/$(PACKAGE).info \
1505		--capture
1506
1507gen-coverage:
1508	genhtml --output-directory $(COVERAGE_OUT) \
1509		$(COVERAGE_OUT)/$(PACKAGE).info \
1510		--highlight --frames --legend \
1511		--title "$(PACKAGE_NAME)"
1512
1513coverage: init-coverage build-coverage gen-coverage
1514
1515# Some projects carry local adjustments for gnulib modules via patches in
1516# a gnulib patch directory whose default name is gl/ (defined in bootstrap
1517# via local_gl_dir=gl).  Those patches become stale as the originals evolve
1518# in gnulib.  Use this rule to refresh any stale patches.  It applies each
1519# patch to the original in $(gnulib_dir) and uses the temporary result to
1520# generate a fuzz-free .diff file.  If you customize the name of your local
1521# gnulib patch directory via bootstrap.conf, this rule detects that name.
1522# Run this from a non-VPATH (i.e., srcdir) build directory.
1523.PHONY: refresh-gnulib-patches
1524refresh-gnulib-patches:
1525	gl=gl;								\
1526	if test -f bootstrap.conf; then					\
1527	  t=$$(perl -lne '/^\s*local_gl_dir=(\S+)/ and $$d=$$1;'	\
1528	       -e 'END{defined $$d and print $$d}' bootstrap.conf);	\
1529	  test -n "$$t" && gl=$$t;					\
1530	fi;								\
1531	for diff in $$(cd $$gl; git ls-files | grep '\.diff$$'); do	\
1532	  b=$$(printf %s "$$diff"|$(SED) 's/\.diff$$//');		\
1533	  VERSION_CONTROL=none						\
1534	    patch "$(gnulib_dir)/$$b" "$$gl/$$diff" || exit 1;		\
1535	  ( cd $(gnulib_dir) || exit 1;					\
1536	    git diff "$$b" > "../$$gl/$$diff";				\
1537	    git checkout $$b ) || exit 1;				\
1538	done
1539
1540# Update gettext files.
1541PACKAGE ?= $(shell basename $(PWD))
1542PO_DOMAIN ?= $(PACKAGE)
1543POURL = http://translationproject.org/latest/$(PO_DOMAIN)/
1544PODIR ?= po
1545refresh-po:
1546	rm -f $(PODIR)/*.po && \
1547	echo "$(ME): getting translations into po (please ignore the robots.txt ERROR 404)..." && \
1548	wget --no-verbose --directory-prefix $(PODIR) --no-directories --recursive --level 1 --accept .po --accept .po.1 $(POURL) && \
1549	echo 'en@boldquot' > $(PODIR)/LINGUAS && \
1550	echo 'en@quot' >> $(PODIR)/LINGUAS && \
1551	ls $(PODIR)/*.po | $(SED) 's/\.po//;s,$(PODIR)/,,' | \
1552	  sort >> $(PODIR)/LINGUAS
1553
1554 # Running indent once is not idempotent, but running it twice is.
1555INDENT_SOURCES ?= $(C_SOURCES)
1556.PHONY: indent
1557indent:
1558	indent $(INDENT_SOURCES)
1559	indent $(INDENT_SOURCES)
1560
1561# If you want to set UPDATE_COPYRIGHT_* environment variables,
1562# put the assignments in this variable.
1563update-copyright-env ?=
1564
1565# Run this rule once per year (usually early in January)
1566# to update all FSF copyright year lists in your project.
1567# If you have an additional project-specific rule,
1568# add it in cfg.mk along with a line 'update-copyright: prereq'.
1569# By default, exclude all variants of COPYING; you can also
1570# add exemptions (such as ChangeLog..* for rotated change logs)
1571# in the file .x-update-copyright.
1572.PHONY: update-copyright
1573update-copyright:
1574	$(AM_V_GEN)grep -l -w Copyright                                  \
1575	  $$(export VC_LIST_EXCEPT_DEFAULT=COPYING && $(VC_LIST_EXCEPT)) \
1576	  | $(update-copyright-env) xargs $(srcdir)/$(_build-aux)/$@
1577
1578# This tight_scope test is skipped with a warning if $(_gl_TS_headers) is not
1579# overridden and $(_gl_TS_dir)/Makefile.am does not mention noinst_HEADERS.
1580
1581# NOTE: to override any _gl_TS_* default value, you must
1582# define the variable(s) using "export" in cfg.mk.
1583_gl_TS_dir ?= src
1584
1585ALL_RECURSIVE_TARGETS += sc_tight_scope
1586sc_tight_scope: tight-scope.mk
1587	@fail=0;							\
1588	if ! grep '^ *export _gl_TS_headers *=' $(srcdir)/cfg.mk	\
1589		> /dev/null						\
1590	   && ! grep -w noinst_HEADERS $(srcdir)/$(_gl_TS_dir)/Makefile.am \
1591		> /dev/null 2>&1; then					\
1592	    echo '$(ME): skipping $@';					\
1593	else								\
1594	    $(MAKE) -s -C $(_gl_TS_dir)					\
1595		-f Makefile						\
1596		-f $(abs_top_srcdir)/cfg.mk				\
1597		-f $(abs_top_builddir)/$<				\
1598	      _gl_tight_scope						\
1599		|| fail=1;						\
1600	fi;								\
1601	rm -f $<;							\
1602	exit $$fail
1603
1604tight-scope.mk: $(ME)
1605	@rm -f $@ $@-t
1606	@perl -ne '/^# TS-start/.../^# TS-end/ and print' $(srcdir)/$(ME) > $@-t
1607	@chmod a=r $@-t && mv $@-t $@
1608
1609ifeq (a,b)
1610# TS-start
1611
1612# Most functions should have static scope.
1613# Any that don't must be marked with 'extern', but 'main'
1614# and 'usage' are exceptions: they're always extern, but
1615# do not need to be marked.  Symbols matching '__.*' are
1616# reserved by the compiler, so are automatically excluded below.
1617_gl_TS_unmarked_extern_functions ?= main usage
1618_gl_TS_function_match ?= /^(?:$(_gl_TS_extern)) +.*?(\w+) *\(/
1619
1620# If your project uses a macro like "XTERN", then put
1621# the following in cfg.mk to override this default:
1622# export _gl_TS_extern = extern|XTERN
1623_gl_TS_extern ?= extern
1624
1625# The second nm|grep checks for file-scope variables with 'extern' scope.
1626# Without gnulib's progname module, you might put program_name here.
1627# Symbols matching '__.*' are reserved by the compiler,
1628# so are automatically excluded below.
1629_gl_TS_unmarked_extern_vars ?=
1630
1631# NOTE: the _match variables are perl expressions -- not mere regular
1632# expressions -- so that you can extend them to match other patterns
1633# and easily extract matched variable names.
1634# For example, if your project declares some global variables via
1635# a macro like this: GLOBAL(type, var_name, initializer), then you
1636# can override this definition to automatically extract those names:
1637# export _gl_TS_var_match = \
1638#   /^(?:$(_gl_TS_extern)) .*?\**(\w+)(\[.*?\])?;/ || /\bGLOBAL\(.*?,\s*(.*?),/
1639_gl_TS_var_match ?= /^(?:$(_gl_TS_extern)) .*?(\w+)(\[.*?\])?;/
1640
1641# The names of object files in (or relative to) $(_gl_TS_dir).
1642_gl_TS_obj_files ?= *.$(OBJEXT)
1643
1644# Files in which to search for the one-line style extern declarations.
1645# $(_gl_TS_dir)-relative.
1646_gl_TS_headers ?= $(noinst_HEADERS)
1647_gl_TS_other_headers ?= *.h
1648
1649.PHONY: _gl_tight_scope
1650_gl_tight_scope: $(bin_PROGRAMS)
1651	sed_wrap='s/^/^_?/;s/$$/$$/';					\
1652	t=exceptions-$$$$;						\
1653	trap 's=$$?; rm -f $$t; exit $$s' 0;				\
1654	for sig in 1 2 3 13 15; do					\
1655	  eval "trap 'v=`expr $$sig + 128`; (exit $$v); exit $$v' $$sig"; \
1656	done;								\
1657	src=`for f in $(SOURCES); do					\
1658	       test -f $$f && d= || d=$(srcdir)/; echo $$d$$f; done`;	\
1659	hdr=`for f in $(_gl_TS_headers); do				\
1660	       test -f $$f && d= || d=$(srcdir)/; echo $$d$$f; done`;	\
1661	( printf '%s\n' '__.*' $(_gl_TS_unmarked_extern_functions);	\
1662	  grep -h -A1 '^extern .*[^;]$$' $$src				\
1663	    | grep -vE '^(extern |--|#)' | $(SED) 's/ .*//; /^$$/d';	\
1664	  perl -lne							\
1665	     '$(_gl_TS_function_match) and print $$1' $$hdr;		\
1666	) | sort -u | $(SED) "$$sed_wrap" > $$t;			\
1667	nm -g $(_gl_TS_obj_files)|$(SED) -n 's/.* T //p'|grep -Ev -f $$t \
1668	  && { echo the above functions should have static scope >&2;	\
1669	       exit 1; } || : ;						\
1670	( printf '%s\n' '__.*' main $(_gl_TS_unmarked_extern_vars);	\
1671	  perl -lne '$(_gl_TS_var_match) and print $$1'			\
1672		$$hdr $(_gl_TS_other_headers)				\
1673	) | sort -u | $(SED) "$$sed_wrap" > $$t;			\
1674	nm -g $(_gl_TS_obj_files) | $(SED) -n 's/.* [BCDGRS] //p'	\
1675            | sort -u | grep -Ev -f $$t					\
1676	  && { echo the above variables should have static scope >&2;	\
1677	       exit 1; } || :
1678# TS-end
1679endif
1680