]> git.draconx.ca Git - dxcommon.git/blob - m4/base.m4
DX_C_ALIGNAS: Work around bash-5 parsing bug.
[dxcommon.git] / m4 / base.m4
1 # Copyright © 2012, 2014, 2021, 2023-2024 Nick Bowler
2 #
3 # Basic macros for dxcommon.
4 #
5 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
6 # This is free software: you are free to do what the fuck you want to.
7 # There is NO WARRANTY, to the extent permitted by law.
8
9 AC_PREREQ([2.64])
10
11 m4_pattern_forbid([^_?DX_])
12
13 dnl _DX_BASE_FILE
14 dnl
15 dnl This macro identifies the filename of this file, used to automatically
16 dnl determine the dxcommon base directory.  It is a literal string, thus it
17 dnl should not be expanded directly; use m4_defn([_DX_BASE_FILE]) instead.
18 m4_define([_DX_BASE_FILE], __file__)
19
20 dnl _DX_SET_BASEDIR([directory])
21 dnl
22 dnl Defines the macro _DX_BASEDIR to the dxcommon base directory.  If the
23 dnl argument is empty, the base directory will be determined automatically.
24 dnl Otherwise, the base directory is set to the argument (which is not
25 dnl subject to macro expansion).
26 dnl
27 dnl Quoting here is tricky as we must avoid macro expansion of _DX_BASE_FILE,
28 dnl but m4_bpatsubst does not quote its result.  The resulting _DX_BASEDIR
29 dnl macro is a literal string, thus it should not be expanded directly; use
30 dnl m4_defn([_DX_BASEDIR]) instead.
31 m4_define([_DX_SET_BASEDIR], [m4_do(
32   [m4_define([_DX_BASEDIR], m4_ifval([$1], [[$1]],
33     [m4_bpatsubst(m4_dquote(m4_defn([_DX_BASE_FILE])),
34       [/m4/[^/]*\.m4\(.\)$], [\1])]))],
35   [_DX_STAMP])])
36
37 dnl Include the stamp file to force failure at autoconf time if it does
38 dnl not exist in the checkout.  We circumvent m4_include to avoid warnings
39 dnl about multiple inclusions.
40 m4_define([_DX_STAMP], [m4_do(
41   [m4_pushdef([m4_include], [m4_builtin([include], $][@)])],
42   [m4_include(m4_defn([_DX_BASEDIR])[/m4/dx-stamp.m4])],
43   [m4_popdef([m4_include])])])
44
45 dnl DX_INIT(directory)
46 dnl
47 dnl Sets the dxcommon base directory, which is used by other macros to
48 dnl find any source files that they may need.  It is not normally necessary
49 dnl to use this macro.  If it used, it must be expanded prior to any other
50 dnl dxcommon macro.  Otherwise, the base directory will be determined
51 dnl automatically.
52 dnl
53 dnl The directory argument is a literal string, not subject to macro expansion.
54 dnl
55 dnl Autoconf versions <= 2.69 have a bug which causes the first argument of
56 dnl AC_SUBST to be expanded as a macro when using the two-argument form.  This
57 dnl bug has been fixed upstream but as of September, 2014 there is no release
58 dnl which includes the fix.
59 AC_DEFUN_ONCE([DX_INIT], [m4_do(
60   [_DX_SET_BASEDIR([$1])],
61   [AC_SUBST([DX_BASEDIR])],
62   [AC_CONFIG_COMMANDS_PRE(
63     [[DX_BASEDIR]="\${top_srcdir}/AS_ESCAPE(
64       m4_dquote(m4_defn([_DX_BASEDIR])))"])])])
65
66 dnl DX_BASEDIR
67 dnl
68 dnl This macro expands to the dxcommon base directory, as a quoted string.
69 AC_DEFUN([DX_BASEDIR], [m4_ignore(DX_INIT())m4_defn([_DX_BASEDIR])])
70
71 # DX_RUN_LOG(command)
72 #
73 # Run a command, logging all of the command, its output, and overall
74 # exit status to config.log.  The expansion of this macro is a single
75 # complex shell command suitable for use in shell conditionals.
76 AC_DEFUN([DX_RUN_LOG],
77 [{ _dx_shopt=$-
78 ( set -x
79 $1
80 ) >&AS_MESSAGE_LOG_FD 2>&1
81 _dx_status=$?
82 test x"$-" = x"$_dx_shopt" || set +x # work around DJGPP subshell bug
83 AS_ECHO(["\$? = $_dx_status"]) >&AS_MESSAGE_LOG_FD
84 test $_dx_status = 0; } 2>/dev/null])
85
86 # DX_PATCH_MACRO([macro-name], [regexp], [replacement])
87 #
88 # Patches the definition of macro-name by replacing substrings that match
89 # the given regexp (a la m4_bpatsubst).
90 AC_DEFUN([DX_PATCH_MACRO], [m4_ifdef([$1],
91   [m4_define([$1], m4_bpatsubst(m4_dquote(m4_defn([$1])), [$2], [$3]))])])