]> sigrok.org Git - libsigrok.git/blob - m4/sigrok.m4
e9052585c2339e302130e865d102b9cbc4745902
[libsigrok.git] / m4 / sigrok.m4
1 ## Copyright (c) 2009  Openismus GmbH
2 ## Copyright (c) 2015  Daniel Elstner <daniel.kitta@gmail.com>
3 ##
4 ## This file is part of the sigrok project.
5 ##
6 ## sigrok is free software: you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation, either version 3 of the License, or
9 ## (at your option) any later version.
10 ##
11 ## sigrok is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with sigrok. If not, see <http://www.gnu.org/licenses/>.
18
19 #serial 20150824
20
21 ## SR_APPEND(var-name, [list-sep,] element)
22 ##
23 ## Append the shell word <element> to the shell variable named <var-name>,
24 ## prefixed by <list-sep> unless the list was empty before appending. If
25 ## only two arguments are supplied, <list-sep> defaults to a single space
26 ## character.
27 ##
28 AC_DEFUN([SR_APPEND],
29 [dnl
30 m4_assert([$# >= 2])[]dnl
31 $1=[$]{$1[}]m4_if([$#], [2], [[$]{$1:+' '}$2], [[$]{$1:+$2}$3])[]dnl
32 ])
33
34 ## SR_PREPEND(var-name, [list-sep,] element)
35 ##
36 ## Prepend the shell word <element> to the shell variable named <var-name>,
37 ## suffixed by <list-sep> unless the list was empty before prepending. If
38 ## only two arguments are supplied, <list-sep> defaults to a single space
39 ## character.
40 ##
41 AC_DEFUN([SR_PREPEND],
42 [dnl
43 m4_assert([$# >= 2])[]dnl
44 $1=m4_if([$#], [2], [$2[$]{$1:+' '}], [$3[$]{$1:+$2}])[$]$1[]dnl
45 ])
46
47 ## _SR_PKG_VERSION_SET(var-prefix, pkg-name, tag-prefix, base-version, major, minor, [micro])
48 ##
49 m4_define([_SR_PKG_VERSION_SET],
50 [dnl
51 m4_assert([$# >= 6])[]dnl
52 $1=$4
53 sr_git_deps=
54 # Check if we can get revision information from git.
55 sr_head=`git -C "$srcdir" rev-parse --verify --short HEAD 2>&AS_MESSAGE_LOG_FD`
56
57 AS_IF([test "$?" = 0 && test "x$sr_head" != x], [dnl
58         test ! -f "$srcdir/.git/HEAD" \
59                 || sr_git_deps="$sr_git_deps \$(top_srcdir)/.git/HEAD"
60
61         sr_head_name=`git -C "$srcdir" rev-parse --symbolic-full-name HEAD 2>&AS_MESSAGE_LOG_FD`
62         AS_IF([test "$?" = 0 && test -f "$srcdir/.git/$sr_head_name"],
63                 [sr_git_deps="$sr_git_deps \$(top_srcdir)/.git/$sr_head_name"])
64
65         # Append the revision hash unless we are exactly on a tagged release.
66         git -C "$srcdir" describe --match "$3$4" \
67                 --exact-match >&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD \
68                 || $1="[$]$1-git-$sr_head"
69 ])
70 # Use $(wildcard) so that things do not break if for whatever
71 # reason these files do not exist anymore at make time.
72 AS_IF([test -n "$sr_git_deps"],
73         [SR_APPEND([CONFIG_STATUS_DEPENDENCIES], ["\$(wildcard$sr_git_deps)"])])
74 AC_SUBST([CONFIG_STATUS_DEPENDENCIES])[]dnl
75 AC_SUBST([$1])[]dnl
76 dnl
77 AC_DEFINE([$1_MAJOR], [$5], [Major version number of $2.])[]dnl
78 AC_DEFINE([$1_MINOR], [$6], [Minor version number of $2.])[]dnl
79 m4_ifval([$7], [AC_DEFINE([$1_MICRO], [$7], [Micro version number of $2.])])[]dnl
80 AC_DEFINE_UNQUOTED([$1_STRING], ["[$]$1"], [Version of $2.])[]dnl
81 ])
82
83 ## SR_PKG_VERSION_SET(var-prefix, version-triple)
84 ##
85 ## Set up substitution variables and macro definitions for the package
86 ## version components. Derive the version suffix from the repository
87 ## revision if possible.
88 ##
89 ## Substitutions: <var-prefix>
90 ## Macro defines: <var-prefix>_{MAJOR,MINOR,MICRO,STRING}
91 ##
92 AC_DEFUN([SR_PKG_VERSION_SET],
93 [dnl
94 m4_assert([$# >= 2])[]dnl
95 _SR_PKG_VERSION_SET([$1],
96         m4_defn([AC_PACKAGE_NAME]),
97         m4_defn([AC_PACKAGE_TARNAME])[-],
98         m4_expand([$2]),
99         m4_unquote(m4_split(m4_expand([$2]), [\.])))
100 ])
101
102 ## _SR_LIB_VERSION_SET(var-prefix, pkg-name, abi-triple, current, revision, age)
103 ##
104 m4_define([_SR_LIB_VERSION_SET],
105 [dnl
106 m4_assert([$# >= 6])[]dnl
107 $1=$3
108 AC_SUBST([$1])[]dnl
109 AC_DEFINE([$1_CURRENT], [$4], [Binary version of $2.])[]dnl
110 AC_DEFINE([$1_REVISION], [$5], [Binary revision of $2.])[]dnl
111 AC_DEFINE([$1_AGE], [$6], [Binary age of $2.])[]dnl
112 AC_DEFINE([$1_STRING], ["$3"], [Binary version triple of $2.])[]dnl
113 ])
114
115 ## SR_LIB_VERSION_SET(var-prefix, abi-triple)
116 ##
117 ## Set up substitution variables and macro definitions for a library
118 ## binary version.
119 ##
120 ## Substitutions: <var-prefix>
121 ## Macro defines: <var-prefix>_{CURRENT,REVISION,AGE,STRING}
122 ##
123 AC_DEFUN([SR_LIB_VERSION_SET],
124 [dnl
125 m4_assert([$# >= 1])[]dnl
126 _SR_LIB_VERSION_SET([$1],
127         m4_defn([AC_PACKAGE_NAME]),
128         [$2], m4_unquote(m4_split([$2], [:])))
129 ])
130
131 ## SR_SEARCH_LIBS(libs-var, function, search-libs,
132 ##                [action-if-found], [action-if-not-found], [other-libs])
133 ##
134 ## Same as AC_SEARCH_LIBS, except that the result is prepended
135 ## to <libs-var> instead of LIBS. Calls AC_SUBST on <libs-var>.
136 ##
137 AC_DEFUN([SR_SEARCH_LIBS],
138 [dnl
139 m4_assert([$# >= 3])[]dnl
140 sr_sl_save_LIBS=$LIBS
141 AC_SEARCH_LIBS([$2], [$3],,, m4_join([$6], [[$]$1]))
142 LIBS=$sr_sl_save_LIBS
143 AS_CASE([$ac_cv_search_$2], [no*],,
144         [SR_PREPEND([$1], [$ac_cv_search_$2])])
145 m4_ifvaln([$4$5], [AS_IF([test "x$ac_cv_search_$2" = xno], [$5], [$4])])[]dnl
146 AC_SUBST([$1])[]dnl
147 ])
148
149 ## _SR_VAR_SUMMARY(tag, var-name, line-leader, align-columns, align-char)
150 ##
151 m4_define([_SR_VAR_SUMMARY], [dnl
152 $2=
153 $1_append() {
154         sr_aligned=`printf '%.$4s' "[$][1]m4_for([i], [1], [$4],, [$5])"`
155         $2="[$]{$2}$3$sr_aligned [$]2"'
156 '
157 }
158 ])
159
160 ## SR_VAR_SUMMARY(tag, [var-name = <tag>],
161 ##                [line-leader = [ - ]], [align-columns = 32], [align-char = .])
162 ##
163 ## Define a shell function <tag>_append() to be used for aggregating
164 ## a summary of test results in the shell variable <var-name>.
165 ##
166 AC_DEFUN([SR_VAR_SUMMARY],
167 [dnl
168 m4_assert([$# >= 1])[]dnl
169 _SR_VAR_SUMMARY([$1],
170         m4_default_quoted([$2], [$1]),
171         m4_default_quoted([$3], [ - ]),
172         m4_default_quoted([$4], [32]),
173         m4_default_quoted([$5], [.]))[]dnl
174 ])
175
176 ## SR_PKG_CHECK_SUMMARY([var-name = sr_pkg_check_summary],
177 ##                      [line-leader = [ - ]], [align-columns = 32], [align-char = .])
178 ##
179 ## Prepare for the aggregation of package check results
180 ## in the shell variable <var-name>.
181 ##
182 AC_DEFUN([SR_PKG_CHECK_SUMMARY],
183         [SR_VAR_SUMMARY([sr_pkg_check_summary], $@)])
184
185 ## SR_PKG_CHECK(tag, [collect-var], module...)
186 ##
187 ## Check for each pkg-config <module> in the argument list. <module> may
188 ## include a version constraint.
189 ##
190 ## Output variables: sr_have_<tag>, sr_<tag>_version
191 ##
192 AC_DEFUN([SR_PKG_CHECK],
193 [dnl
194 m4_assert([$# >= 3])[]dnl
195 AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
196 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
197 dnl
198 PKG_CHECK_EXISTS([$3], [dnl
199         sr_have_$1=yes
200         m4_ifval([$2], [SR_APPEND([$2], ["$3"])
201         ])sr_$1_version=`$PKG_CONFIG --modversion "$3" 2>&AS_MESSAGE_LOG_FD`
202         sr_pkg_check_summary_append "$3" "$sr_$1_version"[]dnl
203 ], [dnl
204         sr_pkg_check_summary_append "$3" no
205         m4_ifval([$4],
206                 [SR_PKG_CHECK([$1], [$2], m4_shift3($@))],
207                 [sr_have_$1=no sr_$1_version=])[]dnl
208 ])
209 ])
210
211 ## SR_VAR_OPT_PKG([modules-var], [features-var])
212 ##
213 ## Enable the collection of SR_ARG_OPT_PKG results into the shell variables
214 ## <modules-var> and <features-var>.
215 ##
216 AC_DEFUN([SR_VAR_OPT_PKG],
217 [dnl
218 m4_define([_SR_VAR_OPT_PKG_MODULES], [$1])[]dnl
219 m4_define([_SR_VAR_OPT_PKG_FEATURES], [$2])[]dnl
220 m4_ifvaln([$1], [$1=])[]dnl
221 m4_ifvaln([$2], [$2=])[]dnl
222 ])
223
224 ## _SR_ARG_OPT_PKG(sh-name, [modules-var], [features-var],
225 ##                 opt-name, [cpp-name], [cond-name], module...)
226 ##
227 m4_define([_SR_ARG_OPT_PKG],
228 [dnl
229 AC_ARG_WITH([$4], [AS_HELP_STRING([--without-$4],
230                         [disable $4 support [default=detect]])])
231 AS_IF([test "x$with_$1" = xno], [sr_have_$1=no],
232         [test "x$sr_have_$1" != xyes],
233                 [SR_PKG_CHECK([$1], [$2], m4_shiftn([6], $@))])
234 AS_IF([test "x$with_$1$sr_have_$1" = xyesno],
235         [AC_MSG_ERROR([$4 support requested, but it was not found.])])
236 AS_IF([test "x$sr_have_$1" = xyes], [m4_ifval([$3], [
237         SR_APPEND([$3], ["$4"])])[]m4_ifval([$5], [
238         AC_DEFINE([HAVE_$5], [1], [Whether $4 is available.])
239         AC_DEFINE_UNQUOTED([CONF_$5_VERSION], ["$sr_$1_version"],
240                 [Build-time version of $4.])])[]dnl
241 ])
242 m4_ifvaln([$6], [AM_CONDITIONAL([$6], [test "x$sr_have_$1" = xyes])])[]dnl
243 ])
244
245 ## SR_ARG_OPT_PKG(opt-name, [cpp-name], [cond-name], module...)
246 ##
247 ## Provide a --without-<opt-name> configure option for explicit disabling
248 ## of an optional dependency. If not disabled, the availability of the
249 ## optional dependency is auto-detected.
250 ##
251 ## Each pkg-config <module> argument is checked in turn, and the first one
252 ## found is selected. On success, the shell variable sr_have_<opt-name>
253 ## is set to "yes", otherwise to "no". Optionally, a preprocessor macro
254 ## HAVE_<cpp-name> and an Automake conditional <cond-name> are generated.
255 ##
256 ## Use SR_VAR_OPT_PKG to generate lists of available modules and features.
257 ##
258 AC_DEFUN([SR_ARG_OPT_PKG],
259 [dnl
260 m4_assert([$# >= 4])[]dnl
261 AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
262 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
263 AC_REQUIRE([SR_VAR_OPT_PKG])[]dnl
264 dnl
265 _SR_ARG_OPT_PKG(m4_expand([AS_TR_SH([$1])]),
266         m4_defn([_SR_VAR_OPT_PKG_MODULES]),
267         m4_defn([_SR_VAR_OPT_PKG_FEATURES]),
268         $@)[]dnl
269 ])
270
271 ## SR_CHECK_COMPILE_FLAGS(flags-var, description, flags)
272 ##
273 ## Find a compiler flag for <description>. For each flag in <flags>, check
274 ## if the compiler for the current language accepts it. On success, stop the
275 ## search and append the last tested flag to <flags-var>. Calls AC_SUBST
276 ## on <flags-var>.
277 ##
278 AC_DEFUN([SR_CHECK_COMPILE_FLAGS],
279 [dnl
280 m4_assert([$# >= 3])[]dnl
281 AC_MSG_CHECKING([compiler flag for $2])
282 sr_ccf_result=no
283 sr_ccf_save_CPPFLAGS=$CPPFLAGS
284 for sr_flag in $3
285 do
286         CPPFLAGS="$sr_ccf_save_CPPFLAGS $sr_flag"
287         AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])], [sr_ccf_result=$sr_flag])
288         test "x$sr_ccf_result" = xno || break
289 done
290 CPPFLAGS=$sr_ccf_save_CPPFLAGS
291 AS_IF([test "x$sr_ccf_result" != xno],
292         [SR_APPEND([$1], [$sr_ccf_result])])
293 AC_MSG_RESULT([$sr_ccf_result])
294 AC_SUBST([$1])
295 ])
296
297 ## _SR_ARG_ENABLE_WARNINGS_ONCE
298 ##
299 ## Implementation helper macro of SR_ARG_ENABLE_WARNINGS. Pulled in
300 ## through AC_REQUIRE so that it is only expanded once.
301 ##
302 m4_define([_SR_ARG_ENABLE_WARNINGS_ONCE],
303 [dnl
304 AC_PROVIDE([$0])[]dnl
305 AC_ARG_ENABLE([warnings],
306                 [AS_HELP_STRING([[--enable-warnings[=min|max|fatal|no]]],
307                                 [set compile pedantry level [default=max]])],
308                 [sr_enable_warnings=$enableval],
309                 [sr_enable_warnings=max])[]dnl
310 dnl
311 # Test whether the compiler accepts each flag.  Look at standard output,
312 # since GCC only shows a warning message if an option is not supported.
313 sr_check_compile_warning_flags() {
314         for sr_flag
315         do
316                 sr_cc_out=`$sr_cc $sr_warning_flags $sr_flag -c "$sr_conftest" 2>&1 || echo failed`
317                 AS_IF([test "$?$sr_cc_out" = 0],
318                         [SR_APPEND([sr_warning_flags], [$sr_flag])],
319                         [AS_ECHO(["$sr_cc: $sr_cc_out"]) >&AS_MESSAGE_LOG_FD])
320                 rm -f "conftest.[$]{OBJEXT:-o}"
321         done
322 }
323 ])
324
325 ## SR_ARG_ENABLE_WARNINGS(variable, min-flags, max-flags)
326 ##
327 ## Provide the --enable-warnings configure argument, set to "min" by default.
328 ## <min-flags> and <max-flags> should be space-separated lists of compiler
329 ## warning flags to use with --enable-warnings=min or --enable-warnings=max,
330 ## respectively. Warning level "fatal" is the same as "max" but in addition
331 ## enables -Werror mode.
332 ##
333 ## In order to determine the warning options to use with the C++ compiler,
334 ## call AC_LANG([C++]) first to change the current language. If different
335 ## output variables are used, it is also fine to call SR_ARG_ENABLE_WARNINGS
336 ## repeatedly, once for each language setting.
337 ##
338 AC_DEFUN([SR_ARG_ENABLE_WARNINGS],
339 [dnl
340 m4_assert([$# >= 3])[]dnl
341 AC_REQUIRE([_SR_ARG_ENABLE_WARNINGS_ONCE])[]dnl
342 dnl
343 AS_CASE([$ac_compile],
344         [[*'$CXXFLAGS '*]], [sr_lang='C++' sr_cc=$CXX sr_conftest="conftest.[$]{ac_ext:-cc}"],
345         [[*'$CFLAGS '*]],   [sr_lang=C sr_cc=$CC sr_conftest="conftest.[$]{ac_ext:-c}"],
346         [AC_MSG_ERROR([[current language is neither C nor C++]])])
347 dnl
348 AC_MSG_CHECKING([which $sr_lang compiler warning flags to use])
349 sr_warning_flags=
350 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
351 int main(int argc, char** argv) { return (argv != 0) ? argc : 0; }
352 ]])])
353 AS_CASE([$sr_enable_warnings],
354         [no], [],
355         [min], [sr_check_compile_warning_flags $2],
356         [fatal], [sr_check_compile_warning_flags $3 -Werror],
357         [sr_check_compile_warning_flags $3])
358 rm -f "$sr_conftest"
359 AC_SUBST([$1], [$sr_warning_flags])
360 AC_MSG_RESULT([[$]{sr_warning_flags:-none}])[]dnl
361 ])