]> sigrok.org Git - sigrok-test.git/blob - m4/sigrok.m4
dsi: Update due to recent minor PD changes.
[sigrok-test.git] / m4 / sigrok.m4
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2009 Openismus GmbH
5 ## Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com>
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
21 #serial 20150910
22
23 ## SR_APPEND(var-name, [list-sep,] element)
24 ##
25 ## Append the shell word <element> to the shell variable named <var-name>,
26 ## prefixed by <list-sep> unless the list was empty before appending. If
27 ## only two arguments are supplied, <list-sep> defaults to a single space
28 ## character.
29 ##
30 AC_DEFUN([SR_APPEND],
31 [dnl
32 m4_assert([$# >= 2])[]dnl
33 $1=[$]{$1[}]m4_if([$#], [2], [[$]{$1:+' '}$2], [[$]{$1:+$2}$3])[]dnl
34 ])
35
36 ## SR_PREPEND(var-name, [list-sep,] element)
37 ##
38 ## Prepend the shell word <element> to the shell variable named <var-name>,
39 ## suffixed by <list-sep> unless the list was empty before prepending. If
40 ## only two arguments are supplied, <list-sep> defaults to a single space
41 ## character.
42 ##
43 AC_DEFUN([SR_PREPEND],
44 [dnl
45 m4_assert([$# >= 2])[]dnl
46 $1=m4_if([$#], [2], [$2[$]{$1:+' '}], [$3[$]{$1:+$2}])[$]$1[]dnl
47 ])
48
49 ## _SR_PKG_VERSION_SET(var-prefix, pkg-name, tag-prefix, base-version, major, minor, [micro])
50 ##
51 m4_define([_SR_PKG_VERSION_SET],
52 [dnl
53 m4_assert([$# >= 6])[]dnl
54 $1=$4
55 sr_git_deps=
56 # Check if we can get revision information from git.
57 sr_head=`git -C "$srcdir" rev-parse --verify --short HEAD 2>&AS_MESSAGE_LOG_FD`
58
59 AS_IF([test "$?" = 0 && test "x$sr_head" != x], [dnl
60         test ! -f "$srcdir/.git/HEAD" \
61                 || sr_git_deps="$sr_git_deps \$(top_srcdir)/.git/HEAD"
62
63         sr_head_name=`git -C "$srcdir" rev-parse --symbolic-full-name HEAD 2>&AS_MESSAGE_LOG_FD`
64         AS_IF([test "$?" = 0 && test -f "$srcdir/.git/$sr_head_name"],
65                 [sr_git_deps="$sr_git_deps \$(top_srcdir)/.git/$sr_head_name"])
66
67         # Append the revision hash unless we are exactly on a tagged release.
68         git -C "$srcdir" describe --match "$3$4" \
69                 --exact-match >&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD \
70                 || $1="[$]$1-git-$sr_head"
71 ])
72 # Use $(wildcard) so that things do not break if for whatever
73 # reason these files do not exist anymore at make time.
74 AS_IF([test -n "$sr_git_deps"],
75         [SR_APPEND([CONFIG_STATUS_DEPENDENCIES], ["\$(wildcard$sr_git_deps)"])])
76 AC_SUBST([CONFIG_STATUS_DEPENDENCIES])[]dnl
77 AC_SUBST([$1])[]dnl
78 dnl
79 AC_DEFINE([$1_MAJOR], [$5], [Major version number of $2.])[]dnl
80 AC_DEFINE([$1_MINOR], [$6], [Minor version number of $2.])[]dnl
81 m4_ifval([$7], [AC_DEFINE([$1_MICRO], [$7], [Micro version number of $2.])])[]dnl
82 AC_DEFINE_UNQUOTED([$1_STRING], ["[$]$1"], [Version of $2.])[]dnl
83 ])
84
85 ## SR_PKG_VERSION_SET(var-prefix, version-triple)
86 ##
87 ## Set up substitution variables and macro definitions for the package
88 ## version components. Derive the version suffix from the repository
89 ## revision if possible.
90 ##
91 ## Substitutions: <var-prefix>
92 ## Macro defines: <var-prefix>_{MAJOR,MINOR,MICRO,STRING}
93 ##
94 AC_DEFUN([SR_PKG_VERSION_SET],
95 [dnl
96 m4_assert([$# >= 2])[]dnl
97 _SR_PKG_VERSION_SET([$1],
98         m4_defn([AC_PACKAGE_NAME]),
99         m4_defn([AC_PACKAGE_TARNAME])[-],
100         m4_expand([$2]),
101         m4_unquote(m4_split(m4_expand([$2]), [\.])))
102 ])
103
104 ## _SR_LIB_VERSION_SET(var-prefix, pkg-name, abi-triple, current, revision, age)
105 ##
106 m4_define([_SR_LIB_VERSION_SET],
107 [dnl
108 m4_assert([$# >= 6])[]dnl
109 $1=$3
110 AC_SUBST([$1])[]dnl
111 AC_DEFINE([$1_CURRENT], [$4], [Binary version of $2.])[]dnl
112 AC_DEFINE([$1_REVISION], [$5], [Binary revision of $2.])[]dnl
113 AC_DEFINE([$1_AGE], [$6], [Binary age of $2.])[]dnl
114 AC_DEFINE([$1_STRING], ["$3"], [Binary version triple of $2.])[]dnl
115 ])
116
117 ## SR_LIB_VERSION_SET(var-prefix, abi-triple)
118 ##
119 ## Set up substitution variables and macro definitions for a library
120 ## binary version.
121 ##
122 ## Substitutions: <var-prefix>
123 ## Macro defines: <var-prefix>_{CURRENT,REVISION,AGE,STRING}
124 ##
125 AC_DEFUN([SR_LIB_VERSION_SET],
126 [dnl
127 m4_assert([$# >= 1])[]dnl
128 _SR_LIB_VERSION_SET([$1],
129         m4_defn([AC_PACKAGE_NAME]),
130         [$2], m4_unquote(m4_split([$2], [:])))
131 ])
132
133 ## SR_SEARCH_LIBS(libs-var, function, search-libs,
134 ##                [action-if-found], [action-if-not-found], [other-libs])
135 ##
136 ## Same as AC_SEARCH_LIBS, except that the result is prepended
137 ## to <libs-var> instead of LIBS. Calls AC_SUBST on <libs-var>.
138 ##
139 AC_DEFUN([SR_SEARCH_LIBS],
140 [dnl
141 m4_assert([$# >= 3])[]dnl
142 sr_sl_save_LIBS=$LIBS
143 AC_SEARCH_LIBS([$2], [$3],,, m4_join([$6], [[$]$1]))
144 LIBS=$sr_sl_save_LIBS
145 AS_CASE([$ac_cv_search_$2], [no*],,
146         [SR_PREPEND([$1], [$ac_cv_search_$2])])
147 m4_ifvaln([$4$5], [AS_IF([test "x$ac_cv_search_$2" = xno], [$5], [$4])])[]dnl
148 AC_SUBST([$1])[]dnl
149 ])
150
151 ## _SR_VAR_SUMMARY(tag, var-name, line-leader, align-columns, align-char)
152 ##
153 m4_define([_SR_VAR_SUMMARY], [dnl
154 $2=
155 $1_append() {
156         sr_aligned=`printf '%.$4s' "[$][1]m4_for([i], [1], [$4],, [$5])"`
157         $2="[$]{$2}$3$sr_aligned [$]2"'
158 '
159 }
160 ])
161
162 ## SR_VAR_SUMMARY(tag, [var-name = <tag>],
163 ##                [line-leader = [ - ]], [align-columns = 32], [align-char = .])
164 ##
165 ## Define a shell function <tag>_append() to be used for aggregating
166 ## a summary of test results in the shell variable <var-name>.
167 ##
168 AC_DEFUN([SR_VAR_SUMMARY],
169 [dnl
170 m4_assert([$# >= 1])[]dnl
171 _SR_VAR_SUMMARY([$1],
172         m4_default_quoted([$2], [$1]),
173         m4_default_quoted([$3], [ - ]),
174         m4_default_quoted([$4], [32]),
175         m4_default_quoted([$5], [.]))[]dnl
176 ])
177
178 ## SR_PKG_CHECK_SUMMARY([var-name = sr_pkg_check_summary],
179 ##                      [line-leader = [ - ]], [align-columns = 32], [align-char = .])
180 ##
181 ## Prepare for the aggregation of package check results
182 ## in the shell variable <var-name>.
183 ##
184 AC_DEFUN([SR_PKG_CHECK_SUMMARY],
185         [SR_VAR_SUMMARY([sr_pkg_check_summary], $@)])
186
187 ## SR_PKG_CHECK(tag, [collect-var], module...)
188 ##
189 ## Check for each pkg-config <module> in the argument list. <module> may
190 ## include a version constraint.
191 ##
192 ## Output variables: sr_have_<tag>, sr_<tag>_version
193 ##
194 AC_DEFUN([SR_PKG_CHECK],
195 [dnl
196 m4_assert([$# >= 3])[]dnl
197 AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
198 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
199 dnl
200 PKG_CHECK_EXISTS([$3], [dnl
201         sr_have_$1=yes
202         m4_ifval([$2], [SR_APPEND([$2], ["$3"])
203         ])sr_$1_version=`$PKG_CONFIG --modversion "$3" 2>&AS_MESSAGE_LOG_FD`
204         sr_pkg_check_summary_append "$3" "$sr_$1_version"[]dnl
205 ], [dnl
206         sr_pkg_check_summary_append "$3" no
207         m4_ifval([$4],
208                 [SR_PKG_CHECK([$1], [$2], m4_shift3($@))],
209                 [sr_have_$1=no sr_$1_version=])[]dnl
210 ])
211 ])
212
213 ## SR_VAR_OPT_PKG([modules-var], [features-var])
214 ##
215 ## Enable the collection of SR_ARG_OPT_PKG results into the shell variables
216 ## <modules-var> and <features-var>.
217 ##
218 AC_DEFUN([SR_VAR_OPT_PKG],
219 [dnl
220 m4_define([_SR_VAR_OPT_PKG_MODULES], [$1])[]dnl
221 m4_define([_SR_VAR_OPT_PKG_FEATURES], [$2])[]dnl
222 m4_ifvaln([$1], [$1=])[]dnl
223 m4_ifvaln([$2], [$2=])[]dnl
224 ])
225
226 ## _SR_ARG_OPT_IMPL(sh-name, [features-var], opt-name,
227 ##                  [cpp-name], [cond-name], check-commands)
228 ##
229 m4_define([_SR_ARG_OPT_IMPL],
230 [dnl
231 AC_ARG_WITH([$3], [AS_HELP_STRING([--without-$3],
232                         [disable $3 support [default=detect]])])
233 AS_IF([test "x$with_$1" = xno], [sr_have_$1=no],
234         [test "x$sr_have_$1" != xyes], [dnl
235 AC_MSG_CHECKING([for $3])
236 $6
237 AC_MSG_RESULT([$sr_have_$1])[]dnl
238 ])
239 AS_IF([test "x$with_$1$sr_have_$1" = xyesno],
240         [AC_MSG_ERROR([$3 support requested, but it was not found.])])
241 AS_IF([test "x$sr_have_$1" = xyes], [m4_ifval([$2], [
242         SR_APPEND([$2], ["$3"])])[]m4_ifval([$4], [
243         AC_DEFINE([HAVE_$4], [1], [Whether $3 is available.])])[]dnl
244 ])
245 m4_ifvaln([$5], [AM_CONDITIONAL([$5], [test "x$sr_have_$1" = xyes])])[]dnl
246 ])
247
248 ## _SR_ARG_OPT_CHECK(sh-name, [features-var], opt-name, [cpp-name],
249 ##                   [cond-name], check-commands, [summary-result])
250 ##
251 m4_define([_SR_ARG_OPT_CHECK],
252 [dnl
253 _SR_ARG_OPT_IMPL($@)
254 sr_pkg_check_summary_append "$3" m4_default([$7], ["$sr_have_$1"])
255 ])
256
257 ## SR_ARG_OPT_CHECK(opt-name, [cpp-name], [cond-name], check-commands,
258 ##                  [summary-result = $sr_have_<opt-name>])
259 ##
260 ## Provide a --without-<opt-name> configure option for explicit disabling
261 ## of an optional dependency. If not disabled, the availability of the
262 ## optional dependency is auto-detected by running <check-commands>.
263 ##
264 ## The <check-commands> should set the shell variable sr_have_<opt-name>
265 ## to "yes" if the dependency is available, otherwise to "no". Optionally,
266 ## the <summary-result> argument may be used to generate a line in the
267 ## configuration summary. If supplied, it should be a shell word which
268 ## expands to the result to be displayed for the <opt-name> dependency.
269 ##
270 ## Use SR_VAR_OPT_PKG to generate lists of available modules and features.
271 ##
272 AC_DEFUN([SR_ARG_OPT_CHECK],
273 [dnl
274 m4_assert([$# >= 4])[]dnl
275 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
276 AC_REQUIRE([SR_VAR_OPT_PKG])[]dnl
277 dnl
278 _SR_ARG_OPT_CHECK(m4_expand([AS_TR_SH([$1])]),
279         m4_defn([_SR_VAR_OPT_PKG_FEATURES]),
280         $@)[]dnl
281 ])
282
283 ## _SR_ARG_OPT_PKG([features-var], [cond-name], opt-name,
284 ##                 [cpp-name], sh-name, [modules-var], module...)
285 ##
286 m4_define([_SR_ARG_OPT_PKG],
287 [dnl
288 _SR_ARG_OPT_IMPL([$5], [$1], [$3], [$4], [$2],
289         [SR_PKG_CHECK(m4_shiftn([4], $@))])
290 m4_ifvaln([$4], [AS_IF([test "x$sr_have_$5" = xyes],
291         [AC_DEFINE_UNQUOTED([CONF_$4_VERSION], ["$sr_$5_version"],
292                 [Build-time version of $3.])])])[]dnl
293 ])
294
295 ## SR_ARG_OPT_PKG(opt-name, [cpp-name], [cond-name], module...)
296 ##
297 ## Provide a --without-<opt-name> configure option for explicit disabling
298 ## of an optional dependency. If not disabled, the availability of the
299 ## optional dependency is auto-detected.
300 ##
301 ## Each pkg-config <module> argument is checked in turn, and the first one
302 ## found is selected. On success, the shell variable sr_have_<opt-name>
303 ## is set to "yes", otherwise to "no". Optionally, a preprocessor macro
304 ## HAVE_<cpp-name> and an Automake conditional <cond-name> are generated.
305 ##
306 ## Use SR_VAR_OPT_PKG to generate lists of available modules and features.
307 ##
308 AC_DEFUN([SR_ARG_OPT_PKG],
309 [dnl
310 m4_assert([$# >= 4])[]dnl
311 AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
312 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
313 AC_REQUIRE([SR_VAR_OPT_PKG])[]dnl
314 dnl
315 _SR_ARG_OPT_PKG(m4_defn([_SR_VAR_OPT_PKG_FEATURES]),
316         [$3], [$1], [$2],
317         m4_expand([AS_TR_SH([$1])]),
318         m4_defn([_SR_VAR_OPT_PKG_MODULES]),
319         m4_shift3($@))[]dnl
320 ])
321
322 ## SR_PROG_VERSION(program, sh-var)
323 ##
324 ## Obtain the version of <program> and store it in <sh-var>.
325 ##
326 AC_DEFUN([SR_PROG_VERSION],
327 [dnl
328 m4_assert([$# >= 2])[]dnl
329 sr_prog_ver=`$1 --version 2>&AS_MESSAGE_LOG_FD | sed 1q 2>&AS_MESSAGE_LOG_FD`
330 AS_CASE([[$]?:$sr_prog_ver],
331         [[0:*[0-9].[0-9]*]], [$2=$sr_prog_ver],
332         [$2=unknown])[]dnl
333 ])
334
335 ## SR_PROG_MAKE_ORDER_ONLY
336 ##
337 ## Check whether the make program supports order-only prerequisites.
338 ## If so, set the substitution variable ORDER to '|', or to the empty
339 ## string otherwise.
340 ##
341 AC_DEFUN([SR_PROG_MAKE_ORDER_ONLY],
342 [dnl
343 AC_CACHE_CHECK([whether [$]{MAKE:-make} supports order-only prerequisites],
344         [sr_cv_prog_make_order_only], [
345 cat >conftest.mk <<'_SREOF'
346 a: b | c
347 a b c: ; @:
348 .PHONY: a b c
349 _SREOF
350 AS_IF([[$]{MAKE:-make} -f conftest.mk >&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD],
351         [sr_cv_prog_make_order_only=yes], [sr_cv_prog_make_order_only=no])
352 rm -f conftest.mk
353 ])
354 AS_IF([test "x$sr_cv_prog_make_order_only" = xyes], [ORDER='|'], [ORDER=])
355 AC_SUBST([ORDER])
356 AM_SUBST_NOTMAKE([ORDER])[]dnl
357 ])
358
359 ## SR_CHECK_COMPILE_FLAGS(flags-var, description, flags)
360 ##
361 ## Find a compiler flag for <description>. For each flag in <flags>, check
362 ## if the compiler for the current language accepts it. On success, stop the
363 ## search and append the last tested flag to <flags-var>. Calls AC_SUBST
364 ## on <flags-var>.
365 ##
366 AC_DEFUN([SR_CHECK_COMPILE_FLAGS],
367 [dnl
368 m4_assert([$# >= 3])[]dnl
369 AC_MSG_CHECKING([compiler flag for $2])
370 sr_ccf_result=no
371 sr_ccf_save_CPPFLAGS=$CPPFLAGS
372 for sr_flag in $3
373 do
374         CPPFLAGS="$sr_ccf_save_CPPFLAGS $sr_flag"
375         AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])], [sr_ccf_result=$sr_flag])
376         test "x$sr_ccf_result" = xno || break
377 done
378 CPPFLAGS=$sr_ccf_save_CPPFLAGS
379 AS_IF([test "x$sr_ccf_result" != xno],
380         [SR_APPEND([$1], [$sr_ccf_result])])
381 AC_MSG_RESULT([$sr_ccf_result])
382 AC_SUBST([$1])
383 ])
384
385 ## _SR_ARG_ENABLE_WARNINGS_ONCE
386 ##
387 ## Implementation helper macro of SR_ARG_ENABLE_WARNINGS. Pulled in
388 ## through AC_REQUIRE so that it is only expanded once.
389 ##
390 m4_define([_SR_ARG_ENABLE_WARNINGS_ONCE],
391 [dnl
392 AC_PROVIDE([$0])[]dnl
393 AC_ARG_ENABLE([warnings],
394                 [AS_HELP_STRING([[--enable-warnings[=min|max|fatal|no]]],
395                                 [set compile pedantry level [default=max]])],
396                 [sr_enable_warnings=$enableval],
397                 [sr_enable_warnings=max])[]dnl
398 dnl
399 # Test whether the compiler accepts each flag.  Look at standard output,
400 # since GCC only shows a warning message if an option is not supported.
401 sr_check_compile_warning_flags() {
402         for sr_flag
403         do
404                 sr_cc_out=`$sr_cc $sr_warning_flags $sr_flag -c "$sr_conftest" 2>&1 || echo failed`
405                 AS_IF([test "$?$sr_cc_out" = 0],
406                         [SR_APPEND([sr_warning_flags], [$sr_flag])],
407                         [AS_ECHO(["$sr_cc: $sr_cc_out"]) >&AS_MESSAGE_LOG_FD])
408                 rm -f "conftest.[$]{OBJEXT:-o}"
409         done
410 }
411 ])
412
413 ## SR_ARG_ENABLE_WARNINGS(variable, min-flags, max-flags)
414 ##
415 ## Provide the --enable-warnings configure argument, set to "min" by default.
416 ## <min-flags> and <max-flags> should be space-separated lists of compiler
417 ## warning flags to use with --enable-warnings=min or --enable-warnings=max,
418 ## respectively. Warning level "fatal" is the same as "max" but in addition
419 ## enables -Werror mode.
420 ##
421 ## In order to determine the warning options to use with the C++ compiler,
422 ## call AC_LANG([C++]) first to change the current language. If different
423 ## output variables are used, it is also fine to call SR_ARG_ENABLE_WARNINGS
424 ## repeatedly, once for each language setting.
425 ##
426 AC_DEFUN([SR_ARG_ENABLE_WARNINGS],
427 [dnl
428 m4_assert([$# >= 3])[]dnl
429 AC_REQUIRE([_SR_ARG_ENABLE_WARNINGS_ONCE])[]dnl
430 dnl
431 AS_CASE([$ac_compile],
432         [[*'$CXXFLAGS '*]], [sr_lang='C++' sr_cc=$CXX sr_conftest="conftest.[$]{ac_ext:-cc}"],
433         [[*'$CFLAGS '*]],   [sr_lang=C sr_cc=$CC sr_conftest="conftest.[$]{ac_ext:-c}"],
434         [AC_MSG_ERROR([[current language is neither C nor C++]])])
435 dnl
436 AC_MSG_CHECKING([which $sr_lang compiler warning flags to use])
437 sr_warning_flags=
438 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
439 int main(int argc, char** argv) { return (argv != 0) ? argc : 0; }
440 ]])])
441 AS_CASE([$sr_enable_warnings],
442         [no], [],
443         [min], [sr_check_compile_warning_flags $2],
444         [fatal], [sr_check_compile_warning_flags $3 -Werror],
445         [sr_check_compile_warning_flags $3])
446 rm -f "$sr_conftest"
447 AC_SUBST([$1], [$sr_warning_flags])
448 AC_MSG_RESULT([[$]{sr_warning_flags:-none}])[]dnl
449 ])