def do_populate_sysroot(d):
    # SYSROOT 'version' 2
    bb.build.exec_func("sysroot_stage_all", d)
    bb.build.exec_func("sysroot_strip", d)
    for f in (d.getVar('SYSROOT_PREPROCESS_FUNCS') or '').split():
        bb.build.exec_func(f, d)
    pn = d.getVar("PN")
    multiprov = d.getVar("BB_MULTI_PROVIDER_ALLOWED").split()
    provdir = d.expand("${SYSROOT_DESTDIR}${base_prefix}/sysroot-providers/")
    bb.utils.mkdirhier(provdir)
    for p in d.getVar("PROVIDES").split():
        if p in multiprov:
            continue
        p = p.replace("/", "_")
        with open(provdir + p, "w") as f:
            f.write(pn)

do_populate_sysroot(d)

def useradd_sysroot_sstate(d):
    for type, sort_prefix in [("group", "01"), ("user", "02"), ("groupmems", "03")]:
        scriptfile = None
        task = d.getVar("BB_CURRENTTASK")
        if task == "package_setscene":
            bb.build.exec_func(type + "add_sysroot", d)
        elif task == "prepare_recipe_sysroot":
            # Used to update this recipe's own sysroot so the user/groups are available to do_install

            # If do_populate_sysroot is triggered and we write the file here, there would be an overlapping
            # files. See usergrouptests.UserGroupTests.test_add_task_between_p_sysroot_and_package
            scriptfile = d.expand("${RECIPE_SYSROOT}${bindir}/postinst-useradd-" + sort_prefix + type + "-${PN}-recipedebug")

            bb.build.exec_func(type + "add_sysroot", d)
        elif task == "populate_sysroot":
            # Used when installed in dependent task sysroots
            scriptfile = d.expand("${SYSROOT_DESTDIR}${bindir}/postinst-useradd-" + sort_prefix + type + "-${PN}")

        if scriptfile:
            bb.utils.mkdirhier(os.path.dirname(scriptfile))
            with open(scriptfile, 'w') as script:
                script.write("#!/bin/sh -e\n")
                bb.data.emit_func(type + "add_sysroot", script, d)
                script.write(type + "add_sysroot\n")
            os.chmod(scriptfile, 0o755)

def sysroot_strip(d):
    inhibit_sysroot = d.getVar('INHIBIT_SYSROOT_STRIP')
    if inhibit_sysroot and oe.types.boolean(inhibit_sysroot):
        return

    dstdir = d.getVar('SYSROOT_DESTDIR')
    pn = d.getVar('PN')
    libdir = d.getVar("libdir")
    base_libdir = d.getVar("base_libdir")
    qa_already_stripped = 'already-stripped' in (d.getVar('INSANE_SKIP:' + pn) or "").split()
    strip_cmd = d.getVar("STRIP")

    max_process = oe.utils.get_bb_number_threads(d)
    oe.package.strip_execs(pn, dstdir, strip_cmd, libdir, base_libdir, max_process,
                           qa_already_stripped=qa_already_stripped)

