On Mon, 5 Aug 2019 at 18:39, chris.laplante@agilent.com <chris.laplante@agilent.com> wrote:
> But I would like something smarter, e.g. first trying to check if the SRC_URI is available, if not switching on using the prebuilt package if available (e.g.
> in a DIR_PREBUILT)...
>
> Before going further is there already an existing solution for that? do you have any recommendation on the easier/best way to
> achieve this?

I don't have a "best practice" recommendation to offer. But coincidentally I suggested something similar for upstreaming a few weeks ago. It was (rightfully) rejected as too "hacky" for inclusion in core code. Regardless, here's a bbclass we currently use:

addhandler skip_eventhandler
python skip_eventhandler() {
    try:
        bb.fetch2.Fetch(d.getVar("SRC_URI").split(), d)
    except bb.fetch2.FetchError:
        raise bb.parse.SkipRecipe("skip-inaccessible: could not access upstream repo; check SRC_URI, access rights, and network availability")
}
skip_eventhandler[eventmask] = "bb.event.RecipePreFinalise"


As was pointed out to me, this is a pretty heavy-handed approach - it can fail for any number of reasons, not just the URI actually being unavailable. So you may consider a more surgical check, e.g. actually trying to fetch the SRC_URI directly using raw GET requests.

To implement the handover, you'd have separate recipes for the build-from-source and the prebuilt case which PROVIDE the same package. You probably also want to set PREFERRED_PROVIDER in your layer conf to prefer the build-from-source recipe.

Alternatively, you can do it all in a single recipe by rewriting the SRC_URI from within the event handler. But I think separate recipes is nicer.

Well, I ended up with the attached class, which generates or install prebuilt tarball. It's only a draft and I'll come back with a polished version, but the idea is to look for the related prebuilt package into a 'PREBUILT_SRC_DIR' (if defined) and install it instead of fetching and compiling from source. This allows having only one recipe. Then a company just needs to provide closed-source software as a set of prebuilt tarballs and customer need to point to the prebuilt dir (e.g. in local.conf).

Regards,
Loic