source: trunk/m4/yat_common.m4 @ 4326

Last change on this file since 4326 was 4326, checked in by Peter, 7 months ago

We had two macros with very similar behaviour: YAT_AC_APPEND in
'yat_svn_release' and private macro _YAT_AC_APPEND_TO_FILE. Replace
with new macro YAT_AC_APPEND_TO_FILE with tested and documented
behaviour. Similarly a macro YAT_AC_WRITE_TO_FILE.

Introduce a new macro YAT_AM_LOCAL, which is recommended to use when
using any of the macros generating automake input.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Date
File size: 5.0 KB
Line 
1## $Id: yat_common.m4 4326 2023-03-12 00:37:24Z peter $
2#
3# Copyright (C) 2009, 2010, 2012, 2013, 2014, 2018, 2020 Peter Johansson
4#
5# This file is part of the yat library, http://dev.thep.lu.se/yat
6#
7# The yat library is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License as
9# published by the Free Software Foundation; either version 3 of the
10# License, or (at your option) any later version.
11#
12# The yat library 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 GNU
15# General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with yat. If not, see <http://www.gnu.org/licenses/>.
19#
20
21#
22# This file contains some small useful macros.
23#
24# serial 4 (yat 0.16)
25#
26
27
28# YAT_ECHO_LOG(STRING)
29# ====================
30# print STRING to config.log
31AC_DEFUN([YAT_ECHO_LOG],
32[
33  AC_PREREQ([2.58])
34  AS_ECHO(["$as_me:${as_lineno-$LINENO}: $1"]) >&AS_MESSAGE_LOG_FD
35]) #YAT_ECHO_LOG
36
37
38# YAT_RUN_LOG(COMMAND, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
39# =========================================================
40# run COMMAND and log the output, set status in $yat_status and log
41# it. If status is zero execute ACTION-IF-TRUE, else execute
42# ACTION-IF-FALSE and log stderror from COMMAND
43AC_DEFUN([YAT_RUN_LOG],
44[
45  AC_PREREQ([2.58])
46  m4_pushdef([COMMAND], [$1])
47  YAT_ECHO_LOG([COMMAND])
48  COMMAND >&AS_MESSAGE_LOG_FD 2>conftest.err
49  yat_status=$?
50  YAT_ECHO_LOG([status: $yat_status])
51  AS_IF([test $yat_status = 0], [$2],
52        [test -f conftest.err && cat conftest.err >&AS_MESSAGE_LOG_FD; $3])
53  m4_popdef([COMMAND])
54]) #YAT_RUN_LOG
55
56
57# YAT_SET_CONTAINS(set, value, [if-present], [if-not-present])
58# ============================================================
59# Loop over set (space-separated) and if value exists, execute
60# if-present. otherwise execute if-not-present
61AC_DEFUN([YAT_SET_CONTAINS],
62[
63AS_IF([AS_ECHO([$1]) | tr ' ' '\n' | grep "^$2$" > /dev/null], [$3], [$4])
64]) #YAT_SET_CONTAINS
65
66
67# YAT_VAR_REMOVE(VAR, LIST)
68# ==========================
69# Loop over LIST (space-separated) and for each value, remove value from VAR.
70AC_DEFUN([YAT_VAR_REMOVE],
71[
72AS_IF([test x"$2" != x], [
73  for value in $2; do
74    APR_REMOVEFROM([$1], [$value])
75  done
76])
77])
78
79# YAT_CHECK_LA_LIBS(CODE, LIBS, VALUE, VAR)
80# ============================================
81# Check if we can create a libtool archive based on CODE linking with
82# LIBS. If not, add VALUE to VAR.
83AC_DEFUN([YAT_CHECK_LA_LIBS],
84[
85save_LIBS=$LIBS
86LIBS="$2"
87AC_MSG_CHECKING([if libtool archive can be created with $LIBS])
88YAT_LT_LINK_LA_IFELSE([$1],
89  [AC_MSG_RESULT([yes])],
90  [AC_MSG_RESULT([no])
91   $4="$$4 $3"])
92LIBS=$save_LIBS
93])
94
95# YAT_SEARCH_LIBS(function, libraries, [action-if-found],
96#                 [action-if-not-found], [other-libraries])
97# =================================================================
98# Same as AC_SEARCH_LIBS, but do not append LIBS with found library
99# (if-found). Found library is stored in $yat_cv_search_<function>, set
100# to empty string if no library required, and set to no if no library
101# found.
102AC_DEFUN([YAT_SEARCH_LIBS],
103[
104AS_VAR_PUSHDEF([yat_Search], [yat_cv_search_$1])
105dnl cache variable used in autoconf code (AC_SEARCH_LIBS)
106AS_VAR_PUSHDEF([yat_ac_Search], [ac_cv_search_$1])
107yat_func_search_save_LIBS=$LIBS
108
109AC_SEARCH_LIBS([$1], [$2], [
110    LIBS=$yat_func_search_save_LIBS
111    AS_VAR_IF([yat_ac_Search], ["none required"], [], [
112      AS_VAR_COPY([yat_Search], [yat_ac_Search])
113    ])
114    $3
115  ], [
116    AS_VAR_COPY([yat_ac_Search], [yat_Search])
117    $4
118  ], [$5])
119
120AS_VAR_POPDEF([yat_ac_Search])
121AS_VAR_POPDEF([yat_Search])
122]) # YAT_SEARCH_LIBS
123
124
125# YAT_AC_WRITE_TO_FILE([FILE-NAME], [CONTENT])
126# ============================================
127#
128# When autoconf is run, create a file 'FILE-NAME' with CONTENT.
129# Variables that need to be expanded by M4 cannot be quoted e.g.
130# m4_define([my_text], [yada yada])
131# YAT_AC_WRITE_TO_FILE([foo.txt], [This is my example ]my_text[ and no less])
132#
133# Within a macro definition, a dollar ($) followed by 0-9, #, @, or *
134# is expanded by M4 and to avoid that one can use the pattern
135# YAT_AC_WRITE_TO_FILE([foo.txt], [some prefix]$[1 and suffix])
136# which outputs: some prefix $1 and suffix
137AC_DEFUN([YAT_AC_WRITE_TO_FILE],
138[
139_YAT_AC_WRITE_TO_FILE([$1], [$2], [write])
140])
141
142
143# YAT_AC_APPEND_TO_FILE([FILE-NAME], [CONTENT])
144# =============================================
145#
146# Same as YAT_AC_WRITE_TO_FILE but append to file instead
147AC_DEFUN([YAT_AC_APPEND_TO_FILE],
148[
149_YAT_AC_WRITE_TO_FILE([$1], [$2], [append])
150])
151
152
153# _YAT_AC_WRITE_TO_FILE([FILE-NAME], [CONTENT], [MODE])
154# =====================================================
155AC_DEFUN([_YAT_AC_WRITE_TO_FILE],
156[m4_define([yat_DIRECTION_OP], [m4_case([$3], [write], [>],
157                                    [append], [>>],
158            [m4_fatal([invalid mode $3])])])
159m4_syscmd([cat ]yat_DIRECTION_OP[ $1 << '_yat_EOF'
160$2
161_yat_EOF
162])
163dnl abort if syscmd failed
164m4_if(m4_sysval, [0], [], [m4_fatal([$0: cannot write: $1])])
165])
Note: See TracBrowser for help on using the repository browser.