]> sigrok.org Git - libsigrok.git/blob - m4/sigrok.m4
6bf008401fb77ccb7279ee4b86e0dd849e862a2b
[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 20150823
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_PKG_VERSION_SET(var-prefix, pkg-name, tag-prefix, base-version, major, minor, [micro])
35 ##
36 m4_define([_SR_PKG_VERSION_SET],
37 [dnl
38 m4_assert([$# >= 6])[]dnl
39 $1=$4
40 sr_git_deps=
41 # Check if we can get revision information from git.
42 sr_head=`git -C "$srcdir" rev-parse --verify --short HEAD 2>&AS_MESSAGE_LOG_FD`
43
44 AS_IF([test "$?" = 0 && test "x$sr_head" != x], [dnl
45         test ! -f "$srcdir/.git/HEAD" \
46                 || sr_git_deps="$sr_git_deps \$(top_srcdir)/.git/HEAD"
47
48         sr_head_name=`git -C "$srcdir" rev-parse --symbolic-full-name HEAD 2>&AS_MESSAGE_LOG_FD`
49         AS_IF([test "$?" = 0 && test -f "$srcdir/.git/$sr_head_name"],
50                 [sr_git_deps="$sr_git_deps \$(top_srcdir)/.git/$sr_head_name"])
51
52         # Append the revision hash unless we are exactly on a tagged release.
53         git -C "$srcdir" describe --match "$3$4" \
54                 --exact-match >&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD \
55                 || $1="[$]$1-git-$sr_head"
56 ])
57 # Use $(wildcard) so that things do not break if for whatever
58 # reason these files do not exist anymore at make time.
59 AS_IF([test -n "$sr_git_deps"],
60         [SR_APPEND([CONFIG_STATUS_DEPENDENCIES], ["\$(wildcard$sr_git_deps)"])])
61 AC_SUBST([CONFIG_STATUS_DEPENDENCIES])[]dnl
62 AC_SUBST([$1])[]dnl
63 dnl
64 AC_DEFINE([$1_MAJOR], [$5], [Major version number of $2.])[]dnl
65 AC_DEFINE([$1_MINOR], [$6], [Minor version number of $2.])[]dnl
66 m4_ifval([$7], [AC_DEFINE([$1_MICRO], [$7], [Micro version number of $2.])])[]dnl
67 AC_DEFINE_UNQUOTED([$1_STRING], ["[$]$1"], [Version of $2.])[]dnl
68 ])
69
70 ## SR_PKG_VERSION_SET(var-prefix, version-triple)
71 ##
72 ## Set up substitution variables and macro definitions for the package
73 ## version components. Derive the version suffix from the repository
74 ## revision if possible.
75 ##
76 ## Substitutions: <var-prefix>
77 ## Macro defines: <var-prefix>_{MAJOR,MINOR,MICRO,STRING}
78 ##
79 AC_DEFUN([SR_PKG_VERSION_SET],
80 [dnl
81 m4_assert([$# >= 2])[]dnl
82 _SR_PKG_VERSION_SET([$1],
83         m4_defn([AC_PACKAGE_NAME]),
84         m4_defn([AC_PACKAGE_TARNAME])[-],
85         m4_expand([$2]),
86         m4_unquote(m4_split(m4_expand([$2]), [\.])))
87 ])
88
89 ## _SR_LIB_VERSION_SET(var-prefix, pkg-name, abi-triple, current, revision, age)
90 ##
91 m4_define([_SR_LIB_VERSION_SET],
92 [dnl
93 m4_assert([$# >= 6])[]dnl
94 $1=$3
95 AC_SUBST([$1])[]dnl
96 AC_DEFINE([$1_CURRENT], [$4], [Binary version of $2.])[]dnl
97 AC_DEFINE([$1_REVISION], [$5], [Binary revision of $2.])[]dnl
98 AC_DEFINE([$1_AGE], [$6], [Binary age of $2.])[]dnl
99 AC_DEFINE([$1_STRING], ["$3"], [Binary version triple of $2.])[]dnl
100 ])
101
102 ## SR_LIB_VERSION_SET(var-prefix, abi-triple)
103 ##
104 ## Set up substitution variables and macro definitions for a library
105 ## binary version.
106 ##
107 ## Substitutions: <var-prefix>
108 ## Macro defines: <var-prefix>_{CURRENT,REVISION,AGE,STRING}
109 ##
110 AC_DEFUN([SR_LIB_VERSION_SET],
111 [dnl
112 m4_assert([$# >= 1])[]dnl
113 _SR_LIB_VERSION_SET([$1],
114         m4_defn([AC_PACKAGE_NAME]),
115         [$2], m4_unquote(m4_split([$2], [:])))
116 ])
117
118 ## _SR_VAR_SUMMARY(tag, var-name, line-leader, align-columns, align-char)
119 ##
120 m4_define([_SR_VAR_SUMMARY], [dnl
121 $2=
122 $1_append() {
123         sr_aligned=`printf '%.$4s' "[$][1]m4_for([i], [1], [$4],, [$5])"`
124         $2="[$]{$2}$3$sr_aligned [$]2"'
125 '
126 }
127 ])
128
129 ## SR_VAR_SUMMARY(tag, [var-name = <tag>],
130 ##                [line-leader = [ - ]], [align-columns = 32], [align-char = .])
131 ##
132 ## Define a shell function <tag>_append() to be used for aggregating
133 ## a summary of test results in the shell variable <var-name>.
134 ##
135 AC_DEFUN([SR_VAR_SUMMARY],
136 [dnl
137 m4_assert([$# >= 1])[]dnl
138 _SR_VAR_SUMMARY([$1],
139         m4_default_quoted([$2], [$1]),
140         m4_default_quoted([$3], [ - ]),
141         m4_default_quoted([$4], [32]),
142         m4_default_quoted([$5], [.]))[]dnl
143 ])
144
145 ## SR_PKG_CHECK_SUMMARY([var-name = sr_pkg_check_summary],
146 ##                      [line-leader = [ - ]], [align-columns = 32], [align-char = .])
147 ##
148 ## Prepare for the aggregation of package check results
149 ## in the shell variable <var-name>.
150 ##
151 AC_DEFUN([SR_PKG_CHECK_SUMMARY],
152         [SR_VAR_SUMMARY([sr_pkg_check_summary], $@)])
153
154 ## SR_PKG_CHECK(tag, [collect-var], module...)
155 ##
156 ## Check for each pkg-config <module> in the argument list. <module> may
157 ## include a version constraint.
158 ##
159 ## Output variables: sr_have_<tag>, sr_<tag>_version
160 ##
161 AC_DEFUN([SR_PKG_CHECK],
162 [dnl
163 m4_assert([$# >= 3])[]dnl
164 AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
165 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
166 dnl
167 PKG_CHECK_EXISTS([$3], [dnl
168         sr_have_$1=yes
169         m4_ifval([$2], [SR_APPEND([$2], ["$3"])
170         ])sr_$1_version=`$PKG_CONFIG --modversion "$3" 2>&AS_MESSAGE_LOG_FD`
171         sr_pkg_check_summary_append "$3" "$sr_$1_version"[]dnl
172 ], [dnl
173         sr_pkg_check_summary_append "$3" no
174         m4_ifval([$4],
175                 [SR_PKG_CHECK([$1], [$2], m4_shift3($@))],
176                 [sr_have_$1=no sr_$1_version=])[]dnl
177 ])
178 ])
179
180 ## SR_VAR_OPT_PKG([modules-var], [features-var])
181 ##
182 ## Enable the collection of SR_ARG_OPT_PKG results into the shell variables
183 ## <modules-var> and <features-var>.
184 ##
185 AC_DEFUN([SR_VAR_OPT_PKG],
186 [dnl
187 m4_define([_SR_VAR_OPT_PKG_MODULES], [$1])[]dnl
188 m4_define([_SR_VAR_OPT_PKG_FEATURES], [$2])[]dnl
189 m4_ifvaln([$1], [$1=])[]dnl
190 m4_ifvaln([$2], [$2=])[]dnl
191 ])
192
193 ## _SR_ARG_OPT_PKG(sh-name, [modules-var], [features-var],
194 ##                 opt-name, [cpp-name], [cond-name], module...)
195 ##
196 m4_define([_SR_ARG_OPT_PKG],
197 [dnl
198 AC_ARG_WITH([$4], [AS_HELP_STRING([--without-$4],
199                         [disable $4 support [default=detect]])])
200 AS_IF([test "x$with_$1" = xno], [sr_have_$1=no],
201         [test "x$sr_have_$1" != xyes],
202                 [SR_PKG_CHECK([$1], [$2], m4_shiftn([6], $@))])
203 AS_IF([test "x$with_$1$sr_have_$1" = xyesno],
204         [AC_MSG_ERROR([$4 support requested, but it was not found.])])
205 AS_IF([test "x$sr_have_$1" = xyes], [m4_ifval([$3], [
206         SR_APPEND([$3], ["$4"])])[]m4_ifval([$5], [
207         AC_DEFINE([HAVE_$5], [1], [Whether $4 is available.])
208         AC_DEFINE_UNQUOTED([CONF_$5_VERSION], ["$sr_$1_version"],
209                 [Build-time version of $4.])])[]dnl
210 ])
211 m4_ifvaln([$6], [AM_CONDITIONAL([$6], [test "x$sr_have_$1" = xyes])])[]dnl
212 ])
213
214 ## SR_ARG_OPT_PKG(opt-name, [cpp-name], [cond-name], module...)
215 ##
216 ## Provide a --without-<opt-name> configure option for explicit disabling
217 ## of an optional dependency. If not disabled, the availability of the
218 ## optional dependency is auto-detected.
219 ##
220 ## Each pkg-config <module> argument is checked in turn, and the first one
221 ## found is selected. On success, the shell variable sr_have_<opt-name>
222 ## is set to "yes", otherwise to "no". Optionally, a preprocessor macro
223 ## HAVE_<cpp-name> and an Automake conditional <cond-name> are generated.
224 ##
225 ## Use SR_VAR_OPT_PKG to generate lists of available modules and features.
226 ##
227 AC_DEFUN([SR_ARG_OPT_PKG],
228 [dnl
229 m4_assert([$# >= 4])[]dnl
230 AC_REQUIRE([PKG_PROG_PKG_CONFIG])[]dnl
231 AC_REQUIRE([SR_PKG_CHECK_SUMMARY])[]dnl
232 AC_REQUIRE([SR_VAR_OPT_PKG])[]dnl
233 dnl
234 _SR_ARG_OPT_PKG(m4_expand([AS_TR_SH([$1])]),
235         m4_defn([_SR_VAR_OPT_PKG_MODULES]),
236         m4_defn([_SR_VAR_OPT_PKG_FEATURES]),
237         $@)[]dnl
238 ])
239
240 ## _SR_ARG_ENABLE_WARNINGS_ONCE
241 ##
242 ## Implementation helper macro of SR_ARG_ENABLE_WARNINGS. Pulled in
243 ## through AC_REQUIRE so that it is only expanded once.
244 ##
245 m4_define([_SR_ARG_ENABLE_WARNINGS_ONCE],
246 [dnl
247 AC_PROVIDE([$0])[]dnl
248 AC_ARG_ENABLE([warnings],
249                 [AS_HELP_STRING([[--enable-warnings[=min|max|fatal|no]]],
250                                 [set compile pedantry level [default=max]])],
251                 [sr_enable_warnings=$enableval],
252                 [sr_enable_warnings=max])[]dnl
253 dnl
254 # Test whether the compiler accepts each flag.  Look at standard output,
255 # since GCC only shows a warning message if an option is not supported.
256 sr_check_compile_warning_flags() {
257         for sr_flag
258         do
259                 sr_cc_out=`$sr_cc $sr_warning_flags $sr_flag -c "$sr_conftest" 2>&1 || echo failed`
260                 AS_IF([test "$?$sr_cc_out" = 0],
261                         [SR_APPEND([sr_warning_flags], [$sr_flag])],
262                         [AS_ECHO(["$sr_cc: $sr_cc_out"]) >&AS_MESSAGE_LOG_FD])
263                 rm -f "conftest.[$]{OBJEXT:-o}"
264         done
265 }
266 ])
267
268 ## SR_ARG_ENABLE_WARNINGS(variable, min-flags, max-flags)
269 ##
270 ## Provide the --enable-warnings configure argument, set to "min" by default.
271 ## <min-flags> and <max-flags> should be space-separated lists of compiler
272 ## warning flags to use with --enable-warnings=min or --enable-warnings=max,
273 ## respectively. Warning level "fatal" is the same as "max" but in addition
274 ## enables -Werror mode.
275 ##
276 ## In order to determine the warning options to use with the C++ compiler,
277 ## call AC_LANG([C++]) first to change the current language. If different
278 ## output variables are used, it is also fine to call SR_ARG_ENABLE_WARNINGS
279 ## repeatedly, once for each language setting.
280 ##
281 AC_DEFUN([SR_ARG_ENABLE_WARNINGS],
282 [dnl
283 m4_assert([$# >= 3])[]dnl
284 AC_REQUIRE([_SR_ARG_ENABLE_WARNINGS_ONCE])[]dnl
285 dnl
286 AS_CASE([$ac_compile],
287         [[*'$CXXFLAGS '*]], [sr_lang='C++' sr_cc=$CXX sr_conftest="conftest.[$]{ac_ext:-cc}"],
288         [[*'$CFLAGS '*]],   [sr_lang=C sr_cc=$CC sr_conftest="conftest.[$]{ac_ext:-c}"],
289         [AC_MSG_ERROR([[current language is neither C nor C++]])])
290 dnl
291 AC_MSG_CHECKING([which $sr_lang compiler warning flags to use])
292 sr_warning_flags=
293 AC_LANG_CONFTEST([AC_LANG_SOURCE([[
294 int main(int argc, char** argv) { return (argv != 0) ? argc : 0; }
295 ]])])
296 AS_CASE([$sr_enable_warnings],
297         [no], [],
298         [min], [sr_check_compile_warning_flags $2],
299         [fatal], [sr_check_compile_warning_flags $3 -Werror],
300         [sr_check_compile_warning_flags $3])
301 rm -f "$sr_conftest"
302 AC_SUBST([$1], [$sr_warning_flags])
303 AC_MSG_RESULT([[$]{sr_warning_flags:-none}])[]dnl
304 ])