msys path conversion (or cygpath for msys?)

前端 未结 7 2082
心在旅途
心在旅途 2020-12-02 17:15

I need to pass /DEF:c:\\filepath\\myLib.def\" command line option from a bash script to MS compiler/linker. The path is generated as part of build process by a bash script.

相关标签:
7条回答
  • 2020-12-02 17:35

    use pwd -W

    or download cygpath for msys from here http://mingw.5.n7.nabble.com/enhanced-version-of-cygpath-td28556.html

    and use cygpath -wa

    0 讨论(0)
  • 2020-12-02 17:35

    How about this one ? cmd //c echo <your path>

    It may not work always but it is the shortest I found

    0 讨论(0)
  • 2020-12-02 17:36

    Update (Aug-2016):

    This question is no longer relevant, as msys2 now comes with cygpath in its installation.

    ...

    I'll summarize my research here.

    The cygpath equivalent in MSYS is to use this command:

    { cd /c/some/path && pwd -W; } | sed 's|/|\\|g'
    

    The problem with this approach is that it requires existing path, e.g. the c:\some\path has to be an existing directory; however, real cygpath supports paths that do not exist.

    So, if you need to get path to a directory that doesn't exist, then you can fallback to sed conversion of the path:

    { cd 2>/dev/null /c/some/path && pwd -W ||
      echo /c/some/path | sed 's|^/\([a-z,A-Z]\)/|\1:/|'; } | sed 's|/|\\|g'
    

    The mouthful of slashes is there to satisfy quoting rules of sed. So, if c:\some\path doesn't exist on your PC, it will try to convert forward to back slashes and replace /c/ with c:\ (or any other drive letter). The only drawback for this is that it won't work correctly non-existing paths that contain a mounted component, such as /bin/does-not-exist or /usr/bin/does-not-exist.

    One more approach is to use cygpath from cygwin in MSYS. It seems that cygwin sets global environment variable CYGPATH, that is, you can use it from regular cmd.exe:

    %CYGPATH% -w /c/some/path
    C:\some\path
    

    or from MSYS:

    $CYGPATH -w /c/some/path
    C:\some\path
    

    as long as you set to point /c to /cygdrive/c in cygwin. But this approach will print you /usr located in cygwin installation, not in MSYS.

    In short, I think msys should really include real cygpath in the default set of tools just for some cases that aren't handled automatically by msys command line argument conversion logic

    0 讨论(0)
  • 2020-12-02 17:44

    I am using this with msysgit:

    winpath() {
        if [ -z "$1" ]; then
            echo "$@"
        else
            if [ -f "$1" ]; then
                local dir=$(dirname "$1")
                local fn=$(basename "$1")
                echo "$(cd "$dir"; echo "$(pwd -W)/$fn")" | sed 's|/|\\|g';
            else
                if [ -d "$1" ]; then
                    echo "$(cd "$1"; pwd -W)" | sed 's|/|\\|g';
                else
                    echo "$1" | sed 's|^/\(.\)/|\1:\\|g; s|/|\\|g';
                fi
            fi
        fi
    }
    
    0 讨论(0)
  • 2020-12-02 17:44

    My bash foo is weak and I couldn't get regexes working in bash 3.1 so I hacked out a perl script for it:

    #!/bin/env perl
    use strict;
    
    my @r;
    foreach my $e (@ARGV) {
     $e=~s/\//\\/g;
     $e=~s/^\\([A-Za-z])\\/\1:\\/;
     push @r, $e;
    }
    
    print join(" ", @r);
    
    0 讨论(0)
  • 2020-12-02 17:46

    Similar to dmitri-rubinstein@ above, I've cleaned up the code a bit and added the reverse conversion as well.

    winpath() {
        if [ ${#} -eq 0 ]; then
            : skip
        elif [ -f "$1" ]; then
            local dirname=$(dirname "$1")
            local basename=$(basename "$1")
            echo "$(cd "$dirname" && pwd -W)/$basename" \
            | sed \
              -e 's|/|\\|g';
        elif [ -d "$1" ]; then
            echo "$(cd "$1" && pwd -W)" \
            | sed \
              -e 's|/|\\|g';
        else
            echo "$1" \
            | sed \
              -e 's|^/\(.\)/|\1:\\|g' \
              -e 's|/|\\|g'
        fi
    }
    
    unixpath() {
        echo "$1" \
        | sed -r \
          -e 's/\\/\//g' \
          -e 's/^([^:]+):/\/\1/'
    }
    
    0 讨论(0)
提交回复
热议问题