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
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`
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"
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"])
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"
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
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
85 ## SR_PKG_VERSION_SET(var-prefix, version-triple)
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.
91 ## Substitutions: <var-prefix>
92 ## Macro defines: <var-prefix>_{MAJOR,MINOR,MICRO,STRING}
94 AC_DEFUN([SR_PKG_VERSION_SET],
96 m4_assert([$# >= 2])[]dnl
97 _SR_PKG_VERSION_SET([$1],
98 m4_defn([AC_PACKAGE_NAME]),
99 m4_defn([AC_PACKAGE_TARNAME])[-],
101 m4_unquote(m4_split(m4_expand([$2]), [\.])))
104 ## _SR_LIB_VERSION_SET(var-prefix, pkg-name, abi-triple, current, revision, age)
106 m4_define([_SR_LIB_VERSION_SET],
108 m4_assert([$# >= 6])[]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
117 ## SR_LIB_VERSION_SET(var-prefix, abi-triple)
119 ## Set up substitution variables and macro definitions for a library
122 ## Substitutions: <var-prefix>
123 ## Macro defines: <var-prefix>_{CURRENT,REVISION,AGE,STRING}
125 AC_DEFUN([SR_LIB_VERSION_SET],
127 m4_assert([$# >= 1])[]dnl
128 _SR_LIB_VERSION_SET([$1],
129 m4_defn([AC_PACKAGE_NAME]),
130 [$2], m4_unquote(m4_split([$2], [:])))
133 ## SR_SEARCH_LIBS(libs-var, function, search-libs,
134 ## [action-if-found], [action-if-not-found], [other-libs])
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>.
139 AC_DEFUN([SR_SEARCH_LIBS],
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
151 ## _SR_VAR_SUMMARY(tag, var-name, line-leader, align-columns, align-char)
153 m4_define([_SR_VAR_SUMMARY], [dnl
156 sr_aligned=`printf '%.$4s' "[$][1]m4_for([i], [1], [$4],, [$5])"`
157 $2="[$]{$2}$3$sr_aligned [$]2"'
162 ## SR_VAR_SUMMARY(tag, [var-name = <tag>],
163 ## [line-leader = [ - ]], [align-columns = 32], [align-char = .])
165 ## Define a shell function <tag>_append() to be used for aggregating
166 ## a summary of test results in the shell variable <var-name>.
168 AC_DEFUN([SR_VAR_SUMMARY],
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
178 ## SR_PKG_CHECK_SUMMARY([var-name = sr_pkg_check_summary],
179 ## [line-leader = [ - ]], [align-columns = 32], [align-char = .])
181 ## Prepare for the aggregation of package check results
182 ## in the shell variable <var-name>.
184 AC_DEFUN([SR_PKG_CHECK_SUMMARY],
185 [SR_VAR_SUMMARY([sr_pkg_check_summary], $@)])
187 ## SR_PKG_CHECK(tag, [collect-var], module...)
189 ## Check for each pkg-config <module> in the argument list. <module> may
190 ## include a version constraint.
192 ## Output variables: sr_have_<tag>, sr_<tag>_version
194 AC_DEFUN([SR_PKG_CHECK],
196 m4_assert([$# >= 3])[]dnl
197 AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
198 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
200 PKG_CHECK_EXISTS([$3], [dnl
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
206 sr_pkg_check_summary_append "$3" no
208 [SR_PKG_CHECK([$1], [$2], m4_shift3($@))],
209 [sr_have_$1=no sr_$1_version=])[]dnl
213 ## SR_VAR_OPT_PKG([modules-var], [features-var])
215 ## Enable the collection of SR_ARG_OPT_PKG results into the shell variables
216 ## <modules-var> and <features-var>.
218 AC_DEFUN([SR_VAR_OPT_PKG],
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
226 ## _SR_ARG_OPT_PKG(sh-name, [modules-var], [features-var],
227 ## opt-name, [cpp-name], [cond-name], module...)
229 m4_define([_SR_ARG_OPT_PKG],
231 AC_ARG_WITH([$4], [AS_HELP_STRING([--without-$4],
232 [disable $4 support [default=detect]])])
233 AS_IF([test "x$with_$1" = xno], [sr_have_$1=no],
234 [test "x$sr_have_$1" != xyes],
235 [SR_PKG_CHECK([$1], [$2], m4_shiftn([6], $@))])
236 AS_IF([test "x$with_$1$sr_have_$1" = xyesno],
237 [AC_MSG_ERROR([$4 support requested, but it was not found.])])
238 AS_IF([test "x$sr_have_$1" = xyes], [m4_ifval([$3], [
239 SR_APPEND([$3], ["$4"])])[]m4_ifval([$5], [
240 AC_DEFINE([HAVE_$5], [1], [Whether $4 is available.])
241 AC_DEFINE_UNQUOTED([CONF_$5_VERSION], ["$sr_$1_version"],
242 [Build-time version of $4.])])[]dnl
244 m4_ifvaln([$6], [AM_CONDITIONAL([$6], [test "x$sr_have_$1" = xyes])])[]dnl
247 ## SR_ARG_OPT_PKG(opt-name, [cpp-name], [cond-name], module...)
249 ## Provide a --without-<opt-name> configure option for explicit disabling
250 ## of an optional dependency. If not disabled, the availability of the
251 ## optional dependency is auto-detected.
253 ## Each pkg-config <module> argument is checked in turn, and the first one
254 ## found is selected. On success, the shell variable sr_have_<opt-name>
255 ## is set to "yes", otherwise to "no". Optionally, a preprocessor macro
256 ## HAVE_<cpp-name> and an Automake conditional <cond-name> are generated.
258 ## Use SR_VAR_OPT_PKG to generate lists of available modules and features.
260 AC_DEFUN([SR_ARG_OPT_PKG],
262 m4_assert([$# >= 4])[]dnl
263 AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
264 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
265 AC_REQUIRE([SR_VAR_OPT_PKG])[]dnl
267 _SR_ARG_OPT_PKG(m4_expand([AS_TR_SH([$1])]),
268 m4_defn([_SR_VAR_OPT_PKG_MODULES]),
269 m4_defn([_SR_VAR_OPT_PKG_FEATURES]),
273 ## SR_CHECK_COMPILE_FLAGS(flags-var, description, flags)
275 ## Find a compiler flag for <description>. For each flag in <flags>, check
276 ## if the compiler for the current language accepts it. On success, stop the
277 ## search and append the last tested flag to <flags-var>. Calls AC_SUBST
280 AC_DEFUN([SR_CHECK_COMPILE_FLAGS],
282 m4_assert([$# >= 3])[]dnl
283 AC_MSG_CHECKING([compiler flag for $2])
285 sr_ccf_save_CPPFLAGS=$CPPFLAGS
288 CPPFLAGS="$sr_ccf_save_CPPFLAGS $sr_flag"
289 AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])], [sr_ccf_result=$sr_flag])
290 test "x$sr_ccf_result" = xno || break
292 CPPFLAGS=$sr_ccf_save_CPPFLAGS
293 AS_IF([test "x$sr_ccf_result" != xno],
294 [SR_APPEND([$1], [$sr_ccf_result])])
295 AC_MSG_RESULT([$sr_ccf_result])
299 ## _SR_ARG_ENABLE_WARNINGS_ONCE
301 ## Implementation helper macro of SR_ARG_ENABLE_WARNINGS. Pulled in
302 ## through AC_REQUIRE so that it is only expanded once.
304 m4_define([_SR_ARG_ENABLE_WARNINGS_ONCE],
306 AC_PROVIDE([$0])[]dnl
307 AC_ARG_ENABLE([warnings],
308 [AS_HELP_STRING([[--enable-warnings[=min|max|fatal|no]]],
309 [set compile pedantry level [default=max]])],
310 [sr_enable_warnings=$enableval],
311 [sr_enable_warnings=max])[]dnl
313 # Test whether the compiler accepts each flag. Look at standard output,
314 # since GCC only shows a warning message if an option is not supported.
315 sr_check_compile_warning_flags() {
318 sr_cc_out=`$sr_cc $sr_warning_flags $sr_flag -c "$sr_conftest" 2>&1 || echo failed`
319 AS_IF([test "$?$sr_cc_out" = 0],
320 [SR_APPEND([sr_warning_flags], [$sr_flag])],
321 [AS_ECHO(["$sr_cc: $sr_cc_out"]) >&AS_MESSAGE_LOG_FD])
322 rm -f "conftest.[$]{OBJEXT:-o}"
327 ## SR_ARG_ENABLE_WARNINGS(variable, min-flags, max-flags)
329 ## Provide the --enable-warnings configure argument, set to "min" by default.
330 ## <min-flags> and <max-flags> should be space-separated lists of compiler
331 ## warning flags to use with --enable-warnings=min or --enable-warnings=max,
332 ## respectively. Warning level "fatal" is the same as "max" but in addition
333 ## enables -Werror mode.
335 ## In order to determine the warning options to use with the C++ compiler,
336 ## call AC_LANG([C++]) first to change the current language. If different
337 ## output variables are used, it is also fine to call SR_ARG_ENABLE_WARNINGS
338 ## repeatedly, once for each language setting.
340 AC_DEFUN([SR_ARG_ENABLE_WARNINGS],
342 m4_assert([$# >= 3])[]dnl
343 AC_REQUIRE([_SR_ARG_ENABLE_WARNINGS_ONCE])[]dnl
345 AS_CASE([$ac_compile],
346 [[*'$CXXFLAGS '*]], [sr_lang='C++' sr_cc=$CXX sr_conftest="conftest.[$]{ac_ext:-cc}"],
347 [[*'$CFLAGS '*]], [sr_lang=C sr_cc=$CC sr_conftest="conftest.[$]{ac_ext:-c}"],
348 [AC_MSG_ERROR([[current language is neither C nor C++]])])
350 AC_MSG_CHECKING([which $sr_lang compiler warning flags to use])
352 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
353 int main(int argc, char** argv) { return (argv != 0) ? argc : 0; }
355 AS_CASE([$sr_enable_warnings],
357 [min], [sr_check_compile_warning_flags $2],
358 [fatal], [sr_check_compile_warning_flags $3 -Werror],
359 [sr_check_compile_warning_flags $3])
361 AC_SUBST([$1], [$sr_warning_flags])
362 AC_MSG_RESULT([[$]{sr_warning_flags:-none}])[]dnl