def make_relative_path()

in cbmc_viewer/srcloct.py [0:0]


def make_relative_path(srcfile, srcdir=None, wkdir=None):
    """The relative path to the source file from the source root.

    The source locations appearing in a goto program consist of
      a source file name (goto-cc was invoked on this file)
      a function name,
      a line number, and
      a working directory (goto-cc was invoked in this directory)

    The source file name is either an absolute path or a relative path
    to the source file from the working directory.  This function
    returns a relative path to the source file from the source root.

    The directories srcdir and wkdir must be absolute paths if they
    are given, and srcfile must be an absolute path if wkdir is not
    given.

    """

    assert not srcdir or os.path.isabs(srcdir)
    assert not wkdir or os.path.isabs(wkdir)
    assert srcfile
    assert os.path.isabs(srcfile) or wkdir

    srcdir = normpath(srcdir) if srcdir else None
    wkdir = normpath(wkdir) if wkdir else None
    srcfile = normpath(srcfile)

    path = srcfile
    if is_builtin(path):
        path = builtin_name(path)
    else:
        path = os.path.join(wkdir, path) if wkdir else path
        path = relpath(path, srcdir) if srcdir else path

    return normpath(path)