def do_unpack(d):
    # Export function set
    bb.build.exec_func('base_do_unpack', d)

do_unpack(d)

def base_do_unpack(d):
    import shutil

    sourcedir = d.getVar('S')
    # Intentionally keep SOURCE_BASEDIR internal to the task just for SDE
    d.setVar("SOURCE_BASEDIR", sourcedir)

    src_uri = (d.getVar('SRC_URI') or "").split()
    if not src_uri:
        return

    basedir = None
    unpackdir = d.getVar('UNPACKDIR')
    workdir = d.getVar('WORKDIR')
    if sourcedir.startswith(workdir) and not sourcedir.startswith(unpackdir):
        basedir = sourcedir.replace(workdir, '').strip("/").split('/')[0]
        if basedir:
            bb.utils.remove(workdir + '/' + basedir, True)
            d.setVar("SOURCE_BASEDIR", workdir + '/' + basedir)

    try:
        fetcher = bb.fetch2.Fetch(src_uri, d)
        fetcher.unpack(d.getVar('UNPACKDIR'))
    except bb.fetch2.BBFetchException as e:
        bb.fatal("Bitbake Fetcher Error: " + repr(e))

    if basedir and os.path.exists(unpackdir + '/' + basedir):
        # Compatibility magic to ensure ${WORKDIR}/git and ${WORKDIR}/${BP}
        # as often used in S work as expected.
        shutil.move(unpackdir + '/' + basedir, workdir + '/' + basedir)

