]> sigrok.org Git - libsigrokdecode.git/blob - m4/sigrok.m4
2d0d70fc0af83da629ec5f21e3dc7a9a85e6dee6
[libsigrokdecode.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 20150825
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_PKG(sh-name, [modules-var], [features-var],
227 ##                 opt-name, [cpp-name], [cond-name], module...)
228 ##
229 m4_define([_SR_ARG_OPT_PKG],
230 [dnl
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
243 ])
244 m4_ifvaln([$6], [AM_CONDITIONAL([$6], [test "x$sr_have_$1" = xyes])])[]dnl
245 ])
246
247 ## SR_ARG_OPT_PKG(opt-name, [cpp-name], [cond-name], module...)
248 ##
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.
252 ##
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.
257 ##
258 ## Use SR_VAR_OPT_PKG to generate lists of available modules and features.
259 ##
260 AC_DEFUN([SR_ARG_OPT_PKG],
261 [dnl
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
266 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]),
270         $@)[]dnl
271 ])
272
273 ## SR_CHECK_COMPILE_FLAGS(flags-var, description, flags)
274 ##
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
278 ## on <flags-var>.
279 ##
280 AC_DEFUN([SR_CHECK_COMPILE_FLAGS],
281 [dnl
282 m4_assert([$# >= 3])[]dnl
283 AC_MSG_CHECKING([compiler flag for $2])
284 sr_ccf_result=no
285 sr_ccf_save_CPPFLAGS=$CPPFLAGS
286 for sr_flag in $3
287 do
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
291 done
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])
296 AC_SUBST([$1])
297 ])
298
299 ## _SR_ARG_ENABLE_WARNINGS_ONCE
300 ##
301 ## Implementation helper macro of SR_ARG_ENABLE_WARNINGS. Pulled in
302 ## through AC_REQUIRE so that it is only expanded once.
303 ##
304 m4_define([_SR_ARG_ENABLE_WARNINGS_ONCE],
305 [dnl
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
312 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() {
316         for sr_flag
317         do
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}"
323         done
324 }
325 ])
326
327 ## SR_ARG_ENABLE_WARNINGS(variable, min-flags, max-flags)
328 ##
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.
334 ##
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.
339 ##
340 AC_DEFUN([SR_ARG_ENABLE_WARNINGS],
341 [dnl
342 m4_assert([$# >= 3])[]dnl
343 AC_REQUIRE([_SR_ARG_ENABLE_WARNINGS_ONCE])[]dnl
344 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++]])])
349 dnl
350 AC_MSG_CHECKING([which $sr_lang compiler warning flags to use])
351 sr_warning_flags=
352 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
353 int main(int argc, char** argv) { return (argv != 0) ? argc : 0; }
354 ]])])
355 AS_CASE([$sr_enable_warnings],
356         [no], [],
357         [min], [sr_check_compile_warning_flags $2],
358         [fatal], [sr_check_compile_warning_flags $3 -Werror],
359         [sr_check_compile_warning_flags $3])
360 rm -f "$sr_conftest"
361 AC_SUBST([$1], [$sr_warning_flags])
362 AC_MSG_RESULT([[$]{sr_warning_flags:-none}])[]dnl
363 ])