2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2009 Openismus GmbH
5 ## Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com>
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.
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.
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/>.
23 ## SR_APPEND(var-name, [list-sep,] element)
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
32 m4_assert([$# >= 2])[]dnl
33 $1=[$]{$1[}]m4_if([$#], [2], [[$]{$1:+' '}$2], [[$]{$1:+$2}$3])[]dnl
36 ## SR_PREPEND(var-name, [list-sep,] element)
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
43 AC_DEFUN([SR_PREPEND],
45 m4_assert([$# >= 2])[]dnl
46 $1=m4_if([$#], [2], [$2[$]{$1:+' '}], [$3[$]{$1:+$2}])[$]$1[]dnl
49 ## _SR_PKG_VERSION_SET(var-prefix, pkg-name, tag-prefix, base-version, major, minor, [micro])
51 m4_define([_SR_PKG_VERSION_SET],
53 m4_assert([$# >= 6])[]dnl
55 dnl Check if we can get version control details. Re-configure when
56 dnl branches change (when HEAD starts pointing somewhere else).
57 dnl Track individual revisions at compile time, and only as a local
58 dnl dependency of just a part of the library build. In other words:
59 dnl Don't re-configure and re-build everything just because a commit
60 dnl happened). Checks for tagged sources also happen at compile time.
62 sr_head=`git -C "$srcdir" rev-parse --verify --short HEAD 2>&AS_MESSAGE_LOG_FD`
63 AS_IF([test "$?" = 0 && test -n "$sr_head"], [dnl
64 test ! -f "$srcdir/.git/HEAD" || \
65 sr_git_deps="$sr_git_deps ${ac_abs_confdir}/.git/HEAD"
66 sr_head_name=`git -C "$srcdir" rev-parse --symbolic-full-name HEAD 2>&AS_MESSAGE_LOG_FD`
67 AS_IF([test "$?" = 0 && test -f "$srcdir/.git/$sr_head_name"], [dnl
68 sr_head_file="${ac_abs_confdir}/.git/$sr_head_name"
69 AC_SUBST(VERSION_HEAD_FILE, [$sr_head_file])
71 sr_hash=`git -C "$srcdir" describe --match "$3$4" --always --dirty`
72 sr_hash=`echo "$sr_hash" | sed 's/$3$4-//'` && \
73 $1_STRING_SUFFIX=`git -C "$srcdir" describe --match "$3$4" --exact-match > /dev/null 2> /dev/null || echo "-$sr_hash"`
74 AS_IF([test -n "$1_STRING_SUFFIX"], [$1="[$]$1-git"])
76 AM_CONDITIONAL([VCS_IS_GIT], [test -n "$sr_git_deps"])
77 # Use $(wildcard) so that things do not break if for whatever reason
78 # these files do not exist anymore at make time.
79 AS_IF([test -n "$sr_git_deps"],
80 [SR_APPEND([CONFIG_STATUS_DEPENDENCIES], ["\$(wildcard $sr_git_deps)"])])
81 AC_SUBST([CONFIG_STATUS_DEPENDENCIES])[]dnl
82 AS_IF([test -n "$sr_git_deps$sr_head_file"],
83 [SR_APPEND([VERSION_GITVERSION_DEPS], ["\$(wildcard $sr_git_deps $sr_head_file)"])])
84 AC_SUBST(VERSION_GITVERSION_DEPS)[]dnl
85 AC_SUBST(VERSION_SOURCE_DIR, [${ac_abs_confdir}])
86 AC_SUBST(VERSION_TAG_PREFIX, [$3])[]dnl
87 AC_SUBST(VERSION_TAG_NUMBER, [$4])[]dnl
88 AC_SUBST(VERSION_TAG_MATCH, [$3$4])[]dnl
89 dnl End of git version control details gathering.
93 AC_DEFINE([$1_MAJOR], [$5], [Major version number of $2.])[]dnl
94 AC_DEFINE([$1_MINOR], [$6], [Minor version number of $2.])[]dnl
95 m4_ifval([$7], [AC_DEFINE([$1_MICRO], [$7], [Micro version number of $2.])])[]dnl
96 AC_DEFINE_UNQUOTED([$1_STRING_PREFIX], ["[$]$1"], [Version of $2.])[]dnl
99 ## SR_PKG_VERSION_SET(var-prefix, version-triple)
101 ## Set up substitution variables and macro definitions for the package
102 ## version components. Derive the version suffix from the repository
103 ## revision if possible.
105 ## Substitutions: <var-prefix>
106 ## Macro defines: <var-prefix>_{MAJOR,MINOR,MICRO,STRING}
108 AC_DEFUN([SR_PKG_VERSION_SET],
110 m4_assert([$# >= 2])[]dnl
111 _SR_PKG_VERSION_SET([$1],
112 m4_defn([AC_PACKAGE_NAME]),
113 m4_defn([AC_PACKAGE_TARNAME])[-],
115 m4_unquote(m4_split(m4_expand([$2]), [\.])))
118 ## _SR_LIB_VERSION_SET(var-prefix, pkg-name, abi-triple, current, revision, age)
120 m4_define([_SR_LIB_VERSION_SET],
122 m4_assert([$# >= 6])[]dnl
125 AC_DEFINE([$1_CURRENT], [$4], [Binary version of $2.])[]dnl
126 AC_DEFINE([$1_REVISION], [$5], [Binary revision of $2.])[]dnl
127 AC_DEFINE([$1_AGE], [$6], [Binary age of $2.])[]dnl
128 AC_DEFINE([$1_STRING], ["$3"], [Binary version triple of $2.])[]dnl
131 ## SR_LIB_VERSION_SET(var-prefix, abi-triple)
133 ## Set up substitution variables and macro definitions for a library
136 ## Substitutions: <var-prefix>
137 ## Macro defines: <var-prefix>_{CURRENT,REVISION,AGE,STRING}
139 AC_DEFUN([SR_LIB_VERSION_SET],
141 m4_assert([$# >= 1])[]dnl
142 _SR_LIB_VERSION_SET([$1],
143 m4_defn([AC_PACKAGE_NAME]),
144 [$2], m4_unquote(m4_split([$2], [:])))
147 ## SR_SEARCH_LIBS(libs-var, function, search-libs,
148 ## [action-if-found], [action-if-not-found], [other-libs])
150 ## Same as AC_SEARCH_LIBS, except that the result is prepended
151 ## to <libs-var> instead of LIBS. Calls AC_SUBST on <libs-var>.
153 AC_DEFUN([SR_SEARCH_LIBS],
155 m4_assert([$# >= 3])[]dnl
156 sr_sl_save_LIBS=$LIBS
157 AC_SEARCH_LIBS([$2], [$3],,, m4_join([$6], [[$]$1]))
158 LIBS=$sr_sl_save_LIBS
159 AS_CASE([$ac_cv_search_$2], [no*],,
160 [SR_PREPEND([$1], [$ac_cv_search_$2])])
161 m4_ifvaln([$4$5], [AS_IF([test "x$ac_cv_search_$2" = xno], [$5], [$4])])[]dnl
165 ## _SR_VAR_SUMMARY(tag, var-name, line-leader, align-columns, align-char)
167 m4_define([_SR_VAR_SUMMARY], [dnl
170 sr_aligned=`printf '%.$4s' "[$][1]m4_for([i], [1], [$4],, [$5])"`
171 $2="[$]{$2}$3$sr_aligned [$]2"'
176 ## SR_VAR_SUMMARY(tag, [var-name = <tag>],
177 ## [line-leader = [ - ]], [align-columns = 32], [align-char = .])
179 ## Define a shell function <tag>_append() to be used for aggregating
180 ## a summary of test results in the shell variable <var-name>.
182 AC_DEFUN([SR_VAR_SUMMARY],
184 m4_assert([$# >= 1])[]dnl
185 _SR_VAR_SUMMARY([$1],
186 m4_default_quoted([$2], [$1]),
187 m4_default_quoted([$3], [ - ]),
188 m4_default_quoted([$4], [32]),
189 m4_default_quoted([$5], [.]))[]dnl
192 ## SR_PKG_CHECK_SUMMARY([var-name = sr_pkg_check_summary],
193 ## [line-leader = [ - ]], [align-columns = 32], [align-char = .])
195 ## Prepare for the aggregation of package check results
196 ## in the shell variable <var-name>.
198 AC_DEFUN([SR_PKG_CHECK_SUMMARY],
199 [SR_VAR_SUMMARY([sr_pkg_check_summary], $@)])
201 ## SR_PKG_CHECK(tag, [collect-var], module...)
203 ## Check for each pkg-config <module> in the argument list. <module> may
204 ## include a version constraint.
206 ## Output variables: sr_have_<tag>, sr_<tag>_version
208 AC_DEFUN([SR_PKG_CHECK],
210 m4_assert([$# >= 3])[]dnl
211 AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
212 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
214 PKG_CHECK_EXISTS([$3], [dnl
216 m4_ifval([$2], [SR_APPEND([$2], ["$3"])
217 ])sr_$1_version=`$PKG_CONFIG --modversion "$3" 2>&AS_MESSAGE_LOG_FD`
218 sr_pkg_check_summary_append "$3" "$sr_$1_version"[]dnl
220 sr_pkg_check_summary_append "$3" no
222 [SR_PKG_CHECK([$1], [$2], m4_shift3($@))],
223 [sr_have_$1=no sr_$1_version=])[]dnl
227 ## SR_VAR_OPT_PKG([modules-var], [features-var])
229 ## Enable the collection of SR_ARG_OPT_PKG results into the shell variables
230 ## <modules-var> and <features-var>.
232 AC_DEFUN([SR_VAR_OPT_PKG],
234 m4_define([_SR_VAR_OPT_PKG_MODULES], [$1])[]dnl
235 m4_define([_SR_VAR_OPT_PKG_FEATURES], [$2])[]dnl
236 m4_ifvaln([$1], [$1=])[]dnl
237 m4_ifvaln([$2], [$2=])[]dnl
240 ## _SR_ARG_OPT_IMPL(sh-name, [features-var], opt-name,
241 ## [cpp-name], [cond-name], check-commands)
243 m4_define([_SR_ARG_OPT_IMPL],
245 AC_ARG_WITH([$3], [AS_HELP_STRING([--without-$3],
246 [disable $3 support [default=detect]])])
247 AS_IF([test "x$with_$1" = xno], [sr_have_$1=no],
248 [test "x$sr_have_$1" != xyes], [dnl
249 AC_MSG_CHECKING([for $3])
251 AC_MSG_RESULT([$sr_have_$1])[]dnl
253 AS_IF([test "x$with_$1$sr_have_$1" = xyesno],
254 [AC_MSG_ERROR([$3 support requested, but it was not found.])])
255 AS_IF([test "x$sr_have_$1" = xyes], [m4_ifval([$2], [
256 SR_APPEND([$2], ["$3"])])[]m4_ifval([$4], [
257 AC_DEFINE([HAVE_$4], [1], [Whether $3 is available.])])[]dnl
259 m4_ifvaln([$5], [AM_CONDITIONAL([$5], [test "x$sr_have_$1" = xyes])])[]dnl
262 ## _SR_ARG_OPT_CHECK(sh-name, [features-var], opt-name, [cpp-name],
263 ## [cond-name], check-commands, [summary-result])
265 m4_define([_SR_ARG_OPT_CHECK],
268 sr_pkg_check_summary_append "$3" m4_default([$7], ["$sr_have_$1"])
271 ## SR_ARG_OPT_CHECK(opt-name, [cpp-name], [cond-name], check-commands,
272 ## [summary-result = $sr_have_<opt-name>])
274 ## Provide a --without-<opt-name> configure option for explicit disabling
275 ## of an optional dependency. If not disabled, the availability of the
276 ## optional dependency is auto-detected by running <check-commands>.
278 ## The <check-commands> should set the shell variable sr_have_<opt-name>
279 ## to "yes" if the dependency is available, otherwise to "no". Optionally,
280 ## the <summary-result> argument may be used to generate a line in the
281 ## configuration summary. If supplied, it should be a shell word which
282 ## expands to the result to be displayed for the <opt-name> dependency.
284 ## Use SR_VAR_OPT_PKG to generate lists of available modules and features.
286 AC_DEFUN([SR_ARG_OPT_CHECK],
288 m4_assert([$# >= 4])[]dnl
289 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
290 AC_REQUIRE([SR_VAR_OPT_PKG])[]dnl
292 _SR_ARG_OPT_CHECK(m4_expand([AS_TR_SH([$1])]),
293 m4_defn([_SR_VAR_OPT_PKG_FEATURES]),
297 ## _SR_ARG_OPT_PKG([features-var], [cond-name], opt-name,
298 ## [cpp-name], sh-name, [modules-var], module...)
300 m4_define([_SR_ARG_OPT_PKG],
302 _SR_ARG_OPT_IMPL([$5], [$1], [$3], [$4], [$2],
303 [SR_PKG_CHECK(m4_shiftn([4], $@))])
304 m4_ifvaln([$4], [AS_IF([test "x$sr_have_$5" = xyes],
305 [AC_DEFINE_UNQUOTED([CONF_$4_VERSION], ["$sr_$5_version"],
306 [Build-time version of $3.])])])[]dnl
309 ## SR_ARG_OPT_PKG(opt-name, [cpp-name], [cond-name], module...)
311 ## Provide a --without-<opt-name> configure option for explicit disabling
312 ## of an optional dependency. If not disabled, the availability of the
313 ## optional dependency is auto-detected.
315 ## Each pkg-config <module> argument is checked in turn, and the first one
316 ## found is selected. On success, the shell variable sr_have_<opt-name>
317 ## is set to "yes", otherwise to "no". Optionally, a preprocessor macro
318 ## HAVE_<cpp-name> and an Automake conditional <cond-name> are generated.
320 ## Use SR_VAR_OPT_PKG to generate lists of available modules and features.
322 AC_DEFUN([SR_ARG_OPT_PKG],
324 m4_assert([$# >= 4])[]dnl
325 AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
326 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
327 AC_REQUIRE([SR_VAR_OPT_PKG])[]dnl
329 _SR_ARG_OPT_PKG(m4_defn([_SR_VAR_OPT_PKG_FEATURES]),
331 m4_expand([AS_TR_SH([$1])]),
332 m4_defn([_SR_VAR_OPT_PKG_MODULES]),
336 ## SR_PROG_VERSION(program, sh-var)
338 ## Obtain the version of <program> and store it in <sh-var>.
340 AC_DEFUN([SR_PROG_VERSION],
342 m4_assert([$# >= 2])[]dnl
343 sr_prog_ver=`$1 --version 2>&AS_MESSAGE_LOG_FD | sed 1q 2>&AS_MESSAGE_LOG_FD`
344 AS_CASE([[$]?:$sr_prog_ver],
345 [[0:*[0-9].[0-9]*]], [$2=$sr_prog_ver],
349 ## SR_PROG_MAKE_ORDER_ONLY
351 ## Check whether the make program supports order-only prerequisites.
352 ## If so, set the substitution variable ORDER to '|', or to the empty
355 AC_DEFUN([SR_PROG_MAKE_ORDER_ONLY],
357 AC_CACHE_CHECK([whether [$]{MAKE:-make} supports order-only prerequisites],
358 [sr_cv_prog_make_order_only], [
359 cat >conftest.mk <<'_SREOF'
364 AS_IF([[$]{MAKE:-make} -f conftest.mk >&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD],
365 [sr_cv_prog_make_order_only=yes], [sr_cv_prog_make_order_only=no])
368 AS_IF([test "x$sr_cv_prog_make_order_only" = xyes], [ORDER='|'], [ORDER=])
370 AM_SUBST_NOTMAKE([ORDER])[]dnl
373 ## SR_CHECK_COMPILE_FLAGS(flags-var, description, flags)
375 ## Find a compiler flag for <description>. For each flag in <flags>, check
376 ## if the compiler for the current language accepts it. On success, stop the
377 ## search and append the last tested flag to <flags-var>. Calls AC_SUBST
380 AC_DEFUN([SR_CHECK_COMPILE_FLAGS],
382 m4_assert([$# >= 3])[]dnl
383 AC_MSG_CHECKING([compiler flag for $2])
385 sr_ccf_save_CPPFLAGS=$CPPFLAGS
388 CPPFLAGS="$sr_ccf_save_CPPFLAGS $sr_flag"
389 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])], [sr_ccf_result=$sr_flag])
390 test "x$sr_ccf_result" = xno || break
392 CPPFLAGS=$sr_ccf_save_CPPFLAGS
393 AS_IF([test "x$sr_ccf_result" != xno],
394 [SR_APPEND([$1], [$sr_ccf_result])])
395 AC_MSG_RESULT([$sr_ccf_result])
399 ## _SR_ARG_ENABLE_WARNINGS_ONCE
401 ## Implementation helper macro of SR_ARG_ENABLE_WARNINGS. Pulled in
402 ## through AC_REQUIRE so that it is only expanded once.
404 m4_define([_SR_ARG_ENABLE_WARNINGS_ONCE],
406 AC_PROVIDE([$0])[]dnl
407 AC_ARG_ENABLE([warnings],
408 [AS_HELP_STRING([[--enable-warnings[=min|max|fatal|no]]],
409 [set compile pedantry level [default=max]])],
410 [sr_enable_warnings=$enableval],
411 [sr_enable_warnings=max])[]dnl
413 # Test whether the compiler accepts each flag. Look at standard output,
414 # since GCC only shows a warning message if an option is not supported.
415 sr_check_compile_warning_flags() {
418 sr_cc_out=`$sr_cc $sr_warning_flags $sr_flag -c "$sr_conftest" 2>&1 || echo failed`
419 AS_IF([test "$?$sr_cc_out" = 0],
420 [SR_APPEND([sr_warning_flags], [$sr_flag])],
421 [AS_ECHO(["$sr_cc: $sr_cc_out"]) >&AS_MESSAGE_LOG_FD])
422 rm -f "conftest.[$]{OBJEXT:-o}"
427 ## SR_ARG_ENABLE_WARNINGS(variable, min-flags, max-flags)
429 ## Provide the --enable-warnings configure argument, set to "min" by default.
430 ## <min-flags> and <max-flags> should be space-separated lists of compiler
431 ## warning flags to use with --enable-warnings=min or --enable-warnings=max,
432 ## respectively. Warning level "fatal" is the same as "max" but in addition
433 ## enables -Werror mode.
435 ## In order to determine the warning options to use with the C++ compiler,
436 ## call AC_LANG([C++]) first to change the current language. If different
437 ## output variables are used, it is also fine to call SR_ARG_ENABLE_WARNINGS
438 ## repeatedly, once for each language setting.
440 AC_DEFUN([SR_ARG_ENABLE_WARNINGS],
442 m4_assert([$# >= 3])[]dnl
443 AC_REQUIRE([_SR_ARG_ENABLE_WARNINGS_ONCE])[]dnl
445 AS_CASE([$ac_compile],
446 [[*'$CXXFLAGS '*]], [sr_lang='C++' sr_cc=$CXX sr_conftest="conftest.[$]{ac_ext:-cc}"],
447 [[*'$CFLAGS '*]], [sr_lang=C sr_cc=$CC sr_conftest="conftest.[$]{ac_ext:-c}"],
448 [AC_MSG_ERROR([[current language is neither C nor C++]])])
450 AC_MSG_CHECKING([which $sr_lang compiler warning flags to use])
452 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
453 int main(int argc, char** argv) { return (argv != 0) ? argc : 0; }
455 AS_CASE([$sr_enable_warnings],
457 [min], [sr_check_compile_warning_flags $2],
458 [fatal], [sr_check_compile_warning_flags $3 -Werror],
459 [sr_check_compile_warning_flags $3])
461 AC_SUBST([$1], [$sr_warning_flags])
462 AC_MSG_RESULT([[$]{sr_warning_flags:-none}])[]dnl