]> sigrok.org Git - libsigrok.git/blame - m4/sigrok.m4
hp-3457a: free memory that was allocated by SCPI get routines
[libsigrok.git] / m4 / sigrok.m4
CommitLineData
24138539
DE
1##
2## This file is part of the sigrok project.
3##
64bc73f5
UH
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
24138539
DE
9## the Free Software Foundation, either version 3 of the License, or
10## (at your option) any later version.
11##
64bc73f5 12## This program is distributed in the hope that it will be useful,
24138539 13## but WITHOUT ANY WARRANTY; without even the implied warranty of
64bc73f5 14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24138539
DE
15## GNU General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
64bc73f5
UH
18## along with this program. If not, see <http://www.gnu.org/licenses/>.
19##
24138539 20
69f68553 21#serial 20150910
24138539
DE
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##
30AC_DEFUN([SR_APPEND],
31[dnl
32m4_assert([$# >= 2])[]dnl
33$1=[$]{$1[}]m4_if([$#], [2], [[$]{$1:+' '}$2], [[$]{$1:+$2}$3])[]dnl
34])
35
bc8ff24d
DE
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##
43AC_DEFUN([SR_PREPEND],
44[dnl
45m4_assert([$# >= 2])[]dnl
46$1=m4_if([$#], [2], [$2[$]{$1:+' '}], [$3[$]{$1:+$2}])[$]$1[]dnl
47])
48
24138539
DE
49## _SR_PKG_VERSION_SET(var-prefix, pkg-name, tag-prefix, base-version, major, minor, [micro])
50##
51m4_define([_SR_PKG_VERSION_SET],
52[dnl
53m4_assert([$# >= 6])[]dnl
54$1=$4
55sr_git_deps=
56# Check if we can get revision information from git.
57sr_head=`git -C "$srcdir" rev-parse --verify --short HEAD 2>&AS_MESSAGE_LOG_FD`
58
59AS_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.
74AS_IF([test -n "$sr_git_deps"],
75 [SR_APPEND([CONFIG_STATUS_DEPENDENCIES], ["\$(wildcard$sr_git_deps)"])])
76AC_SUBST([CONFIG_STATUS_DEPENDENCIES])[]dnl
77AC_SUBST([$1])[]dnl
78dnl
79AC_DEFINE([$1_MAJOR], [$5], [Major version number of $2.])[]dnl
80AC_DEFINE([$1_MINOR], [$6], [Minor version number of $2.])[]dnl
81m4_ifval([$7], [AC_DEFINE([$1_MICRO], [$7], [Micro version number of $2.])])[]dnl
82AC_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##
94AC_DEFUN([SR_PKG_VERSION_SET],
95[dnl
96m4_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##
106m4_define([_SR_LIB_VERSION_SET],
107[dnl
108m4_assert([$# >= 6])[]dnl
109$1=$3
110AC_SUBST([$1])[]dnl
111AC_DEFINE([$1_CURRENT], [$4], [Binary version of $2.])[]dnl
112AC_DEFINE([$1_REVISION], [$5], [Binary revision of $2.])[]dnl
113AC_DEFINE([$1_AGE], [$6], [Binary age of $2.])[]dnl
114AC_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##
125AC_DEFUN([SR_LIB_VERSION_SET],
126[dnl
127m4_assert([$# >= 1])[]dnl
128_SR_LIB_VERSION_SET([$1],
129 m4_defn([AC_PACKAGE_NAME]),
130 [$2], m4_unquote(m4_split([$2], [:])))
131])
132
bc8ff24d
DE
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##
139AC_DEFUN([SR_SEARCH_LIBS],
140[dnl
141m4_assert([$# >= 3])[]dnl
142sr_sl_save_LIBS=$LIBS
143AC_SEARCH_LIBS([$2], [$3],,, m4_join([$6], [[$]$1]))
144LIBS=$sr_sl_save_LIBS
145AS_CASE([$ac_cv_search_$2], [no*],,
146 [SR_PREPEND([$1], [$ac_cv_search_$2])])
147m4_ifvaln([$4$5], [AS_IF([test "x$ac_cv_search_$2" = xno], [$5], [$4])])[]dnl
148AC_SUBST([$1])[]dnl
149])
150
1bb196e4
DE
151## _SR_VAR_SUMMARY(tag, var-name, line-leader, align-columns, align-char)
152##
153m4_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##
168AC_DEFUN([SR_VAR_SUMMARY],
169[dnl
170m4_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##
184AC_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##
194AC_DEFUN([SR_PKG_CHECK],
195[dnl
196m4_assert([$# >= 3])[]dnl
197AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
198AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
199dnl
200PKG_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##
218AC_DEFUN([SR_VAR_OPT_PKG],
219[dnl
220m4_define([_SR_VAR_OPT_PKG_MODULES], [$1])[]dnl
221m4_define([_SR_VAR_OPT_PKG_FEATURES], [$2])[]dnl
222m4_ifvaln([$1], [$1=])[]dnl
223m4_ifvaln([$2], [$2=])[]dnl
224])
225
38662688
DE
226## _SR_ARG_OPT_IMPL(sh-name, [features-var], opt-name,
227## [cpp-name], [cond-name], check-commands)
1bb196e4 228##
38662688 229m4_define([_SR_ARG_OPT_IMPL],
1bb196e4 230[dnl
38662688
DE
231AC_ARG_WITH([$3], [AS_HELP_STRING([--without-$3],
232 [disable $3 support [default=detect]])])
1bb196e4 233AS_IF([test "x$with_$1" = xno], [sr_have_$1=no],
38662688
DE
234 [test "x$sr_have_$1" != xyes], [dnl
235AC_MSG_CHECKING([for $3])
236$6
237AC_MSG_RESULT([$sr_have_$1])[]dnl
238])
1bb196e4 239AS_IF([test "x$with_$1$sr_have_$1" = xyesno],
38662688
DE
240 [AC_MSG_ERROR([$3 support requested, but it was not found.])])
241AS_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
1bb196e4 244])
38662688
DE
245m4_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##
251m4_define([_SR_ARG_OPT_CHECK],
252[dnl
253_SR_ARG_OPT_IMPL($@)
254sr_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##
272AC_DEFUN([SR_ARG_OPT_CHECK],
273[dnl
274m4_assert([$# >= 4])[]dnl
275AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
276AC_REQUIRE([SR_VAR_OPT_PKG])[]dnl
277dnl
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##
286m4_define([_SR_ARG_OPT_PKG],
287[dnl
288_SR_ARG_OPT_IMPL([$5], [$1], [$3], [$4], [$2],
289 [SR_PKG_CHECK(m4_shiftn([4], $@))])
290m4_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
1bb196e4
DE
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##
308AC_DEFUN([SR_ARG_OPT_PKG],
309[dnl
310m4_assert([$# >= 4])[]dnl
311AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
312AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
313AC_REQUIRE([SR_VAR_OPT_PKG])[]dnl
314dnl
38662688
DE
315_SR_ARG_OPT_PKG(m4_defn([_SR_VAR_OPT_PKG_FEATURES]),
316 [$3], [$1], [$2],
317 m4_expand([AS_TR_SH([$1])]),
1bb196e4 318 m4_defn([_SR_VAR_OPT_PKG_MODULES]),
38662688 319 m4_shift3($@))[]dnl
1bb196e4
DE
320])
321
a6e8a8f7
DE
322## SR_PROG_VERSION(program, sh-var)
323##
324## Obtain the version of <program> and store it in <sh-var>.
325##
326AC_DEFUN([SR_PROG_VERSION],
327[dnl
328m4_assert([$# >= 2])[]dnl
329sr_prog_ver=`$1 --version 2>&AS_MESSAGE_LOG_FD | sed 1q 2>&AS_MESSAGE_LOG_FD`
330AS_CASE([[$]?:$sr_prog_ver],
331 [[0:*[0-9].[0-9]*]], [$2=$sr_prog_ver],
332 [$2=unknown])[]dnl
333])
334
c2b0f429
DE
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##
341AC_DEFUN([SR_PROG_MAKE_ORDER_ONLY],
342[dnl
c2b0f429
DE
343AC_CACHE_CHECK([whether [$]{MAKE:-make} supports order-only prerequisites],
344 [sr_cv_prog_make_order_only], [
345cat >conftest.mk <<'_SREOF'
346a: b | c
347a b c: ; @:
348.PHONY: a b c
349_SREOF
350AS_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])
352rm -f conftest.mk
353])
354AS_IF([test "x$sr_cv_prog_make_order_only" = xyes], [ORDER='|'], [ORDER=])
355AC_SUBST([ORDER])
c1783861 356AM_SUBST_NOTMAKE([ORDER])[]dnl
c2b0f429
DE
357])
358
b84a7331
DE
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##
366AC_DEFUN([SR_CHECK_COMPILE_FLAGS],
367[dnl
368m4_assert([$# >= 3])[]dnl
369AC_MSG_CHECKING([compiler flag for $2])
370sr_ccf_result=no
371sr_ccf_save_CPPFLAGS=$CPPFLAGS
372for sr_flag in $3
373do
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
377done
378CPPFLAGS=$sr_ccf_save_CPPFLAGS
2d2240bb
DE
379AS_IF([test "x$sr_ccf_result" != xno],
380 [SR_APPEND([$1], [$sr_ccf_result])])
b84a7331
DE
381AC_MSG_RESULT([$sr_ccf_result])
382AC_SUBST([$1])
383])
384
24138539
DE
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##
390m4_define([_SR_ARG_ENABLE_WARNINGS_ONCE],
391[dnl
392AC_PROVIDE([$0])[]dnl
393AC_ARG_ENABLE([warnings],
394 [AS_HELP_STRING([[--enable-warnings[=min|max|fatal|no]]],
4cf2f34f 395 [set compile pedantry level [default=max]])],
24138539 396 [sr_enable_warnings=$enableval],
4cf2f34f 397 [sr_enable_warnings=max])[]dnl
24138539
DE
398dnl
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.
401sr_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##
426AC_DEFUN([SR_ARG_ENABLE_WARNINGS],
427[dnl
428m4_assert([$# >= 3])[]dnl
429AC_REQUIRE([_SR_ARG_ENABLE_WARNINGS_ONCE])[]dnl
430dnl
431AS_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++]])])
435dnl
436AC_MSG_CHECKING([which $sr_lang compiler warning flags to use])
437sr_warning_flags=
438AC_LANG_CONFTEST([AC_LANG_SOURCE([[
439int main(int argc, char** argv) { return (argv != 0) ? argc : 0; }
440]])])
441AS_CASE([$sr_enable_warnings],
442 [no], [],
4cf2f34f 443 [min], [sr_check_compile_warning_flags $2],
24138539 444 [fatal], [sr_check_compile_warning_flags $3 -Werror],
4cf2f34f 445 [sr_check_compile_warning_flags $3])
24138539
DE
446rm -f "$sr_conftest"
447AC_SUBST([$1], [$sr_warning_flags])
448AC_MSG_RESULT([[$]{sr_warning_flags:-none}])[]dnl
449])