automake
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Relative path without realpath


From: Eric Blake
Subject: Re: Relative path without realpath
Date: Fri, 9 Oct 2020 16:03:19 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0

On 10/9/20 10:46 AM, Cristian Morales Vega wrote:
> I would like to send some patches making pkg-config files relocatable.
> pkg-config offers the "pcfiledir" variable to do this.
> The thing is that I need to be able to obtain prefix, libdir,
> includedir, etc. relative to the directory where the .pc file gets
> installed. I can easily do this with realpath, from coreutils, but
> it's not POSIX and I'm not sure people will accept such a dependency.
> The use of automake per se has no dependency to coreutils, does it?
> 
> There is any way to obtain the path of one directory relative to
> another in automake without adding a new dependency? "realpath"
> implemented as a m4 macro, maybe?

https://stackoverflow.com/questions/2564634/convert-absolute-path-into-relative-path-given-a-current-directory-using-bash
suggests that it can be scripted in portable shell (although I did not
test if it has odd corner cases when one or the other input contains
things like foo/../bar or symlinks):

#!/bin/sh
relpath () {
    [ $# -ge 1 ] && [ $# -le 2 ] || return 1
    current="${2:+"$1"}"
    target="${2:-"$1"}"
    [ "$target" != . ] || target=/
    target="/${target##/}"
    [ "$current" != . ] || current=/
    current="${current:="/"}"
    current="/${current##/}"
    appendix="${target##/}"
    relative=''
    while appendix="${target#"$current"/}"
        [ "$current" != '/' ] && [ "$appendix" = "$target" ]; do
        if [ "$current" = "$appendix" ]; then
            relative="${relative:-.}"
            echo "${relative#/}"
            return 0
        fi
        current="${current%/*}"
        relative="$relative${relative:+/}.."
    done
    relative="$relative${relative:+${appendix:+/}}${appendix#/}"
    echo "$relative"
}
relpath "$@"


-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]