]> sigrok.org Git - libsigrok.git/blame - m4/sigrok.m4
sigrok.m4: Add SR_PROG_MAKE_NO_PRINT_DIRECTORY macro
[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
64bc73f5 21#serial 20150825
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
82b01e42
DE
322## SR_PROG_MAKE_NO_PRINT_DIRECTORY
323##
324## Check whether the make program supports the --no-print-directory flag.
325## If so, add it to AM_MAKEFLAGS.
326##
327AC_DEFUN([SR_PROG_MAKE_NO_PRINT_DIRECTORY],
328[dnl
329AC_CACHE_CHECK([whether [$]{MAKE:-make} supports --no-print-directory],
330 [sr_cv_prog_make_no_print_dir], [
331cat >conftest.mk <<'_SREOF'
332all: ; @:
333.PHONY: all
334_SREOF
335AS_IF([[$]{MAKE:-make} -f conftest.mk --no-print-directory >&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD],
336 [sr_cv_prog_make_no_print_dir=yes], [sr_cv_prog_make_no_print_dir=no])
337rm -f conftest.mk
338])
339AS_IF([test "x$sr_cv_prog_make_no_print_dir" = xyes],
340 [SR_APPEND([AM_MAKEFLAGS], [--no-print-directory])])
341AC_SUBST([AM_MAKEFLAGS])
342])
343
c2b0f429
DE
344## SR_PROG_MAKE_ORDER_ONLY
345##
346## Check whether the make program supports order-only prerequisites.
347## If so, set the substitution variable ORDER to '|', or to the empty
348## string otherwise.
349##
350AC_DEFUN([SR_PROG_MAKE_ORDER_ONLY],
351[dnl
c2b0f429
DE
352AC_CACHE_CHECK([whether [$]{MAKE:-make} supports order-only prerequisites],
353 [sr_cv_prog_make_order_only], [
354cat >conftest.mk <<'_SREOF'
355a: b | c
356a b c: ; @:
357.PHONY: a b c
358_SREOF
359AS_IF([[$]{MAKE:-make} -f conftest.mk >&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD],
360 [sr_cv_prog_make_order_only=yes], [sr_cv_prog_make_order_only=no])
361rm -f conftest.mk
362])
363AS_IF([test "x$sr_cv_prog_make_order_only" = xyes], [ORDER='|'], [ORDER=])
364AC_SUBST([ORDER])
365])
366
b84a7331
DE
367## SR_CHECK_COMPILE_FLAGS(flags-var, description, flags)
368##
369## Find a compiler flag for <description>. For each flag in <flags>, check
370## if the compiler for the current language accepts it. On success, stop the
371## search and append the last tested flag to <flags-var>. Calls AC_SUBST
372## on <flags-var>.
373##
374AC_DEFUN([SR_CHECK_COMPILE_FLAGS],
375[dnl
376m4_assert([$# >= 3])[]dnl
377AC_MSG_CHECKING([compiler flag for $2])
378sr_ccf_result=no
379sr_ccf_save_CPPFLAGS=$CPPFLAGS
380for sr_flag in $3
381do
382 CPPFLAGS="$sr_ccf_save_CPPFLAGS $sr_flag"
383 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])], [sr_ccf_result=$sr_flag])
384 test "x$sr_ccf_result" = xno || break
385done
386CPPFLAGS=$sr_ccf_save_CPPFLAGS
2d2240bb
DE
387AS_IF([test "x$sr_ccf_result" != xno],
388 [SR_APPEND([$1], [$sr_ccf_result])])
b84a7331
DE
389AC_MSG_RESULT([$sr_ccf_result])
390AC_SUBST([$1])
391])
392
24138539
DE
393## _SR_ARG_ENABLE_WARNINGS_ONCE
394##
395## Implementation helper macro of SR_ARG_ENABLE_WARNINGS. Pulled in
396## through AC_REQUIRE so that it is only expanded once.
397##
398m4_define([_SR_ARG_ENABLE_WARNINGS_ONCE],
399[dnl
400AC_PROVIDE([$0])[]dnl
401AC_ARG_ENABLE([warnings],
402 [AS_HELP_STRING([[--enable-warnings[=min|max|fatal|no]]],
4cf2f34f 403 [set compile pedantry level [default=max]])],
24138539 404 [sr_enable_warnings=$enableval],
4cf2f34f 405 [sr_enable_warnings=max])[]dnl
24138539
DE
406dnl
407# Test whether the compiler accepts each flag. Look at standard output,
408# since GCC only shows a warning message if an option is not supported.
409sr_check_compile_warning_flags() {
410 for sr_flag
411 do
412 sr_cc_out=`$sr_cc $sr_warning_flags $sr_flag -c "$sr_conftest" 2>&1 || echo failed`
413 AS_IF([test "$?$sr_cc_out" = 0],
414 [SR_APPEND([sr_warning_flags], [$sr_flag])],
415 [AS_ECHO(["$sr_cc: $sr_cc_out"]) >&AS_MESSAGE_LOG_FD])
416 rm -f "conftest.[$]{OBJEXT:-o}"
417 done
418}
419])
420
421## SR_ARG_ENABLE_WARNINGS(variable, min-flags, max-flags)
422##
423## Provide the --enable-warnings configure argument, set to "min" by default.
424## <min-flags> and <max-flags> should be space-separated lists of compiler
425## warning flags to use with --enable-warnings=min or --enable-warnings=max,
426## respectively. Warning level "fatal" is the same as "max" but in addition
427## enables -Werror mode.
428##
429## In order to determine the warning options to use with the C++ compiler,
430## call AC_LANG([C++]) first to change the current language. If different
431## output variables are used, it is also fine to call SR_ARG_ENABLE_WARNINGS
432## repeatedly, once for each language setting.
433##
434AC_DEFUN([SR_ARG_ENABLE_WARNINGS],
435[dnl
436m4_assert([$# >= 3])[]dnl
437AC_REQUIRE([_SR_ARG_ENABLE_WARNINGS_ONCE])[]dnl
438dnl
439AS_CASE([$ac_compile],
440 [[*'$CXXFLAGS '*]], [sr_lang='C++' sr_cc=$CXX sr_conftest="conftest.[$]{ac_ext:-cc}"],
441 [[*'$CFLAGS '*]], [sr_lang=C sr_cc=$CC sr_conftest="conftest.[$]{ac_ext:-c}"],
442 [AC_MSG_ERROR([[current language is neither C nor C++]])])
443dnl
444AC_MSG_CHECKING([which $sr_lang compiler warning flags to use])
445sr_warning_flags=
446AC_LANG_CONFTEST([AC_LANG_SOURCE([[
447int main(int argc, char** argv) { return (argv != 0) ? argc : 0; }
448]])])
449AS_CASE([$sr_enable_warnings],
450 [no], [],
4cf2f34f 451 [min], [sr_check_compile_warning_flags $2],
24138539 452 [fatal], [sr_check_compile_warning_flags $3 -Werror],
4cf2f34f 453 [sr_check_compile_warning_flags $3])
24138539
DE
454rm -f "$sr_conftest"
455AC_SUBST([$1], [$sr_warning_flags])
456AC_MSG_RESULT([[$]{sr_warning_flags:-none}])[]dnl
457])