source: trunk/m4/yat_am_macros.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: 12.2 KB
Line 
1## $Id: yat_am_macros.m4 4326 2023-03-12 00:37:24Z peter $
2
3# serial 16 (yat 0.21)
4
5m4_define([yat_am_macros_copyright], [
6#
7#   Copyright (C) 2011, 2012, 2019 Peter Johansson
8#
9#   This file is part of the yat library, http://dev.thep.lu.se/yat
10#
11#   The yat library is free software; you can redistribute it and/or
12#   modify it under the terms of the GNU General Public License as
13#   published by the Free Software Foundation; either version 3 of the
14#   License, or (at your option) any later version.
15#
16#   The yat library is distributed in the hope that it will be useful,
17#   but WITHOUT ANY WARRANTY; without even the implied warranty of
18#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19#   General Public License for more details.
20#
21#   You should have received a copy of the GNU General Public License
22#   along with yat. If not, see <http://www.gnu.org/licenses/>.
23#
24])
25
26# YAT_HAVE_SVN_WC
27#
28# Test if we are building from a subversion repository and sets shell
29# variable `have_svn_wc' to `yes' or `no'. Macro calls
30# AC_SUBST([have_svn_wc]) and defines an Automake Conditional
31# HAVE_SVN_WC.
32#
33AC_DEFUN([YAT_HAVE_SVN_WC],
34[
35  AC_MSG_CHECKING([if building from subversion wc])
36  AS_IF([test -d $srcdir/.svn], [have_svn_wc=yes], [have_svn_wc=no])
37  AM_CONDITIONAL([HAVE_SVN_WC], [test "x$have_svn_wc" = "xyes"])
38  AC_SUBST([have_svn_wc])
39  AC_MSG_RESULT([$have_svn_wc])
40]) # YAT_HAVE_SVN_WC
41
42
43# YAT_PROG_MOVE_IF_CHANGE
44#
45# Create shell script move-if-change at autoconf time and AC_SUBST
46# MOVE_IF_CHANGE
47AC_DEFUN([YAT_PROG_MOVE_IF_CHANGE],
48[
49m4_define([yat_MOVE_IF_CHANGE], m4_default([$1], [move-if-change]))
50AC_SUBST([MOVE_IF_CHANGE], ['${SHELL} $(top_srcdir)/yat_MOVE_IF_CHANGE'])
51
52YAT_AC_WRITE_TO_FILE(yat_MOVE_IF_CHANGE,dnl
53[#!/bin/sh
54# Like mv ]$[1 ]$[2, but if the files are the same, just delete ]$[1.
55# Status is zero if successful, nonzero otherwise.
56
57usage="]$[0: usage: ]$[0 SOURCE DEST"
58
59test ]$[# = 2 || { echo $usage >&2 && exit 1; }
60
61if test -r "]$[2" && cmp -s "]$[1" "]$[2"; then
62  rm -f "]$[1"
63else
64  mv -f "]$[1" "]$[2"
65fi
66]) # YAT_AC_WRITE_TO_FILE
67]) # YAT_PROG_MOVE_IF_CHANGE
68
69
70# YAT_AM_LOCAL([am-file = yat_am_local.am])
71# ========================================================
72#
73# This file and 'yat_svn_release.m4' contain several macros that
74# create Automake snippets that need to be included in your
75# Makefile.am. Some of macros AC_REQUIRE others, which makes it
76# tedious to keep track of which files are generated and need to be
77# included. If this macro has been called first, those macros append
78# the Automake snippets to am-file instead, which makes it easier to
79# include once.
80AC_DEFUN([YAT_AM_LOCAL],
81[
82  m4_define([YAT_AM_LOCAL_FILE], [m4_default([$1], [yat_am_local.am])])
83  _YAT_AM_TOP(YAT_AM_LOCAL_FILE)
84])
85
86
87# YAT_SVN_REVISION([am-file = svn_revison.am], [revision-file = .revision])
88#
89# Create a file, am-file, at Autoconf time that should be included in
90# your top Makefile.am. The file creates make rules for creation of a
91# file REVISION_FILE that will contain the current revision of the
92# subversion wc. We use `svnversion' to generate the revision number,
93# so `svnversion' (included in Apache Subversion) must be available
94# when building in a subversion wc. When building in the tarball there
95# is no need to re-generate `.revision' as the file is included in the
96# distribution and the revision number is hopefully constant in a
97# tarball release.
98#
99# You need to declare Automake variable in your Makefile.am
100#   'EXTRA_DIST = '
101#
102# You may want to use YAT_AM_LOCAL
103#
104AC_DEFUN([YAT_SVN_REVISION],
105[
106  AC_REQUIRE([YAT_HAVE_SVN_WC])
107  AC_REQUIRE([YAT_PROG_MOVE_IF_CHANGE])
108  AC_REQUIRE([_YAT_PROG_SVNVERSION])
109  m4_pushdef([YAT_AM_FILE], [m4_default([$1], [svn_revision.am])])
110  m4_pushdef([yat_svn_revision_FILE], m4_default([$2], [.revision]))
111  AS_VAR_IF([have_svn_wc], [yes], [
112             AS_VAR_IF([SVNVERSION], [false], [
113                        AC_MSG_ERROR([could not find svnversion, which is
114                         required when building from a subversion working copy])
115                       ])
116            ])
117dnl create dummy file in 'svn export'
118m4_syscmd([test -d .svn || test -r ]yat_svn_revision_FILE[ || \
119           echo "exported" > ]yat_svn_revision_FILE[])
120
121dnl write rules to create .revision
122_YAT_AM_LOCAL_WRITE(YAT_AM_FILE,[
123EXTRA_DIST += $(srcdir)/]yat_svn_revision_FILE[
124if HAVE_SVN_WC
125YAT_SVN_REVISION_FORCE:
126$(srcdir)/]yat_svn_revision_FILE[: YAT_SVN_REVISION_FORCE
127  $(AM_V_at)$(SVNVERSION) $(srcdir) > ]$[@-t \
128  && $(MOVE_IF_CHANGE) ]$[@-t ]$[@
129endif
130])
131dnl end YAT_AM_LOCAL_FILE
132m4_popdef([YAT_AM_FILE])
133]) # YAT_SVN_REVISION
134
135
136# YAT_REVISION_HEADER([AM_FILE = svn_revision_header.am],
137#                     [HEADER = svn_revision.h],
138#                     [DEFINE = SVN_REVISION],
139#                     [REVISION = .revision])
140#
141# Create make rules for creation of a C header file, which defines the
142# current revision as DEFINE. The make rule is generated in file,
143# AM_FILE, that should be included in your Makefile.am. The C header
144# file, HEADER, in two steps. First, at Autoconf time HEADER.in is
145# created. Then at Make time HEADER is created from files HEADER.in
146# and REVISION. The latter file is supposed to contain the current
147# revision and a convenient way to generate this file is to the use
148# macro YAT_SVN_REVISION.  You need to declare the following variables
149# in your Makefile.am:
150#   BUILT_SOURCES =
151#   DISTCLEANFILES =
152#   EXTRA_DIST =
153#
154# You may want to use YAT_AM_LOCAL
155#
156AC_DEFUN([YAT_REVISION_HEADER],
157[
158AC_REQUIRE([YAT_PROG_MOVE_IF_CHANGE])
159m4_pushdef([yat_AM_FILE], [m4_default([$1], [svn_revision_header.am])])
160m4_define([yat_HEADER_FILE], [m4_default([$2], [svn_revision.h])])
161m4_pushdef([yat_REVISION_DEFINE], [m4_default([$3], [SVN_REVISION])])
162m4_pushdef([yat_INPUT_FILE], [m4_default([$4], [.revision])])
163
164dnl create svn_revision.h.in
165YAT_AC_WRITE_TO_FILE([]yat_HEADER_FILE[.in-t],[
166/* created by $0 */
167#ifndef ]yat_REVISION_DEFINE[
168#define ]yat_REVISION_DEFINE[ "@]yat_REVISION_DEFINE[@"
169#endif
170])
171m4_syscmd([sh ]yat_MOVE_IF_CHANGE[ ]yat_HEADER_FILE[.in-t ]yat_HEADER_FILE[.in])
172dnl write make rule for svn_revision.h
173_YAT_AM_LOCAL_WRITE(yat_AM_FILE,[
174EXTRA_DIST += $(srcdir)/]yat_HEADER_FILE[.in
175DISTCLEANFILES += $(builddir)/]yat_HEADER_FILE[
176BUILT_SOURCES += $(builddir)/]yat_HEADER_FILE[
177$(builddir)/]yat_HEADER_FILE[: $(srcdir)/]yat_HEADER_FILE[.in $(srcdir)/]yat_INPUT_FILE[
178  $(AM_V_at)revision=$$(cat $(srcdir)/]yat_INPUT_FILE[) \
179  && sed "s|@]yat_REVISION_DEFINE[@|$$revision|g" < $(srcdir)/]yat_HEADER_FILE[.in \
180  > ]$[@-t && mv ]$[@-t ]$[@
181])
182m4_popdef([yat_AM_FILE])
183m4_popdef([yat_REVISION_DEFINE])
184m4_popdef([yat_INPUT_FILE])
185]) # YAT_REVISION_HEADER
186
187
188# YAT_SVN_RELEASE_YEAR([AM_FILE = svn_release_year.am],[OUTPUT = .release_year])
189#
190# Create file, AM_FILE, with make rules for creation of a file,
191# OUTPUT, that holds the year the package was last modified. The rule
192# is only active when building in subversion wc and the wc is modified
193# compared to the repository. The value of OUTPUT is AC_SUBSTed and
194# AC_DEFINEed and OUTPUT added to CONFIG_STATUS_DEPENDENCIES which
195# means ./configure will be re-run when OUTPUT is updated. Typically
196# it is a good idea to check in OUTPUT in repository because then
197# OUTPUT will get expected value also in 'svn export' case. You need
198# to declare the following variables in Makefile.am:
199#   CONFIG_STATUS_DEPENDENCIES =
200#   EXTRA_DIST =
201#
202AC_DEFUN([YAT_SVN_RELEASE_YEAR],
203[
204AC_REQUIRE([YAT_HAVE_SVN_WC])
205AC_REQUIRE([_YAT_PROG_SVNVERSION])
206AC_REQUIRE([YAT_PROG_MOVE_IF_CHANGE])
207m4_pushdef([YAT_am_file], [m4_default([$1], [svn_release_year.am])])
208m4_pushdef([YAT_release_year], [m4_default([$2], [.release_year])])
209# .release_year is generated during make so in order to avoid bootstrap problem
210test -r $srcdir/YAT_release_year || date -u "+%Y" > $srcdir/YAT_release_year
211# propagate RELEASE_YEAR from file .release_year
212RELEASE_YEAR=`cat "$srcdir/YAT_release_year"`
213AC_SUBST([RELEASE_YEAR])
214AC_DEFINE_UNQUOTED([RELEASE_YEAR], ["$RELEASE_YEAR"],
215                   [Define year package was last modified])
216dnl generate make rule for .release_year
217_YAT_AM_LOCAL_WRITE(YAT_am_file,[
218EXTRA_DIST += $(srcdir)/]YAT_release_year[
219CONFIG_STATUS_DEPENDENCIES += $(srcdir)/]YAT_release_year[
220RELEASE_YEAR_FORCE:
221if HAVE_SVN_WC
222$(srcdir)/YAT_release_year: RELEASE_YEAR_FORCE
223  $(AM_V_at)if $(SVNVERSION) $(srcdir) | grep 'M' > /dev/null; then \
224     date -u "+%Y" > ]$[@-t \
225     && $(MOVE_IF_CHANGE) ]$[@-t ]$[@; \
226  fi
227endif
228])
229m4_popdef([YAT_am_file])
230m4_popdef([YAT_release_year])
231]) # YAT_SVN_RELEASE_YEAR
232
233
234# YAT_SVN_TIMESTAMP([AM_FILE = svn_timestamp.am])
235#
236# Create a file, AM_FILE, at Autoconf time that should be included in
237# your top Makefile.am. The file contains rules for creation of a file
238# '.timestamp' and associated '.svn_timestamp',
239# '$(srcdir)/.svn_export_timestamp' and '$(srcdir)/.tarball_timestamp'
240# depending on whether building from subversion working-copy,
241# subversion export, or a tarball, respectively. The macro inherits
242# the same requirements from YAT_SVN_REVISION and in addition the
243# Makefile.am needs to define Automake variable
244#   yat_am_dist_hooks =
245# and let the dist hook depend on this variable
246#   dist-hook: $(yat_am_dist_hooks)
247AC_DEFUN([YAT_SVN_TIMESTAMP],
248[
249  AC_REQUIRE([YAT_HAVE_SVN_WC])
250  AC_REQUIRE([YAT_SVN_REVISION])
251  AC_REQUIRE([_YAT_PROG_SVN])
252  m4_pushdef([YAT_AM_FILE], [m4_default([$1], [svn_timestamp.am])])
253
254dnl create dummy file in 'svn export'
255m4_syscmd([test -d .svn || test -r .tarball_timestamp \
256    || test -e .svn_export_timestamp \
257    || date +%s > .svn_export_timestamp])
258
259AC_MSG_CHECKING([if .tarball_timestamp exists])
260have_tarball_timestamp=no
261test -e $srcdir/.tarball_timestamp && have_tarball_timestamp=yes
262AC_MSG_RESULT([$have_tarball_timestamp])
263
264AC_MSG_CHECKING([if .svn_export_timestamp exists])
265have_svn_export_timestamp=no
266test -e $srcdir/.svn_export_timestamp && have_svn_export_timestamp=yes
267AC_MSG_RESULT([$have_svn_export_timestamp])
268
269AS_IF([test x"$have_svn_wc" = x"yes"], [
270    yat_timestamp_source=.svn_timestamp
271  ], [test x"$have_svn_export_timestamp" = x"yes"], [
272    yat_timestamp_source='$(srcdir)/.svn_export_timestamp'
273  ], [test x"$have_tarball_timestamp" = x"yes"], [
274    yat_timestamp_source='$(srcdir)/.tarball_timestamp'
275  ], [
276    AC_MSG_ERROR([cannot find required file;
277                  both .tarball_timestamp and .svn_export_timestamp are missing])
278  ])
279AC_SUBST([yat_timestamp_source])
280
281dnl write rules to create .timestamp
282_YAT_AM_LOCAL_WRITE(YAT_AM_FILE,[
283## The .timestamp reflects the last change to the svn repo, i.e., it
284## changes via 'svn update' or 'svn commit', which is a subset of when
285## .revision changes and only need to update the .timestamp when
286## .revision has changed.
287if HAVE_SVN_WC
288.svn_timestamp: $(srcdir)/.revision
289  $(AM_V_at)date=$($(SVN) info $(scrdir) --show-item last-changed-date --no-newline); \
290  date +%s --date="$$date" > ]$[@
291DISTCLEANFILES += .svn_timestamp
292endif
293
294.timestamp: $(yat_timestamp_source)
295  $(AM_V_at)cp $(yat_timestamp_source) ]$[@
296
297CLEANFILES += .timestamp
298
299yat_am_dist_hooks += yat_dist_tarball_timestamp
300
301if HAVE_SVN_WC
302yat_dist_tarball_timestamp: .timestamp
303  $(AM_V_at)cp .timestamp $(distdir)/.tarball_timestamp
304else
305# in case some does a dist from a non-wc, use 'now' as timestamp
306# (rather than timestamp file in builddir, which reflects the date of
307# the previous release)
308yat_dist_tarball_timestamp:
309  date +%s > $(distdir)/.tarball_timestamp
310endif
311]) dnl end of writing automake segment
312]) # YAT_SVN_TIMESTAMP
313
314
315### Private Macros ###
316
317# _YAT_AM_TOP(FILE)
318#
319# Write the header of a Automake snippet with the copyright noticed
320# decalared in top of this file.
321AC_DEFUN([_YAT_AM_TOP],
322[
323AC_PREREQ([2.62])
324YAT_AC_WRITE_TO_FILE([$1],dnl
325[# $1 generated automatically by GNU Autoconf
326]yat_am_macros_copyright[
327# stub rule to work around bug in Automake 1.11
328$(top_srcdir)/$1:
329
330])
331]) # _YAT_AM_TOP
332
333
334# _YAT_AM_LOCAL_WRITE
335# ===================
336#
337#
338AC_DEFUN([_YAT_AM_LOCAL_WRITE],
339[  m4_ifndef([YAT_AM_LOCAL_FILE], [
340     _YAT_AM_TOP([$1])
341     YAT_AC_APPEND_TO_FILE([$1], [$2])
342   ], [
343     YAT_AC_APPEND_TO_FILE(YAT_AM_LOCAL_FILE, [$2])
344   ])
345])
346
347
348# _YAT_PROG_SVN
349#
350# Wrapper around AC_CHECK_PROG to allow using it as argument to
351# AC_REQUIRE
352AC_DEFUN([_YAT_PROG_SVN],
353[
354AC_CHECK_PROG([SVN], [svn], [svn], [false])
355])
356
357
358# _YAT_PROG_SVNVERSION
359#
360# Wrapper around AC_CHECK_PROG to allow using it as argument to
361# AC_REQUIRE
362AC_DEFUN([_YAT_PROG_SVNVERSION],
363[
364AC_CHECK_PROG([SVNVERSION], [svnversion], [svnversion], [false])
365])
Note: See TracBrowser for help on using the repository browser.