1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
2
"http://www.w3.org/TR/html4/strict.dtd">
6
<title>Autoconf Macro: relpaths</title>
7
<link rel="stylesheet" type="text/css" href="ac-archive.css">
11
<table summary="web navigation" style="width:100%;">
14
<td style="width:50%;" align="center">[<a href="index.html">Macro Index
17
<td style="width:50%;" align="center">[<a href=
18
"../m4source/relpaths.m4">Download M4 Source</a>]</td>
29
<p style="text-align:left; white-space:nowrap;">
30
<code>adl_COMPUTE_RELATIVE_PATHS(PATH_LIST)</code></p>
36
<p>PATH_LIST is a space-separated list of colon-separated triplets of the
37
form 'FROM:TO:RESULT'. This function iterates over these triplets and set
38
$RESULT to the relative path from $FROM to $TO. Note that $FROM and $TO
39
needs to be absolute filenames for this macro to success.</p>
44
second=/usr/local/share
45
adl_COMPUTE_RELATIVE_PATHS([first:second:fs second:first:sf])
46
# $fs is set to ../share
47
# $sf is set to ../bin
50
<p>$FROM and $TO are both eval'ed recursively and normalized, this means
51
that you can call this macro with autoconf's dirnames like `prefix' or
52
`datadir'. For example:</p>
54
adl_COMPUTE_RELATIVE_PATHS([bindir:datadir:bin_to_data])
57
<p>adl_COMPUTE_RELATIVE_PATHS should also works with DOS filenames.</p>
59
<p>You may want to use this macro in order to make your package
60
relocatable. Instead of hardcoding $datadir into your programs just encode
61
$bin_to_data and try to determine $bindir at run-time.</p>
63
<p>This macro requires adl_NORMALIZE_PATH.</p>
69
<p>1.1 (last modified: 2001-05-25)</p>
75
<p>Alexandre Duret-Lutz <duret_g@epita.fr></p>
78
<h2>M4 Source Code</h2>
81
<pre class="m4source">
82
AC_DEFUN([adl_COMPUTE_RELATIVE_PATHS],
84
_lcl_from=\[$]`echo "[$]_lcl_i" | sed 's,:.*$,,'`
85
_lcl_to=\[$]`echo "[$]_lcl_i" | sed 's,^[[^:]]*:,,' | sed 's,:[[^:]]*$,,'`
86
_lcl_result_var=`echo "[$]_lcl_i" | sed 's,^.*:,,'`
87
adl_RECURSIVE_EVAL([[$]_lcl_from], [_lcl_from])
88
adl_RECURSIVE_EVAL([[$]_lcl_to], [_lcl_to])
89
_lcl_notation="$_lcl_from$_lcl_to"
90
adl_NORMALIZE_PATH([_lcl_from],['/'])
91
adl_NORMALIZE_PATH([_lcl_to],['/'])
92
adl_COMPUTE_RELATIVE_PATH([_lcl_from], [_lcl_to], [_lcl_result_tmp])
93
adl_NORMALIZE_PATH([_lcl_result_tmp],["[$]_lcl_notation"])
94
eval $_lcl_result_var='[$]_lcl_result_tmp'
99
## The following helper macros are too fragile to be used out
100
## of adl_COMPUTE_RELATIVE_PATHS (mainly because they assume that
101
## paths are normalized), that's why I'm keeping them in the same file.
102
## Still, some of them maybe worth to reuse.
104
dnl adl_COMPUTE_RELATIVE_PATH(FROM, TO, RESULT)
105
dnl ===========================================
106
dnl Compute the relative path to go from $FROM to $TO and set the value
107
dnl of $RESULT to that value. This function work on raw filenames
108
dnl (for instead it will considerate /usr//local and /usr/local as
109
dnl two distinct paths), you should really use adl_COMPUTE_REALTIVE_PATHS
110
dnl instead to have the paths sanitized automatically.
113
dnl first_dir=/somewhere/on/my/disk/bin
114
dnl second_dir=/somewhere/on/another/disk/share
115
dnl adl_COMPUTE_RELATIVE_PATH(first_dir, second_dir, first_to_second)
116
dnl will set $first_to_second to '../../../another/disk/share'.
117
AC_DEFUN([adl_COMPUTE_RELATIVE_PATH],
118
[adl_COMPUTE_COMMON_PATH([$1], [$2], [_lcl_common_prefix])
119
adl_COMPUTE_BACK_PATH([$1], [_lcl_common_prefix], [_lcl_first_rel])
120
adl_COMPUTE_SUFFIX_PATH([$2], [_lcl_common_prefix], [_lcl_second_suffix])
121
$3="[$]_lcl_first_rel[$]_lcl_second_suffix"])
123
dnl adl_COMPUTE_COMMON_PATH(LEFT, RIGHT, RESULT)
124
dnl ============================================
125
dnl Compute the common path to $LEFT and $RIGHT and set the result to $RESULT.
128
dnl first_path=/somewhere/on/my/disk/bin
129
dnl second_path=/somewhere/on/another/disk/share
130
dnl adl_COMPUTE_COMMON_PATH(first_path, second_path, common_path)
131
dnl will set $common_path to '/somewhere/on'.
132
AC_DEFUN([adl_COMPUTE_COMMON_PATH],
134
_lcl_second_prefix_match=''
135
while test "[$]_lcl_second_prefix_match" != 0; do
136
_lcl_first_prefix=`expr "x[$]$1" : "x\([$]$3/*[[^/]]*\)"`
137
_lcl_second_prefix_match=`expr "x[$]$2" : "x[$]_lcl_first_prefix"`
138
if test "[$]_lcl_second_prefix_match" != 0; then
139
if test "[$]_lcl_first_prefix" != "[$]$3"; then
140
$3="[$]_lcl_first_prefix"
142
_lcl_second_prefix_match=0
147
dnl adl_COMPUTE_SUFFIX_PATH(PATH, SUBPATH, RESULT)
148
dnl ==============================================
149
dnl Substrack $SUBPATH from $PATH, and set the resulting suffix
150
dnl (or the empty string if $SUBPATH is not a subpath of $PATH)
154
dnl first_path=/somewhere/on/my/disk/bin
155
dnl second_path=/somewhere/on
156
dnl adl_COMPUTE_SUFFIX_PATH(first_path, second_path, common_path)
157
dnl will set $common_path to '/my/disk/bin'.
158
AC_DEFUN([adl_COMPUTE_SUFFIX_PATH],
159
[$3=`expr "x[$]$1" : "x[$]$2/*\(.*\)"`])
161
dnl adl_COMPUTE_BACK_PATH(PATH, SUBPATH, RESULT)
162
dnl ============================================
163
dnl Compute the relative path to go from $PATH to $SUBPATH, knowing that
164
dnl $SUBPATH is a subpath of $PATH (any other words, only repeated '../'
165
dnl should be needed to move from $PATH to $SUBPATH) and set the value
166
dnl of $RESULT to that value. If $SUBPATH is not a subpath of PATH,
167
dnl set $RESULT to the empty string.
170
dnl first_path=/somewhere/on/my/disk/bin
171
dnl second_path=/somewhere/on
172
dnl adl_COMPUTE_BACK_PATH(first_path, second_path, back_path)
173
dnl will set $back_path to '../../../'.
174
AC_DEFUN([adl_COMPUTE_BACK_PATH],
175
[adl_COMPUTE_SUFFIX_PATH([$1], [$2], [_lcl_first_suffix])
178
while test "[$]_lcl_tmp" != ''; do
179
_lcl_tmp=`expr "x[$]_lcl_first_suffix" : "x[[^/]]*/*\(.*\)"`
180
if test "[$]_lcl_first_suffix" != ''; then
181
_lcl_first_suffix="[$]_lcl_tmp"
187
dnl adl_RECURSIVE_EVAL(VALUE, RESULT)
188
dnl =================================
189
dnl Interpolate the VALUE in loop until it doesn't change,
190
dnl and set the result to $RESULT.
191
dnl WARNING: It's easy to get an infinite loop with some unsane input.
192
AC_DEFUN([adl_RECURSIVE_EVAL],
194
$2=`(test "x$prefix" = xNONE && prefix="$ac_default_prefix"
195
test "x$exec_prefix" = xNONE && exec_prefix="${prefix}"
197
while test "[$]_lcl_receval_old" != "[$]_lcl_receval"; do
198
_lcl_receval_old="[$]_lcl_receval"
199
eval _lcl_receval="\"[$]_lcl_receval\""
201
echo "[$]_lcl_receval")`])
208
<a href="COPYING.html">GNU General Public License</a> with this special
209
<a href="COPYING-Exception.html">exception</a>.