summaryrefslogtreecommitdiff
path: root/m4/ax_am_override_var.m4
blob: 01d57697a0933a5c05c105b77492c50e6d02747e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# ===========================================================================
#    https://www.gnu.org/software/autoconf-archive/ax_am_override_var.html
# ===========================================================================
#
# SYNOPSIS
#
#   AX_AM_OVERRIDE_VAR([varname1 varname ... ])
#   AX_AM_OVERRIDE_FINALIZE
#
# DESCRIPTION
#
#   This autoconf macro generalizes the approach given in
#   <http://lists.gnu.org/archive/html/automake/2005-09/msg00108.html> which
#   moves user specified values for variable 'varname' given at configure
#   time into the corresponding AM_${varname} variable and clears out
#   'varname', allowing further manipulation by the configure script so that
#   target specific variables can be given specialized versions.  'varname
#   may still be specified on the make command line and will be appended as
#   usual.
#
#   As an example usage, consider a project which might benefit from
#   different compiler flags for different components. Typically this is
#   done via target specific flags, e.g.
#
#    libgtest_la_CXXFLAGS    =                        \
#                     -I $(top_srcdir)/tests          \
#                     -I $(top_builddir)/tests        \
#                     $(GTEST_CXXFLAGS)
#
#   automake will automatically append $(CXXFLAGS) -- provided by the user
#   -- to the build rule for libgtest_la.  That might be problematic, as
#   CXXFLAGS may contain compiler options which are inappropriate for
#   libgtest_la.
#
#   The approach laid out in the referenced mailing list message is to
#   supply a base value for a variable during _configure_ time, during which
#   it is possible to amend it for specific targets. The user may
#   subsequently specify a value for the variable during _build_ time, which
#   make will apply (via the standard automake rules) to all appropriate
#   targets.
#
#   For example,
#
#    AX_AM_OVERRIDE_VAR([CXXFLAGS])
#
#   will store the value of CXXFLAGS specified at configure time into the
#   AM_CXXFLAGS variable, AC_SUBST it, and clear CXXFLAGS. configure may
#   then create a target specific set of flags based upon AM_CXXFLAGS, e.g.
#
#    # googletest uses variadic macros, which g++ -pedantic-errors
#    # is very unhappy about
#    AC_SUBST([GTEST_CXXFLAGS],
#       [`AS_ECHO_N(["$AM_CXXFLAGS"]) \
#             | sed s/-pedantic-errors/-pedantic/`
#        ]
#     )
#
#   which would be used in a Makefile.am as above.  Since CXXFLAGS is
#   cleared, the configure time value will not affect the build for
#   libgtest_la.
#
#   Prior to _any other command_ which may set ${varname}, call
#
#    AX_AM_OVERRIDE_VAR([varname])
#
#   This will preserve the value (if any) passed to configure in
#   AM_${varname} and AC_SUBST([AM_${varname}).  You may pass a space
#   separated list of variable names, or may call AX_AM_OVERRIDE_VAR
#   multiple times for the same effect.
#
#   If any subsequent configure commands set ${varname} and you wish to
#   capture the resultant value into AM_${varname} in the case where
#   ${varname} was _not_ provided at configure time,  call
#
#    AX_AM_OVERRIDE_FINALIZE
#
#   after _all_ commands which might affect any of the variables specified
#   in calls to AX_AM_OVERRIDE_VAR.  This need be done only once, but
#   repeated calls will not cause harm.
#
#   There is a bit of trickery required to allow further manipulation of the
#   AM_${varname} in a Makefile.am file.  If AM_CFLAGS is used as is in a
#   Makefile.am, e.g.
#
#    libfoo_la_CFLAGS = $(AM_CFLAGS)
#
#   then automake will emit code in Makefile.in which sets AM_CFLAGS from
#   the configure'd value.
#
#   If however, AM_CFLAGS is manipulated (i.e. appended to), you will have
#   to explicitly arrange for the configure'd value to be substituted:
#
#    AM_CFLAGS = @AM_CFLAGS@
#    AM_CFLAGS += -lfoo
#
#   or else automake will complain about using += before =.
#
# LICENSE
#
#   Copyright (c) 2013 Smithsonian Astrophysical Observatory
#   Copyright (c) 2013 Diab Jerius <djerius@cfa.harvard.edu>
#
#   Copying and distribution of this file, with or without modification, are
#   permitted in any medium without royalty provided the copyright notice
#   and this notice are preserved. This file is offered as-is, without any
#   warranty.

#serial 2

AC_DEFUN([_AX_AM_OVERRIDE_INITIALIZE],
[
        m4_define([_mst_am_override_vars],[])
])


# _AX_AM_OVERRIDE_VAR(varname)
AC_DEFUN([_AX_AM_OVERRIDE_VAR],
[
  m4_define([_mst_am_override_vars], m4_defn([_mst_am_override_vars]) $1 )
  _mst_am_override_$1_set=false

  AS_IF( [test "${$1+set}" = set],
         [AC_SUBST([AM_$1],["$$1"])
          $1=
          _mst_am_override_$1_set=:
         ]
  )
]) # _AX_AM_OVERRIDE_VAR

# _AX_AM_OVERRIDE_FINALIZE(varname)
AC_DEFUN([_AX_AM_OVERRIDE_FINALIZE],
[
  AS_IF([$_mst_am_override_$1_set = :],
        [],
        [AC_SUBST([AM_$1],["$$1"])
         $1=
         _mst_am_override_$1_set=
        ]
  )
  AC_SUBST($1)
]) # _AX_AM_OVERRIDE_FINALIZE

AC_DEFUN([AX_AM_OVERRIDE_VAR],
[
  AC_REQUIRE([_AX_AM_OVERRIDE_INITIALIZE])
  m4_map_args_w([$1],[_AX_AM_OVERRIDE_VAR(],[)])
])# AX_OVERRIDE_VAR


# AX_AM_OVERRIDE_FINALIZE
AC_DEFUN([AX_AM_OVERRIDE_FINALIZE],
[
  AC_REQUIRE([_AX_AM_OVERRIDE_INITIALIZE])
  m4_map_args_w(_mst_am_override_vars,[_AX_AM_OVERRIDE_FINALIZE(],[)])
]) # AX_AM_OVERRIDE_FINALIZE